blob: 19bd12788eb7e6a973f20c6254a884e73089b76e [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
Andrzej Kurek8a045ce2022-12-23 11:00:06 -050055#if defined(MBEDTLS_USE_PSA_CRYPTO)
56#define PSA_TO_MBEDTLS_ERR(status) PSA_TO_MBEDTLS_ERR_LIST(status, \
57 psa_to_ssl_errors, \
58 psa_generic_status_to_mbedtls)
Andrzej Kurekdaf5b562023-03-03 05:52:28 -050059#define PSA_TO_MD_ERR(status) PSA_TO_MBEDTLS_ERR_LIST(status, \
60 psa_to_md_errors, \
61 psa_generic_status_to_mbedtls)
Andrzej Kurek8a045ce2022-12-23 11:00:06 -050062#endif
63
Ronald Cronad8c17b2022-06-10 17:18:09 +020064#if defined(MBEDTLS_TEST_HOOKS)
65static mbedtls_ssl_chk_buf_ptr_args chk_buf_ptr_fail_args;
66
67void mbedtls_ssl_set_chk_buf_ptr_fail_args(
Gilles Peskine449bd832023-01-11 14:50:10 +010068 const uint8_t *cur, const uint8_t *end, size_t need)
Ronald Cronad8c17b2022-06-10 17:18:09 +020069{
70 chk_buf_ptr_fail_args.cur = cur;
71 chk_buf_ptr_fail_args.end = end;
72 chk_buf_ptr_fail_args.need = need;
73}
74
Gilles Peskine449bd832023-01-11 14:50:10 +010075void mbedtls_ssl_reset_chk_buf_ptr_fail_args(void)
Ronald Cronad8c17b2022-06-10 17:18:09 +020076{
Gilles Peskine449bd832023-01-11 14:50:10 +010077 memset(&chk_buf_ptr_fail_args, 0, sizeof(chk_buf_ptr_fail_args));
Ronald Cronad8c17b2022-06-10 17:18:09 +020078}
79
Gilles Peskine449bd832023-01-11 14:50:10 +010080int mbedtls_ssl_cmp_chk_buf_ptr_fail_args(mbedtls_ssl_chk_buf_ptr_args *args)
Ronald Cronad8c17b2022-06-10 17:18:09 +020081{
Gilles Peskine449bd832023-01-11 14:50:10 +010082 return (chk_buf_ptr_fail_args.cur != args->cur) ||
83 (chk_buf_ptr_fail_args.end != args->end) ||
84 (chk_buf_ptr_fail_args.need != args->need);
Ronald Cronad8c17b2022-06-10 17:18:09 +020085}
86#endif /* MBEDTLS_TEST_HOOKS */
87
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020088#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +010089
Hanno Beckera0e20d02019-05-15 14:03:01 +010090#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf8542cf2019-04-09 15:22:03 +010091/* Top-level Connection ID API */
92
Gilles Peskine449bd832023-01-11 14:50:10 +010093int mbedtls_ssl_conf_cid(mbedtls_ssl_config *conf,
94 size_t len,
95 int ignore_other_cid)
Hanno Beckerad4a1372019-05-03 13:06:44 +010096{
Gilles Peskine449bd832023-01-11 14:50:10 +010097 if (len > MBEDTLS_SSL_CID_IN_LEN_MAX) {
98 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
99 }
Hanno Beckerad4a1372019-05-03 13:06:44 +0100100
Gilles Peskine449bd832023-01-11 14:50:10 +0100101 if (ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_FAIL &&
102 ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE) {
103 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Becker611ac772019-05-14 11:45:26 +0100104 }
105
106 conf->ignore_unexpected_cid = ignore_other_cid;
Hanno Beckerad4a1372019-05-03 13:06:44 +0100107 conf->cid_len = len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100108 return 0;
Hanno Beckerad4a1372019-05-03 13:06:44 +0100109}
110
Gilles Peskine449bd832023-01-11 14:50:10 +0100111int mbedtls_ssl_set_cid(mbedtls_ssl_context *ssl,
112 int enable,
113 unsigned char const *own_cid,
114 size_t own_cid_len)
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100115{
Gilles Peskine449bd832023-01-11 14:50:10 +0100116 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
117 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
118 }
Hanno Becker76a79ab2019-05-03 14:38:32 +0100119
Hanno Beckerca092242019-04-25 16:01:49 +0100120 ssl->negotiate_cid = enable;
Gilles Peskine449bd832023-01-11 14:50:10 +0100121 if (enable == MBEDTLS_SSL_CID_DISABLED) {
122 MBEDTLS_SSL_DEBUG_MSG(3, ("Disable use of CID extension."));
123 return 0;
Hanno Beckerca092242019-04-25 16:01:49 +0100124 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100125 MBEDTLS_SSL_DEBUG_MSG(3, ("Enable use of CID extension."));
126 MBEDTLS_SSL_DEBUG_BUF(3, "Own CID", own_cid, own_cid_len);
Hanno Beckerca092242019-04-25 16:01:49 +0100127
Gilles Peskine449bd832023-01-11 14:50:10 +0100128 if (own_cid_len != ssl->conf->cid_len) {
129 MBEDTLS_SSL_DEBUG_MSG(3, ("CID length %u does not match CID length %u in config",
130 (unsigned) own_cid_len,
131 (unsigned) ssl->conf->cid_len));
132 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Beckerca092242019-04-25 16:01:49 +0100133 }
134
Gilles Peskine449bd832023-01-11 14:50:10 +0100135 memcpy(ssl->own_cid, own_cid, own_cid_len);
Hanno Beckerb7ee0cf2019-04-30 14:07:31 +0100136 /* Truncation is not an issue here because
137 * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */
138 ssl->own_cid_len = (uint8_t) own_cid_len;
Hanno Beckerca092242019-04-25 16:01:49 +0100139
Gilles Peskine449bd832023-01-11 14:50:10 +0100140 return 0;
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100141}
142
Gilles Peskine449bd832023-01-11 14:50:10 +0100143int mbedtls_ssl_get_own_cid(mbedtls_ssl_context *ssl,
144 int *enabled,
145 unsigned char own_cid[MBEDTLS_SSL_CID_OUT_LEN_MAX],
146 size_t *own_cid_len)
Paul Elliott0113cf12022-03-11 20:26:47 +0000147{
148 *enabled = MBEDTLS_SSL_CID_DISABLED;
149
Gilles Peskine449bd832023-01-11 14:50:10 +0100150 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
151 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
152 }
Paul Elliott0113cf12022-03-11 20:26:47 +0000153
154 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID length is
155 * zero as this is indistinguishable from not requesting to use
156 * the CID extension. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100157 if (ssl->own_cid_len == 0 || ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED) {
158 return 0;
159 }
Paul Elliott0113cf12022-03-11 20:26:47 +0000160
Gilles Peskine449bd832023-01-11 14:50:10 +0100161 if (own_cid_len != NULL) {
Paul Elliott0113cf12022-03-11 20:26:47 +0000162 *own_cid_len = ssl->own_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100163 if (own_cid != NULL) {
164 memcpy(own_cid, ssl->own_cid, ssl->own_cid_len);
165 }
Paul Elliott0113cf12022-03-11 20:26:47 +0000166 }
167
168 *enabled = MBEDTLS_SSL_CID_ENABLED;
169
Gilles Peskine449bd832023-01-11 14:50:10 +0100170 return 0;
Paul Elliott0113cf12022-03-11 20:26:47 +0000171}
172
Gilles Peskine449bd832023-01-11 14:50:10 +0100173int mbedtls_ssl_get_peer_cid(mbedtls_ssl_context *ssl,
174 int *enabled,
175 unsigned char peer_cid[MBEDTLS_SSL_CID_OUT_LEN_MAX],
176 size_t *peer_cid_len)
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100177{
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100178 *enabled = MBEDTLS_SSL_CID_DISABLED;
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100179
Gilles Peskine449bd832023-01-11 14:50:10 +0100180 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
181 mbedtls_ssl_is_handshake_over(ssl) == 0) {
182 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Becker76a79ab2019-05-03 14:38:32 +0100183 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100184
Hanno Beckerc5f24222019-05-03 12:54:52 +0100185 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions
186 * were used, but client and server requested the empty CID.
187 * This is indistinguishable from not using the CID extension
188 * in the first place. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100189 if (ssl->transform_in->in_cid_len == 0 &&
190 ssl->transform_in->out_cid_len == 0) {
191 return 0;
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100192 }
193
Gilles Peskine449bd832023-01-11 14:50:10 +0100194 if (peer_cid_len != NULL) {
Hanno Becker615ef172019-05-22 16:50:35 +0100195 *peer_cid_len = ssl->transform_in->out_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100196 if (peer_cid != NULL) {
197 memcpy(peer_cid, ssl->transform_in->out_cid,
198 ssl->transform_in->out_cid_len);
Hanno Becker615ef172019-05-22 16:50:35 +0100199 }
200 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100201
202 *enabled = MBEDTLS_SSL_CID_ENABLED;
203
Gilles Peskine449bd832023-01-11 14:50:10 +0100204 return 0;
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100205}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100206#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200208#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200209
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200210#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200211/*
212 * Convert max_fragment_length codes to length.
213 * RFC 6066 says:
214 * enum{
215 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
216 * } MaxFragmentLength;
217 * and we add 0 -> extension unused
218 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100219static unsigned int ssl_mfl_code_to_length(int mfl)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200220{
Gilles Peskine449bd832023-01-11 14:50:10 +0100221 switch (mfl) {
222 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
223 return MBEDTLS_TLS_EXT_ADV_CONTENT_LEN;
224 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
225 return 512;
226 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
227 return 1024;
228 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
229 return 2048;
230 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
231 return 4096;
232 default:
233 return MBEDTLS_TLS_EXT_ADV_CONTENT_LEN;
Angus Grattond8213d02016-05-25 20:56:48 +1000234 }
235}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200236#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200237
Gilles Peskine449bd832023-01-11 14:50:10 +0100238int mbedtls_ssl_session_copy(mbedtls_ssl_session *dst,
239 const mbedtls_ssl_session *src)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200240{
Gilles Peskine449bd832023-01-11 14:50:10 +0100241 mbedtls_ssl_session_free(dst);
242 memcpy(dst, src, sizeof(mbedtls_ssl_session));
吴敬辉0b716112021-11-29 10:46:35 +0800243#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
244 dst->ticket = NULL;
Xiaokang Qian87306442022-10-12 09:47:38 +0000245#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
246 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaokang Qian126bf8e2022-10-13 02:22:40 +0000247 dst->hostname = NULL;
吴敬辉0b716112021-11-29 10:46:35 +0800248#endif
Xiaokang Qian87306442022-10-12 09:47:38 +0000249#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
吴敬辉0b716112021-11-29 10:46:35 +0800250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200251#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker6d1986e2019-02-07 12:27:42 +0000252
253#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100254 if (src->peer_cert != NULL) {
Janos Follath865b3eb2019-12-16 11:46:15 +0000255 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker2292d1f2013-09-15 17:06:49 +0200256
Gilles Peskine449bd832023-01-11 14:50:10 +0100257 dst->peer_cert = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
258 if (dst->peer_cert == NULL) {
259 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
260 }
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200261
Gilles Peskine449bd832023-01-11 14:50:10 +0100262 mbedtls_x509_crt_init(dst->peer_cert);
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200263
Gilles Peskine449bd832023-01-11 14:50:10 +0100264 if ((ret = mbedtls_x509_crt_parse_der(dst->peer_cert, src->peer_cert->raw.p,
265 src->peer_cert->raw.len)) != 0) {
266 mbedtls_free(dst->peer_cert);
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200267 dst->peer_cert = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100268 return ret;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200269 }
270 }
Hanno Becker6d1986e2019-02-07 12:27:42 +0000271#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100272 if (src->peer_cert_digest != NULL) {
Hanno Becker9198ad12019-02-05 17:00:50 +0000273 dst->peer_cert_digest =
Gilles Peskine449bd832023-01-11 14:50:10 +0100274 mbedtls_calloc(1, src->peer_cert_digest_len);
275 if (dst->peer_cert_digest == NULL) {
276 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
277 }
Hanno Becker9198ad12019-02-05 17:00:50 +0000278
Gilles Peskine449bd832023-01-11 14:50:10 +0100279 memcpy(dst->peer_cert_digest, src->peer_cert_digest,
280 src->peer_cert_digest_len);
Hanno Becker9198ad12019-02-05 17:00:50 +0000281 dst->peer_cert_digest_type = src->peer_cert_digest_type;
Hanno Beckeraccc5992019-02-25 10:06:59 +0000282 dst->peer_cert_digest_len = src->peer_cert_digest_len;
Hanno Becker9198ad12019-02-05 17:00:50 +0000283 }
284#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
285
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200286#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200287
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200288#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100289 if (src->ticket != NULL) {
290 dst->ticket = mbedtls_calloc(1, src->ticket_len);
291 if (dst->ticket == NULL) {
292 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
293 }
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200294
Gilles Peskine449bd832023-01-11 14:50:10 +0100295 memcpy(dst->ticket, src->ticket, src->ticket_len);
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200296 }
Xiaokang Qian126bf8e2022-10-13 02:22:40 +0000297
298#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
299 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +0100300 if (src->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Xiaokang Qian126bf8e2022-10-13 02:22:40 +0000301 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100302 ret = mbedtls_ssl_session_set_hostname(dst, src->hostname);
303 if (ret != 0) {
304 return ret;
305 }
Xiaokang Qian126bf8e2022-10-13 02:22:40 +0000306 }
307#endif /* MBEDTLS_SSL_PROTO_TLS1_3 &&
308 MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200309#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200310
Gilles Peskine449bd832023-01-11 14:50:10 +0100311 return 0;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200312}
313
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500314#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200315MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100316static int resize_buffer(unsigned char **buffer, size_t len_new, size_t *len_old)
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500317{
Gilles Peskine449bd832023-01-11 14:50:10 +0100318 unsigned char *resized_buffer = mbedtls_calloc(1, len_new);
319 if (resized_buffer == NULL) {
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500320 return -1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100321 }
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500322
323 /* We want to copy len_new bytes when downsizing the buffer, and
324 * len_old bytes when upsizing, so we choose the smaller of two sizes,
325 * to fit one buffer into another. Size checks, ensuring that no data is
326 * lost, are done outside of this function. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100327 memcpy(resized_buffer, *buffer,
328 (len_new < *len_old) ? len_new : *len_old);
329 mbedtls_platform_zeroize(*buffer, *len_old);
330 mbedtls_free(*buffer);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500331
332 *buffer = resized_buffer;
333 *len_old = len_new;
334
335 return 0;
336}
Andrzej Kurek4a063792020-10-21 15:08:44 +0200337
Gilles Peskine449bd832023-01-11 14:50:10 +0100338static void handle_buffer_resizing(mbedtls_ssl_context *ssl, int downsizing,
339 size_t in_buf_new_len,
340 size_t out_buf_new_len)
Andrzej Kurek4a063792020-10-21 15:08:44 +0200341{
342 int modified = 0;
343 size_t written_in = 0, iv_offset_in = 0, len_offset_in = 0;
344 size_t written_out = 0, iv_offset_out = 0, len_offset_out = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100345 if (ssl->in_buf != NULL) {
Andrzej Kurek4a063792020-10-21 15:08:44 +0200346 written_in = ssl->in_msg - ssl->in_buf;
347 iv_offset_in = ssl->in_iv - ssl->in_buf;
348 len_offset_in = ssl->in_len - ssl->in_buf;
Gilles Peskine449bd832023-01-11 14:50:10 +0100349 if (downsizing ?
Andrzej Kurek4a063792020-10-21 15:08:44 +0200350 ssl->in_buf_len > in_buf_new_len && ssl->in_left < in_buf_new_len :
Gilles Peskine449bd832023-01-11 14:50:10 +0100351 ssl->in_buf_len < in_buf_new_len) {
352 if (resize_buffer(&ssl->in_buf, in_buf_new_len, &ssl->in_buf_len) != 0) {
353 MBEDTLS_SSL_DEBUG_MSG(1, ("input buffer resizing failed - out of memory"));
354 } else {
355 MBEDTLS_SSL_DEBUG_MSG(2, ("Reallocating in_buf to %" MBEDTLS_PRINTF_SIZET,
356 in_buf_new_len));
Andrzej Kurek4a063792020-10-21 15:08:44 +0200357 modified = 1;
358 }
359 }
360 }
361
Gilles Peskine449bd832023-01-11 14:50:10 +0100362 if (ssl->out_buf != NULL) {
Andrzej Kurek4a063792020-10-21 15:08:44 +0200363 written_out = ssl->out_msg - ssl->out_buf;
364 iv_offset_out = ssl->out_iv - ssl->out_buf;
365 len_offset_out = ssl->out_len - ssl->out_buf;
Gilles Peskine449bd832023-01-11 14:50:10 +0100366 if (downsizing ?
Andrzej Kurek4a063792020-10-21 15:08:44 +0200367 ssl->out_buf_len > out_buf_new_len && ssl->out_left < out_buf_new_len :
Gilles Peskine449bd832023-01-11 14:50:10 +0100368 ssl->out_buf_len < out_buf_new_len) {
369 if (resize_buffer(&ssl->out_buf, out_buf_new_len, &ssl->out_buf_len) != 0) {
370 MBEDTLS_SSL_DEBUG_MSG(1, ("output buffer resizing failed - out of memory"));
371 } else {
372 MBEDTLS_SSL_DEBUG_MSG(2, ("Reallocating out_buf to %" MBEDTLS_PRINTF_SIZET,
373 out_buf_new_len));
Andrzej Kurek4a063792020-10-21 15:08:44 +0200374 modified = 1;
375 }
376 }
377 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100378 if (modified) {
Andrzej Kurek4a063792020-10-21 15:08:44 +0200379 /* Update pointers here to avoid doing it twice. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100380 mbedtls_ssl_reset_in_out_pointers(ssl);
Andrzej Kurek4a063792020-10-21 15:08:44 +0200381 /* Fields below might not be properly updated with record
382 * splitting or with CID, so they are manually updated here. */
383 ssl->out_msg = ssl->out_buf + written_out;
384 ssl->out_len = ssl->out_buf + len_offset_out;
385 ssl->out_iv = ssl->out_buf + iv_offset_out;
386
387 ssl->in_msg = ssl->in_buf + written_in;
388 ssl->in_len = ssl->in_buf + len_offset_in;
389 ssl->in_iv = ssl->in_buf + iv_offset_in;
390 }
391}
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500392#endif /* MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH */
393
Jerry Yudb8c48a2022-01-27 14:54:54 +0800394#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yued14c932022-02-17 13:40:45 +0800395
396#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Gilles Peskine449bd832023-01-11 14:50:10 +0100397typedef int (*tls_prf_fn)(const unsigned char *secret, size_t slen,
398 const char *label,
399 const unsigned char *random, size_t rlen,
400 unsigned char *dstbuf, size_t dlen);
Jerry Yued14c932022-02-17 13:40:45 +0800401
Gilles Peskine449bd832023-01-11 14:50:10 +0100402static tls_prf_fn ssl_tls12prf_from_cs(int ciphersuite_id);
Jerry Yued14c932022-02-17 13:40:45 +0800403
404#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
405
406/* Type for the TLS PRF */
407typedef int ssl_tls_prf_t(const unsigned char *, size_t, const char *,
408 const unsigned char *, size_t,
409 unsigned char *, size_t);
410
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200411MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100412static int ssl_tls12_populate_transform(mbedtls_ssl_transform *transform,
413 int ciphersuite,
414 const unsigned char master[48],
Neil Armstrongf2c82f02022-04-05 11:16:53 +0200415#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +0100416 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +0200417#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +0100418 ssl_tls_prf_t tls_prf,
419 const unsigned char randbytes[64],
420 mbedtls_ssl_protocol_version tls_version,
421 unsigned endpoint,
422 const mbedtls_ssl_context *ssl);
Jerry Yued14c932022-02-17 13:40:45 +0800423
Andrzej Kurek25f27152022-08-17 16:09:31 -0400424#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200425MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100426static int tls_prf_sha256(const unsigned char *secret, size_t slen,
Ron Eldor51d3ab52019-05-12 14:54:30 +0300427 const char *label,
428 const unsigned char *random, size_t rlen,
Gilles Peskine449bd832023-01-11 14:50:10 +0100429 unsigned char *dstbuf, size_t dlen);
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100430static int ssl_calc_verify_tls_sha256(const mbedtls_ssl_context *, unsigned char *, size_t *);
431static int ssl_calc_finished_tls_sha256(mbedtls_ssl_context *, unsigned char *, int);
Gilles Peskine449bd832023-01-11 14:50:10 +0100432
433#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
434
435#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
436MBEDTLS_CHECK_RETURN_CRITICAL
437static int tls_prf_sha384(const unsigned char *secret, size_t slen,
438 const char *label,
439 const unsigned char *random, size_t rlen,
440 unsigned char *dstbuf, size_t dlen);
441
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100442static int ssl_calc_verify_tls_sha384(const mbedtls_ssl_context *, unsigned char *, size_t *);
443static int ssl_calc_finished_tls_sha384(mbedtls_ssl_context *, unsigned char *, int);
Gilles Peskine449bd832023-01-11 14:50:10 +0100444#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
445
446static size_t ssl_tls12_session_save(const mbedtls_ssl_session *session,
447 unsigned char *buf,
448 size_t buf_len);
449
450MBEDTLS_CHECK_RETURN_CRITICAL
451static int ssl_tls12_session_load(mbedtls_ssl_session *session,
452 const unsigned char *buf,
453 size_t len);
454#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
455
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100456static int ssl_update_checksum_start(mbedtls_ssl_context *, const unsigned char *, size_t);
Gilles Peskine449bd832023-01-11 14:50:10 +0100457
458#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100459static int ssl_update_checksum_sha256(mbedtls_ssl_context *, const unsigned char *, size_t);
Gilles Peskine449bd832023-01-11 14:50:10 +0100460#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
461
462#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100463static int ssl_update_checksum_sha384(mbedtls_ssl_context *, const unsigned char *, size_t);
Gilles Peskine449bd832023-01-11 14:50:10 +0100464#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
465
466int mbedtls_ssl_tls_prf(const mbedtls_tls_prf_types prf,
467 const unsigned char *secret, size_t slen,
468 const char *label,
469 const unsigned char *random, size_t rlen,
470 unsigned char *dstbuf, size_t dlen)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300471{
472 mbedtls_ssl_tls_prf_cb *tls_prf = NULL;
473
Gilles Peskine449bd832023-01-11 14:50:10 +0100474 switch (prf) {
Ron Eldord2f25f72019-05-15 14:54:22 +0300475#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek25f27152022-08-17 16:09:31 -0400476#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300477 case MBEDTLS_SSL_TLS_PRF_SHA384:
478 tls_prf = tls_prf_sha384;
Gilles Peskine449bd832023-01-11 14:50:10 +0100479 break;
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400480#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -0400481#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300482 case MBEDTLS_SSL_TLS_PRF_SHA256:
483 tls_prf = tls_prf_sha256;
Gilles Peskine449bd832023-01-11 14:50:10 +0100484 break;
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400485#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Ron Eldord2f25f72019-05-15 14:54:22 +0300486#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100487 default:
488 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Ron Eldor51d3ab52019-05-12 14:54:30 +0300489 }
490
Gilles Peskine449bd832023-01-11 14:50:10 +0100491 return tls_prf(secret, slen, label, random, rlen, dstbuf, dlen);
Ron Eldor51d3ab52019-05-12 14:54:30 +0300492}
493
Jerry Yuc73c6182022-02-08 20:29:25 +0800494#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100495static void ssl_clear_peer_cert(mbedtls_ssl_session *session)
Jerry Yuc73c6182022-02-08 20:29:25 +0800496{
497#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100498 if (session->peer_cert != NULL) {
499 mbedtls_x509_crt_free(session->peer_cert);
500 mbedtls_free(session->peer_cert);
Jerry Yuc73c6182022-02-08 20:29:25 +0800501 session->peer_cert = NULL;
502 }
503#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100504 if (session->peer_cert_digest != NULL) {
Jerry Yuc73c6182022-02-08 20:29:25 +0800505 /* Zeroization is not necessary. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100506 mbedtls_free(session->peer_cert_digest);
Jerry Yuc73c6182022-02-08 20:29:25 +0800507 session->peer_cert_digest = NULL;
508 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
509 session->peer_cert_digest_len = 0;
510 }
511#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
512}
513#endif /* MBEDTLS_X509_CRT_PARSE_C */
514
Gilles Peskine449bd832023-01-11 14:50:10 +0100515uint32_t mbedtls_ssl_get_extension_id(unsigned int extension_type)
Jerry Yu7a485c12022-10-31 13:08:18 +0800516{
Gilles Peskine449bd832023-01-11 14:50:10 +0100517 switch (extension_type) {
Jerry Yu7a485c12022-10-31 13:08:18 +0800518 case MBEDTLS_TLS_EXT_SERVERNAME:
Gilles Peskine449bd832023-01-11 14:50:10 +0100519 return MBEDTLS_SSL_EXT_ID_SERVERNAME;
Jerry Yu7a485c12022-10-31 13:08:18 +0800520
521 case MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100522 return MBEDTLS_SSL_EXT_ID_MAX_FRAGMENT_LENGTH;
Jerry Yu7a485c12022-10-31 13:08:18 +0800523
524 case MBEDTLS_TLS_EXT_STATUS_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +0100525 return MBEDTLS_SSL_EXT_ID_STATUS_REQUEST;
Jerry Yu7a485c12022-10-31 13:08:18 +0800526
527 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
Gilles Peskine449bd832023-01-11 14:50:10 +0100528 return MBEDTLS_SSL_EXT_ID_SUPPORTED_GROUPS;
Jerry Yu7a485c12022-10-31 13:08:18 +0800529
530 case MBEDTLS_TLS_EXT_SIG_ALG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100531 return MBEDTLS_SSL_EXT_ID_SIG_ALG;
Jerry Yu7a485c12022-10-31 13:08:18 +0800532
533 case MBEDTLS_TLS_EXT_USE_SRTP:
Gilles Peskine449bd832023-01-11 14:50:10 +0100534 return MBEDTLS_SSL_EXT_ID_USE_SRTP;
Jerry Yu7a485c12022-10-31 13:08:18 +0800535
536 case MBEDTLS_TLS_EXT_HEARTBEAT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100537 return MBEDTLS_SSL_EXT_ID_HEARTBEAT;
Jerry Yu7a485c12022-10-31 13:08:18 +0800538
539 case MBEDTLS_TLS_EXT_ALPN:
Gilles Peskine449bd832023-01-11 14:50:10 +0100540 return MBEDTLS_SSL_EXT_ID_ALPN;
Jerry Yu7a485c12022-10-31 13:08:18 +0800541
542 case MBEDTLS_TLS_EXT_SCT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100543 return MBEDTLS_SSL_EXT_ID_SCT;
Jerry Yu7a485c12022-10-31 13:08:18 +0800544
545 case MBEDTLS_TLS_EXT_CLI_CERT_TYPE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100546 return MBEDTLS_SSL_EXT_ID_CLI_CERT_TYPE;
Jerry Yu7a485c12022-10-31 13:08:18 +0800547
548 case MBEDTLS_TLS_EXT_SERV_CERT_TYPE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100549 return MBEDTLS_SSL_EXT_ID_SERV_CERT_TYPE;
Jerry Yu7a485c12022-10-31 13:08:18 +0800550
551 case MBEDTLS_TLS_EXT_PADDING:
Gilles Peskine449bd832023-01-11 14:50:10 +0100552 return MBEDTLS_SSL_EXT_ID_PADDING;
Jerry Yu7a485c12022-10-31 13:08:18 +0800553
554 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100555 return MBEDTLS_SSL_EXT_ID_PRE_SHARED_KEY;
Jerry Yu7a485c12022-10-31 13:08:18 +0800556
557 case MBEDTLS_TLS_EXT_EARLY_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100558 return MBEDTLS_SSL_EXT_ID_EARLY_DATA;
Jerry Yu7a485c12022-10-31 13:08:18 +0800559
560 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
Gilles Peskine449bd832023-01-11 14:50:10 +0100561 return MBEDTLS_SSL_EXT_ID_SUPPORTED_VERSIONS;
Jerry Yu7a485c12022-10-31 13:08:18 +0800562
563 case MBEDTLS_TLS_EXT_COOKIE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100564 return MBEDTLS_SSL_EXT_ID_COOKIE;
Jerry Yu7a485c12022-10-31 13:08:18 +0800565
566 case MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES:
Gilles Peskine449bd832023-01-11 14:50:10 +0100567 return MBEDTLS_SSL_EXT_ID_PSK_KEY_EXCHANGE_MODES;
Jerry Yu7a485c12022-10-31 13:08:18 +0800568
569 case MBEDTLS_TLS_EXT_CERT_AUTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100570 return MBEDTLS_SSL_EXT_ID_CERT_AUTH;
Jerry Yu7a485c12022-10-31 13:08:18 +0800571
572 case MBEDTLS_TLS_EXT_OID_FILTERS:
Gilles Peskine449bd832023-01-11 14:50:10 +0100573 return MBEDTLS_SSL_EXT_ID_OID_FILTERS;
Jerry Yu7a485c12022-10-31 13:08:18 +0800574
575 case MBEDTLS_TLS_EXT_POST_HANDSHAKE_AUTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100576 return MBEDTLS_SSL_EXT_ID_POST_HANDSHAKE_AUTH;
Jerry Yu7a485c12022-10-31 13:08:18 +0800577
578 case MBEDTLS_TLS_EXT_SIG_ALG_CERT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100579 return MBEDTLS_SSL_EXT_ID_SIG_ALG_CERT;
Jerry Yu7a485c12022-10-31 13:08:18 +0800580
581 case MBEDTLS_TLS_EXT_KEY_SHARE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100582 return MBEDTLS_SSL_EXT_ID_KEY_SHARE;
Jerry Yu7a485c12022-10-31 13:08:18 +0800583
584 case MBEDTLS_TLS_EXT_TRUNCATED_HMAC:
Gilles Peskine449bd832023-01-11 14:50:10 +0100585 return MBEDTLS_SSL_EXT_ID_TRUNCATED_HMAC;
Jerry Yu7a485c12022-10-31 13:08:18 +0800586
587 case MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS:
Gilles Peskine449bd832023-01-11 14:50:10 +0100588 return MBEDTLS_SSL_EXT_ID_SUPPORTED_POINT_FORMATS;
Jerry Yu7a485c12022-10-31 13:08:18 +0800589
590 case MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC:
Gilles Peskine449bd832023-01-11 14:50:10 +0100591 return MBEDTLS_SSL_EXT_ID_ENCRYPT_THEN_MAC;
Jerry Yu7a485c12022-10-31 13:08:18 +0800592
593 case MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +0100594 return MBEDTLS_SSL_EXT_ID_EXTENDED_MASTER_SECRET;
Jerry Yu7a485c12022-10-31 13:08:18 +0800595
596 case MBEDTLS_TLS_EXT_SESSION_TICKET:
Gilles Peskine449bd832023-01-11 14:50:10 +0100597 return MBEDTLS_SSL_EXT_ID_SESSION_TICKET;
Jerry Yu7a485c12022-10-31 13:08:18 +0800598
599 }
600
Gilles Peskine449bd832023-01-11 14:50:10 +0100601 return MBEDTLS_SSL_EXT_ID_UNRECOGNIZED;
Jerry Yu7a485c12022-10-31 13:08:18 +0800602}
603
Gilles Peskine449bd832023-01-11 14:50:10 +0100604uint32_t mbedtls_ssl_get_extension_mask(unsigned int extension_type)
Jerry Yu7a485c12022-10-31 13:08:18 +0800605{
Gilles Peskine449bd832023-01-11 14:50:10 +0100606 return 1 << mbedtls_ssl_get_extension_id(extension_type);
Jerry Yu7a485c12022-10-31 13:08:18 +0800607}
608
Jerry Yud25cab02022-10-31 12:48:30 +0800609#if defined(MBEDTLS_DEBUG_C)
610static const char *extension_name_table[] = {
Jerry Yuea52ed92022-11-08 21:01:17 +0800611 [MBEDTLS_SSL_EXT_ID_UNRECOGNIZED] = "unrecognized",
Jerry Yud25cab02022-10-31 12:48:30 +0800612 [MBEDTLS_SSL_EXT_ID_SERVERNAME] = "server_name",
613 [MBEDTLS_SSL_EXT_ID_MAX_FRAGMENT_LENGTH] = "max_fragment_length",
614 [MBEDTLS_SSL_EXT_ID_STATUS_REQUEST] = "status_request",
615 [MBEDTLS_SSL_EXT_ID_SUPPORTED_GROUPS] = "supported_groups",
616 [MBEDTLS_SSL_EXT_ID_SIG_ALG] = "signature_algorithms",
617 [MBEDTLS_SSL_EXT_ID_USE_SRTP] = "use_srtp",
618 [MBEDTLS_SSL_EXT_ID_HEARTBEAT] = "heartbeat",
619 [MBEDTLS_SSL_EXT_ID_ALPN] = "application_layer_protocol_negotiation",
620 [MBEDTLS_SSL_EXT_ID_SCT] = "signed_certificate_timestamp",
621 [MBEDTLS_SSL_EXT_ID_CLI_CERT_TYPE] = "client_certificate_type",
622 [MBEDTLS_SSL_EXT_ID_SERV_CERT_TYPE] = "server_certificate_type",
623 [MBEDTLS_SSL_EXT_ID_PADDING] = "padding",
624 [MBEDTLS_SSL_EXT_ID_PRE_SHARED_KEY] = "pre_shared_key",
625 [MBEDTLS_SSL_EXT_ID_EARLY_DATA] = "early_data",
626 [MBEDTLS_SSL_EXT_ID_SUPPORTED_VERSIONS] = "supported_versions",
627 [MBEDTLS_SSL_EXT_ID_COOKIE] = "cookie",
628 [MBEDTLS_SSL_EXT_ID_PSK_KEY_EXCHANGE_MODES] = "psk_key_exchange_modes",
629 [MBEDTLS_SSL_EXT_ID_CERT_AUTH] = "certificate_authorities",
630 [MBEDTLS_SSL_EXT_ID_OID_FILTERS] = "oid_filters",
631 [MBEDTLS_SSL_EXT_ID_POST_HANDSHAKE_AUTH] = "post_handshake_auth",
632 [MBEDTLS_SSL_EXT_ID_SIG_ALG_CERT] = "signature_algorithms_cert",
633 [MBEDTLS_SSL_EXT_ID_KEY_SHARE] = "key_share",
634 [MBEDTLS_SSL_EXT_ID_TRUNCATED_HMAC] = "truncated_hmac",
635 [MBEDTLS_SSL_EXT_ID_SUPPORTED_POINT_FORMATS] = "supported_point_formats",
636 [MBEDTLS_SSL_EXT_ID_ENCRYPT_THEN_MAC] = "encrypt_then_mac",
637 [MBEDTLS_SSL_EXT_ID_EXTENDED_MASTER_SECRET] = "extended_master_secret",
638 [MBEDTLS_SSL_EXT_ID_SESSION_TICKET] = "session_ticket"
639};
640
Gilles Peskine449bd832023-01-11 14:50:10 +0100641static unsigned int extension_type_table[] = {
Jerry Yud25cab02022-10-31 12:48:30 +0800642 [MBEDTLS_SSL_EXT_ID_UNRECOGNIZED] = 0xff,
643 [MBEDTLS_SSL_EXT_ID_SERVERNAME] = MBEDTLS_TLS_EXT_SERVERNAME,
644 [MBEDTLS_SSL_EXT_ID_MAX_FRAGMENT_LENGTH] = MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH,
645 [MBEDTLS_SSL_EXT_ID_STATUS_REQUEST] = MBEDTLS_TLS_EXT_STATUS_REQUEST,
646 [MBEDTLS_SSL_EXT_ID_SUPPORTED_GROUPS] = MBEDTLS_TLS_EXT_SUPPORTED_GROUPS,
647 [MBEDTLS_SSL_EXT_ID_SIG_ALG] = MBEDTLS_TLS_EXT_SIG_ALG,
648 [MBEDTLS_SSL_EXT_ID_USE_SRTP] = MBEDTLS_TLS_EXT_USE_SRTP,
649 [MBEDTLS_SSL_EXT_ID_HEARTBEAT] = MBEDTLS_TLS_EXT_HEARTBEAT,
650 [MBEDTLS_SSL_EXT_ID_ALPN] = MBEDTLS_TLS_EXT_ALPN,
651 [MBEDTLS_SSL_EXT_ID_SCT] = MBEDTLS_TLS_EXT_SCT,
652 [MBEDTLS_SSL_EXT_ID_CLI_CERT_TYPE] = MBEDTLS_TLS_EXT_CLI_CERT_TYPE,
653 [MBEDTLS_SSL_EXT_ID_SERV_CERT_TYPE] = MBEDTLS_TLS_EXT_SERV_CERT_TYPE,
654 [MBEDTLS_SSL_EXT_ID_PADDING] = MBEDTLS_TLS_EXT_PADDING,
655 [MBEDTLS_SSL_EXT_ID_PRE_SHARED_KEY] = MBEDTLS_TLS_EXT_PRE_SHARED_KEY,
656 [MBEDTLS_SSL_EXT_ID_EARLY_DATA] = MBEDTLS_TLS_EXT_EARLY_DATA,
657 [MBEDTLS_SSL_EXT_ID_SUPPORTED_VERSIONS] = MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS,
658 [MBEDTLS_SSL_EXT_ID_COOKIE] = MBEDTLS_TLS_EXT_COOKIE,
659 [MBEDTLS_SSL_EXT_ID_PSK_KEY_EXCHANGE_MODES] = MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES,
660 [MBEDTLS_SSL_EXT_ID_CERT_AUTH] = MBEDTLS_TLS_EXT_CERT_AUTH,
661 [MBEDTLS_SSL_EXT_ID_OID_FILTERS] = MBEDTLS_TLS_EXT_OID_FILTERS,
662 [MBEDTLS_SSL_EXT_ID_POST_HANDSHAKE_AUTH] = MBEDTLS_TLS_EXT_POST_HANDSHAKE_AUTH,
663 [MBEDTLS_SSL_EXT_ID_SIG_ALG_CERT] = MBEDTLS_TLS_EXT_SIG_ALG_CERT,
664 [MBEDTLS_SSL_EXT_ID_KEY_SHARE] = MBEDTLS_TLS_EXT_KEY_SHARE,
665 [MBEDTLS_SSL_EXT_ID_TRUNCATED_HMAC] = MBEDTLS_TLS_EXT_TRUNCATED_HMAC,
666 [MBEDTLS_SSL_EXT_ID_SUPPORTED_POINT_FORMATS] = MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS,
667 [MBEDTLS_SSL_EXT_ID_ENCRYPT_THEN_MAC] = MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC,
668 [MBEDTLS_SSL_EXT_ID_EXTENDED_MASTER_SECRET] = MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET,
669 [MBEDTLS_SSL_EXT_ID_SESSION_TICKET] = MBEDTLS_TLS_EXT_SESSION_TICKET
670};
671
Gilles Peskine449bd832023-01-11 14:50:10 +0100672const char *mbedtls_ssl_get_extension_name(unsigned int extension_type)
Jerry Yud25cab02022-10-31 12:48:30 +0800673{
Gilles Peskine449bd832023-01-11 14:50:10 +0100674 return extension_name_table[
675 mbedtls_ssl_get_extension_id(extension_type)];
Jerry Yud25cab02022-10-31 12:48:30 +0800676}
677
Gilles Peskine449bd832023-01-11 14:50:10 +0100678static const char *ssl_tls13_get_hs_msg_name(int hs_msg_type)
Jerry Yud25cab02022-10-31 12:48:30 +0800679{
Gilles Peskine449bd832023-01-11 14:50:10 +0100680 switch (hs_msg_type) {
Jerry Yud25cab02022-10-31 12:48:30 +0800681 case MBEDTLS_SSL_HS_CLIENT_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +0100682 return "ClientHello";
Jerry Yud25cab02022-10-31 12:48:30 +0800683 case MBEDTLS_SSL_HS_SERVER_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +0100684 return "ServerHello";
Jerry Yud25cab02022-10-31 12:48:30 +0800685 case MBEDTLS_SSL_TLS1_3_HS_HELLO_RETRY_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +0100686 return "HelloRetryRequest";
Jerry Yud25cab02022-10-31 12:48:30 +0800687 case MBEDTLS_SSL_HS_NEW_SESSION_TICKET:
Gilles Peskine449bd832023-01-11 14:50:10 +0100688 return "NewSessionTicket";
Jerry Yud25cab02022-10-31 12:48:30 +0800689 case MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS:
Gilles Peskine449bd832023-01-11 14:50:10 +0100690 return "EncryptedExtensions";
Jerry Yud25cab02022-10-31 12:48:30 +0800691 case MBEDTLS_SSL_HS_CERTIFICATE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100692 return "Certificate";
Jerry Yud25cab02022-10-31 12:48:30 +0800693 case MBEDTLS_SSL_HS_CERTIFICATE_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +0100694 return "CertificateRequest";
Jerry Yud25cab02022-10-31 12:48:30 +0800695 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100696 return "Unknown";
Jerry Yud25cab02022-10-31 12:48:30 +0800697}
698
Gilles Peskine449bd832023-01-11 14:50:10 +0100699void mbedtls_ssl_print_extension(const mbedtls_ssl_context *ssl,
700 int level, const char *file, int line,
701 int hs_msg_type, unsigned int extension_type,
702 const char *extra_msg0, const char *extra_msg1)
Jerry Yud25cab02022-10-31 12:48:30 +0800703{
704 const char *extra_msg;
Gilles Peskine449bd832023-01-11 14:50:10 +0100705 if (extra_msg0 && extra_msg1) {
Jerry Yud25cab02022-10-31 12:48:30 +0800706 mbedtls_debug_print_msg(
707 ssl, level, file, line,
708 "%s: %s(%u) extension %s %s.",
Gilles Peskine449bd832023-01-11 14:50:10 +0100709 ssl_tls13_get_hs_msg_name(hs_msg_type),
710 mbedtls_ssl_get_extension_name(extension_type),
Jerry Yud25cab02022-10-31 12:48:30 +0800711 extension_type,
Gilles Peskine449bd832023-01-11 14:50:10 +0100712 extra_msg0, extra_msg1);
Jerry Yud25cab02022-10-31 12:48:30 +0800713 return;
714 }
715
716 extra_msg = extra_msg0 ? extra_msg0 : extra_msg1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100717 if (extra_msg) {
Jerry Yud25cab02022-10-31 12:48:30 +0800718 mbedtls_debug_print_msg(
719 ssl, level, file, line,
Gilles Peskine449bd832023-01-11 14:50:10 +0100720 "%s: %s(%u) extension %s.", ssl_tls13_get_hs_msg_name(hs_msg_type),
721 mbedtls_ssl_get_extension_name(extension_type), extension_type,
722 extra_msg);
Jerry Yud25cab02022-10-31 12:48:30 +0800723 return;
724 }
725
726 mbedtls_debug_print_msg(
727 ssl, level, file, line,
Gilles Peskine449bd832023-01-11 14:50:10 +0100728 "%s: %s(%u) extension.", ssl_tls13_get_hs_msg_name(hs_msg_type),
729 mbedtls_ssl_get_extension_name(extension_type), extension_type);
Jerry Yud25cab02022-10-31 12:48:30 +0800730}
731
Gilles Peskine449bd832023-01-11 14:50:10 +0100732void mbedtls_ssl_print_extensions(const mbedtls_ssl_context *ssl,
733 int level, const char *file, int line,
734 int hs_msg_type, uint32_t extensions_mask,
735 const char *extra)
Jerry Yud25cab02022-10-31 12:48:30 +0800736{
737
Gilles Peskine449bd832023-01-11 14:50:10 +0100738 for (unsigned i = 0;
739 i < sizeof(extension_name_table) / sizeof(extension_name_table[0]);
740 i++) {
Jerry Yu79aa7212022-11-08 21:30:21 +0800741 mbedtls_ssl_print_extension(
Jerry Yuea52ed92022-11-08 21:01:17 +0800742 ssl, level, file, line, hs_msg_type, extension_type_table[i],
Gilles Peskine449bd832023-01-11 14:50:10 +0100743 extensions_mask & (1 << i) ? "exists" : "does not exist", extra);
Jerry Yud25cab02022-10-31 12:48:30 +0800744 }
745}
746
Pengyu Lvee455c02023-01-12 14:37:24 +0800747#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS)
748#define ARRAY_LENGTH(a) (sizeof(a) / sizeof(*(a)))
749
750static const char *ticket_flag_name_table[] =
751{
752 [0] = "ALLOW_PSK_RESUMPTION",
753 [2] = "ALLOW_PSK_EPHEMERAL_RESUMPTION",
754 [3] = "ALLOW_EARLY_DATA",
755};
756
Pengyu Lv4938a562023-01-16 11:28:49 +0800757void mbedtls_ssl_print_ticket_flags(const mbedtls_ssl_context *ssl,
758 int level, const char *file, int line,
759 unsigned int flags)
Pengyu Lvee455c02023-01-12 14:37:24 +0800760{
761 size_t i;
762
763 mbedtls_debug_print_msg(ssl, level, file, line,
Pengyu Lv4938a562023-01-16 11:28:49 +0800764 "print ticket_flags (0x%02x)", flags);
765
766 flags = flags & MBEDTLS_SSL_TLS1_3_TICKET_FLAGS_MASK;
Pengyu Lvee455c02023-01-12 14:37:24 +0800767
768 for (i = 0; i < ARRAY_LENGTH(ticket_flag_name_table); i++) {
Pengyu Lv4938a562023-01-16 11:28:49 +0800769 if ((flags & (1 << i))) {
Pengyu Lvee455c02023-01-12 14:37:24 +0800770 mbedtls_debug_print_msg(ssl, level, file, line, "- %s is set.",
771 ticket_flag_name_table[i]);
772 }
773 }
774}
775#endif /* MBEDTLS_SSL_PROTO_TLS1_3 && MBEDTLS_SSL_SESSION_TICKETS */
776
Jerry Yud25cab02022-10-31 12:48:30 +0800777#endif /* MBEDTLS_DEBUG_C */
778
Gilles Peskine449bd832023-01-11 14:50:10 +0100779void mbedtls_ssl_optimize_checksum(mbedtls_ssl_context *ssl,
780 const mbedtls_ssl_ciphersuite_t *ciphersuite_info)
Jerry Yuc73c6182022-02-08 20:29:25 +0800781{
782 ((void) ciphersuite_info);
783
Andrzej Kurek25f27152022-08-17 16:09:31 -0400784#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +0100785 if (ciphersuite_info->mac == MBEDTLS_MD_SHA384) {
Jerry Yuc73c6182022-02-08 20:29:25 +0800786 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
Gilles Peskine449bd832023-01-11 14:50:10 +0100787 } else
Jerry Yuc73c6182022-02-08 20:29:25 +0800788#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400789#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +0100790 if (ciphersuite_info->mac != MBEDTLS_MD_SHA384) {
Jerry Yuc73c6182022-02-08 20:29:25 +0800791 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Gilles Peskine449bd832023-01-11 14:50:10 +0100792 } else
Jerry Yuc73c6182022-02-08 20:29:25 +0800793#endif
794 {
Gilles Peskine449bd832023-01-11 14:50:10 +0100795 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
Jerry Yuc73c6182022-02-08 20:29:25 +0800796 return;
797 }
798}
799
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100800int mbedtls_ssl_add_hs_hdr_to_checksum(mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100801 unsigned hs_type,
802 size_t total_hs_len)
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100803{
804 unsigned char hs_hdr[4];
805
806 /* Build HS header for checksum update. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100807 hs_hdr[0] = MBEDTLS_BYTE_0(hs_type);
808 hs_hdr[1] = MBEDTLS_BYTE_2(total_hs_len);
809 hs_hdr[2] = MBEDTLS_BYTE_1(total_hs_len);
810 hs_hdr[3] = MBEDTLS_BYTE_0(total_hs_len);
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100811
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100812 return ssl->handshake->update_checksum(ssl, hs_hdr, sizeof(hs_hdr));
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100813}
814
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100815int mbedtls_ssl_add_hs_msg_to_checksum(mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100816 unsigned hs_type,
817 unsigned char const *msg,
818 size_t msg_len)
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100819{
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100820 int ret;
821 ret = mbedtls_ssl_add_hs_hdr_to_checksum(ssl, hs_type, msg_len);
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100822 if (ret != 0) {
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100823 return ret;
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100824 }
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100825 return ssl->handshake->update_checksum(ssl, msg, msg_len);
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100826}
827
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100828int mbedtls_ssl_reset_checksum(mbedtls_ssl_context *ssl)
Jerry Yuc73c6182022-02-08 20:29:25 +0800829{
Manuel Pégourié-Gonnard626aaed2023-02-06 22:03:06 +0100830#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) || \
831 defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100832#if defined(MBEDTLS_USE_PSA_CRYPTO)
833 psa_status_t status;
834#else
835 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
836#endif
Manuel Pégourié-Gonnard626aaed2023-02-06 22:03:06 +0100837#else /* SHA-256 or SHA-384 */
Jerry Yuc73c6182022-02-08 20:29:25 +0800838 ((void) ssl);
Manuel Pégourié-Gonnard626aaed2023-02-06 22:03:06 +0100839#endif /* SHA-256 or SHA-384 */
Andrzej Kurek25f27152022-08-17 16:09:31 -0400840#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800841#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100842 status = psa_hash_abort(&ssl->handshake->fin_sha256_psa);
843 if (status != PSA_SUCCESS) {
Andrzej Kurekdaf5b562023-03-03 05:52:28 -0500844 return PSA_TO_MD_ERR(status);
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100845 }
846 status = psa_hash_setup(&ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256);
847 if (status != PSA_SUCCESS) {
Andrzej Kurekdaf5b562023-03-03 05:52:28 -0500848 return PSA_TO_MD_ERR(status);
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100849 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800850#else
Manuel Pégourié-Gonnard947cee12023-03-06 11:59:59 +0100851 mbedtls_md_free(&ssl->handshake->fin_sha256);
852 mbedtls_md_init(&ssl->handshake->fin_sha256);
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +0100853 ret = mbedtls_md_setup(&ssl->handshake->fin_sha256,
854 mbedtls_md_info_from_type(MBEDTLS_MD_SHA256),
855 0);
856 if (ret != 0) {
857 return ret;
858 }
859 ret = mbedtls_md_starts(&ssl->handshake->fin_sha256);
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100860 if (ret != 0) {
861 return ret;
862 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800863#endif
864#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400865#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800866#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100867 status = psa_hash_abort(&ssl->handshake->fin_sha384_psa);
868 if (status != PSA_SUCCESS) {
Andrzej Kurekdaf5b562023-03-03 05:52:28 -0500869 return PSA_TO_MD_ERR(status);
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100870 }
871 status = psa_hash_setup(&ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384);
872 if (status != PSA_SUCCESS) {
Andrzej Kurekdaf5b562023-03-03 05:52:28 -0500873 return PSA_TO_MD_ERR(status);
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100874 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800875#else
Manuel Pégourié-Gonnard947cee12023-03-06 11:59:59 +0100876 mbedtls_md_free(&ssl->handshake->fin_sha384);
877 mbedtls_md_init(&ssl->handshake->fin_sha384);
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +0100878 ret = mbedtls_md_setup(&ssl->handshake->fin_sha384,
879 mbedtls_md_info_from_type(MBEDTLS_MD_SHA384), 0);
880 if (ret != 0) {
881 return ret;
882 }
883 ret = mbedtls_md_starts(&ssl->handshake->fin_sha384);
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100884 if (ret != 0) {
885 return ret;
886 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800887#endif
888#endif
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100889 return 0;
Jerry Yuc73c6182022-02-08 20:29:25 +0800890}
891
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100892static int ssl_update_checksum_start(mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100893 const unsigned char *buf, size_t len)
Jerry Yuc73c6182022-02-08 20:29:25 +0800894{
Manuel Pégourié-Gonnard626aaed2023-02-06 22:03:06 +0100895#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) || \
896 defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100897#if defined(MBEDTLS_USE_PSA_CRYPTO)
898 psa_status_t status;
899#else
900 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
901#endif
Manuel Pégourié-Gonnard626aaed2023-02-06 22:03:06 +0100902#else /* SHA-256 or SHA-384 */
903 ((void) ssl);
904 (void) buf;
905 (void) len;
906#endif /* SHA-256 or SHA-384 */
Andrzej Kurek25f27152022-08-17 16:09:31 -0400907#if defined(MBEDTLS_HAS_ALG_SHA_256_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_sha256_psa, buf, len);
910 if (status != PSA_SUCCESS) {
Andrzej Kurekdaf5b562023-03-03 05:52:28 -0500911 return PSA_TO_MD_ERR(status);
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100912 }
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_sha256, 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
Andrzej Kurek25f27152022-08-17 16:09:31 -0400920#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800921#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100922 status = psa_hash_update(&ssl->handshake->fin_sha384_psa, buf, len);
923 if (status != PSA_SUCCESS) {
Andrzej Kurekdaf5b562023-03-03 05:52:28 -0500924 return PSA_TO_MD_ERR(status);
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100925 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800926#else
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +0100927 ret = mbedtls_md_update(&ssl->handshake->fin_sha384, buf, len);
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100928 if (ret != 0) {
929 return ret;
930 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800931#endif
932#endif
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100933 return 0;
Jerry Yuc73c6182022-02-08 20:29:25 +0800934}
935
Andrzej Kurek25f27152022-08-17 16:09:31 -0400936#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100937static int ssl_update_checksum_sha256(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)
Andrzej Kurekdaf5b562023-03-03 05:52:28 -0500941 return PSA_TO_MD_ERR(psa_hash_update(
942 &ssl->handshake->fin_sha256_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_sha256, buf, len);
Jerry Yuc73c6182022-02-08 20:29:25 +0800945#endif
946}
947#endif
948
Andrzej Kurek25f27152022-08-17 16:09:31 -0400949#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100950static int ssl_update_checksum_sha384(mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100951 const unsigned char *buf, size_t len)
Jerry Yuc73c6182022-02-08 20:29:25 +0800952{
953#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurekdaf5b562023-03-03 05:52:28 -0500954 return PSA_TO_MD_ERR(psa_hash_update(
955 &ssl->handshake->fin_sha384_psa, buf, len));
Jerry Yuc73c6182022-02-08 20:29:25 +0800956#else
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +0100957 return mbedtls_md_update(&ssl->handshake->fin_sha384, buf, len);
Jerry Yuc73c6182022-02-08 20:29:25 +0800958#endif
959}
960#endif
961
Gilles Peskine449bd832023-01-11 14:50:10 +0100962static void ssl_handshake_params_init(mbedtls_ssl_handshake_params *handshake)
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200963{
Gilles Peskine449bd832023-01-11 14:50:10 +0100964 memset(handshake, 0, sizeof(mbedtls_ssl_handshake_params));
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200965
Andrzej Kurek25f27152022-08-17 16:09:31 -0400966#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -0500967#if defined(MBEDTLS_USE_PSA_CRYPTO)
968 handshake->fin_sha256_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -0500969#else
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +0100970 mbedtls_md_init(&handshake->fin_sha256);
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200971#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -0500972#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400973#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -0500974#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -0500975 handshake->fin_sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -0500976#else
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +0100977 mbedtls_md_init(&handshake->fin_sha384);
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200978#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -0500979#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200980
981 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +0100982
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200983#if defined(MBEDTLS_DHM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100984 mbedtls_dhm_init(&handshake->dhm_ctx);
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200985#endif
Neil Armstrongf3f46412022-04-12 14:43:39 +0200986#if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100987 mbedtls_ecdh_init(&handshake->ecdh_ctx);
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200988#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +0200989#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Neil Armstrongca7d5062022-05-31 14:43:23 +0200990#if defined(MBEDTLS_USE_PSA_CRYPTO)
991 handshake->psa_pake_ctx = psa_pake_operation_init();
992 handshake->psa_pake_password = MBEDTLS_SVC_KEY_ID_INIT;
993#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100994 mbedtls_ecjpake_init(&handshake->ecjpake_ctx);
Neil Armstrongca7d5062022-05-31 14:43:23 +0200995#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +0200996#if defined(MBEDTLS_SSL_CLI_C)
997 handshake->ecjpake_cache = NULL;
998 handshake->ecjpake_cache_len = 0;
999#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02001000#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001001
Gilles Peskineeccd8882020-03-10 12:19:08 +01001002#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001003 mbedtls_x509_crt_restart_init(&handshake->ecrs_ctx);
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02001004#endif
1005
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001006#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1007 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
1008#endif
Hanno Becker75173122019-02-06 16:18:31 +00001009
1010#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
1011 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01001012 mbedtls_pk_init(&handshake->peer_pubkey);
Hanno Becker75173122019-02-06 16:18:31 +00001013#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001014}
1015
Gilles Peskine449bd832023-01-11 14:50:10 +01001016void mbedtls_ssl_transform_init(mbedtls_ssl_transform *transform)
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001017{
Gilles Peskine449bd832023-01-11 14:50:10 +01001018 memset(transform, 0, sizeof(mbedtls_ssl_transform));
Paul Bakker84bbeb52014-07-01 14:53:22 +02001019
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +01001020#if defined(MBEDTLS_USE_PSA_CRYPTO)
1021 transform->psa_key_enc = MBEDTLS_SVC_KEY_ID_INIT;
1022 transform->psa_key_dec = MBEDTLS_SVC_KEY_ID_INIT;
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01001023#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001024 mbedtls_cipher_init(&transform->cipher_ctx_enc);
1025 mbedtls_cipher_init(&transform->cipher_ctx_dec);
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +01001026#endif
1027
Hanno Beckerfd86ca82020-11-30 08:54:23 +00001028#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong39b8e7d2022-02-23 09:24:45 +01001029#if defined(MBEDTLS_USE_PSA_CRYPTO)
1030 transform->psa_mac_enc = MBEDTLS_SVC_KEY_ID_INIT;
1031 transform->psa_mac_dec = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrongcf8841a2022-02-24 11:17:45 +01001032#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001033 mbedtls_md_init(&transform->md_ctx_enc);
1034 mbedtls_md_init(&transform->md_ctx_dec);
Hanno Beckerd56ed242018-01-03 15:32:51 +00001035#endif
Neil Armstrongcf8841a2022-02-24 11:17:45 +01001036#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001037}
1038
Gilles Peskine449bd832023-01-11 14:50:10 +01001039void mbedtls_ssl_session_init(mbedtls_ssl_session *session)
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001040{
Gilles Peskine449bd832023-01-11 14:50:10 +01001041 memset(session, 0, sizeof(mbedtls_ssl_session));
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001042}
1043
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001044MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001045static int ssl_handshake_init(mbedtls_ssl_context *ssl)
Paul Bakker48916f92012-09-16 19:57:18 +00001046{
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001047 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1048
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001049 /* Clear old handshake information if present */
Jerry Yu2e199812022-12-01 18:57:19 +08001050#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01001051 if (ssl->transform_negotiate) {
1052 mbedtls_ssl_transform_free(ssl->transform_negotiate);
1053 }
Jerry Yu2e199812022-12-01 18:57:19 +08001054#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001055 if (ssl->session_negotiate) {
1056 mbedtls_ssl_session_free(ssl->session_negotiate);
1057 }
1058 if (ssl->handshake) {
1059 mbedtls_ssl_handshake_free(ssl);
1060 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001061
Jerry Yu2e199812022-12-01 18:57:19 +08001062#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001063 /*
1064 * Either the pointers are now NULL or cleared properly and can be freed.
1065 * Now allocate missing structures.
1066 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001067 if (ssl->transform_negotiate == NULL) {
1068 ssl->transform_negotiate = mbedtls_calloc(1, sizeof(mbedtls_ssl_transform));
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001069 }
Jerry Yu2e199812022-12-01 18:57:19 +08001070#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker48916f92012-09-16 19:57:18 +00001071
Gilles Peskine449bd832023-01-11 14:50:10 +01001072 if (ssl->session_negotiate == NULL) {
1073 ssl->session_negotiate = mbedtls_calloc(1, sizeof(mbedtls_ssl_session));
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001074 }
Paul Bakker48916f92012-09-16 19:57:18 +00001075
Gilles Peskine449bd832023-01-11 14:50:10 +01001076 if (ssl->handshake == NULL) {
1077 ssl->handshake = mbedtls_calloc(1, sizeof(mbedtls_ssl_handshake_params));
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001078 }
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05001079#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1080 /* If the buffers are too small - reallocate */
Andrzej Kurek8ea68722020-04-03 06:40:47 -04001081
Gilles Peskine449bd832023-01-11 14:50:10 +01001082 handle_buffer_resizing(ssl, 0, MBEDTLS_SSL_IN_BUFFER_LEN,
1083 MBEDTLS_SSL_OUT_BUFFER_LEN);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05001084#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001085
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001086 /* All pointers should exist and can be directly freed without issue */
Gilles Peskine449bd832023-01-11 14:50:10 +01001087 if (ssl->handshake == NULL ||
Jerry Yu2e199812022-12-01 18:57:19 +08001088#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker48916f92012-09-16 19:57:18 +00001089 ssl->transform_negotiate == NULL ||
Jerry Yu2e199812022-12-01 18:57:19 +08001090#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001091 ssl->session_negotiate == NULL) {
1092 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc() of ssl sub-contexts failed"));
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001093
Gilles Peskine449bd832023-01-11 14:50:10 +01001094 mbedtls_free(ssl->handshake);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001095 ssl->handshake = NULL;
Jerry Yu2e199812022-12-01 18:57:19 +08001096
1097#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01001098 mbedtls_free(ssl->transform_negotiate);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001099 ssl->transform_negotiate = NULL;
Jerry Yu2e199812022-12-01 18:57:19 +08001100#endif
1101
Gilles Peskine449bd832023-01-11 14:50:10 +01001102 mbedtls_free(ssl->session_negotiate);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001103 ssl->session_negotiate = NULL;
1104
Gilles Peskine449bd832023-01-11 14:50:10 +01001105 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Paul Bakker48916f92012-09-16 19:57:18 +00001106 }
1107
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001108 /* Initialize structures */
Gilles Peskine449bd832023-01-11 14:50:10 +01001109 mbedtls_ssl_session_init(ssl->session_negotiate);
1110 ssl_handshake_params_init(ssl->handshake);
Paul Bakker968afaa2014-07-09 11:09:24 +02001111
Jerry Yu2e199812022-12-01 18:57:19 +08001112#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01001113 mbedtls_ssl_transform_init(ssl->transform_negotiate);
Jerry Yu2e199812022-12-01 18:57:19 +08001114#endif
1115
Manuel Pégourié-Gonnard537f2312023-02-05 10:17:45 +01001116 /* Setup handshake checksums */
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001117 ret = mbedtls_ssl_reset_checksum(ssl);
1118 if (ret != 0) {
1119 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_reset_checksum", ret);
1120 return ret;
1121 }
Manuel Pégourié-Gonnard537f2312023-02-05 10:17:45 +01001122
Jerry Yud0766ec2022-09-22 10:46:57 +08001123#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
1124 defined(MBEDTLS_SSL_SRV_C) && \
1125 defined(MBEDTLS_SSL_SESSION_TICKETS)
1126 ssl->handshake->new_session_tickets_count =
Gilles Peskine449bd832023-01-11 14:50:10 +01001127 ssl->conf->new_session_tickets_count;
Jerry Yud0766ec2022-09-22 10:46:57 +08001128#endif
1129
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001130#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001131 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001132 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001133
Gilles Peskine449bd832023-01-11 14:50:10 +01001134 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001135 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Gilles Peskine449bd832023-01-11 14:50:10 +01001136 } else {
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001137 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Gilles Peskine449bd832023-01-11 14:50:10 +01001138 }
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001139
Gilles Peskine449bd832023-01-11 14:50:10 +01001140 mbedtls_ssl_set_timer(ssl, 0);
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001141 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001142#endif
1143
Brett Warrene0edc842021-08-17 09:53:13 +01001144/*
1145 * curve_list is translated to IANA TLS group identifiers here because
1146 * mbedtls_ssl_conf_curves returns void and so can't return
1147 * any error codes.
1148 */
1149#if defined(MBEDTLS_ECP_C)
1150#if !defined(MBEDTLS_DEPRECATED_REMOVED)
1151 /* Heap allocate and translate curve_list from internal to IANA group ids */
Gilles Peskine449bd832023-01-11 14:50:10 +01001152 if (ssl->conf->curve_list != NULL) {
Brett Warrene0edc842021-08-17 09:53:13 +01001153 size_t length;
1154 const mbedtls_ecp_group_id *curve_list = ssl->conf->curve_list;
1155
Gilles Peskine449bd832023-01-11 14:50:10 +01001156 for (length = 0; (curve_list[length] != MBEDTLS_ECP_DP_NONE) &&
1157 (length < MBEDTLS_ECP_DP_MAX); length++) {
1158 }
Brett Warrene0edc842021-08-17 09:53:13 +01001159
1160 /* Leave room for zero termination */
Gilles Peskine449bd832023-01-11 14:50:10 +01001161 uint16_t *group_list = mbedtls_calloc(length + 1, sizeof(uint16_t));
1162 if (group_list == NULL) {
1163 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
1164 }
Brett Warrene0edc842021-08-17 09:53:13 +01001165
Gilles Peskine449bd832023-01-11 14:50:10 +01001166 for (size_t i = 0; i < length; i++) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01001167 uint16_t tls_id = mbedtls_ssl_get_tls_id_from_ecp_group_id(
Gilles Peskine449bd832023-01-11 14:50:10 +01001168 curve_list[i]);
1169 if (tls_id == 0) {
1170 mbedtls_free(group_list);
1171 return MBEDTLS_ERR_SSL_BAD_CONFIG;
Brett Warrene0edc842021-08-17 09:53:13 +01001172 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01001173 group_list[i] = tls_id;
Brett Warrene0edc842021-08-17 09:53:13 +01001174 }
1175
1176 group_list[length] = 0;
1177
1178 ssl->handshake->group_list = group_list;
1179 ssl->handshake->group_list_heap_allocated = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001180 } else {
Brett Warrene0edc842021-08-17 09:53:13 +01001181 ssl->handshake->group_list = ssl->conf->group_list;
1182 ssl->handshake->group_list_heap_allocated = 0;
1183 }
1184#endif /* MBEDTLS_DEPRECATED_REMOVED */
1185#endif /* MBEDTLS_ECP_C */
1186
Ronald Crone68ab4f2022-10-05 12:46:29 +02001187#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +08001188#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Jerry Yua69269a2022-01-17 21:06:01 +08001189#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yu713013f2022-01-17 18:16:35 +08001190 /* Heap allocate and translate sig_hashes from internal hash identifiers to
1191 signature algorithms IANA identifiers. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001192 if (mbedtls_ssl_conf_is_tls12_only(ssl->conf) &&
1193 ssl->conf->sig_hashes != NULL) {
Jerry Yuf017ee42022-01-12 15:49:48 +08001194 const int *md;
1195 const int *sig_hashes = ssl->conf->sig_hashes;
Jerry Yub476a442022-01-21 18:14:45 +08001196 size_t sig_algs_len = 0;
Jerry Yuf017ee42022-01-12 15:49:48 +08001197 uint16_t *p;
1198
Jerry Yub476a442022-01-21 18:14:45 +08001199#if defined(static_assert)
Gilles Peskine449bd832023-01-11 14:50:10 +01001200 static_assert(MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN
1201 <= (SIZE_MAX - (2 * sizeof(uint16_t))),
1202 "MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN too big");
Jerry Yua68dca22022-01-20 16:28:27 +08001203#endif
1204
Gilles Peskine449bd832023-01-11 14:50:10 +01001205 for (md = sig_hashes; *md != MBEDTLS_MD_NONE; md++) {
1206 if (mbedtls_ssl_hash_from_md_alg(*md) == MBEDTLS_SSL_HASH_NONE) {
Jerry Yuf017ee42022-01-12 15:49:48 +08001207 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001208 }
Jerry Yub476a442022-01-21 18:14:45 +08001209#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001210 sig_algs_len += sizeof(uint16_t);
Jerry Yub476a442022-01-21 18:14:45 +08001211#endif
Jerry Yua68dca22022-01-20 16:28:27 +08001212
Jerry Yub476a442022-01-21 18:14:45 +08001213#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001214 sig_algs_len += sizeof(uint16_t);
Jerry Yub476a442022-01-21 18:14:45 +08001215#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001216 if (sig_algs_len > MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN) {
1217 return MBEDTLS_ERR_SSL_BAD_CONFIG;
1218 }
Jerry Yuf017ee42022-01-12 15:49:48 +08001219 }
1220
Gilles Peskine449bd832023-01-11 14:50:10 +01001221 if (sig_algs_len < MBEDTLS_SSL_MIN_SIG_ALG_LIST_LEN) {
1222 return MBEDTLS_ERR_SSL_BAD_CONFIG;
1223 }
Jerry Yuf017ee42022-01-12 15:49:48 +08001224
Gilles Peskine449bd832023-01-11 14:50:10 +01001225 ssl->handshake->sig_algs = mbedtls_calloc(1, sig_algs_len +
1226 sizeof(uint16_t));
1227 if (ssl->handshake->sig_algs == NULL) {
1228 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
1229 }
Jerry Yuf017ee42022-01-12 15:49:48 +08001230
Gilles Peskine449bd832023-01-11 14:50:10 +01001231 p = (uint16_t *) ssl->handshake->sig_algs;
1232 for (md = sig_hashes; *md != MBEDTLS_MD_NONE; md++) {
1233 unsigned char hash = mbedtls_ssl_hash_from_md_alg(*md);
1234 if (hash == MBEDTLS_SSL_HASH_NONE) {
Jerry Yuf017ee42022-01-12 15:49:48 +08001235 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001236 }
Jerry Yu6106fdc2022-01-12 16:36:14 +08001237#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001238 *p = ((hash << 8) | MBEDTLS_SSL_SIG_ECDSA);
Jerry Yuf017ee42022-01-12 15:49:48 +08001239 p++;
Jerry Yu6106fdc2022-01-12 16:36:14 +08001240#endif
1241#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001242 *p = ((hash << 8) | MBEDTLS_SSL_SIG_RSA);
Jerry Yuf017ee42022-01-12 15:49:48 +08001243 p++;
Jerry Yu6106fdc2022-01-12 16:36:14 +08001244#endif
Jerry Yuf017ee42022-01-12 15:49:48 +08001245 }
Gabor Mezei15b95a62022-05-09 16:37:58 +02001246 *p = MBEDTLS_TLS_SIG_NONE;
Jerry Yuf017ee42022-01-12 15:49:48 +08001247 ssl->handshake->sig_algs_heap_allocated = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001248 } else
Jerry Yua69269a2022-01-17 21:06:01 +08001249#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Jerry Yuf017ee42022-01-12 15:49:48 +08001250 {
Jerry Yuf017ee42022-01-12 15:49:48 +08001251 ssl->handshake->sig_algs_heap_allocated = 0;
1252 }
Jerry Yucc539102022-06-27 16:27:35 +08001253#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Ronald Crone68ab4f2022-10-05 12:46:29 +02001254#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Gilles Peskine449bd832023-01-11 14:50:10 +01001255 return 0;
Paul Bakker48916f92012-09-16 19:57:18 +00001256}
1257
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001258#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001259/* Dummy cookie callbacks for defaults */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001260MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001261static int ssl_cookie_write_dummy(void *ctx,
1262 unsigned char **p, unsigned char *end,
1263 const unsigned char *cli_id, size_t cli_id_len)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001264{
1265 ((void) ctx);
1266 ((void) p);
1267 ((void) end);
1268 ((void) cli_id);
1269 ((void) cli_id_len);
1270
Gilles Peskine449bd832023-01-11 14:50:10 +01001271 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001272}
1273
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001274MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001275static int ssl_cookie_check_dummy(void *ctx,
1276 const unsigned char *cookie, size_t cookie_len,
1277 const unsigned char *cli_id, size_t cli_id_len)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001278{
1279 ((void) ctx);
1280 ((void) cookie);
1281 ((void) cookie_len);
1282 ((void) cli_id);
1283 ((void) cli_id_len);
1284
Gilles Peskine449bd832023-01-11 14:50:10 +01001285 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001286}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001287#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001288
Paul Bakker5121ce52009-01-03 21:22:43 +00001289/*
1290 * Initialize an SSL context
1291 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001292void mbedtls_ssl_init(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001293{
Gilles Peskine449bd832023-01-11 14:50:10 +01001294 memset(ssl, 0, sizeof(mbedtls_ssl_context));
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001295}
1296
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001297MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001298static int ssl_conf_version_check(const mbedtls_ssl_context *ssl)
Jerry Yu60835a82021-08-04 10:13:52 +08001299{
Ronald Cron086ee0b2022-03-15 15:18:51 +01001300 const mbedtls_ssl_config *conf = ssl->conf;
1301
Ronald Cron6f135e12021-12-08 16:57:54 +01001302#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001303 if (mbedtls_ssl_conf_is_tls13_only(conf)) {
1304 if (conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
1305 MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS 1.3 is not yet supported."));
1306 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQianed582dd2022-04-13 08:21:05 +00001307 }
1308
Gilles Peskine449bd832023-01-11 14:50:10 +01001309 MBEDTLS_SSL_DEBUG_MSG(4, ("The SSL configuration is tls13 only."));
1310 return 0;
Jerry Yu60835a82021-08-04 10:13:52 +08001311 }
1312#endif
1313
1314#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01001315 if (mbedtls_ssl_conf_is_tls12_only(conf)) {
1316 MBEDTLS_SSL_DEBUG_MSG(4, ("The SSL configuration is tls12 only."));
1317 return 0;
Jerry Yu60835a82021-08-04 10:13:52 +08001318 }
1319#endif
1320
Ronald Cron6f135e12021-12-08 16:57:54 +01001321#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001322 if (mbedtls_ssl_conf_is_hybrid_tls12_tls13(conf)) {
1323 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
1324 MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS not yet supported in Hybrid TLS 1.3 + TLS 1.2"));
1325 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQianed582dd2022-04-13 08:21:05 +00001326 }
1327
Gilles Peskine449bd832023-01-11 14:50:10 +01001328 if (conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
1329 MBEDTLS_SSL_DEBUG_MSG(1, ("TLS 1.3 server is not supported yet."));
1330 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQian8f9dfe42022-04-15 02:52:39 +00001331 }
1332
1333
Gilles Peskine449bd832023-01-11 14:50:10 +01001334 MBEDTLS_SSL_DEBUG_MSG(4, ("The SSL configuration is TLS 1.3 or TLS 1.2."));
1335 return 0;
Jerry Yu60835a82021-08-04 10:13:52 +08001336 }
1337#endif
1338
Gilles Peskine449bd832023-01-11 14:50:10 +01001339 MBEDTLS_SSL_DEBUG_MSG(1, ("The SSL configuration is invalid."));
1340 return MBEDTLS_ERR_SSL_BAD_CONFIG;
Jerry Yu60835a82021-08-04 10:13:52 +08001341}
1342
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001343MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu60835a82021-08-04 10:13:52 +08001344static int ssl_conf_check(const mbedtls_ssl_context *ssl)
1345{
1346 int ret;
Gilles Peskine449bd832023-01-11 14:50:10 +01001347 ret = ssl_conf_version_check(ssl);
1348 if (ret != 0) {
1349 return ret;
1350 }
Jerry Yu60835a82021-08-04 10:13:52 +08001351
Jerry Yudef7ae42022-10-30 14:13:19 +08001352#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1353 /* RFC 8446 section 4.4.3
1354 *
1355 * If the verification fails, the receiver MUST terminate the handshake with
1356 * a "decrypt_error" alert.
1357 *
1358 * If the client is configured as TLS 1.3 only with optional verify, return
1359 * bad config.
1360 *
1361 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001362 if (mbedtls_ssl_conf_tls13_ephemeral_enabled(
1363 (mbedtls_ssl_context *) ssl) &&
Jerry Yudef7ae42022-10-30 14:13:19 +08001364 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
1365 ssl->conf->max_tls_version == MBEDTLS_SSL_VERSION_TLS1_3 &&
1366 ssl->conf->min_tls_version == MBEDTLS_SSL_VERSION_TLS1_3 &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001367 ssl->conf->authmode == MBEDTLS_SSL_VERIFY_OPTIONAL) {
Jerry Yudef7ae42022-10-30 14:13:19 +08001368 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +01001369 1, ("Optional verify auth mode "
1370 "is not available for TLS 1.3 client"));
1371 return MBEDTLS_ERR_SSL_BAD_CONFIG;
Jerry Yudef7ae42022-10-30 14:13:19 +08001372 }
1373#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
1374
Jerry Yu60835a82021-08-04 10:13:52 +08001375 /* Space for further checks */
1376
Gilles Peskine449bd832023-01-11 14:50:10 +01001377 return 0;
Jerry Yu60835a82021-08-04 10:13:52 +08001378}
1379
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001380/*
1381 * Setup an SSL context
1382 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01001383
Gilles Peskine449bd832023-01-11 14:50:10 +01001384int mbedtls_ssl_setup(mbedtls_ssl_context *ssl,
1385 const mbedtls_ssl_config *conf)
Paul Bakker5121ce52009-01-03 21:22:43 +00001386{
Janos Follath865b3eb2019-12-16 11:46:15 +00001387 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Darryl Greenb33cc762019-11-28 14:29:44 +00001388 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1389 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00001390
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001391 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00001392
Gilles Peskine449bd832023-01-11 14:50:10 +01001393 if ((ret = ssl_conf_check(ssl)) != 0) {
1394 return ret;
1395 }
Jerry Yu60835a82021-08-04 10:13:52 +08001396
Paul Bakker62f2dee2012-09-28 07:31:51 +00001397 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001398 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00001399 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02001400
1401 /* Set to NULL in case of an error condition */
1402 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02001403
Darryl Greenb33cc762019-11-28 14:29:44 +00001404#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1405 ssl->in_buf_len = in_buf_len;
1406#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001407 ssl->in_buf = mbedtls_calloc(1, in_buf_len);
1408 if (ssl->in_buf == NULL) {
1409 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", in_buf_len));
k-stachowiak9f7798e2018-07-31 16:52:32 +02001410 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02001411 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10001412 }
1413
Darryl Greenb33cc762019-11-28 14:29:44 +00001414#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1415 ssl->out_buf_len = out_buf_len;
1416#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001417 ssl->out_buf = mbedtls_calloc(1, out_buf_len);
1418 if (ssl->out_buf == NULL) {
1419 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", out_buf_len));
k-stachowiak9f7798e2018-07-31 16:52:32 +02001420 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02001421 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00001422 }
1423
Gilles Peskine449bd832023-01-11 14:50:10 +01001424 mbedtls_ssl_reset_in_out_pointers(ssl);
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02001425
Johan Pascalb62bb512015-12-03 21:56:45 +01001426#if defined(MBEDTLS_SSL_DTLS_SRTP)
Gilles Peskine449bd832023-01-11 14:50:10 +01001427 memset(&ssl->dtls_srtp_info, 0, sizeof(ssl->dtls_srtp_info));
Johan Pascalb62bb512015-12-03 21:56:45 +01001428#endif
1429
Gilles Peskine449bd832023-01-11 14:50:10 +01001430 if ((ret = ssl_handshake_init(ssl)) != 0) {
k-stachowiaka47911c2018-07-04 17:41:58 +02001431 goto error;
Gilles Peskine449bd832023-01-11 14:50:10 +01001432 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001433
Gilles Peskine449bd832023-01-11 14:50:10 +01001434 return 0;
k-stachowiaka47911c2018-07-04 17:41:58 +02001435
1436error:
Gilles Peskine449bd832023-01-11 14:50:10 +01001437 mbedtls_free(ssl->in_buf);
1438 mbedtls_free(ssl->out_buf);
k-stachowiaka47911c2018-07-04 17:41:58 +02001439
1440 ssl->conf = NULL;
1441
Darryl Greenb33cc762019-11-28 14:29:44 +00001442#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1443 ssl->in_buf_len = 0;
1444 ssl->out_buf_len = 0;
1445#endif
k-stachowiaka47911c2018-07-04 17:41:58 +02001446 ssl->in_buf = NULL;
1447 ssl->out_buf = NULL;
1448
1449 ssl->in_hdr = NULL;
1450 ssl->in_ctr = NULL;
1451 ssl->in_len = NULL;
1452 ssl->in_iv = NULL;
1453 ssl->in_msg = NULL;
1454
1455 ssl->out_hdr = NULL;
1456 ssl->out_ctr = NULL;
1457 ssl->out_len = NULL;
1458 ssl->out_iv = NULL;
1459 ssl->out_msg = NULL;
1460
Gilles Peskine449bd832023-01-11 14:50:10 +01001461 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001462}
1463
1464/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00001465 * Reset an initialized and used SSL context for re-use while retaining
1466 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001467 *
1468 * If partial is non-zero, keep data in the input buffer and client ID.
1469 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00001470 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001471void mbedtls_ssl_session_reset_msg_layer(mbedtls_ssl_context *ssl,
1472 int partial)
Paul Bakker7eb013f2011-10-06 12:37:39 +00001473{
Darryl Greenb33cc762019-11-28 14:29:44 +00001474#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1475 size_t in_buf_len = ssl->in_buf_len;
1476 size_t out_buf_len = ssl->out_buf_len;
1477#else
1478 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1479 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
1480#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001481
Hanno Beckerb0302c42021-08-03 09:39:42 +01001482#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || !defined(MBEDTLS_SSL_SRV_C)
1483 partial = 0;
Hanno Becker7e772132018-08-10 12:38:21 +01001484#endif
1485
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001486 /* Cancel any possibly running timer */
Gilles Peskine449bd832023-01-11 14:50:10 +01001487 mbedtls_ssl_set_timer(ssl, 0);
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001488
Gilles Peskine449bd832023-01-11 14:50:10 +01001489 mbedtls_ssl_reset_in_out_pointers(ssl);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001490
1491 /* Reset incoming message parsing */
1492 ssl->in_offt = NULL;
1493 ssl->nb_zero = 0;
1494 ssl->in_msgtype = 0;
1495 ssl->in_msglen = 0;
1496 ssl->in_hslen = 0;
1497 ssl->keep_current_message = 0;
1498 ssl->transform_in = NULL;
1499
1500#if defined(MBEDTLS_SSL_PROTO_DTLS)
1501 ssl->next_record_offset = 0;
1502 ssl->in_epoch = 0;
1503#endif
1504
1505 /* Keep current datagram if partial == 1 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001506 if (partial == 0) {
Hanno Beckerb0302c42021-08-03 09:39:42 +01001507 ssl->in_left = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001508 memset(ssl->in_buf, 0, in_buf_len);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001509 }
1510
Ronald Cronad8c17b2022-06-10 17:18:09 +02001511 ssl->send_alert = 0;
1512
Hanno Beckerb0302c42021-08-03 09:39:42 +01001513 /* Reset outgoing message writing */
1514 ssl->out_msgtype = 0;
1515 ssl->out_msglen = 0;
1516 ssl->out_left = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001517 memset(ssl->out_buf, 0, out_buf_len);
1518 memset(ssl->cur_out_ctr, 0, sizeof(ssl->cur_out_ctr));
Hanno Beckerb0302c42021-08-03 09:39:42 +01001519 ssl->transform_out = NULL;
1520
1521#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine449bd832023-01-11 14:50:10 +01001522 mbedtls_ssl_dtls_replay_reset(ssl);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001523#endif
1524
XiaokangQian2b01dc32022-01-21 02:53:13 +00001525#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01001526 if (ssl->transform) {
1527 mbedtls_ssl_transform_free(ssl->transform);
1528 mbedtls_free(ssl->transform);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001529 ssl->transform = NULL;
1530 }
XiaokangQian2b01dc32022-01-21 02:53:13 +00001531#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1532
XiaokangQian2b01dc32022-01-21 02:53:13 +00001533#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001534 mbedtls_ssl_transform_free(ssl->transform_application);
1535 mbedtls_free(ssl->transform_application);
XiaokangQian2b01dc32022-01-21 02:53:13 +00001536 ssl->transform_application = NULL;
1537
Gilles Peskine449bd832023-01-11 14:50:10 +01001538 if (ssl->handshake != NULL) {
Jerry Yu3d9b5902022-11-04 14:07:25 +08001539#if defined(MBEDTLS_SSL_EARLY_DATA)
Gilles Peskine449bd832023-01-11 14:50:10 +01001540 mbedtls_ssl_transform_free(ssl->handshake->transform_earlydata);
1541 mbedtls_free(ssl->handshake->transform_earlydata);
XiaokangQian2b01dc32022-01-21 02:53:13 +00001542 ssl->handshake->transform_earlydata = NULL;
Jerry Yu3d9b5902022-11-04 14:07:25 +08001543#endif
XiaokangQian2b01dc32022-01-21 02:53:13 +00001544
Gilles Peskine449bd832023-01-11 14:50:10 +01001545 mbedtls_ssl_transform_free(ssl->handshake->transform_handshake);
1546 mbedtls_free(ssl->handshake->transform_handshake);
XiaokangQian2b01dc32022-01-21 02:53:13 +00001547 ssl->handshake->transform_handshake = NULL;
1548 }
1549
XiaokangQian2b01dc32022-01-21 02:53:13 +00001550#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerb0302c42021-08-03 09:39:42 +01001551}
1552
Gilles Peskine449bd832023-01-11 14:50:10 +01001553int mbedtls_ssl_session_reset_int(mbedtls_ssl_context *ssl, int partial)
Hanno Beckerb0302c42021-08-03 09:39:42 +01001554{
1555 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1556
1557 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
1558
Gilles Peskine449bd832023-01-11 14:50:10 +01001559 mbedtls_ssl_session_reset_msg_layer(ssl, partial);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001560
1561 /* Reset renegotiation state */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001562#if defined(MBEDTLS_SSL_RENEGOTIATION)
1563 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001564 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00001565
1566 ssl->verify_data_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001567 memset(ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN);
1568 memset(ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN);
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001569#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001570 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00001571
Hanno Beckerb0302c42021-08-03 09:39:42 +01001572 ssl->session_in = NULL;
Hanno Becker78640902018-08-13 16:35:15 +01001573 ssl->session_out = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001574 if (ssl->session) {
1575 mbedtls_ssl_session_free(ssl->session);
1576 mbedtls_free(ssl->session);
Paul Bakkerc0463502013-02-14 11:19:38 +01001577 ssl->session = NULL;
1578 }
1579
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001580#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02001581 ssl->alpn_chosen = NULL;
1582#endif
1583
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001584#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
David Horstmann3b2276a2022-10-06 14:49:08 +01001585 int free_cli_id = 1;
Hanno Becker4ccbf062018-08-10 11:20:38 +01001586#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Gilles Peskine449bd832023-01-11 14:50:10 +01001587 free_cli_id = (partial == 0);
Hanno Becker4ccbf062018-08-10 11:20:38 +01001588#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001589 if (free_cli_id) {
1590 mbedtls_free(ssl->cli_id);
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001591 ssl->cli_id = NULL;
1592 ssl->cli_id_len = 0;
1593 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02001594#endif
1595
Gilles Peskine449bd832023-01-11 14:50:10 +01001596 if ((ret = ssl_handshake_init(ssl)) != 0) {
1597 return ret;
1598 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00001599
Gilles Peskine449bd832023-01-11 14:50:10 +01001600 return 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00001601}
1602
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02001603/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001604 * Reset an initialized and used SSL context for re-use while retaining
1605 * all application-set variables, function pointers and data.
1606 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001607int mbedtls_ssl_session_reset(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001608{
Gilles Peskine449bd832023-01-11 14:50:10 +01001609 return mbedtls_ssl_session_reset_int(ssl, 0);
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001610}
1611
1612/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001613 * SSL set accessors
1614 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001615void mbedtls_ssl_conf_endpoint(mbedtls_ssl_config *conf, int endpoint)
Paul Bakker5121ce52009-01-03 21:22:43 +00001616{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001617 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00001618}
1619
Gilles Peskine449bd832023-01-11 14:50:10 +01001620void mbedtls_ssl_conf_transport(mbedtls_ssl_config *conf, int transport)
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001621{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001622 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001623}
1624
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001625#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine449bd832023-01-11 14:50:10 +01001626void mbedtls_ssl_conf_dtls_anti_replay(mbedtls_ssl_config *conf, char mode)
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001627{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001628 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001629}
1630#endif
1631
Gilles Peskine449bd832023-01-11 14:50:10 +01001632void mbedtls_ssl_conf_dtls_badmac_limit(mbedtls_ssl_config *conf, unsigned limit)
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001633{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001634 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001635}
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001636
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001637#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01001638
Gilles Peskine449bd832023-01-11 14:50:10 +01001639void mbedtls_ssl_set_datagram_packing(mbedtls_ssl_context *ssl,
1640 unsigned allow_packing)
Hanno Becker04da1892018-08-14 13:22:10 +01001641{
1642 ssl->disable_datagram_packing = !allow_packing;
1643}
1644
Gilles Peskine449bd832023-01-11 14:50:10 +01001645void mbedtls_ssl_conf_handshake_timeout(mbedtls_ssl_config *conf,
1646 uint32_t min, uint32_t max)
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001647{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001648 conf->hs_timeout_min = min;
1649 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001650}
1651#endif
1652
Gilles Peskine449bd832023-01-11 14:50:10 +01001653void mbedtls_ssl_conf_authmode(mbedtls_ssl_config *conf, int authmode)
Paul Bakker5121ce52009-01-03 21:22:43 +00001654{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001655 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00001656}
1657
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001658#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001659void mbedtls_ssl_conf_verify(mbedtls_ssl_config *conf,
1660 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
1661 void *p_vrfy)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001662{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001663 conf->f_vrfy = f_vrfy;
1664 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001665}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001666#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001667
Gilles Peskine449bd832023-01-11 14:50:10 +01001668void mbedtls_ssl_conf_rng(mbedtls_ssl_config *conf,
1669 int (*f_rng)(void *, unsigned char *, size_t),
1670 void *p_rng)
Paul Bakker5121ce52009-01-03 21:22:43 +00001671{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001672 conf->f_rng = f_rng;
1673 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00001674}
1675
Gilles Peskine449bd832023-01-11 14:50:10 +01001676void mbedtls_ssl_conf_dbg(mbedtls_ssl_config *conf,
1677 void (*f_dbg)(void *, int, const char *, int, const char *),
1678 void *p_dbg)
Paul Bakker5121ce52009-01-03 21:22:43 +00001679{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001680 conf->f_dbg = f_dbg;
1681 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00001682}
1683
Gilles Peskine449bd832023-01-11 14:50:10 +01001684void mbedtls_ssl_set_bio(mbedtls_ssl_context *ssl,
1685 void *p_bio,
1686 mbedtls_ssl_send_t *f_send,
1687 mbedtls_ssl_recv_t *f_recv,
1688 mbedtls_ssl_recv_timeout_t *f_recv_timeout)
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001689{
1690 ssl->p_bio = p_bio;
1691 ssl->f_send = f_send;
1692 ssl->f_recv = f_recv;
1693 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001694}
1695
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02001696#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001697void mbedtls_ssl_set_mtu(mbedtls_ssl_context *ssl, uint16_t mtu)
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02001698{
1699 ssl->mtu = mtu;
1700}
1701#endif
1702
Gilles Peskine449bd832023-01-11 14:50:10 +01001703void mbedtls_ssl_conf_read_timeout(mbedtls_ssl_config *conf, uint32_t timeout)
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001704{
1705 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001706}
1707
Gilles Peskine449bd832023-01-11 14:50:10 +01001708void mbedtls_ssl_set_timer_cb(mbedtls_ssl_context *ssl,
1709 void *p_timer,
1710 mbedtls_ssl_set_timer_t *f_set_timer,
1711 mbedtls_ssl_get_timer_t *f_get_timer)
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001712{
1713 ssl->p_timer = p_timer;
1714 ssl->f_set_timer = f_set_timer;
1715 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001716
1717 /* Make sure we start with no timer running */
Gilles Peskine449bd832023-01-11 14:50:10 +01001718 mbedtls_ssl_set_timer(ssl, 0);
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001719}
1720
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001721#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001722void mbedtls_ssl_conf_session_cache(mbedtls_ssl_config *conf,
1723 void *p_cache,
1724 mbedtls_ssl_cache_get_t *f_get_cache,
1725 mbedtls_ssl_cache_set_t *f_set_cache)
Paul Bakker5121ce52009-01-03 21:22:43 +00001726{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01001727 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001728 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001729 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00001730}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001731#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001732
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001733#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001734int mbedtls_ssl_set_session(mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session)
Paul Bakker5121ce52009-01-03 21:22:43 +00001735{
Janos Follath865b3eb2019-12-16 11:46:15 +00001736 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001737
Gilles Peskine449bd832023-01-11 14:50:10 +01001738 if (ssl == NULL ||
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001739 session == NULL ||
1740 ssl->session_negotiate == NULL ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001741 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT) {
1742 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001743 }
1744
Gilles Peskine449bd832023-01-11 14:50:10 +01001745 if (ssl->handshake->resume == 1) {
1746 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
1747 }
Hanno Beckere810bbc2021-05-14 16:01:05 +01001748
Jerry Yu21092062022-10-10 21:21:31 +08001749#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001750 if (session->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
Jerry Yu21092062022-10-10 21:21:31 +08001751 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01001752 mbedtls_ssl_ciphersuite_from_id(session->ciphersuite);
Jerry Yu21092062022-10-10 21:21:31 +08001753
Gilles Peskine449bd832023-01-11 14:50:10 +01001754 if (mbedtls_ssl_validate_ciphersuite(
Jerry Yu21092062022-10-10 21:21:31 +08001755 ssl, ciphersuite_info, MBEDTLS_SSL_VERSION_TLS1_3,
Gilles Peskine449bd832023-01-11 14:50:10 +01001756 MBEDTLS_SSL_VERSION_TLS1_3) != 0) {
1757 MBEDTLS_SSL_DEBUG_MSG(4, ("%d is not a valid TLS 1.3 ciphersuite.",
1758 session->ciphersuite));
1759 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jerry Yu21092062022-10-10 21:21:31 +08001760 }
Jerry Yu40afab62022-10-08 10:42:13 +08001761 }
Jerry Yu21092062022-10-10 21:21:31 +08001762#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu40afab62022-10-08 10:42:13 +08001763
Gilles Peskine449bd832023-01-11 14:50:10 +01001764 if ((ret = mbedtls_ssl_session_copy(ssl->session_negotiate,
1765 session)) != 0) {
1766 return ret;
1767 }
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001768
Paul Bakker0a597072012-09-25 21:55:46 +00001769 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001770
Gilles Peskine449bd832023-01-11 14:50:10 +01001771 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001772}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001773#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001774
Gilles Peskine449bd832023-01-11 14:50:10 +01001775void mbedtls_ssl_conf_ciphersuites(mbedtls_ssl_config *conf,
1776 const int *ciphersuites)
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001777{
Hanno Beckerd60b6c62021-04-29 12:04:11 +01001778 conf->ciphersuite_list = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00001779}
1780
Ronald Cron6f135e12021-12-08 16:57:54 +01001781#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001782void mbedtls_ssl_conf_tls13_key_exchange_modes(mbedtls_ssl_config *conf,
1783 const int kex_modes)
Hanno Becker71f1ed62021-07-24 06:01:47 +01001784{
Xiaofei Bai746f9482021-11-12 08:53:56 +00001785 conf->tls13_kex_modes = kex_modes & MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Hanno Becker71f1ed62021-07-24 06:01:47 +01001786}
Xiaokang Qian72de95d2022-10-25 02:54:33 +00001787
1788#if defined(MBEDTLS_SSL_EARLY_DATA)
Gilles Peskine449bd832023-01-11 14:50:10 +01001789void mbedtls_ssl_tls13_conf_early_data(mbedtls_ssl_config *conf,
1790 int early_data_enabled)
Xiaokang Qian72de95d2022-10-25 02:54:33 +00001791{
1792 conf->early_data_enabled = early_data_enabled;
1793}
Jerry Yucc4e0072022-11-22 17:22:22 +08001794
1795#if defined(MBEDTLS_SSL_SRV_C)
1796void mbedtls_ssl_tls13_conf_max_early_data_size(
Gilles Peskine449bd832023-01-11 14:50:10 +01001797 mbedtls_ssl_config *conf, uint32_t max_early_data_size)
Jerry Yucc4e0072022-11-22 17:22:22 +08001798{
Jerry Yu39da9852022-12-06 16:58:36 +08001799 conf->max_early_data_size = max_early_data_size;
Jerry Yucc4e0072022-11-22 17:22:22 +08001800}
1801#endif /* MBEDTLS_SSL_SRV_C */
1802
Xiaokang Qian72de95d2022-10-25 02:54:33 +00001803#endif /* MBEDTLS_SSL_EARLY_DATA */
Ronald Cron6f135e12021-12-08 16:57:54 +01001804#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01001805
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001806#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001807void mbedtls_ssl_conf_cert_profile(mbedtls_ssl_config *conf,
1808 const mbedtls_x509_crt_profile *profile)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02001809{
1810 conf->cert_profile = profile;
1811}
1812
Gilles Peskine449bd832023-01-11 14:50:10 +01001813static void ssl_key_cert_free(mbedtls_ssl_key_cert *key_cert)
Glenn Strauss36872db2022-01-22 05:06:31 -05001814{
1815 mbedtls_ssl_key_cert *cur = key_cert, *next;
1816
Gilles Peskine449bd832023-01-11 14:50:10 +01001817 while (cur != NULL) {
Glenn Strauss36872db2022-01-22 05:06:31 -05001818 next = cur->next;
Gilles Peskine449bd832023-01-11 14:50:10 +01001819 mbedtls_free(cur);
Glenn Strauss36872db2022-01-22 05:06:31 -05001820 cur = next;
1821 }
1822}
1823
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001824/* Append a new keycert entry to a (possibly empty) list */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001825MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001826static int ssl_append_key_cert(mbedtls_ssl_key_cert **head,
1827 mbedtls_x509_crt *cert,
1828 mbedtls_pk_context *key)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001829{
niisato8ee24222018-06-25 19:05:48 +09001830 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001831
Gilles Peskine449bd832023-01-11 14:50:10 +01001832 if (cert == NULL) {
Glenn Strauss36872db2022-01-22 05:06:31 -05001833 /* Free list if cert is null */
Gilles Peskine449bd832023-01-11 14:50:10 +01001834 ssl_key_cert_free(*head);
Glenn Strauss36872db2022-01-22 05:06:31 -05001835 *head = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001836 return 0;
Glenn Strauss36872db2022-01-22 05:06:31 -05001837 }
1838
Gilles Peskine449bd832023-01-11 14:50:10 +01001839 new_cert = mbedtls_calloc(1, sizeof(mbedtls_ssl_key_cert));
1840 if (new_cert == NULL) {
1841 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
1842 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001843
niisato8ee24222018-06-25 19:05:48 +09001844 new_cert->cert = cert;
1845 new_cert->key = key;
1846 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001847
Glenn Strauss36872db2022-01-22 05:06:31 -05001848 /* Update head if the list was null, else add to the end */
Gilles Peskine449bd832023-01-11 14:50:10 +01001849 if (*head == NULL) {
niisato8ee24222018-06-25 19:05:48 +09001850 *head = new_cert;
Gilles Peskine449bd832023-01-11 14:50:10 +01001851 } else {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001852 mbedtls_ssl_key_cert *cur = *head;
Gilles Peskine449bd832023-01-11 14:50:10 +01001853 while (cur->next != NULL) {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001854 cur = cur->next;
Gilles Peskine449bd832023-01-11 14:50:10 +01001855 }
niisato8ee24222018-06-25 19:05:48 +09001856 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001857 }
1858
Gilles Peskine449bd832023-01-11 14:50:10 +01001859 return 0;
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001860}
1861
Gilles Peskine449bd832023-01-11 14:50:10 +01001862int mbedtls_ssl_conf_own_cert(mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001863 mbedtls_x509_crt *own_cert,
Gilles Peskine449bd832023-01-11 14:50:10 +01001864 mbedtls_pk_context *pk_key)
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001865{
Gilles Peskine449bd832023-01-11 14:50:10 +01001866 return ssl_append_key_cert(&conf->key_cert, own_cert, pk_key);
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001867}
1868
Gilles Peskine449bd832023-01-11 14:50:10 +01001869void mbedtls_ssl_conf_ca_chain(mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001870 mbedtls_x509_crt *ca_chain,
Gilles Peskine449bd832023-01-11 14:50:10 +01001871 mbedtls_x509_crl *ca_crl)
Paul Bakker5121ce52009-01-03 21:22:43 +00001872{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001873 conf->ca_chain = ca_chain;
1874 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00001875
1876#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1877 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1878 * cannot be used together. */
1879 conf->f_ca_cb = NULL;
1880 conf->p_ca_cb = NULL;
1881#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00001882}
Hanno Becker5adaad92019-03-27 16:54:37 +00001883
1884#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Gilles Peskine449bd832023-01-11 14:50:10 +01001885void mbedtls_ssl_conf_ca_cb(mbedtls_ssl_config *conf,
1886 mbedtls_x509_crt_ca_cb_t f_ca_cb,
1887 void *p_ca_cb)
Hanno Becker5adaad92019-03-27 16:54:37 +00001888{
1889 conf->f_ca_cb = f_ca_cb;
1890 conf->p_ca_cb = p_ca_cb;
1891
1892 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1893 * cannot be used together. */
1894 conf->ca_chain = NULL;
1895 conf->ca_crl = NULL;
1896}
1897#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001898#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00001899
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001900#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01001901const unsigned char *mbedtls_ssl_get_hs_sni(mbedtls_ssl_context *ssl,
1902 size_t *name_len)
Glenn Strauss69894072022-01-24 12:58:00 -05001903{
1904 *name_len = ssl->handshake->sni_name_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001905 return ssl->handshake->sni_name;
Glenn Strauss69894072022-01-24 12:58:00 -05001906}
1907
Gilles Peskine449bd832023-01-11 14:50:10 +01001908int mbedtls_ssl_set_hs_own_cert(mbedtls_ssl_context *ssl,
1909 mbedtls_x509_crt *own_cert,
1910 mbedtls_pk_context *pk_key)
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001911{
Gilles Peskine449bd832023-01-11 14:50:10 +01001912 return ssl_append_key_cert(&ssl->handshake->sni_key_cert,
1913 own_cert, pk_key);
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001914}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02001915
Gilles Peskine449bd832023-01-11 14:50:10 +01001916void mbedtls_ssl_set_hs_ca_chain(mbedtls_ssl_context *ssl,
1917 mbedtls_x509_crt *ca_chain,
1918 mbedtls_x509_crl *ca_crl)
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02001919{
1920 ssl->handshake->sni_ca_chain = ca_chain;
1921 ssl->handshake->sni_ca_crl = ca_crl;
1922}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001923
Glenn Strauss999ef702022-03-11 01:37:23 -05001924#if defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001925void mbedtls_ssl_set_hs_dn_hints(mbedtls_ssl_context *ssl,
1926 const mbedtls_x509_crt *crt)
Glenn Strauss999ef702022-03-11 01:37:23 -05001927{
1928 ssl->handshake->dn_hints = crt;
1929}
1930#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
1931
Gilles Peskine449bd832023-01-11 14:50:10 +01001932void mbedtls_ssl_set_hs_authmode(mbedtls_ssl_context *ssl,
1933 int authmode)
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001934{
1935 ssl->handshake->sni_authmode = authmode;
1936}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001937#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
1938
Hanno Becker8927c832019-04-03 12:52:50 +01001939#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001940void mbedtls_ssl_set_verify(mbedtls_ssl_context *ssl,
1941 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
1942 void *p_vrfy)
Hanno Becker8927c832019-04-03 12:52:50 +01001943{
1944 ssl->f_vrfy = f_vrfy;
1945 ssl->p_vrfy = p_vrfy;
1946}
1947#endif
1948
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02001949#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001950
Valerio Setti016f6822022-12-09 14:17:50 +01001951#if defined(MBEDTLS_USE_PSA_CRYPTO)
1952static psa_status_t mbedtls_ssl_set_hs_ecjpake_password_common(
Gilles Peskine449bd832023-01-11 14:50:10 +01001953 mbedtls_ssl_context *ssl,
1954 mbedtls_svc_key_id_t pwd)
Valerio Setti016f6822022-12-09 14:17:50 +01001955{
1956 psa_status_t status;
1957 psa_pake_role_t psa_role;
1958 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
1959
Gilles Peskine449bd832023-01-11 14:50:10 +01001960 psa_pake_cs_set_algorithm(&cipher_suite, PSA_ALG_JPAKE);
1961 psa_pake_cs_set_primitive(&cipher_suite,
1962 PSA_PAKE_PRIMITIVE(PSA_PAKE_PRIMITIVE_TYPE_ECC,
1963 PSA_ECC_FAMILY_SECP_R1,
1964 256));
1965 psa_pake_cs_set_hash(&cipher_suite, PSA_ALG_SHA_256);
Valerio Setti016f6822022-12-09 14:17:50 +01001966
Gilles Peskine449bd832023-01-11 14:50:10 +01001967 status = psa_pake_setup(&ssl->handshake->psa_pake_ctx, &cipher_suite);
1968 if (status != PSA_SUCCESS) {
Valerio Setti016f6822022-12-09 14:17:50 +01001969 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001970 }
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001971
Gilles Peskine449bd832023-01-11 14:50:10 +01001972 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Neil Armstrongca7d5062022-05-31 14:43:23 +02001973 psa_role = PSA_PAKE_ROLE_SERVER;
Gilles Peskine449bd832023-01-11 14:50:10 +01001974 } else {
Neil Armstrongca7d5062022-05-31 14:43:23 +02001975 psa_role = PSA_PAKE_ROLE_CLIENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001976 }
Neil Armstrongca7d5062022-05-31 14:43:23 +02001977
Gilles Peskine449bd832023-01-11 14:50:10 +01001978 status = psa_pake_set_role(&ssl->handshake->psa_pake_ctx, psa_role);
1979 if (status != PSA_SUCCESS) {
Valerio Setti016f6822022-12-09 14:17:50 +01001980 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001981 }
Valerio Setti016f6822022-12-09 14:17:50 +01001982
Gilles Peskine449bd832023-01-11 14:50:10 +01001983 status = psa_pake_set_password_key(&ssl->handshake->psa_pake_ctx, pwd);
1984 if (status != PSA_SUCCESS) {
Valerio Setti016f6822022-12-09 14:17:50 +01001985 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001986 }
Valerio Setti016f6822022-12-09 14:17:50 +01001987
1988 ssl->handshake->psa_pake_ctx_is_ok = 1;
1989
Gilles Peskine449bd832023-01-11 14:50:10 +01001990 return PSA_SUCCESS;
Valerio Setti016f6822022-12-09 14:17:50 +01001991}
1992
Gilles Peskine449bd832023-01-11 14:50:10 +01001993int mbedtls_ssl_set_hs_ecjpake_password(mbedtls_ssl_context *ssl,
1994 const unsigned char *pw,
1995 size_t pw_len)
Valerio Setti016f6822022-12-09 14:17:50 +01001996{
1997 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1998 psa_status_t status;
1999
Gilles Peskine449bd832023-01-11 14:50:10 +01002000 if (ssl->handshake == NULL || ssl->conf == NULL) {
2001 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Neil Armstrongca7d5062022-05-31 14:43:23 +02002002 }
2003
Gilles Peskine449bd832023-01-11 14:50:10 +01002004 /* Empty password is not valid */
2005 if ((pw == NULL) || (pw_len == 0)) {
2006 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2007 }
2008
2009 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
2010 psa_set_key_algorithm(&attributes, PSA_ALG_JPAKE);
2011 psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD);
2012
2013 status = psa_import_key(&attributes, pw, pw_len,
2014 &ssl->handshake->psa_pake_password);
2015 if (status != PSA_SUCCESS) {
2016 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2017 }
2018
2019 status = mbedtls_ssl_set_hs_ecjpake_password_common(ssl,
2020 ssl->handshake->psa_pake_password);
2021 if (status != PSA_SUCCESS) {
2022 psa_destroy_key(ssl->handshake->psa_pake_password);
2023 psa_pake_abort(&ssl->handshake->psa_pake_ctx);
2024 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2025 }
2026
2027 return 0;
Valerio Setti02c25b52022-11-15 14:08:42 +01002028}
Valerio Settia9a97dc2022-11-28 18:26:16 +01002029
Gilles Peskine449bd832023-01-11 14:50:10 +01002030int mbedtls_ssl_set_hs_ecjpake_password_opaque(mbedtls_ssl_context *ssl,
2031 mbedtls_svc_key_id_t pwd)
Valerio Settia9a97dc2022-11-28 18:26:16 +01002032{
Valerio Settia9a97dc2022-11-28 18:26:16 +01002033 psa_status_t status;
2034
Gilles Peskine449bd832023-01-11 14:50:10 +01002035 if (ssl->handshake == NULL || ssl->conf == NULL) {
2036 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Neil Armstrongca7d5062022-05-31 14:43:23 +02002037 }
2038
Gilles Peskine449bd832023-01-11 14:50:10 +01002039 if (mbedtls_svc_key_id_is_null(pwd)) {
2040 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2041 }
2042
2043 status = mbedtls_ssl_set_hs_ecjpake_password_common(ssl, pwd);
2044 if (status != PSA_SUCCESS) {
2045 psa_pake_abort(&ssl->handshake->psa_pake_ctx);
2046 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2047 }
2048
2049 return 0;
Valerio Setti02c25b52022-11-15 14:08:42 +01002050}
2051#else /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01002052int mbedtls_ssl_set_hs_ecjpake_password(mbedtls_ssl_context *ssl,
2053 const unsigned char *pw,
2054 size_t pw_len)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002055{
2056 mbedtls_ecjpake_role role;
2057
Gilles Peskine449bd832023-01-11 14:50:10 +01002058 if (ssl->handshake == NULL || ssl->conf == NULL) {
2059 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2060 }
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002061
Valerio Settic689ed82022-12-07 14:40:38 +01002062 /* Empty password is not valid */
Gilles Peskine449bd832023-01-11 14:50:10 +01002063 if ((pw == NULL) || (pw_len == 0)) {
2064 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2065 }
Valerio Settic689ed82022-12-07 14:40:38 +01002066
Gilles Peskine449bd832023-01-11 14:50:10 +01002067 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002068 role = MBEDTLS_ECJPAKE_SERVER;
Gilles Peskine449bd832023-01-11 14:50:10 +01002069 } else {
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002070 role = MBEDTLS_ECJPAKE_CLIENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01002071 }
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002072
Gilles Peskine449bd832023-01-11 14:50:10 +01002073 return mbedtls_ecjpake_setup(&ssl->handshake->ecjpake_ctx,
2074 role,
2075 MBEDTLS_MD_SHA256,
2076 MBEDTLS_ECP_DP_SECP256R1,
2077 pw, pw_len);
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002078}
Valerio Setti02c25b52022-11-15 14:08:42 +01002079#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02002080#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002081
Ronald Cron73fe8df2022-10-05 14:31:43 +02002082#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002083int mbedtls_ssl_conf_has_static_psk(mbedtls_ssl_config const *conf)
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002084{
Gilles Peskine449bd832023-01-11 14:50:10 +01002085 if (conf->psk_identity == NULL ||
2086 conf->psk_identity_len == 0) {
2087 return 0;
Ronald Crond29e13e2022-10-19 10:33:48 +02002088 }
2089
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002090#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002091 if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) {
2092 return 1;
2093 }
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002094#endif /* MBEDTLS_USE_PSA_CRYPTO */
Ronald Crond29e13e2022-10-19 10:33:48 +02002095
Gilles Peskine449bd832023-01-11 14:50:10 +01002096 if (conf->psk != NULL && conf->psk_len != 0) {
2097 return 1;
2098 }
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002099
Gilles Peskine449bd832023-01-11 14:50:10 +01002100 return 0;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002101}
2102
Gilles Peskine449bd832023-01-11 14:50:10 +01002103static void ssl_conf_remove_psk(mbedtls_ssl_config *conf)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002104{
2105 /* Remove reference to existing PSK, if any. */
2106#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002107 if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) {
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002108 /* The maintenance of the PSK key slot is the
2109 * user's responsibility. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002110 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002111 }
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002112#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01002113 if (conf->psk != NULL) {
2114 mbedtls_platform_zeroize(conf->psk, conf->psk_len);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002115
Gilles Peskine449bd832023-01-11 14:50:10 +01002116 mbedtls_free(conf->psk);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002117 conf->psk = NULL;
2118 conf->psk_len = 0;
2119 }
2120
2121 /* Remove reference to PSK identity, if any. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002122 if (conf->psk_identity != NULL) {
2123 mbedtls_free(conf->psk_identity);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002124 conf->psk_identity = NULL;
2125 conf->psk_identity_len = 0;
2126 }
2127}
2128
Hanno Becker7390c712018-11-15 13:33:04 +00002129/* This function assumes that PSK identity in the SSL config is unset.
2130 * It checks that the provided identity is well-formed and attempts
2131 * to make a copy of it in the SSL config.
2132 * On failure, the PSK identity in the config remains unset. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002133MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002134static int ssl_conf_set_psk_identity(mbedtls_ssl_config *conf,
2135 unsigned char const *psk_identity,
2136 size_t psk_identity_len)
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002137{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02002138 /* Identity len will be encoded on two bytes */
Gilles Peskine449bd832023-01-11 14:50:10 +01002139 if (psk_identity == NULL ||
Ronald Cron2a87e9b2022-10-19 10:55:26 +02002140 psk_identity_len == 0 ||
Gilles Peskine449bd832023-01-11 14:50:10 +01002141 (psk_identity_len >> 16) != 0 ||
2142 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN) {
2143 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02002144 }
2145
Gilles Peskine449bd832023-01-11 14:50:10 +01002146 conf->psk_identity = mbedtls_calloc(1, psk_identity_len);
2147 if (conf->psk_identity == NULL) {
2148 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2149 }
Paul Bakker6db455e2013-09-18 17:29:31 +02002150
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01002151 conf->psk_identity_len = psk_identity_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002152 memcpy(conf->psk_identity, psk_identity, conf->psk_identity_len);
Paul Bakker5ad403f2013-09-18 21:21:30 +02002153
Gilles Peskine449bd832023-01-11 14:50:10 +01002154 return 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02002155}
2156
Gilles Peskine449bd832023-01-11 14:50:10 +01002157int mbedtls_ssl_conf_psk(mbedtls_ssl_config *conf,
2158 const unsigned char *psk, size_t psk_len,
2159 const unsigned char *psk_identity, size_t psk_identity_len)
Hanno Becker7390c712018-11-15 13:33:04 +00002160{
Janos Follath865b3eb2019-12-16 11:46:15 +00002161 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002162
2163 /* We currently only support one PSK, raw or opaque. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002164 if (mbedtls_ssl_conf_has_static_psk(conf)) {
2165 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
2166 }
Hanno Becker7390c712018-11-15 13:33:04 +00002167
2168 /* Check and set raw PSK */
Gilles Peskine449bd832023-01-11 14:50:10 +01002169 if (psk == NULL) {
2170 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2171 }
2172 if (psk_len == 0) {
2173 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2174 }
2175 if (psk_len > MBEDTLS_PSK_MAX_LEN) {
2176 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2177 }
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01002178
Gilles Peskine449bd832023-01-11 14:50:10 +01002179 if ((conf->psk = mbedtls_calloc(1, psk_len)) == NULL) {
2180 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2181 }
Hanno Becker7390c712018-11-15 13:33:04 +00002182 conf->psk_len = psk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002183 memcpy(conf->psk, psk, conf->psk_len);
Hanno Becker7390c712018-11-15 13:33:04 +00002184
2185 /* Check and set PSK Identity */
Gilles Peskine449bd832023-01-11 14:50:10 +01002186 ret = ssl_conf_set_psk_identity(conf, psk_identity, psk_identity_len);
2187 if (ret != 0) {
2188 ssl_conf_remove_psk(conf);
2189 }
Hanno Becker7390c712018-11-15 13:33:04 +00002190
Gilles Peskine449bd832023-01-11 14:50:10 +01002191 return ret;
Hanno Becker7390c712018-11-15 13:33:04 +00002192}
2193
Gilles Peskine449bd832023-01-11 14:50:10 +01002194static void ssl_remove_psk(mbedtls_ssl_context *ssl)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002195{
2196#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002197 if (!mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque)) {
Neil Armstrong501c9322022-05-03 09:35:09 +02002198 /* The maintenance of the external PSK key slot is the
2199 * user's responsibility. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002200 if (ssl->handshake->psk_opaque_is_internal) {
2201 psa_destroy_key(ssl->handshake->psk_opaque);
Neil Armstrong501c9322022-05-03 09:35:09 +02002202 ssl->handshake->psk_opaque_is_internal = 0;
2203 }
Ronald Croncf56a0a2020-08-04 09:51:30 +02002204 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002205 }
Neil Armstronge952a302022-05-03 10:22:14 +02002206#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002207 if (ssl->handshake->psk != NULL) {
2208 mbedtls_platform_zeroize(ssl->handshake->psk,
2209 ssl->handshake->psk_len);
2210 mbedtls_free(ssl->handshake->psk);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002211 ssl->handshake->psk_len = 0;
2212 }
Neil Armstronge952a302022-05-03 10:22:14 +02002213#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002214}
2215
Gilles Peskine449bd832023-01-11 14:50:10 +01002216int mbedtls_ssl_set_hs_psk(mbedtls_ssl_context *ssl,
2217 const unsigned char *psk, size_t psk_len)
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002218{
Neil Armstrong501c9322022-05-03 09:35:09 +02002219#if defined(MBEDTLS_USE_PSA_CRYPTO)
2220 psa_key_attributes_t key_attributes = psa_key_attributes_init();
Jerry Yu5d01c052022-08-17 10:18:10 +08002221 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jerry Yu24b8c812022-08-20 19:06:56 +08002222 psa_algorithm_t alg = PSA_ALG_NONE;
Jerry Yu5d01c052022-08-17 10:18:10 +08002223 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrong501c9322022-05-03 09:35:09 +02002224#endif /* MBEDTLS_USE_PSA_CRYPTO */
2225
Gilles Peskine449bd832023-01-11 14:50:10 +01002226 if (psk == NULL || ssl->handshake == NULL) {
2227 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2228 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002229
Gilles Peskine449bd832023-01-11 14:50:10 +01002230 if (psk_len > MBEDTLS_PSK_MAX_LEN) {
2231 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2232 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002233
Gilles Peskine449bd832023-01-11 14:50:10 +01002234 ssl_remove_psk(ssl);
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002235
Neil Armstrong501c9322022-05-03 09:35:09 +02002236#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yuccc68a42022-07-26 16:39:20 +08002237#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01002238 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2) {
2239 if (ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384) {
2240 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
2241 } else {
2242 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
2243 }
2244 psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE);
Jerry Yuccc68a42022-07-26 16:39:20 +08002245 }
2246#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Neil Armstrong501c9322022-05-03 09:35:09 +02002247
Jerry Yu568ec252022-07-22 21:27:34 +08002248#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01002249 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
2250 alg = PSA_ALG_HKDF_EXTRACT(PSA_ALG_ANY_HASH);
2251 psa_set_key_usage_flags(&key_attributes,
2252 PSA_KEY_USAGE_DERIVE | PSA_KEY_USAGE_EXPORT);
Jerry Yuccc68a42022-07-26 16:39:20 +08002253 }
2254#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
2255
Gilles Peskine449bd832023-01-11 14:50:10 +01002256 psa_set_key_algorithm(&key_attributes, alg);
2257 psa_set_key_type(&key_attributes, PSA_KEY_TYPE_DERIVE);
Neil Armstrong501c9322022-05-03 09:35:09 +02002258
Gilles Peskine449bd832023-01-11 14:50:10 +01002259 status = psa_import_key(&key_attributes, psk, psk_len, &key);
2260 if (status != PSA_SUCCESS) {
2261 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2262 }
Neil Armstrong501c9322022-05-03 09:35:09 +02002263
2264 /* Allow calling psa_destroy_key() on psk remove */
2265 ssl->handshake->psk_opaque_is_internal = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01002266 return mbedtls_ssl_set_hs_psk_opaque(ssl, key);
Neil Armstrong501c9322022-05-03 09:35:09 +02002267#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002268 if ((ssl->handshake->psk = mbedtls_calloc(1, psk_len)) == NULL) {
2269 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2270 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002271
2272 ssl->handshake->psk_len = psk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002273 memcpy(ssl->handshake->psk, psk, ssl->handshake->psk_len);
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002274
Gilles Peskine449bd832023-01-11 14:50:10 +01002275 return 0;
Neil Armstrong501c9322022-05-03 09:35:09 +02002276#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002277}
2278
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002279#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002280int mbedtls_ssl_conf_psk_opaque(mbedtls_ssl_config *conf,
2281 mbedtls_svc_key_id_t psk,
2282 const unsigned char *psk_identity,
2283 size_t psk_identity_len)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002284{
Janos Follath865b3eb2019-12-16 11:46:15 +00002285 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002286
2287 /* We currently only support one PSK, raw or opaque. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002288 if (mbedtls_ssl_conf_has_static_psk(conf)) {
2289 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
2290 }
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002291
Hanno Becker7390c712018-11-15 13:33:04 +00002292 /* Check and set opaque PSK */
Gilles Peskine449bd832023-01-11 14:50:10 +01002293 if (mbedtls_svc_key_id_is_null(psk)) {
2294 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2295 }
Ronald Croncf56a0a2020-08-04 09:51:30 +02002296 conf->psk_opaque = psk;
Hanno Becker7390c712018-11-15 13:33:04 +00002297
2298 /* Check and set PSK Identity */
Gilles Peskine449bd832023-01-11 14:50:10 +01002299 ret = ssl_conf_set_psk_identity(conf, psk_identity,
2300 psk_identity_len);
2301 if (ret != 0) {
2302 ssl_conf_remove_psk(conf);
2303 }
Hanno Becker7390c712018-11-15 13:33:04 +00002304
Gilles Peskine449bd832023-01-11 14:50:10 +01002305 return ret;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002306}
2307
Gilles Peskine449bd832023-01-11 14:50:10 +01002308int mbedtls_ssl_set_hs_psk_opaque(mbedtls_ssl_context *ssl,
2309 mbedtls_svc_key_id_t psk)
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002310{
Gilles Peskine449bd832023-01-11 14:50:10 +01002311 if ((mbedtls_svc_key_id_is_null(psk)) ||
2312 (ssl->handshake == NULL)) {
2313 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2314 }
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002315
Gilles Peskine449bd832023-01-11 14:50:10 +01002316 ssl_remove_psk(ssl);
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002317 ssl->handshake->psk_opaque = psk;
Gilles Peskine449bd832023-01-11 14:50:10 +01002318 return 0;
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002319}
2320#endif /* MBEDTLS_USE_PSA_CRYPTO */
2321
Jerry Yu8897c072022-08-12 13:56:53 +08002322#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002323void mbedtls_ssl_conf_psk_cb(mbedtls_ssl_config *conf,
2324 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
2325 size_t),
2326 void *p_psk)
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002327{
2328 conf->f_psk = f_psk;
2329 conf->p_psk = p_psk;
2330}
Jerry Yu8897c072022-08-12 13:56:53 +08002331#endif /* MBEDTLS_SSL_SRV_C */
2332
Ronald Cron73fe8df2022-10-05 14:31:43 +02002333#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002334
2335#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine301711e2022-04-26 16:57:05 +02002336static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
Gilles Peskine449bd832023-01-11 14:50:10 +01002337 psa_algorithm_t alg)
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002338{
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002339#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01002340 if (alg == PSA_ALG_CBC_NO_PADDING) {
2341 return MBEDTLS_SSL_MODE_CBC;
2342 }
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002343#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
Gilles Peskine449bd832023-01-11 14:50:10 +01002344 if (PSA_ALG_IS_AEAD(alg)) {
2345 return MBEDTLS_SSL_MODE_AEAD;
2346 }
2347 return MBEDTLS_SSL_MODE_STREAM;
Gilles Peskine301711e2022-04-26 16:57:05 +02002348}
2349
2350#else /* MBEDTLS_USE_PSA_CRYPTO */
2351
2352static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
Gilles Peskine449bd832023-01-11 14:50:10 +01002353 mbedtls_cipher_mode_t mode)
Gilles Peskine301711e2022-04-26 16:57:05 +02002354{
2355#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01002356 if (mode == MBEDTLS_MODE_CBC) {
2357 return MBEDTLS_SSL_MODE_CBC;
2358 }
Gilles Peskine301711e2022-04-26 16:57:05 +02002359#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
2360
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002361#if defined(MBEDTLS_GCM_C) || \
2362 defined(MBEDTLS_CCM_C) || \
2363 defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002364 if (mode == MBEDTLS_MODE_GCM ||
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002365 mode == MBEDTLS_MODE_CCM ||
Gilles Peskine449bd832023-01-11 14:50:10 +01002366 mode == MBEDTLS_MODE_CHACHAPOLY) {
2367 return MBEDTLS_SSL_MODE_AEAD;
2368 }
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002369#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002370
Gilles Peskine449bd832023-01-11 14:50:10 +01002371 return MBEDTLS_SSL_MODE_STREAM;
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002372}
Gilles Peskine301711e2022-04-26 16:57:05 +02002373#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002374
Gilles Peskinee108d982022-04-26 16:50:40 +02002375static mbedtls_ssl_mode_t mbedtls_ssl_get_actual_mode(
2376 mbedtls_ssl_mode_t base_mode,
Gilles Peskine449bd832023-01-11 14:50:10 +01002377 int encrypt_then_mac)
Gilles Peskinee108d982022-04-26 16:50:40 +02002378{
2379#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01002380 if (encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED &&
2381 base_mode == MBEDTLS_SSL_MODE_CBC) {
2382 return MBEDTLS_SSL_MODE_CBC_ETM;
Gilles Peskinee108d982022-04-26 16:50:40 +02002383 }
2384#else
2385 (void) encrypt_then_mac;
2386#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002387 return base_mode;
Gilles Peskinee108d982022-04-26 16:50:40 +02002388}
2389
Neil Armstrongab555e02022-04-04 11:07:59 +02002390mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_transform(
Gilles Peskine449bd832023-01-11 14:50:10 +01002391 const mbedtls_ssl_transform *transform)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002392{
Gilles Peskinee108d982022-04-26 16:50:40 +02002393 mbedtls_ssl_mode_t base_mode = mbedtls_ssl_get_base_mode(
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002394#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002395 transform->psa_alg
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002396#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002397 mbedtls_cipher_get_cipher_mode(&transform->cipher_ctx_enc)
Gilles Peskinee108d982022-04-26 16:50:40 +02002398#endif
2399 );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002400
Gilles Peskinee108d982022-04-26 16:50:40 +02002401 int encrypt_then_mac = 0;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002402#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskinee108d982022-04-26 16:50:40 +02002403 encrypt_then_mac = transform->encrypt_then_mac;
2404#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002405 return mbedtls_ssl_get_actual_mode(base_mode, encrypt_then_mac);
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002406}
2407
Neil Armstrongab555e02022-04-04 11:07:59 +02002408mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002409#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01002410 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002411#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01002412 const mbedtls_ssl_ciphersuite_t *suite)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002413{
Gilles Peskinee108d982022-04-26 16:50:40 +02002414 mbedtls_ssl_mode_t base_mode = MBEDTLS_SSL_MODE_STREAM;
2415
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002416#if defined(MBEDTLS_USE_PSA_CRYPTO)
2417 psa_status_t status;
2418 psa_algorithm_t alg;
2419 psa_key_type_t type;
2420 size_t size;
Gilles Peskine449bd832023-01-11 14:50:10 +01002421 status = mbedtls_ssl_cipher_to_psa(suite->cipher, 0, &alg, &type, &size);
2422 if (status == PSA_SUCCESS) {
2423 base_mode = mbedtls_ssl_get_base_mode(alg);
2424 }
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002425#else
2426 const mbedtls_cipher_info_t *cipher =
Gilles Peskine449bd832023-01-11 14:50:10 +01002427 mbedtls_cipher_info_from_type(suite->cipher);
2428 if (cipher != NULL) {
Gilles Peskinee108d982022-04-26 16:50:40 +02002429 base_mode =
2430 mbedtls_ssl_get_base_mode(
Gilles Peskine449bd832023-01-11 14:50:10 +01002431 mbedtls_cipher_info_get_mode(cipher));
Gilles Peskinee108d982022-04-26 16:50:40 +02002432 }
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002433#endif /* MBEDTLS_USE_PSA_CRYPTO */
2434
Gilles Peskinee108d982022-04-26 16:50:40 +02002435#if !defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
2436 int encrypt_then_mac = 0;
2437#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002438 return mbedtls_ssl_get_actual_mode(base_mode, encrypt_then_mac);
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002439}
2440
Neil Armstrong8395d7a2022-05-18 11:44:56 +02002441#if defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu251a12e2022-07-13 15:15:48 +08002442
2443#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu438ddd82022-07-07 06:55:50 +00002444/* Serialization of TLS 1.3 sessions:
2445 *
2446 * struct {
Xiaokang Qian126bf8e2022-10-13 02:22:40 +00002447 * opaque hostname<0..2^16-1>;
Jerry Yu438ddd82022-07-07 06:55:50 +00002448 * uint64 ticket_received;
2449 * uint32 ticket_lifetime;
Jerry Yu34191072022-08-18 10:32:09 +08002450 * opaque ticket<1..2^16-1>;
Jerry Yu438ddd82022-07-07 06:55:50 +00002451 * } ClientOnlyData;
2452 *
2453 * struct {
2454 * uint8 endpoint;
2455 * uint8 ciphersuite[2];
2456 * uint32 ticket_age_add;
2457 * uint8 ticket_flags;
2458 * opaque resumption_key<0..255>;
2459 * select ( endpoint ) {
2460 * case client: ClientOnlyData;
2461 * case server: uint64 start_time;
2462 * };
2463 * } serialized_session_tls13;
2464 *
2465 */
2466#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yue36fdd62022-08-17 21:31:36 +08002467MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002468static int ssl_tls13_session_save(const mbedtls_ssl_session *session,
2469 unsigned char *buf,
2470 size_t buf_len,
2471 size_t *olen)
Jerry Yu438ddd82022-07-07 06:55:50 +00002472{
2473 unsigned char *p = buf;
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00002474#if defined(MBEDTLS_SSL_CLI_C) && \
2475 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01002476 size_t hostname_len = (session->hostname == NULL) ?
2477 0 : strlen(session->hostname) + 1;
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00002478#endif
Jerry Yubc7c1a42022-07-21 22:57:37 +08002479 size_t needed = 1 /* endpoint */
2480 + 2 /* ciphersuite */
2481 + 4 /* ticket_age_add */
Jerry Yu34191072022-08-18 10:32:09 +08002482 + 1 /* ticket_flags */
2483 + 1; /* resumption_key length */
Jerry Yue36fdd62022-08-17 21:31:36 +08002484 *olen = 0;
Jerry Yu34191072022-08-18 10:32:09 +08002485
Gilles Peskine449bd832023-01-11 14:50:10 +01002486 if (session->resumption_key_len > MBEDTLS_SSL_TLS1_3_TICKET_RESUMPTION_KEY_LEN) {
2487 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2488 }
Jerry Yu34191072022-08-18 10:32:09 +08002489 needed += session->resumption_key_len; /* resumption_key */
2490
Jerry Yu438ddd82022-07-07 06:55:50 +00002491#if defined(MBEDTLS_HAVE_TIME)
2492 needed += 8; /* start_time or ticket_received */
2493#endif
2494
2495#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002496 if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Xiaokang Qian126bf8e2022-10-13 02:22:40 +00002497#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaokang Qianbc663a02022-10-09 11:14:39 +00002498 needed += 2 /* hostname_len */
Gilles Peskine449bd832023-01-11 14:50:10 +01002499 + hostname_len; /* hostname */
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00002500#endif
2501
Jerry Yu438ddd82022-07-07 06:55:50 +00002502 needed += 4 /* ticket_lifetime */
Jerry Yue36fdd62022-08-17 21:31:36 +08002503 + 2; /* ticket_len */
Jerry Yu34191072022-08-18 10:32:09 +08002504
2505 /* Check size_t overflow */
Gilles Peskine449bd832023-01-11 14:50:10 +01002506 if (session->ticket_len > SIZE_MAX - needed) {
2507 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2508 }
Jerry Yu34191072022-08-18 10:32:09 +08002509
Jerry Yue28d9742022-08-18 15:44:03 +08002510 needed += session->ticket_len; /* ticket */
Jerry Yu438ddd82022-07-07 06:55:50 +00002511 }
2512#endif /* MBEDTLS_SSL_CLI_C */
2513
Jerry Yue36fdd62022-08-17 21:31:36 +08002514 *olen = needed;
Gilles Peskine449bd832023-01-11 14:50:10 +01002515 if (needed > buf_len) {
2516 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
2517 }
Jerry Yu438ddd82022-07-07 06:55:50 +00002518
2519 p[0] = session->endpoint;
Gilles Peskine449bd832023-01-11 14:50:10 +01002520 MBEDTLS_PUT_UINT16_BE(session->ciphersuite, p, 1);
2521 MBEDTLS_PUT_UINT32_BE(session->ticket_age_add, p, 3);
Jerry Yu438ddd82022-07-07 06:55:50 +00002522 p[7] = session->ticket_flags;
2523
2524 /* save resumption_key */
Jerry Yubc7c1a42022-07-21 22:57:37 +08002525 p[8] = session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002526 p += 9;
Gilles Peskine449bd832023-01-11 14:50:10 +01002527 memcpy(p, session->resumption_key, session->resumption_key_len);
Jerry Yubc7c1a42022-07-21 22:57:37 +08002528 p += session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002529
2530#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002531 if (session->endpoint == MBEDTLS_SSL_IS_SERVER) {
2532 MBEDTLS_PUT_UINT64_BE((uint64_t) session->start, p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002533 p += 8;
2534 }
2535#endif /* MBEDTLS_HAVE_TIME */
2536
2537#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002538 if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002539#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01002540 MBEDTLS_PUT_UINT16_BE(hostname_len, p, 0);
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002541 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002542 if (hostname_len > 0) {
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002543 /* save host name */
Gilles Peskine449bd832023-01-11 14:50:10 +01002544 memcpy(p, session->hostname, hostname_len);
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002545 p += hostname_len;
2546 }
2547#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
2548
Jerry Yu438ddd82022-07-07 06:55:50 +00002549#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01002550 MBEDTLS_PUT_UINT64_BE((uint64_t) session->ticket_received, p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002551 p += 8;
2552#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002553 MBEDTLS_PUT_UINT32_BE(session->ticket_lifetime, p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002554 p += 4;
2555
Gilles Peskine449bd832023-01-11 14:50:10 +01002556 MBEDTLS_PUT_UINT16_BE(session->ticket_len, p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002557 p += 2;
Jerry Yu34191072022-08-18 10:32:09 +08002558
Gilles Peskine449bd832023-01-11 14:50:10 +01002559 if (session->ticket != NULL && session->ticket_len > 0) {
2560 memcpy(p, session->ticket, session->ticket_len);
Jerry Yu438ddd82022-07-07 06:55:50 +00002561 p += session->ticket_len;
2562 }
2563 }
2564#endif /* MBEDTLS_SSL_CLI_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01002565 return 0;
Jerry Yu438ddd82022-07-07 06:55:50 +00002566}
2567
2568MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002569static int ssl_tls13_session_load(mbedtls_ssl_session *session,
2570 const unsigned char *buf,
2571 size_t len)
Jerry Yu438ddd82022-07-07 06:55:50 +00002572{
2573 const unsigned char *p = buf;
2574 const unsigned char *end = buf + len;
2575
Gilles Peskine449bd832023-01-11 14:50:10 +01002576 if (end - p < 9) {
2577 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2578 }
Jerry Yu438ddd82022-07-07 06:55:50 +00002579 session->endpoint = p[0];
Gilles Peskine449bd832023-01-11 14:50:10 +01002580 session->ciphersuite = MBEDTLS_GET_UINT16_BE(p, 1);
2581 session->ticket_age_add = MBEDTLS_GET_UINT32_BE(p, 3);
Jerry Yu438ddd82022-07-07 06:55:50 +00002582 session->ticket_flags = p[7];
2583
2584 /* load resumption_key */
Jerry Yubc7c1a42022-07-21 22:57:37 +08002585 session->resumption_key_len = p[8];
Jerry Yu438ddd82022-07-07 06:55:50 +00002586 p += 9;
2587
Gilles Peskine449bd832023-01-11 14:50:10 +01002588 if (end - p < session->resumption_key_len) {
2589 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2590 }
Jerry Yu438ddd82022-07-07 06:55:50 +00002591
Gilles Peskine449bd832023-01-11 14:50:10 +01002592 if (sizeof(session->resumption_key) < session->resumption_key_len) {
2593 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2594 }
2595 memcpy(session->resumption_key, p, session->resumption_key_len);
Jerry Yubc7c1a42022-07-21 22:57:37 +08002596 p += session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002597
2598#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002599 if (session->endpoint == MBEDTLS_SSL_IS_SERVER) {
2600 if (end - p < 8) {
2601 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2602 }
2603 session->start = MBEDTLS_GET_UINT64_BE(p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002604 p += 8;
2605 }
2606#endif /* MBEDTLS_HAVE_TIME */
2607
2608#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002609 if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002610#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01002611 defined(MBEDTLS_SSL_SESSION_TICKETS)
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002612 size_t hostname_len;
2613 /* load host name */
Gilles Peskine449bd832023-01-11 14:50:10 +01002614 if (end - p < 2) {
2615 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2616 }
2617 hostname_len = MBEDTLS_GET_UINT16_BE(p, 0);
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002618 p += 2;
2619
Gilles Peskine449bd832023-01-11 14:50:10 +01002620 if (end - p < (long int) hostname_len) {
2621 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2622 }
2623 if (hostname_len > 0) {
2624 session->hostname = mbedtls_calloc(1, hostname_len);
2625 if (session->hostname == NULL) {
2626 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2627 }
2628 memcpy(session->hostname, p, hostname_len);
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002629 p += hostname_len;
2630 }
2631#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION &&
2632 MBEDTLS_SSL_SESSION_TICKETS */
2633
Jerry Yu438ddd82022-07-07 06:55:50 +00002634#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01002635 if (end - p < 8) {
2636 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2637 }
2638 session->ticket_received = MBEDTLS_GET_UINT64_BE(p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002639 p += 8;
2640#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002641 if (end - p < 4) {
2642 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2643 }
2644 session->ticket_lifetime = MBEDTLS_GET_UINT32_BE(p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002645 p += 4;
2646
Gilles Peskine449bd832023-01-11 14:50:10 +01002647 if (end - p < 2) {
2648 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2649 }
2650 session->ticket_len = MBEDTLS_GET_UINT16_BE(p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002651 p += 2;
2652
Gilles Peskine449bd832023-01-11 14:50:10 +01002653 if (end - p < (long int) session->ticket_len) {
2654 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2655 }
2656 if (session->ticket_len > 0) {
2657 session->ticket = mbedtls_calloc(1, session->ticket_len);
2658 if (session->ticket == NULL) {
2659 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2660 }
2661 memcpy(session->ticket, p, session->ticket_len);
Jerry Yu438ddd82022-07-07 06:55:50 +00002662 p += session->ticket_len;
2663 }
2664 }
2665#endif /* MBEDTLS_SSL_CLI_C */
2666
Gilles Peskine449bd832023-01-11 14:50:10 +01002667 return 0;
Jerry Yu438ddd82022-07-07 06:55:50 +00002668
2669}
2670#else /* MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yue36fdd62022-08-17 21:31:36 +08002671MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002672static int ssl_tls13_session_save(const mbedtls_ssl_session *session,
2673 unsigned char *buf,
2674 size_t buf_len,
2675 size_t *olen)
Jerry Yu251a12e2022-07-13 15:15:48 +08002676{
2677 ((void) session);
2678 ((void) buf);
2679 ((void) buf_len);
Jerry Yue36fdd62022-08-17 21:31:36 +08002680 *olen = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002681 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Jerry Yu251a12e2022-07-13 15:15:48 +08002682}
Jerry Yu438ddd82022-07-07 06:55:50 +00002683
Gilles Peskine449bd832023-01-11 14:50:10 +01002684static int ssl_tls13_session_load(const mbedtls_ssl_session *session,
2685 unsigned char *buf,
2686 size_t buf_len)
Jerry Yu438ddd82022-07-07 06:55:50 +00002687{
2688 ((void) session);
2689 ((void) buf);
2690 ((void) buf_len);
Gilles Peskine449bd832023-01-11 14:50:10 +01002691 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Jerry Yu438ddd82022-07-07 06:55:50 +00002692}
2693#endif /* !MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yu251a12e2022-07-13 15:15:48 +08002694#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
2695
Gilles Peskine449bd832023-01-11 14:50:10 +01002696psa_status_t mbedtls_ssl_cipher_to_psa(mbedtls_cipher_type_t mbedtls_cipher_type,
2697 size_t taglen,
2698 psa_algorithm_t *alg,
2699 psa_key_type_t *key_type,
2700 size_t *key_size)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002701{
Gilles Peskine449bd832023-01-11 14:50:10 +01002702 switch (mbedtls_cipher_type) {
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002703 case MBEDTLS_CIPHER_AES_128_CBC:
2704 *alg = PSA_ALG_CBC_NO_PADDING;
2705 *key_type = PSA_KEY_TYPE_AES;
2706 *key_size = 128;
2707 break;
2708 case MBEDTLS_CIPHER_AES_128_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002709 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002710 *key_type = PSA_KEY_TYPE_AES;
2711 *key_size = 128;
2712 break;
2713 case MBEDTLS_CIPHER_AES_128_GCM:
2714 *alg = PSA_ALG_GCM;
2715 *key_type = PSA_KEY_TYPE_AES;
2716 *key_size = 128;
2717 break;
2718 case MBEDTLS_CIPHER_AES_192_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002719 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002720 *key_type = PSA_KEY_TYPE_AES;
2721 *key_size = 192;
2722 break;
2723 case MBEDTLS_CIPHER_AES_192_GCM:
2724 *alg = PSA_ALG_GCM;
2725 *key_type = PSA_KEY_TYPE_AES;
2726 *key_size = 192;
2727 break;
2728 case MBEDTLS_CIPHER_AES_256_CBC:
2729 *alg = PSA_ALG_CBC_NO_PADDING;
2730 *key_type = PSA_KEY_TYPE_AES;
2731 *key_size = 256;
2732 break;
2733 case MBEDTLS_CIPHER_AES_256_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002734 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002735 *key_type = PSA_KEY_TYPE_AES;
2736 *key_size = 256;
2737 break;
2738 case MBEDTLS_CIPHER_AES_256_GCM:
2739 *alg = PSA_ALG_GCM;
2740 *key_type = PSA_KEY_TYPE_AES;
2741 *key_size = 256;
2742 break;
2743 case MBEDTLS_CIPHER_ARIA_128_CBC:
2744 *alg = PSA_ALG_CBC_NO_PADDING;
2745 *key_type = PSA_KEY_TYPE_ARIA;
2746 *key_size = 128;
2747 break;
2748 case MBEDTLS_CIPHER_ARIA_128_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002749 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002750 *key_type = PSA_KEY_TYPE_ARIA;
2751 *key_size = 128;
2752 break;
2753 case MBEDTLS_CIPHER_ARIA_128_GCM:
2754 *alg = PSA_ALG_GCM;
2755 *key_type = PSA_KEY_TYPE_ARIA;
2756 *key_size = 128;
2757 break;
2758 case MBEDTLS_CIPHER_ARIA_192_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002759 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002760 *key_type = PSA_KEY_TYPE_ARIA;
2761 *key_size = 192;
2762 break;
2763 case MBEDTLS_CIPHER_ARIA_192_GCM:
2764 *alg = PSA_ALG_GCM;
2765 *key_type = PSA_KEY_TYPE_ARIA;
2766 *key_size = 192;
2767 break;
2768 case MBEDTLS_CIPHER_ARIA_256_CBC:
2769 *alg = PSA_ALG_CBC_NO_PADDING;
2770 *key_type = PSA_KEY_TYPE_ARIA;
2771 *key_size = 256;
2772 break;
2773 case MBEDTLS_CIPHER_ARIA_256_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002774 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002775 *key_type = PSA_KEY_TYPE_ARIA;
2776 *key_size = 256;
2777 break;
2778 case MBEDTLS_CIPHER_ARIA_256_GCM:
2779 *alg = PSA_ALG_GCM;
2780 *key_type = PSA_KEY_TYPE_ARIA;
2781 *key_size = 256;
2782 break;
2783 case MBEDTLS_CIPHER_CAMELLIA_128_CBC:
2784 *alg = PSA_ALG_CBC_NO_PADDING;
2785 *key_type = PSA_KEY_TYPE_CAMELLIA;
2786 *key_size = 128;
2787 break;
2788 case MBEDTLS_CIPHER_CAMELLIA_128_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002789 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002790 *key_type = PSA_KEY_TYPE_CAMELLIA;
2791 *key_size = 128;
2792 break;
2793 case MBEDTLS_CIPHER_CAMELLIA_128_GCM:
2794 *alg = PSA_ALG_GCM;
2795 *key_type = PSA_KEY_TYPE_CAMELLIA;
2796 *key_size = 128;
2797 break;
2798 case MBEDTLS_CIPHER_CAMELLIA_192_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002799 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002800 *key_type = PSA_KEY_TYPE_CAMELLIA;
2801 *key_size = 192;
2802 break;
2803 case MBEDTLS_CIPHER_CAMELLIA_192_GCM:
2804 *alg = PSA_ALG_GCM;
2805 *key_type = PSA_KEY_TYPE_CAMELLIA;
2806 *key_size = 192;
2807 break;
2808 case MBEDTLS_CIPHER_CAMELLIA_256_CBC:
2809 *alg = PSA_ALG_CBC_NO_PADDING;
2810 *key_type = PSA_KEY_TYPE_CAMELLIA;
2811 *key_size = 256;
2812 break;
2813 case MBEDTLS_CIPHER_CAMELLIA_256_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002814 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002815 *key_type = PSA_KEY_TYPE_CAMELLIA;
2816 *key_size = 256;
2817 break;
2818 case MBEDTLS_CIPHER_CAMELLIA_256_GCM:
2819 *alg = PSA_ALG_GCM;
2820 *key_type = PSA_KEY_TYPE_CAMELLIA;
2821 *key_size = 256;
2822 break;
2823 case MBEDTLS_CIPHER_CHACHA20_POLY1305:
2824 *alg = PSA_ALG_CHACHA20_POLY1305;
2825 *key_type = PSA_KEY_TYPE_CHACHA20;
2826 *key_size = 256;
2827 break;
2828 case MBEDTLS_CIPHER_NULL:
2829 *alg = MBEDTLS_SSL_NULL_CIPHER;
2830 *key_type = 0;
2831 *key_size = 0;
2832 break;
2833 default:
2834 return PSA_ERROR_NOT_SUPPORTED;
2835 }
2836
2837 return PSA_SUCCESS;
2838}
Neil Armstrong8395d7a2022-05-18 11:44:56 +02002839#endif /* MBEDTLS_USE_PSA_CRYPTO || MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002840
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002841#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002842int mbedtls_ssl_conf_dh_param_bin(mbedtls_ssl_config *conf,
2843 const unsigned char *dhm_P, size_t P_len,
2844 const unsigned char *dhm_G, size_t G_len)
Hanno Beckera90658f2017-10-04 15:29:08 +01002845{
Janos Follath865b3eb2019-12-16 11:46:15 +00002846 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckera90658f2017-10-04 15:29:08 +01002847
Gilles Peskine449bd832023-01-11 14:50:10 +01002848 mbedtls_mpi_free(&conf->dhm_P);
2849 mbedtls_mpi_free(&conf->dhm_G);
Glenn Strausscee11292021-12-20 01:43:17 -05002850
Gilles Peskine449bd832023-01-11 14:50:10 +01002851 if ((ret = mbedtls_mpi_read_binary(&conf->dhm_P, dhm_P, P_len)) != 0 ||
2852 (ret = mbedtls_mpi_read_binary(&conf->dhm_G, dhm_G, G_len)) != 0) {
2853 mbedtls_mpi_free(&conf->dhm_P);
2854 mbedtls_mpi_free(&conf->dhm_G);
2855 return ret;
Hanno Beckera90658f2017-10-04 15:29:08 +01002856 }
2857
Gilles Peskine449bd832023-01-11 14:50:10 +01002858 return 0;
Hanno Beckera90658f2017-10-04 15:29:08 +01002859}
Paul Bakker5121ce52009-01-03 21:22:43 +00002860
Gilles Peskine449bd832023-01-11 14:50:10 +01002861int mbedtls_ssl_conf_dh_param_ctx(mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx)
Paul Bakker1b57b062011-01-06 15:48:19 +00002862{
Janos Follath865b3eb2019-12-16 11:46:15 +00002863 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1b57b062011-01-06 15:48:19 +00002864
Gilles Peskine449bd832023-01-11 14:50:10 +01002865 mbedtls_mpi_free(&conf->dhm_P);
2866 mbedtls_mpi_free(&conf->dhm_G);
Glenn Strausscee11292021-12-20 01:43:17 -05002867
Gilles Peskine449bd832023-01-11 14:50:10 +01002868 if ((ret = mbedtls_dhm_get_value(dhm_ctx, MBEDTLS_DHM_PARAM_P,
2869 &conf->dhm_P)) != 0 ||
2870 (ret = mbedtls_dhm_get_value(dhm_ctx, MBEDTLS_DHM_PARAM_G,
2871 &conf->dhm_G)) != 0) {
2872 mbedtls_mpi_free(&conf->dhm_P);
2873 mbedtls_mpi_free(&conf->dhm_G);
2874 return ret;
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01002875 }
Paul Bakker1b57b062011-01-06 15:48:19 +00002876
Gilles Peskine449bd832023-01-11 14:50:10 +01002877 return 0;
Paul Bakker1b57b062011-01-06 15:48:19 +00002878}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002879#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00002880
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02002881#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
2882/*
2883 * Set the minimum length for Diffie-Hellman parameters
2884 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002885void mbedtls_ssl_conf_dhm_min_bitlen(mbedtls_ssl_config *conf,
2886 unsigned int bitlen)
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02002887{
2888 conf->dhm_min_bitlen = bitlen;
2889}
2890#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
2891
Ronald Crone68ab4f2022-10-05 12:46:29 +02002892#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002893#if !defined(MBEDTLS_DEPRECATED_REMOVED) && defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002894/*
2895 * Set allowed/preferred hashes for handshake signatures
2896 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002897void mbedtls_ssl_conf_sig_hashes(mbedtls_ssl_config *conf,
2898 const int *hashes)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002899{
2900 conf->sig_hashes = hashes;
2901}
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002902#endif /* !MBEDTLS_DEPRECATED_REMOVED && MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker1cd6e002021-08-10 13:27:10 +01002903
Jerry Yuf017ee42022-01-12 15:49:48 +08002904/* Configure allowed signature algorithms for handshake */
Gilles Peskine449bd832023-01-11 14:50:10 +01002905void mbedtls_ssl_conf_sig_algs(mbedtls_ssl_config *conf,
2906 const uint16_t *sig_algs)
Hanno Becker1cd6e002021-08-10 13:27:10 +01002907{
Jerry Yuf017ee42022-01-12 15:49:48 +08002908#if !defined(MBEDTLS_DEPRECATED_REMOVED)
2909 conf->sig_hashes = NULL;
2910#endif /* !MBEDTLS_DEPRECATED_REMOVED */
2911 conf->sig_algs = sig_algs;
Hanno Becker1cd6e002021-08-10 13:27:10 +01002912}
Ronald Crone68ab4f2022-10-05 12:46:29 +02002913#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002914
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02002915#if defined(MBEDTLS_ECP_C)
Brett Warrene0edc842021-08-17 09:53:13 +01002916#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002917/*
2918 * Set the allowed elliptic curves
Brett Warrene0edc842021-08-17 09:53:13 +01002919 *
2920 * mbedtls_ssl_setup() takes the provided list
2921 * and translates it to a list of IANA TLS group identifiers,
2922 * stored in ssl->handshake->group_list.
2923 *
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002924 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002925void mbedtls_ssl_conf_curves(mbedtls_ssl_config *conf,
2926 const mbedtls_ecp_group_id *curve_list)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002927{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002928 conf->curve_list = curve_list;
Brett Warrene0edc842021-08-17 09:53:13 +01002929 conf->group_list = NULL;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002930}
Brett Warrene0edc842021-08-17 09:53:13 +01002931#endif /* MBEDTLS_DEPRECATED_REMOVED */
Hanno Becker947194e2017-04-07 13:25:49 +01002932#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002933
Brett Warrene0edc842021-08-17 09:53:13 +01002934/*
2935 * Set the allowed groups
2936 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002937void mbedtls_ssl_conf_groups(mbedtls_ssl_config *conf,
2938 const uint16_t *group_list)
Brett Warrene0edc842021-08-17 09:53:13 +01002939{
2940#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
2941 conf->curve_list = NULL;
2942#endif
2943 conf->group_list = group_list;
2944}
2945
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002946#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002947int mbedtls_ssl_set_hostname(mbedtls_ssl_context *ssl, const char *hostname)
Paul Bakker5121ce52009-01-03 21:22:43 +00002948{
Hanno Becker947194e2017-04-07 13:25:49 +01002949 /* Initialize to suppress unnecessary compiler warning */
2950 size_t hostname_len = 0;
2951
2952 /* Check if new hostname is valid before
2953 * making any change to current one */
Gilles Peskine449bd832023-01-11 14:50:10 +01002954 if (hostname != NULL) {
2955 hostname_len = strlen(hostname);
Hanno Becker947194e2017-04-07 13:25:49 +01002956
Gilles Peskine449bd832023-01-11 14:50:10 +01002957 if (hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN) {
2958 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2959 }
Hanno Becker947194e2017-04-07 13:25:49 +01002960 }
2961
2962 /* Now it's clear that we will overwrite the old hostname,
2963 * so we can free it safely */
2964
Gilles Peskine449bd832023-01-11 14:50:10 +01002965 if (ssl->hostname != NULL) {
2966 mbedtls_platform_zeroize(ssl->hostname, strlen(ssl->hostname));
2967 mbedtls_free(ssl->hostname);
Hanno Becker947194e2017-04-07 13:25:49 +01002968 }
2969
2970 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01002971
Gilles Peskine449bd832023-01-11 14:50:10 +01002972 if (hostname == NULL) {
Hanno Becker947194e2017-04-07 13:25:49 +01002973 ssl->hostname = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01002974 } else {
2975 ssl->hostname = mbedtls_calloc(1, hostname_len + 1);
2976 if (ssl->hostname == NULL) {
2977 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2978 }
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002979
Gilles Peskine449bd832023-01-11 14:50:10 +01002980 memcpy(ssl->hostname, hostname, hostname_len);
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002981
Hanno Becker947194e2017-04-07 13:25:49 +01002982 ssl->hostname[hostname_len] = '\0';
2983 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002984
Gilles Peskine449bd832023-01-11 14:50:10 +01002985 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002986}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01002987#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002988
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002989#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01002990void mbedtls_ssl_conf_sni(mbedtls_ssl_config *conf,
2991 int (*f_sni)(void *, mbedtls_ssl_context *,
2992 const unsigned char *, size_t),
2993 void *p_sni)
Paul Bakker5701cdc2012-09-27 21:49:42 +00002994{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002995 conf->f_sni = f_sni;
2996 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00002997}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002998#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00002999
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003000#if defined(MBEDTLS_SSL_ALPN)
Gilles Peskine449bd832023-01-11 14:50:10 +01003001int mbedtls_ssl_conf_alpn_protocols(mbedtls_ssl_config *conf, const char **protos)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003002{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02003003 size_t cur_len, tot_len;
3004 const char **p;
3005
3006 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08003007 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
3008 * MUST NOT be truncated."
3009 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02003010 */
3011 tot_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003012 for (p = protos; *p != NULL; p++) {
3013 cur_len = strlen(*p);
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02003014 tot_len += cur_len;
3015
Gilles Peskine449bd832023-01-11 14:50:10 +01003016 if ((cur_len == 0) ||
3017 (cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN) ||
3018 (tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN)) {
3019 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3020 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02003021 }
3022
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003023 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02003024
Gilles Peskine449bd832023-01-11 14:50:10 +01003025 return 0;
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003026}
3027
Gilles Peskine449bd832023-01-11 14:50:10 +01003028const char *mbedtls_ssl_get_alpn_protocol(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003029{
Gilles Peskine449bd832023-01-11 14:50:10 +01003030 return ssl->alpn_chosen;
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003031}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003032#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003033
Johan Pascalb62bb512015-12-03 21:56:45 +01003034#if defined(MBEDTLS_SSL_DTLS_SRTP)
Gilles Peskine449bd832023-01-11 14:50:10 +01003035void mbedtls_ssl_conf_srtp_mki_value_supported(mbedtls_ssl_config *conf,
3036 int support_mki_value)
Ron Eldor591f1622018-01-22 12:30:04 +02003037{
3038 conf->dtls_srtp_mki_support = support_mki_value;
3039}
3040
Gilles Peskine449bd832023-01-11 14:50:10 +01003041int mbedtls_ssl_dtls_srtp_set_mki_value(mbedtls_ssl_context *ssl,
3042 unsigned char *mki_value,
3043 uint16_t mki_len)
Ron Eldor591f1622018-01-22 12:30:04 +02003044{
Gilles Peskine449bd832023-01-11 14:50:10 +01003045 if (mki_len > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH) {
3046 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Ron Eldora9788042018-12-05 11:04:31 +02003047 }
Ron Eldor591f1622018-01-22 12:30:04 +02003048
Gilles Peskine449bd832023-01-11 14:50:10 +01003049 if (ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_UNSUPPORTED) {
3050 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Ron Eldora9788042018-12-05 11:04:31 +02003051 }
Ron Eldor591f1622018-01-22 12:30:04 +02003052
Gilles Peskine449bd832023-01-11 14:50:10 +01003053 memcpy(ssl->dtls_srtp_info.mki_value, mki_value, mki_len);
Ron Eldor591f1622018-01-22 12:30:04 +02003054 ssl->dtls_srtp_info.mki_len = mki_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01003055 return 0;
Ron Eldor591f1622018-01-22 12:30:04 +02003056}
3057
Gilles Peskine449bd832023-01-11 14:50:10 +01003058int mbedtls_ssl_conf_dtls_srtp_protection_profiles(mbedtls_ssl_config *conf,
3059 const mbedtls_ssl_srtp_profile *profiles)
Johan Pascalb62bb512015-12-03 21:56:45 +01003060{
Johan Pascal253d0262020-09-22 13:04:45 +02003061 const mbedtls_ssl_srtp_profile *p;
3062 size_t list_size = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01003063
Johan Pascal253d0262020-09-22 13:04:45 +02003064 /* check the profiles list: all entry must be valid,
3065 * its size cannot be more than the total number of supported profiles, currently 4 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003066 for (p = profiles; *p != MBEDTLS_TLS_SRTP_UNSET &&
3067 list_size <= MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH;
3068 p++) {
3069 if (mbedtls_ssl_check_srtp_profile_value(*p) != MBEDTLS_TLS_SRTP_UNSET) {
Johan Pascal76fdf1d2020-10-22 23:31:00 +02003070 list_size++;
Gilles Peskine449bd832023-01-11 14:50:10 +01003071 } else {
Johan Pascal76fdf1d2020-10-22 23:31:00 +02003072 /* unsupported value, stop parsing and set the size to an error value */
3073 list_size = MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH + 1;
Johan Pascalb62bb512015-12-03 21:56:45 +01003074 }
3075 }
3076
Gilles Peskine449bd832023-01-11 14:50:10 +01003077 if (list_size > MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH) {
3078 conf->dtls_srtp_profile_list = NULL;
3079 conf->dtls_srtp_profile_list_len = 0;
3080 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Johan Pascal253d0262020-09-22 13:04:45 +02003081 }
3082
Johan Pascal9bc97ca2020-09-21 23:44:45 +02003083 conf->dtls_srtp_profile_list = profiles;
Johan Pascal253d0262020-09-22 13:04:45 +02003084 conf->dtls_srtp_profile_list_len = list_size;
Johan Pascalb62bb512015-12-03 21:56:45 +01003085
Gilles Peskine449bd832023-01-11 14:50:10 +01003086 return 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01003087}
3088
Gilles Peskine449bd832023-01-11 14:50:10 +01003089void mbedtls_ssl_get_dtls_srtp_negotiation_result(const mbedtls_ssl_context *ssl,
3090 mbedtls_dtls_srtp_info *dtls_srtp_info)
Johan Pascalb62bb512015-12-03 21:56:45 +01003091{
Johan Pascal2258a4f2020-10-28 13:53:09 +01003092 dtls_srtp_info->chosen_dtls_srtp_profile = ssl->dtls_srtp_info.chosen_dtls_srtp_profile;
3093 /* do not copy the mki value if there is no chosen profile */
Gilles Peskine449bd832023-01-11 14:50:10 +01003094 if (dtls_srtp_info->chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET) {
Johan Pascal2258a4f2020-10-28 13:53:09 +01003095 dtls_srtp_info->mki_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003096 } else {
Johan Pascal2258a4f2020-10-28 13:53:09 +01003097 dtls_srtp_info->mki_len = ssl->dtls_srtp_info.mki_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01003098 memcpy(dtls_srtp_info->mki_value, ssl->dtls_srtp_info.mki_value,
3099 ssl->dtls_srtp_info.mki_len);
Johan Pascal2258a4f2020-10-28 13:53:09 +01003100 }
Johan Pascalb62bb512015-12-03 21:56:45 +01003101}
Johan Pascalb62bb512015-12-03 21:56:45 +01003102#endif /* MBEDTLS_SSL_DTLS_SRTP */
3103
Aditya Patwardhan3096f332022-07-26 14:31:46 +05303104#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003105void mbedtls_ssl_conf_max_version(mbedtls_ssl_config *conf, int major, int minor)
Paul Bakker490ecc82011-10-06 13:04:09 +00003106{
Glenn Strauss2dfcea22022-03-14 17:26:42 -04003107 conf->max_tls_version = (major << 8) | minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00003108}
3109
Gilles Peskine449bd832023-01-11 14:50:10 +01003110void mbedtls_ssl_conf_min_version(mbedtls_ssl_config *conf, int major, int minor)
Paul Bakker1d29fb52012-09-28 13:28:45 +00003111{
Glenn Strauss2dfcea22022-03-14 17:26:42 -04003112 conf->min_tls_version = (major << 8) | minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00003113}
Aditya Patwardhan3096f332022-07-26 14:31:46 +05303114#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker1d29fb52012-09-28 13:28:45 +00003115
Janos Follath088ce432017-04-10 12:42:31 +01003116#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003117void mbedtls_ssl_conf_cert_req_ca_list(mbedtls_ssl_config *conf,
3118 char cert_req_ca_list)
Janos Follath088ce432017-04-10 12:42:31 +01003119{
3120 conf->cert_req_ca_list = cert_req_ca_list;
3121}
3122#endif
3123
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003124#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01003125void mbedtls_ssl_conf_encrypt_then_mac(mbedtls_ssl_config *conf, char etm)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01003126{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003127 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01003128}
3129#endif
3130
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003131#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Gilles Peskine449bd832023-01-11 14:50:10 +01003132void mbedtls_ssl_conf_extended_master_secret(mbedtls_ssl_config *conf, char ems)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02003133{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003134 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02003135}
3136#endif
3137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003138#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01003139int mbedtls_ssl_conf_max_frag_len(mbedtls_ssl_config *conf, unsigned char mfl_code)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003140{
Gilles Peskine449bd832023-01-11 14:50:10 +01003141 if (mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
3142 ssl_mfl_code_to_length(mfl_code) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN) {
3143 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003144 }
3145
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01003146 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003147
Gilles Peskine449bd832023-01-11 14:50:10 +01003148 return 0;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003149}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003150#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003151
Gilles Peskine449bd832023-01-11 14:50:10 +01003152void mbedtls_ssl_conf_legacy_renegotiation(mbedtls_ssl_config *conf, int allow_legacy)
Paul Bakker48916f92012-09-16 19:57:18 +00003153{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003154 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00003155}
3156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003157#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01003158void mbedtls_ssl_conf_renegotiation(mbedtls_ssl_config *conf, int renegotiation)
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003159{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003160 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003161}
3162
Gilles Peskine449bd832023-01-11 14:50:10 +01003163void mbedtls_ssl_conf_renegotiation_enforced(mbedtls_ssl_config *conf, int max_records)
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003164{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003165 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003166}
3167
Gilles Peskine449bd832023-01-11 14:50:10 +01003168void mbedtls_ssl_conf_renegotiation_period(mbedtls_ssl_config *conf,
3169 const unsigned char period[8])
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01003170{
Gilles Peskine449bd832023-01-11 14:50:10 +01003171 memcpy(conf->renego_period, period, 8);
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01003172}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003173#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00003174
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003175#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003176#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003177void mbedtls_ssl_conf_session_tickets(mbedtls_ssl_config *conf, int use_tickets)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003178{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01003179 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003180}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003181#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02003182
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003183#if defined(MBEDTLS_SSL_SRV_C)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08003184
Jerry Yud0766ec2022-09-22 10:46:57 +08003185#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003186void mbedtls_ssl_conf_new_session_tickets(mbedtls_ssl_config *conf,
3187 uint16_t num_tickets)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08003188{
Jerry Yud0766ec2022-09-22 10:46:57 +08003189 conf->new_session_tickets_count = num_tickets;
Jerry Yu1ad7ace2022-08-09 13:28:39 +08003190}
3191#endif
3192
Gilles Peskine449bd832023-01-11 14:50:10 +01003193void mbedtls_ssl_conf_session_tickets_cb(mbedtls_ssl_config *conf,
3194 mbedtls_ssl_ticket_write_t *f_ticket_write,
3195 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
3196 void *p_ticket)
Paul Bakker606b4ba2013-08-14 16:52:14 +02003197{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02003198 conf->f_ticket_write = f_ticket_write;
3199 conf->f_ticket_parse = f_ticket_parse;
3200 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02003201}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003202#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003203#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003204
Gilles Peskine449bd832023-01-11 14:50:10 +01003205void mbedtls_ssl_set_export_keys_cb(mbedtls_ssl_context *ssl,
3206 mbedtls_ssl_export_keys_t *f_export_keys,
3207 void *p_export_keys)
Robert Cragie4feb7ae2015-10-02 13:33:37 +01003208{
Hanno Becker7e6c1782021-06-08 09:24:55 +01003209 ssl->f_export_keys = f_export_keys;
3210 ssl->p_export_keys = p_export_keys;
Ron Eldorf5cc10d2019-05-07 18:33:40 +03003211}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01003212
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003213#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003214void mbedtls_ssl_conf_async_private_cb(
3215 mbedtls_ssl_config *conf,
3216 mbedtls_ssl_async_sign_t *f_async_sign,
3217 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
3218 mbedtls_ssl_async_resume_t *f_async_resume,
3219 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskine449bd832023-01-11 14:50:10 +01003220 void *async_config_data)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003221{
3222 conf->f_async_sign_start = f_async_sign;
3223 conf->f_async_decrypt_start = f_async_decrypt;
3224 conf->f_async_resume = f_async_resume;
3225 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003226 conf->p_async_config_data = async_config_data;
3227}
3228
Gilles Peskine449bd832023-01-11 14:50:10 +01003229void *mbedtls_ssl_conf_get_async_config_data(const mbedtls_ssl_config *conf)
Gilles Peskine8f97af72018-04-26 11:46:10 +02003230{
Gilles Peskine449bd832023-01-11 14:50:10 +01003231 return conf->p_async_config_data;
Gilles Peskine8f97af72018-04-26 11:46:10 +02003232}
3233
Gilles Peskine449bd832023-01-11 14:50:10 +01003234void *mbedtls_ssl_get_async_operation_data(const mbedtls_ssl_context *ssl)
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003235{
Gilles Peskine449bd832023-01-11 14:50:10 +01003236 if (ssl->handshake == NULL) {
3237 return NULL;
3238 } else {
3239 return ssl->handshake->user_async_ctx;
3240 }
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003241}
3242
Gilles Peskine449bd832023-01-11 14:50:10 +01003243void mbedtls_ssl_set_async_operation_data(mbedtls_ssl_context *ssl,
3244 void *ctx)
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003245{
Gilles Peskine449bd832023-01-11 14:50:10 +01003246 if (ssl->handshake != NULL) {
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003247 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +01003248 }
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003249}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003250#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003251
Paul Bakker5121ce52009-01-03 21:22:43 +00003252/*
3253 * SSL get accessors
3254 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003255uint32_t mbedtls_ssl_get_verify_result(const mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00003256{
Gilles Peskine449bd832023-01-11 14:50:10 +01003257 if (ssl->session != NULL) {
3258 return ssl->session->verify_result;
3259 }
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00003260
Gilles Peskine449bd832023-01-11 14:50:10 +01003261 if (ssl->session_negotiate != NULL) {
3262 return ssl->session_negotiate->verify_result;
3263 }
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00003264
Gilles Peskine449bd832023-01-11 14:50:10 +01003265 return 0xFFFFFFFF;
Paul Bakker5121ce52009-01-03 21:22:43 +00003266}
3267
Gilles Peskine449bd832023-01-11 14:50:10 +01003268int mbedtls_ssl_get_ciphersuite_id_from_ssl(const mbedtls_ssl_context *ssl)
Glenn Strauss8f526902022-01-13 00:04:49 -05003269{
Gilles Peskine449bd832023-01-11 14:50:10 +01003270 if (ssl == NULL || ssl->session == NULL) {
3271 return 0;
3272 }
Glenn Strauss8f526902022-01-13 00:04:49 -05003273
Gilles Peskine449bd832023-01-11 14:50:10 +01003274 return ssl->session->ciphersuite;
Glenn Strauss8f526902022-01-13 00:04:49 -05003275}
3276
Gilles Peskine449bd832023-01-11 14:50:10 +01003277const char *mbedtls_ssl_get_ciphersuite(const mbedtls_ssl_context *ssl)
Paul Bakker72f62662011-01-16 21:27:44 +00003278{
Gilles Peskine449bd832023-01-11 14:50:10 +01003279 if (ssl == NULL || ssl->session == NULL) {
3280 return NULL;
3281 }
Paul Bakker926c8e42013-03-06 10:23:34 +01003282
Gilles Peskine449bd832023-01-11 14:50:10 +01003283 return mbedtls_ssl_get_ciphersuite_name(ssl->session->ciphersuite);
Paul Bakker72f62662011-01-16 21:27:44 +00003284}
3285
Gilles Peskine449bd832023-01-11 14:50:10 +01003286const char *mbedtls_ssl_get_version(const mbedtls_ssl_context *ssl)
Paul Bakker43ca69c2011-01-15 17:35:19 +00003287{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003288#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003289 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
3290 switch (ssl->tls_version) {
Glenn Strauss60bfe602022-03-14 19:04:24 -04003291 case MBEDTLS_SSL_VERSION_TLS1_2:
Gilles Peskine449bd832023-01-11 14:50:10 +01003292 return "DTLSv1.2";
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003293 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01003294 return "unknown (DTLS)";
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003295 }
3296 }
3297#endif
3298
Gilles Peskine449bd832023-01-11 14:50:10 +01003299 switch (ssl->tls_version) {
Glenn Strauss60bfe602022-03-14 19:04:24 -04003300 case MBEDTLS_SSL_VERSION_TLS1_2:
Gilles Peskine449bd832023-01-11 14:50:10 +01003301 return "TLSv1.2";
Glenn Strauss60bfe602022-03-14 19:04:24 -04003302 case MBEDTLS_SSL_VERSION_TLS1_3:
Gilles Peskine449bd832023-01-11 14:50:10 +01003303 return "TLSv1.3";
Paul Bakker43ca69c2011-01-15 17:35:19 +00003304 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01003305 return "unknown";
Paul Bakker43ca69c2011-01-15 17:35:19 +00003306 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00003307}
3308
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003309#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01003310size_t mbedtls_ssl_get_input_max_frag_len(const mbedtls_ssl_context *ssl)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003311{
David Horstmann95d516f2021-05-04 18:36:56 +01003312 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003313 size_t read_mfl;
3314
Jerry Yuddda0502022-12-01 19:43:12 +08003315#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003316 /* Use the configured MFL for the client if we're past SERVER_HELLO_DONE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003317 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
3318 ssl->state >= MBEDTLS_SSL_SERVER_HELLO_DONE) {
3319 return ssl_mfl_code_to_length(ssl->conf->mfl_code);
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003320 }
Jerry Yuddda0502022-12-01 19:43:12 +08003321#endif
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003322
3323 /* Check if a smaller max length was negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01003324 if (ssl->session_out != NULL) {
3325 read_mfl = ssl_mfl_code_to_length(ssl->session_out->mfl_code);
3326 if (read_mfl < max_len) {
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003327 max_len = read_mfl;
3328 }
3329 }
3330
Jerry Yuddda0502022-12-01 19:43:12 +08003331 /* During a handshake, use the value being negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01003332 if (ssl->session_negotiate != NULL) {
3333 read_mfl = ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code);
3334 if (read_mfl < max_len) {
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003335 max_len = read_mfl;
3336 }
3337 }
3338
Gilles Peskine449bd832023-01-11 14:50:10 +01003339 return max_len;
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003340}
3341
Gilles Peskine449bd832023-01-11 14:50:10 +01003342size_t mbedtls_ssl_get_output_max_frag_len(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003343{
3344 size_t max_len;
3345
3346 /*
3347 * Assume mfl_code is correct since it was checked when set
3348 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003349 max_len = ssl_mfl_code_to_length(ssl->conf->mfl_code);
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003350
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02003351 /* Check if a smaller max length was negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01003352 if (ssl->session_out != NULL &&
3353 ssl_mfl_code_to_length(ssl->session_out->mfl_code) < max_len) {
3354 max_len = ssl_mfl_code_to_length(ssl->session_out->mfl_code);
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003355 }
3356
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02003357 /* During a handshake, use the value being negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01003358 if (ssl->session_negotiate != NULL &&
3359 ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code) < max_len) {
3360 max_len = ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code);
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02003361 }
3362
Gilles Peskine449bd832023-01-11 14:50:10 +01003363 return max_len;
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003364}
3365#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
3366
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003367#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003368size_t mbedtls_ssl_get_current_mtu(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003369{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04003370 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003371 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
3372 (ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
3373 ssl->state == MBEDTLS_SSL_SERVER_HELLO)) {
3374 return 0;
3375 }
Andrzej Kurekef43ce62018-10-09 08:24:12 -04003376
Gilles Peskine449bd832023-01-11 14:50:10 +01003377 if (ssl->handshake == NULL || ssl->handshake->mtu == 0) {
3378 return ssl->mtu;
3379 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003380
Gilles Peskine449bd832023-01-11 14:50:10 +01003381 if (ssl->mtu == 0) {
3382 return ssl->handshake->mtu;
3383 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003384
Gilles Peskine449bd832023-01-11 14:50:10 +01003385 return ssl->mtu < ssl->handshake->mtu ?
3386 ssl->mtu : ssl->handshake->mtu;
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003387}
3388#endif /* MBEDTLS_SSL_PROTO_DTLS */
3389
Gilles Peskine449bd832023-01-11 14:50:10 +01003390int mbedtls_ssl_get_max_out_record_payload(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003391{
3392 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
3393
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02003394#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
3395 !defined(MBEDTLS_SSL_PROTO_DTLS)
3396 (void) ssl;
3397#endif
3398
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003399#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01003400 const size_t mfl = mbedtls_ssl_get_output_max_frag_len(ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003401
Gilles Peskine449bd832023-01-11 14:50:10 +01003402 if (max_len > mfl) {
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003403 max_len = mfl;
Gilles Peskine449bd832023-01-11 14:50:10 +01003404 }
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003405#endif
3406
3407#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003408 if (mbedtls_ssl_get_current_mtu(ssl) != 0) {
3409 const size_t mtu = mbedtls_ssl_get_current_mtu(ssl);
3410 const int ret = mbedtls_ssl_get_record_expansion(ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003411 const size_t overhead = (size_t) ret;
3412
Gilles Peskine449bd832023-01-11 14:50:10 +01003413 if (ret < 0) {
3414 return ret;
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003415 }
3416
Gilles Peskine449bd832023-01-11 14:50:10 +01003417 if (mtu <= overhead) {
3418 MBEDTLS_SSL_DEBUG_MSG(1, ("MTU too low for record expansion"));
3419 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
3420 }
3421
3422 if (max_len > mtu - overhead) {
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003423 max_len = mtu - overhead;
Gilles Peskine449bd832023-01-11 14:50:10 +01003424 }
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003425 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003426#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003427
Hanno Becker0defedb2018-08-10 12:35:02 +01003428#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
3429 !defined(MBEDTLS_SSL_PROTO_DTLS)
3430 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003431#endif
3432
Gilles Peskine449bd832023-01-11 14:50:10 +01003433 return (int) max_len;
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003434}
3435
Gilles Peskine449bd832023-01-11 14:50:10 +01003436int mbedtls_ssl_get_max_in_record_payload(const mbedtls_ssl_context *ssl)
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003437{
3438 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
3439
3440#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
3441 (void) ssl;
3442#endif
3443
3444#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01003445 const size_t mfl = mbedtls_ssl_get_input_max_frag_len(ssl);
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003446
Gilles Peskine449bd832023-01-11 14:50:10 +01003447 if (max_len > mfl) {
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003448 max_len = mfl;
Gilles Peskine449bd832023-01-11 14:50:10 +01003449 }
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003450#endif
3451
Gilles Peskine449bd832023-01-11 14:50:10 +01003452 return (int) max_len;
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003453}
3454
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003455#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003456const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert(const mbedtls_ssl_context *ssl)
Paul Bakkerb0550d92012-10-30 07:51:03 +00003457{
Gilles Peskine449bd832023-01-11 14:50:10 +01003458 if (ssl == NULL || ssl->session == NULL) {
3459 return NULL;
3460 }
Paul Bakkerb0550d92012-10-30 07:51:03 +00003461
Hanno Beckere6824572019-02-07 13:18:46 +00003462#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01003463 return ssl->session->peer_cert;
Hanno Beckere6824572019-02-07 13:18:46 +00003464#else
Gilles Peskine449bd832023-01-11 14:50:10 +01003465 return NULL;
Hanno Beckere6824572019-02-07 13:18:46 +00003466#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00003467}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003468#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00003469
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003470#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003471int mbedtls_ssl_get_session(const mbedtls_ssl_context *ssl,
3472 mbedtls_ssl_session *dst)
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003473{
Hanno Beckere810bbc2021-05-14 16:01:05 +01003474 int ret;
3475
Gilles Peskine449bd832023-01-11 14:50:10 +01003476 if (ssl == NULL ||
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003477 dst == NULL ||
3478 ssl->session == NULL ||
Gilles Peskine449bd832023-01-11 14:50:10 +01003479 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT) {
3480 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003481 }
3482
Hanno Beckere810bbc2021-05-14 16:01:05 +01003483 /* Since Mbed TLS 3.0, mbedtls_ssl_get_session() is no longer
3484 * idempotent: Each session can only be exported once.
3485 *
3486 * (This is in preparation for TLS 1.3 support where we will
3487 * need the ability to export multiple sessions (aka tickets),
3488 * which will be achieved by calling mbedtls_ssl_get_session()
3489 * multiple times until it fails.)
3490 *
3491 * Check whether we have already exported the current session,
3492 * and fail if so.
3493 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003494 if (ssl->session->exported == 1) {
3495 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
3496 }
Hanno Beckere810bbc2021-05-14 16:01:05 +01003497
Gilles Peskine449bd832023-01-11 14:50:10 +01003498 ret = mbedtls_ssl_session_copy(dst, ssl->session);
3499 if (ret != 0) {
3500 return ret;
3501 }
Hanno Beckere810bbc2021-05-14 16:01:05 +01003502
3503 /* Remember that we've exported the session. */
3504 ssl->session->exported = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003505 return 0;
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003506}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003507#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003508
Paul Bakker5121ce52009-01-03 21:22:43 +00003509/*
Hanno Beckera835da52019-05-16 12:39:07 +01003510 * Define ticket header determining Mbed TLS version
3511 * and structure of the ticket.
3512 */
3513
Hanno Becker94ef3b32019-05-16 12:50:45 +01003514/*
Hanno Becker50b59662019-05-28 14:30:45 +01003515 * Define bitflag determining compile-time settings influencing
3516 * structure of serialized SSL sessions.
Hanno Becker94ef3b32019-05-16 12:50:45 +01003517 */
3518
Hanno Becker50b59662019-05-28 14:30:45 +01003519#if defined(MBEDTLS_HAVE_TIME)
Hanno Becker3e088662019-05-29 11:10:18 +01003520#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker50b59662019-05-28 14:30:45 +01003521#else
Hanno Becker3e088662019-05-29 11:10:18 +01003522#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003523#endif /* MBEDTLS_HAVE_TIME */
3524
3525#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker3e088662019-05-29 11:10:18 +01003526#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003527#else
Hanno Becker3e088662019-05-29 11:10:18 +01003528#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003529#endif /* MBEDTLS_X509_CRT_PARSE_C */
3530
3531#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Becker3e088662019-05-29 11:10:18 +01003532#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003533#else
Hanno Becker3e088662019-05-29 11:10:18 +01003534#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003535#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
3536
3537#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Becker3e088662019-05-29 11:10:18 +01003538#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003539#else
Hanno Becker3e088662019-05-29 11:10:18 +01003540#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003541#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
3542
Hanno Becker94ef3b32019-05-16 12:50:45 +01003543#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3e088662019-05-29 11:10:18 +01003544#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003545#else
Hanno Becker3e088662019-05-29 11:10:18 +01003546#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003547#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
3548
Hanno Becker94ef3b32019-05-16 12:50:45 +01003549#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3550#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
3551#else
3552#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
3553#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3554
Hanno Becker3e088662019-05-29 11:10:18 +01003555#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
3556#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
3557#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
3558#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
Hanno Becker37bdbe62021-08-01 05:38:58 +01003559#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 4
3560#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 5
Hanno Becker3e088662019-05-29 11:10:18 +01003561
Hanno Becker50b59662019-05-28 14:30:45 +01003562#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Gilles Peskine449bd832023-01-11 14:50:10 +01003563 ((uint16_t) ( \
3564 (SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT) | \
3565 (SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT) | \
3566 (SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << \
3567 SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT) | \
3568 (SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT) | \
3569 (SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT) | \
3570 (SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT)))
Hanno Becker94ef3b32019-05-16 12:50:45 +01003571
Hanno Beckerf8787072019-05-16 12:41:07 +01003572static unsigned char ssl_serialized_session_header[] = {
Hanno Becker94ef3b32019-05-16 12:50:45 +01003573 MBEDTLS_VERSION_MAJOR,
3574 MBEDTLS_VERSION_MINOR,
3575 MBEDTLS_VERSION_PATCH,
Gilles Peskine449bd832023-01-11 14:50:10 +01003576 MBEDTLS_BYTE_1(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
3577 MBEDTLS_BYTE_0(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
Hanno Beckerf8787072019-05-16 12:41:07 +01003578};
Hanno Beckera835da52019-05-16 12:39:07 +01003579
3580/*
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003581 * Serialize a session in the following format:
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02003582 * (in the presentation language of TLS, RFC 8446 section 3)
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003583 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003584 * struct {
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01003585 *
Hanno Beckerdce50972021-08-01 05:39:23 +01003586 * opaque mbedtls_version[3]; // library version: major, minor, patch
3587 * opaque session_format[2]; // library-version specific 16-bit field
3588 * // determining the format of the remaining
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003589 * // serialized data.
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01003590 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003591 * Note: When updating the format, remember to keep
3592 * these version+format bytes.
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003593 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003594 * // In this version, `session_format` determines
3595 * // the setting of those compile-time
3596 * // configuration options which influence
3597 * // the structure of mbedtls_ssl_session.
3598 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003599 * uint8_t minor_ver; // Protocol minor version. Possible values:
Jerry Yu0c2a7382022-12-06 13:27:25 +08003600 * // - TLS 1.2 (0x0303)
3601 * // - TLS 1.3 (0x0304)
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003602 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003603 * select (serialized_session.tls_version) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003604 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003605 * case MBEDTLS_SSL_VERSION_TLS1_2:
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003606 * serialized_session_tls12 data;
Jerry Yu0c2a7382022-12-06 13:27:25 +08003607 * case MBEDTLS_SSL_VERSION_TLS1_3:
Jerry Yuddda0502022-12-01 19:43:12 +08003608 * serialized_session_tls13 data;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003609 *
3610 * };
3611 *
3612 * } serialized_session;
3613 *
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003614 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003615
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003616MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003617static int ssl_session_save(const mbedtls_ssl_session *session,
3618 unsigned char omit_header,
3619 unsigned char *buf,
3620 size_t buf_len,
3621 size_t *olen)
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003622{
3623 unsigned char *p = buf;
3624 size_t used = 0;
Jerry Yu251a12e2022-07-13 15:15:48 +08003625 size_t remaining_len;
Jerry Yue36fdd62022-08-17 21:31:36 +08003626#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3627 size_t out_len;
3628 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3629#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01003630 if (session == NULL) {
3631 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
3632 }
Jerry Yu438ddd82022-07-07 06:55:50 +00003633
Gilles Peskine449bd832023-01-11 14:50:10 +01003634 if (!omit_header) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003635 /*
3636 * Add Mbed TLS version identifier
3637 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003638 used += sizeof(ssl_serialized_session_header);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003639
Gilles Peskine449bd832023-01-11 14:50:10 +01003640 if (used <= buf_len) {
3641 memcpy(p, ssl_serialized_session_header,
3642 sizeof(ssl_serialized_session_header));
3643 p += sizeof(ssl_serialized_session_header);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003644 }
3645 }
3646
3647 /*
3648 * TLS version identifier
3649 */
3650 used += 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003651 if (used <= buf_len) {
3652 *p++ = MBEDTLS_BYTE_0(session->tls_version);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003653 }
3654
3655 /* Forward to version-specific serialization routine. */
Jerry Yufca4d572022-07-21 10:37:48 +08003656 remaining_len = (buf_len >= used) ? buf_len - used : 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003657 switch (session->tls_version) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003658#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01003659 case MBEDTLS_SSL_VERSION_TLS1_2:
3660 used += ssl_tls12_session_save(session, p, remaining_len);
3661 break;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003662#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3663
Jerry Yu251a12e2022-07-13 15:15:48 +08003664#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01003665 case MBEDTLS_SSL_VERSION_TLS1_3:
3666 ret = ssl_tls13_session_save(session, p, remaining_len, &out_len);
3667 if (ret != 0 && ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL) {
3668 return ret;
3669 }
3670 used += out_len;
3671 break;
Jerry Yu251a12e2022-07-13 15:15:48 +08003672#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3673
Gilles Peskine449bd832023-01-11 14:50:10 +01003674 default:
3675 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003676 }
3677
3678 *olen = used;
Gilles Peskine449bd832023-01-11 14:50:10 +01003679 if (used > buf_len) {
3680 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
3681 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003682
Gilles Peskine449bd832023-01-11 14:50:10 +01003683 return 0;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003684}
3685
3686/*
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003687 * Public wrapper for ssl_session_save()
3688 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003689int mbedtls_ssl_session_save(const mbedtls_ssl_session *session,
3690 unsigned char *buf,
3691 size_t buf_len,
3692 size_t *olen)
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003693{
Gilles Peskine449bd832023-01-11 14:50:10 +01003694 return ssl_session_save(session, 0, buf, buf_len, olen);
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003695}
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003696
Jerry Yuf1b23ca2022-02-18 11:48:47 +08003697/*
3698 * Deserialize session, see mbedtls_ssl_session_save() for format.
3699 *
3700 * This internal version is wrapped by a public function that cleans up in
3701 * case of error, and has an extra option omit_header.
3702 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003703MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003704static int ssl_session_load(mbedtls_ssl_session *session,
3705 unsigned char omit_header,
3706 const unsigned char *buf,
3707 size_t len)
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003708{
3709 const unsigned char *p = buf;
3710 const unsigned char * const end = buf + len;
Jerry Yu438ddd82022-07-07 06:55:50 +00003711 size_t remaining_len;
3712
3713
Gilles Peskine449bd832023-01-11 14:50:10 +01003714 if (session == NULL) {
3715 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
3716 }
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003717
Gilles Peskine449bd832023-01-11 14:50:10 +01003718 if (!omit_header) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003719 /*
3720 * Check Mbed TLS version identifier
3721 */
3722
Gilles Peskine449bd832023-01-11 14:50:10 +01003723 if ((size_t) (end - p) < sizeof(ssl_serialized_session_header)) {
3724 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003725 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003726
3727 if (memcmp(p, ssl_serialized_session_header,
3728 sizeof(ssl_serialized_session_header)) != 0) {
3729 return MBEDTLS_ERR_SSL_VERSION_MISMATCH;
3730 }
3731 p += sizeof(ssl_serialized_session_header);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003732 }
3733
3734 /*
3735 * TLS version identifier
3736 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003737 if (1 > (size_t) (end - p)) {
3738 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3739 }
Glenn Straussda7851c2022-03-14 13:29:48 -04003740 session->tls_version = 0x0300 | *p++;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003741
3742 /* Dispatch according to TLS version. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003743 remaining_len = (end - p);
3744 switch (session->tls_version) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003745#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01003746 case MBEDTLS_SSL_VERSION_TLS1_2:
3747 return ssl_tls12_session_load(session, p, remaining_len);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003748#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3749
Jerry Yu438ddd82022-07-07 06:55:50 +00003750#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01003751 case MBEDTLS_SSL_VERSION_TLS1_3:
3752 return ssl_tls13_session_load(session, p, remaining_len);
Jerry Yu438ddd82022-07-07 06:55:50 +00003753#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3754
Gilles Peskine449bd832023-01-11 14:50:10 +01003755 default:
3756 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003757 }
3758}
3759
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003760/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02003761 * Deserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003762 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003763int mbedtls_ssl_session_load(mbedtls_ssl_session *session,
3764 const unsigned char *buf,
3765 size_t len)
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003766{
Gilles Peskine449bd832023-01-11 14:50:10 +01003767 int ret = ssl_session_load(session, 0, buf, len);
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003768
Gilles Peskine449bd832023-01-11 14:50:10 +01003769 if (ret != 0) {
3770 mbedtls_ssl_session_free(session);
3771 }
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003772
Gilles Peskine449bd832023-01-11 14:50:10 +01003773 return ret;
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003774}
3775
3776/*
Paul Bakker1961b702013-01-25 14:49:24 +01003777 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00003778 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003779MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003780static int ssl_prepare_handshake_step(mbedtls_ssl_context *ssl)
Hanno Becker41934dd2021-08-07 19:13:43 +01003781{
3782 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3783
Ronald Cron66dbf912022-02-02 15:33:46 +01003784 /*
3785 * We may have not been able to send to the peer all the handshake data
Ronald Cron3f20b772022-03-08 16:00:02 +01003786 * that were written into the output buffer by the previous handshake step,
3787 * if the write to the network callback returned with the
Ronald Cron66dbf912022-02-02 15:33:46 +01003788 * #MBEDTLS_ERR_SSL_WANT_WRITE error code.
3789 * We proceed to the next handshake step only when all data from the
3790 * previous one have been sent to the peer, thus we make sure that this is
3791 * the case here by calling `mbedtls_ssl_flush_output()`. The function may
3792 * return with the #MBEDTLS_ERR_SSL_WANT_WRITE error code in which case
3793 * we have to wait before to go ahead.
3794 * In the case of TLS 1.3, handshake step handlers do not send data to the
3795 * peer. Data are only sent here and through
3796 * `mbedtls_ssl_handle_pending_alert` in case an error that triggered an
Andrzej Kurek5c65c572022-04-13 14:28:52 -04003797 * alert occurred.
Ronald Cron66dbf912022-02-02 15:33:46 +01003798 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003799 if ((ret = mbedtls_ssl_flush_output(ssl)) != 0) {
3800 return ret;
3801 }
Hanno Becker41934dd2021-08-07 19:13:43 +01003802
3803#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003804 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3805 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING) {
3806 if ((ret = mbedtls_ssl_flight_transmit(ssl)) != 0) {
3807 return ret;
3808 }
Hanno Becker41934dd2021-08-07 19:13:43 +01003809 }
3810#endif /* MBEDTLS_SSL_PROTO_DTLS */
3811
Gilles Peskine449bd832023-01-11 14:50:10 +01003812 return ret;
Hanno Becker41934dd2021-08-07 19:13:43 +01003813}
3814
Gilles Peskine449bd832023-01-11 14:50:10 +01003815int mbedtls_ssl_handshake_step(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00003816{
Hanno Becker41934dd2021-08-07 19:13:43 +01003817 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003818
Gilles Peskine449bd832023-01-11 14:50:10 +01003819 if (ssl == NULL ||
Hanno Becker41934dd2021-08-07 19:13:43 +01003820 ssl->conf == NULL ||
3821 ssl->handshake == NULL ||
Gilles Peskine449bd832023-01-11 14:50:10 +01003822 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER) {
3823 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Becker41934dd2021-08-07 19:13:43 +01003824 }
3825
Gilles Peskine449bd832023-01-11 14:50:10 +01003826 ret = ssl_prepare_handshake_step(ssl);
3827 if (ret != 0) {
3828 return ret;
3829 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003830
Gilles Peskine449bd832023-01-11 14:50:10 +01003831 ret = mbedtls_ssl_handle_pending_alert(ssl);
3832 if (ret != 0) {
Jerry Yue7047812021-09-13 19:26:39 +08003833 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01003834 }
Jerry Yue7047812021-09-13 19:26:39 +08003835
Tom Cosgrove2fdc7b32022-09-21 12:33:17 +01003836 /* If ssl->conf->endpoint is not one of MBEDTLS_SSL_IS_CLIENT or
3837 * MBEDTLS_SSL_IS_SERVER, this is the return code we give */
3838 ret = MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3839
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003840#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003841 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
3842 MBEDTLS_SSL_DEBUG_MSG(2, ("client state: %s",
3843 mbedtls_ssl_states_str(ssl->state)));
Jerry Yub9930e72021-08-06 17:11:51 +08003844
Gilles Peskine449bd832023-01-11 14:50:10 +01003845 switch (ssl->state) {
Ronald Cron9f0fba32022-02-10 16:45:15 +01003846 case MBEDTLS_SSL_HELLO_REQUEST:
3847 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
Tom Cosgrove87d9c6c2022-09-22 09:27:56 +01003848 ret = 0;
Ronald Cron9f0fba32022-02-10 16:45:15 +01003849 break;
Jerry Yub9930e72021-08-06 17:11:51 +08003850
Ronald Cron9f0fba32022-02-10 16:45:15 +01003851 case MBEDTLS_SSL_CLIENT_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01003852 ret = mbedtls_ssl_write_client_hello(ssl);
Ronald Cron9f0fba32022-02-10 16:45:15 +01003853 break;
3854
3855 default:
3856#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01003857 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
3858 ret = mbedtls_ssl_tls13_handshake_client_step(ssl);
3859 } else {
3860 ret = mbedtls_ssl_handshake_client_step(ssl);
3861 }
Ronald Cron9f0fba32022-02-10 16:45:15 +01003862#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01003863 ret = mbedtls_ssl_handshake_client_step(ssl);
Ronald Cron9f0fba32022-02-10 16:45:15 +01003864#else
Gilles Peskine449bd832023-01-11 14:50:10 +01003865 ret = mbedtls_ssl_tls13_handshake_client_step(ssl);
Ronald Cron9f0fba32022-02-10 16:45:15 +01003866#endif
3867 }
Jerry Yub9930e72021-08-06 17:11:51 +08003868 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003869#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003870#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003871 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Ronald Cron6f135e12021-12-08 16:57:54 +01003872#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01003873 if (mbedtls_ssl_conf_is_tls13_only(ssl->conf)) {
3874 ret = mbedtls_ssl_tls13_handshake_server_step(ssl);
3875 }
Ronald Cron6f135e12021-12-08 16:57:54 +01003876#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yub9930e72021-08-06 17:11:51 +08003877
3878#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01003879 if (mbedtls_ssl_conf_is_tls12_only(ssl->conf)) {
3880 ret = mbedtls_ssl_handshake_server_step(ssl);
3881 }
Jerry Yub9930e72021-08-06 17:11:51 +08003882#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3883 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003884#endif
3885
Gilles Peskine449bd832023-01-11 14:50:10 +01003886 if (ret != 0) {
Jerry Yubbd5a3f2021-09-18 20:50:22 +08003887 /* handshake_step return error. And it is same
3888 * with alert_reason.
3889 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003890 if (ssl->send_alert) {
3891 ret = mbedtls_ssl_handle_pending_alert(ssl);
Jerry Yue7047812021-09-13 19:26:39 +08003892 goto cleanup;
3893 }
3894 }
3895
3896cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01003897 return ret;
Paul Bakker1961b702013-01-25 14:49:24 +01003898}
3899
3900/*
3901 * Perform the SSL handshake
3902 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003903int mbedtls_ssl_handshake(mbedtls_ssl_context *ssl)
Paul Bakker1961b702013-01-25 14:49:24 +01003904{
3905 int ret = 0;
3906
Hanno Beckera817ea42020-10-20 15:20:23 +01003907 /* Sanity checks */
3908
Gilles Peskine449bd832023-01-11 14:50:10 +01003909 if (ssl == NULL || ssl->conf == NULL) {
3910 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3911 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003912
Hanno Beckera817ea42020-10-20 15:20:23 +01003913#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003914 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3915 (ssl->f_set_timer == NULL || ssl->f_get_timer == NULL)) {
3916 MBEDTLS_SSL_DEBUG_MSG(1, ("You must use "
3917 "mbedtls_ssl_set_timer_cb() for DTLS"));
3918 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Beckera817ea42020-10-20 15:20:23 +01003919 }
3920#endif /* MBEDTLS_SSL_PROTO_DTLS */
3921
Gilles Peskine449bd832023-01-11 14:50:10 +01003922 MBEDTLS_SSL_DEBUG_MSG(2, ("=> handshake"));
Paul Bakker1961b702013-01-25 14:49:24 +01003923
Hanno Beckera817ea42020-10-20 15:20:23 +01003924 /* Main handshake loop */
Gilles Peskine449bd832023-01-11 14:50:10 +01003925 while (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER) {
3926 ret = mbedtls_ssl_handshake_step(ssl);
Paul Bakker1961b702013-01-25 14:49:24 +01003927
Gilles Peskine449bd832023-01-11 14:50:10 +01003928 if (ret != 0) {
Paul Bakker1961b702013-01-25 14:49:24 +01003929 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01003930 }
Paul Bakker1961b702013-01-25 14:49:24 +01003931 }
3932
Gilles Peskine449bd832023-01-11 14:50:10 +01003933 MBEDTLS_SSL_DEBUG_MSG(2, ("<= handshake"));
Paul Bakker5121ce52009-01-03 21:22:43 +00003934
Gilles Peskine449bd832023-01-11 14:50:10 +01003935 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003936}
3937
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003938#if defined(MBEDTLS_SSL_RENEGOTIATION)
3939#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003940/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003941 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00003942 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003943MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003944static int ssl_write_hello_request(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003945{
Janos Follath865b3eb2019-12-16 11:46:15 +00003946 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003947
Gilles Peskine449bd832023-01-11 14:50:10 +01003948 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write hello request"));
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003949
3950 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003951 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3952 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003953
Gilles Peskine449bd832023-01-11 14:50:10 +01003954 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
3955 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
3956 return ret;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003957 }
3958
Gilles Peskine449bd832023-01-11 14:50:10 +01003959 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write hello request"));
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003960
Gilles Peskine449bd832023-01-11 14:50:10 +01003961 return 0;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003962}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003963#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003964
3965/*
3966 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003967 * - any side: calling mbedtls_ssl_renegotiate(),
3968 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
3969 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02003970 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003971 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003972 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003973 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003974int mbedtls_ssl_start_renegotiation(mbedtls_ssl_context *ssl)
Paul Bakker48916f92012-09-16 19:57:18 +00003975{
Janos Follath865b3eb2019-12-16 11:46:15 +00003976 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker48916f92012-09-16 19:57:18 +00003977
Gilles Peskine449bd832023-01-11 14:50:10 +01003978 MBEDTLS_SSL_DEBUG_MSG(2, ("=> renegotiate"));
Paul Bakker48916f92012-09-16 19:57:18 +00003979
Gilles Peskine449bd832023-01-11 14:50:10 +01003980 if ((ret = ssl_handshake_init(ssl)) != 0) {
3981 return ret;
3982 }
Paul Bakker48916f92012-09-16 19:57:18 +00003983
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003984 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
3985 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003986#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003987 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3988 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING) {
3989 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003990 ssl->handshake->out_msg_seq = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003991 } else {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003992 ssl->handshake->in_msg_seq = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003993 }
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003994 }
3995#endif
3996
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003997 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
3998 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00003999
Gilles Peskine449bd832023-01-11 14:50:10 +01004000 if ((ret = mbedtls_ssl_handshake(ssl)) != 0) {
4001 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_handshake", ret);
4002 return ret;
Paul Bakker48916f92012-09-16 19:57:18 +00004003 }
4004
Gilles Peskine449bd832023-01-11 14:50:10 +01004005 MBEDTLS_SSL_DEBUG_MSG(2, ("<= renegotiate"));
Paul Bakker48916f92012-09-16 19:57:18 +00004006
Gilles Peskine449bd832023-01-11 14:50:10 +01004007 return 0;
Paul Bakker48916f92012-09-16 19:57:18 +00004008}
4009
4010/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004011 * Renegotiate current connection on client,
4012 * or request renegotiation on server
4013 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004014int mbedtls_ssl_renegotiate(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004015{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004016 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004017
Gilles Peskine449bd832023-01-11 14:50:10 +01004018 if (ssl == NULL || ssl->conf == NULL) {
4019 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4020 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02004021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004022#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004023 /* On server, just send the request */
Gilles Peskine449bd832023-01-11 14:50:10 +01004024 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
4025 if (mbedtls_ssl_is_handshake_over(ssl) == 0) {
4026 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4027 }
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004028
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004029 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02004030
4031 /* Did we already try/start sending HelloRequest? */
Gilles Peskine449bd832023-01-11 14:50:10 +01004032 if (ssl->out_left != 0) {
4033 return mbedtls_ssl_flush_output(ssl);
4034 }
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02004035
Gilles Peskine449bd832023-01-11 14:50:10 +01004036 return ssl_write_hello_request(ssl);
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004037 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004038#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004040#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004041 /*
4042 * On client, either start the renegotiation process or,
4043 * if already in progress, continue the handshake
4044 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004045 if (ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
4046 if (mbedtls_ssl_is_handshake_over(ssl) == 0) {
4047 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004048 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004049
4050 if ((ret = mbedtls_ssl_start_renegotiation(ssl)) != 0) {
4051 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_start_renegotiation", ret);
4052 return ret;
4053 }
4054 } else {
4055 if ((ret = mbedtls_ssl_handshake(ssl)) != 0) {
4056 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_handshake", ret);
4057 return ret;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004058 }
4059 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004060#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004061
Gilles Peskine449bd832023-01-11 14:50:10 +01004062 return ret;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004063}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004064#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004065
Gilles Peskine449bd832023-01-11 14:50:10 +01004066void mbedtls_ssl_handshake_free(mbedtls_ssl_context *ssl)
Paul Bakker48916f92012-09-16 19:57:18 +00004067{
Gilles Peskine9b562d52018-04-25 20:32:43 +02004068 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
4069
Gilles Peskine449bd832023-01-11 14:50:10 +01004070 if (handshake == NULL) {
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004071 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01004072 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004073
Brett Warrene0edc842021-08-17 09:53:13 +01004074#if defined(MBEDTLS_ECP_C)
4075#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01004076 if (ssl->handshake->group_list_heap_allocated) {
4077 mbedtls_free((void *) handshake->group_list);
4078 }
Brett Warrene0edc842021-08-17 09:53:13 +01004079 handshake->group_list = NULL;
4080#endif /* MBEDTLS_DEPRECATED_REMOVED */
4081#endif /* MBEDTLS_ECP_C */
4082
Ronald Crone68ab4f2022-10-05 12:46:29 +02004083#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +08004084#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01004085 if (ssl->handshake->sig_algs_heap_allocated) {
4086 mbedtls_free((void *) handshake->sig_algs);
4087 }
Jerry Yuf017ee42022-01-12 15:49:48 +08004088 handshake->sig_algs = NULL;
4089#endif /* MBEDTLS_DEPRECATED_REMOVED */
Xiaofei Baic234ecf2022-02-08 09:59:23 +00004090#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01004091 if (ssl->handshake->certificate_request_context) {
4092 mbedtls_free((void *) handshake->certificate_request_context);
Xiaofei Baic234ecf2022-02-08 09:59:23 +00004093 }
4094#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Ronald Crone68ab4f2022-10-05 12:46:29 +02004095#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yuf017ee42022-01-12 15:49:48 +08004096
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02004097#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01004098 if (ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0) {
4099 ssl->conf->f_async_cancel(ssl);
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02004100 handshake->async_in_progress = 0;
4101 }
4102#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
4103
Andrzej Kurek25f27152022-08-17 16:09:31 -04004104#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -05004105#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01004106 psa_hash_abort(&handshake->fin_sha256_psa);
Andrzej Kurekeb342242019-01-29 09:14:33 -05004107#else
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01004108 mbedtls_md_free(&handshake->fin_sha256);
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02004109#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05004110#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004111#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -05004112#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01004113 psa_hash_abort(&handshake->fin_sha384_psa);
Andrzej Kurekeb342242019-01-29 09:14:33 -05004114#else
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01004115 mbedtls_md_free(&handshake->fin_sha384);
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02004116#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05004117#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02004118
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004119#if defined(MBEDTLS_DHM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004120 mbedtls_dhm_free(&handshake->dhm_ctx);
Paul Bakker48916f92012-09-16 19:57:18 +00004121#endif
Neil Armstrongf3f46412022-04-12 14:43:39 +02004122#if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004123 mbedtls_ecdh_free(&handshake->ecdh_ctx);
Paul Bakker61d113b2013-07-04 11:51:43 +02004124#endif
Valerio Setti02c25b52022-11-15 14:08:42 +01004125
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02004126#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Neil Armstrongca7d5062022-05-31 14:43:23 +02004127#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01004128 psa_pake_abort(&handshake->psa_pake_ctx);
Valerio Settieb3f7882022-12-08 18:42:58 +01004129 /*
4130 * Opaque keys are not stored in the handshake's data and it's the user
4131 * responsibility to destroy them. Clear ones, instead, are created by
4132 * the TLS library and should be destroyed at the same level
4133 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004134 if (!mbedtls_svc_key_id_is_null(handshake->psa_pake_password)) {
4135 psa_destroy_key(handshake->psa_pake_password);
Valerio Settieb3f7882022-12-08 18:42:58 +01004136 }
Neil Armstrongca7d5062022-05-31 14:43:23 +02004137 handshake->psa_pake_password = MBEDTLS_SVC_KEY_ID_INIT;
4138#else
Gilles Peskine449bd832023-01-11 14:50:10 +01004139 mbedtls_ecjpake_free(&handshake->ecjpake_ctx);
Neil Armstrongca7d5062022-05-31 14:43:23 +02004140#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02004141#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004142 mbedtls_free(handshake->ecjpake_cache);
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02004143 handshake->ecjpake_cache = NULL;
4144 handshake->ecjpake_cache_len = 0;
4145#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02004146#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02004147
Janos Follath4ae5c292016-02-10 11:27:43 +00004148#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
4149 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02004150 /* explicit void pointer cast for buggy MS compiler */
Gilles Peskine449bd832023-01-11 14:50:10 +01004151 mbedtls_free((void *) handshake->curves_tls_id);
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004152#endif
4153
Ronald Cron73fe8df2022-10-05 14:31:43 +02004154#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02004155#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01004156 if (!mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque)) {
Neil Armstrong501c9322022-05-03 09:35:09 +02004157 /* The maintenance of the external PSK key slot is the
4158 * user's responsibility. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004159 if (ssl->handshake->psk_opaque_is_internal) {
4160 psa_destroy_key(ssl->handshake->psk_opaque);
Neil Armstrong501c9322022-05-03 09:35:09 +02004161 ssl->handshake->psk_opaque_is_internal = 0;
4162 }
4163 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
4164 }
Neil Armstronge952a302022-05-03 10:22:14 +02004165#else
Gilles Peskine449bd832023-01-11 14:50:10 +01004166 if (handshake->psk != NULL) {
4167 mbedtls_platform_zeroize(handshake->psk, handshake->psk_len);
4168 mbedtls_free(handshake->psk);
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004169 }
Neil Armstronge952a302022-05-03 10:22:14 +02004170#endif /* MBEDTLS_USE_PSA_CRYPTO */
Ronald Cron73fe8df2022-10-05 14:31:43 +02004171#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004172
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004173#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
4174 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02004175 /*
4176 * Free only the linked list wrapper, not the keys themselves
4177 * since the belong to the SNI callback
4178 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004179 ssl_key_cert_free(handshake->sni_key_cert);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004180#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004181
Gilles Peskineeccd8882020-03-10 12:19:08 +01004182#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01004183 mbedtls_x509_crt_restart_free(&handshake->ecrs_ctx);
4184 if (handshake->ecrs_peer_cert != NULL) {
4185 mbedtls_x509_crt_free(handshake->ecrs_peer_cert);
4186 mbedtls_free(handshake->ecrs_peer_cert);
Hanno Becker3dad3112019-02-05 17:19:52 +00004187 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02004188#endif
4189
Hanno Becker75173122019-02-06 16:18:31 +00004190#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
4191 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01004192 mbedtls_pk_free(&handshake->peer_pubkey);
Hanno Becker75173122019-02-06 16:18:31 +00004193#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
4194
XiaokangQian9b93c0d2022-02-09 06:02:25 +00004195#if defined(MBEDTLS_SSL_CLI_C) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01004196 (defined(MBEDTLS_SSL_PROTO_DTLS) || defined(MBEDTLS_SSL_PROTO_TLS1_3))
4197 mbedtls_free(handshake->cookie);
XiaokangQian9b93c0d2022-02-09 06:02:25 +00004198#endif /* MBEDTLS_SSL_CLI_C &&
4199 ( MBEDTLS_SSL_PROTO_DTLS || MBEDTLS_SSL_PROTO_TLS1_3 ) */
XiaokangQian8499b6c2022-01-27 09:00:11 +00004200
4201#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004202 mbedtls_ssl_flight_free(handshake->flight);
4203 mbedtls_ssl_buffering_free(ssl);
XiaokangQian8499b6c2022-01-27 09:00:11 +00004204#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02004205
Ronald Cronf12b81d2022-03-15 10:42:41 +01004206#if defined(MBEDTLS_ECDH_C) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01004207 (defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3))
4208 if (handshake->ecdh_psa_privkey_is_external == 0) {
4209 psa_destroy_key(handshake->ecdh_psa_privkey);
4210 }
Hanno Becker4a63ed42019-01-08 11:39:35 +00004211#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
4212
Ronald Cron6f135e12021-12-08 16:57:54 +01004213#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01004214 mbedtls_ssl_transform_free(handshake->transform_handshake);
4215 mbedtls_free(handshake->transform_handshake);
Jerry Yu3d9b5902022-11-04 14:07:25 +08004216#if defined(MBEDTLS_SSL_EARLY_DATA)
Gilles Peskine449bd832023-01-11 14:50:10 +01004217 mbedtls_ssl_transform_free(handshake->transform_earlydata);
4218 mbedtls_free(handshake->transform_earlydata);
Jerry Yu3d9b5902022-11-04 14:07:25 +08004219#endif
Ronald Cron6f135e12021-12-08 16:57:54 +01004220#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yuba9c7272021-10-30 11:54:10 +08004221
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004222
4223#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4224 /* If the buffers are too big - reallocate. Because of the way Mbed TLS
4225 * processes datagrams and the fact that a datagram is allowed to have
4226 * several records in it, it is possible that the I/O buffers are not
4227 * empty at this stage */
Gilles Peskine449bd832023-01-11 14:50:10 +01004228 handle_buffer_resizing(ssl, 1, mbedtls_ssl_get_input_buflen(ssl),
4229 mbedtls_ssl_get_output_buflen(ssl));
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004230#endif
Hanno Becker3aa186f2021-08-10 09:24:19 +01004231
Jerry Yuba9c7272021-10-30 11:54:10 +08004232 /* mbedtls_platform_zeroize MUST be last one in this function */
Gilles Peskine449bd832023-01-11 14:50:10 +01004233 mbedtls_platform_zeroize(handshake,
4234 sizeof(mbedtls_ssl_handshake_params));
Paul Bakker48916f92012-09-16 19:57:18 +00004235}
4236
Gilles Peskine449bd832023-01-11 14:50:10 +01004237void mbedtls_ssl_session_free(mbedtls_ssl_session *session)
Paul Bakker48916f92012-09-16 19:57:18 +00004238{
Gilles Peskine449bd832023-01-11 14:50:10 +01004239 if (session == NULL) {
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004240 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01004241 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004242
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004243#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004244 ssl_clear_peer_cert(session);
Paul Bakkered27a042013-04-18 22:46:23 +02004245#endif
Paul Bakker0a597072012-09-25 21:55:46 +00004246
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02004247#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Xiaokang Qianbc663a02022-10-09 11:14:39 +00004248#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
4249 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01004250 mbedtls_free(session->hostname);
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00004251#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01004252 mbedtls_free(session->ticket);
Paul Bakkera503a632013-08-14 13:48:06 +02004253#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004254
Gilles Peskine449bd832023-01-11 14:50:10 +01004255 mbedtls_platform_zeroize(session, sizeof(mbedtls_ssl_session));
Paul Bakker48916f92012-09-16 19:57:18 +00004256}
4257
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02004258#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004259
4260#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4261#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u
4262#else
4263#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u
4264#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4265
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004266#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004267
4268#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4269#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u
4270#else
4271#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u
4272#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4273
4274#if defined(MBEDTLS_SSL_ALPN)
4275#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u
4276#else
4277#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u
4278#endif /* MBEDTLS_SSL_ALPN */
4279
4280#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0
4281#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1
4282#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2
4283#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3
4284
4285#define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \
Gilles Peskine449bd832023-01-11 14:50:10 +01004286 ((uint32_t) ( \
4287 (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << \
4288 SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT) | \
4289 (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << \
4290 SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT) | \
4291 (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << \
4292 SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT) | \
4293 (SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT) | \
4294 0u))
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004295
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004296static unsigned char ssl_serialized_context_header[] = {
4297 MBEDTLS_VERSION_MAJOR,
4298 MBEDTLS_VERSION_MINOR,
4299 MBEDTLS_VERSION_PATCH,
Gilles Peskine449bd832023-01-11 14:50:10 +01004300 MBEDTLS_BYTE_1(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
4301 MBEDTLS_BYTE_0(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
4302 MBEDTLS_BYTE_2(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG),
4303 MBEDTLS_BYTE_1(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG),
4304 MBEDTLS_BYTE_0(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG),
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004305};
4306
Paul Bakker5121ce52009-01-03 21:22:43 +00004307/*
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004308 * Serialize a full SSL context
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02004309 *
4310 * The format of the serialized data is:
4311 * (in the presentation language of TLS, RFC 8446 section 3)
4312 *
4313 * // header
4314 * opaque mbedtls_version[3]; // major, minor, patch
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004315 * opaque context_format[5]; // version-specific field determining
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02004316 * // the format of the remaining
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004317 * // serialized data.
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004318 * Note: When updating the format, remember to keep these
4319 * version+format bytes. (We may make their size part of the API.)
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02004320 *
4321 * // session sub-structure
4322 * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save()
4323 * // transform sub-structure
4324 * uint8 random[64]; // ServerHello.random+ClientHello.random
4325 * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value
4326 * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use
4327 * // fields from ssl_context
4328 * uint32 badmac_seen; // DTLS: number of records with failing MAC
4329 * uint64 in_window_top; // DTLS: last validated record seq_num
4330 * uint64 in_window; // DTLS: bitmask for replay protection
4331 * uint8 disable_datagram_packing; // DTLS: only one record per datagram
4332 * uint64 cur_out_ctr; // Record layer: outgoing sequence number
4333 * uint16 mtu; // DTLS: path mtu (max outgoing fragment size)
4334 * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol
4335 *
4336 * Note that many fields of the ssl_context or sub-structures are not
4337 * serialized, as they fall in one of the following categories:
4338 *
4339 * 1. forced value (eg in_left must be 0)
4340 * 2. pointer to dynamically-allocated memory (eg session, transform)
4341 * 3. value can be re-derived from other data (eg session keys from MS)
4342 * 4. value was temporary (eg content of input buffer)
4343 * 5. value will be provided by the user again (eg I/O callbacks and context)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004344 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004345int mbedtls_ssl_context_save(mbedtls_ssl_context *ssl,
4346 unsigned char *buf,
4347 size_t buf_len,
4348 size_t *olen)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004349{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004350 unsigned char *p = buf;
4351 size_t used = 0;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004352 size_t session_len;
4353 int ret = 0;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004354
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02004355 /*
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004356 * Enforce usage restrictions, see "return BAD_INPUT_DATA" in
4357 * this function's documentation.
4358 *
4359 * These are due to assumptions/limitations in the implementation. Some of
4360 * them are likely to stay (no handshake in progress) some might go away
4361 * (only DTLS) but are currently used to simplify the implementation.
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02004362 */
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004363 /* The initial handshake must be over */
Gilles Peskine449bd832023-01-11 14:50:10 +01004364 if (mbedtls_ssl_is_handshake_over(ssl) == 0) {
4365 MBEDTLS_SSL_DEBUG_MSG(1, ("Initial handshake isn't over"));
4366 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004367 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004368 if (ssl->handshake != NULL) {
4369 MBEDTLS_SSL_DEBUG_MSG(1, ("Handshake isn't completed"));
4370 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004371 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004372 /* Double-check that sub-structures are indeed ready */
Gilles Peskine449bd832023-01-11 14:50:10 +01004373 if (ssl->transform == NULL || ssl->session == NULL) {
4374 MBEDTLS_SSL_DEBUG_MSG(1, ("Serialised structures aren't ready"));
4375 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004376 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004377 /* There must be no pending incoming or outgoing data */
Gilles Peskine449bd832023-01-11 14:50:10 +01004378 if (mbedtls_ssl_check_pending(ssl) != 0) {
4379 MBEDTLS_SSL_DEBUG_MSG(1, ("There is pending incoming data"));
4380 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004381 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004382 if (ssl->out_left != 0) {
4383 MBEDTLS_SSL_DEBUG_MSG(1, ("There is pending outgoing data"));
4384 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004385 }
Dave Rodgman556e8a32022-12-06 16:31:25 +00004386 /* Protocol must be DTLS, not TLS */
Gilles Peskine449bd832023-01-11 14:50:10 +01004387 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
4388 MBEDTLS_SSL_DEBUG_MSG(1, ("Only DTLS is supported"));
4389 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004390 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004391 /* Version must be 1.2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004392 if (ssl->tls_version != MBEDTLS_SSL_VERSION_TLS1_2) {
4393 MBEDTLS_SSL_DEBUG_MSG(1, ("Only version 1.2 supported"));
4394 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004395 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004396 /* We must be using an AEAD ciphersuite */
Gilles Peskine449bd832023-01-11 14:50:10 +01004397 if (mbedtls_ssl_transform_uses_aead(ssl->transform) != 1) {
4398 MBEDTLS_SSL_DEBUG_MSG(1, ("Only AEAD ciphersuites supported"));
4399 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004400 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004401 /* Renegotiation must not be enabled */
4402#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01004403 if (ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED) {
4404 MBEDTLS_SSL_DEBUG_MSG(1, ("Renegotiation must not be enabled"));
4405 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004406 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004407#endif
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004408
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004409 /*
4410 * Version and format identifier
4411 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004412 used += sizeof(ssl_serialized_context_header);
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004413
Gilles Peskine449bd832023-01-11 14:50:10 +01004414 if (used <= buf_len) {
4415 memcpy(p, ssl_serialized_context_header,
4416 sizeof(ssl_serialized_context_header));
4417 p += sizeof(ssl_serialized_context_header);
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004418 }
4419
4420 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004421 * Session (length + data)
4422 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004423 ret = ssl_session_save(ssl->session, 1, NULL, 0, &session_len);
4424 if (ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL) {
4425 return ret;
4426 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004427
4428 used += 4 + session_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004429 if (used <= buf_len) {
4430 MBEDTLS_PUT_UINT32_BE(session_len, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004431 p += 4;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004432
Gilles Peskine449bd832023-01-11 14:50:10 +01004433 ret = ssl_session_save(ssl->session, 1,
4434 p, session_len, &session_len);
4435 if (ret != 0) {
4436 return ret;
4437 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004438
4439 p += session_len;
4440 }
4441
4442 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004443 * Transform
4444 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004445 used += sizeof(ssl->transform->randbytes);
4446 if (used <= buf_len) {
4447 memcpy(p, ssl->transform->randbytes,
4448 sizeof(ssl->transform->randbytes));
4449 p += sizeof(ssl->transform->randbytes);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004450 }
4451
4452#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4453 used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004454 if (used <= buf_len) {
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004455 *p++ = ssl->transform->in_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004456 memcpy(p, ssl->transform->in_cid, ssl->transform->in_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004457 p += ssl->transform->in_cid_len;
4458
4459 *p++ = ssl->transform->out_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004460 memcpy(p, ssl->transform->out_cid, ssl->transform->out_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004461 p += ssl->transform->out_cid_len;
4462 }
4463#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4464
4465 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004466 * Saved fields from top-level ssl_context structure
4467 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004468 used += 4;
Gilles Peskine449bd832023-01-11 14:50:10 +01004469 if (used <= buf_len) {
4470 MBEDTLS_PUT_UINT32_BE(ssl->badmac_seen, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004471 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004472 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004473
4474#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4475 used += 16;
Gilles Peskine449bd832023-01-11 14:50:10 +01004476 if (used <= buf_len) {
4477 MBEDTLS_PUT_UINT64_BE(ssl->in_window_top, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004478 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004479
Gilles Peskine449bd832023-01-11 14:50:10 +01004480 MBEDTLS_PUT_UINT64_BE(ssl->in_window, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004481 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004482 }
4483#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4484
4485#if defined(MBEDTLS_SSL_PROTO_DTLS)
4486 used += 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004487 if (used <= buf_len) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004488 *p++ = ssl->disable_datagram_packing;
4489 }
4490#endif /* MBEDTLS_SSL_PROTO_DTLS */
4491
Jerry Yuae0b2e22021-10-08 15:21:19 +08004492 used += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Gilles Peskine449bd832023-01-11 14:50:10 +01004493 if (used <= buf_len) {
4494 memcpy(p, ssl->cur_out_ctr, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN);
Jerry Yuae0b2e22021-10-08 15:21:19 +08004495 p += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004496 }
4497
4498#if defined(MBEDTLS_SSL_PROTO_DTLS)
4499 used += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01004500 if (used <= buf_len) {
4501 MBEDTLS_PUT_UINT16_BE(ssl->mtu, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004502 p += 2;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004503 }
4504#endif /* MBEDTLS_SSL_PROTO_DTLS */
4505
4506#if defined(MBEDTLS_SSL_ALPN)
4507 {
4508 const uint8_t alpn_len = ssl->alpn_chosen
Gilles Peskine449bd832023-01-11 14:50:10 +01004509 ? (uint8_t) strlen(ssl->alpn_chosen)
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004510 : 0;
4511
4512 used += 1 + alpn_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004513 if (used <= buf_len) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004514 *p++ = alpn_len;
4515
Gilles Peskine449bd832023-01-11 14:50:10 +01004516 if (ssl->alpn_chosen != NULL) {
4517 memcpy(p, ssl->alpn_chosen, alpn_len);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004518 p += alpn_len;
4519 }
4520 }
4521 }
4522#endif /* MBEDTLS_SSL_ALPN */
4523
4524 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004525 * Done
4526 */
4527 *olen = used;
4528
Gilles Peskine449bd832023-01-11 14:50:10 +01004529 if (used > buf_len) {
4530 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
4531 }
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004532
Gilles Peskine449bd832023-01-11 14:50:10 +01004533 MBEDTLS_SSL_DEBUG_BUF(4, "saved context", buf, used);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004534
Gilles Peskine449bd832023-01-11 14:50:10 +01004535 return mbedtls_ssl_session_reset_int(ssl, 0);
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004536}
4537
4538/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02004539 * Deserialize context, see mbedtls_ssl_context_save() for format.
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004540 *
4541 * This internal version is wrapped by a public function that cleans up in
4542 * case of error.
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004543 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004544MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01004545static int ssl_context_load(mbedtls_ssl_context *ssl,
4546 const unsigned char *buf,
4547 size_t len)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004548{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004549 const unsigned char *p = buf;
4550 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004551 size_t session_len;
Janos Follath865b3eb2019-12-16 11:46:15 +00004552 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004553#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek894edde2022-09-29 06:31:14 -04004554 tls_prf_fn prf_func = NULL;
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004555#endif
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004556
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004557 /*
4558 * The context should have been freshly setup or reset.
4559 * Give the user an error in case of obvious misuse.
Manuel Pégourié-Gonnard4ca930f2019-07-26 16:31:53 +02004560 * (Checking session is useful because it won't be NULL if we're
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004561 * renegotiating, or if the user mistakenly loaded a session first.)
4562 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004563 if (ssl->state != MBEDTLS_SSL_HELLO_REQUEST ||
4564 ssl->session != NULL) {
4565 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004566 }
4567
4568 /*
4569 * We can't check that the config matches the initial one, but we can at
4570 * least check it matches the requirements for serializing.
4571 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004572 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
Glenn Strauss2dfcea22022-03-14 17:26:42 -04004573 ssl->conf->max_tls_version < MBEDTLS_SSL_VERSION_TLS1_2 ||
4574 ssl->conf->min_tls_version > MBEDTLS_SSL_VERSION_TLS1_2 ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004575#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02004576 ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004577#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01004578 0) {
4579 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004580 }
4581
Gilles Peskine449bd832023-01-11 14:50:10 +01004582 MBEDTLS_SSL_DEBUG_BUF(4, "context to load", buf, len);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004583
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004584 /*
4585 * Check version identifier
4586 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004587 if ((size_t) (end - p) < sizeof(ssl_serialized_context_header)) {
4588 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004589 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004590
4591 if (memcmp(p, ssl_serialized_context_header,
4592 sizeof(ssl_serialized_context_header)) != 0) {
4593 return MBEDTLS_ERR_SSL_VERSION_MISMATCH;
4594 }
4595 p += sizeof(ssl_serialized_context_header);
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004596
4597 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004598 * Session
4599 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004600 if ((size_t) (end - p) < 4) {
4601 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4602 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004603
Gilles Peskine449bd832023-01-11 14:50:10 +01004604 session_len = ((size_t) p[0] << 24) |
4605 ((size_t) p[1] << 16) |
4606 ((size_t) p[2] << 8) |
4607 ((size_t) p[3]);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004608 p += 4;
4609
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004610 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00004611 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004612 ssl->session = ssl->session_negotiate;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004613 ssl->session_in = ssl->session;
4614 ssl->session_out = ssl->session;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004615 ssl->session_negotiate = NULL;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004616
Gilles Peskine449bd832023-01-11 14:50:10 +01004617 if ((size_t) (end - p) < session_len) {
4618 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4619 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004620
Gilles Peskine449bd832023-01-11 14:50:10 +01004621 ret = ssl_session_load(ssl->session, 1, p, session_len);
4622 if (ret != 0) {
4623 mbedtls_ssl_session_free(ssl->session);
4624 return ret;
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004625 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004626
4627 p += session_len;
4628
4629 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004630 * Transform
4631 */
4632
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004633 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00004634 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Jerry Yu2e199812022-12-01 18:57:19 +08004635#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004636 ssl->transform = ssl->transform_negotiate;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004637 ssl->transform_in = ssl->transform;
4638 ssl->transform_out = ssl->transform;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004639 ssl->transform_negotiate = NULL;
Jerry Yu2e199812022-12-01 18:57:19 +08004640#endif
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004641
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004642#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01004643 prf_func = ssl_tls12prf_from_cs(ssl->session->ciphersuite);
4644 if (prf_func == NULL) {
4645 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4646 }
Andrzej Kurek894edde2022-09-29 06:31:14 -04004647
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004648 /* Read random bytes and populate structure */
Gilles Peskine449bd832023-01-11 14:50:10 +01004649 if ((size_t) (end - p) < sizeof(ssl->transform->randbytes)) {
4650 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4651 }
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004652
Gilles Peskine449bd832023-01-11 14:50:10 +01004653 ret = ssl_tls12_populate_transform(ssl->transform,
4654 ssl->session->ciphersuite,
4655 ssl->session->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02004656#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004657 ssl->session->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02004658#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01004659 prf_func,
4660 p, /* currently pointing to randbytes */
4661 MBEDTLS_SSL_VERSION_TLS1_2, /* (D)TLS 1.2 is forced */
4662 ssl->conf->endpoint,
4663 ssl);
4664 if (ret != 0) {
4665 return ret;
4666 }
Jerry Yu840fbb22022-02-17 14:59:29 +08004667#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004668 p += sizeof(ssl->transform->randbytes);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004669
4670#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4671 /* Read connection IDs and store them */
Gilles Peskine449bd832023-01-11 14:50:10 +01004672 if ((size_t) (end - p) < 1) {
4673 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4674 }
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004675
4676 ssl->transform->in_cid_len = *p++;
4677
Gilles Peskine449bd832023-01-11 14:50:10 +01004678 if ((size_t) (end - p) < ssl->transform->in_cid_len + 1u) {
4679 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4680 }
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004681
Gilles Peskine449bd832023-01-11 14:50:10 +01004682 memcpy(ssl->transform->in_cid, p, ssl->transform->in_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004683 p += ssl->transform->in_cid_len;
4684
4685 ssl->transform->out_cid_len = *p++;
4686
Gilles Peskine449bd832023-01-11 14:50:10 +01004687 if ((size_t) (end - p) < ssl->transform->out_cid_len) {
4688 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4689 }
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004690
Gilles Peskine449bd832023-01-11 14:50:10 +01004691 memcpy(ssl->transform->out_cid, p, ssl->transform->out_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004692 p += ssl->transform->out_cid_len;
4693#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4694
4695 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004696 * Saved fields from top-level ssl_context structure
4697 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004698 if ((size_t) (end - p) < 4) {
4699 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4700 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004701
Gilles Peskine449bd832023-01-11 14:50:10 +01004702 ssl->badmac_seen = ((uint32_t) p[0] << 24) |
4703 ((uint32_t) p[1] << 16) |
4704 ((uint32_t) p[2] << 8) |
4705 ((uint32_t) p[3]);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004706 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004707
4708#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine449bd832023-01-11 14:50:10 +01004709 if ((size_t) (end - p) < 16) {
4710 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4711 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004712
Gilles Peskine449bd832023-01-11 14:50:10 +01004713 ssl->in_window_top = ((uint64_t) p[0] << 56) |
4714 ((uint64_t) p[1] << 48) |
4715 ((uint64_t) p[2] << 40) |
4716 ((uint64_t) p[3] << 32) |
4717 ((uint64_t) p[4] << 24) |
4718 ((uint64_t) p[5] << 16) |
4719 ((uint64_t) p[6] << 8) |
4720 ((uint64_t) p[7]);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004721 p += 8;
4722
Gilles Peskine449bd832023-01-11 14:50:10 +01004723 ssl->in_window = ((uint64_t) p[0] << 56) |
4724 ((uint64_t) p[1] << 48) |
4725 ((uint64_t) p[2] << 40) |
4726 ((uint64_t) p[3] << 32) |
4727 ((uint64_t) p[4] << 24) |
4728 ((uint64_t) p[5] << 16) |
4729 ((uint64_t) p[6] << 8) |
4730 ((uint64_t) p[7]);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004731 p += 8;
4732#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4733
4734#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004735 if ((size_t) (end - p) < 1) {
4736 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4737 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004738
4739 ssl->disable_datagram_packing = *p++;
4740#endif /* MBEDTLS_SSL_PROTO_DTLS */
4741
Gilles Peskine449bd832023-01-11 14:50:10 +01004742 if ((size_t) (end - p) < sizeof(ssl->cur_out_ctr)) {
4743 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4744 }
4745 memcpy(ssl->cur_out_ctr, p, sizeof(ssl->cur_out_ctr));
4746 p += sizeof(ssl->cur_out_ctr);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004747
4748#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004749 if ((size_t) (end - p) < 2) {
4750 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4751 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004752
Gilles Peskine449bd832023-01-11 14:50:10 +01004753 ssl->mtu = (p[0] << 8) | p[1];
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004754 p += 2;
4755#endif /* MBEDTLS_SSL_PROTO_DTLS */
4756
4757#if defined(MBEDTLS_SSL_ALPN)
4758 {
4759 uint8_t alpn_len;
4760 const char **cur;
4761
Gilles Peskine449bd832023-01-11 14:50:10 +01004762 if ((size_t) (end - p) < 1) {
4763 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4764 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004765
4766 alpn_len = *p++;
4767
Gilles Peskine449bd832023-01-11 14:50:10 +01004768 if (alpn_len != 0 && ssl->conf->alpn_list != NULL) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004769 /* alpn_chosen should point to an item in the configured list */
Gilles Peskine449bd832023-01-11 14:50:10 +01004770 for (cur = ssl->conf->alpn_list; *cur != NULL; cur++) {
4771 if (strlen(*cur) == alpn_len &&
4772 memcmp(p, cur, alpn_len) == 0) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004773 ssl->alpn_chosen = *cur;
4774 break;
4775 }
4776 }
4777 }
4778
4779 /* can only happen on conf mismatch */
Gilles Peskine449bd832023-01-11 14:50:10 +01004780 if (alpn_len != 0 && ssl->alpn_chosen == NULL) {
4781 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4782 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004783
4784 p += alpn_len;
4785 }
4786#endif /* MBEDTLS_SSL_ALPN */
4787
4788 /*
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004789 * Forced fields from top-level ssl_context structure
4790 *
4791 * Most of them already set to the correct value by mbedtls_ssl_init() and
4792 * mbedtls_ssl_reset(), so we only need to set the remaining ones.
4793 */
4794 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
Glenn Strauss60bfe602022-03-14 19:04:24 -04004795 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004796
Hanno Becker361b10d2019-08-30 10:42:49 +01004797 /* Adjust pointers for header fields of outgoing records to
4798 * the given transform, accounting for explicit IV and CID. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004799 mbedtls_ssl_update_out_pointers(ssl, ssl->transform);
Hanno Becker361b10d2019-08-30 10:42:49 +01004800
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004801#if defined(MBEDTLS_SSL_PROTO_DTLS)
4802 ssl->in_epoch = 1;
4803#endif
4804
4805 /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated,
4806 * which we don't want - otherwise we'd end up freeing the wrong transform
Hanno Beckerce5f5fd2020-02-05 10:47:44 +00004807 * by calling mbedtls_ssl_handshake_wrapup_free_hs_transform()
4808 * inappropriately. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004809 if (ssl->handshake != NULL) {
4810 mbedtls_ssl_handshake_free(ssl);
4811 mbedtls_free(ssl->handshake);
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004812 ssl->handshake = NULL;
4813 }
4814
4815 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004816 * Done - should have consumed entire buffer
4817 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004818 if (p != end) {
4819 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4820 }
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004821
Gilles Peskine449bd832023-01-11 14:50:10 +01004822 return 0;
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004823}
4824
4825/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02004826 * Deserialize context: public wrapper for error cleaning
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004827 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004828int mbedtls_ssl_context_load(mbedtls_ssl_context *context,
4829 const unsigned char *buf,
4830 size_t len)
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004831{
Gilles Peskine449bd832023-01-11 14:50:10 +01004832 int ret = ssl_context_load(context, buf, len);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004833
Gilles Peskine449bd832023-01-11 14:50:10 +01004834 if (ret != 0) {
4835 mbedtls_ssl_free(context);
4836 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004837
Gilles Peskine449bd832023-01-11 14:50:10 +01004838 return ret;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004839}
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02004840#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004841
4842/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004843 * Free an SSL context
4844 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004845void mbedtls_ssl_free(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00004846{
Gilles Peskine449bd832023-01-11 14:50:10 +01004847 if (ssl == NULL) {
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004848 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01004849 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004850
Gilles Peskine449bd832023-01-11 14:50:10 +01004851 MBEDTLS_SSL_DEBUG_MSG(2, ("=> free"));
Paul Bakker5121ce52009-01-03 21:22:43 +00004852
Gilles Peskine449bd832023-01-11 14:50:10 +01004853 if (ssl->out_buf != NULL) {
sander-visserb8aa2072020-05-06 22:05:13 +02004854#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4855 size_t out_buf_len = ssl->out_buf_len;
4856#else
4857 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
4858#endif
4859
Gilles Peskine449bd832023-01-11 14:50:10 +01004860 mbedtls_platform_zeroize(ssl->out_buf, out_buf_len);
4861 mbedtls_free(ssl->out_buf);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004862 ssl->out_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004863 }
4864
Gilles Peskine449bd832023-01-11 14:50:10 +01004865 if (ssl->in_buf != NULL) {
sander-visserb8aa2072020-05-06 22:05:13 +02004866#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4867 size_t in_buf_len = ssl->in_buf_len;
4868#else
4869 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
4870#endif
4871
Gilles Peskine449bd832023-01-11 14:50:10 +01004872 mbedtls_platform_zeroize(ssl->in_buf, in_buf_len);
4873 mbedtls_free(ssl->in_buf);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004874 ssl->in_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004875 }
4876
Gilles Peskine449bd832023-01-11 14:50:10 +01004877 if (ssl->transform) {
4878 mbedtls_ssl_transform_free(ssl->transform);
4879 mbedtls_free(ssl->transform);
Paul Bakker48916f92012-09-16 19:57:18 +00004880 }
4881
Gilles Peskine449bd832023-01-11 14:50:10 +01004882 if (ssl->handshake) {
4883 mbedtls_ssl_handshake_free(ssl);
4884 mbedtls_free(ssl->handshake);
Jerry Yu2e199812022-12-01 18:57:19 +08004885
4886#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01004887 mbedtls_ssl_transform_free(ssl->transform_negotiate);
4888 mbedtls_free(ssl->transform_negotiate);
Jerry Yu2e199812022-12-01 18:57:19 +08004889#endif
4890
Gilles Peskine449bd832023-01-11 14:50:10 +01004891 mbedtls_ssl_session_free(ssl->session_negotiate);
4892 mbedtls_free(ssl->session_negotiate);
Paul Bakker48916f92012-09-16 19:57:18 +00004893 }
4894
Ronald Cron6f135e12021-12-08 16:57:54 +01004895#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01004896 mbedtls_ssl_transform_free(ssl->transform_application);
4897 mbedtls_free(ssl->transform_application);
Ronald Cron6f135e12021-12-08 16:57:54 +01004898#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker3aa186f2021-08-10 09:24:19 +01004899
Gilles Peskine449bd832023-01-11 14:50:10 +01004900 if (ssl->session) {
4901 mbedtls_ssl_session_free(ssl->session);
4902 mbedtls_free(ssl->session);
Paul Bakkerc0463502013-02-14 11:19:38 +01004903 }
4904
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02004905#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004906 if (ssl->hostname != NULL) {
4907 mbedtls_platform_zeroize(ssl->hostname, strlen(ssl->hostname));
4908 mbedtls_free(ssl->hostname);
Paul Bakker5121ce52009-01-03 21:22:43 +00004909 }
Paul Bakker0be444a2013-08-27 21:55:01 +02004910#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004911
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02004912#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004913 mbedtls_free(ssl->cli_id);
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02004914#endif
4915
Gilles Peskine449bd832023-01-11 14:50:10 +01004916 MBEDTLS_SSL_DEBUG_MSG(2, ("<= free"));
Paul Bakker2da561c2009-02-05 18:00:28 +00004917
Paul Bakker86f04f42013-02-14 11:20:09 +01004918 /* Actually clear after last debug message */
Gilles Peskine449bd832023-01-11 14:50:10 +01004919 mbedtls_platform_zeroize(ssl, sizeof(mbedtls_ssl_context));
Paul Bakker5121ce52009-01-03 21:22:43 +00004920}
4921
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004922/*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08004923 * Initialize mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004924 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004925void mbedtls_ssl_config_init(mbedtls_ssl_config *conf)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004926{
Gilles Peskine449bd832023-01-11 14:50:10 +01004927 memset(conf, 0, sizeof(mbedtls_ssl_config));
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004928}
4929
Gilles Peskineae270bf2021-06-02 00:05:29 +02004930/* The selection should be the same as mbedtls_x509_crt_profile_default in
4931 * x509_crt.c, plus Montgomery curves for ECDHE. Here, the order matters:
Gilles Peskineb1940a72021-06-02 15:18:12 +02004932 * curves with a lower resource usage come first.
Gilles Peskineae270bf2021-06-02 00:05:29 +02004933 * See the documentation of mbedtls_ssl_conf_curves() for what we promise
Gilles Peskineb1940a72021-06-02 15:18:12 +02004934 * about this list.
4935 */
Brett Warrene0edc842021-08-17 09:53:13 +01004936static uint16_t ssl_preset_default_groups[] = {
Gilles Peskineae270bf2021-06-02 00:05:29 +02004937#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004938 MBEDTLS_SSL_IANA_TLS_GROUP_X25519,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004939#endif
4940#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004941 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004942#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02004943#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004944 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004945#endif
4946#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004947 MBEDTLS_SSL_IANA_TLS_GROUP_X448,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004948#endif
4949#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004950 MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004951#endif
Gilles Peskineae270bf2021-06-02 00:05:29 +02004952#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004953 MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004954#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02004955#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004956 MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004957#endif
4958#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004959 MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004960#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004961 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Gilles Peskineae270bf2021-06-02 00:05:29 +02004962};
Gilles Peskineae270bf2021-06-02 00:05:29 +02004963
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004964static int ssl_preset_suiteb_ciphersuites[] = {
4965 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
4966 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
4967 0
4968};
4969
Ronald Crone68ab4f2022-10-05 12:46:29 +02004970#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004971
Jerry Yu909df7b2022-01-22 11:56:27 +08004972/* NOTICE:
Jerry Yu0b994b82022-01-25 17:22:12 +08004973 * For ssl_preset_*_sig_algs and ssl_tls12_preset_*_sig_algs, the following
Jerry Yu370e1462022-01-25 10:36:53 +08004974 * rules SHOULD be upheld.
4975 * - No duplicate entries.
4976 * - But if there is a good reason, do not change the order of the algorithms.
Jerry Yu09a99fc2022-07-28 14:22:17 +08004977 * - ssl_tls12_preset* is for TLS 1.2 use only.
Jerry Yu370e1462022-01-25 10:36:53 +08004978 * - ssl_preset_* is for TLS 1.3 only or hybrid TLS 1.3/1.2 handshakes.
Jerry Yu1a8b4812022-01-20 17:56:50 +08004979 */
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004980static uint16_t ssl_preset_default_sig_algs[] = {
Jerry Yu1a8b4812022-01-20 17:56:50 +08004981
Andrzej Kurek25f27152022-08-17 16:09:31 -04004982#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 +08004983 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
4984 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004985#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA &&
Jerry Yued5e9f42022-01-26 11:21:34 +08004986 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004987
Andrzej Kurek25f27152022-08-17 16:09:31 -04004988#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 +08004989 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
4990 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004991#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu909df7b2022-01-22 11:56:27 +08004992 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
4993
Andrzej Kurek25f27152022-08-17 16:09:31 -04004994#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 +08004995 defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
4996 MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004997#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yued5e9f42022-01-26 11:21:34 +08004998 MBEDTLS_ECP_DP_SECP521R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004999
Gilles Peskine449bd832023-01-11 14:50:10 +01005000#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && \
5001 defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu9bb3ee42022-06-23 10:16:33 +08005002 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
Gilles Peskine449bd832023-01-11 14:50:10 +01005003#endif \
5004 /* 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 +08005005
Gilles Peskine449bd832023-01-11 14:50:10 +01005006#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && \
5007 defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu9bb3ee42022-06-23 10:16:33 +08005008 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
Gilles Peskine449bd832023-01-11 14:50:10 +01005009#endif \
5010 /* 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 +08005011
Gilles Peskine449bd832023-01-11 14:50:10 +01005012#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && \
5013 defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu9bb3ee42022-06-23 10:16:33 +08005014 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Gilles Peskine449bd832023-01-11 14:50:10 +01005015#endif \
5016 /* 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 +08005017
Andrzej Kurek25f27152022-08-17 16:09:31 -04005018#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 +08005019 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512,
5020#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA512_C */
5021
Andrzej Kurek25f27152022-08-17 16:09:31 -04005022#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 +08005023 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384,
5024#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA384_C */
5025
Andrzej Kurek25f27152022-08-17 16:09:31 -04005026#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 +08005027 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
5028#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA256_C */
5029
Gabor Mezei15b95a62022-05-09 16:37:58 +02005030 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08005031};
5032
5033/* NOTICE: see above */
5034#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5035static uint16_t ssl_tls12_preset_default_sig_algs[] = {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005036#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02005037#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005038 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA512),
Jerry Yu713013f2022-01-17 18:16:35 +08005039#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08005040#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
5041 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
5042#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02005043#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005044 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA512),
Gabor Mezeic1051b62022-05-10 13:13:58 +02005045#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005046#endif /* MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04005047#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02005048#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005049 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08005050#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08005051#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
5052 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
5053#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02005054#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005055 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384),
Gabor Mezeic1051b62022-05-10 13:13:58 +02005056#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005057#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04005058#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02005059#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005060 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08005061#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08005062#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
5063 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
5064#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02005065#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005066 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256),
Gabor Mezeic1051b62022-05-10 13:13:58 +02005067#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005068#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Gabor Mezei15b95a62022-05-09 16:37:58 +02005069 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08005070};
5071#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5072/* NOTICE: see above */
5073static uint16_t ssl_preset_suiteb_sig_algs[] = {
5074
Andrzej Kurek25f27152022-08-17 16:09:31 -04005075#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 +08005076 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
5077 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005078#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu909df7b2022-01-22 11:56:27 +08005079 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
5080
Andrzej Kurek25f27152022-08-17 16:09:31 -04005081#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 +08005082 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
5083 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005084#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu53037892022-01-25 11:02:06 +08005085 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08005086
Gilles Peskine449bd832023-01-11 14:50:10 +01005087#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && \
5088 defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu909df7b2022-01-22 11:56:27 +08005089 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Gilles Peskine449bd832023-01-11 14:50:10 +01005090#endif \
5091 /* 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 +08005092
Andrzej Kurek25f27152022-08-17 16:09:31 -04005093#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 +08005094 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005095#endif /* MBEDTLS_RSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yu53037892022-01-25 11:02:06 +08005096
Gabor Mezei15b95a62022-05-09 16:37:58 +02005097 MBEDTLS_TLS_SIG_NONE
Jerry Yu713013f2022-01-17 18:16:35 +08005098};
Jerry Yu6106fdc2022-01-12 16:36:14 +08005099
Jerry Yu909df7b2022-01-22 11:56:27 +08005100/* NOTICE: see above */
5101#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5102static uint16_t ssl_tls12_preset_suiteb_sig_algs[] = {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005103#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02005104#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005105 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256),
Jerry Yu713013f2022-01-17 18:16:35 +08005106#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02005107#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005108 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256),
Gabor Mezeic1051b62022-05-10 13:13:58 +02005109#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005110#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04005111#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02005112#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005113 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384),
Jerry Yu18c833e2022-01-25 10:55:47 +08005114#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02005115#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005116 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384),
Gabor Mezeic1051b62022-05-10 13:13:58 +02005117#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005118#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Gabor Mezei15b95a62022-05-09 16:37:58 +02005119 MBEDTLS_TLS_SIG_NONE
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01005120};
Jerry Yu909df7b2022-01-22 11:56:27 +08005121#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5122
Ronald Crone68ab4f2022-10-05 12:46:29 +02005123#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005124
Brett Warrene0edc842021-08-17 09:53:13 +01005125static uint16_t ssl_preset_suiteb_groups[] = {
Jaeden Amerod4311042019-06-03 08:27:16 +01005126#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01005127 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01005128#endif
5129#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01005130 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01005131#endif
Brett Warrene0edc842021-08-17 09:53:13 +01005132 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005133};
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005134
Ronald Crone68ab4f2022-10-05 12:46:29 +02005135#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08005136/* Function for checking `ssl_preset_*_sig_algs` and `ssl_tls12_preset_*_sig_algs`
Jerry Yu370e1462022-01-25 10:36:53 +08005137 * to make sure there are no duplicated signature algorithm entries. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005138MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01005139static int ssl_check_no_sig_alg_duplication(uint16_t *sig_algs)
Jerry Yu1a8b4812022-01-20 17:56:50 +08005140{
5141 size_t i, j;
5142 int ret = 0;
Jerry Yu909df7b2022-01-22 11:56:27 +08005143
Gilles Peskine449bd832023-01-11 14:50:10 +01005144 for (i = 0; sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++) {
5145 for (j = 0; j < i; j++) {
5146 if (sig_algs[i] != sig_algs[j]) {
Jerry Yuf377d642022-01-25 10:43:59 +08005147 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01005148 }
5149 mbedtls_printf(" entry(%04x,%" MBEDTLS_PRINTF_SIZET
5150 ") is duplicated at %" MBEDTLS_PRINTF_SIZET "\n",
5151 sig_algs[i], j, i);
Jerry Yuf377d642022-01-25 10:43:59 +08005152 ret = -1;
Jerry Yu1a8b4812022-01-20 17:56:50 +08005153 }
5154 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005155 return ret;
Jerry Yu1a8b4812022-01-20 17:56:50 +08005156}
5157
Ronald Crone68ab4f2022-10-05 12:46:29 +02005158#endif /* MBEDTLS_DEBUG_C && MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yu1a8b4812022-01-20 17:56:50 +08005159
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005160/*
Tillmann Karras588ad502015-09-25 04:27:22 +02005161 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005162 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005163int mbedtls_ssl_config_defaults(mbedtls_ssl_config *conf,
5164 int endpoint, int transport, int preset)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005165{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005166#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Janos Follath865b3eb2019-12-16 11:46:15 +00005167 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005168#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005169
Ronald Crone68ab4f2022-10-05 12:46:29 +02005170#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01005171 if (ssl_check_no_sig_alg_duplication(ssl_preset_suiteb_sig_algs)) {
5172 mbedtls_printf("ssl_preset_suiteb_sig_algs has duplicated entries\n");
5173 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu1a8b4812022-01-20 17:56:50 +08005174 }
5175
Gilles Peskine449bd832023-01-11 14:50:10 +01005176 if (ssl_check_no_sig_alg_duplication(ssl_preset_default_sig_algs)) {
5177 mbedtls_printf("ssl_preset_default_sig_algs has duplicated entries\n");
5178 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu1a8b4812022-01-20 17:56:50 +08005179 }
Jerry Yu909df7b2022-01-22 11:56:27 +08005180
5181#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01005182 if (ssl_check_no_sig_alg_duplication(ssl_tls12_preset_suiteb_sig_algs)) {
5183 mbedtls_printf("ssl_tls12_preset_suiteb_sig_algs has duplicated entries\n");
5184 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu909df7b2022-01-22 11:56:27 +08005185 }
5186
Gilles Peskine449bd832023-01-11 14:50:10 +01005187 if (ssl_check_no_sig_alg_duplication(ssl_tls12_preset_default_sig_algs)) {
5188 mbedtls_printf("ssl_tls12_preset_default_sig_algs has duplicated entries\n");
5189 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu909df7b2022-01-22 11:56:27 +08005190 }
5191#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Ronald Crone68ab4f2022-10-05 12:46:29 +02005192#endif /* MBEDTLS_DEBUG_C && MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yu1a8b4812022-01-20 17:56:50 +08005193
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02005194 /* Use the functions here so that they are covered in tests,
5195 * but otherwise access member directly for efficiency */
Gilles Peskine449bd832023-01-11 14:50:10 +01005196 mbedtls_ssl_conf_endpoint(conf, endpoint);
5197 mbedtls_ssl_conf_transport(conf, transport);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005198
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005199 /*
5200 * Things that are common to all presets
5201 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02005202#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005203 if (endpoint == MBEDTLS_SSL_IS_CLIENT) {
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02005204 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
5205#if defined(MBEDTLS_SSL_SESSION_TICKETS)
5206 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
5207#endif
5208 }
5209#endif
5210
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005211#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
5212 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
5213#endif
5214
5215#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
5216 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
5217#endif
5218
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02005219#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005220 conf->f_cookie_write = ssl_cookie_write_dummy;
5221 conf->f_cookie_check = ssl_cookie_check_dummy;
5222#endif
5223
5224#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
5225 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
5226#endif
5227
Janos Follath088ce432017-04-10 12:42:31 +01005228#if defined(MBEDTLS_SSL_SRV_C)
5229 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
TRodziewicz3946f792021-06-14 12:11:18 +02005230 conf->respect_cli_pref = MBEDTLS_SSL_SRV_CIPHERSUITE_ORDER_SERVER;
Janos Follath088ce432017-04-10 12:42:31 +01005231#endif
5232
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005233#if defined(MBEDTLS_SSL_PROTO_DTLS)
5234 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
5235 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
5236#endif
5237
5238#if defined(MBEDTLS_SSL_RENEGOTIATION)
5239 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005240 memset(conf->renego_period, 0x00, 2);
5241 memset(conf->renego_period + 2, 0xFF, 6);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005242#endif
5243
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005244#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005245 if (endpoint == MBEDTLS_SSL_IS_SERVER) {
Hanno Beckere2defad2021-07-24 05:59:17 +01005246 const unsigned char dhm_p[] =
5247 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
5248 const unsigned char dhm_g[] =
5249 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
Hanno Becker00d0a682017-10-04 13:14:29 +01005250
Gilles Peskine449bd832023-01-11 14:50:10 +01005251 if ((ret = mbedtls_ssl_conf_dh_param_bin(conf,
5252 dhm_p, sizeof(dhm_p),
5253 dhm_g, sizeof(dhm_g))) != 0) {
5254 return ret;
Hanno Beckere2defad2021-07-24 05:59:17 +01005255 }
5256 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02005257#endif
5258
Ronald Cron6f135e12021-12-08 16:57:54 +01005259#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu6ee56aa2022-12-06 17:47:22 +08005260
5261#if defined(MBEDTLS_SSL_EARLY_DATA)
Gilles Peskine449bd832023-01-11 14:50:10 +01005262 mbedtls_ssl_tls13_conf_early_data(conf, MBEDTLS_SSL_EARLY_DATA_DISABLED);
Jerry Yu6ee56aa2022-12-06 17:47:22 +08005263#if defined(MBEDTLS_SSL_SRV_C)
5264 mbedtls_ssl_tls13_conf_max_early_data_size(
Gilles Peskine449bd832023-01-11 14:50:10 +01005265 conf, MBEDTLS_SSL_MAX_EARLY_DATA_SIZE);
Jerry Yu6ee56aa2022-12-06 17:47:22 +08005266#endif
5267#endif /* MBEDTLS_SSL_EARLY_DATA */
5268
Jerry Yud0766ec2022-09-22 10:46:57 +08005269#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08005270 mbedtls_ssl_conf_new_session_tickets(
Gilles Peskine449bd832023-01-11 14:50:10 +01005271 conf, MBEDTLS_SSL_TLS1_3_DEFAULT_NEW_SESSION_TICKETS);
Jerry Yu1ad7ace2022-08-09 13:28:39 +08005272#endif
Hanno Becker71f1ed62021-07-24 06:01:47 +01005273 /*
5274 * Allow all TLS 1.3 key exchange modes by default.
5275 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00005276 conf->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Ronald Cron6f135e12021-12-08 16:57:54 +01005277#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01005278
Gilles Peskine449bd832023-01-11 14:50:10 +01005279 if (transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Glenn Strauss2dfcea22022-03-14 17:26:42 -04005280#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
XiaokangQian4d3a6042022-04-21 13:46:17 +00005281 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian060d8672022-04-21 09:24:56 +00005282 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian4d3a6042022-04-21 13:46:17 +00005283#else
Gilles Peskine449bd832023-01-11 14:50:10 +01005284 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQian060d8672022-04-21 09:24:56 +00005285#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005286 } else {
XiaokangQian4d3a6042022-04-21 13:46:17 +00005287#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01005288 if (endpoint == MBEDTLS_SSL_IS_CLIENT) {
XiaokangQian4d3a6042022-04-21 13:46:17 +00005289 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5290 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
Gilles Peskine449bd832023-01-11 14:50:10 +01005291 } else {
5292 /* Hybrid TLS 1.2 / 1.3 is not supported on server side yet */
XiaokangQian4d3a6042022-04-21 13:46:17 +00005293 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5294 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5295 }
5296#elif defined(MBEDTLS_SSL_PROTO_TLS1_3)
5297 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
5298 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
5299#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
5300 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5301 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5302#else
Gilles Peskine449bd832023-01-11 14:50:10 +01005303 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQian4d3a6042022-04-21 13:46:17 +00005304#endif
5305 }
Glenn Strauss2dfcea22022-03-14 17:26:42 -04005306
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005307 /*
5308 * Preset-specific defaults
5309 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005310 switch (preset) {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005311 /*
5312 * NSA Suite B
5313 */
5314 case MBEDTLS_SSL_PRESET_SUITEB:
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005315
Hanno Beckerd60b6c62021-04-29 12:04:11 +01005316 conf->ciphersuite_list = ssl_preset_suiteb_ciphersuites;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005317
5318#if defined(MBEDTLS_X509_CRT_PARSE_C)
5319 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005320#endif
5321
Ronald Crone68ab4f2022-10-05 12:46:29 +02005322#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08005323#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01005324 if (mbedtls_ssl_conf_is_tls12_only(conf)) {
Jerry Yu909df7b2022-01-22 11:56:27 +08005325 conf->sig_algs = ssl_tls12_preset_suiteb_sig_algs;
Gilles Peskine449bd832023-01-11 14:50:10 +01005326 } else
Jerry Yu909df7b2022-01-22 11:56:27 +08005327#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005328 conf->sig_algs = ssl_preset_suiteb_sig_algs;
Ronald Crone68ab4f2022-10-05 12:46:29 +02005329#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005330
Brett Warrene0edc842021-08-17 09:53:13 +01005331#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
5332 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005333#endif
Brett Warrene0edc842021-08-17 09:53:13 +01005334 conf->group_list = ssl_preset_suiteb_groups;
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02005335 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005336
5337 /*
5338 * Default
5339 */
5340 default:
Ronald Cronf6606552022-03-15 11:23:25 +01005341
Hanno Beckerd60b6c62021-04-29 12:04:11 +01005342 conf->ciphersuite_list = mbedtls_ssl_list_ciphersuites();
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005343
5344#if defined(MBEDTLS_X509_CRT_PARSE_C)
5345 conf->cert_profile = &mbedtls_x509_crt_profile_default;
5346#endif
5347
Ronald Crone68ab4f2022-10-05 12:46:29 +02005348#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08005349#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01005350 if (mbedtls_ssl_conf_is_tls12_only(conf)) {
Jerry Yu909df7b2022-01-22 11:56:27 +08005351 conf->sig_algs = ssl_tls12_preset_default_sig_algs;
Gilles Peskine449bd832023-01-11 14:50:10 +01005352 } else
Jerry Yu909df7b2022-01-22 11:56:27 +08005353#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005354 conf->sig_algs = ssl_preset_default_sig_algs;
Ronald Crone68ab4f2022-10-05 12:46:29 +02005355#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005356
Brett Warrene0edc842021-08-17 09:53:13 +01005357#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
5358 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005359#endif
Brett Warrene0edc842021-08-17 09:53:13 +01005360 conf->group_list = ssl_preset_default_groups;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005361
5362#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
5363 conf->dhm_min_bitlen = 1024;
5364#endif
5365 }
5366
Gilles Peskine449bd832023-01-11 14:50:10 +01005367 return 0;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005368}
5369
5370/*
5371 * Free mbedtls_ssl_config
5372 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005373void mbedtls_ssl_config_free(mbedtls_ssl_config *conf)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005374{
5375#if defined(MBEDTLS_DHM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005376 mbedtls_mpi_free(&conf->dhm_P);
5377 mbedtls_mpi_free(&conf->dhm_G);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005378#endif
5379
Ronald Cron73fe8df2022-10-05 14:31:43 +02005380#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02005381#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01005382 if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) {
Neil Armstrong501c9322022-05-03 09:35:09 +02005383 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
5384 }
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005385#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01005386 if (conf->psk != NULL) {
5387 mbedtls_platform_zeroize(conf->psk, conf->psk_len);
5388 mbedtls_free(conf->psk);
Azim Khan27e8a122018-03-21 14:24:11 +00005389 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005390 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09005391 }
5392
Gilles Peskine449bd832023-01-11 14:50:10 +01005393 if (conf->psk_identity != NULL) {
5394 mbedtls_platform_zeroize(conf->psk_identity, conf->psk_identity_len);
5395 mbedtls_free(conf->psk_identity);
Azim Khan27e8a122018-03-21 14:24:11 +00005396 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005397 conf->psk_identity_len = 0;
5398 }
Ronald Cron73fe8df2022-10-05 14:31:43 +02005399#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005400
5401#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005402 ssl_key_cert_free(conf->key_cert);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005403#endif
5404
Gilles Peskine449bd832023-01-11 14:50:10 +01005405 mbedtls_platform_zeroize(conf, sizeof(mbedtls_ssl_config));
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005406}
5407
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02005408#if defined(MBEDTLS_PK_C) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01005409 (defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C))
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005410/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005411 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005412 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005413unsigned char mbedtls_ssl_sig_from_pk(mbedtls_pk_context *pk)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005414{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005415#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005416 if (mbedtls_pk_can_do(pk, MBEDTLS_PK_RSA)) {
5417 return MBEDTLS_SSL_SIG_RSA;
5418 }
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005419#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005420#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005421 if (mbedtls_pk_can_do(pk, MBEDTLS_PK_ECDSA)) {
5422 return MBEDTLS_SSL_SIG_ECDSA;
5423 }
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005424#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005425 return MBEDTLS_SSL_SIG_ANON;
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005426}
5427
Gilles Peskine449bd832023-01-11 14:50:10 +01005428unsigned char mbedtls_ssl_sig_from_pk_alg(mbedtls_pk_type_t type)
Hanno Becker7e5437a2017-04-28 17:15:26 +01005429{
Gilles Peskine449bd832023-01-11 14:50:10 +01005430 switch (type) {
Hanno Becker7e5437a2017-04-28 17:15:26 +01005431 case MBEDTLS_PK_RSA:
Gilles Peskine449bd832023-01-11 14:50:10 +01005432 return MBEDTLS_SSL_SIG_RSA;
Hanno Becker7e5437a2017-04-28 17:15:26 +01005433 case MBEDTLS_PK_ECDSA:
5434 case MBEDTLS_PK_ECKEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01005435 return MBEDTLS_SSL_SIG_ECDSA;
Hanno Becker7e5437a2017-04-28 17:15:26 +01005436 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005437 return MBEDTLS_SSL_SIG_ANON;
Hanno Becker7e5437a2017-04-28 17:15:26 +01005438 }
5439}
5440
Gilles Peskine449bd832023-01-11 14:50:10 +01005441mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig(unsigned char sig)
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005442{
Gilles Peskine449bd832023-01-11 14:50:10 +01005443 switch (sig) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005444#if defined(MBEDTLS_RSA_C)
5445 case MBEDTLS_SSL_SIG_RSA:
Gilles Peskine449bd832023-01-11 14:50:10 +01005446 return MBEDTLS_PK_RSA;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005447#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005448#if defined(MBEDTLS_ECDSA_C)
5449 case MBEDTLS_SSL_SIG_ECDSA:
Gilles Peskine449bd832023-01-11 14:50:10 +01005450 return MBEDTLS_PK_ECDSA;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005451#endif
5452 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005453 return MBEDTLS_PK_NONE;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005454 }
5455}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02005456#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005457
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02005458/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005459 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02005460 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005461mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash(unsigned char hash)
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005462{
Gilles Peskine449bd832023-01-11 14:50:10 +01005463 switch (hash) {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005464#if defined(MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005465 case MBEDTLS_SSL_HASH_MD5:
Gilles Peskine449bd832023-01-11 14:50:10 +01005466 return MBEDTLS_MD_MD5;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005467#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005468#if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005469 case MBEDTLS_SSL_HASH_SHA1:
Gilles Peskine449bd832023-01-11 14:50:10 +01005470 return MBEDTLS_MD_SHA1;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005471#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005472#if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005473 case MBEDTLS_SSL_HASH_SHA224:
Gilles Peskine449bd832023-01-11 14:50:10 +01005474 return MBEDTLS_MD_SHA224;
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02005475#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005476#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005477 case MBEDTLS_SSL_HASH_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01005478 return MBEDTLS_MD_SHA256;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005479#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005480#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005481 case MBEDTLS_SSL_HASH_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01005482 return MBEDTLS_MD_SHA384;
Mateusz Starzyk3352a532021-04-06 14:28:22 +02005483#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005484#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005485 case MBEDTLS_SSL_HASH_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01005486 return MBEDTLS_MD_SHA512;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005487#endif
5488 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005489 return MBEDTLS_MD_NONE;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005490 }
5491}
5492
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005493/*
5494 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
5495 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005496unsigned char mbedtls_ssl_hash_from_md_alg(int md)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005497{
Gilles Peskine449bd832023-01-11 14:50:10 +01005498 switch (md) {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005499#if defined(MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005500 case MBEDTLS_MD_MD5:
Gilles Peskine449bd832023-01-11 14:50:10 +01005501 return MBEDTLS_SSL_HASH_MD5;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005502#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005503#if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005504 case MBEDTLS_MD_SHA1:
Gilles Peskine449bd832023-01-11 14:50:10 +01005505 return MBEDTLS_SSL_HASH_SHA1;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005506#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005507#if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005508 case MBEDTLS_MD_SHA224:
Gilles Peskine449bd832023-01-11 14:50:10 +01005509 return MBEDTLS_SSL_HASH_SHA224;
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02005510#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005511#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005512 case MBEDTLS_MD_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01005513 return MBEDTLS_SSL_HASH_SHA256;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005514#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005515#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005516 case MBEDTLS_MD_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01005517 return MBEDTLS_SSL_HASH_SHA384;
Mateusz Starzyk3352a532021-04-06 14:28:22 +02005518#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005519#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005520 case MBEDTLS_MD_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01005521 return MBEDTLS_SSL_HASH_SHA512;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005522#endif
5523 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005524 return MBEDTLS_SSL_HASH_NONE;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005525 }
5526}
5527
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005528/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005529 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005530 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005531 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005532int mbedtls_ssl_check_curve_tls_id(const mbedtls_ssl_context *ssl, uint16_t tls_id)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005533{
Gilles Peskine449bd832023-01-11 14:50:10 +01005534 const uint16_t *group_list = mbedtls_ssl_get_groups(ssl);
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005535
Gilles Peskine449bd832023-01-11 14:50:10 +01005536 if (group_list == NULL) {
5537 return -1;
Brett Warrene0edc842021-08-17 09:53:13 +01005538 }
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005539
Gilles Peskine449bd832023-01-11 14:50:10 +01005540 for (; *group_list != 0; group_list++) {
5541 if (*group_list == tls_id) {
5542 return 0;
5543 }
5544 }
5545
5546 return -1;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005547}
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005548
5549#if defined(MBEDTLS_ECP_C)
5550/*
5551 * Same as mbedtls_ssl_check_curve_tls_id() but with a mbedtls_ecp_group_id.
5552 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005553int mbedtls_ssl_check_curve(const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id)
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005554{
Gilles Peskine449bd832023-01-11 14:50:10 +01005555 uint16_t tls_id = mbedtls_ssl_get_tls_id_from_ecp_group_id(grp_id);
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005556
Gilles Peskine449bd832023-01-11 14:50:10 +01005557 if (tls_id == 0) {
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005558 return -1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005559 }
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005560
Gilles Peskine449bd832023-01-11 14:50:10 +01005561 return mbedtls_ssl_check_curve_tls_id(ssl, tls_id);
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005562}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02005563#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005564
Gilles Peskine449bd832023-01-11 14:50:10 +01005565#if defined(MBEDTLS_DEBUG_C)
Valerio Setti67419f02023-01-04 16:12:42 +01005566#define EC_NAME(_name_) _name_
5567#else
5568#define EC_NAME(_name_) NULL
5569#endif
5570
Valerio Setti18c9fed2022-12-30 17:44:24 +01005571static const struct {
5572 uint16_t tls_id;
5573 mbedtls_ecp_group_id ecp_group_id;
5574 psa_ecc_family_t psa_family;
5575 uint16_t bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01005576 const char *name;
Valerio Setti18c9fed2022-12-30 17:44:24 +01005577} tls_id_match_table[] =
5578{
5579#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_521)
Gilles Peskine449bd832023-01-11 14:50:10 +01005580 { 25, MBEDTLS_ECP_DP_SECP521R1, PSA_ECC_FAMILY_SECP_R1, 521, EC_NAME("secp521r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005581#endif
5582#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED) || defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512)
Gilles Peskine449bd832023-01-11 14:50:10 +01005583 { 28, MBEDTLS_ECP_DP_BP512R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 512, EC_NAME("brainpoolP512r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005584#endif
Valerio Setti67419f02023-01-04 16:12:42 +01005585#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_384)
Gilles Peskine449bd832023-01-11 14:50:10 +01005586 { 24, MBEDTLS_ECP_DP_SECP384R1, PSA_ECC_FAMILY_SECP_R1, 384, EC_NAME("secp384r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005587#endif
Valerio Setti67419f02023-01-04 16:12:42 +01005588#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED) || defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384)
Gilles Peskine449bd832023-01-11 14:50:10 +01005589 { 27, MBEDTLS_ECP_DP_BP384R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 384, EC_NAME("brainpoolP384r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005590#endif
Valerio Setti67419f02023-01-04 16:12:42 +01005591#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_256)
Gilles Peskine449bd832023-01-11 14:50:10 +01005592 { 23, MBEDTLS_ECP_DP_SECP256R1, PSA_ECC_FAMILY_SECP_R1, 256, EC_NAME("secp256r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005593#endif
5594#if defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED) || defined(PSA_WANT_ECC_SECP_K1_256)
Gilles Peskine449bd832023-01-11 14:50:10 +01005595 { 22, MBEDTLS_ECP_DP_SECP256K1, PSA_ECC_FAMILY_SECP_K1, 256, EC_NAME("secp256k1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005596#endif
Valerio Setti67419f02023-01-04 16:12:42 +01005597#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED) || defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256)
Gilles Peskine449bd832023-01-11 14:50:10 +01005598 { 26, MBEDTLS_ECP_DP_BP256R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 256, EC_NAME("brainpoolP256r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005599#endif
5600#if defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_224)
Gilles Peskine449bd832023-01-11 14:50:10 +01005601 { 21, MBEDTLS_ECP_DP_SECP224R1, PSA_ECC_FAMILY_SECP_R1, 224, EC_NAME("secp224r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005602#endif
5603#if defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED) || defined(PSA_WANT_ECC_SECP_K1_224)
Gilles Peskine449bd832023-01-11 14:50:10 +01005604 { 20, MBEDTLS_ECP_DP_SECP224K1, PSA_ECC_FAMILY_SECP_K1, 224, EC_NAME("secp224k1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005605#endif
5606#if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_192)
Gilles Peskine449bd832023-01-11 14:50:10 +01005607 { 19, MBEDTLS_ECP_DP_SECP192R1, PSA_ECC_FAMILY_SECP_R1, 192, EC_NAME("secp192r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005608#endif
5609#if defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED) || defined(PSA_WANT_ECC_SECP_K1_192)
Gilles Peskine449bd832023-01-11 14:50:10 +01005610 { 18, MBEDTLS_ECP_DP_SECP192K1, PSA_ECC_FAMILY_SECP_K1, 192, EC_NAME("secp192k1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005611#endif
5612#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED) || defined(PSA_WANT_ECC_MONTGOMERY_255)
Gilles Peskine449bd832023-01-11 14:50:10 +01005613 { 29, MBEDTLS_ECP_DP_CURVE25519, PSA_ECC_FAMILY_MONTGOMERY, 255, EC_NAME("x25519") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005614#endif
5615#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED) || defined(PSA_WANT_ECC_MONTGOMERY_448)
Gilles Peskine449bd832023-01-11 14:50:10 +01005616 { 30, MBEDTLS_ECP_DP_CURVE448, PSA_ECC_FAMILY_MONTGOMERY, 448, EC_NAME("x448") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005617#endif
5618 { 0, MBEDTLS_ECP_DP_NONE, 0, 0, NULL },
5619};
5620
Gilles Peskine449bd832023-01-11 14:50:10 +01005621int mbedtls_ssl_get_psa_curve_info_from_tls_id(uint16_t tls_id,
5622 psa_ecc_family_t *family,
5623 size_t *bits)
Valerio Setti18c9fed2022-12-30 17:44:24 +01005624{
Gilles Peskine449bd832023-01-11 14:50:10 +01005625 for (int i = 0; tls_id_match_table[i].tls_id != 0; i++) {
5626 if (tls_id_match_table[i].tls_id == tls_id) {
5627 if (family != NULL) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01005628 *family = tls_id_match_table[i].psa_family;
Gilles Peskine449bd832023-01-11 14:50:10 +01005629 }
5630 if (bits != NULL) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01005631 *bits = tls_id_match_table[i].bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01005632 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01005633 return PSA_SUCCESS;
5634 }
5635 }
5636
5637 return PSA_ERROR_NOT_SUPPORTED;
5638}
5639
Gilles Peskine449bd832023-01-11 14:50:10 +01005640mbedtls_ecp_group_id mbedtls_ssl_get_ecp_group_id_from_tls_id(uint16_t tls_id)
Valerio Setti18c9fed2022-12-30 17:44:24 +01005641{
Gilles Peskine449bd832023-01-11 14:50:10 +01005642 for (int i = 0; tls_id_match_table[i].tls_id != 0; i++) {
5643 if (tls_id_match_table[i].tls_id == tls_id) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01005644 return tls_id_match_table[i].ecp_group_id;
Gilles Peskine449bd832023-01-11 14:50:10 +01005645 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01005646 }
5647
5648 return MBEDTLS_ECP_DP_NONE;
5649}
5650
Gilles Peskine449bd832023-01-11 14:50:10 +01005651uint16_t mbedtls_ssl_get_tls_id_from_ecp_group_id(mbedtls_ecp_group_id grp_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].ecp_group_id != MBEDTLS_ECP_DP_NONE;
5654 i++) {
5655 if (tls_id_match_table[i].ecp_group_id == grp_id) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01005656 return tls_id_match_table[i].tls_id;
Gilles Peskine449bd832023-01-11 14:50:10 +01005657 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01005658 }
5659
5660 return 0;
5661}
5662
Valerio Setti67419f02023-01-04 16:12:42 +01005663#if defined(MBEDTLS_DEBUG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005664const char *mbedtls_ssl_get_curve_name_from_tls_id(uint16_t tls_id)
Valerio Setti18c9fed2022-12-30 17:44:24 +01005665{
Gilles Peskine449bd832023-01-11 14:50:10 +01005666 for (int i = 0; tls_id_match_table[i].tls_id != 0; i++) {
5667 if (tls_id_match_table[i].tls_id == tls_id) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01005668 return tls_id_match_table[i].name;
Gilles Peskine449bd832023-01-11 14:50:10 +01005669 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01005670 }
5671
5672 return NULL;
5673}
Valerio Setti67419f02023-01-04 16:12:42 +01005674#endif
Valerio Setti18c9fed2022-12-30 17:44:24 +01005675
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005676#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005677int mbedtls_ssl_check_cert_usage(const mbedtls_x509_crt *cert,
5678 const mbedtls_ssl_ciphersuite_t *ciphersuite,
5679 int cert_endpoint,
5680 uint32_t *flags)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005681{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005682 int ret = 0;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005683 int usage = 0;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005684 const char *ext_oid;
5685 size_t ext_len;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005686
Gilles Peskine449bd832023-01-11 14:50:10 +01005687 if (cert_endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005688 /* Server part of the key exchange */
Gilles Peskine449bd832023-01-11 14:50:10 +01005689 switch (ciphersuite->key_exchange) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005690 case MBEDTLS_KEY_EXCHANGE_RSA:
5691 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005692 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005693 break;
5694
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005695 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
5696 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
5697 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
5698 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005699 break;
5700
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005701 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
5702 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005703 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005704 break;
5705
5706 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005707 case MBEDTLS_KEY_EXCHANGE_NONE:
5708 case MBEDTLS_KEY_EXCHANGE_PSK:
5709 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
5710 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02005711 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005712 usage = 0;
5713 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005714 } else {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005715 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
5716 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005717 }
5718
Gilles Peskine449bd832023-01-11 14:50:10 +01005719 if (mbedtls_x509_crt_check_key_usage(cert, usage) != 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005720 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005721 ret = -1;
5722 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005723
Gilles Peskine449bd832023-01-11 14:50:10 +01005724 if (cert_endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005725 ext_oid = MBEDTLS_OID_SERVER_AUTH;
Gilles Peskine449bd832023-01-11 14:50:10 +01005726 ext_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_SERVER_AUTH);
5727 } else {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005728 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
Gilles Peskine449bd832023-01-11 14:50:10 +01005729 ext_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_CLIENT_AUTH);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005730 }
5731
Gilles Peskine449bd832023-01-11 14:50:10 +01005732 if (mbedtls_x509_crt_check_extended_key_usage(cert, ext_oid, ext_len) != 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005733 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005734 ret = -1;
5735 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005736
Gilles Peskine449bd832023-01-11 14:50:10 +01005737 return ret;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005738}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005739#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02005740
Jerry Yu148165c2021-09-24 23:20:59 +08005741#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01005742int mbedtls_ssl_get_handshake_transcript(mbedtls_ssl_context *ssl,
5743 const mbedtls_md_type_t md,
5744 unsigned char *dst,
5745 size_t dst_len,
5746 size_t *olen)
Jerry Yu148165c2021-09-24 23:20:59 +08005747{
Ronald Cronf6893e12022-01-07 22:09:01 +01005748 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5749 psa_hash_operation_t *hash_operation_to_clone;
5750 psa_hash_operation_t hash_operation = psa_hash_operation_init();
5751
Jerry Yu148165c2021-09-24 23:20:59 +08005752 *olen = 0;
Ronald Cronf6893e12022-01-07 22:09:01 +01005753
Gilles Peskine449bd832023-01-11 14:50:10 +01005754 switch (md) {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005755#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01005756 case MBEDTLS_MD_SHA384:
5757 hash_operation_to_clone = &ssl->handshake->fin_sha384_psa;
5758 break;
Ronald Cronf6893e12022-01-07 22:09:01 +01005759#endif
5760
Andrzej Kurek25f27152022-08-17 16:09:31 -04005761#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01005762 case MBEDTLS_MD_SHA256:
5763 hash_operation_to_clone = &ssl->handshake->fin_sha256_psa;
5764 break;
Ronald Cronf6893e12022-01-07 22:09:01 +01005765#endif
5766
Gilles Peskine449bd832023-01-11 14:50:10 +01005767 default:
5768 goto exit;
5769 }
5770
5771 status = psa_hash_clone(hash_operation_to_clone, &hash_operation);
5772 if (status != PSA_SUCCESS) {
Ronald Cronf6893e12022-01-07 22:09:01 +01005773 goto exit;
5774 }
5775
Gilles Peskine449bd832023-01-11 14:50:10 +01005776 status = psa_hash_finish(&hash_operation, dst, dst_len, olen);
5777 if (status != PSA_SUCCESS) {
Ronald Cronf6893e12022-01-07 22:09:01 +01005778 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005779 }
Ronald Cronf6893e12022-01-07 22:09:01 +01005780
5781exit:
Andrzej Kurekeabeb302022-10-17 07:52:51 -04005782#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
5783 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
5784 (void) ssl;
5785#endif
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05005786 return PSA_TO_MBEDTLS_ERR(status);
Jerry Yu148165c2021-09-24 23:20:59 +08005787}
5788#else /* MBEDTLS_USE_PSA_CRYPTO */
5789
Andrzej Kurek25f27152022-08-17 16:09:31 -04005790#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005791MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01005792static int ssl_get_handshake_transcript_sha384(mbedtls_ssl_context *ssl,
5793 unsigned char *dst,
5794 size_t dst_len,
5795 size_t *olen)
Jerry Yu24c0ec32021-09-09 14:21:07 +08005796{
Jerry Yu24c0ec32021-09-09 14:21:07 +08005797 int ret;
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01005798 mbedtls_md_context_t sha384;
Jerry Yu24c0ec32021-09-09 14:21:07 +08005799
Gilles Peskine449bd832023-01-11 14:50:10 +01005800 if (dst_len < 48) {
5801 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
5802 }
Jerry Yu24c0ec32021-09-09 14:21:07 +08005803
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01005804 mbedtls_md_init(&sha384);
5805 ret = mbedtls_md_setup(&sha384, mbedtls_md_info_from_type(MBEDTLS_MD_SHA384), 0);
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01005806 if (ret != 0) {
5807 goto exit;
5808 }
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01005809 ret = mbedtls_md_clone(&sha384, &ssl->handshake->fin_sha384);
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01005810 if (ret != 0) {
5811 goto exit;
5812 }
Jerry Yu24c0ec32021-09-09 14:21:07 +08005813
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01005814 if ((ret = mbedtls_md_finish(&sha384, dst)) != 0) {
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01005815 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_finish", ret);
Jerry Yu24c0ec32021-09-09 14:21:07 +08005816 goto exit;
5817 }
5818
5819 *olen = 48;
5820
5821exit:
5822
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01005823 mbedtls_md_free(&sha384);
Gilles Peskine449bd832023-01-11 14:50:10 +01005824 return ret;
Jerry Yu24c0ec32021-09-09 14:21:07 +08005825}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005826#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005827
Andrzej Kurek25f27152022-08-17 16:09:31 -04005828#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005829MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01005830static int ssl_get_handshake_transcript_sha256(mbedtls_ssl_context *ssl,
5831 unsigned char *dst,
5832 size_t dst_len,
5833 size_t *olen)
Jerry Yu24c0ec32021-09-09 14:21:07 +08005834{
Jerry Yu24c0ec32021-09-09 14:21:07 +08005835 int ret;
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01005836 mbedtls_md_context_t sha256;
Jerry Yu24c0ec32021-09-09 14:21:07 +08005837
Gilles Peskine449bd832023-01-11 14:50:10 +01005838 if (dst_len < 32) {
5839 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
5840 }
Jerry Yu24c0ec32021-09-09 14:21:07 +08005841
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01005842 mbedtls_md_init(&sha256);
5843 ret = mbedtls_md_setup(&sha256, mbedtls_md_info_from_type(MBEDTLS_MD_SHA256), 0);
5844 if (ret != 0) {
5845 goto exit;
5846 }
5847 ret = mbedtls_md_clone(&sha256, &ssl->handshake->fin_sha256);
5848 if (ret != 0) {
5849 goto exit;
5850 }
Jerry Yuc5aef882021-12-23 20:15:02 +08005851
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01005852 if ((ret = mbedtls_md_finish(&sha256, dst)) != 0) {
5853 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_finish", ret);
Jerry Yu24c0ec32021-09-09 14:21:07 +08005854 goto exit;
5855 }
5856
5857 *olen = 32;
5858
5859exit:
5860
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01005861 mbedtls_md_free(&sha256);
Gilles Peskine449bd832023-01-11 14:50:10 +01005862 return ret;
Jerry Yu24c0ec32021-09-09 14:21:07 +08005863}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005864#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005865
Gilles Peskine449bd832023-01-11 14:50:10 +01005866int mbedtls_ssl_get_handshake_transcript(mbedtls_ssl_context *ssl,
5867 const mbedtls_md_type_t md,
5868 unsigned char *dst,
5869 size_t dst_len,
5870 size_t *olen)
Jerry Yu24c0ec32021-09-09 14:21:07 +08005871{
Gilles Peskine449bd832023-01-11 14:50:10 +01005872 switch (md) {
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005873
Andrzej Kurek25f27152022-08-17 16:09:31 -04005874#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01005875 case MBEDTLS_MD_SHA384:
5876 return ssl_get_handshake_transcript_sha384(ssl, dst, dst_len, olen);
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005877#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005878
Andrzej Kurek25f27152022-08-17 16:09:31 -04005879#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01005880 case MBEDTLS_MD_SHA256:
5881 return ssl_get_handshake_transcript_sha256(ssl, dst, dst_len, olen);
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005882#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005883
Gilles Peskine449bd832023-01-11 14:50:10 +01005884 default:
Andrzej Kurek409248a2022-10-24 10:33:21 -04005885#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01005886 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
5887 (void) ssl;
5888 (void) dst;
5889 (void) dst_len;
5890 (void) olen;
Andrzej Kurek409248a2022-10-24 10:33:21 -04005891#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005892 break;
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005893 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005894 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu24c0ec32021-09-09 14:21:07 +08005895}
XiaokangQian647719a2021-12-07 09:16:29 +00005896
Jerry Yu148165c2021-09-24 23:20:59 +08005897#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005898
Ronald Crone68ab4f2022-10-05 12:46:29 +02005899#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Gabor Mezei078e8032022-04-27 21:17:56 +02005900/* mbedtls_ssl_parse_sig_alg_ext()
5901 *
5902 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
5903 * value (TLS 1.3 RFC8446):
5904 * enum {
5905 * ....
5906 * ecdsa_secp256r1_sha256( 0x0403 ),
5907 * ecdsa_secp384r1_sha384( 0x0503 ),
5908 * ecdsa_secp521r1_sha512( 0x0603 ),
5909 * ....
5910 * } SignatureScheme;
5911 *
5912 * struct {
5913 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
5914 * } SignatureSchemeList;
5915 *
5916 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
5917 * value (TLS 1.2 RFC5246):
5918 * enum {
5919 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
5920 * sha512(6), (255)
5921 * } HashAlgorithm;
5922 *
5923 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
5924 * SignatureAlgorithm;
5925 *
5926 * struct {
5927 * HashAlgorithm hash;
5928 * SignatureAlgorithm signature;
5929 * } SignatureAndHashAlgorithm;
5930 *
5931 * SignatureAndHashAlgorithm
5932 * supported_signature_algorithms<2..2^16-2>;
5933 *
5934 * The TLS 1.3 signature algorithm extension was defined to be a compatible
5935 * generalization of the TLS 1.2 signature algorithm extension.
5936 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
5937 * `SignatureScheme` field of TLS 1.3
5938 *
5939 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005940int mbedtls_ssl_parse_sig_alg_ext(mbedtls_ssl_context *ssl,
5941 const unsigned char *buf,
5942 const unsigned char *end)
Gabor Mezei078e8032022-04-27 21:17:56 +02005943{
5944 const unsigned char *p = buf;
5945 size_t supported_sig_algs_len = 0;
5946 const unsigned char *supported_sig_algs_end;
5947 uint16_t sig_alg;
5948 uint32_t common_idx = 0;
5949
Gilles Peskine449bd832023-01-11 14:50:10 +01005950 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
5951 supported_sig_algs_len = MBEDTLS_GET_UINT16_BE(p, 0);
Gabor Mezei078e8032022-04-27 21:17:56 +02005952 p += 2;
5953
Gilles Peskine449bd832023-01-11 14:50:10 +01005954 memset(ssl->handshake->received_sig_algs, 0,
5955 sizeof(ssl->handshake->received_sig_algs));
Gabor Mezei078e8032022-04-27 21:17:56 +02005956
Gilles Peskine449bd832023-01-11 14:50:10 +01005957 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, supported_sig_algs_len);
Gabor Mezei078e8032022-04-27 21:17:56 +02005958 supported_sig_algs_end = p + supported_sig_algs_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01005959 while (p < supported_sig_algs_end) {
5960 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, supported_sig_algs_end, 2);
5961 sig_alg = MBEDTLS_GET_UINT16_BE(p, 0);
Gabor Mezei078e8032022-04-27 21:17:56 +02005962 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01005963 MBEDTLS_SSL_DEBUG_MSG(4, ("received signature algorithm: 0x%x %s",
5964 sig_alg,
5965 mbedtls_ssl_sig_alg_to_str(sig_alg)));
Jerry Yu2fe6c632022-06-29 10:02:38 +08005966#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01005967 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 &&
5968 (!(mbedtls_ssl_sig_alg_is_supported(ssl, sig_alg) &&
5969 mbedtls_ssl_sig_alg_is_offered(ssl, sig_alg)))) {
Gabor Mezei078e8032022-04-27 21:17:56 +02005970 continue;
Jerry Yu2fe6c632022-06-29 10:02:38 +08005971 }
5972#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gabor Mezei078e8032022-04-27 21:17:56 +02005973
Gilles Peskine449bd832023-01-11 14:50:10 +01005974 MBEDTLS_SSL_DEBUG_MSG(4, ("valid signature algorithm: %s",
5975 mbedtls_ssl_sig_alg_to_str(sig_alg)));
Gabor Mezei078e8032022-04-27 21:17:56 +02005976
Gilles Peskine449bd832023-01-11 14:50:10 +01005977 if (common_idx + 1 < MBEDTLS_RECEIVED_SIG_ALGS_SIZE) {
Gabor Mezei078e8032022-04-27 21:17:56 +02005978 ssl->handshake->received_sig_algs[common_idx] = sig_alg;
5979 common_idx += 1;
5980 }
5981 }
5982 /* Check that we consumed all the message. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005983 if (p != end) {
5984 MBEDTLS_SSL_DEBUG_MSG(1,
5985 ("Signature algorithms extension length misaligned"));
5986 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
5987 MBEDTLS_ERR_SSL_DECODE_ERROR);
5988 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Gabor Mezei078e8032022-04-27 21:17:56 +02005989 }
5990
Gilles Peskine449bd832023-01-11 14:50:10 +01005991 if (common_idx == 0) {
5992 MBEDTLS_SSL_DEBUG_MSG(3, ("no signature algorithm in common"));
5993 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
5994 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
5995 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Gabor Mezei078e8032022-04-27 21:17:56 +02005996 }
5997
Gabor Mezei15b95a62022-05-09 16:37:58 +02005998 ssl->handshake->received_sig_algs[common_idx] = MBEDTLS_TLS_SIG_NONE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005999 return 0;
Gabor Mezei078e8032022-04-27 21:17:56 +02006000}
6001
Ronald Crone68ab4f2022-10-05 12:46:29 +02006002#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Gabor Mezei078e8032022-04-27 21:17:56 +02006003
Jerry Yudc7bd172022-02-17 13:44:15 +08006004#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6005
6006#if defined(MBEDTLS_USE_PSA_CRYPTO)
6007
Gilles Peskine449bd832023-01-11 14:50:10 +01006008static psa_status_t setup_psa_key_derivation(psa_key_derivation_operation_t *derivation,
6009 mbedtls_svc_key_id_t key,
6010 psa_algorithm_t alg,
6011 const unsigned char *raw_psk, size_t raw_psk_length,
6012 const unsigned char *seed, size_t seed_length,
6013 const unsigned char *label, size_t label_length,
6014 const unsigned char *other_secret,
6015 size_t other_secret_length,
6016 size_t capacity)
Jerry Yudc7bd172022-02-17 13:44:15 +08006017{
6018 psa_status_t status;
6019
Gilles Peskine449bd832023-01-11 14:50:10 +01006020 status = psa_key_derivation_setup(derivation, alg);
6021 if (status != PSA_SUCCESS) {
6022 return status;
6023 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006024
Gilles Peskine449bd832023-01-11 14:50:10 +01006025 if (PSA_ALG_IS_TLS12_PRF(alg) || PSA_ALG_IS_TLS12_PSK_TO_MS(alg)) {
6026 status = psa_key_derivation_input_bytes(derivation,
6027 PSA_KEY_DERIVATION_INPUT_SEED,
6028 seed, seed_length);
6029 if (status != PSA_SUCCESS) {
6030 return status;
Przemek Stekiel1f027032022-04-05 17:12:11 +02006031 }
6032
Gilles Peskine449bd832023-01-11 14:50:10 +01006033 if (other_secret != NULL) {
6034 status = psa_key_derivation_input_bytes(derivation,
6035 PSA_KEY_DERIVATION_INPUT_OTHER_SECRET,
6036 other_secret, other_secret_length);
6037 if (status != PSA_SUCCESS) {
6038 return status;
6039 }
6040 }
6041
6042 if (mbedtls_svc_key_id_is_null(key)) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006043 status = psa_key_derivation_input_bytes(
6044 derivation, PSA_KEY_DERIVATION_INPUT_SECRET,
Gilles Peskine449bd832023-01-11 14:50:10 +01006045 raw_psk, raw_psk_length);
6046 } else {
Jerry Yudc7bd172022-02-17 13:44:15 +08006047 status = psa_key_derivation_input_key(
Gilles Peskine449bd832023-01-11 14:50:10 +01006048 derivation, PSA_KEY_DERIVATION_INPUT_SECRET, key);
Jerry Yudc7bd172022-02-17 13:44:15 +08006049 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006050 if (status != PSA_SUCCESS) {
6051 return status;
6052 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006053
Gilles Peskine449bd832023-01-11 14:50:10 +01006054 status = psa_key_derivation_input_bytes(derivation,
6055 PSA_KEY_DERIVATION_INPUT_LABEL,
6056 label, label_length);
6057 if (status != PSA_SUCCESS) {
6058 return status;
6059 }
6060 } else {
6061 return PSA_ERROR_NOT_SUPPORTED;
Jerry Yudc7bd172022-02-17 13:44:15 +08006062 }
6063
Gilles Peskine449bd832023-01-11 14:50:10 +01006064 status = psa_key_derivation_set_capacity(derivation, capacity);
6065 if (status != PSA_SUCCESS) {
6066 return status;
6067 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006068
Gilles Peskine449bd832023-01-11 14:50:10 +01006069 return PSA_SUCCESS;
Jerry Yudc7bd172022-02-17 13:44:15 +08006070}
6071
Andrzej Kurek57d10632022-10-24 10:32:01 -04006072#if defined(PSA_WANT_ALG_SHA_384) || \
6073 defined(PSA_WANT_ALG_SHA_256)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006074MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006075static int tls_prf_generic(mbedtls_md_type_t md_type,
6076 const unsigned char *secret, size_t slen,
6077 const char *label,
6078 const unsigned char *random, size_t rlen,
6079 unsigned char *dstbuf, size_t dlen)
Jerry Yudc7bd172022-02-17 13:44:15 +08006080{
6081 psa_status_t status;
6082 psa_algorithm_t alg;
6083 mbedtls_svc_key_id_t master_key = MBEDTLS_SVC_KEY_ID_INIT;
6084 psa_key_derivation_operation_t derivation =
6085 PSA_KEY_DERIVATION_OPERATION_INIT;
6086
Gilles Peskine449bd832023-01-11 14:50:10 +01006087 if (md_type == MBEDTLS_MD_SHA384) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006088 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
Gilles Peskine449bd832023-01-11 14:50:10 +01006089 } else {
Jerry Yudc7bd172022-02-17 13:44:15 +08006090 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
Gilles Peskine449bd832023-01-11 14:50:10 +01006091 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006092
6093 /* Normally a "secret" should be long enough to be impossible to
6094 * find by brute force, and in particular should not be empty. But
6095 * this PRF is also used to derive an IV, in particular in EAP-TLS,
6096 * and for this use case it makes sense to have a 0-length "secret".
6097 * Since the key API doesn't allow importing a key of length 0,
6098 * keep master_key=0, which setup_psa_key_derivation() understands
6099 * to mean a 0-length "secret" input. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006100 if (slen != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006101 psa_key_attributes_t key_attributes = psa_key_attributes_init();
Gilles Peskine449bd832023-01-11 14:50:10 +01006102 psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE);
6103 psa_set_key_algorithm(&key_attributes, alg);
6104 psa_set_key_type(&key_attributes, PSA_KEY_TYPE_DERIVE);
Jerry Yudc7bd172022-02-17 13:44:15 +08006105
Gilles Peskine449bd832023-01-11 14:50:10 +01006106 status = psa_import_key(&key_attributes, secret, slen, &master_key);
6107 if (status != PSA_SUCCESS) {
6108 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
6109 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006110 }
6111
Gilles Peskine449bd832023-01-11 14:50:10 +01006112 status = setup_psa_key_derivation(&derivation,
6113 master_key, alg,
6114 NULL, 0,
6115 random, rlen,
6116 (unsigned char const *) label,
6117 (size_t) strlen(label),
6118 NULL, 0,
6119 dlen);
6120 if (status != PSA_SUCCESS) {
6121 psa_key_derivation_abort(&derivation);
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 status = psa_key_derivation_output_bytes(&derivation, dstbuf, dlen);
6127 if (status != PSA_SUCCESS) {
6128 psa_key_derivation_abort(&derivation);
6129 psa_destroy_key(master_key);
6130 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yudc7bd172022-02-17 13:44:15 +08006131 }
6132
Gilles Peskine449bd832023-01-11 14:50:10 +01006133 status = psa_key_derivation_abort(&derivation);
6134 if (status != PSA_SUCCESS) {
6135 psa_destroy_key(master_key);
6136 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yudc7bd172022-02-17 13:44:15 +08006137 }
6138
Gilles Peskine449bd832023-01-11 14:50:10 +01006139 if (!mbedtls_svc_key_id_is_null(master_key)) {
6140 status = psa_destroy_key(master_key);
6141 }
6142 if (status != PSA_SUCCESS) {
6143 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
6144 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006145
Gilles Peskine449bd832023-01-11 14:50:10 +01006146 return 0;
Jerry Yudc7bd172022-02-17 13:44:15 +08006147}
Andrzej Kurek57d10632022-10-24 10:32:01 -04006148#endif /* PSA_WANT_ALG_SHA_256 || PSA_WANT_ALG_SHA_384 */
Jerry Yudc7bd172022-02-17 13:44:15 +08006149#else /* MBEDTLS_USE_PSA_CRYPTO */
6150
Andrzej Kurek57d10632022-10-24 10:32:01 -04006151#if defined(MBEDTLS_MD_C) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01006152 (defined(MBEDTLS_SHA256_C) || \
6153 defined(MBEDTLS_SHA384_C))
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006154MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006155static int tls_prf_generic(mbedtls_md_type_t md_type,
6156 const unsigned char *secret, size_t slen,
6157 const char *label,
6158 const unsigned char *random, size_t rlen,
6159 unsigned char *dstbuf, size_t dlen)
Jerry Yudc7bd172022-02-17 13:44:15 +08006160{
6161 size_t nb;
6162 size_t i, j, k, md_len;
6163 unsigned char *tmp;
6164 size_t tmp_len = 0;
6165 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
6166 const mbedtls_md_info_t *md_info;
6167 mbedtls_md_context_t md_ctx;
6168 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6169
Gilles Peskine449bd832023-01-11 14:50:10 +01006170 mbedtls_md_init(&md_ctx);
Jerry Yudc7bd172022-02-17 13:44:15 +08006171
Gilles Peskine449bd832023-01-11 14:50:10 +01006172 if ((md_info = mbedtls_md_info_from_type(md_type)) == NULL) {
6173 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
6174 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006175
Gilles Peskine449bd832023-01-11 14:50:10 +01006176 md_len = mbedtls_md_get_size(md_info);
Jerry Yudc7bd172022-02-17 13:44:15 +08006177
Gilles Peskine449bd832023-01-11 14:50:10 +01006178 tmp_len = md_len + strlen(label) + rlen;
6179 tmp = mbedtls_calloc(1, tmp_len);
6180 if (tmp == NULL) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006181 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
6182 goto exit;
6183 }
6184
Gilles Peskine449bd832023-01-11 14:50:10 +01006185 nb = strlen(label);
6186 memcpy(tmp + md_len, label, nb);
6187 memcpy(tmp + md_len + nb, random, rlen);
Jerry Yudc7bd172022-02-17 13:44:15 +08006188 nb += rlen;
6189
6190 /*
6191 * Compute P_<hash>(secret, label + random)[0..dlen]
6192 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006193 if ((ret = mbedtls_md_setup(&md_ctx, md_info, 1)) != 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 ret = mbedtls_md_hmac_starts(&md_ctx, secret, slen);
6198 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006199 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006200 }
6201 ret = mbedtls_md_hmac_update(&md_ctx, tmp + md_len, nb);
6202 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006203 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006204 }
6205 ret = mbedtls_md_hmac_finish(&md_ctx, tmp);
6206 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006207 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006208 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006209
Gilles Peskine449bd832023-01-11 14:50:10 +01006210 for (i = 0; i < dlen; i += md_len) {
6211 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 + nb);
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, h_i);
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 ret = mbedtls_md_hmac_reset(&md_ctx);
6225 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006226 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006227 }
6228 ret = mbedtls_md_hmac_update(&md_ctx, tmp, md_len);
6229 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006230 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006231 }
6232 ret = mbedtls_md_hmac_finish(&md_ctx, tmp);
6233 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006234 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006235 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006236
Gilles Peskine449bd832023-01-11 14:50:10 +01006237 k = (i + md_len > dlen) ? dlen % md_len : md_len;
Jerry Yudc7bd172022-02-17 13:44:15 +08006238
Gilles Peskine449bd832023-01-11 14:50:10 +01006239 for (j = 0; j < k; j++) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006240 dstbuf[i + j] = h_i[j];
Gilles Peskine449bd832023-01-11 14:50:10 +01006241 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006242 }
6243
6244exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006245 mbedtls_md_free(&md_ctx);
Jerry Yudc7bd172022-02-17 13:44:15 +08006246
Gilles Peskine449bd832023-01-11 14:50:10 +01006247 if (tmp != NULL) {
6248 mbedtls_platform_zeroize(tmp, tmp_len);
6249 }
Dave Rodgman29b9b2b2022-11-01 16:08:14 +00006250
Gilles Peskine449bd832023-01-11 14:50:10 +01006251 mbedtls_platform_zeroize(h_i, sizeof(h_i));
Jerry Yudc7bd172022-02-17 13:44:15 +08006252
Gilles Peskine449bd832023-01-11 14:50:10 +01006253 mbedtls_free(tmp);
Jerry Yudc7bd172022-02-17 13:44:15 +08006254
Gilles Peskine449bd832023-01-11 14:50:10 +01006255 return ret;
Jerry Yudc7bd172022-02-17 13:44:15 +08006256}
Andrzej Kurek57d10632022-10-24 10:32:01 -04006257#endif /* MBEDTLS_MD_C && ( MBEDTLS_SHA256_C || MBEDTLS_SHA384_C ) */
Jerry Yudc7bd172022-02-17 13:44:15 +08006258#endif /* MBEDTLS_USE_PSA_CRYPTO */
6259
Andrzej Kurek25f27152022-08-17 16:09:31 -04006260#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006261MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006262static int tls_prf_sha256(const unsigned char *secret, size_t slen,
6263 const char *label,
6264 const unsigned char *random, size_t rlen,
6265 unsigned char *dstbuf, size_t dlen)
Jerry Yudc7bd172022-02-17 13:44:15 +08006266{
Gilles Peskine449bd832023-01-11 14:50:10 +01006267 return tls_prf_generic(MBEDTLS_MD_SHA256, secret, slen,
6268 label, random, rlen, dstbuf, dlen);
Jerry Yudc7bd172022-02-17 13:44:15 +08006269}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04006270#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudc7bd172022-02-17 13:44:15 +08006271
Andrzej Kurek25f27152022-08-17 16:09:31 -04006272#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006273MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006274static int tls_prf_sha384(const unsigned char *secret, size_t slen,
6275 const char *label,
6276 const unsigned char *random, size_t rlen,
6277 unsigned char *dstbuf, size_t dlen)
Jerry Yudc7bd172022-02-17 13:44:15 +08006278{
Gilles Peskine449bd832023-01-11 14:50:10 +01006279 return tls_prf_generic(MBEDTLS_MD_SHA384, secret, slen,
6280 label, random, rlen, dstbuf, dlen);
Jerry Yudc7bd172022-02-17 13:44:15 +08006281}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04006282#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudc7bd172022-02-17 13:44:15 +08006283
Jerry Yuf009d862022-02-17 14:01:37 +08006284/*
6285 * Set appropriate PRF function and other SSL / TLS1.2 functions
6286 *
6287 * Inputs:
Jerry Yuf009d862022-02-17 14:01:37 +08006288 * - hash associated with the ciphersuite (only used by TLS 1.2)
6289 *
6290 * Outputs:
6291 * - the tls_prf, calc_verify and calc_finished members of handshake structure
6292 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006293MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006294static int ssl_set_handshake_prfs(mbedtls_ssl_handshake_params *handshake,
6295 mbedtls_md_type_t hash)
Jerry Yuf009d862022-02-17 14:01:37 +08006296{
Andrzej Kurek25f27152022-08-17 16:09:31 -04006297#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01006298 if (hash == MBEDTLS_MD_SHA384) {
Jerry Yuf009d862022-02-17 14:01:37 +08006299 handshake->tls_prf = tls_prf_sha384;
6300 handshake->calc_verify = ssl_calc_verify_tls_sha384;
6301 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Gilles Peskine449bd832023-01-11 14:50:10 +01006302 } else
Jerry Yuf009d862022-02-17 14:01:37 +08006303#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04006304#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuf009d862022-02-17 14:01:37 +08006305 {
Ronald Cron81591aa2022-03-07 09:05:51 +01006306 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08006307 handshake->tls_prf = tls_prf_sha256;
6308 handshake->calc_verify = ssl_calc_verify_tls_sha256;
6309 handshake->calc_finished = ssl_calc_finished_tls_sha256;
6310 }
Ronald Cron81591aa2022-03-07 09:05:51 +01006311#else
Jerry Yuf009d862022-02-17 14:01:37 +08006312 {
Jerry Yuf009d862022-02-17 14:01:37 +08006313 (void) handshake;
Ronald Cron81591aa2022-03-07 09:05:51 +01006314 (void) hash;
Gilles Peskine449bd832023-01-11 14:50:10 +01006315 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yuf009d862022-02-17 14:01:37 +08006316 }
Ronald Cron81591aa2022-03-07 09:05:51 +01006317#endif
Jerry Yuf009d862022-02-17 14:01:37 +08006318
Gilles Peskine449bd832023-01-11 14:50:10 +01006319 return 0;
Jerry Yuf009d862022-02-17 14:01:37 +08006320}
Jerry Yud6ab2352022-02-17 14:03:43 +08006321
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006322/*
6323 * Compute master secret if needed
6324 *
6325 * Parameters:
6326 * [in/out] handshake
6327 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
6328 * (PSA-PSK) ciphersuite_info, psk_opaque
6329 * [out] premaster (cleared)
6330 * [out] master
6331 * [in] ssl: optionally used for debugging, EMS and PSA-PSK
6332 * debug: conf->f_dbg, conf->p_dbg
6333 * EMS: passed to calc_verify (debug + session_negotiate)
Ronald Crona25cf582022-03-07 11:10:36 +01006334 * PSA-PSA: conf
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006335 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006336MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006337static int ssl_compute_master(mbedtls_ssl_handshake_params *handshake,
6338 unsigned char *master,
6339 const mbedtls_ssl_context *ssl)
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006340{
6341 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6342
6343 /* cf. RFC 5246, Section 8.1:
6344 * "The master secret is always exactly 48 bytes in length." */
6345 size_t const master_secret_len = 48;
6346
6347#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
6348 unsigned char session_hash[48];
6349#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
6350
6351 /* The label for the KDF used for key expansion.
6352 * This is either "master secret" or "extended master secret"
6353 * depending on whether the Extended Master Secret extension
6354 * is used. */
6355 char const *lbl = "master secret";
6356
Przemek Stekielae4ed302022-04-05 17:15:55 +02006357 /* The seed for the KDF used for key expansion.
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006358 * - If the Extended Master Secret extension is not used,
6359 * this is ClientHello.Random + ServerHello.Random
6360 * (see Sect. 8.1 in RFC 5246).
6361 * - If the Extended Master Secret extension is used,
6362 * this is the transcript of the handshake so far.
6363 * (see Sect. 4 in RFC 7627). */
Przemek Stekielae4ed302022-04-05 17:15:55 +02006364 unsigned char const *seed = handshake->randbytes;
6365 size_t seed_len = 64;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006366
6367#if !defined(MBEDTLS_DEBUG_C) && \
6368 !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \
6369 !(defined(MBEDTLS_USE_PSA_CRYPTO) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01006370 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED))
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006371 ssl = NULL; /* make sure we don't use it except for those cases */
6372 (void) ssl;
6373#endif
6374
Gilles Peskine449bd832023-01-11 14:50:10 +01006375 if (handshake->resume != 0) {
6376 MBEDTLS_SSL_DEBUG_MSG(3, ("no premaster (session resumed)"));
6377 return 0;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006378 }
6379
6380#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Gilles Peskine449bd832023-01-11 14:50:10 +01006381 if (handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED) {
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006382 lbl = "extended master secret";
Przemek Stekielae4ed302022-04-05 17:15:55 +02006383 seed = session_hash;
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01006384 ret = handshake->calc_verify(ssl, session_hash, &seed_len);
6385 if (ret != 0) {
6386 MBEDTLS_SSL_DEBUG_RET(1, "calc_verify", ret);
6387 }
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006388
Gilles Peskine449bd832023-01-11 14:50:10 +01006389 MBEDTLS_SSL_DEBUG_BUF(3, "session hash for extended master secret",
6390 session_hash, seed_len);
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006391 }
Przemek Stekiel169bf0b2022-04-29 07:53:29 +02006392#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006393
Przemek Stekiel99114f32022-04-22 11:20:09 +02006394#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Przemek Stekiel8a4b7fd2022-04-28 09:22:22 +02006395 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006396 if (mbedtls_ssl_ciphersuite_uses_psk(handshake->ciphersuite_info) == 1) {
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006397 /* Perform PSK-to-MS expansion in a single step. */
6398 psa_status_t status;
6399 psa_algorithm_t alg;
6400 mbedtls_svc_key_id_t psk;
6401 psa_key_derivation_operation_t derivation =
6402 PSA_KEY_DERIVATION_OPERATION_INIT;
6403 mbedtls_md_type_t hash_alg = handshake->ciphersuite_info->mac;
6404
Gilles Peskine449bd832023-01-11 14:50:10 +01006405 MBEDTLS_SSL_DEBUG_MSG(2, ("perform PSA-based PSK-to-MS expansion"));
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006406
Gilles Peskine449bd832023-01-11 14:50:10 +01006407 psk = mbedtls_ssl_get_opaque_psk(ssl);
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006408
Gilles Peskine449bd832023-01-11 14:50:10 +01006409 if (hash_alg == MBEDTLS_MD_SHA384) {
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006410 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
Gilles Peskine449bd832023-01-11 14:50:10 +01006411 } else {
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006412 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
Gilles Peskine449bd832023-01-11 14:50:10 +01006413 }
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006414
Przemek Stekiel51a1f362022-04-13 08:57:06 +02006415 size_t other_secret_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006416 unsigned char *other_secret = NULL;
Przemek Stekielc2033402022-04-05 17:19:41 +02006417
Gilles Peskine449bd832023-01-11 14:50:10 +01006418 switch (handshake->ciphersuite_info->key_exchange) {
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006419 /* Provide other secret.
Przemek Stekiel51a1f362022-04-13 08:57:06 +02006420 * Other secret is stored in premaster, where first 2 bytes hold the
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006421 * length of the other key.
Przemek Stekielc2033402022-04-05 17:19:41 +02006422 */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006423 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Przemek Stekiel8abcee92022-04-28 09:16:28 +02006424 /* For RSA-PSK other key length is always 48 bytes. */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006425 other_secret_len = 48;
6426 other_secret = handshake->premaster + 2;
6427 break;
6428 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006429 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006430 other_secret_len = MBEDTLS_GET_UINT16_BE(handshake->premaster, 0);
6431 other_secret = handshake->premaster + 2;
6432 break;
6433 default:
6434 break;
Przemek Stekielc2033402022-04-05 17:19:41 +02006435 }
6436
Gilles Peskine449bd832023-01-11 14:50:10 +01006437 status = setup_psa_key_derivation(&derivation, psk, alg,
6438 ssl->conf->psk, ssl->conf->psk_len,
6439 seed, seed_len,
6440 (unsigned char const *) lbl,
6441 (size_t) strlen(lbl),
6442 other_secret, other_secret_len,
6443 master_secret_len);
6444 if (status != PSA_SUCCESS) {
6445 psa_key_derivation_abort(&derivation);
6446 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006447 }
6448
Gilles Peskine449bd832023-01-11 14:50:10 +01006449 status = psa_key_derivation_output_bytes(&derivation,
6450 master,
6451 master_secret_len);
6452 if (status != PSA_SUCCESS) {
6453 psa_key_derivation_abort(&derivation);
6454 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006455 }
6456
Gilles Peskine449bd832023-01-11 14:50:10 +01006457 status = psa_key_derivation_abort(&derivation);
6458 if (status != PSA_SUCCESS) {
6459 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
6460 }
6461 } else
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006462#endif
6463 {
Neil Armstrongca7d5062022-05-31 14:43:23 +02006464#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01006465 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
6466 if (handshake->ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE) {
Neil Armstrongca7d5062022-05-31 14:43:23 +02006467 psa_status_t status;
6468 psa_algorithm_t alg = PSA_ALG_TLS12_ECJPAKE_TO_PMS;
6469 psa_key_derivation_operation_t derivation =
6470 PSA_KEY_DERIVATION_OPERATION_INIT;
6471
Gilles Peskine449bd832023-01-11 14:50:10 +01006472 MBEDTLS_SSL_DEBUG_MSG(2, ("perform PSA-based PMS KDF for ECJPAKE"));
Neil Armstrongca7d5062022-05-31 14:43:23 +02006473
6474 handshake->pmslen = PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE;
6475
Gilles Peskine449bd832023-01-11 14:50:10 +01006476 status = psa_key_derivation_setup(&derivation, alg);
6477 if (status != PSA_SUCCESS) {
6478 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Neil Armstrongca7d5062022-05-31 14:43:23 +02006479 }
6480
Gilles Peskine449bd832023-01-11 14:50:10 +01006481 status = psa_key_derivation_set_capacity(&derivation,
6482 PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE);
6483 if (status != PSA_SUCCESS) {
6484 psa_key_derivation_abort(&derivation);
6485 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Neil Armstrongca7d5062022-05-31 14:43:23 +02006486 }
6487
Gilles Peskine449bd832023-01-11 14:50:10 +01006488 status = psa_pake_get_implicit_key(&handshake->psa_pake_ctx,
6489 &derivation);
6490 if (status != PSA_SUCCESS) {
6491 psa_key_derivation_abort(&derivation);
6492 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Neil Armstrongca7d5062022-05-31 14:43:23 +02006493 }
6494
Gilles Peskine449bd832023-01-11 14:50:10 +01006495 status = psa_key_derivation_output_bytes(&derivation,
6496 handshake->premaster,
6497 handshake->pmslen);
6498 if (status != PSA_SUCCESS) {
6499 psa_key_derivation_abort(&derivation);
6500 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
6501 }
6502
6503 status = psa_key_derivation_abort(&derivation);
6504 if (status != PSA_SUCCESS) {
6505 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Neil Armstrongca7d5062022-05-31 14:43:23 +02006506 }
6507 }
6508#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01006509 ret = handshake->tls_prf(handshake->premaster, handshake->pmslen,
6510 lbl, seed, seed_len,
6511 master,
6512 master_secret_len);
6513 if (ret != 0) {
6514 MBEDTLS_SSL_DEBUG_RET(1, "prf", ret);
6515 return ret;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006516 }
6517
Gilles Peskine449bd832023-01-11 14:50:10 +01006518 MBEDTLS_SSL_DEBUG_BUF(3, "premaster secret",
6519 handshake->premaster,
6520 handshake->pmslen);
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006521
Gilles Peskine449bd832023-01-11 14:50:10 +01006522 mbedtls_platform_zeroize(handshake->premaster,
6523 sizeof(handshake->premaster));
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006524 }
6525
Gilles Peskine449bd832023-01-11 14:50:10 +01006526 return 0;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006527}
6528
Gilles Peskine449bd832023-01-11 14:50:10 +01006529int mbedtls_ssl_derive_keys(mbedtls_ssl_context *ssl)
Jerry Yud62f87e2022-02-17 14:09:02 +08006530{
6531 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6532 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
6533 ssl->handshake->ciphersuite_info;
6534
Gilles Peskine449bd832023-01-11 14:50:10 +01006535 MBEDTLS_SSL_DEBUG_MSG(2, ("=> derive keys"));
Jerry Yud62f87e2022-02-17 14:09:02 +08006536
6537 /* Set PRF, calc_verify and calc_finished function pointers */
Gilles Peskine449bd832023-01-11 14:50:10 +01006538 ret = ssl_set_handshake_prfs(ssl->handshake,
6539 ciphersuite_info->mac);
6540 if (ret != 0) {
6541 MBEDTLS_SSL_DEBUG_RET(1, "ssl_set_handshake_prfs", ret);
6542 return ret;
Jerry Yud62f87e2022-02-17 14:09:02 +08006543 }
6544
6545 /* Compute master secret if needed */
Gilles Peskine449bd832023-01-11 14:50:10 +01006546 ret = ssl_compute_master(ssl->handshake,
6547 ssl->session_negotiate->master,
6548 ssl);
6549 if (ret != 0) {
6550 MBEDTLS_SSL_DEBUG_RET(1, "ssl_compute_master", ret);
6551 return ret;
Jerry Yud62f87e2022-02-17 14:09:02 +08006552 }
6553
6554 /* Swap the client and server random values:
6555 * - MS derivation wanted client+server (RFC 5246 8.1)
6556 * - key derivation wants server+client (RFC 5246 6.3) */
6557 {
6558 unsigned char tmp[64];
Gilles Peskine449bd832023-01-11 14:50:10 +01006559 memcpy(tmp, ssl->handshake->randbytes, 64);
6560 memcpy(ssl->handshake->randbytes, tmp + 32, 32);
6561 memcpy(ssl->handshake->randbytes + 32, tmp, 32);
6562 mbedtls_platform_zeroize(tmp, sizeof(tmp));
Jerry Yud62f87e2022-02-17 14:09:02 +08006563 }
6564
6565 /* Populate transform structure */
Gilles Peskine449bd832023-01-11 14:50:10 +01006566 ret = ssl_tls12_populate_transform(ssl->transform_negotiate,
6567 ssl->session_negotiate->ciphersuite,
6568 ssl->session_negotiate->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02006569#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01006570 ssl->session_negotiate->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02006571#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01006572 ssl->handshake->tls_prf,
6573 ssl->handshake->randbytes,
6574 ssl->tls_version,
6575 ssl->conf->endpoint,
6576 ssl);
6577 if (ret != 0) {
6578 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls12_populate_transform", ret);
6579 return ret;
Jerry Yud62f87e2022-02-17 14:09:02 +08006580 }
6581
6582 /* We no longer need Server/ClientHello.random values */
Gilles Peskine449bd832023-01-11 14:50:10 +01006583 mbedtls_platform_zeroize(ssl->handshake->randbytes,
6584 sizeof(ssl->handshake->randbytes));
Jerry Yud62f87e2022-02-17 14:09:02 +08006585
Gilles Peskine449bd832023-01-11 14:50:10 +01006586 MBEDTLS_SSL_DEBUG_MSG(2, ("<= derive keys"));
Jerry Yud62f87e2022-02-17 14:09:02 +08006587
Gilles Peskine449bd832023-01-11 14:50:10 +01006588 return 0;
Jerry Yud62f87e2022-02-17 14:09:02 +08006589}
Jerry Yu8392e0d2022-02-17 14:10:24 +08006590
Gilles Peskine449bd832023-01-11 14:50:10 +01006591int mbedtls_ssl_set_calc_verify_md(mbedtls_ssl_context *ssl, int md)
Ronald Cron4dcbca92022-03-07 10:21:40 +01006592{
Gilles Peskine449bd832023-01-11 14:50:10 +01006593 switch (md) {
Andrzej Kurek25f27152022-08-17 16:09:31 -04006594#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron4dcbca92022-03-07 10:21:40 +01006595 case MBEDTLS_SSL_HASH_SHA384:
6596 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
6597 break;
6598#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04006599#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron4dcbca92022-03-07 10:21:40 +01006600 case MBEDTLS_SSL_HASH_SHA256:
6601 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
6602 break;
6603#endif
6604 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006605 return -1;
Ronald Cron4dcbca92022-03-07 10:21:40 +01006606 }
Andrzej Kurekeabeb302022-10-17 07:52:51 -04006607#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
6608 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
6609 (void) ssl;
6610#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01006611 return 0;
Ronald Cron4dcbca92022-03-07 10:21:40 +01006612}
6613
Andrzej Kurek25f27152022-08-17 16:09:31 -04006614#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01006615int ssl_calc_verify_tls_sha256(const mbedtls_ssl_context *ssl,
6616 unsigned char *hash,
6617 size_t *hlen)
Jerry Yu8392e0d2022-02-17 14:10:24 +08006618{
6619#if defined(MBEDTLS_USE_PSA_CRYPTO)
6620 size_t hash_size;
6621 psa_status_t status;
6622 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
6623
Gilles Peskine449bd832023-01-11 14:50:10 +01006624 MBEDTLS_SSL_DEBUG_MSG(2, ("=> PSA calc verify sha256"));
6625 status = psa_hash_clone(&ssl->handshake->fin_sha256_psa, &sha256_psa);
6626 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006627 goto exit;
Jerry Yu8392e0d2022-02-17 14:10:24 +08006628 }
6629
Gilles Peskine449bd832023-01-11 14:50:10 +01006630 status = psa_hash_finish(&sha256_psa, hash, 32, &hash_size);
6631 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006632 goto exit;
Jerry Yu8392e0d2022-02-17 14:10:24 +08006633 }
6634
6635 *hlen = 32;
Gilles Peskine449bd832023-01-11 14:50:10 +01006636 MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated verify result", hash, *hlen);
6637 MBEDTLS_SSL_DEBUG_MSG(2, ("<= PSA calc verify"));
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006638
6639exit:
6640 psa_hash_abort(&sha256_psa);
Andrzej Kurekdaf5b562023-03-03 05:52:28 -05006641 return PSA_TO_MD_ERR(status);
Jerry Yu8392e0d2022-02-17 14:10:24 +08006642#else
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006643 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01006644 mbedtls_md_context_t sha256;
Jerry Yu8392e0d2022-02-17 14:10:24 +08006645
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01006646 mbedtls_md_init(&sha256);
Jerry Yu8392e0d2022-02-17 14:10:24 +08006647
Gilles Peskine449bd832023-01-11 14:50:10 +01006648 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc verify sha256"));
Jerry Yu8392e0d2022-02-17 14:10:24 +08006649
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01006650 ret = mbedtls_md_setup(&sha256, mbedtls_md_info_from_type(MBEDTLS_MD_SHA256), 0);
6651 if (ret != 0) {
6652 goto exit;
6653 }
6654 ret = mbedtls_md_clone(&sha256, &ssl->handshake->fin_sha256);
6655 if (ret != 0) {
6656 goto exit;
6657 }
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006658
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01006659 ret = mbedtls_md_finish(&sha256, hash);
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006660 if (ret != 0) {
6661 goto exit;
6662 }
Jerry Yu8392e0d2022-02-17 14:10:24 +08006663
6664 *hlen = 32;
6665
Gilles Peskine449bd832023-01-11 14:50:10 +01006666 MBEDTLS_SSL_DEBUG_BUF(3, "calculated verify result", hash, *hlen);
6667 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc verify"));
Jerry Yu8392e0d2022-02-17 14:10:24 +08006668
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006669exit:
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01006670 mbedtls_md_free(&sha256);
Manuel Pégourié-Gonnard8e176f72023-02-09 10:33:54 +01006671 return ret;
Jerry Yu8392e0d2022-02-17 14:10:24 +08006672#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu8392e0d2022-02-17 14:10:24 +08006673}
Andrzej Kurek25f27152022-08-17 16:09:31 -04006674#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu8392e0d2022-02-17 14:10:24 +08006675
Andrzej Kurek25f27152022-08-17 16:09:31 -04006676#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01006677int ssl_calc_verify_tls_sha384(const mbedtls_ssl_context *ssl,
6678 unsigned char *hash,
6679 size_t *hlen)
Jerry Yuc1cb3842022-02-17 14:13:48 +08006680{
6681#if defined(MBEDTLS_USE_PSA_CRYPTO)
6682 size_t hash_size;
6683 psa_status_t status;
6684 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
6685
Gilles Peskine449bd832023-01-11 14:50:10 +01006686 MBEDTLS_SSL_DEBUG_MSG(2, ("=> PSA calc verify sha384"));
6687 status = psa_hash_clone(&ssl->handshake->fin_sha384_psa, &sha384_psa);
6688 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006689 goto exit;
Jerry Yuc1cb3842022-02-17 14:13:48 +08006690 }
6691
Gilles Peskine449bd832023-01-11 14:50:10 +01006692 status = psa_hash_finish(&sha384_psa, hash, 48, &hash_size);
6693 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006694 goto exit;
Jerry Yuc1cb3842022-02-17 14:13:48 +08006695 }
6696
6697 *hlen = 48;
Gilles Peskine449bd832023-01-11 14:50:10 +01006698 MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated verify result", hash, *hlen);
6699 MBEDTLS_SSL_DEBUG_MSG(2, ("<= PSA calc verify"));
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006700
6701exit:
6702 psa_hash_abort(&sha384_psa);
Andrzej Kurekdaf5b562023-03-03 05:52:28 -05006703 return PSA_TO_MD_ERR(status);
Jerry Yuc1cb3842022-02-17 14:13:48 +08006704#else
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006705 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01006706 mbedtls_md_context_t sha384;
Jerry Yuc1cb3842022-02-17 14:13:48 +08006707
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01006708 mbedtls_md_init(&sha384);
Jerry Yuc1cb3842022-02-17 14:13:48 +08006709
Gilles Peskine449bd832023-01-11 14:50:10 +01006710 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc verify sha384"));
Jerry Yuc1cb3842022-02-17 14:13:48 +08006711
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01006712 ret = mbedtls_md_setup(&sha384, mbedtls_md_info_from_type(MBEDTLS_MD_SHA384), 0);
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01006713 if (ret != 0) {
6714 goto exit;
6715 }
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01006716 ret = mbedtls_md_clone(&sha384, &ssl->handshake->fin_sha384);
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01006717 if (ret != 0) {
6718 goto exit;
6719 }
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006720
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01006721 ret = mbedtls_md_finish(&sha384, hash);
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006722 if (ret != 0) {
6723 goto exit;
6724 }
Jerry Yuc1cb3842022-02-17 14:13:48 +08006725
6726 *hlen = 48;
6727
Gilles Peskine449bd832023-01-11 14:50:10 +01006728 MBEDTLS_SSL_DEBUG_BUF(3, "calculated verify result", hash, *hlen);
6729 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc verify"));
Jerry Yuc1cb3842022-02-17 14:13:48 +08006730
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006731exit:
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01006732 mbedtls_md_free(&sha384);
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006733 return ret;
Jerry Yuc1cb3842022-02-17 14:13:48 +08006734#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yuc1cb3842022-02-17 14:13:48 +08006735}
Andrzej Kurek25f27152022-08-17 16:09:31 -04006736#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yuc1cb3842022-02-17 14:13:48 +08006737
Neil Armstrong80f6f322022-05-03 17:56:38 +02006738#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
6739 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006740int mbedtls_ssl_psk_derive_premaster(mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex)
Jerry Yuce3dca42022-02-17 14:16:37 +08006741{
6742 unsigned char *p = ssl->handshake->premaster;
Gilles Peskine449bd832023-01-11 14:50:10 +01006743 unsigned char *end = p + sizeof(ssl->handshake->premaster);
Jerry Yuce3dca42022-02-17 14:16:37 +08006744 const unsigned char *psk = NULL;
6745 size_t psk_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006746 int psk_ret = mbedtls_ssl_get_psk(ssl, &psk, &psk_len);
Jerry Yuce3dca42022-02-17 14:16:37 +08006747
Gilles Peskine449bd832023-01-11 14:50:10 +01006748 if (psk_ret == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED) {
Jerry Yuce3dca42022-02-17 14:16:37 +08006749 /*
6750 * This should never happen because the existence of a PSK is always
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006751 * checked before calling this function.
6752 *
6753 * The exception is opaque DHE-PSK. For DHE-PSK fill premaster with
Przemek Stekiel8abcee92022-04-28 09:16:28 +02006754 * the shared secret without PSK.
Jerry Yuce3dca42022-02-17 14:16:37 +08006755 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006756 if (key_ex != MBEDTLS_KEY_EXCHANGE_DHE_PSK) {
6757 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
6758 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006759 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006760 }
6761
6762 /*
6763 * PMS = struct {
6764 * opaque other_secret<0..2^16-1>;
6765 * opaque psk<0..2^16-1>;
6766 * };
6767 * with "other_secret" depending on the particular key exchange
6768 */
6769#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006770 if (key_ex == MBEDTLS_KEY_EXCHANGE_PSK) {
6771 if (end - p < 2) {
6772 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6773 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006774
Gilles Peskine449bd832023-01-11 14:50:10 +01006775 MBEDTLS_PUT_UINT16_BE(psk_len, p, 0);
Jerry Yuce3dca42022-02-17 14:16:37 +08006776 p += 2;
6777
Gilles Peskine449bd832023-01-11 14:50:10 +01006778 if (end < p || (size_t) (end - p) < psk_len) {
6779 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6780 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006781
Gilles Peskine449bd832023-01-11 14:50:10 +01006782 memset(p, 0, psk_len);
Jerry Yuce3dca42022-02-17 14:16:37 +08006783 p += psk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01006784 } else
Jerry Yuce3dca42022-02-17 14:16:37 +08006785#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
6786#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006787 if (key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK) {
Jerry Yuce3dca42022-02-17 14:16:37 +08006788 /*
6789 * other_secret already set by the ClientKeyExchange message,
6790 * and is 48 bytes long
6791 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006792 if (end - p < 2) {
6793 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6794 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006795
6796 *p++ = 0;
6797 *p++ = 48;
6798 p += 48;
Gilles Peskine449bd832023-01-11 14:50:10 +01006799 } else
Jerry Yuce3dca42022-02-17 14:16:37 +08006800#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
6801#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006802 if (key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK) {
Jerry Yuce3dca42022-02-17 14:16:37 +08006803 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6804 size_t len;
6805
6806 /* Write length only when we know the actual value */
Gilles Peskine449bd832023-01-11 14:50:10 +01006807 if ((ret = mbedtls_dhm_calc_secret(&ssl->handshake->dhm_ctx,
6808 p + 2, end - (p + 2), &len,
6809 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
6810 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_calc_secret", ret);
6811 return ret;
Jerry Yuce3dca42022-02-17 14:16:37 +08006812 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006813 MBEDTLS_PUT_UINT16_BE(len, p, 0);
Jerry Yuce3dca42022-02-17 14:16:37 +08006814 p += 2 + len;
6815
Gilles Peskine449bd832023-01-11 14:50:10 +01006816 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: K ", &ssl->handshake->dhm_ctx.K);
6817 } else
Jerry Yuce3dca42022-02-17 14:16:37 +08006818#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Neil Armstrong80f6f322022-05-03 17:56:38 +02006819#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006820 if (key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK) {
Jerry Yuce3dca42022-02-17 14:16:37 +08006821 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6822 size_t zlen;
6823
Gilles Peskine449bd832023-01-11 14:50:10 +01006824 if ((ret = mbedtls_ecdh_calc_secret(&ssl->handshake->ecdh_ctx, &zlen,
6825 p + 2, end - (p + 2),
6826 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
6827 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecdh_calc_secret", ret);
6828 return ret;
Jerry Yuce3dca42022-02-17 14:16:37 +08006829 }
6830
Gilles Peskine449bd832023-01-11 14:50:10 +01006831 MBEDTLS_PUT_UINT16_BE(zlen, p, 0);
Jerry Yuce3dca42022-02-17 14:16:37 +08006832 p += 2 + zlen;
6833
Gilles Peskine449bd832023-01-11 14:50:10 +01006834 MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx,
6835 MBEDTLS_DEBUG_ECDH_Z);
6836 } else
Neil Armstrong80f6f322022-05-03 17:56:38 +02006837#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Jerry Yuce3dca42022-02-17 14:16:37 +08006838 {
Gilles Peskine449bd832023-01-11 14:50:10 +01006839 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
6840 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yuce3dca42022-02-17 14:16:37 +08006841 }
6842
6843 /* opaque psk<0..2^16-1>; */
Gilles Peskine449bd832023-01-11 14:50:10 +01006844 if (end - p < 2) {
6845 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6846 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006847
Gilles Peskine449bd832023-01-11 14:50:10 +01006848 MBEDTLS_PUT_UINT16_BE(psk_len, p, 0);
Jerry Yuce3dca42022-02-17 14:16:37 +08006849 p += 2;
6850
Gilles Peskine449bd832023-01-11 14:50:10 +01006851 if (end < p || (size_t) (end - p) < psk_len) {
6852 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6853 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006854
Gilles Peskine449bd832023-01-11 14:50:10 +01006855 memcpy(p, psk, psk_len);
Jerry Yuce3dca42022-02-17 14:16:37 +08006856 p += psk_len;
6857
6858 ssl->handshake->pmslen = p - ssl->handshake->premaster;
6859
Gilles Peskine449bd832023-01-11 14:50:10 +01006860 return 0;
Jerry Yuce3dca42022-02-17 14:16:37 +08006861}
Neil Armstrong80f6f322022-05-03 17:56:38 +02006862#endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Jerry Yuc2c673d2022-02-17 14:20:39 +08006863
6864#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006865MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006866static int ssl_write_hello_request(mbedtls_ssl_context *ssl);
Jerry Yuc2c673d2022-02-17 14:20:39 +08006867
6868#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006869int mbedtls_ssl_resend_hello_request(mbedtls_ssl_context *ssl)
Jerry Yuc2c673d2022-02-17 14:20:39 +08006870{
6871 /* If renegotiation is not enforced, retransmit until we would reach max
6872 * timeout if we were using the usual handshake doubling scheme */
Gilles Peskine449bd832023-01-11 14:50:10 +01006873 if (ssl->conf->renego_max_records < 0) {
Jerry Yuc2c673d2022-02-17 14:20:39 +08006874 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
6875 unsigned char doublings = 1;
6876
Gilles Peskine449bd832023-01-11 14:50:10 +01006877 while (ratio != 0) {
Jerry Yuc2c673d2022-02-17 14:20:39 +08006878 ++doublings;
6879 ratio >>= 1;
6880 }
6881
Gilles Peskine449bd832023-01-11 14:50:10 +01006882 if (++ssl->renego_records_seen > doublings) {
6883 MBEDTLS_SSL_DEBUG_MSG(2, ("no longer retransmitting hello request"));
6884 return 0;
Jerry Yuc2c673d2022-02-17 14:20:39 +08006885 }
6886 }
6887
Gilles Peskine449bd832023-01-11 14:50:10 +01006888 return ssl_write_hello_request(ssl);
Jerry Yuc2c673d2022-02-17 14:20:39 +08006889}
6890#endif
6891#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Jerry Yud9526692022-02-17 14:23:47 +08006892
Jerry Yud9526692022-02-17 14:23:47 +08006893/*
6894 * Handshake functions
6895 */
6896#if !defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
6897/* No certificate support -> dummy functions */
Gilles Peskine449bd832023-01-11 14:50:10 +01006898int mbedtls_ssl_write_certificate(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08006899{
6900 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6901 ssl->handshake->ciphersuite_info;
6902
Gilles Peskine449bd832023-01-11 14:50:10 +01006903 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006904
Gilles Peskine449bd832023-01-11 14:50:10 +01006905 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
6906 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006907 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01006908 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08006909 }
6910
Gilles Peskine449bd832023-01-11 14:50:10 +01006911 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
6912 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08006913}
6914
Gilles Peskine449bd832023-01-11 14:50:10 +01006915int mbedtls_ssl_parse_certificate(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08006916{
6917 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6918 ssl->handshake->ciphersuite_info;
6919
Gilles Peskine449bd832023-01-11 14:50:10 +01006920 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006921
Gilles Peskine449bd832023-01-11 14:50:10 +01006922 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
6923 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006924 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01006925 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08006926 }
6927
Gilles Peskine449bd832023-01-11 14:50:10 +01006928 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
6929 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08006930}
6931
6932#else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
6933/* Some certificate support -> implement write and parse */
6934
Gilles Peskine449bd832023-01-11 14:50:10 +01006935int mbedtls_ssl_write_certificate(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08006936{
6937 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
6938 size_t i, n;
6939 const mbedtls_x509_crt *crt;
6940 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6941 ssl->handshake->ciphersuite_info;
6942
Gilles Peskine449bd832023-01-11 14:50:10 +01006943 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006944
Gilles Peskine449bd832023-01-11 14:50:10 +01006945 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
6946 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006947 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01006948 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08006949 }
6950
6951#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01006952 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
6953 if (ssl->handshake->client_auth == 0) {
6954 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006955 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01006956 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08006957 }
6958 }
6959#endif /* MBEDTLS_SSL_CLI_C */
6960#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01006961 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
6962 if (mbedtls_ssl_own_cert(ssl) == NULL) {
Jerry Yud9526692022-02-17 14:23:47 +08006963 /* Should never happen because we shouldn't have picked the
6964 * ciphersuite if we don't have a certificate. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006965 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08006966 }
6967 }
6968#endif
6969
Gilles Peskine449bd832023-01-11 14:50:10 +01006970 MBEDTLS_SSL_DEBUG_CRT(3, "own certificate", mbedtls_ssl_own_cert(ssl));
Jerry Yud9526692022-02-17 14:23:47 +08006971
6972 /*
6973 * 0 . 0 handshake type
6974 * 1 . 3 handshake length
6975 * 4 . 6 length of all certs
6976 * 7 . 9 length of cert. 1
6977 * 10 . n-1 peer certificate
6978 * n . n+2 length of cert. 2
6979 * n+3 . ... upper level cert, etc.
6980 */
6981 i = 7;
Gilles Peskine449bd832023-01-11 14:50:10 +01006982 crt = mbedtls_ssl_own_cert(ssl);
Jerry Yud9526692022-02-17 14:23:47 +08006983
Gilles Peskine449bd832023-01-11 14:50:10 +01006984 while (crt != NULL) {
Jerry Yud9526692022-02-17 14:23:47 +08006985 n = crt->raw.len;
Gilles Peskine449bd832023-01-11 14:50:10 +01006986 if (n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i) {
6987 MBEDTLS_SSL_DEBUG_MSG(1, ("certificate too large, %" MBEDTLS_PRINTF_SIZET
6988 " > %" MBEDTLS_PRINTF_SIZET,
6989 i + 3 + n, (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN));
6990 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
Jerry Yud9526692022-02-17 14:23:47 +08006991 }
6992
Gilles Peskine449bd832023-01-11 14:50:10 +01006993 ssl->out_msg[i] = MBEDTLS_BYTE_2(n);
6994 ssl->out_msg[i + 1] = MBEDTLS_BYTE_1(n);
6995 ssl->out_msg[i + 2] = MBEDTLS_BYTE_0(n);
Jerry Yud9526692022-02-17 14:23:47 +08006996
Gilles Peskine449bd832023-01-11 14:50:10 +01006997 i += 3; memcpy(ssl->out_msg + i, crt->raw.p, n);
Jerry Yud9526692022-02-17 14:23:47 +08006998 i += n; crt = crt->next;
6999 }
7000
Gilles Peskine449bd832023-01-11 14:50:10 +01007001 ssl->out_msg[4] = MBEDTLS_BYTE_2(i - 7);
7002 ssl->out_msg[5] = MBEDTLS_BYTE_1(i - 7);
7003 ssl->out_msg[6] = MBEDTLS_BYTE_0(i - 7);
Jerry Yud9526692022-02-17 14:23:47 +08007004
7005 ssl->out_msglen = i;
7006 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7007 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
7008
7009 ssl->state++;
7010
Gilles Peskine449bd832023-01-11 14:50:10 +01007011 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
7012 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
7013 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007014 }
7015
Gilles Peskine449bd832023-01-11 14:50:10 +01007016 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007017
Gilles Peskine449bd832023-01-11 14:50:10 +01007018 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007019}
7020
7021#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
7022
7023#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007024MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007025static int ssl_check_peer_crt_unchanged(mbedtls_ssl_context *ssl,
7026 unsigned char *crt_buf,
7027 size_t crt_buf_len)
Jerry Yud9526692022-02-17 14:23:47 +08007028{
7029 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
7030
Gilles Peskine449bd832023-01-11 14:50:10 +01007031 if (peer_crt == NULL) {
7032 return -1;
7033 }
Jerry Yud9526692022-02-17 14:23:47 +08007034
Gilles Peskine449bd832023-01-11 14:50:10 +01007035 if (peer_crt->raw.len != crt_buf_len) {
7036 return -1;
7037 }
Jerry Yud9526692022-02-17 14:23:47 +08007038
Gilles Peskine449bd832023-01-11 14:50:10 +01007039 return memcmp(peer_crt->raw.p, crt_buf, peer_crt->raw.len);
Jerry Yud9526692022-02-17 14:23:47 +08007040}
7041#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007042MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007043static int ssl_check_peer_crt_unchanged(mbedtls_ssl_context *ssl,
7044 unsigned char *crt_buf,
7045 size_t crt_buf_len)
Jerry Yud9526692022-02-17 14:23:47 +08007046{
7047 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7048 unsigned char const * const peer_cert_digest =
7049 ssl->session->peer_cert_digest;
7050 mbedtls_md_type_t const peer_cert_digest_type =
7051 ssl->session->peer_cert_digest_type;
7052 mbedtls_md_info_t const * const digest_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01007053 mbedtls_md_info_from_type(peer_cert_digest_type);
Jerry Yud9526692022-02-17 14:23:47 +08007054 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
7055 size_t digest_len;
7056
Gilles Peskine449bd832023-01-11 14:50:10 +01007057 if (peer_cert_digest == NULL || digest_info == NULL) {
7058 return -1;
7059 }
Jerry Yud9526692022-02-17 14:23:47 +08007060
Gilles Peskine449bd832023-01-11 14:50:10 +01007061 digest_len = mbedtls_md_get_size(digest_info);
7062 if (digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN) {
7063 return -1;
7064 }
Jerry Yud9526692022-02-17 14:23:47 +08007065
Gilles Peskine449bd832023-01-11 14:50:10 +01007066 ret = mbedtls_md(digest_info, crt_buf, crt_buf_len, tmp_digest);
7067 if (ret != 0) {
7068 return -1;
7069 }
Jerry Yud9526692022-02-17 14:23:47 +08007070
Gilles Peskine449bd832023-01-11 14:50:10 +01007071 return memcmp(tmp_digest, peer_cert_digest, digest_len);
Jerry Yud9526692022-02-17 14:23:47 +08007072}
7073#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7074#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
7075
7076/*
7077 * Once the certificate message is read, parse it into a cert chain and
7078 * perform basic checks, but leave actual verification to the caller
7079 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007080MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007081static int ssl_parse_certificate_chain(mbedtls_ssl_context *ssl,
7082 mbedtls_x509_crt *chain)
Jerry Yud9526692022-02-17 14:23:47 +08007083{
7084 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7085#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007086 int crt_cnt = 0;
Jerry Yud9526692022-02-17 14:23:47 +08007087#endif
7088 size_t i, n;
7089 uint8_t alert;
7090
Gilles Peskine449bd832023-01-11 14:50:10 +01007091 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
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_UNEXPECTED_MESSAGE);
7095 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Jerry Yud9526692022-02-17 14:23:47 +08007096 }
7097
Gilles Peskine449bd832023-01-11 14:50:10 +01007098 if (ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE) {
7099 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7100 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
7101 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Jerry Yud9526692022-02-17 14:23:47 +08007102 }
7103
Gilles Peskine449bd832023-01-11 14:50:10 +01007104 if (ssl->in_hslen < mbedtls_ssl_hs_hdr_len(ssl) + 3 + 3) {
7105 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7106 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7107 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
7108 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007109 }
7110
Gilles Peskine449bd832023-01-11 14:50:10 +01007111 i = mbedtls_ssl_hs_hdr_len(ssl);
Jerry Yud9526692022-02-17 14:23:47 +08007112
7113 /*
7114 * Same message structure as in mbedtls_ssl_write_certificate()
7115 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007116 n = (ssl->in_msg[i+1] << 8) | ssl->in_msg[i+2];
Jerry Yud9526692022-02-17 14:23:47 +08007117
Gilles Peskine449bd832023-01-11 14:50:10 +01007118 if (ssl->in_msg[i] != 0 ||
7119 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len(ssl)) {
7120 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7121 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7122 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
7123 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007124 }
7125
7126 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
7127 i += 3;
7128
7129 /* Iterate through and parse the CRTs in the provided chain. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007130 while (i < ssl->in_hslen) {
Jerry Yud9526692022-02-17 14:23:47 +08007131 /* Check that there's room for the next CRT's length fields. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007132 if (i + 3 > ssl->in_hslen) {
7133 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7134 mbedtls_ssl_send_alert_message(ssl,
7135 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7136 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
7137 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007138 }
7139 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
7140 * anything beyond 2**16 ~ 64K. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007141 if (ssl->in_msg[i] != 0) {
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_UNSUPPORTED_CERT);
7146 return MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Jerry Yud9526692022-02-17 14:23:47 +08007147 }
7148
7149 /* Read length of the next CRT in the chain. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007150 n = ((unsigned int) ssl->in_msg[i + 1] << 8)
Jerry Yud9526692022-02-17 14:23:47 +08007151 | (unsigned int) ssl->in_msg[i + 2];
7152 i += 3;
7153
Gilles Peskine449bd832023-01-11 14:50:10 +01007154 if (n < 128 || i + n > ssl->in_hslen) {
7155 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7156 mbedtls_ssl_send_alert_message(ssl,
7157 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7158 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
7159 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007160 }
7161
7162 /* Check if we're handling the first CRT in the chain. */
7163#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007164 if (crt_cnt++ == 0 &&
Jerry Yud9526692022-02-17 14:23:47 +08007165 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Gilles Peskine449bd832023-01-11 14:50:10 +01007166 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
Jerry Yud9526692022-02-17 14:23:47 +08007167 /* During client-side renegotiation, check that the server's
7168 * end-CRTs hasn't changed compared to the initial handshake,
7169 * mitigating the triple handshake attack. On success, reuse
7170 * the original end-CRT instead of parsing it again. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007171 MBEDTLS_SSL_DEBUG_MSG(3, ("Check that peer CRT hasn't changed during renegotiation"));
7172 if (ssl_check_peer_crt_unchanged(ssl,
7173 &ssl->in_msg[i],
7174 n) != 0) {
7175 MBEDTLS_SSL_DEBUG_MSG(1, ("new server cert during renegotiation"));
7176 mbedtls_ssl_send_alert_message(ssl,
7177 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7178 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED);
7179 return MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Jerry Yud9526692022-02-17 14:23:47 +08007180 }
7181
7182 /* Now we can safely free the original chain. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007183 ssl_clear_peer_cert(ssl->session);
Jerry Yud9526692022-02-17 14:23:47 +08007184 }
7185#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
7186
7187 /* Parse the next certificate in the chain. */
7188#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007189 ret = mbedtls_x509_crt_parse_der(chain, ssl->in_msg + i, n);
Jerry Yud9526692022-02-17 14:23:47 +08007190#else
7191 /* If we don't need to store the CRT chain permanently, parse
7192 * it in-place from the input buffer instead of making a copy. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007193 ret = mbedtls_x509_crt_parse_der_nocopy(chain, ssl->in_msg + i, n);
Jerry Yud9526692022-02-17 14:23:47 +08007194#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007195 switch (ret) {
Jerry Yud9526692022-02-17 14:23:47 +08007196 case 0: /*ok*/
7197 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
7198 /* Ignore certificate with an unknown algorithm: maybe a
7199 prior certificate was already trusted. */
7200 break;
7201
7202 case MBEDTLS_ERR_X509_ALLOC_FAILED:
7203 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
7204 goto crt_parse_der_failed;
7205
7206 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
7207 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7208 goto crt_parse_der_failed;
7209
7210 default:
7211 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007212crt_parse_der_failed:
7213 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert);
7214 MBEDTLS_SSL_DEBUG_RET(1, " mbedtls_x509_crt_parse_der", ret);
7215 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007216 }
7217
7218 i += n;
7219 }
7220
Gilles Peskine449bd832023-01-11 14:50:10 +01007221 MBEDTLS_SSL_DEBUG_CRT(3, "peer certificate", chain);
7222 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08007223}
7224
7225#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007226MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007227static int ssl_srv_check_client_no_crt_notification(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08007228{
Gilles Peskine449bd832023-01-11 14:50:10 +01007229 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
7230 return -1;
7231 }
Jerry Yud9526692022-02-17 14:23:47 +08007232
Gilles Peskine449bd832023-01-11 14:50:10 +01007233 if (ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len(ssl) &&
Jerry Yud9526692022-02-17 14:23:47 +08007234 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
7235 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
Gilles Peskine449bd832023-01-11 14:50:10 +01007236 memcmp(ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl), "\0\0\0", 3) == 0) {
7237 MBEDTLS_SSL_DEBUG_MSG(1, ("peer has no certificate"));
7238 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08007239 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007240 return -1;
Jerry Yud9526692022-02-17 14:23:47 +08007241}
7242#endif /* MBEDTLS_SSL_SRV_C */
7243
7244/* Check if a certificate message is expected.
7245 * Return either
7246 * - SSL_CERTIFICATE_EXPECTED, or
7247 * - SSL_CERTIFICATE_SKIP
7248 * indicating whether a Certificate message is expected or not.
7249 */
7250#define SSL_CERTIFICATE_EXPECTED 0
7251#define SSL_CERTIFICATE_SKIP 1
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007252MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007253static int ssl_parse_certificate_coordinate(mbedtls_ssl_context *ssl,
7254 int authmode)
Jerry Yud9526692022-02-17 14:23:47 +08007255{
7256 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
7257 ssl->handshake->ciphersuite_info;
7258
Gilles Peskine449bd832023-01-11 14:50:10 +01007259 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
7260 return SSL_CERTIFICATE_SKIP;
7261 }
Jerry Yud9526692022-02-17 14:23:47 +08007262
7263#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007264 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
7265 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK) {
7266 return SSL_CERTIFICATE_SKIP;
7267 }
Jerry Yud9526692022-02-17 14:23:47 +08007268
Gilles Peskine449bd832023-01-11 14:50:10 +01007269 if (authmode == MBEDTLS_SSL_VERIFY_NONE) {
Jerry Yud9526692022-02-17 14:23:47 +08007270 ssl->session_negotiate->verify_result =
7271 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
Gilles Peskine449bd832023-01-11 14:50:10 +01007272 return SSL_CERTIFICATE_SKIP;
Jerry Yud9526692022-02-17 14:23:47 +08007273 }
7274 }
7275#else
7276 ((void) authmode);
7277#endif /* MBEDTLS_SSL_SRV_C */
7278
Gilles Peskine449bd832023-01-11 14:50:10 +01007279 return SSL_CERTIFICATE_EXPECTED;
Jerry Yud9526692022-02-17 14:23:47 +08007280}
7281
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007282MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007283static int ssl_parse_certificate_verify(mbedtls_ssl_context *ssl,
7284 int authmode,
7285 mbedtls_x509_crt *chain,
7286 void *rs_ctx)
Jerry Yud9526692022-02-17 14:23:47 +08007287{
7288 int ret = 0;
7289 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
7290 ssl->handshake->ciphersuite_info;
7291 int have_ca_chain = 0;
7292
7293 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
7294 void *p_vrfy;
7295
Gilles Peskine449bd832023-01-11 14:50:10 +01007296 if (authmode == MBEDTLS_SSL_VERIFY_NONE) {
7297 return 0;
7298 }
Jerry Yud9526692022-02-17 14:23:47 +08007299
Gilles Peskine449bd832023-01-11 14:50:10 +01007300 if (ssl->f_vrfy != NULL) {
7301 MBEDTLS_SSL_DEBUG_MSG(3, ("Use context-specific verification callback"));
Jerry Yud9526692022-02-17 14:23:47 +08007302 f_vrfy = ssl->f_vrfy;
7303 p_vrfy = ssl->p_vrfy;
Gilles Peskine449bd832023-01-11 14:50:10 +01007304 } else {
7305 MBEDTLS_SSL_DEBUG_MSG(3, ("Use configuration-specific verification callback"));
Jerry Yud9526692022-02-17 14:23:47 +08007306 f_vrfy = ssl->conf->f_vrfy;
7307 p_vrfy = ssl->conf->p_vrfy;
7308 }
7309
7310 /*
7311 * Main check: verify certificate
7312 */
7313#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Gilles Peskine449bd832023-01-11 14:50:10 +01007314 if (ssl->conf->f_ca_cb != NULL) {
Jerry Yud9526692022-02-17 14:23:47 +08007315 ((void) rs_ctx);
7316 have_ca_chain = 1;
7317
Gilles Peskine449bd832023-01-11 14:50:10 +01007318 MBEDTLS_SSL_DEBUG_MSG(3, ("use CA callback for X.509 CRT verification"));
Jerry Yud9526692022-02-17 14:23:47 +08007319 ret = mbedtls_x509_crt_verify_with_ca_cb(
7320 chain,
7321 ssl->conf->f_ca_cb,
7322 ssl->conf->p_ca_cb,
7323 ssl->conf->cert_profile,
7324 ssl->hostname,
7325 &ssl->session_negotiate->verify_result,
Gilles Peskine449bd832023-01-11 14:50:10 +01007326 f_vrfy, p_vrfy);
7327 } else
Jerry Yud9526692022-02-17 14:23:47 +08007328#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
7329 {
7330 mbedtls_x509_crt *ca_chain;
7331 mbedtls_x509_crl *ca_crl;
7332
7333#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01007334 if (ssl->handshake->sni_ca_chain != NULL) {
Jerry Yud9526692022-02-17 14:23:47 +08007335 ca_chain = ssl->handshake->sni_ca_chain;
7336 ca_crl = ssl->handshake->sni_ca_crl;
Gilles Peskine449bd832023-01-11 14:50:10 +01007337 } else
Jerry Yud9526692022-02-17 14:23:47 +08007338#endif
7339 {
7340 ca_chain = ssl->conf->ca_chain;
7341 ca_crl = ssl->conf->ca_crl;
7342 }
7343
Gilles Peskine449bd832023-01-11 14:50:10 +01007344 if (ca_chain != NULL) {
Jerry Yud9526692022-02-17 14:23:47 +08007345 have_ca_chain = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01007346 }
Jerry Yud9526692022-02-17 14:23:47 +08007347
7348 ret = mbedtls_x509_crt_verify_restartable(
7349 chain,
7350 ca_chain, ca_crl,
7351 ssl->conf->cert_profile,
7352 ssl->hostname,
7353 &ssl->session_negotiate->verify_result,
Gilles Peskine449bd832023-01-11 14:50:10 +01007354 f_vrfy, p_vrfy, rs_ctx);
Jerry Yud9526692022-02-17 14:23:47 +08007355 }
7356
Gilles Peskine449bd832023-01-11 14:50:10 +01007357 if (ret != 0) {
7358 MBEDTLS_SSL_DEBUG_RET(1, "x509_verify_cert", ret);
Jerry Yud9526692022-02-17 14:23:47 +08007359 }
7360
7361#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007362 if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
7363 return MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
7364 }
Jerry Yud9526692022-02-17 14:23:47 +08007365#endif
7366
7367 /*
7368 * Secondary checks: always done, but change 'ret' only if it was 0
7369 */
7370
7371#if defined(MBEDTLS_ECP_C)
7372 {
7373 const mbedtls_pk_context *pk = &chain->pk;
7374
Manuel Pégourié-Gonnard66b0d612022-06-17 10:49:29 +02007375 /* If certificate uses an EC key, make sure the curve is OK.
7376 * This is a public key, so it can't be opaque, so can_do() is a good
7377 * enough check to ensure pk_ec() is safe to use here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007378 if (mbedtls_pk_can_do(pk, MBEDTLS_PK_ECKEY)) {
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007379 /* and in the unlikely case the above assumption no longer holds
7380 * we are making sure that pk_ec() here does not return a NULL
7381 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007382 const mbedtls_ecp_keypair *ec = mbedtls_pk_ec(*pk);
7383 if (ec == NULL) {
7384 MBEDTLS_SSL_DEBUG_MSG(1, ("mbedtls_pk_ec() returned NULL"));
7385 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007386 }
Jerry Yud9526692022-02-17 14:23:47 +08007387
Gilles Peskine449bd832023-01-11 14:50:10 +01007388 if (mbedtls_ssl_check_curve(ssl, ec->grp.id) != 0) {
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007389 ssl->session_negotiate->verify_result |=
7390 MBEDTLS_X509_BADCERT_BAD_KEY;
7391
Gilles Peskine449bd832023-01-11 14:50:10 +01007392 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate (EC key curve)"));
7393 if (ret == 0) {
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007394 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Gilles Peskine449bd832023-01-11 14:50:10 +01007395 }
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007396 }
Jerry Yud9526692022-02-17 14:23:47 +08007397 }
7398 }
7399#endif /* MBEDTLS_ECP_C */
7400
Gilles Peskine449bd832023-01-11 14:50:10 +01007401 if (mbedtls_ssl_check_cert_usage(chain,
7402 ciphersuite_info,
7403 !ssl->conf->endpoint,
7404 &ssl->session_negotiate->verify_result) != 0) {
7405 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate (usage extensions)"));
7406 if (ret == 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007407 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Gilles Peskine449bd832023-01-11 14:50:10 +01007408 }
Jerry Yud9526692022-02-17 14:23:47 +08007409 }
7410
7411 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
7412 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
7413 * with details encoded in the verification flags. All other kinds
7414 * of error codes, including those from the user provided f_vrfy
7415 * functions, are treated as fatal and lead to a failure of
7416 * ssl_parse_certificate even if verification was optional. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007417 if (authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
7418 (ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
7419 ret == MBEDTLS_ERR_SSL_BAD_CERTIFICATE)) {
Jerry Yud9526692022-02-17 14:23:47 +08007420 ret = 0;
7421 }
7422
Gilles Peskine449bd832023-01-11 14:50:10 +01007423 if (have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED) {
7424 MBEDTLS_SSL_DEBUG_MSG(1, ("got no CA chain"));
Jerry Yud9526692022-02-17 14:23:47 +08007425 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
7426 }
7427
Gilles Peskine449bd832023-01-11 14:50:10 +01007428 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007429 uint8_t alert;
7430
7431 /* The certificate may have been rejected for several reasons.
7432 Pick one and send the corresponding alert. Which alert to send
7433 may be a subject of debate in some cases. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007434 if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER) {
Jerry Yud9526692022-02-17 14:23:47 +08007435 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
Gilles Peskine449bd832023-01-11 14:50:10 +01007436 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH) {
Jerry Yud9526692022-02-17 14:23:47 +08007437 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007438 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE) {
Jerry Yud9526692022-02-17 14:23:47 +08007439 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007440 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE) {
Jerry Yud9526692022-02-17 14:23:47 +08007441 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007442 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE) {
Jerry Yud9526692022-02-17 14:23:47 +08007443 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007444 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK) {
Jerry Yud9526692022-02-17 14:23:47 +08007445 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007446 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY) {
Jerry Yud9526692022-02-17 14:23:47 +08007447 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007448 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED) {
Jerry Yud9526692022-02-17 14:23:47 +08007449 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
Gilles Peskine449bd832023-01-11 14:50:10 +01007450 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED) {
Jerry Yud9526692022-02-17 14:23:47 +08007451 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
Gilles Peskine449bd832023-01-11 14:50:10 +01007452 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED) {
Jerry Yud9526692022-02-17 14:23:47 +08007453 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
Gilles Peskine449bd832023-01-11 14:50:10 +01007454 } else {
Jerry Yud9526692022-02-17 14:23:47 +08007455 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
Gilles Peskine449bd832023-01-11 14:50:10 +01007456 }
7457 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7458 alert);
Jerry Yud9526692022-02-17 14:23:47 +08007459 }
7460
7461#if defined(MBEDTLS_DEBUG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007462 if (ssl->session_negotiate->verify_result != 0) {
7463 MBEDTLS_SSL_DEBUG_MSG(3, ("! Certificate verification flags %08x",
7464 (unsigned int) ssl->session_negotiate->verify_result));
7465 } else {
7466 MBEDTLS_SSL_DEBUG_MSG(3, ("Certificate verification flags clear"));
Jerry Yud9526692022-02-17 14:23:47 +08007467 }
7468#endif /* MBEDTLS_DEBUG_C */
7469
Gilles Peskine449bd832023-01-11 14:50:10 +01007470 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007471}
7472
7473#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007474MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007475static int ssl_remember_peer_crt_digest(mbedtls_ssl_context *ssl,
7476 unsigned char *start, size_t len)
Jerry Yud9526692022-02-17 14:23:47 +08007477{
7478 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7479 /* Remember digest of the peer's end-CRT. */
7480 ssl->session_negotiate->peer_cert_digest =
Gilles Peskine449bd832023-01-11 14:50:10 +01007481 mbedtls_calloc(1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN);
7482 if (ssl->session_negotiate->peer_cert_digest == NULL) {
7483 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%d bytes) failed",
7484 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN));
7485 mbedtls_ssl_send_alert_message(ssl,
7486 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7487 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
Jerry Yud9526692022-02-17 14:23:47 +08007488
Gilles Peskine449bd832023-01-11 14:50:10 +01007489 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Jerry Yud9526692022-02-17 14:23:47 +08007490 }
7491
Gilles Peskine449bd832023-01-11 14:50:10 +01007492 ret = mbedtls_md(mbedtls_md_info_from_type(
7493 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE),
7494 start, len,
7495 ssl->session_negotiate->peer_cert_digest);
Jerry Yud9526692022-02-17 14:23:47 +08007496
7497 ssl->session_negotiate->peer_cert_digest_type =
7498 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
7499 ssl->session_negotiate->peer_cert_digest_len =
7500 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
7501
Gilles Peskine449bd832023-01-11 14:50:10 +01007502 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007503}
7504
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007505MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007506static int ssl_remember_peer_pubkey(mbedtls_ssl_context *ssl,
7507 unsigned char *start, size_t len)
Jerry Yud9526692022-02-17 14:23:47 +08007508{
7509 unsigned char *end = start + len;
7510 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7511
7512 /* Make a copy of the peer's raw public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007513 mbedtls_pk_init(&ssl->handshake->peer_pubkey);
7514 ret = mbedtls_pk_parse_subpubkey(&start, end,
7515 &ssl->handshake->peer_pubkey);
7516 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007517 /* We should have parsed the public key before. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007518 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007519 }
7520
Gilles Peskine449bd832023-01-11 14:50:10 +01007521 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08007522}
7523#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7524
Gilles Peskine449bd832023-01-11 14:50:10 +01007525int mbedtls_ssl_parse_certificate(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08007526{
7527 int ret = 0;
7528 int crt_expected;
7529#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7530 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
7531 ? ssl->handshake->sni_authmode
7532 : ssl->conf->authmode;
7533#else
7534 const int authmode = ssl->conf->authmode;
7535#endif
7536 void *rs_ctx = NULL;
7537 mbedtls_x509_crt *chain = NULL;
7538
Gilles Peskine449bd832023-01-11 14:50:10 +01007539 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007540
Gilles Peskine449bd832023-01-11 14:50:10 +01007541 crt_expected = ssl_parse_certificate_coordinate(ssl, authmode);
7542 if (crt_expected == SSL_CERTIFICATE_SKIP) {
7543 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007544 goto exit;
7545 }
7546
7547#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007548 if (ssl->handshake->ecrs_enabled &&
7549 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify) {
Jerry Yud9526692022-02-17 14:23:47 +08007550 chain = ssl->handshake->ecrs_peer_cert;
7551 ssl->handshake->ecrs_peer_cert = NULL;
7552 goto crt_verify;
7553 }
7554#endif
7555
Gilles Peskine449bd832023-01-11 14:50:10 +01007556 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007557 /* mbedtls_ssl_read_record may have sent an alert already. We
7558 let it decide whether to alert. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007559 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
Jerry Yud9526692022-02-17 14:23:47 +08007560 goto exit;
7561 }
7562
7563#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007564 if (ssl_srv_check_client_no_crt_notification(ssl) == 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007565 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
7566
Gilles Peskine449bd832023-01-11 14:50:10 +01007567 if (authmode != MBEDTLS_SSL_VERIFY_OPTIONAL) {
Jerry Yud9526692022-02-17 14:23:47 +08007568 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Gilles Peskine449bd832023-01-11 14:50:10 +01007569 }
Jerry Yud9526692022-02-17 14:23:47 +08007570
7571 goto exit;
7572 }
7573#endif /* MBEDTLS_SSL_SRV_C */
7574
7575 /* Clear existing peer CRT structure in case we tried to
7576 * reuse a session but it failed, and allocate a new one. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007577 ssl_clear_peer_cert(ssl->session_negotiate);
Jerry Yud9526692022-02-17 14:23:47 +08007578
Gilles Peskine449bd832023-01-11 14:50:10 +01007579 chain = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
7580 if (chain == NULL) {
7581 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed",
7582 sizeof(mbedtls_x509_crt)));
7583 mbedtls_ssl_send_alert_message(ssl,
7584 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7585 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
Jerry Yud9526692022-02-17 14:23:47 +08007586
7587 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
7588 goto exit;
7589 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007590 mbedtls_x509_crt_init(chain);
Jerry Yud9526692022-02-17 14:23:47 +08007591
Gilles Peskine449bd832023-01-11 14:50:10 +01007592 ret = ssl_parse_certificate_chain(ssl, chain);
7593 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007594 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007595 }
Jerry Yud9526692022-02-17 14:23:47 +08007596
7597#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007598 if (ssl->handshake->ecrs_enabled) {
Jerry Yud9526692022-02-17 14:23:47 +08007599 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Gilles Peskine449bd832023-01-11 14:50:10 +01007600 }
Jerry Yud9526692022-02-17 14:23:47 +08007601
7602crt_verify:
Gilles Peskine449bd832023-01-11 14:50:10 +01007603 if (ssl->handshake->ecrs_enabled) {
Jerry Yud9526692022-02-17 14:23:47 +08007604 rs_ctx = &ssl->handshake->ecrs_ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +01007605 }
Jerry Yud9526692022-02-17 14:23:47 +08007606#endif
7607
Gilles Peskine449bd832023-01-11 14:50:10 +01007608 ret = ssl_parse_certificate_verify(ssl, authmode,
7609 chain, rs_ctx);
7610 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007611 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007612 }
Jerry Yud9526692022-02-17 14:23:47 +08007613
7614#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7615 {
7616 unsigned char *crt_start, *pk_start;
7617 size_t crt_len, pk_len;
7618
7619 /* We parse the CRT chain without copying, so
7620 * these pointers point into the input buffer,
7621 * and are hence still valid after freeing the
7622 * CRT chain. */
7623
7624 crt_start = chain->raw.p;
7625 crt_len = chain->raw.len;
7626
7627 pk_start = chain->pk_raw.p;
7628 pk_len = chain->pk_raw.len;
7629
7630 /* Free the CRT structures before computing
7631 * digest and copying the peer's public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007632 mbedtls_x509_crt_free(chain);
7633 mbedtls_free(chain);
Jerry Yud9526692022-02-17 14:23:47 +08007634 chain = NULL;
7635
Gilles Peskine449bd832023-01-11 14:50:10 +01007636 ret = ssl_remember_peer_crt_digest(ssl, crt_start, crt_len);
7637 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007638 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007639 }
Jerry Yud9526692022-02-17 14:23:47 +08007640
Gilles Peskine449bd832023-01-11 14:50:10 +01007641 ret = ssl_remember_peer_pubkey(ssl, pk_start, pk_len);
7642 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007643 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007644 }
Jerry Yud9526692022-02-17 14:23:47 +08007645 }
7646#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7647 /* Pass ownership to session structure. */
7648 ssl->session_negotiate->peer_cert = chain;
7649 chain = NULL;
7650#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7651
Gilles Peskine449bd832023-01-11 14:50:10 +01007652 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007653
7654exit:
7655
Gilles Peskine449bd832023-01-11 14:50:10 +01007656 if (ret == 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007657 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01007658 }
Jerry Yud9526692022-02-17 14:23:47 +08007659
7660#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007661 if (ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS) {
Jerry Yud9526692022-02-17 14:23:47 +08007662 ssl->handshake->ecrs_peer_cert = chain;
7663 chain = NULL;
7664 }
7665#endif
7666
Gilles Peskine449bd832023-01-11 14:50:10 +01007667 if (chain != NULL) {
7668 mbedtls_x509_crt_free(chain);
7669 mbedtls_free(chain);
Jerry Yud9526692022-02-17 14:23:47 +08007670 }
7671
Gilles Peskine449bd832023-01-11 14:50:10 +01007672 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007673}
7674#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
7675
Andrzej Kurek25f27152022-08-17 16:09:31 -04007676#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01007677static int ssl_calc_finished_tls_sha256(
Gilles Peskine449bd832023-01-11 14:50:10 +01007678 mbedtls_ssl_context *ssl, unsigned char *buf, int from)
Jerry Yu615bd6f2022-02-17 14:25:15 +08007679{
7680 int len = 12;
7681 const char *sender;
7682 unsigned char padbuf[32];
7683#if defined(MBEDTLS_USE_PSA_CRYPTO)
7684 size_t hash_size;
7685 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
7686 psa_status_t status;
7687#else
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007688 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01007689 mbedtls_md_context_t sha256;
Jerry Yu615bd6f2022-02-17 14:25:15 +08007690#endif
7691
7692 mbedtls_ssl_session *session = ssl->session_negotiate;
Gilles Peskine449bd832023-01-11 14:50:10 +01007693 if (!session) {
Jerry Yu615bd6f2022-02-17 14:25:15 +08007694 session = ssl->session;
Gilles Peskine449bd832023-01-11 14:50:10 +01007695 }
Jerry Yu615bd6f2022-02-17 14:25:15 +08007696
Gilles Peskine449bd832023-01-11 14:50:10 +01007697 sender = (from == MBEDTLS_SSL_IS_CLIENT)
Jerry Yu615bd6f2022-02-17 14:25:15 +08007698 ? "client finished"
7699 : "server finished";
7700
7701#if defined(MBEDTLS_USE_PSA_CRYPTO)
7702 sha256_psa = psa_hash_operation_init();
7703
Gilles Peskine449bd832023-01-11 14:50:10 +01007704 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc PSA finished tls sha256"));
Jerry Yu615bd6f2022-02-17 14:25:15 +08007705
Gilles Peskine449bd832023-01-11 14:50:10 +01007706 status = psa_hash_clone(&ssl->handshake->fin_sha256_psa, &sha256_psa);
7707 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007708 goto exit;
Jerry Yu615bd6f2022-02-17 14:25:15 +08007709 }
7710
Gilles Peskine449bd832023-01-11 14:50:10 +01007711 status = psa_hash_finish(&sha256_psa, padbuf, sizeof(padbuf), &hash_size);
7712 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007713 goto exit;
Jerry Yu615bd6f2022-02-17 14:25:15 +08007714 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007715 MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated padbuf", padbuf, 32);
Jerry Yu615bd6f2022-02-17 14:25:15 +08007716#else
7717
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01007718 mbedtls_md_init(&sha256);
Jerry Yu615bd6f2022-02-17 14:25:15 +08007719
Gilles Peskine449bd832023-01-11 14:50:10 +01007720 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc finished tls sha256"));
Jerry Yu615bd6f2022-02-17 14:25:15 +08007721
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01007722 ret = mbedtls_md_setup(&sha256, mbedtls_md_info_from_type(MBEDTLS_MD_SHA256), 0);
7723 if (ret != 0) {
7724 goto exit;
7725 }
7726 ret = mbedtls_md_clone(&sha256, &ssl->handshake->fin_sha256);
7727 if (ret != 0) {
7728 goto exit;
7729 }
Jerry Yu615bd6f2022-02-17 14:25:15 +08007730
7731 /*
7732 * TLSv1.2:
7733 * hash = PRF( master, finished_label,
7734 * Hash( handshake ) )[0.11]
7735 */
7736
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01007737 ret = mbedtls_md_finish(&sha256, padbuf);
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007738 if (ret != 0) {
7739 goto exit;
7740 }
Jerry Yu615bd6f2022-02-17 14:25:15 +08007741#endif /* MBEDTLS_USE_PSA_CRYPTO */
7742
Manuel Pégourié-Gonnard0ac71c02023-02-24 12:13:55 +01007743 MBEDTLS_SSL_DEBUG_BUF(4, "finished sha256 output", padbuf, 32);
7744
Gilles Peskine449bd832023-01-11 14:50:10 +01007745 ssl->handshake->tls_prf(session->master, 48, sender,
7746 padbuf, 32, buf, len);
Jerry Yu615bd6f2022-02-17 14:25:15 +08007747
Gilles Peskine449bd832023-01-11 14:50:10 +01007748 MBEDTLS_SSL_DEBUG_BUF(3, "calc finished result", buf, len);
Jerry Yu615bd6f2022-02-17 14:25:15 +08007749
Gilles Peskine449bd832023-01-11 14:50:10 +01007750 mbedtls_platform_zeroize(padbuf, sizeof(padbuf));
Jerry Yu615bd6f2022-02-17 14:25:15 +08007751
Gilles Peskine449bd832023-01-11 14:50:10 +01007752 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc finished"));
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007753
7754exit:
7755#if defined(MBEDTLS_USE_PSA_CRYPTO)
7756 psa_hash_abort(&sha256_psa);
Andrzej Kurekdaf5b562023-03-03 05:52:28 -05007757 return PSA_TO_MD_ERR(status);
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007758#else
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01007759 mbedtls_md_free(&sha256);
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007760 return ret;
7761#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu615bd6f2022-02-17 14:25:15 +08007762}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04007763#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yu615bd6f2022-02-17 14:25:15 +08007764
Jerry Yub7ba49e2022-02-17 14:25:53 +08007765
Andrzej Kurek25f27152022-08-17 16:09:31 -04007766#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01007767static int ssl_calc_finished_tls_sha384(
Gilles Peskine449bd832023-01-11 14:50:10 +01007768 mbedtls_ssl_context *ssl, unsigned char *buf, int from)
Jerry Yub7ba49e2022-02-17 14:25:53 +08007769{
7770 int len = 12;
7771 const char *sender;
7772 unsigned char padbuf[48];
7773#if defined(MBEDTLS_USE_PSA_CRYPTO)
7774 size_t hash_size;
7775 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
7776 psa_status_t status;
7777#else
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007778 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01007779 mbedtls_md_context_t sha384;
Jerry Yub7ba49e2022-02-17 14:25:53 +08007780#endif
7781
7782 mbedtls_ssl_session *session = ssl->session_negotiate;
Gilles Peskine449bd832023-01-11 14:50:10 +01007783 if (!session) {
Jerry Yub7ba49e2022-02-17 14:25:53 +08007784 session = ssl->session;
Gilles Peskine449bd832023-01-11 14:50:10 +01007785 }
Jerry Yub7ba49e2022-02-17 14:25:53 +08007786
Gilles Peskine449bd832023-01-11 14:50:10 +01007787 sender = (from == MBEDTLS_SSL_IS_CLIENT)
Jerry Yub7ba49e2022-02-17 14:25:53 +08007788 ? "client finished"
7789 : "server finished";
7790
7791#if defined(MBEDTLS_USE_PSA_CRYPTO)
7792 sha384_psa = psa_hash_operation_init();
7793
Gilles Peskine449bd832023-01-11 14:50:10 +01007794 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc PSA finished tls sha384"));
Jerry Yub7ba49e2022-02-17 14:25:53 +08007795
Gilles Peskine449bd832023-01-11 14:50:10 +01007796 status = psa_hash_clone(&ssl->handshake->fin_sha384_psa, &sha384_psa);
7797 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007798 goto exit;
Jerry Yub7ba49e2022-02-17 14:25:53 +08007799 }
7800
Gilles Peskine449bd832023-01-11 14:50:10 +01007801 status = psa_hash_finish(&sha384_psa, padbuf, sizeof(padbuf), &hash_size);
7802 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007803 goto exit;
Jerry Yub7ba49e2022-02-17 14:25:53 +08007804 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007805 MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated padbuf", padbuf, 48);
Jerry Yub7ba49e2022-02-17 14:25:53 +08007806#else
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01007807 mbedtls_md_init(&sha384);
Jerry Yub7ba49e2022-02-17 14:25:53 +08007808
Gilles Peskine449bd832023-01-11 14:50:10 +01007809 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc finished tls sha384"));
Jerry Yub7ba49e2022-02-17 14:25:53 +08007810
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01007811 ret = mbedtls_md_setup(&sha384, mbedtls_md_info_from_type(MBEDTLS_MD_SHA384), 0);
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01007812 if (ret != 0) {
7813 goto exit;
7814 }
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01007815 ret = mbedtls_md_clone(&sha384, &ssl->handshake->fin_sha384);
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01007816 if (ret != 0) {
7817 goto exit;
7818 }
Jerry Yub7ba49e2022-02-17 14:25:53 +08007819
7820 /*
7821 * TLSv1.2:
7822 * hash = PRF( master, finished_label,
7823 * Hash( handshake ) )[0.11]
7824 */
7825
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01007826 ret = mbedtls_md_finish(&sha384, padbuf);
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007827 if (ret != 0) {
7828 goto exit;
7829 }
Jerry Yub7ba49e2022-02-17 14:25:53 +08007830#endif
7831
Manuel Pégourié-Gonnard0ac71c02023-02-24 12:13:55 +01007832 MBEDTLS_SSL_DEBUG_BUF(4, "finished sha384 output", padbuf, 48);
7833
Gilles Peskine449bd832023-01-11 14:50:10 +01007834 ssl->handshake->tls_prf(session->master, 48, sender,
7835 padbuf, 48, buf, len);
Jerry Yub7ba49e2022-02-17 14:25:53 +08007836
Gilles Peskine449bd832023-01-11 14:50:10 +01007837 MBEDTLS_SSL_DEBUG_BUF(3, "calc finished result", buf, len);
Jerry Yub7ba49e2022-02-17 14:25:53 +08007838
Gilles Peskine449bd832023-01-11 14:50:10 +01007839 mbedtls_platform_zeroize(padbuf, sizeof(padbuf));
Jerry Yub7ba49e2022-02-17 14:25:53 +08007840
Gilles Peskine449bd832023-01-11 14:50:10 +01007841 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc finished"));
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007842
7843exit:
7844#if defined(MBEDTLS_USE_PSA_CRYPTO)
7845 psa_hash_abort(&sha384_psa);
Andrzej Kurekdaf5b562023-03-03 05:52:28 -05007846 return PSA_TO_MD_ERR(status);
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007847#else
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01007848 mbedtls_md_free(&sha384);
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007849 return ret;
7850#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yub7ba49e2022-02-17 14:25:53 +08007851}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04007852#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yub7ba49e2022-02-17 14:25:53 +08007853
Gilles Peskine449bd832023-01-11 14:50:10 +01007854void mbedtls_ssl_handshake_wrapup_free_hs_transform(mbedtls_ssl_context *ssl)
Jerry Yuaef00152022-02-17 14:27:31 +08007855{
Gilles Peskine449bd832023-01-11 14:50:10 +01007856 MBEDTLS_SSL_DEBUG_MSG(3, ("=> handshake wrapup: final free"));
Jerry Yuaef00152022-02-17 14:27:31 +08007857
7858 /*
7859 * Free our handshake params
7860 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007861 mbedtls_ssl_handshake_free(ssl);
7862 mbedtls_free(ssl->handshake);
Jerry Yuaef00152022-02-17 14:27:31 +08007863 ssl->handshake = NULL;
7864
7865 /*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08007866 * Free the previous transform and switch in the current one
Jerry Yuaef00152022-02-17 14:27:31 +08007867 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007868 if (ssl->transform) {
7869 mbedtls_ssl_transform_free(ssl->transform);
7870 mbedtls_free(ssl->transform);
Jerry Yuaef00152022-02-17 14:27:31 +08007871 }
7872 ssl->transform = ssl->transform_negotiate;
7873 ssl->transform_negotiate = NULL;
7874
Gilles Peskine449bd832023-01-11 14:50:10 +01007875 MBEDTLS_SSL_DEBUG_MSG(3, ("<= handshake wrapup: final free"));
Jerry Yuaef00152022-02-17 14:27:31 +08007876}
7877
Gilles Peskine449bd832023-01-11 14:50:10 +01007878void mbedtls_ssl_handshake_wrapup(mbedtls_ssl_context *ssl)
Jerry Yu2a9fff52022-02-17 14:28:51 +08007879{
7880 int resume = ssl->handshake->resume;
7881
Gilles Peskine449bd832023-01-11 14:50:10 +01007882 MBEDTLS_SSL_DEBUG_MSG(3, ("=> handshake wrapup"));
Jerry Yu2a9fff52022-02-17 14:28:51 +08007883
7884#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01007885 if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
Jerry Yu2a9fff52022-02-17 14:28:51 +08007886 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
7887 ssl->renego_records_seen = 0;
7888 }
7889#endif
7890
7891 /*
7892 * Free the previous session and switch in the current one
7893 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007894 if (ssl->session) {
Jerry Yu2a9fff52022-02-17 14:28:51 +08007895#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
7896 /* RFC 7366 3.1: keep the EtM state */
7897 ssl->session_negotiate->encrypt_then_mac =
Gilles Peskine449bd832023-01-11 14:50:10 +01007898 ssl->session->encrypt_then_mac;
Jerry Yu2a9fff52022-02-17 14:28:51 +08007899#endif
7900
Gilles Peskine449bd832023-01-11 14:50:10 +01007901 mbedtls_ssl_session_free(ssl->session);
7902 mbedtls_free(ssl->session);
Jerry Yu2a9fff52022-02-17 14:28:51 +08007903 }
7904 ssl->session = ssl->session_negotiate;
7905 ssl->session_negotiate = NULL;
7906
7907 /*
7908 * Add cache entry
7909 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007910 if (ssl->conf->f_set_cache != NULL &&
Jerry Yu2a9fff52022-02-17 14:28:51 +08007911 ssl->session->id_len != 0 &&
Gilles Peskine449bd832023-01-11 14:50:10 +01007912 resume == 0) {
7913 if (ssl->conf->f_set_cache(ssl->conf->p_cache,
7914 ssl->session->id,
7915 ssl->session->id_len,
7916 ssl->session) != 0) {
7917 MBEDTLS_SSL_DEBUG_MSG(1, ("cache did not store session"));
7918 }
Jerry Yu2a9fff52022-02-17 14:28:51 +08007919 }
7920
7921#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007922 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7923 ssl->handshake->flight != NULL) {
Jerry Yu2a9fff52022-02-17 14:28:51 +08007924 /* Cancel handshake timer */
Gilles Peskine449bd832023-01-11 14:50:10 +01007925 mbedtls_ssl_set_timer(ssl, 0);
Jerry Yu2a9fff52022-02-17 14:28:51 +08007926
7927 /* Keep last flight around in case we need to resend it:
7928 * we need the handshake and transform structures for that */
Gilles Peskine449bd832023-01-11 14:50:10 +01007929 MBEDTLS_SSL_DEBUG_MSG(3, ("skip freeing handshake and transform"));
7930 } else
Jerry Yu2a9fff52022-02-17 14:28:51 +08007931#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01007932 mbedtls_ssl_handshake_wrapup_free_hs_transform(ssl);
Jerry Yu2a9fff52022-02-17 14:28:51 +08007933
Jerry Yu5ed73ff2022-10-27 13:08:42 +08007934 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
Jerry Yu2a9fff52022-02-17 14:28:51 +08007935
Gilles Peskine449bd832023-01-11 14:50:10 +01007936 MBEDTLS_SSL_DEBUG_MSG(3, ("<= handshake wrapup"));
Jerry Yu2a9fff52022-02-17 14:28:51 +08007937}
7938
Gilles Peskine449bd832023-01-11 14:50:10 +01007939int mbedtls_ssl_write_finished(mbedtls_ssl_context *ssl)
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007940{
7941 int ret, hash_len;
7942
Gilles Peskine449bd832023-01-11 14:50:10 +01007943 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write finished"));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007944
Gilles Peskine449bd832023-01-11 14:50:10 +01007945 mbedtls_ssl_update_out_pointers(ssl, ssl->transform_negotiate);
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007946
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01007947 ret = ssl->handshake->calc_finished(ssl, ssl->out_msg + 4, ssl->conf->endpoint);
7948 if (ret != 0) {
7949 MBEDTLS_SSL_DEBUG_RET(1, "calc_finished", ret);
7950 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007951
7952 /*
7953 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7954 * may define some other value. Currently (early 2016), no defined
7955 * ciphersuite does this (and this is unlikely to change as activity has
7956 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7957 */
7958 hash_len = 12;
7959
7960#if defined(MBEDTLS_SSL_RENEGOTIATION)
7961 ssl->verify_data_len = hash_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01007962 memcpy(ssl->own_verify_data, ssl->out_msg + 4, hash_len);
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007963#endif
7964
7965 ssl->out_msglen = 4 + hash_len;
7966 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7967 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
7968
7969 /*
7970 * In case of session resuming, invert the client and server
7971 * ChangeCipherSpec messages order.
7972 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007973 if (ssl->handshake->resume != 0) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007974#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007975 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007976 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Gilles Peskine449bd832023-01-11 14:50:10 +01007977 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007978#endif
7979#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007980 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007981 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Gilles Peskine449bd832023-01-11 14:50:10 +01007982 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007983#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01007984 } else {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007985 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01007986 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007987
7988 /*
7989 * Switch to our negotiated transform and session parameters for outbound
7990 * data.
7991 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007992 MBEDTLS_SSL_DEBUG_MSG(3, ("switching to new transform spec for outbound data"));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007993
7994#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007995 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007996 unsigned char i;
7997
7998 /* Remember current epoch settings for resending */
7999 ssl->handshake->alt_transform_out = ssl->transform_out;
Gilles Peskine449bd832023-01-11 14:50:10 +01008000 memcpy(ssl->handshake->alt_out_ctr, ssl->cur_out_ctr,
8001 sizeof(ssl->handshake->alt_out_ctr));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008002
8003 /* Set sequence_number to zero */
Gilles Peskine449bd832023-01-11 14:50:10 +01008004 memset(&ssl->cur_out_ctr[2], 0, sizeof(ssl->cur_out_ctr) - 2);
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008005
8006
8007 /* Increment epoch */
Gilles Peskine449bd832023-01-11 14:50:10 +01008008 for (i = 2; i > 0; i--) {
8009 if (++ssl->cur_out_ctr[i - 1] != 0) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008010 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01008011 }
8012 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008013
8014 /* The loop goes to its end iff the counter is wrapping */
Gilles Peskine449bd832023-01-11 14:50:10 +01008015 if (i == 0) {
8016 MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS epoch would wrap"));
8017 return MBEDTLS_ERR_SSL_COUNTER_WRAPPING;
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008018 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008019 } else
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008020#endif /* MBEDTLS_SSL_PROTO_DTLS */
Gilles Peskine449bd832023-01-11 14:50:10 +01008021 memset(ssl->cur_out_ctr, 0, sizeof(ssl->cur_out_ctr));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008022
8023 ssl->transform_out = ssl->transform_negotiate;
8024 ssl->session_out = ssl->session_negotiate;
8025
8026#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01008027 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
8028 mbedtls_ssl_send_flight_completed(ssl);
8029 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008030#endif
8031
Gilles Peskine449bd832023-01-11 14:50:10 +01008032 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
8033 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
8034 return ret;
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008035 }
8036
8037#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01008038 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
8039 (ret = mbedtls_ssl_flight_transmit(ssl)) != 0) {
8040 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_flight_transmit", ret);
8041 return ret;
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008042 }
8043#endif
8044
Gilles Peskine449bd832023-01-11 14:50:10 +01008045 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write finished"));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008046
Gilles Peskine449bd832023-01-11 14:50:10 +01008047 return 0;
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008048}
8049
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008050#define SSL_MAX_HASH_LEN 12
8051
Gilles Peskine449bd832023-01-11 14:50:10 +01008052int mbedtls_ssl_parse_finished(mbedtls_ssl_context *ssl)
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008053{
8054 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
8055 unsigned int hash_len = 12;
8056 unsigned char buf[SSL_MAX_HASH_LEN];
8057
Gilles Peskine449bd832023-01-11 14:50:10 +01008058 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse finished"));
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008059
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01008060 ret = ssl->handshake->calc_finished(ssl, buf, ssl->conf->endpoint ^ 1);
8061 if (ret != 0) {
8062 MBEDTLS_SSL_DEBUG_RET(1, "calc_finished", ret);
8063 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008064
Gilles Peskine449bd832023-01-11 14:50:10 +01008065 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
8066 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008067 goto exit;
8068 }
8069
Gilles Peskine449bd832023-01-11 14:50:10 +01008070 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
8071 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
8072 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8073 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008074 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
8075 goto exit;
8076 }
8077
Gilles Peskine449bd832023-01-11 14:50:10 +01008078 if (ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED) {
8079 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8080 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008081 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
8082 goto exit;
8083 }
8084
Gilles Peskine449bd832023-01-11 14:50:10 +01008085 if (ssl->in_hslen != mbedtls_ssl_hs_hdr_len(ssl) + hash_len) {
8086 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
8087 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8088 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008089 ret = MBEDTLS_ERR_SSL_DECODE_ERROR;
8090 goto exit;
8091 }
8092
Gilles Peskine449bd832023-01-11 14:50:10 +01008093 if (mbedtls_ct_memcmp(ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl),
8094 buf, hash_len) != 0) {
8095 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
8096 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8097 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008098 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
8099 goto exit;
8100 }
8101
8102#if defined(MBEDTLS_SSL_RENEGOTIATION)
8103 ssl->verify_data_len = hash_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008104 memcpy(ssl->peer_verify_data, buf, hash_len);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008105#endif
8106
Gilles Peskine449bd832023-01-11 14:50:10 +01008107 if (ssl->handshake->resume != 0) {
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008108#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008109 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008110 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Gilles Peskine449bd832023-01-11 14:50:10 +01008111 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008112#endif
8113#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008114 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008115 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Gilles Peskine449bd832023-01-11 14:50:10 +01008116 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008117#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01008118 } else {
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008119 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01008120 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008121
8122#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01008123 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
8124 mbedtls_ssl_recv_flight_completed(ssl);
8125 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008126#endif
8127
Gilles Peskine449bd832023-01-11 14:50:10 +01008128 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse finished"));
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008129
8130exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008131 mbedtls_platform_zeroize(buf, hash_len);
8132 return ret;
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008133}
8134
Jerry Yu392112c2022-02-17 14:34:10 +08008135#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
8136/*
8137 * Helper to get TLS 1.2 PRF from ciphersuite
8138 * (Duplicates bits of logic from ssl_set_handshake_prfs().)
8139 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008140static tls_prf_fn ssl_tls12prf_from_cs(int ciphersuite_id)
Jerry Yu392112c2022-02-17 14:34:10 +08008141{
Jerry Yu392112c2022-02-17 14:34:10 +08008142 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01008143 mbedtls_ssl_ciphersuite_from_id(ciphersuite_id);
Andrzej Kurek68327742022-10-03 06:18:18 -04008144#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01008145 if (ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA384) {
8146 return tls_prf_sha384;
8147 } else
Jerry Yu392112c2022-02-17 14:34:10 +08008148#endif
Andrzej Kurek894edde2022-09-29 06:31:14 -04008149#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
8150 {
Gilles Peskine449bd832023-01-11 14:50:10 +01008151 if (ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA256) {
8152 return tls_prf_sha256;
8153 }
Andrzej Kurek894edde2022-09-29 06:31:14 -04008154 }
8155#endif
8156#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
8157 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
8158 (void) ciphersuite_info;
8159#endif
Andrzej Kurekeabeb302022-10-17 07:52:51 -04008160
Gilles Peskine449bd832023-01-11 14:50:10 +01008161 return NULL;
Jerry Yu392112c2022-02-17 14:34:10 +08008162}
8163#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Jerry Yue93ffcd2022-02-17 14:37:06 +08008164
Gilles Peskine449bd832023-01-11 14:50:10 +01008165static mbedtls_tls_prf_types tls_prf_get_type(mbedtls_ssl_tls_prf_cb *tls_prf)
Jerry Yue93ffcd2022-02-17 14:37:06 +08008166{
8167 ((void) tls_prf);
Andrzej Kurek25f27152022-08-17 16:09:31 -04008168#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01008169 if (tls_prf == tls_prf_sha384) {
8170 return MBEDTLS_SSL_TLS_PRF_SHA384;
8171 } else
Jerry Yue93ffcd2022-02-17 14:37:06 +08008172#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04008173#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01008174 if (tls_prf == tls_prf_sha256) {
8175 return MBEDTLS_SSL_TLS_PRF_SHA256;
8176 } else
Jerry Yue93ffcd2022-02-17 14:37:06 +08008177#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01008178 return MBEDTLS_SSL_TLS_PRF_NONE;
Jerry Yue93ffcd2022-02-17 14:37:06 +08008179}
8180
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008181/*
8182 * Populate a transform structure with session keys and all the other
8183 * necessary information.
8184 *
8185 * Parameters:
8186 * - [in/out]: transform: structure to populate
8187 * [in] must be just initialised with mbedtls_ssl_transform_init()
8188 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
8189 * - [in] ciphersuite
8190 * - [in] master
8191 * - [in] encrypt_then_mac
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008192 * - [in] tls_prf: pointer to PRF to use for key derivation
8193 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
Glenn Strauss07c64162022-03-14 12:34:51 -04008194 * - [in] tls_version: TLS version
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008195 * - [in] endpoint: client or server
8196 * - [in] ssl: used for:
8197 * - ssl->conf->{f,p}_export_keys
8198 * [in] optionally used for:
8199 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
8200 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02008201MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01008202static int ssl_tls12_populate_transform(mbedtls_ssl_transform *transform,
8203 int ciphersuite,
8204 const unsigned char master[48],
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008205#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01008206 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008207#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01008208 ssl_tls_prf_t tls_prf,
8209 const unsigned char randbytes[64],
8210 mbedtls_ssl_protocol_version tls_version,
8211 unsigned endpoint,
8212 const mbedtls_ssl_context *ssl)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008213{
8214 int ret = 0;
8215 unsigned char keyblk[256];
8216 unsigned char *key1;
8217 unsigned char *key2;
8218 unsigned char *mac_enc;
8219 unsigned char *mac_dec;
8220 size_t mac_key_len = 0;
8221 size_t iv_copy_len;
8222 size_t keylen;
8223 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008224 mbedtls_ssl_mode_t ssl_mode;
Neil Armstronge4512952022-03-08 09:08:22 +01008225#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008226 const mbedtls_cipher_info_t *cipher_info;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008227 const mbedtls_md_info_t *md_info;
Neil Armstronge4512952022-03-08 09:08:22 +01008228#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008229
8230#if defined(MBEDTLS_USE_PSA_CRYPTO)
8231 psa_key_type_t key_type;
8232 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8233 psa_algorithm_t alg;
Neil Armstronge4512952022-03-08 09:08:22 +01008234 psa_algorithm_t mac_alg = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008235 size_t key_bits;
8236 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
8237#endif
8238
8239#if !defined(MBEDTLS_DEBUG_C) && \
8240 !defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Gilles Peskine449bd832023-01-11 14:50:10 +01008241 if (ssl->f_export_keys == NULL) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008242 ssl = NULL; /* make sure we don't use it except for these cases */
8243 (void) ssl;
8244 }
8245#endif
8246
8247 /*
8248 * Some data just needs copying into the structure
8249 */
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008250#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008251 transform->encrypt_then_mac = encrypt_then_mac;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008252#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Glenn Strauss07c64162022-03-14 12:34:51 -04008253 transform->tls_version = tls_version;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008254
8255#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01008256 memcpy(transform->randbytes, randbytes, sizeof(transform->randbytes));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008257#endif
8258
8259#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01008260 if (tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008261 /* At the moment, we keep TLS <= 1.2 and TLS 1.3 transform
8262 * generation separate. This should never happen. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008263 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008264 }
8265#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
8266
8267 /*
8268 * Get various info structures
8269 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008270 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(ciphersuite);
8271 if (ciphersuite_info == NULL) {
8272 MBEDTLS_SSL_DEBUG_MSG(1, ("ciphersuite info for %d not found",
8273 ciphersuite));
8274 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008275 }
8276
Neil Armstrongab555e02022-04-04 11:07:59 +02008277 ssl_mode = mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008278#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01008279 encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008280#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01008281 ciphersuite_info);
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008282
Gilles Peskine449bd832023-01-11 14:50:10 +01008283 if (ssl_mode == MBEDTLS_SSL_MODE_AEAD) {
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008284 transform->taglen =
8285 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Gilles Peskine449bd832023-01-11 14:50:10 +01008286 }
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008287
8288#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008289 if ((status = mbedtls_ssl_cipher_to_psa(ciphersuite_info->cipher,
8290 transform->taglen,
8291 &alg,
8292 &key_type,
8293 &key_bits)) != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05008294 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01008295 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_cipher_to_psa", ret);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008296 goto end;
8297 }
8298#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008299 cipher_info = mbedtls_cipher_info_from_type(ciphersuite_info->cipher);
8300 if (cipher_info == NULL) {
8301 MBEDTLS_SSL_DEBUG_MSG(1, ("cipher info for %u not found",
8302 ciphersuite_info->cipher));
8303 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008304 }
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008305#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008306
Neil Armstronge4512952022-03-08 09:08:22 +01008307#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008308 mac_alg = mbedtls_hash_info_psa_from_md(ciphersuite_info->mac);
8309 if (mac_alg == 0) {
8310 MBEDTLS_SSL_DEBUG_MSG(1, ("mbedtls_hash_info_psa_from_md for %u not found",
8311 (unsigned) ciphersuite_info->mac));
8312 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Neil Armstronge4512952022-03-08 09:08:22 +01008313 }
8314#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008315 md_info = mbedtls_md_info_from_type(ciphersuite_info->mac);
8316 if (md_info == NULL) {
8317 MBEDTLS_SSL_DEBUG_MSG(1, ("mbedtls_md info for %u not found",
8318 (unsigned) ciphersuite_info->mac));
8319 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008320 }
Neil Armstronge4512952022-03-08 09:08:22 +01008321#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008322
8323#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
8324 /* Copy own and peer's CID if the use of the CID
8325 * extension has been negotiated. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008326 if (ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED) {
8327 MBEDTLS_SSL_DEBUG_MSG(3, ("Copy CIDs into SSL transform"));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008328
8329 transform->in_cid_len = ssl->own_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008330 memcpy(transform->in_cid, ssl->own_cid, ssl->own_cid_len);
8331 MBEDTLS_SSL_DEBUG_BUF(3, "Incoming CID", transform->in_cid,
8332 transform->in_cid_len);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008333
8334 transform->out_cid_len = ssl->handshake->peer_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008335 memcpy(transform->out_cid, ssl->handshake->peer_cid,
8336 ssl->handshake->peer_cid_len);
8337 MBEDTLS_SSL_DEBUG_BUF(3, "Outgoing CID", transform->out_cid,
8338 transform->out_cid_len);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008339 }
8340#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
8341
8342 /*
8343 * Compute key block using the PRF
8344 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008345 ret = tls_prf(master, 48, "key expansion", randbytes, 64, keyblk, 256);
8346 if (ret != 0) {
8347 MBEDTLS_SSL_DEBUG_RET(1, "prf", ret);
8348 return ret;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008349 }
8350
Gilles Peskine449bd832023-01-11 14:50:10 +01008351 MBEDTLS_SSL_DEBUG_MSG(3, ("ciphersuite = %s",
8352 mbedtls_ssl_get_ciphersuite_name(ciphersuite)));
8353 MBEDTLS_SSL_DEBUG_BUF(3, "master secret", master, 48);
8354 MBEDTLS_SSL_DEBUG_BUF(4, "random bytes", randbytes, 64);
8355 MBEDTLS_SSL_DEBUG_BUF(4, "key block", keyblk, 256);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008356
8357 /*
8358 * Determine the appropriate key, IV and MAC length.
8359 */
8360
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008361#if defined(MBEDTLS_USE_PSA_CRYPTO)
8362 keylen = PSA_BITS_TO_BYTES(key_bits);
8363#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008364 keylen = mbedtls_cipher_info_get_key_bitlen(cipher_info) / 8;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008365#endif
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008366
8367#if defined(MBEDTLS_GCM_C) || \
8368 defined(MBEDTLS_CCM_C) || \
8369 defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008370 if (ssl_mode == MBEDTLS_SSL_MODE_AEAD) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008371 size_t explicit_ivlen;
8372
8373 transform->maclen = 0;
8374 mac_key_len = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008375
8376 /* All modes haves 96-bit IVs, but the length of the static parts vary
8377 * with mode and version:
8378 * - For GCM and CCM in TLS 1.2, there's a static IV of 4 Bytes
8379 * (to be concatenated with a dynamically chosen IV of 8 Bytes)
8380 * - For ChaChaPoly in TLS 1.2, and all modes in TLS 1.3, there's
8381 * a static IV of 12 Bytes (to be XOR'ed with the 8 Byte record
8382 * sequence number).
8383 */
8384 transform->ivlen = 12;
David Horstmann3b2276a2022-10-06 14:49:08 +01008385
8386 int is_chachapoly = 0;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008387#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008388 is_chachapoly = (key_type == PSA_KEY_TYPE_CHACHA20);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008389#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008390 is_chachapoly = (mbedtls_cipher_info_get_mode(cipher_info)
8391 == MBEDTLS_MODE_CHACHAPOLY);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008392#endif /* MBEDTLS_USE_PSA_CRYPTO */
David Horstmann3b2276a2022-10-06 14:49:08 +01008393
Gilles Peskine449bd832023-01-11 14:50:10 +01008394 if (is_chachapoly) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008395 transform->fixed_ivlen = 12;
Gilles Peskine449bd832023-01-11 14:50:10 +01008396 } else {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008397 transform->fixed_ivlen = 4;
Gilles Peskine449bd832023-01-11 14:50:10 +01008398 }
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008399
8400 /* Minimum length of encrypted record */
8401 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
8402 transform->minlen = explicit_ivlen + transform->taglen;
Gilles Peskine449bd832023-01-11 14:50:10 +01008403 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008404#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
8405#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01008406 if (ssl_mode == MBEDTLS_SSL_MODE_STREAM ||
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008407 ssl_mode == MBEDTLS_SSL_MODE_CBC ||
Gilles Peskine449bd832023-01-11 14:50:10 +01008408 ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM) {
Neil Armstronge4512952022-03-08 09:08:22 +01008409#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008410 size_t block_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008411#else
8412 size_t block_size = cipher_info->block_size;
8413#endif /* MBEDTLS_USE_PSA_CRYPTO */
8414
8415#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronge4512952022-03-08 09:08:22 +01008416 /* Get MAC length */
8417 mac_key_len = PSA_HASH_LENGTH(mac_alg);
8418#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008419 /* Initialize HMAC contexts */
Gilles Peskine449bd832023-01-11 14:50:10 +01008420 if ((ret = mbedtls_md_setup(&transform->md_ctx_enc, md_info, 1)) != 0 ||
8421 (ret = mbedtls_md_setup(&transform->md_ctx_dec, md_info, 1)) != 0) {
8422 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_setup", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008423 goto end;
8424 }
8425
8426 /* Get MAC length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008427 mac_key_len = mbedtls_md_get_size(md_info);
Neil Armstronge4512952022-03-08 09:08:22 +01008428#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008429 transform->maclen = mac_key_len;
8430
8431 /* IV length */
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008432#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008433 transform->ivlen = PSA_CIPHER_IV_LENGTH(key_type, alg);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008434#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008435 transform->ivlen = cipher_info->iv_size;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008436#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008437
8438 /* Minimum length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008439 if (ssl_mode == MBEDTLS_SSL_MODE_STREAM) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008440 transform->minlen = transform->maclen;
Gilles Peskine449bd832023-01-11 14:50:10 +01008441 } else {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008442 /*
8443 * GenericBlockCipher:
8444 * 1. if EtM is in use: one block plus MAC
8445 * otherwise: * first multiple of blocklen greater than maclen
8446 * 2. IV
8447 */
8448#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01008449 if (ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008450 transform->minlen = transform->maclen
Gilles Peskine449bd832023-01-11 14:50:10 +01008451 + block_size;
8452 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008453#endif
8454 {
8455 transform->minlen = transform->maclen
Gilles Peskine449bd832023-01-11 14:50:10 +01008456 + block_size
8457 - transform->maclen % block_size;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008458 }
8459
Gilles Peskine449bd832023-01-11 14:50:10 +01008460 if (tls_version == MBEDTLS_SSL_VERSION_TLS1_2) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008461 transform->minlen += transform->ivlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01008462 } else {
8463 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008464 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
8465 goto end;
8466 }
8467 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008468 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008469#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
8470 {
Gilles Peskine449bd832023-01-11 14:50:10 +01008471 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
8472 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008473 }
8474
Gilles Peskine449bd832023-01-11 14:50:10 +01008475 MBEDTLS_SSL_DEBUG_MSG(3, ("keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
8476 (unsigned) keylen,
8477 (unsigned) transform->minlen,
8478 (unsigned) transform->ivlen,
8479 (unsigned) transform->maclen));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008480
8481 /*
8482 * Finally setup the cipher contexts, IVs and MAC secrets.
8483 */
8484#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008485 if (endpoint == MBEDTLS_SSL_IS_CLIENT) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008486 key1 = keyblk + mac_key_len * 2;
8487 key2 = keyblk + mac_key_len * 2 + keylen;
8488
8489 mac_enc = keyblk;
8490 mac_dec = keyblk + mac_key_len;
8491
Gilles Peskine449bd832023-01-11 14:50:10 +01008492 iv_copy_len = (transform->fixed_ivlen) ?
8493 transform->fixed_ivlen : transform->ivlen;
8494 memcpy(transform->iv_enc, key2 + keylen, iv_copy_len);
8495 memcpy(transform->iv_dec, key2 + keylen + iv_copy_len,
8496 iv_copy_len);
8497 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008498#endif /* MBEDTLS_SSL_CLI_C */
8499#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008500 if (endpoint == MBEDTLS_SSL_IS_SERVER) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008501 key1 = keyblk + mac_key_len * 2 + keylen;
8502 key2 = keyblk + mac_key_len * 2;
8503
8504 mac_enc = keyblk + mac_key_len;
8505 mac_dec = keyblk;
8506
Gilles Peskine449bd832023-01-11 14:50:10 +01008507 iv_copy_len = (transform->fixed_ivlen) ?
8508 transform->fixed_ivlen : transform->ivlen;
8509 memcpy(transform->iv_dec, key1 + keylen, iv_copy_len);
8510 memcpy(transform->iv_enc, key1 + keylen + iv_copy_len,
8511 iv_copy_len);
8512 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008513#endif /* MBEDTLS_SSL_SRV_C */
8514 {
Gilles Peskine449bd832023-01-11 14:50:10 +01008515 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008516 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
8517 goto end;
8518 }
8519
Gilles Peskine449bd832023-01-11 14:50:10 +01008520 if (ssl != NULL && ssl->f_export_keys != NULL) {
8521 ssl->f_export_keys(ssl->p_export_keys,
8522 MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET,
8523 master, 48,
8524 randbytes + 32,
8525 randbytes,
8526 tls_prf_get_type(tls_prf));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008527 }
8528
8529#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008530 transform->psa_alg = alg;
8531
Gilles Peskine449bd832023-01-11 14:50:10 +01008532 if (alg != MBEDTLS_SSL_NULL_CIPHER) {
8533 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
8534 psa_set_key_algorithm(&attributes, alg);
8535 psa_set_key_type(&attributes, key_type);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008536
Gilles Peskine449bd832023-01-11 14:50:10 +01008537 if ((status = psa_import_key(&attributes,
8538 key1,
8539 PSA_BITS_TO_BYTES(key_bits),
8540 &transform->psa_key_enc)) != PSA_SUCCESS) {
8541 MBEDTLS_SSL_DEBUG_RET(3, "psa_import_key", (int) status);
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05008542 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01008543 MBEDTLS_SSL_DEBUG_RET(1, "psa_import_key", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008544 goto end;
8545 }
8546
Gilles Peskine449bd832023-01-11 14:50:10 +01008547 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008548
Gilles Peskine449bd832023-01-11 14:50:10 +01008549 if ((status = psa_import_key(&attributes,
8550 key2,
8551 PSA_BITS_TO_BYTES(key_bits),
8552 &transform->psa_key_dec)) != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05008553 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01008554 MBEDTLS_SSL_DEBUG_RET(1, "psa_import_key", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008555 goto end;
8556 }
8557 }
8558#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008559 if ((ret = mbedtls_cipher_setup(&transform->cipher_ctx_enc,
8560 cipher_info)) != 0) {
8561 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup", 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_setup(&transform->cipher_ctx_dec,
8566 cipher_info)) != 0) {
8567 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008568 goto end;
8569 }
8570
Gilles Peskine449bd832023-01-11 14:50:10 +01008571 if ((ret = mbedtls_cipher_setkey(&transform->cipher_ctx_enc, key1,
8572 (int) mbedtls_cipher_info_get_key_bitlen(cipher_info),
8573 MBEDTLS_ENCRYPT)) != 0) {
8574 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setkey", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008575 goto end;
8576 }
8577
Gilles Peskine449bd832023-01-11 14:50:10 +01008578 if ((ret = mbedtls_cipher_setkey(&transform->cipher_ctx_dec, key2,
8579 (int) mbedtls_cipher_info_get_key_bitlen(cipher_info),
8580 MBEDTLS_DECRYPT)) != 0) {
8581 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setkey", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008582 goto end;
8583 }
8584
8585#if defined(MBEDTLS_CIPHER_MODE_CBC)
Gilles Peskine449bd832023-01-11 14:50:10 +01008586 if (mbedtls_cipher_info_get_mode(cipher_info) == MBEDTLS_MODE_CBC) {
8587 if ((ret = mbedtls_cipher_set_padding_mode(&transform->cipher_ctx_enc,
8588 MBEDTLS_PADDING_NONE)) != 0) {
8589 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_set_padding_mode", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008590 goto end;
8591 }
8592
Gilles Peskine449bd832023-01-11 14:50:10 +01008593 if ((ret = mbedtls_cipher_set_padding_mode(&transform->cipher_ctx_dec,
8594 MBEDTLS_PADDING_NONE)) != 0) {
8595 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_set_padding_mode", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008596 goto end;
8597 }
8598 }
8599#endif /* MBEDTLS_CIPHER_MODE_CBC */
8600#endif /* MBEDTLS_USE_PSA_CRYPTO */
8601
Neil Armstrong29c0c042022-03-17 17:47:28 +01008602#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
8603 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
8604 For AEAD-based ciphersuites, there is nothing to do here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008605 if (mac_key_len != 0) {
Neil Armstrong29c0c042022-03-17 17:47:28 +01008606#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008607 transform->psa_mac_alg = PSA_ALG_HMAC(mac_alg);
Neil Armstrong29c0c042022-03-17 17:47:28 +01008608
Gilles Peskine449bd832023-01-11 14:50:10 +01008609 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
8610 psa_set_key_algorithm(&attributes, PSA_ALG_HMAC(mac_alg));
8611 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
Neil Armstrong29c0c042022-03-17 17:47:28 +01008612
Gilles Peskine449bd832023-01-11 14:50:10 +01008613 if ((status = psa_import_key(&attributes,
8614 mac_enc, mac_key_len,
8615 &transform->psa_mac_enc)) != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05008616 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01008617 MBEDTLS_SSL_DEBUG_RET(1, "psa_import_mac_key", ret);
Neil Armstrong29c0c042022-03-17 17:47:28 +01008618 goto end;
8619 }
8620
Gilles Peskine449bd832023-01-11 14:50:10 +01008621 if ((transform->psa_alg == MBEDTLS_SSL_NULL_CIPHER) ||
8622 ((transform->psa_alg == PSA_ALG_CBC_NO_PADDING)
Andrzej Kurek8c95ac42022-08-17 16:17:00 -04008623#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01008624 && (transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED)
Andrzej Kurek8c95ac42022-08-17 16:17:00 -04008625#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01008626 )) {
Neil Armstrong29c0c042022-03-17 17:47:28 +01008627 /* mbedtls_ct_hmac() requires the key to be exportable */
Gilles Peskine449bd832023-01-11 14:50:10 +01008628 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT |
8629 PSA_KEY_USAGE_VERIFY_HASH);
8630 } else {
8631 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
8632 }
Neil Armstrong29c0c042022-03-17 17:47:28 +01008633
Gilles Peskine449bd832023-01-11 14:50:10 +01008634 if ((status = psa_import_key(&attributes,
8635 mac_dec, mac_key_len,
8636 &transform->psa_mac_dec)) != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05008637 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01008638 MBEDTLS_SSL_DEBUG_RET(1, "psa_import_mac_key", ret);
Neil Armstrong29c0c042022-03-17 17:47:28 +01008639 goto end;
8640 }
8641#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008642 ret = mbedtls_md_hmac_starts(&transform->md_ctx_enc, mac_enc, mac_key_len);
8643 if (ret != 0) {
Neil Armstrong29c0c042022-03-17 17:47:28 +01008644 goto end;
Gilles Peskine449bd832023-01-11 14:50:10 +01008645 }
8646 ret = mbedtls_md_hmac_starts(&transform->md_ctx_dec, mac_dec, mac_key_len);
8647 if (ret != 0) {
Neil Armstrong29c0c042022-03-17 17:47:28 +01008648 goto end;
Gilles Peskine449bd832023-01-11 14:50:10 +01008649 }
Neil Armstrong29c0c042022-03-17 17:47:28 +01008650#endif /* MBEDTLS_USE_PSA_CRYPTO */
8651 }
8652#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
8653
8654 ((void) mac_dec);
8655 ((void) mac_enc);
8656
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008657end:
Gilles Peskine449bd832023-01-11 14:50:10 +01008658 mbedtls_platform_zeroize(keyblk, sizeof(keyblk));
8659 return ret;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008660}
8661
Valerio Settia08b1a42022-11-17 15:10:02 +01008662#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) && \
8663 defined(MBEDTLS_USE_PSA_CRYPTO)
Valerio Setti6b3dab02022-11-17 17:14:54 +01008664int mbedtls_psa_ecjpake_read_round(
Gilles Peskine449bd832023-01-11 14:50:10 +01008665 psa_pake_operation_t *pake_ctx,
8666 const unsigned char *buf,
8667 size_t len, mbedtls_ecjpake_rounds_t round)
Valerio Settia08b1a42022-11-17 15:10:02 +01008668{
8669 psa_status_t status;
8670 size_t input_offset = 0;
Valerio Setti819de862022-11-17 18:05:19 +01008671 /*
Valerio Setti6b3dab02022-11-17 17:14:54 +01008672 * At round one repeat the KEY_SHARE, ZK_PUBLIC & ZF_PROOF twice
8673 * At round two perform a single cycle
8674 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008675 unsigned int remaining_steps = (round == MBEDTLS_ECJPAKE_ROUND_ONE) ? 2 : 1;
Valerio Settia08b1a42022-11-17 15:10:02 +01008676
Gilles Peskine449bd832023-01-11 14:50:10 +01008677 for (; remaining_steps > 0; remaining_steps--) {
8678 for (psa_pake_step_t step = PSA_PAKE_STEP_KEY_SHARE;
Valerio Settia08b1a42022-11-17 15:10:02 +01008679 step <= PSA_PAKE_STEP_ZK_PROOF;
Gilles Peskine449bd832023-01-11 14:50:10 +01008680 ++step) {
Valerio Settia08b1a42022-11-17 15:10:02 +01008681 /* Length is stored at the first byte */
8682 size_t length = buf[input_offset];
8683 input_offset += 1;
8684
Gilles Peskine449bd832023-01-11 14:50:10 +01008685 if (input_offset + length > len) {
Valerio Settia08b1a42022-11-17 15:10:02 +01008686 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
8687 }
8688
Gilles Peskine449bd832023-01-11 14:50:10 +01008689 status = psa_pake_input(pake_ctx, step,
8690 buf + input_offset, length);
8691 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05008692 return PSA_TO_MBEDTLS_ERR(status);
Valerio Settia08b1a42022-11-17 15:10:02 +01008693 }
8694
8695 input_offset += length;
8696 }
8697 }
8698
Gilles Peskine449bd832023-01-11 14:50:10 +01008699 if (input_offset != len) {
Valerio Setti61ea17d2022-11-18 12:11:00 +01008700 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01008701 }
Valerio Setti30ebe112022-11-17 16:23:34 +01008702
Gilles Peskine449bd832023-01-11 14:50:10 +01008703 return 0;
Valerio Settia08b1a42022-11-17 15:10:02 +01008704}
8705
Valerio Setti6b3dab02022-11-17 17:14:54 +01008706int mbedtls_psa_ecjpake_write_round(
Gilles Peskine449bd832023-01-11 14:50:10 +01008707 psa_pake_operation_t *pake_ctx,
8708 unsigned char *buf,
8709 size_t len, size_t *olen,
8710 mbedtls_ecjpake_rounds_t round)
Valerio Settia08b1a42022-11-17 15:10:02 +01008711{
8712 psa_status_t status;
8713 size_t output_offset = 0;
8714 size_t output_len;
Valerio Setti819de862022-11-17 18:05:19 +01008715 /*
Valerio Setti6b3dab02022-11-17 17:14:54 +01008716 * At round one repeat the KEY_SHARE, ZK_PUBLIC & ZF_PROOF twice
8717 * At round two perform a single cycle
8718 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008719 unsigned int remaining_steps = (round == MBEDTLS_ECJPAKE_ROUND_ONE) ? 2 : 1;
Valerio Settia08b1a42022-11-17 15:10:02 +01008720
Gilles Peskine449bd832023-01-11 14:50:10 +01008721 for (; remaining_steps > 0; remaining_steps--) {
8722 for (psa_pake_step_t step = PSA_PAKE_STEP_KEY_SHARE;
8723 step <= PSA_PAKE_STEP_ZK_PROOF;
8724 ++step) {
Valerio Setti79f6b6b2022-11-21 14:17:03 +01008725 /*
Valerio Settid4a9b1a2022-11-22 11:11:10 +01008726 * For each step, prepend 1 byte with the length of the data as
8727 * given by psa_pake_output().
Valerio Setti79f6b6b2022-11-21 14:17:03 +01008728 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008729 status = psa_pake_output(pake_ctx, step,
8730 buf + output_offset + 1,
8731 len - output_offset - 1,
8732 &output_len);
8733 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05008734 return PSA_TO_MBEDTLS_ERR(status);
Valerio Settia08b1a42022-11-17 15:10:02 +01008735 }
8736
Valerio Setti99d88c12022-11-22 16:03:43 +01008737 *(buf + output_offset) = (uint8_t) output_len;
Valerio Setti79f6b6b2022-11-21 14:17:03 +01008738
8739 output_offset += output_len + 1;
Valerio Settia08b1a42022-11-17 15:10:02 +01008740 }
8741 }
8742
8743 *olen = output_offset;
8744
Gilles Peskine449bd832023-01-11 14:50:10 +01008745 return 0;
Valerio Settia08b1a42022-11-17 15:10:02 +01008746}
Valerio Settia08b1a42022-11-17 15:10:02 +01008747#endif //MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED && MBEDTLS_USE_PSA_CRYPTO
8748
Jerry Yuee40f9d2022-02-17 14:55:16 +08008749#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008750int mbedtls_ssl_get_key_exchange_md_tls1_2(mbedtls_ssl_context *ssl,
8751 unsigned char *hash, size_t *hashlen,
8752 unsigned char *data, size_t data_len,
8753 mbedtls_md_type_t md_alg)
Jerry Yuee40f9d2022-02-17 14:55:16 +08008754{
8755 psa_status_t status;
8756 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01008757 psa_algorithm_t hash_alg = mbedtls_hash_info_psa_from_md(md_alg);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008758
Gilles Peskine449bd832023-01-11 14:50:10 +01008759 MBEDTLS_SSL_DEBUG_MSG(3, ("Perform PSA-based computation of digest of ServerKeyExchange"));
Jerry Yuee40f9d2022-02-17 14:55:16 +08008760
Gilles Peskine449bd832023-01-11 14:50:10 +01008761 if ((status = psa_hash_setup(&hash_operation,
8762 hash_alg)) != PSA_SUCCESS) {
8763 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_setup", status);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008764 goto exit;
8765 }
8766
Gilles Peskine449bd832023-01-11 14:50:10 +01008767 if ((status = psa_hash_update(&hash_operation, ssl->handshake->randbytes,
8768 64)) != PSA_SUCCESS) {
8769 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_update", status);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008770 goto exit;
8771 }
8772
Gilles Peskine449bd832023-01-11 14:50:10 +01008773 if ((status = psa_hash_update(&hash_operation,
8774 data, data_len)) != PSA_SUCCESS) {
8775 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_update", status);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008776 goto exit;
8777 }
8778
Gilles Peskine449bd832023-01-11 14:50:10 +01008779 if ((status = psa_hash_finish(&hash_operation, hash, PSA_HASH_MAX_SIZE,
8780 hashlen)) != PSA_SUCCESS) {
8781 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_finish", status);
8782 goto exit;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008783 }
8784
8785exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008786 if (status != PSA_SUCCESS) {
8787 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8788 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
8789 switch (status) {
Jerry Yuee40f9d2022-02-17 14:55:16 +08008790 case PSA_ERROR_NOT_SUPPORTED:
Gilles Peskine449bd832023-01-11 14:50:10 +01008791 return MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008792 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
8793 case PSA_ERROR_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +01008794 return MBEDTLS_ERR_MD_BAD_INPUT_DATA;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008795 case PSA_ERROR_INSUFFICIENT_MEMORY:
Gilles Peskine449bd832023-01-11 14:50:10 +01008796 return MBEDTLS_ERR_MD_ALLOC_FAILED;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008797 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01008798 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008799 }
8800 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008801 return 0;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008802}
8803
8804#else
8805
Gilles Peskine449bd832023-01-11 14:50:10 +01008806int mbedtls_ssl_get_key_exchange_md_tls1_2(mbedtls_ssl_context *ssl,
8807 unsigned char *hash, size_t *hashlen,
8808 unsigned char *data, size_t data_len,
8809 mbedtls_md_type_t md_alg)
Jerry Yuee40f9d2022-02-17 14:55:16 +08008810{
8811 int ret = 0;
8812 mbedtls_md_context_t ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +01008813 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type(md_alg);
8814 *hashlen = mbedtls_md_get_size(md_info);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008815
Gilles Peskine449bd832023-01-11 14:50:10 +01008816 MBEDTLS_SSL_DEBUG_MSG(3, ("Perform mbedtls-based computation of digest of ServerKeyExchange"));
Jerry Yuee40f9d2022-02-17 14:55:16 +08008817
Gilles Peskine449bd832023-01-11 14:50:10 +01008818 mbedtls_md_init(&ctx);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008819
8820 /*
8821 * digitally-signed struct {
8822 * opaque client_random[32];
8823 * opaque server_random[32];
8824 * ServerDHParams params;
8825 * };
8826 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008827 if ((ret = mbedtls_md_setup(&ctx, md_info, 0)) != 0) {
8828 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_setup", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008829 goto exit;
8830 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008831 if ((ret = mbedtls_md_starts(&ctx)) != 0) {
8832 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_starts", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008833 goto exit;
8834 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008835 if ((ret = mbedtls_md_update(&ctx, ssl->handshake->randbytes, 64)) != 0) {
8836 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_update", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008837 goto exit;
8838 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008839 if ((ret = mbedtls_md_update(&ctx, data, data_len)) != 0) {
8840 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_update", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008841 goto exit;
8842 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008843 if ((ret = mbedtls_md_finish(&ctx, hash)) != 0) {
8844 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_finish", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008845 goto exit;
8846 }
8847
8848exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008849 mbedtls_md_free(&ctx);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008850
Gilles Peskine449bd832023-01-11 14:50:10 +01008851 if (ret != 0) {
8852 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8853 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
8854 }
Jerry Yuee40f9d2022-02-17 14:55:16 +08008855
Gilles Peskine449bd832023-01-11 14:50:10 +01008856 return ret;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008857}
8858#endif /* MBEDTLS_USE_PSA_CRYPTO */
8859
Jerry Yud9d91da2022-02-17 14:57:06 +08008860#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
8861
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008862/* Find the preferred hash for a given signature algorithm. */
8863unsigned int mbedtls_ssl_tls12_get_preferred_hash_for_sig_alg(
Gilles Peskine449bd832023-01-11 14:50:10 +01008864 mbedtls_ssl_context *ssl,
8865 unsigned int sig_alg)
Jerry Yud9d91da2022-02-17 14:57:06 +08008866{
Gabor Mezei078e8032022-04-27 21:17:56 +02008867 unsigned int i;
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008868 uint16_t *received_sig_algs = ssl->handshake->received_sig_algs;
Gabor Mezei078e8032022-04-27 21:17:56 +02008869
Gilles Peskine449bd832023-01-11 14:50:10 +01008870 if (sig_alg == MBEDTLS_SSL_SIG_ANON) {
8871 return MBEDTLS_SSL_HASH_NONE;
8872 }
Gabor Mezei078e8032022-04-27 21:17:56 +02008873
Gilles Peskine449bd832023-01-11 14:50:10 +01008874 for (i = 0; received_sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++) {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008875 unsigned int hash_alg_received =
Gilles Peskine449bd832023-01-11 14:50:10 +01008876 MBEDTLS_SSL_TLS12_HASH_ALG_FROM_SIG_AND_HASH_ALG(
8877 received_sig_algs[i]);
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008878 unsigned int sig_alg_received =
Gilles Peskine449bd832023-01-11 14:50:10 +01008879 MBEDTLS_SSL_TLS12_SIG_ALG_FROM_SIG_AND_HASH_ALG(
8880 received_sig_algs[i]);
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008881
Gilles Peskine449bd832023-01-11 14:50:10 +01008882 if (sig_alg == sig_alg_received) {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008883#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008884 if (ssl->handshake->key_cert && ssl->handshake->key_cert->key) {
Neil Armstrong96eceb82022-06-30 18:05:05 +02008885 psa_algorithm_t psa_hash_alg =
Gilles Peskine449bd832023-01-11 14:50:10 +01008886 mbedtls_hash_info_psa_from_md(hash_alg_received);
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008887
Gilles Peskine449bd832023-01-11 14:50:10 +01008888 if (sig_alg_received == MBEDTLS_SSL_SIG_ECDSA &&
8889 !mbedtls_pk_can_do_ext(ssl->handshake->key_cert->key,
8890 PSA_ALG_ECDSA(psa_hash_alg),
8891 PSA_KEY_USAGE_SIGN_HASH)) {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008892 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01008893 }
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008894
Gilles Peskine449bd832023-01-11 14:50:10 +01008895 if (sig_alg_received == MBEDTLS_SSL_SIG_RSA &&
8896 !mbedtls_pk_can_do_ext(ssl->handshake->key_cert->key,
8897 PSA_ALG_RSA_PKCS1V15_SIGN(
8898 psa_hash_alg),
8899 PSA_KEY_USAGE_SIGN_HASH)) {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008900 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01008901 }
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008902 }
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008903#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong96eceb82022-06-30 18:05:05 +02008904
Gilles Peskine449bd832023-01-11 14:50:10 +01008905 return hash_alg_received;
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008906 }
Jerry Yud9d91da2022-02-17 14:57:06 +08008907 }
Jerry Yud9d91da2022-02-17 14:57:06 +08008908
Gilles Peskine449bd832023-01-11 14:50:10 +01008909 return MBEDTLS_SSL_HASH_NONE;
Jerry Yud9d91da2022-02-17 14:57:06 +08008910}
8911
8912#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
8913
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008914/* Serialization of TLS 1.2 sessions:
8915 *
8916 * struct {
8917 * uint64 start_time;
8918 * uint8 ciphersuite[2]; // defined by the standard
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008919 * uint8 session_id_len; // at most 32
8920 * opaque session_id[32];
8921 * opaque master[48]; // fixed length in the standard
8922 * uint32 verify_result;
8923 * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert
8924 * opaque ticket<0..2^24-1>; // length 0 means no ticket
8925 * uint32 ticket_lifetime;
8926 * uint8 mfl_code; // up to 255 according to standard
8927 * uint8 encrypt_then_mac; // 0 or 1
8928 * } serialized_session_tls12;
8929 *
8930 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008931static size_t ssl_tls12_session_save(const mbedtls_ssl_session *session,
8932 unsigned char *buf,
8933 size_t buf_len)
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008934{
8935 unsigned char *p = buf;
8936 size_t used = 0;
8937
8938#if defined(MBEDTLS_HAVE_TIME)
8939 uint64_t start;
8940#endif
8941#if defined(MBEDTLS_X509_CRT_PARSE_C)
8942#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8943 size_t cert_len;
8944#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8945#endif /* MBEDTLS_X509_CRT_PARSE_C */
8946
8947 /*
8948 * Time
8949 */
8950#if defined(MBEDTLS_HAVE_TIME)
8951 used += 8;
8952
Gilles Peskine449bd832023-01-11 14:50:10 +01008953 if (used <= buf_len) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008954 start = (uint64_t) session->start;
8955
Gilles Peskine449bd832023-01-11 14:50:10 +01008956 MBEDTLS_PUT_UINT64_BE(start, p, 0);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008957 p += 8;
8958 }
8959#endif /* MBEDTLS_HAVE_TIME */
8960
8961 /*
8962 * Basic mandatory fields
8963 */
8964 used += 2 /* ciphersuite */
Gilles Peskine449bd832023-01-11 14:50:10 +01008965 + 1 /* id_len */
8966 + sizeof(session->id)
8967 + sizeof(session->master)
8968 + 4; /* verify_result */
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008969
Gilles Peskine449bd832023-01-11 14:50:10 +01008970 if (used <= buf_len) {
8971 MBEDTLS_PUT_UINT16_BE(session->ciphersuite, p, 0);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008972 p += 2;
8973
Gilles Peskine449bd832023-01-11 14:50:10 +01008974 *p++ = MBEDTLS_BYTE_0(session->id_len);
8975 memcpy(p, session->id, 32);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008976 p += 32;
8977
Gilles Peskine449bd832023-01-11 14:50:10 +01008978 memcpy(p, session->master, 48);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008979 p += 48;
8980
Gilles Peskine449bd832023-01-11 14:50:10 +01008981 MBEDTLS_PUT_UINT32_BE(session->verify_result, p, 0);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008982 p += 4;
8983 }
8984
8985 /*
8986 * Peer's end-entity certificate
8987 */
8988#if defined(MBEDTLS_X509_CRT_PARSE_C)
8989#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01008990 if (session->peer_cert == NULL) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008991 cert_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01008992 } else {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008993 cert_len = session->peer_cert->raw.len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008994 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008995
8996 used += 3 + cert_len;
8997
Gilles Peskine449bd832023-01-11 14:50:10 +01008998 if (used <= buf_len) {
8999 *p++ = MBEDTLS_BYTE_2(cert_len);
9000 *p++ = MBEDTLS_BYTE_1(cert_len);
9001 *p++ = MBEDTLS_BYTE_0(cert_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009002
Gilles Peskine449bd832023-01-11 14:50:10 +01009003 if (session->peer_cert != NULL) {
9004 memcpy(p, session->peer_cert->raw.p, cert_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009005 p += cert_len;
9006 }
9007 }
9008#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009009 if (session->peer_cert_digest != NULL) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009010 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01009011 if (used <= buf_len) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009012 *p++ = (unsigned char) session->peer_cert_digest_type;
9013 *p++ = (unsigned char) session->peer_cert_digest_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01009014 memcpy(p, session->peer_cert_digest,
9015 session->peer_cert_digest_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009016 p += session->peer_cert_digest_len;
9017 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009018 } else {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009019 used += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01009020 if (used <= buf_len) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009021 *p++ = (unsigned char) MBEDTLS_MD_NONE;
9022 *p++ = 0;
9023 }
9024 }
9025#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9026#endif /* MBEDTLS_X509_CRT_PARSE_C */
9027
9028 /*
9029 * Session ticket if any, plus associated data
9030 */
9031#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
9032 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
9033
Gilles Peskine449bd832023-01-11 14:50:10 +01009034 if (used <= buf_len) {
9035 *p++ = MBEDTLS_BYTE_2(session->ticket_len);
9036 *p++ = MBEDTLS_BYTE_1(session->ticket_len);
9037 *p++ = MBEDTLS_BYTE_0(session->ticket_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009038
Gilles Peskine449bd832023-01-11 14:50:10 +01009039 if (session->ticket != NULL) {
9040 memcpy(p, session->ticket, session->ticket_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009041 p += session->ticket_len;
9042 }
9043
Gilles Peskine449bd832023-01-11 14:50:10 +01009044 MBEDTLS_PUT_UINT32_BE(session->ticket_lifetime, p, 0);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009045 p += 4;
9046 }
9047#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
9048
9049 /*
9050 * Misc extension-related info
9051 */
9052#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9053 used += 1;
9054
Gilles Peskine449bd832023-01-11 14:50:10 +01009055 if (used <= buf_len) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009056 *p++ = session->mfl_code;
Gilles Peskine449bd832023-01-11 14:50:10 +01009057 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009058#endif
9059
9060#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
9061 used += 1;
9062
Gilles Peskine449bd832023-01-11 14:50:10 +01009063 if (used <= buf_len) {
9064 *p++ = MBEDTLS_BYTE_0(session->encrypt_then_mac);
9065 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009066#endif
9067
Gilles Peskine449bd832023-01-11 14:50:10 +01009068 return used;
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009069}
9070
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02009071MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01009072static int ssl_tls12_session_load(mbedtls_ssl_session *session,
9073 const unsigned char *buf,
9074 size_t len)
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009075{
9076#if defined(MBEDTLS_HAVE_TIME)
9077 uint64_t start;
9078#endif
9079#if defined(MBEDTLS_X509_CRT_PARSE_C)
9080#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
9081 size_t cert_len;
9082#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9083#endif /* MBEDTLS_X509_CRT_PARSE_C */
9084
9085 const unsigned char *p = buf;
9086 const unsigned char * const end = buf + len;
9087
9088 /*
9089 * Time
9090 */
9091#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01009092 if (8 > (size_t) (end - p)) {
9093 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9094 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009095
Gilles Peskine449bd832023-01-11 14:50:10 +01009096 start = ((uint64_t) p[0] << 56) |
9097 ((uint64_t) p[1] << 48) |
9098 ((uint64_t) p[2] << 40) |
9099 ((uint64_t) p[3] << 32) |
9100 ((uint64_t) p[4] << 24) |
9101 ((uint64_t) p[5] << 16) |
9102 ((uint64_t) p[6] << 8) |
9103 ((uint64_t) p[7]);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009104 p += 8;
9105
9106 session->start = (time_t) start;
9107#endif /* MBEDTLS_HAVE_TIME */
9108
9109 /*
9110 * Basic mandatory fields
9111 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009112 if (2 + 1 + 32 + 48 + 4 > (size_t) (end - p)) {
9113 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9114 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009115
Gilles Peskine449bd832023-01-11 14:50:10 +01009116 session->ciphersuite = (p[0] << 8) | p[1];
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009117 p += 2;
9118
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009119 session->id_len = *p++;
Gilles Peskine449bd832023-01-11 14:50:10 +01009120 memcpy(session->id, p, 32);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009121 p += 32;
9122
Gilles Peskine449bd832023-01-11 14:50:10 +01009123 memcpy(session->master, p, 48);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009124 p += 48;
9125
Gilles Peskine449bd832023-01-11 14:50:10 +01009126 session->verify_result = ((uint32_t) p[0] << 24) |
9127 ((uint32_t) p[1] << 16) |
9128 ((uint32_t) p[2] << 8) |
9129 ((uint32_t) p[3]);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009130 p += 4;
9131
9132 /* Immediately clear invalid pointer values that have been read, in case
9133 * we exit early before we replaced them with valid ones. */
9134#if defined(MBEDTLS_X509_CRT_PARSE_C)
9135#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
9136 session->peer_cert = NULL;
9137#else
9138 session->peer_cert_digest = NULL;
9139#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9140#endif /* MBEDTLS_X509_CRT_PARSE_C */
9141#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
9142 session->ticket = NULL;
9143#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
9144
9145 /*
9146 * Peer certificate
9147 */
9148#if defined(MBEDTLS_X509_CRT_PARSE_C)
9149#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
9150 /* Deserialize CRT from the end of the ticket. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009151 if (3 > (size_t) (end - p)) {
9152 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9153 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009154
Gilles Peskine449bd832023-01-11 14:50:10 +01009155 cert_len = (p[0] << 16) | (p[1] << 8) | p[2];
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009156 p += 3;
9157
Gilles Peskine449bd832023-01-11 14:50:10 +01009158 if (cert_len != 0) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009159 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
9160
Gilles Peskine449bd832023-01-11 14:50:10 +01009161 if (cert_len > (size_t) (end - p)) {
9162 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9163 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009164
Gilles Peskine449bd832023-01-11 14:50:10 +01009165 session->peer_cert = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009166
Gilles Peskine449bd832023-01-11 14:50:10 +01009167 if (session->peer_cert == NULL) {
9168 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
9169 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009170
Gilles Peskine449bd832023-01-11 14:50:10 +01009171 mbedtls_x509_crt_init(session->peer_cert);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009172
Gilles Peskine449bd832023-01-11 14:50:10 +01009173 if ((ret = mbedtls_x509_crt_parse_der(session->peer_cert,
9174 p, cert_len)) != 0) {
9175 mbedtls_x509_crt_free(session->peer_cert);
9176 mbedtls_free(session->peer_cert);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009177 session->peer_cert = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01009178 return ret;
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009179 }
9180
9181 p += cert_len;
9182 }
9183#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9184 /* Deserialize CRT digest from the end of the ticket. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009185 if (2 > (size_t) (end - p)) {
9186 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9187 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009188
9189 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
9190 session->peer_cert_digest_len = (size_t) *p++;
9191
Gilles Peskine449bd832023-01-11 14:50:10 +01009192 if (session->peer_cert_digest_len != 0) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009193 const mbedtls_md_info_t *md_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01009194 mbedtls_md_info_from_type(session->peer_cert_digest_type);
9195 if (md_info == NULL) {
9196 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9197 }
9198 if (session->peer_cert_digest_len != mbedtls_md_get_size(md_info)) {
9199 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9200 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009201
Gilles Peskine449bd832023-01-11 14:50:10 +01009202 if (session->peer_cert_digest_len > (size_t) (end - p)) {
9203 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9204 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009205
9206 session->peer_cert_digest =
Gilles Peskine449bd832023-01-11 14:50:10 +01009207 mbedtls_calloc(1, session->peer_cert_digest_len);
9208 if (session->peer_cert_digest == NULL) {
9209 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
9210 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009211
Gilles Peskine449bd832023-01-11 14:50:10 +01009212 memcpy(session->peer_cert_digest, p,
9213 session->peer_cert_digest_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009214 p += session->peer_cert_digest_len;
9215 }
9216#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9217#endif /* MBEDTLS_X509_CRT_PARSE_C */
9218
9219 /*
9220 * Session ticket and associated data
9221 */
9222#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01009223 if (3 > (size_t) (end - p)) {
9224 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9225 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009226
Gilles Peskine449bd832023-01-11 14:50:10 +01009227 session->ticket_len = (p[0] << 16) | (p[1] << 8) | p[2];
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009228 p += 3;
9229
Gilles Peskine449bd832023-01-11 14:50:10 +01009230 if (session->ticket_len != 0) {
9231 if (session->ticket_len > (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 = mbedtls_calloc(1, session->ticket_len);
9236 if (session->ticket == NULL) {
9237 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
9238 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009239
Gilles Peskine449bd832023-01-11 14:50:10 +01009240 memcpy(session->ticket, p, session->ticket_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009241 p += session->ticket_len;
9242 }
9243
Gilles Peskine449bd832023-01-11 14:50:10 +01009244 if (4 > (size_t) (end - p)) {
9245 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9246 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009247
Gilles Peskine449bd832023-01-11 14:50:10 +01009248 session->ticket_lifetime = ((uint32_t) p[0] << 24) |
9249 ((uint32_t) p[1] << 16) |
9250 ((uint32_t) p[2] << 8) |
9251 ((uint32_t) p[3]);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009252 p += 4;
9253#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
9254
9255 /*
9256 * Misc extension-related info
9257 */
9258#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01009259 if (1 > (size_t) (end - p)) {
9260 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9261 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009262
9263 session->mfl_code = *p++;
9264#endif
9265
9266#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01009267 if (1 > (size_t) (end - p)) {
9268 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9269 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009270
9271 session->encrypt_then_mac = *p++;
9272#endif
9273
9274 /* Done, should have consumed entire buffer */
Gilles Peskine449bd832023-01-11 14:50:10 +01009275 if (p != end) {
9276 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9277 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009278
Gilles Peskine449bd832023-01-11 14:50:10 +01009279 return 0;
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009280}
Jerry Yudc7bd172022-02-17 13:44:15 +08009281#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9282
XiaokangQian75d40ef2022-04-20 11:05:24 +00009283int mbedtls_ssl_validate_ciphersuite(
9284 const mbedtls_ssl_context *ssl,
9285 const mbedtls_ssl_ciphersuite_t *suite_info,
9286 mbedtls_ssl_protocol_version min_tls_version,
Gilles Peskine449bd832023-01-11 14:50:10 +01009287 mbedtls_ssl_protocol_version max_tls_version)
XiaokangQian75d40ef2022-04-20 11:05:24 +00009288{
9289 (void) ssl;
9290
Gilles Peskine449bd832023-01-11 14:50:10 +01009291 if (suite_info == NULL) {
9292 return -1;
9293 }
XiaokangQian75d40ef2022-04-20 11:05:24 +00009294
Gilles Peskine449bd832023-01-11 14:50:10 +01009295 if ((suite_info->min_tls_version > max_tls_version) ||
9296 (suite_info->max_tls_version < min_tls_version)) {
9297 return -1;
XiaokangQian75d40ef2022-04-20 11:05:24 +00009298 }
9299
XiaokangQian060d8672022-04-21 09:24:56 +00009300#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_CLI_C)
XiaokangQian75d40ef2022-04-20 11:05:24 +00009301#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Neil Armstrongca7d5062022-05-31 14:43:23 +02009302#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01009303 if (suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
9304 ssl->handshake->psa_pake_ctx_is_ok != 1)
Neil Armstrongca7d5062022-05-31 14:43:23 +02009305#else
Gilles Peskine449bd832023-01-11 14:50:10 +01009306 if (suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
9307 mbedtls_ecjpake_check(&ssl->handshake->ecjpake_ctx) != 0)
Neil Armstrongca7d5062022-05-31 14:43:23 +02009308#endif /* MBEDTLS_USE_PSA_CRYPTO */
XiaokangQian75d40ef2022-04-20 11:05:24 +00009309 {
Gilles Peskine449bd832023-01-11 14:50:10 +01009310 return -1;
XiaokangQian75d40ef2022-04-20 11:05:24 +00009311 }
9312#endif
9313
9314 /* Don't suggest PSK-based ciphersuite if no PSK is available. */
9315#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01009316 if (mbedtls_ssl_ciphersuite_uses_psk(suite_info) &&
9317 mbedtls_ssl_conf_has_static_psk(ssl->conf) == 0) {
9318 return -1;
XiaokangQian75d40ef2022-04-20 11:05:24 +00009319 }
9320#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
9321#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9322
Gilles Peskine449bd832023-01-11 14:50:10 +01009323 return 0;
XiaokangQian75d40ef2022-04-20 11:05:24 +00009324}
9325
Ronald Crone68ab4f2022-10-05 12:46:29 +02009326#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
XiaokangQianeaf36512022-04-24 09:07:44 +00009327/*
9328 * Function for writing a signature algorithm extension.
9329 *
9330 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
9331 * value (TLS 1.3 RFC8446):
9332 * enum {
9333 * ....
9334 * ecdsa_secp256r1_sha256( 0x0403 ),
9335 * ecdsa_secp384r1_sha384( 0x0503 ),
9336 * ecdsa_secp521r1_sha512( 0x0603 ),
9337 * ....
9338 * } SignatureScheme;
9339 *
9340 * struct {
9341 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
9342 * } SignatureSchemeList;
9343 *
9344 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
9345 * value (TLS 1.2 RFC5246):
9346 * enum {
9347 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
9348 * sha512(6), (255)
9349 * } HashAlgorithm;
9350 *
9351 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
9352 * SignatureAlgorithm;
9353 *
9354 * struct {
9355 * HashAlgorithm hash;
9356 * SignatureAlgorithm signature;
9357 * } SignatureAndHashAlgorithm;
9358 *
9359 * SignatureAndHashAlgorithm
9360 * supported_signature_algorithms<2..2^16-2>;
9361 *
9362 * The TLS 1.3 signature algorithm extension was defined to be a compatible
9363 * generalization of the TLS 1.2 signature algorithm extension.
9364 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
9365 * `SignatureScheme` field of TLS 1.3
9366 *
9367 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009368int mbedtls_ssl_write_sig_alg_ext(mbedtls_ssl_context *ssl, unsigned char *buf,
9369 const unsigned char *end, size_t *out_len)
XiaokangQianeaf36512022-04-24 09:07:44 +00009370{
9371 unsigned char *p = buf;
9372 unsigned char *supported_sig_alg; /* Start of supported_signature_algorithms */
9373 size_t supported_sig_alg_len = 0; /* Length of supported_signature_algorithms */
9374
9375 *out_len = 0;
9376
Gilles Peskine449bd832023-01-11 14:50:10 +01009377 MBEDTLS_SSL_DEBUG_MSG(3, ("adding signature_algorithms extension"));
XiaokangQianeaf36512022-04-24 09:07:44 +00009378
9379 /* Check if we have space for header and length field:
9380 * - extension_type (2 bytes)
9381 * - extension_data_length (2 bytes)
9382 * - supported_signature_algorithms_length (2 bytes)
9383 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009384 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 6);
XiaokangQianeaf36512022-04-24 09:07:44 +00009385 p += 6;
9386
9387 /*
9388 * Write supported_signature_algorithms
9389 */
9390 supported_sig_alg = p;
Gilles Peskine449bd832023-01-11 14:50:10 +01009391 const uint16_t *sig_alg = mbedtls_ssl_get_sig_algs(ssl);
9392 if (sig_alg == NULL) {
9393 return MBEDTLS_ERR_SSL_BAD_CONFIG;
9394 }
XiaokangQianeaf36512022-04-24 09:07:44 +00009395
Gilles Peskine449bd832023-01-11 14:50:10 +01009396 for (; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++) {
9397 MBEDTLS_SSL_DEBUG_MSG(3, ("got signature scheme [%x] %s",
9398 *sig_alg,
9399 mbedtls_ssl_sig_alg_to_str(*sig_alg)));
9400 if (!mbedtls_ssl_sig_alg_is_supported(ssl, *sig_alg)) {
XiaokangQianeaf36512022-04-24 09:07:44 +00009401 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01009402 }
9403 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
9404 MBEDTLS_PUT_UINT16_BE(*sig_alg, p, 0);
XiaokangQianeaf36512022-04-24 09:07:44 +00009405 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01009406 MBEDTLS_SSL_DEBUG_MSG(3, ("sent signature scheme [%x] %s",
9407 *sig_alg,
9408 mbedtls_ssl_sig_alg_to_str(*sig_alg)));
XiaokangQianeaf36512022-04-24 09:07:44 +00009409 }
9410
9411 /* Length of supported_signature_algorithms */
9412 supported_sig_alg_len = p - supported_sig_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01009413 if (supported_sig_alg_len == 0) {
9414 MBEDTLS_SSL_DEBUG_MSG(1, ("No signature algorithms defined."));
9415 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
XiaokangQianeaf36512022-04-24 09:07:44 +00009416 }
9417
Gilles Peskine449bd832023-01-11 14:50:10 +01009418 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SIG_ALG, buf, 0);
9419 MBEDTLS_PUT_UINT16_BE(supported_sig_alg_len + 2, buf, 2);
9420 MBEDTLS_PUT_UINT16_BE(supported_sig_alg_len, buf, 4);
XiaokangQianeaf36512022-04-24 09:07:44 +00009421
XiaokangQianeaf36512022-04-24 09:07:44 +00009422 *out_len = p - buf;
9423
9424#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01009425 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_SIG_ALG);
XiaokangQianeaf36512022-04-24 09:07:44 +00009426#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu0c354a22022-08-29 15:25:36 +08009427
Gilles Peskine449bd832023-01-11 14:50:10 +01009428 return 0;
XiaokangQianeaf36512022-04-24 09:07:44 +00009429}
Ronald Crone68ab4f2022-10-05 12:46:29 +02009430#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
XiaokangQianeaf36512022-04-24 09:07:44 +00009431
XiaokangQian40a35232022-05-07 09:02:40 +00009432#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
XiaokangQian9b2b7712022-05-17 02:57:00 +00009433/*
9434 * mbedtls_ssl_parse_server_name_ext
9435 *
9436 * Structure of server_name extension:
9437 *
9438 * enum {
9439 * host_name(0), (255)
9440 * } NameType;
9441 * opaque HostName<1..2^16-1>;
9442 *
9443 * struct {
9444 * NameType name_type;
9445 * select (name_type) {
9446 * case host_name: HostName;
9447 * } name;
9448 * } ServerName;
9449 * struct {
9450 * ServerName server_name_list<1..2^16-1>
9451 * } ServerNameList;
9452 */
Ronald Cronce7d76e2022-07-08 18:56:49 +02009453MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01009454int mbedtls_ssl_parse_server_name_ext(mbedtls_ssl_context *ssl,
9455 const unsigned char *buf,
9456 const unsigned char *end)
XiaokangQian40a35232022-05-07 09:02:40 +00009457{
9458 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
9459 const unsigned char *p = buf;
XiaokangQian9b2b7712022-05-17 02:57:00 +00009460 size_t server_name_list_len, hostname_len;
9461 const unsigned char *server_name_list_end;
XiaokangQian40a35232022-05-07 09:02:40 +00009462
Gilles Peskine449bd832023-01-11 14:50:10 +01009463 MBEDTLS_SSL_DEBUG_MSG(3, ("parse ServerName extension"));
XiaokangQian40a35232022-05-07 09:02:40 +00009464
Gilles Peskine449bd832023-01-11 14:50:10 +01009465 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
9466 server_name_list_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian40a35232022-05-07 09:02:40 +00009467 p += 2;
9468
Gilles Peskine449bd832023-01-11 14:50:10 +01009469 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, server_name_list_len);
XiaokangQian9b2b7712022-05-17 02:57:00 +00009470 server_name_list_end = p + server_name_list_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01009471 while (p < server_name_list_end) {
9472 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, server_name_list_end, 3);
9473 hostname_len = MBEDTLS_GET_UINT16_BE(p, 1);
9474 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, server_name_list_end,
9475 hostname_len + 3);
XiaokangQian40a35232022-05-07 09:02:40 +00009476
Gilles Peskine449bd832023-01-11 14:50:10 +01009477 if (p[0] == MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME) {
XiaokangQian75fe8c72022-06-15 09:42:45 +00009478 /* sni_name is intended to be used only during the parsing of the
9479 * ClientHello message (it is reset to NULL before the end of
9480 * the message parsing). Thus it is ok to just point to the
9481 * reception buffer and not make a copy of it.
9482 */
XiaokangQianf2a94202022-05-20 06:44:24 +00009483 ssl->handshake->sni_name = p + 3;
9484 ssl->handshake->sni_name_len = hostname_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01009485 if (ssl->conf->f_sni == NULL) {
9486 return 0;
XiaokangQian40a35232022-05-07 09:02:40 +00009487 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009488 ret = ssl->conf->f_sni(ssl->conf->p_sni,
9489 ssl, p + 3, hostname_len);
9490 if (ret != 0) {
9491 MBEDTLS_SSL_DEBUG_RET(1, "ssl_sni_wrapper", ret);
9492 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_UNRECOGNIZED_NAME,
9493 MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME);
9494 return MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME;
9495 }
9496 return 0;
XiaokangQian40a35232022-05-07 09:02:40 +00009497 }
9498
9499 p += hostname_len + 3;
9500 }
9501
Gilles Peskine449bd832023-01-11 14:50:10 +01009502 return 0;
XiaokangQian40a35232022-05-07 09:02:40 +00009503}
9504#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
9505
XiaokangQianacb39922022-06-17 10:18:48 +00009506#if defined(MBEDTLS_SSL_ALPN)
Ronald Cronce7d76e2022-07-08 18:56:49 +02009507MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01009508int mbedtls_ssl_parse_alpn_ext(mbedtls_ssl_context *ssl,
9509 const unsigned char *buf,
9510 const unsigned char *end)
XiaokangQianacb39922022-06-17 10:18:48 +00009511{
9512 const unsigned char *p = buf;
XiaokangQianc7403452022-06-23 03:24:12 +00009513 size_t protocol_name_list_len;
XiaokangQian95d5f542022-06-24 02:29:26 +00009514 const unsigned char *protocol_name_list;
9515 const unsigned char *protocol_name_list_end;
XiaokangQianc7403452022-06-23 03:24:12 +00009516 size_t protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009517
9518 /* If ALPN not configured, just ignore the extension */
Gilles Peskine449bd832023-01-11 14:50:10 +01009519 if (ssl->conf->alpn_list == NULL) {
9520 return 0;
9521 }
XiaokangQianacb39922022-06-17 10:18:48 +00009522
9523 /*
XiaokangQianc7403452022-06-23 03:24:12 +00009524 * RFC7301, section 3.1
9525 * opaque ProtocolName<1..2^8-1>;
XiaokangQianacb39922022-06-17 10:18:48 +00009526 *
XiaokangQianc7403452022-06-23 03:24:12 +00009527 * struct {
9528 * ProtocolName protocol_name_list<2..2^16-1>
9529 * } ProtocolNameList;
XiaokangQianacb39922022-06-17 10:18:48 +00009530 */
9531
XiaokangQianc7403452022-06-23 03:24:12 +00009532 /*
XiaokangQian0b776e22022-06-24 09:04:59 +00009533 * protocol_name_list_len 2 bytes
9534 * protocol_name_len 1 bytes
9535 * protocol_name >=1 byte
XiaokangQianc7403452022-06-23 03:24:12 +00009536 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009537 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 4);
XiaokangQianacb39922022-06-17 10:18:48 +00009538
Gilles Peskine449bd832023-01-11 14:50:10 +01009539 protocol_name_list_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQianacb39922022-06-17 10:18:48 +00009540 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01009541 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, protocol_name_list_len);
XiaokangQian95d5f542022-06-24 02:29:26 +00009542 protocol_name_list = p;
9543 protocol_name_list_end = p + protocol_name_list_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009544
9545 /* Validate peer's list (lengths) */
Gilles Peskine449bd832023-01-11 14:50:10 +01009546 while (p < protocol_name_list_end) {
XiaokangQian95d5f542022-06-24 02:29:26 +00009547 protocol_name_len = *p++;
Gilles Peskine449bd832023-01-11 14:50:10 +01009548 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, protocol_name_list_end,
9549 protocol_name_len);
9550 if (protocol_name_len == 0) {
XiaokangQian95d5f542022-06-24 02:29:26 +00009551 MBEDTLS_SSL_PEND_FATAL_ALERT(
9552 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
Gilles Peskine449bd832023-01-11 14:50:10 +01009553 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
9554 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
XiaokangQian95d5f542022-06-24 02:29:26 +00009555 }
9556
9557 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009558 }
9559
9560 /* Use our order of preference */
Gilles Peskine449bd832023-01-11 14:50:10 +01009561 for (const char **alpn = ssl->conf->alpn_list; *alpn != NULL; alpn++) {
9562 size_t const alpn_len = strlen(*alpn);
XiaokangQian95d5f542022-06-24 02:29:26 +00009563 p = protocol_name_list;
Gilles Peskine449bd832023-01-11 14:50:10 +01009564 while (p < protocol_name_list_end) {
XiaokangQian95d5f542022-06-24 02:29:26 +00009565 protocol_name_len = *p++;
Gilles Peskine449bd832023-01-11 14:50:10 +01009566 if (protocol_name_len == alpn_len &&
9567 memcmp(p, *alpn, alpn_len) == 0) {
XiaokangQianacb39922022-06-17 10:18:48 +00009568 ssl->alpn_chosen = *alpn;
Gilles Peskine449bd832023-01-11 14:50:10 +01009569 return 0;
XiaokangQianacb39922022-06-17 10:18:48 +00009570 }
XiaokangQian95d5f542022-06-24 02:29:26 +00009571
9572 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009573 }
9574 }
9575
XiaokangQian95d5f542022-06-24 02:29:26 +00009576 /* If we get here, no match was found */
XiaokangQianacb39922022-06-17 10:18:48 +00009577 MBEDTLS_SSL_PEND_FATAL_ALERT(
Gilles Peskine449bd832023-01-11 14:50:10 +01009578 MBEDTLS_SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL,
9579 MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL);
9580 return MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL;
XiaokangQianacb39922022-06-17 10:18:48 +00009581}
9582
Gilles Peskine449bd832023-01-11 14:50:10 +01009583int mbedtls_ssl_write_alpn_ext(mbedtls_ssl_context *ssl,
9584 unsigned char *buf,
9585 unsigned char *end,
9586 size_t *out_len)
XiaokangQianacb39922022-06-17 10:18:48 +00009587{
9588 unsigned char *p = buf;
XiaokangQian95d5f542022-06-24 02:29:26 +00009589 size_t protocol_name_len;
XiaokangQianc7403452022-06-23 03:24:12 +00009590 *out_len = 0;
XiaokangQianacb39922022-06-17 10:18:48 +00009591
Gilles Peskine449bd832023-01-11 14:50:10 +01009592 if (ssl->alpn_chosen == NULL) {
9593 return 0;
XiaokangQianacb39922022-06-17 10:18:48 +00009594 }
9595
Gilles Peskine449bd832023-01-11 14:50:10 +01009596 protocol_name_len = strlen(ssl->alpn_chosen);
9597 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 7 + protocol_name_len);
XiaokangQianacb39922022-06-17 10:18:48 +00009598
Gilles Peskine449bd832023-01-11 14:50:10 +01009599 MBEDTLS_SSL_DEBUG_MSG(3, ("server side, adding alpn extension"));
XiaokangQianacb39922022-06-17 10:18:48 +00009600 /*
9601 * 0 . 1 ext identifier
9602 * 2 . 3 ext length
9603 * 4 . 5 protocol list length
9604 * 6 . 6 protocol name length
9605 * 7 . 7+n protocol name
9606 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009607 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_ALPN, p, 0);
XiaokangQianacb39922022-06-17 10:18:48 +00009608
XiaokangQian95d5f542022-06-24 02:29:26 +00009609 *out_len = 7 + protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009610
Gilles Peskine449bd832023-01-11 14:50:10 +01009611 MBEDTLS_PUT_UINT16_BE(protocol_name_len + 3, p, 2);
9612 MBEDTLS_PUT_UINT16_BE(protocol_name_len + 1, p, 4);
XiaokangQian0b776e22022-06-24 09:04:59 +00009613 /* Note: the length of the chosen protocol has been checked to be less
9614 * than 255 bytes in `mbedtls_ssl_conf_alpn_protocols`.
9615 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009616 p[6] = MBEDTLS_BYTE_0(protocol_name_len);
XiaokangQianacb39922022-06-17 10:18:48 +00009617
Gilles Peskine449bd832023-01-11 14:50:10 +01009618 memcpy(p + 7, ssl->alpn_chosen, protocol_name_len);
Jerry Yub95dd362022-11-08 21:19:34 +08009619
9620#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01009621 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_ALPN);
Jerry Yub95dd362022-11-08 21:19:34 +08009622#endif
9623
Gilles Peskine449bd832023-01-11 14:50:10 +01009624 return 0;
XiaokangQianacb39922022-06-17 10:18:48 +00009625}
9626#endif /* MBEDTLS_SSL_ALPN */
9627
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009628#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
Xiaokang Qian03409292022-10-12 02:49:52 +00009629 defined(MBEDTLS_SSL_SESSION_TICKETS) && \
Xiaokang Qianed0620c2022-10-12 06:58:13 +00009630 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \
Xiaokang Qianed3afcd2022-10-12 08:31:11 +00009631 defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01009632int mbedtls_ssl_session_set_hostname(mbedtls_ssl_session *session,
9633 const char *hostname)
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009634{
9635 /* Initialize to suppress unnecessary compiler warning */
9636 size_t hostname_len = 0;
9637
9638 /* Check if new hostname is valid before
9639 * making any change to current one */
Gilles Peskine449bd832023-01-11 14:50:10 +01009640 if (hostname != NULL) {
9641 hostname_len = strlen(hostname);
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009642
Gilles Peskine449bd832023-01-11 14:50:10 +01009643 if (hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN) {
9644 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9645 }
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009646 }
9647
9648 /* Now it's clear that we will overwrite the old hostname,
9649 * so we can free it safely */
Gilles Peskine449bd832023-01-11 14:50:10 +01009650 if (session->hostname != NULL) {
9651 mbedtls_platform_zeroize(session->hostname,
9652 strlen(session->hostname));
9653 mbedtls_free(session->hostname);
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009654 }
9655
9656 /* Passing NULL as hostname shall clear the old one */
Gilles Peskine449bd832023-01-11 14:50:10 +01009657 if (hostname == NULL) {
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009658 session->hostname = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01009659 } else {
9660 session->hostname = mbedtls_calloc(1, hostname_len + 1);
9661 if (session->hostname == NULL) {
9662 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
9663 }
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009664
Gilles Peskine449bd832023-01-11 14:50:10 +01009665 memcpy(session->hostname, hostname, hostname_len);
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009666 }
9667
Gilles Peskine449bd832023-01-11 14:50:10 +01009668 return 0;
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009669}
Xiaokang Qian03409292022-10-12 02:49:52 +00009670#endif /* MBEDTLS_SSL_PROTO_TLS1_3 &&
9671 MBEDTLS_SSL_SESSION_TICKETS &&
Xiaokang Qianed0620c2022-10-12 06:58:13 +00009672 MBEDTLS_SSL_SERVER_NAME_INDICATION &&
Xiaokang Qianed3afcd2022-10-12 08:31:11 +00009673 MBEDTLS_SSL_CLI_C */
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009674
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009675#endif /* MBEDTLS_SSL_TLS_C */