blob: f7357a9ccef2cef2e787cdadf4ad2b21c22be2d2 [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é-Gonnardb72ff492023-02-06 09:54:49 +0100851 ret = mbedtls_sha256_starts(&ssl->handshake->fin_sha256, 0);
852 if (ret != 0) {
853 return ret;
854 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800855#endif
856#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400857#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800858#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100859 status = psa_hash_abort(&ssl->handshake->fin_sha384_psa);
860 if (status != PSA_SUCCESS) {
Andrzej Kurekdaf5b562023-03-03 05:52:28 -0500861 return PSA_TO_MD_ERR(status);
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100862 }
863 status = psa_hash_setup(&ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384);
864 if (status != PSA_SUCCESS) {
Andrzej Kurekdaf5b562023-03-03 05:52:28 -0500865 return PSA_TO_MD_ERR(status);
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100866 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800867#else
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100868 ret = mbedtls_sha512_starts(&ssl->handshake->fin_sha384, 1);
869 if (ret != 0) {
870 return ret;
871 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800872#endif
873#endif
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100874 return 0;
Jerry Yuc73c6182022-02-08 20:29:25 +0800875}
876
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100877static int ssl_update_checksum_start(mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100878 const unsigned char *buf, size_t len)
Jerry Yuc73c6182022-02-08 20:29:25 +0800879{
Manuel Pégourié-Gonnard626aaed2023-02-06 22:03:06 +0100880#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) || \
881 defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100882#if defined(MBEDTLS_USE_PSA_CRYPTO)
883 psa_status_t status;
884#else
885 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
886#endif
Manuel Pégourié-Gonnard626aaed2023-02-06 22:03:06 +0100887#else /* SHA-256 or SHA-384 */
888 ((void) ssl);
889 (void) buf;
890 (void) len;
891#endif /* SHA-256 or SHA-384 */
Andrzej Kurek25f27152022-08-17 16:09:31 -0400892#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800893#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100894 status = psa_hash_update(&ssl->handshake->fin_sha256_psa, buf, len);
895 if (status != PSA_SUCCESS) {
Andrzej Kurekdaf5b562023-03-03 05:52:28 -0500896 return PSA_TO_MD_ERR(status);
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100897 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800898#else
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100899 ret = mbedtls_sha256_update(&ssl->handshake->fin_sha256, buf, len);
900 if (ret != 0) {
901 return ret;
902 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800903#endif
904#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400905#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800906#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100907 status = psa_hash_update(&ssl->handshake->fin_sha384_psa, buf, len);
908 if (status != PSA_SUCCESS) {
Andrzej Kurekdaf5b562023-03-03 05:52:28 -0500909 return PSA_TO_MD_ERR(status);
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100910 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800911#else
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100912 ret = mbedtls_sha512_update(&ssl->handshake->fin_sha384, buf, len);
913 if (ret != 0) {
914 return ret;
915 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800916#endif
917#endif
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100918 return 0;
Jerry Yuc73c6182022-02-08 20:29:25 +0800919}
920
Andrzej Kurek25f27152022-08-17 16:09:31 -0400921#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100922static int ssl_update_checksum_sha256(mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100923 const unsigned char *buf, size_t len)
Jerry Yuc73c6182022-02-08 20:29:25 +0800924{
925#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurekdaf5b562023-03-03 05:52:28 -0500926 return PSA_TO_MD_ERR(psa_hash_update(
927 &ssl->handshake->fin_sha256_psa, buf, len));
Jerry Yuc73c6182022-02-08 20:29:25 +0800928#else
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100929 return mbedtls_sha256_update(&ssl->handshake->fin_sha256, buf, len);
Jerry Yuc73c6182022-02-08 20:29:25 +0800930#endif
931}
932#endif
933
Andrzej Kurek25f27152022-08-17 16:09:31 -0400934#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100935static int ssl_update_checksum_sha384(mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100936 const unsigned char *buf, size_t len)
Jerry Yuc73c6182022-02-08 20:29:25 +0800937{
938#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurekdaf5b562023-03-03 05:52:28 -0500939 return PSA_TO_MD_ERR(psa_hash_update(
940 &ssl->handshake->fin_sha384_psa, buf, len));
Jerry Yuc73c6182022-02-08 20:29:25 +0800941#else
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100942 return mbedtls_sha512_update(&ssl->handshake->fin_sha384, buf, len);
Jerry Yuc73c6182022-02-08 20:29:25 +0800943#endif
944}
945#endif
946
Gilles Peskine449bd832023-01-11 14:50:10 +0100947static void ssl_handshake_params_init(mbedtls_ssl_handshake_params *handshake)
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200948{
Gilles Peskine449bd832023-01-11 14:50:10 +0100949 memset(handshake, 0, sizeof(mbedtls_ssl_handshake_params));
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200950
Andrzej Kurek25f27152022-08-17 16:09:31 -0400951#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -0500952#if defined(MBEDTLS_USE_PSA_CRYPTO)
953 handshake->fin_sha256_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -0500954#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100955 mbedtls_sha256_init(&handshake->fin_sha256);
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200956#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -0500957#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400958#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -0500959#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -0500960 handshake->fin_sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -0500961#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100962 mbedtls_sha512_init(&handshake->fin_sha384);
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200963#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -0500964#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200965
966 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +0100967
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200968#if defined(MBEDTLS_DHM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100969 mbedtls_dhm_init(&handshake->dhm_ctx);
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200970#endif
Neil Armstrongf3f46412022-04-12 14:43:39 +0200971#if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100972 mbedtls_ecdh_init(&handshake->ecdh_ctx);
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200973#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +0200974#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Neil Armstrongca7d5062022-05-31 14:43:23 +0200975#if defined(MBEDTLS_USE_PSA_CRYPTO)
976 handshake->psa_pake_ctx = psa_pake_operation_init();
977 handshake->psa_pake_password = MBEDTLS_SVC_KEY_ID_INIT;
978#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100979 mbedtls_ecjpake_init(&handshake->ecjpake_ctx);
Neil Armstrongca7d5062022-05-31 14:43:23 +0200980#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +0200981#if defined(MBEDTLS_SSL_CLI_C)
982 handshake->ecjpake_cache = NULL;
983 handshake->ecjpake_cache_len = 0;
984#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +0200985#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +0200986
Gilles Peskineeccd8882020-03-10 12:19:08 +0100987#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100988 mbedtls_x509_crt_restart_init(&handshake->ecrs_ctx);
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +0200989#endif
990
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +0200991#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
992 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
993#endif
Hanno Becker75173122019-02-06 16:18:31 +0000994
995#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
996 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100997 mbedtls_pk_init(&handshake->peer_pubkey);
Hanno Becker75173122019-02-06 16:18:31 +0000998#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200999}
1000
Gilles Peskine449bd832023-01-11 14:50:10 +01001001void mbedtls_ssl_transform_init(mbedtls_ssl_transform *transform)
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001002{
Gilles Peskine449bd832023-01-11 14:50:10 +01001003 memset(transform, 0, sizeof(mbedtls_ssl_transform));
Paul Bakker84bbeb52014-07-01 14:53:22 +02001004
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +01001005#if defined(MBEDTLS_USE_PSA_CRYPTO)
1006 transform->psa_key_enc = MBEDTLS_SVC_KEY_ID_INIT;
1007 transform->psa_key_dec = MBEDTLS_SVC_KEY_ID_INIT;
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01001008#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001009 mbedtls_cipher_init(&transform->cipher_ctx_enc);
1010 mbedtls_cipher_init(&transform->cipher_ctx_dec);
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +01001011#endif
1012
Hanno Beckerfd86ca82020-11-30 08:54:23 +00001013#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong39b8e7d2022-02-23 09:24:45 +01001014#if defined(MBEDTLS_USE_PSA_CRYPTO)
1015 transform->psa_mac_enc = MBEDTLS_SVC_KEY_ID_INIT;
1016 transform->psa_mac_dec = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrongcf8841a2022-02-24 11:17:45 +01001017#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001018 mbedtls_md_init(&transform->md_ctx_enc);
1019 mbedtls_md_init(&transform->md_ctx_dec);
Hanno Beckerd56ed242018-01-03 15:32:51 +00001020#endif
Neil Armstrongcf8841a2022-02-24 11:17:45 +01001021#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001022}
1023
Gilles Peskine449bd832023-01-11 14:50:10 +01001024void mbedtls_ssl_session_init(mbedtls_ssl_session *session)
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001025{
Gilles Peskine449bd832023-01-11 14:50:10 +01001026 memset(session, 0, sizeof(mbedtls_ssl_session));
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001027}
1028
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001029MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001030static int ssl_handshake_init(mbedtls_ssl_context *ssl)
Paul Bakker48916f92012-09-16 19:57:18 +00001031{
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001032 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1033
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001034 /* Clear old handshake information if present */
Jerry Yu2e199812022-12-01 18:57:19 +08001035#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01001036 if (ssl->transform_negotiate) {
1037 mbedtls_ssl_transform_free(ssl->transform_negotiate);
1038 }
Jerry Yu2e199812022-12-01 18:57:19 +08001039#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001040 if (ssl->session_negotiate) {
1041 mbedtls_ssl_session_free(ssl->session_negotiate);
1042 }
1043 if (ssl->handshake) {
1044 mbedtls_ssl_handshake_free(ssl);
1045 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001046
Jerry Yu2e199812022-12-01 18:57:19 +08001047#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001048 /*
1049 * Either the pointers are now NULL or cleared properly and can be freed.
1050 * Now allocate missing structures.
1051 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001052 if (ssl->transform_negotiate == NULL) {
1053 ssl->transform_negotiate = mbedtls_calloc(1, sizeof(mbedtls_ssl_transform));
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001054 }
Jerry Yu2e199812022-12-01 18:57:19 +08001055#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker48916f92012-09-16 19:57:18 +00001056
Gilles Peskine449bd832023-01-11 14:50:10 +01001057 if (ssl->session_negotiate == NULL) {
1058 ssl->session_negotiate = mbedtls_calloc(1, sizeof(mbedtls_ssl_session));
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001059 }
Paul Bakker48916f92012-09-16 19:57:18 +00001060
Gilles Peskine449bd832023-01-11 14:50:10 +01001061 if (ssl->handshake == NULL) {
1062 ssl->handshake = mbedtls_calloc(1, sizeof(mbedtls_ssl_handshake_params));
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001063 }
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05001064#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1065 /* If the buffers are too small - reallocate */
Andrzej Kurek8ea68722020-04-03 06:40:47 -04001066
Gilles Peskine449bd832023-01-11 14:50:10 +01001067 handle_buffer_resizing(ssl, 0, MBEDTLS_SSL_IN_BUFFER_LEN,
1068 MBEDTLS_SSL_OUT_BUFFER_LEN);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05001069#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001070
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001071 /* All pointers should exist and can be directly freed without issue */
Gilles Peskine449bd832023-01-11 14:50:10 +01001072 if (ssl->handshake == NULL ||
Jerry Yu2e199812022-12-01 18:57:19 +08001073#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker48916f92012-09-16 19:57:18 +00001074 ssl->transform_negotiate == NULL ||
Jerry Yu2e199812022-12-01 18:57:19 +08001075#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001076 ssl->session_negotiate == NULL) {
1077 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc() of ssl sub-contexts failed"));
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001078
Gilles Peskine449bd832023-01-11 14:50:10 +01001079 mbedtls_free(ssl->handshake);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001080 ssl->handshake = NULL;
Jerry Yu2e199812022-12-01 18:57:19 +08001081
1082#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01001083 mbedtls_free(ssl->transform_negotiate);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001084 ssl->transform_negotiate = NULL;
Jerry Yu2e199812022-12-01 18:57:19 +08001085#endif
1086
Gilles Peskine449bd832023-01-11 14:50:10 +01001087 mbedtls_free(ssl->session_negotiate);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001088 ssl->session_negotiate = NULL;
1089
Gilles Peskine449bd832023-01-11 14:50:10 +01001090 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Paul Bakker48916f92012-09-16 19:57:18 +00001091 }
1092
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001093 /* Initialize structures */
Gilles Peskine449bd832023-01-11 14:50:10 +01001094 mbedtls_ssl_session_init(ssl->session_negotiate);
1095 ssl_handshake_params_init(ssl->handshake);
Paul Bakker968afaa2014-07-09 11:09:24 +02001096
Jerry Yu2e199812022-12-01 18:57:19 +08001097#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01001098 mbedtls_ssl_transform_init(ssl->transform_negotiate);
Jerry Yu2e199812022-12-01 18:57:19 +08001099#endif
1100
Manuel Pégourié-Gonnard537f2312023-02-05 10:17:45 +01001101 /* Setup handshake checksums */
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001102 ret = mbedtls_ssl_reset_checksum(ssl);
1103 if (ret != 0) {
1104 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_reset_checksum", ret);
1105 return ret;
1106 }
Manuel Pégourié-Gonnard537f2312023-02-05 10:17:45 +01001107
Jerry Yud0766ec2022-09-22 10:46:57 +08001108#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
1109 defined(MBEDTLS_SSL_SRV_C) && \
1110 defined(MBEDTLS_SSL_SESSION_TICKETS)
1111 ssl->handshake->new_session_tickets_count =
Gilles Peskine449bd832023-01-11 14:50:10 +01001112 ssl->conf->new_session_tickets_count;
Jerry Yud0766ec2022-09-22 10:46:57 +08001113#endif
1114
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001115#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001116 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001117 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001118
Gilles Peskine449bd832023-01-11 14:50:10 +01001119 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001120 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Gilles Peskine449bd832023-01-11 14:50:10 +01001121 } else {
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001122 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Gilles Peskine449bd832023-01-11 14:50:10 +01001123 }
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001124
Gilles Peskine449bd832023-01-11 14:50:10 +01001125 mbedtls_ssl_set_timer(ssl, 0);
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001126 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001127#endif
1128
Brett Warrene0edc842021-08-17 09:53:13 +01001129/*
1130 * curve_list is translated to IANA TLS group identifiers here because
1131 * mbedtls_ssl_conf_curves returns void and so can't return
1132 * any error codes.
1133 */
1134#if defined(MBEDTLS_ECP_C)
1135#if !defined(MBEDTLS_DEPRECATED_REMOVED)
1136 /* Heap allocate and translate curve_list from internal to IANA group ids */
Gilles Peskine449bd832023-01-11 14:50:10 +01001137 if (ssl->conf->curve_list != NULL) {
Brett Warrene0edc842021-08-17 09:53:13 +01001138 size_t length;
1139 const mbedtls_ecp_group_id *curve_list = ssl->conf->curve_list;
1140
Gilles Peskine449bd832023-01-11 14:50:10 +01001141 for (length = 0; (curve_list[length] != MBEDTLS_ECP_DP_NONE) &&
1142 (length < MBEDTLS_ECP_DP_MAX); length++) {
1143 }
Brett Warrene0edc842021-08-17 09:53:13 +01001144
1145 /* Leave room for zero termination */
Gilles Peskine449bd832023-01-11 14:50:10 +01001146 uint16_t *group_list = mbedtls_calloc(length + 1, sizeof(uint16_t));
1147 if (group_list == NULL) {
1148 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
1149 }
Brett Warrene0edc842021-08-17 09:53:13 +01001150
Gilles Peskine449bd832023-01-11 14:50:10 +01001151 for (size_t i = 0; i < length; i++) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01001152 uint16_t tls_id = mbedtls_ssl_get_tls_id_from_ecp_group_id(
Gilles Peskine449bd832023-01-11 14:50:10 +01001153 curve_list[i]);
1154 if (tls_id == 0) {
1155 mbedtls_free(group_list);
1156 return MBEDTLS_ERR_SSL_BAD_CONFIG;
Brett Warrene0edc842021-08-17 09:53:13 +01001157 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01001158 group_list[i] = tls_id;
Brett Warrene0edc842021-08-17 09:53:13 +01001159 }
1160
1161 group_list[length] = 0;
1162
1163 ssl->handshake->group_list = group_list;
1164 ssl->handshake->group_list_heap_allocated = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001165 } else {
Brett Warrene0edc842021-08-17 09:53:13 +01001166 ssl->handshake->group_list = ssl->conf->group_list;
1167 ssl->handshake->group_list_heap_allocated = 0;
1168 }
1169#endif /* MBEDTLS_DEPRECATED_REMOVED */
1170#endif /* MBEDTLS_ECP_C */
1171
Ronald Crone68ab4f2022-10-05 12:46:29 +02001172#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +08001173#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Jerry Yua69269a2022-01-17 21:06:01 +08001174#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yu713013f2022-01-17 18:16:35 +08001175 /* Heap allocate and translate sig_hashes from internal hash identifiers to
1176 signature algorithms IANA identifiers. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001177 if (mbedtls_ssl_conf_is_tls12_only(ssl->conf) &&
1178 ssl->conf->sig_hashes != NULL) {
Jerry Yuf017ee42022-01-12 15:49:48 +08001179 const int *md;
1180 const int *sig_hashes = ssl->conf->sig_hashes;
Jerry Yub476a442022-01-21 18:14:45 +08001181 size_t sig_algs_len = 0;
Jerry Yuf017ee42022-01-12 15:49:48 +08001182 uint16_t *p;
1183
Jerry Yub476a442022-01-21 18:14:45 +08001184#if defined(static_assert)
Gilles Peskine449bd832023-01-11 14:50:10 +01001185 static_assert(MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN
1186 <= (SIZE_MAX - (2 * sizeof(uint16_t))),
1187 "MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN too big");
Jerry Yua68dca22022-01-20 16:28:27 +08001188#endif
1189
Gilles Peskine449bd832023-01-11 14:50:10 +01001190 for (md = sig_hashes; *md != MBEDTLS_MD_NONE; md++) {
1191 if (mbedtls_ssl_hash_from_md_alg(*md) == MBEDTLS_SSL_HASH_NONE) {
Jerry Yuf017ee42022-01-12 15:49:48 +08001192 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001193 }
Jerry Yub476a442022-01-21 18:14:45 +08001194#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001195 sig_algs_len += sizeof(uint16_t);
Jerry Yub476a442022-01-21 18:14:45 +08001196#endif
Jerry Yua68dca22022-01-20 16:28:27 +08001197
Jerry Yub476a442022-01-21 18:14:45 +08001198#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001199 sig_algs_len += sizeof(uint16_t);
Jerry Yub476a442022-01-21 18:14:45 +08001200#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001201 if (sig_algs_len > MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN) {
1202 return MBEDTLS_ERR_SSL_BAD_CONFIG;
1203 }
Jerry Yuf017ee42022-01-12 15:49:48 +08001204 }
1205
Gilles Peskine449bd832023-01-11 14:50:10 +01001206 if (sig_algs_len < MBEDTLS_SSL_MIN_SIG_ALG_LIST_LEN) {
1207 return MBEDTLS_ERR_SSL_BAD_CONFIG;
1208 }
Jerry Yuf017ee42022-01-12 15:49:48 +08001209
Gilles Peskine449bd832023-01-11 14:50:10 +01001210 ssl->handshake->sig_algs = mbedtls_calloc(1, sig_algs_len +
1211 sizeof(uint16_t));
1212 if (ssl->handshake->sig_algs == NULL) {
1213 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
1214 }
Jerry Yuf017ee42022-01-12 15:49:48 +08001215
Gilles Peskine449bd832023-01-11 14:50:10 +01001216 p = (uint16_t *) ssl->handshake->sig_algs;
1217 for (md = sig_hashes; *md != MBEDTLS_MD_NONE; md++) {
1218 unsigned char hash = mbedtls_ssl_hash_from_md_alg(*md);
1219 if (hash == MBEDTLS_SSL_HASH_NONE) {
Jerry Yuf017ee42022-01-12 15:49:48 +08001220 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001221 }
Jerry Yu6106fdc2022-01-12 16:36:14 +08001222#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001223 *p = ((hash << 8) | MBEDTLS_SSL_SIG_ECDSA);
Jerry Yuf017ee42022-01-12 15:49:48 +08001224 p++;
Jerry Yu6106fdc2022-01-12 16:36:14 +08001225#endif
1226#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001227 *p = ((hash << 8) | MBEDTLS_SSL_SIG_RSA);
Jerry Yuf017ee42022-01-12 15:49:48 +08001228 p++;
Jerry Yu6106fdc2022-01-12 16:36:14 +08001229#endif
Jerry Yuf017ee42022-01-12 15:49:48 +08001230 }
Gabor Mezei15b95a62022-05-09 16:37:58 +02001231 *p = MBEDTLS_TLS_SIG_NONE;
Jerry Yuf017ee42022-01-12 15:49:48 +08001232 ssl->handshake->sig_algs_heap_allocated = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001233 } else
Jerry Yua69269a2022-01-17 21:06:01 +08001234#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Jerry Yuf017ee42022-01-12 15:49:48 +08001235 {
Jerry Yuf017ee42022-01-12 15:49:48 +08001236 ssl->handshake->sig_algs_heap_allocated = 0;
1237 }
Jerry Yucc539102022-06-27 16:27:35 +08001238#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Ronald Crone68ab4f2022-10-05 12:46:29 +02001239#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Gilles Peskine449bd832023-01-11 14:50:10 +01001240 return 0;
Paul Bakker48916f92012-09-16 19:57:18 +00001241}
1242
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001243#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001244/* Dummy cookie callbacks for defaults */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001245MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001246static int ssl_cookie_write_dummy(void *ctx,
1247 unsigned char **p, unsigned char *end,
1248 const unsigned char *cli_id, size_t cli_id_len)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001249{
1250 ((void) ctx);
1251 ((void) p);
1252 ((void) end);
1253 ((void) cli_id);
1254 ((void) cli_id_len);
1255
Gilles Peskine449bd832023-01-11 14:50:10 +01001256 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001257}
1258
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001259MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001260static int ssl_cookie_check_dummy(void *ctx,
1261 const unsigned char *cookie, size_t cookie_len,
1262 const unsigned char *cli_id, size_t cli_id_len)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001263{
1264 ((void) ctx);
1265 ((void) cookie);
1266 ((void) cookie_len);
1267 ((void) cli_id);
1268 ((void) cli_id_len);
1269
Gilles Peskine449bd832023-01-11 14:50:10 +01001270 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001271}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001272#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001273
Paul Bakker5121ce52009-01-03 21:22:43 +00001274/*
1275 * Initialize an SSL context
1276 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001277void mbedtls_ssl_init(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001278{
Gilles Peskine449bd832023-01-11 14:50:10 +01001279 memset(ssl, 0, sizeof(mbedtls_ssl_context));
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001280}
1281
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001282MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001283static int ssl_conf_version_check(const mbedtls_ssl_context *ssl)
Jerry Yu60835a82021-08-04 10:13:52 +08001284{
Ronald Cron086ee0b2022-03-15 15:18:51 +01001285 const mbedtls_ssl_config *conf = ssl->conf;
1286
Ronald Cron6f135e12021-12-08 16:57:54 +01001287#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001288 if (mbedtls_ssl_conf_is_tls13_only(conf)) {
1289 if (conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
1290 MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS 1.3 is not yet supported."));
1291 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQianed582dd2022-04-13 08:21:05 +00001292 }
1293
Gilles Peskine449bd832023-01-11 14:50:10 +01001294 MBEDTLS_SSL_DEBUG_MSG(4, ("The SSL configuration is tls13 only."));
1295 return 0;
Jerry Yu60835a82021-08-04 10:13:52 +08001296 }
1297#endif
1298
1299#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01001300 if (mbedtls_ssl_conf_is_tls12_only(conf)) {
1301 MBEDTLS_SSL_DEBUG_MSG(4, ("The SSL configuration is tls12 only."));
1302 return 0;
Jerry Yu60835a82021-08-04 10:13:52 +08001303 }
1304#endif
1305
Ronald Cron6f135e12021-12-08 16:57:54 +01001306#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001307 if (mbedtls_ssl_conf_is_hybrid_tls12_tls13(conf)) {
1308 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
1309 MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS not yet supported in Hybrid TLS 1.3 + TLS 1.2"));
1310 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQianed582dd2022-04-13 08:21:05 +00001311 }
1312
Gilles Peskine449bd832023-01-11 14:50:10 +01001313 if (conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
1314 MBEDTLS_SSL_DEBUG_MSG(1, ("TLS 1.3 server is not supported yet."));
1315 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQian8f9dfe42022-04-15 02:52:39 +00001316 }
1317
1318
Gilles Peskine449bd832023-01-11 14:50:10 +01001319 MBEDTLS_SSL_DEBUG_MSG(4, ("The SSL configuration is TLS 1.3 or TLS 1.2."));
1320 return 0;
Jerry Yu60835a82021-08-04 10:13:52 +08001321 }
1322#endif
1323
Gilles Peskine449bd832023-01-11 14:50:10 +01001324 MBEDTLS_SSL_DEBUG_MSG(1, ("The SSL configuration is invalid."));
1325 return MBEDTLS_ERR_SSL_BAD_CONFIG;
Jerry Yu60835a82021-08-04 10:13:52 +08001326}
1327
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001328MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu60835a82021-08-04 10:13:52 +08001329static int ssl_conf_check(const mbedtls_ssl_context *ssl)
1330{
1331 int ret;
Gilles Peskine449bd832023-01-11 14:50:10 +01001332 ret = ssl_conf_version_check(ssl);
1333 if (ret != 0) {
1334 return ret;
1335 }
Jerry Yu60835a82021-08-04 10:13:52 +08001336
Jerry Yudef7ae42022-10-30 14:13:19 +08001337#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1338 /* RFC 8446 section 4.4.3
1339 *
1340 * If the verification fails, the receiver MUST terminate the handshake with
1341 * a "decrypt_error" alert.
1342 *
1343 * If the client is configured as TLS 1.3 only with optional verify, return
1344 * bad config.
1345 *
1346 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001347 if (mbedtls_ssl_conf_tls13_ephemeral_enabled(
1348 (mbedtls_ssl_context *) ssl) &&
Jerry Yudef7ae42022-10-30 14:13:19 +08001349 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
1350 ssl->conf->max_tls_version == MBEDTLS_SSL_VERSION_TLS1_3 &&
1351 ssl->conf->min_tls_version == MBEDTLS_SSL_VERSION_TLS1_3 &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001352 ssl->conf->authmode == MBEDTLS_SSL_VERIFY_OPTIONAL) {
Jerry Yudef7ae42022-10-30 14:13:19 +08001353 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +01001354 1, ("Optional verify auth mode "
1355 "is not available for TLS 1.3 client"));
1356 return MBEDTLS_ERR_SSL_BAD_CONFIG;
Jerry Yudef7ae42022-10-30 14:13:19 +08001357 }
1358#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
1359
Jerry Yu60835a82021-08-04 10:13:52 +08001360 /* Space for further checks */
1361
Gilles Peskine449bd832023-01-11 14:50:10 +01001362 return 0;
Jerry Yu60835a82021-08-04 10:13:52 +08001363}
1364
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001365/*
1366 * Setup an SSL context
1367 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01001368
Gilles Peskine449bd832023-01-11 14:50:10 +01001369int mbedtls_ssl_setup(mbedtls_ssl_context *ssl,
1370 const mbedtls_ssl_config *conf)
Paul Bakker5121ce52009-01-03 21:22:43 +00001371{
Janos Follath865b3eb2019-12-16 11:46:15 +00001372 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Darryl Greenb33cc762019-11-28 14:29:44 +00001373 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1374 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00001375
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001376 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00001377
Gilles Peskine449bd832023-01-11 14:50:10 +01001378 if ((ret = ssl_conf_check(ssl)) != 0) {
1379 return ret;
1380 }
Jerry Yu60835a82021-08-04 10:13:52 +08001381
Paul Bakker62f2dee2012-09-28 07:31:51 +00001382 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001383 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00001384 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02001385
1386 /* Set to NULL in case of an error condition */
1387 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02001388
Darryl Greenb33cc762019-11-28 14:29:44 +00001389#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1390 ssl->in_buf_len = in_buf_len;
1391#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001392 ssl->in_buf = mbedtls_calloc(1, in_buf_len);
1393 if (ssl->in_buf == NULL) {
1394 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", in_buf_len));
k-stachowiak9f7798e2018-07-31 16:52:32 +02001395 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02001396 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10001397 }
1398
Darryl Greenb33cc762019-11-28 14:29:44 +00001399#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1400 ssl->out_buf_len = out_buf_len;
1401#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001402 ssl->out_buf = mbedtls_calloc(1, out_buf_len);
1403 if (ssl->out_buf == NULL) {
1404 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", out_buf_len));
k-stachowiak9f7798e2018-07-31 16:52:32 +02001405 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02001406 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00001407 }
1408
Gilles Peskine449bd832023-01-11 14:50:10 +01001409 mbedtls_ssl_reset_in_out_pointers(ssl);
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02001410
Johan Pascalb62bb512015-12-03 21:56:45 +01001411#if defined(MBEDTLS_SSL_DTLS_SRTP)
Gilles Peskine449bd832023-01-11 14:50:10 +01001412 memset(&ssl->dtls_srtp_info, 0, sizeof(ssl->dtls_srtp_info));
Johan Pascalb62bb512015-12-03 21:56:45 +01001413#endif
1414
Gilles Peskine449bd832023-01-11 14:50:10 +01001415 if ((ret = ssl_handshake_init(ssl)) != 0) {
k-stachowiaka47911c2018-07-04 17:41:58 +02001416 goto error;
Gilles Peskine449bd832023-01-11 14:50:10 +01001417 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001418
Gilles Peskine449bd832023-01-11 14:50:10 +01001419 return 0;
k-stachowiaka47911c2018-07-04 17:41:58 +02001420
1421error:
Gilles Peskine449bd832023-01-11 14:50:10 +01001422 mbedtls_free(ssl->in_buf);
1423 mbedtls_free(ssl->out_buf);
k-stachowiaka47911c2018-07-04 17:41:58 +02001424
1425 ssl->conf = NULL;
1426
Darryl Greenb33cc762019-11-28 14:29:44 +00001427#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1428 ssl->in_buf_len = 0;
1429 ssl->out_buf_len = 0;
1430#endif
k-stachowiaka47911c2018-07-04 17:41:58 +02001431 ssl->in_buf = NULL;
1432 ssl->out_buf = NULL;
1433
1434 ssl->in_hdr = NULL;
1435 ssl->in_ctr = NULL;
1436 ssl->in_len = NULL;
1437 ssl->in_iv = NULL;
1438 ssl->in_msg = NULL;
1439
1440 ssl->out_hdr = NULL;
1441 ssl->out_ctr = NULL;
1442 ssl->out_len = NULL;
1443 ssl->out_iv = NULL;
1444 ssl->out_msg = NULL;
1445
Gilles Peskine449bd832023-01-11 14:50:10 +01001446 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001447}
1448
1449/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00001450 * Reset an initialized and used SSL context for re-use while retaining
1451 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001452 *
1453 * If partial is non-zero, keep data in the input buffer and client ID.
1454 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00001455 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001456void mbedtls_ssl_session_reset_msg_layer(mbedtls_ssl_context *ssl,
1457 int partial)
Paul Bakker7eb013f2011-10-06 12:37:39 +00001458{
Darryl Greenb33cc762019-11-28 14:29:44 +00001459#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1460 size_t in_buf_len = ssl->in_buf_len;
1461 size_t out_buf_len = ssl->out_buf_len;
1462#else
1463 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1464 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
1465#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001466
Hanno Beckerb0302c42021-08-03 09:39:42 +01001467#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || !defined(MBEDTLS_SSL_SRV_C)
1468 partial = 0;
Hanno Becker7e772132018-08-10 12:38:21 +01001469#endif
1470
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001471 /* Cancel any possibly running timer */
Gilles Peskine449bd832023-01-11 14:50:10 +01001472 mbedtls_ssl_set_timer(ssl, 0);
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001473
Gilles Peskine449bd832023-01-11 14:50:10 +01001474 mbedtls_ssl_reset_in_out_pointers(ssl);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001475
1476 /* Reset incoming message parsing */
1477 ssl->in_offt = NULL;
1478 ssl->nb_zero = 0;
1479 ssl->in_msgtype = 0;
1480 ssl->in_msglen = 0;
1481 ssl->in_hslen = 0;
1482 ssl->keep_current_message = 0;
1483 ssl->transform_in = NULL;
1484
1485#if defined(MBEDTLS_SSL_PROTO_DTLS)
1486 ssl->next_record_offset = 0;
1487 ssl->in_epoch = 0;
1488#endif
1489
1490 /* Keep current datagram if partial == 1 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001491 if (partial == 0) {
Hanno Beckerb0302c42021-08-03 09:39:42 +01001492 ssl->in_left = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001493 memset(ssl->in_buf, 0, in_buf_len);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001494 }
1495
Ronald Cronad8c17b2022-06-10 17:18:09 +02001496 ssl->send_alert = 0;
1497
Hanno Beckerb0302c42021-08-03 09:39:42 +01001498 /* Reset outgoing message writing */
1499 ssl->out_msgtype = 0;
1500 ssl->out_msglen = 0;
1501 ssl->out_left = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001502 memset(ssl->out_buf, 0, out_buf_len);
1503 memset(ssl->cur_out_ctr, 0, sizeof(ssl->cur_out_ctr));
Hanno Beckerb0302c42021-08-03 09:39:42 +01001504 ssl->transform_out = NULL;
1505
1506#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine449bd832023-01-11 14:50:10 +01001507 mbedtls_ssl_dtls_replay_reset(ssl);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001508#endif
1509
XiaokangQian2b01dc32022-01-21 02:53:13 +00001510#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01001511 if (ssl->transform) {
1512 mbedtls_ssl_transform_free(ssl->transform);
1513 mbedtls_free(ssl->transform);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001514 ssl->transform = NULL;
1515 }
XiaokangQian2b01dc32022-01-21 02:53:13 +00001516#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1517
XiaokangQian2b01dc32022-01-21 02:53:13 +00001518#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001519 mbedtls_ssl_transform_free(ssl->transform_application);
1520 mbedtls_free(ssl->transform_application);
XiaokangQian2b01dc32022-01-21 02:53:13 +00001521 ssl->transform_application = NULL;
1522
Gilles Peskine449bd832023-01-11 14:50:10 +01001523 if (ssl->handshake != NULL) {
Jerry Yu3d9b5902022-11-04 14:07:25 +08001524#if defined(MBEDTLS_SSL_EARLY_DATA)
Gilles Peskine449bd832023-01-11 14:50:10 +01001525 mbedtls_ssl_transform_free(ssl->handshake->transform_earlydata);
1526 mbedtls_free(ssl->handshake->transform_earlydata);
XiaokangQian2b01dc32022-01-21 02:53:13 +00001527 ssl->handshake->transform_earlydata = NULL;
Jerry Yu3d9b5902022-11-04 14:07:25 +08001528#endif
XiaokangQian2b01dc32022-01-21 02:53:13 +00001529
Gilles Peskine449bd832023-01-11 14:50:10 +01001530 mbedtls_ssl_transform_free(ssl->handshake->transform_handshake);
1531 mbedtls_free(ssl->handshake->transform_handshake);
XiaokangQian2b01dc32022-01-21 02:53:13 +00001532 ssl->handshake->transform_handshake = NULL;
1533 }
1534
XiaokangQian2b01dc32022-01-21 02:53:13 +00001535#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerb0302c42021-08-03 09:39:42 +01001536}
1537
Gilles Peskine449bd832023-01-11 14:50:10 +01001538int mbedtls_ssl_session_reset_int(mbedtls_ssl_context *ssl, int partial)
Hanno Beckerb0302c42021-08-03 09:39:42 +01001539{
1540 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1541
1542 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
1543
Gilles Peskine449bd832023-01-11 14:50:10 +01001544 mbedtls_ssl_session_reset_msg_layer(ssl, partial);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001545
1546 /* Reset renegotiation state */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001547#if defined(MBEDTLS_SSL_RENEGOTIATION)
1548 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001549 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00001550
1551 ssl->verify_data_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001552 memset(ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN);
1553 memset(ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN);
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001554#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001555 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00001556
Hanno Beckerb0302c42021-08-03 09:39:42 +01001557 ssl->session_in = NULL;
Hanno Becker78640902018-08-13 16:35:15 +01001558 ssl->session_out = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001559 if (ssl->session) {
1560 mbedtls_ssl_session_free(ssl->session);
1561 mbedtls_free(ssl->session);
Paul Bakkerc0463502013-02-14 11:19:38 +01001562 ssl->session = NULL;
1563 }
1564
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001565#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02001566 ssl->alpn_chosen = NULL;
1567#endif
1568
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001569#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
David Horstmann3b2276a2022-10-06 14:49:08 +01001570 int free_cli_id = 1;
Hanno Becker4ccbf062018-08-10 11:20:38 +01001571#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Gilles Peskine449bd832023-01-11 14:50:10 +01001572 free_cli_id = (partial == 0);
Hanno Becker4ccbf062018-08-10 11:20:38 +01001573#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001574 if (free_cli_id) {
1575 mbedtls_free(ssl->cli_id);
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001576 ssl->cli_id = NULL;
1577 ssl->cli_id_len = 0;
1578 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02001579#endif
1580
Gilles Peskine449bd832023-01-11 14:50:10 +01001581 if ((ret = ssl_handshake_init(ssl)) != 0) {
1582 return ret;
1583 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00001584
Gilles Peskine449bd832023-01-11 14:50:10 +01001585 return 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00001586}
1587
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02001588/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001589 * Reset an initialized and used SSL context for re-use while retaining
1590 * all application-set variables, function pointers and data.
1591 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001592int mbedtls_ssl_session_reset(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001593{
Gilles Peskine449bd832023-01-11 14:50:10 +01001594 return mbedtls_ssl_session_reset_int(ssl, 0);
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001595}
1596
1597/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001598 * SSL set accessors
1599 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001600void mbedtls_ssl_conf_endpoint(mbedtls_ssl_config *conf, int endpoint)
Paul Bakker5121ce52009-01-03 21:22:43 +00001601{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001602 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00001603}
1604
Gilles Peskine449bd832023-01-11 14:50:10 +01001605void mbedtls_ssl_conf_transport(mbedtls_ssl_config *conf, int transport)
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001606{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001607 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001608}
1609
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001610#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine449bd832023-01-11 14:50:10 +01001611void mbedtls_ssl_conf_dtls_anti_replay(mbedtls_ssl_config *conf, char mode)
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001612{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001613 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001614}
1615#endif
1616
Gilles Peskine449bd832023-01-11 14:50:10 +01001617void mbedtls_ssl_conf_dtls_badmac_limit(mbedtls_ssl_config *conf, unsigned limit)
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001618{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001619 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001620}
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001621
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001622#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01001623
Gilles Peskine449bd832023-01-11 14:50:10 +01001624void mbedtls_ssl_set_datagram_packing(mbedtls_ssl_context *ssl,
1625 unsigned allow_packing)
Hanno Becker04da1892018-08-14 13:22:10 +01001626{
1627 ssl->disable_datagram_packing = !allow_packing;
1628}
1629
Gilles Peskine449bd832023-01-11 14:50:10 +01001630void mbedtls_ssl_conf_handshake_timeout(mbedtls_ssl_config *conf,
1631 uint32_t min, uint32_t max)
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001632{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001633 conf->hs_timeout_min = min;
1634 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001635}
1636#endif
1637
Gilles Peskine449bd832023-01-11 14:50:10 +01001638void mbedtls_ssl_conf_authmode(mbedtls_ssl_config *conf, int authmode)
Paul Bakker5121ce52009-01-03 21:22:43 +00001639{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001640 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00001641}
1642
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001643#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001644void mbedtls_ssl_conf_verify(mbedtls_ssl_config *conf,
1645 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
1646 void *p_vrfy)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001647{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001648 conf->f_vrfy = f_vrfy;
1649 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001650}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001651#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001652
Gilles Peskine449bd832023-01-11 14:50:10 +01001653void mbedtls_ssl_conf_rng(mbedtls_ssl_config *conf,
1654 int (*f_rng)(void *, unsigned char *, size_t),
1655 void *p_rng)
Paul Bakker5121ce52009-01-03 21:22:43 +00001656{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001657 conf->f_rng = f_rng;
1658 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00001659}
1660
Gilles Peskine449bd832023-01-11 14:50:10 +01001661void mbedtls_ssl_conf_dbg(mbedtls_ssl_config *conf,
1662 void (*f_dbg)(void *, int, const char *, int, const char *),
1663 void *p_dbg)
Paul Bakker5121ce52009-01-03 21:22:43 +00001664{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001665 conf->f_dbg = f_dbg;
1666 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00001667}
1668
Gilles Peskine449bd832023-01-11 14:50:10 +01001669void mbedtls_ssl_set_bio(mbedtls_ssl_context *ssl,
1670 void *p_bio,
1671 mbedtls_ssl_send_t *f_send,
1672 mbedtls_ssl_recv_t *f_recv,
1673 mbedtls_ssl_recv_timeout_t *f_recv_timeout)
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001674{
1675 ssl->p_bio = p_bio;
1676 ssl->f_send = f_send;
1677 ssl->f_recv = f_recv;
1678 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001679}
1680
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02001681#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001682void mbedtls_ssl_set_mtu(mbedtls_ssl_context *ssl, uint16_t mtu)
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02001683{
1684 ssl->mtu = mtu;
1685}
1686#endif
1687
Gilles Peskine449bd832023-01-11 14:50:10 +01001688void mbedtls_ssl_conf_read_timeout(mbedtls_ssl_config *conf, uint32_t timeout)
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001689{
1690 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001691}
1692
Gilles Peskine449bd832023-01-11 14:50:10 +01001693void mbedtls_ssl_set_timer_cb(mbedtls_ssl_context *ssl,
1694 void *p_timer,
1695 mbedtls_ssl_set_timer_t *f_set_timer,
1696 mbedtls_ssl_get_timer_t *f_get_timer)
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001697{
1698 ssl->p_timer = p_timer;
1699 ssl->f_set_timer = f_set_timer;
1700 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001701
1702 /* Make sure we start with no timer running */
Gilles Peskine449bd832023-01-11 14:50:10 +01001703 mbedtls_ssl_set_timer(ssl, 0);
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001704}
1705
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001706#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001707void mbedtls_ssl_conf_session_cache(mbedtls_ssl_config *conf,
1708 void *p_cache,
1709 mbedtls_ssl_cache_get_t *f_get_cache,
1710 mbedtls_ssl_cache_set_t *f_set_cache)
Paul Bakker5121ce52009-01-03 21:22:43 +00001711{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01001712 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001713 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001714 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00001715}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001716#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001717
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001718#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001719int mbedtls_ssl_set_session(mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session)
Paul Bakker5121ce52009-01-03 21:22:43 +00001720{
Janos Follath865b3eb2019-12-16 11:46:15 +00001721 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001722
Gilles Peskine449bd832023-01-11 14:50:10 +01001723 if (ssl == NULL ||
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001724 session == NULL ||
1725 ssl->session_negotiate == NULL ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001726 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT) {
1727 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001728 }
1729
Gilles Peskine449bd832023-01-11 14:50:10 +01001730 if (ssl->handshake->resume == 1) {
1731 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
1732 }
Hanno Beckere810bbc2021-05-14 16:01:05 +01001733
Jerry Yu21092062022-10-10 21:21:31 +08001734#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001735 if (session->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
Jerry Yu21092062022-10-10 21:21:31 +08001736 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01001737 mbedtls_ssl_ciphersuite_from_id(session->ciphersuite);
Jerry Yu21092062022-10-10 21:21:31 +08001738
Gilles Peskine449bd832023-01-11 14:50:10 +01001739 if (mbedtls_ssl_validate_ciphersuite(
Jerry Yu21092062022-10-10 21:21:31 +08001740 ssl, ciphersuite_info, MBEDTLS_SSL_VERSION_TLS1_3,
Gilles Peskine449bd832023-01-11 14:50:10 +01001741 MBEDTLS_SSL_VERSION_TLS1_3) != 0) {
1742 MBEDTLS_SSL_DEBUG_MSG(4, ("%d is not a valid TLS 1.3 ciphersuite.",
1743 session->ciphersuite));
1744 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jerry Yu21092062022-10-10 21:21:31 +08001745 }
Jerry Yu40afab62022-10-08 10:42:13 +08001746 }
Jerry Yu21092062022-10-10 21:21:31 +08001747#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu40afab62022-10-08 10:42:13 +08001748
Gilles Peskine449bd832023-01-11 14:50:10 +01001749 if ((ret = mbedtls_ssl_session_copy(ssl->session_negotiate,
1750 session)) != 0) {
1751 return ret;
1752 }
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001753
Paul Bakker0a597072012-09-25 21:55:46 +00001754 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001755
Gilles Peskine449bd832023-01-11 14:50:10 +01001756 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001757}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001758#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001759
Gilles Peskine449bd832023-01-11 14:50:10 +01001760void mbedtls_ssl_conf_ciphersuites(mbedtls_ssl_config *conf,
1761 const int *ciphersuites)
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001762{
Hanno Beckerd60b6c62021-04-29 12:04:11 +01001763 conf->ciphersuite_list = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00001764}
1765
Ronald Cron6f135e12021-12-08 16:57:54 +01001766#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001767void mbedtls_ssl_conf_tls13_key_exchange_modes(mbedtls_ssl_config *conf,
1768 const int kex_modes)
Hanno Becker71f1ed62021-07-24 06:01:47 +01001769{
Xiaofei Bai746f9482021-11-12 08:53:56 +00001770 conf->tls13_kex_modes = kex_modes & MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Hanno Becker71f1ed62021-07-24 06:01:47 +01001771}
Xiaokang Qian72de95d2022-10-25 02:54:33 +00001772
1773#if defined(MBEDTLS_SSL_EARLY_DATA)
Gilles Peskine449bd832023-01-11 14:50:10 +01001774void mbedtls_ssl_tls13_conf_early_data(mbedtls_ssl_config *conf,
1775 int early_data_enabled)
Xiaokang Qian72de95d2022-10-25 02:54:33 +00001776{
1777 conf->early_data_enabled = early_data_enabled;
1778}
Jerry Yucc4e0072022-11-22 17:22:22 +08001779
1780#if defined(MBEDTLS_SSL_SRV_C)
1781void mbedtls_ssl_tls13_conf_max_early_data_size(
Gilles Peskine449bd832023-01-11 14:50:10 +01001782 mbedtls_ssl_config *conf, uint32_t max_early_data_size)
Jerry Yucc4e0072022-11-22 17:22:22 +08001783{
Jerry Yu39da9852022-12-06 16:58:36 +08001784 conf->max_early_data_size = max_early_data_size;
Jerry Yucc4e0072022-11-22 17:22:22 +08001785}
1786#endif /* MBEDTLS_SSL_SRV_C */
1787
Xiaokang Qian72de95d2022-10-25 02:54:33 +00001788#endif /* MBEDTLS_SSL_EARLY_DATA */
Ronald Cron6f135e12021-12-08 16:57:54 +01001789#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01001790
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001791#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001792void mbedtls_ssl_conf_cert_profile(mbedtls_ssl_config *conf,
1793 const mbedtls_x509_crt_profile *profile)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02001794{
1795 conf->cert_profile = profile;
1796}
1797
Gilles Peskine449bd832023-01-11 14:50:10 +01001798static void ssl_key_cert_free(mbedtls_ssl_key_cert *key_cert)
Glenn Strauss36872db2022-01-22 05:06:31 -05001799{
1800 mbedtls_ssl_key_cert *cur = key_cert, *next;
1801
Gilles Peskine449bd832023-01-11 14:50:10 +01001802 while (cur != NULL) {
Glenn Strauss36872db2022-01-22 05:06:31 -05001803 next = cur->next;
Gilles Peskine449bd832023-01-11 14:50:10 +01001804 mbedtls_free(cur);
Glenn Strauss36872db2022-01-22 05:06:31 -05001805 cur = next;
1806 }
1807}
1808
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001809/* Append a new keycert entry to a (possibly empty) list */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001810MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001811static int ssl_append_key_cert(mbedtls_ssl_key_cert **head,
1812 mbedtls_x509_crt *cert,
1813 mbedtls_pk_context *key)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001814{
niisato8ee24222018-06-25 19:05:48 +09001815 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001816
Gilles Peskine449bd832023-01-11 14:50:10 +01001817 if (cert == NULL) {
Glenn Strauss36872db2022-01-22 05:06:31 -05001818 /* Free list if cert is null */
Gilles Peskine449bd832023-01-11 14:50:10 +01001819 ssl_key_cert_free(*head);
Glenn Strauss36872db2022-01-22 05:06:31 -05001820 *head = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001821 return 0;
Glenn Strauss36872db2022-01-22 05:06:31 -05001822 }
1823
Gilles Peskine449bd832023-01-11 14:50:10 +01001824 new_cert = mbedtls_calloc(1, sizeof(mbedtls_ssl_key_cert));
1825 if (new_cert == NULL) {
1826 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
1827 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001828
niisato8ee24222018-06-25 19:05:48 +09001829 new_cert->cert = cert;
1830 new_cert->key = key;
1831 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001832
Glenn Strauss36872db2022-01-22 05:06:31 -05001833 /* Update head if the list was null, else add to the end */
Gilles Peskine449bd832023-01-11 14:50:10 +01001834 if (*head == NULL) {
niisato8ee24222018-06-25 19:05:48 +09001835 *head = new_cert;
Gilles Peskine449bd832023-01-11 14:50:10 +01001836 } else {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001837 mbedtls_ssl_key_cert *cur = *head;
Gilles Peskine449bd832023-01-11 14:50:10 +01001838 while (cur->next != NULL) {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001839 cur = cur->next;
Gilles Peskine449bd832023-01-11 14:50:10 +01001840 }
niisato8ee24222018-06-25 19:05:48 +09001841 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001842 }
1843
Gilles Peskine449bd832023-01-11 14:50:10 +01001844 return 0;
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001845}
1846
Gilles Peskine449bd832023-01-11 14:50:10 +01001847int mbedtls_ssl_conf_own_cert(mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001848 mbedtls_x509_crt *own_cert,
Gilles Peskine449bd832023-01-11 14:50:10 +01001849 mbedtls_pk_context *pk_key)
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001850{
Gilles Peskine449bd832023-01-11 14:50:10 +01001851 return ssl_append_key_cert(&conf->key_cert, own_cert, pk_key);
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001852}
1853
Gilles Peskine449bd832023-01-11 14:50:10 +01001854void mbedtls_ssl_conf_ca_chain(mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001855 mbedtls_x509_crt *ca_chain,
Gilles Peskine449bd832023-01-11 14:50:10 +01001856 mbedtls_x509_crl *ca_crl)
Paul Bakker5121ce52009-01-03 21:22:43 +00001857{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001858 conf->ca_chain = ca_chain;
1859 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00001860
1861#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1862 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1863 * cannot be used together. */
1864 conf->f_ca_cb = NULL;
1865 conf->p_ca_cb = NULL;
1866#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00001867}
Hanno Becker5adaad92019-03-27 16:54:37 +00001868
1869#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Gilles Peskine449bd832023-01-11 14:50:10 +01001870void mbedtls_ssl_conf_ca_cb(mbedtls_ssl_config *conf,
1871 mbedtls_x509_crt_ca_cb_t f_ca_cb,
1872 void *p_ca_cb)
Hanno Becker5adaad92019-03-27 16:54:37 +00001873{
1874 conf->f_ca_cb = f_ca_cb;
1875 conf->p_ca_cb = p_ca_cb;
1876
1877 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1878 * cannot be used together. */
1879 conf->ca_chain = NULL;
1880 conf->ca_crl = NULL;
1881}
1882#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001883#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00001884
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001885#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01001886const unsigned char *mbedtls_ssl_get_hs_sni(mbedtls_ssl_context *ssl,
1887 size_t *name_len)
Glenn Strauss69894072022-01-24 12:58:00 -05001888{
1889 *name_len = ssl->handshake->sni_name_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001890 return ssl->handshake->sni_name;
Glenn Strauss69894072022-01-24 12:58:00 -05001891}
1892
Gilles Peskine449bd832023-01-11 14:50:10 +01001893int mbedtls_ssl_set_hs_own_cert(mbedtls_ssl_context *ssl,
1894 mbedtls_x509_crt *own_cert,
1895 mbedtls_pk_context *pk_key)
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001896{
Gilles Peskine449bd832023-01-11 14:50:10 +01001897 return ssl_append_key_cert(&ssl->handshake->sni_key_cert,
1898 own_cert, pk_key);
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001899}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02001900
Gilles Peskine449bd832023-01-11 14:50:10 +01001901void mbedtls_ssl_set_hs_ca_chain(mbedtls_ssl_context *ssl,
1902 mbedtls_x509_crt *ca_chain,
1903 mbedtls_x509_crl *ca_crl)
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02001904{
1905 ssl->handshake->sni_ca_chain = ca_chain;
1906 ssl->handshake->sni_ca_crl = ca_crl;
1907}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001908
Glenn Strauss999ef702022-03-11 01:37:23 -05001909#if defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001910void mbedtls_ssl_set_hs_dn_hints(mbedtls_ssl_context *ssl,
1911 const mbedtls_x509_crt *crt)
Glenn Strauss999ef702022-03-11 01:37:23 -05001912{
1913 ssl->handshake->dn_hints = crt;
1914}
1915#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
1916
Gilles Peskine449bd832023-01-11 14:50:10 +01001917void mbedtls_ssl_set_hs_authmode(mbedtls_ssl_context *ssl,
1918 int authmode)
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001919{
1920 ssl->handshake->sni_authmode = authmode;
1921}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001922#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
1923
Hanno Becker8927c832019-04-03 12:52:50 +01001924#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001925void mbedtls_ssl_set_verify(mbedtls_ssl_context *ssl,
1926 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
1927 void *p_vrfy)
Hanno Becker8927c832019-04-03 12:52:50 +01001928{
1929 ssl->f_vrfy = f_vrfy;
1930 ssl->p_vrfy = p_vrfy;
1931}
1932#endif
1933
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02001934#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001935
Valerio Setti016f6822022-12-09 14:17:50 +01001936#if defined(MBEDTLS_USE_PSA_CRYPTO)
1937static psa_status_t mbedtls_ssl_set_hs_ecjpake_password_common(
Gilles Peskine449bd832023-01-11 14:50:10 +01001938 mbedtls_ssl_context *ssl,
1939 mbedtls_svc_key_id_t pwd)
Valerio Setti016f6822022-12-09 14:17:50 +01001940{
1941 psa_status_t status;
1942 psa_pake_role_t psa_role;
1943 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
1944
Gilles Peskine449bd832023-01-11 14:50:10 +01001945 psa_pake_cs_set_algorithm(&cipher_suite, PSA_ALG_JPAKE);
1946 psa_pake_cs_set_primitive(&cipher_suite,
1947 PSA_PAKE_PRIMITIVE(PSA_PAKE_PRIMITIVE_TYPE_ECC,
1948 PSA_ECC_FAMILY_SECP_R1,
1949 256));
1950 psa_pake_cs_set_hash(&cipher_suite, PSA_ALG_SHA_256);
Valerio Setti016f6822022-12-09 14:17:50 +01001951
Gilles Peskine449bd832023-01-11 14:50:10 +01001952 status = psa_pake_setup(&ssl->handshake->psa_pake_ctx, &cipher_suite);
1953 if (status != PSA_SUCCESS) {
Valerio Setti016f6822022-12-09 14:17:50 +01001954 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001955 }
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001956
Gilles Peskine449bd832023-01-11 14:50:10 +01001957 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Neil Armstrongca7d5062022-05-31 14:43:23 +02001958 psa_role = PSA_PAKE_ROLE_SERVER;
Gilles Peskine449bd832023-01-11 14:50:10 +01001959 } else {
Neil Armstrongca7d5062022-05-31 14:43:23 +02001960 psa_role = PSA_PAKE_ROLE_CLIENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001961 }
Neil Armstrongca7d5062022-05-31 14:43:23 +02001962
Gilles Peskine449bd832023-01-11 14:50:10 +01001963 status = psa_pake_set_role(&ssl->handshake->psa_pake_ctx, psa_role);
1964 if (status != PSA_SUCCESS) {
Valerio Setti016f6822022-12-09 14:17:50 +01001965 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001966 }
Valerio Setti016f6822022-12-09 14:17:50 +01001967
Gilles Peskine449bd832023-01-11 14:50:10 +01001968 status = psa_pake_set_password_key(&ssl->handshake->psa_pake_ctx, pwd);
1969 if (status != PSA_SUCCESS) {
Valerio Setti016f6822022-12-09 14:17:50 +01001970 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001971 }
Valerio Setti016f6822022-12-09 14:17:50 +01001972
1973 ssl->handshake->psa_pake_ctx_is_ok = 1;
1974
Gilles Peskine449bd832023-01-11 14:50:10 +01001975 return PSA_SUCCESS;
Valerio Setti016f6822022-12-09 14:17:50 +01001976}
1977
Gilles Peskine449bd832023-01-11 14:50:10 +01001978int mbedtls_ssl_set_hs_ecjpake_password(mbedtls_ssl_context *ssl,
1979 const unsigned char *pw,
1980 size_t pw_len)
Valerio Setti016f6822022-12-09 14:17:50 +01001981{
1982 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1983 psa_status_t status;
1984
Gilles Peskine449bd832023-01-11 14:50:10 +01001985 if (ssl->handshake == NULL || ssl->conf == NULL) {
1986 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Neil Armstrongca7d5062022-05-31 14:43:23 +02001987 }
1988
Gilles Peskine449bd832023-01-11 14:50:10 +01001989 /* Empty password is not valid */
1990 if ((pw == NULL) || (pw_len == 0)) {
1991 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
1992 }
1993
1994 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
1995 psa_set_key_algorithm(&attributes, PSA_ALG_JPAKE);
1996 psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD);
1997
1998 status = psa_import_key(&attributes, pw, pw_len,
1999 &ssl->handshake->psa_pake_password);
2000 if (status != PSA_SUCCESS) {
2001 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2002 }
2003
2004 status = mbedtls_ssl_set_hs_ecjpake_password_common(ssl,
2005 ssl->handshake->psa_pake_password);
2006 if (status != PSA_SUCCESS) {
2007 psa_destroy_key(ssl->handshake->psa_pake_password);
2008 psa_pake_abort(&ssl->handshake->psa_pake_ctx);
2009 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2010 }
2011
2012 return 0;
Valerio Setti02c25b52022-11-15 14:08:42 +01002013}
Valerio Settia9a97dc2022-11-28 18:26:16 +01002014
Gilles Peskine449bd832023-01-11 14:50:10 +01002015int mbedtls_ssl_set_hs_ecjpake_password_opaque(mbedtls_ssl_context *ssl,
2016 mbedtls_svc_key_id_t pwd)
Valerio Settia9a97dc2022-11-28 18:26:16 +01002017{
Valerio Settia9a97dc2022-11-28 18:26:16 +01002018 psa_status_t status;
2019
Gilles Peskine449bd832023-01-11 14:50:10 +01002020 if (ssl->handshake == NULL || ssl->conf == NULL) {
2021 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Neil Armstrongca7d5062022-05-31 14:43:23 +02002022 }
2023
Gilles Peskine449bd832023-01-11 14:50:10 +01002024 if (mbedtls_svc_key_id_is_null(pwd)) {
2025 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2026 }
2027
2028 status = mbedtls_ssl_set_hs_ecjpake_password_common(ssl, pwd);
2029 if (status != PSA_SUCCESS) {
2030 psa_pake_abort(&ssl->handshake->psa_pake_ctx);
2031 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2032 }
2033
2034 return 0;
Valerio Setti02c25b52022-11-15 14:08:42 +01002035}
2036#else /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01002037int mbedtls_ssl_set_hs_ecjpake_password(mbedtls_ssl_context *ssl,
2038 const unsigned char *pw,
2039 size_t pw_len)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002040{
2041 mbedtls_ecjpake_role role;
2042
Gilles Peskine449bd832023-01-11 14:50:10 +01002043 if (ssl->handshake == NULL || ssl->conf == NULL) {
2044 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2045 }
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002046
Valerio Settic689ed82022-12-07 14:40:38 +01002047 /* Empty password is not valid */
Gilles Peskine449bd832023-01-11 14:50:10 +01002048 if ((pw == NULL) || (pw_len == 0)) {
2049 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2050 }
Valerio Settic689ed82022-12-07 14:40:38 +01002051
Gilles Peskine449bd832023-01-11 14:50:10 +01002052 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002053 role = MBEDTLS_ECJPAKE_SERVER;
Gilles Peskine449bd832023-01-11 14:50:10 +01002054 } else {
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002055 role = MBEDTLS_ECJPAKE_CLIENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01002056 }
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002057
Gilles Peskine449bd832023-01-11 14:50:10 +01002058 return mbedtls_ecjpake_setup(&ssl->handshake->ecjpake_ctx,
2059 role,
2060 MBEDTLS_MD_SHA256,
2061 MBEDTLS_ECP_DP_SECP256R1,
2062 pw, pw_len);
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002063}
Valerio Setti02c25b52022-11-15 14:08:42 +01002064#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02002065#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002066
Ronald Cron73fe8df2022-10-05 14:31:43 +02002067#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002068int mbedtls_ssl_conf_has_static_psk(mbedtls_ssl_config const *conf)
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002069{
Gilles Peskine449bd832023-01-11 14:50:10 +01002070 if (conf->psk_identity == NULL ||
2071 conf->psk_identity_len == 0) {
2072 return 0;
Ronald Crond29e13e2022-10-19 10:33:48 +02002073 }
2074
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002075#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002076 if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) {
2077 return 1;
2078 }
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002079#endif /* MBEDTLS_USE_PSA_CRYPTO */
Ronald Crond29e13e2022-10-19 10:33:48 +02002080
Gilles Peskine449bd832023-01-11 14:50:10 +01002081 if (conf->psk != NULL && conf->psk_len != 0) {
2082 return 1;
2083 }
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002084
Gilles Peskine449bd832023-01-11 14:50:10 +01002085 return 0;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002086}
2087
Gilles Peskine449bd832023-01-11 14:50:10 +01002088static void ssl_conf_remove_psk(mbedtls_ssl_config *conf)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002089{
2090 /* Remove reference to existing PSK, if any. */
2091#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002092 if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) {
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002093 /* The maintenance of the PSK key slot is the
2094 * user's responsibility. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002095 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002096 }
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002097#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01002098 if (conf->psk != NULL) {
2099 mbedtls_platform_zeroize(conf->psk, conf->psk_len);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002100
Gilles Peskine449bd832023-01-11 14:50:10 +01002101 mbedtls_free(conf->psk);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002102 conf->psk = NULL;
2103 conf->psk_len = 0;
2104 }
2105
2106 /* Remove reference to PSK identity, if any. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002107 if (conf->psk_identity != NULL) {
2108 mbedtls_free(conf->psk_identity);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002109 conf->psk_identity = NULL;
2110 conf->psk_identity_len = 0;
2111 }
2112}
2113
Hanno Becker7390c712018-11-15 13:33:04 +00002114/* This function assumes that PSK identity in the SSL config is unset.
2115 * It checks that the provided identity is well-formed and attempts
2116 * to make a copy of it in the SSL config.
2117 * On failure, the PSK identity in the config remains unset. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002118MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002119static int ssl_conf_set_psk_identity(mbedtls_ssl_config *conf,
2120 unsigned char const *psk_identity,
2121 size_t psk_identity_len)
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002122{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02002123 /* Identity len will be encoded on two bytes */
Gilles Peskine449bd832023-01-11 14:50:10 +01002124 if (psk_identity == NULL ||
Ronald Cron2a87e9b2022-10-19 10:55:26 +02002125 psk_identity_len == 0 ||
Gilles Peskine449bd832023-01-11 14:50:10 +01002126 (psk_identity_len >> 16) != 0 ||
2127 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN) {
2128 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02002129 }
2130
Gilles Peskine449bd832023-01-11 14:50:10 +01002131 conf->psk_identity = mbedtls_calloc(1, psk_identity_len);
2132 if (conf->psk_identity == NULL) {
2133 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2134 }
Paul Bakker6db455e2013-09-18 17:29:31 +02002135
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01002136 conf->psk_identity_len = psk_identity_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002137 memcpy(conf->psk_identity, psk_identity, conf->psk_identity_len);
Paul Bakker5ad403f2013-09-18 21:21:30 +02002138
Gilles Peskine449bd832023-01-11 14:50:10 +01002139 return 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02002140}
2141
Gilles Peskine449bd832023-01-11 14:50:10 +01002142int mbedtls_ssl_conf_psk(mbedtls_ssl_config *conf,
2143 const unsigned char *psk, size_t psk_len,
2144 const unsigned char *psk_identity, size_t psk_identity_len)
Hanno Becker7390c712018-11-15 13:33:04 +00002145{
Janos Follath865b3eb2019-12-16 11:46:15 +00002146 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002147
2148 /* We currently only support one PSK, raw or opaque. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002149 if (mbedtls_ssl_conf_has_static_psk(conf)) {
2150 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
2151 }
Hanno Becker7390c712018-11-15 13:33:04 +00002152
2153 /* Check and set raw PSK */
Gilles Peskine449bd832023-01-11 14:50:10 +01002154 if (psk == NULL) {
2155 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2156 }
2157 if (psk_len == 0) {
2158 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2159 }
2160 if (psk_len > MBEDTLS_PSK_MAX_LEN) {
2161 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2162 }
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01002163
Gilles Peskine449bd832023-01-11 14:50:10 +01002164 if ((conf->psk = mbedtls_calloc(1, psk_len)) == NULL) {
2165 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2166 }
Hanno Becker7390c712018-11-15 13:33:04 +00002167 conf->psk_len = psk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002168 memcpy(conf->psk, psk, conf->psk_len);
Hanno Becker7390c712018-11-15 13:33:04 +00002169
2170 /* Check and set PSK Identity */
Gilles Peskine449bd832023-01-11 14:50:10 +01002171 ret = ssl_conf_set_psk_identity(conf, psk_identity, psk_identity_len);
2172 if (ret != 0) {
2173 ssl_conf_remove_psk(conf);
2174 }
Hanno Becker7390c712018-11-15 13:33:04 +00002175
Gilles Peskine449bd832023-01-11 14:50:10 +01002176 return ret;
Hanno Becker7390c712018-11-15 13:33:04 +00002177}
2178
Gilles Peskine449bd832023-01-11 14:50:10 +01002179static void ssl_remove_psk(mbedtls_ssl_context *ssl)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002180{
2181#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002182 if (!mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque)) {
Neil Armstrong501c9322022-05-03 09:35:09 +02002183 /* The maintenance of the external PSK key slot is the
2184 * user's responsibility. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002185 if (ssl->handshake->psk_opaque_is_internal) {
2186 psa_destroy_key(ssl->handshake->psk_opaque);
Neil Armstrong501c9322022-05-03 09:35:09 +02002187 ssl->handshake->psk_opaque_is_internal = 0;
2188 }
Ronald Croncf56a0a2020-08-04 09:51:30 +02002189 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002190 }
Neil Armstronge952a302022-05-03 10:22:14 +02002191#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002192 if (ssl->handshake->psk != NULL) {
2193 mbedtls_platform_zeroize(ssl->handshake->psk,
2194 ssl->handshake->psk_len);
2195 mbedtls_free(ssl->handshake->psk);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002196 ssl->handshake->psk_len = 0;
2197 }
Neil Armstronge952a302022-05-03 10:22:14 +02002198#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002199}
2200
Gilles Peskine449bd832023-01-11 14:50:10 +01002201int mbedtls_ssl_set_hs_psk(mbedtls_ssl_context *ssl,
2202 const unsigned char *psk, size_t psk_len)
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002203{
Neil Armstrong501c9322022-05-03 09:35:09 +02002204#if defined(MBEDTLS_USE_PSA_CRYPTO)
2205 psa_key_attributes_t key_attributes = psa_key_attributes_init();
Jerry Yu5d01c052022-08-17 10:18:10 +08002206 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jerry Yu24b8c812022-08-20 19:06:56 +08002207 psa_algorithm_t alg = PSA_ALG_NONE;
Jerry Yu5d01c052022-08-17 10:18:10 +08002208 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrong501c9322022-05-03 09:35:09 +02002209#endif /* MBEDTLS_USE_PSA_CRYPTO */
2210
Gilles Peskine449bd832023-01-11 14:50:10 +01002211 if (psk == NULL || ssl->handshake == NULL) {
2212 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2213 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002214
Gilles Peskine449bd832023-01-11 14:50:10 +01002215 if (psk_len > MBEDTLS_PSK_MAX_LEN) {
2216 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2217 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002218
Gilles Peskine449bd832023-01-11 14:50:10 +01002219 ssl_remove_psk(ssl);
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002220
Neil Armstrong501c9322022-05-03 09:35:09 +02002221#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yuccc68a42022-07-26 16:39:20 +08002222#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01002223 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2) {
2224 if (ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384) {
2225 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
2226 } else {
2227 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
2228 }
2229 psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE);
Jerry Yuccc68a42022-07-26 16:39:20 +08002230 }
2231#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Neil Armstrong501c9322022-05-03 09:35:09 +02002232
Jerry Yu568ec252022-07-22 21:27:34 +08002233#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01002234 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
2235 alg = PSA_ALG_HKDF_EXTRACT(PSA_ALG_ANY_HASH);
2236 psa_set_key_usage_flags(&key_attributes,
2237 PSA_KEY_USAGE_DERIVE | PSA_KEY_USAGE_EXPORT);
Jerry Yuccc68a42022-07-26 16:39:20 +08002238 }
2239#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
2240
Gilles Peskine449bd832023-01-11 14:50:10 +01002241 psa_set_key_algorithm(&key_attributes, alg);
2242 psa_set_key_type(&key_attributes, PSA_KEY_TYPE_DERIVE);
Neil Armstrong501c9322022-05-03 09:35:09 +02002243
Gilles Peskine449bd832023-01-11 14:50:10 +01002244 status = psa_import_key(&key_attributes, psk, psk_len, &key);
2245 if (status != PSA_SUCCESS) {
2246 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2247 }
Neil Armstrong501c9322022-05-03 09:35:09 +02002248
2249 /* Allow calling psa_destroy_key() on psk remove */
2250 ssl->handshake->psk_opaque_is_internal = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01002251 return mbedtls_ssl_set_hs_psk_opaque(ssl, key);
Neil Armstrong501c9322022-05-03 09:35:09 +02002252#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002253 if ((ssl->handshake->psk = mbedtls_calloc(1, psk_len)) == NULL) {
2254 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2255 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002256
2257 ssl->handshake->psk_len = psk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002258 memcpy(ssl->handshake->psk, psk, ssl->handshake->psk_len);
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002259
Gilles Peskine449bd832023-01-11 14:50:10 +01002260 return 0;
Neil Armstrong501c9322022-05-03 09:35:09 +02002261#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002262}
2263
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002264#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002265int mbedtls_ssl_conf_psk_opaque(mbedtls_ssl_config *conf,
2266 mbedtls_svc_key_id_t psk,
2267 const unsigned char *psk_identity,
2268 size_t psk_identity_len)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002269{
Janos Follath865b3eb2019-12-16 11:46:15 +00002270 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002271
2272 /* We currently only support one PSK, raw or opaque. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002273 if (mbedtls_ssl_conf_has_static_psk(conf)) {
2274 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
2275 }
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002276
Hanno Becker7390c712018-11-15 13:33:04 +00002277 /* Check and set opaque PSK */
Gilles Peskine449bd832023-01-11 14:50:10 +01002278 if (mbedtls_svc_key_id_is_null(psk)) {
2279 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2280 }
Ronald Croncf56a0a2020-08-04 09:51:30 +02002281 conf->psk_opaque = psk;
Hanno Becker7390c712018-11-15 13:33:04 +00002282
2283 /* Check and set PSK Identity */
Gilles Peskine449bd832023-01-11 14:50:10 +01002284 ret = ssl_conf_set_psk_identity(conf, psk_identity,
2285 psk_identity_len);
2286 if (ret != 0) {
2287 ssl_conf_remove_psk(conf);
2288 }
Hanno Becker7390c712018-11-15 13:33:04 +00002289
Gilles Peskine449bd832023-01-11 14:50:10 +01002290 return ret;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002291}
2292
Gilles Peskine449bd832023-01-11 14:50:10 +01002293int mbedtls_ssl_set_hs_psk_opaque(mbedtls_ssl_context *ssl,
2294 mbedtls_svc_key_id_t psk)
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002295{
Gilles Peskine449bd832023-01-11 14:50:10 +01002296 if ((mbedtls_svc_key_id_is_null(psk)) ||
2297 (ssl->handshake == NULL)) {
2298 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2299 }
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002300
Gilles Peskine449bd832023-01-11 14:50:10 +01002301 ssl_remove_psk(ssl);
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002302 ssl->handshake->psk_opaque = psk;
Gilles Peskine449bd832023-01-11 14:50:10 +01002303 return 0;
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002304}
2305#endif /* MBEDTLS_USE_PSA_CRYPTO */
2306
Jerry Yu8897c072022-08-12 13:56:53 +08002307#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002308void mbedtls_ssl_conf_psk_cb(mbedtls_ssl_config *conf,
2309 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
2310 size_t),
2311 void *p_psk)
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002312{
2313 conf->f_psk = f_psk;
2314 conf->p_psk = p_psk;
2315}
Jerry Yu8897c072022-08-12 13:56:53 +08002316#endif /* MBEDTLS_SSL_SRV_C */
2317
Ronald Cron73fe8df2022-10-05 14:31:43 +02002318#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002319
2320#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine301711e2022-04-26 16:57:05 +02002321static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
Gilles Peskine449bd832023-01-11 14:50:10 +01002322 psa_algorithm_t alg)
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002323{
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002324#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01002325 if (alg == PSA_ALG_CBC_NO_PADDING) {
2326 return MBEDTLS_SSL_MODE_CBC;
2327 }
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002328#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
Gilles Peskine449bd832023-01-11 14:50:10 +01002329 if (PSA_ALG_IS_AEAD(alg)) {
2330 return MBEDTLS_SSL_MODE_AEAD;
2331 }
2332 return MBEDTLS_SSL_MODE_STREAM;
Gilles Peskine301711e2022-04-26 16:57:05 +02002333}
2334
2335#else /* MBEDTLS_USE_PSA_CRYPTO */
2336
2337static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
Gilles Peskine449bd832023-01-11 14:50:10 +01002338 mbedtls_cipher_mode_t mode)
Gilles Peskine301711e2022-04-26 16:57:05 +02002339{
2340#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01002341 if (mode == MBEDTLS_MODE_CBC) {
2342 return MBEDTLS_SSL_MODE_CBC;
2343 }
Gilles Peskine301711e2022-04-26 16:57:05 +02002344#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
2345
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002346#if defined(MBEDTLS_GCM_C) || \
2347 defined(MBEDTLS_CCM_C) || \
2348 defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002349 if (mode == MBEDTLS_MODE_GCM ||
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002350 mode == MBEDTLS_MODE_CCM ||
Gilles Peskine449bd832023-01-11 14:50:10 +01002351 mode == MBEDTLS_MODE_CHACHAPOLY) {
2352 return MBEDTLS_SSL_MODE_AEAD;
2353 }
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002354#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002355
Gilles Peskine449bd832023-01-11 14:50:10 +01002356 return MBEDTLS_SSL_MODE_STREAM;
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002357}
Gilles Peskine301711e2022-04-26 16:57:05 +02002358#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002359
Gilles Peskinee108d982022-04-26 16:50:40 +02002360static mbedtls_ssl_mode_t mbedtls_ssl_get_actual_mode(
2361 mbedtls_ssl_mode_t base_mode,
Gilles Peskine449bd832023-01-11 14:50:10 +01002362 int encrypt_then_mac)
Gilles Peskinee108d982022-04-26 16:50:40 +02002363{
2364#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01002365 if (encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED &&
2366 base_mode == MBEDTLS_SSL_MODE_CBC) {
2367 return MBEDTLS_SSL_MODE_CBC_ETM;
Gilles Peskinee108d982022-04-26 16:50:40 +02002368 }
2369#else
2370 (void) encrypt_then_mac;
2371#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002372 return base_mode;
Gilles Peskinee108d982022-04-26 16:50:40 +02002373}
2374
Neil Armstrongab555e02022-04-04 11:07:59 +02002375mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_transform(
Gilles Peskine449bd832023-01-11 14:50:10 +01002376 const mbedtls_ssl_transform *transform)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002377{
Gilles Peskinee108d982022-04-26 16:50:40 +02002378 mbedtls_ssl_mode_t base_mode = mbedtls_ssl_get_base_mode(
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002379#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002380 transform->psa_alg
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002381#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002382 mbedtls_cipher_get_cipher_mode(&transform->cipher_ctx_enc)
Gilles Peskinee108d982022-04-26 16:50:40 +02002383#endif
2384 );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002385
Gilles Peskinee108d982022-04-26 16:50:40 +02002386 int encrypt_then_mac = 0;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002387#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskinee108d982022-04-26 16:50:40 +02002388 encrypt_then_mac = transform->encrypt_then_mac;
2389#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002390 return mbedtls_ssl_get_actual_mode(base_mode, encrypt_then_mac);
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002391}
2392
Neil Armstrongab555e02022-04-04 11:07:59 +02002393mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002394#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01002395 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002396#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01002397 const mbedtls_ssl_ciphersuite_t *suite)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002398{
Gilles Peskinee108d982022-04-26 16:50:40 +02002399 mbedtls_ssl_mode_t base_mode = MBEDTLS_SSL_MODE_STREAM;
2400
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002401#if defined(MBEDTLS_USE_PSA_CRYPTO)
2402 psa_status_t status;
2403 psa_algorithm_t alg;
2404 psa_key_type_t type;
2405 size_t size;
Gilles Peskine449bd832023-01-11 14:50:10 +01002406 status = mbedtls_ssl_cipher_to_psa(suite->cipher, 0, &alg, &type, &size);
2407 if (status == PSA_SUCCESS) {
2408 base_mode = mbedtls_ssl_get_base_mode(alg);
2409 }
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002410#else
2411 const mbedtls_cipher_info_t *cipher =
Gilles Peskine449bd832023-01-11 14:50:10 +01002412 mbedtls_cipher_info_from_type(suite->cipher);
2413 if (cipher != NULL) {
Gilles Peskinee108d982022-04-26 16:50:40 +02002414 base_mode =
2415 mbedtls_ssl_get_base_mode(
Gilles Peskine449bd832023-01-11 14:50:10 +01002416 mbedtls_cipher_info_get_mode(cipher));
Gilles Peskinee108d982022-04-26 16:50:40 +02002417 }
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002418#endif /* MBEDTLS_USE_PSA_CRYPTO */
2419
Gilles Peskinee108d982022-04-26 16:50:40 +02002420#if !defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
2421 int encrypt_then_mac = 0;
2422#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002423 return mbedtls_ssl_get_actual_mode(base_mode, encrypt_then_mac);
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002424}
2425
Neil Armstrong8395d7a2022-05-18 11:44:56 +02002426#if defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu251a12e2022-07-13 15:15:48 +08002427
2428#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu438ddd82022-07-07 06:55:50 +00002429/* Serialization of TLS 1.3 sessions:
2430 *
2431 * struct {
Xiaokang Qian126bf8e2022-10-13 02:22:40 +00002432 * opaque hostname<0..2^16-1>;
Jerry Yu438ddd82022-07-07 06:55:50 +00002433 * uint64 ticket_received;
2434 * uint32 ticket_lifetime;
Jerry Yu34191072022-08-18 10:32:09 +08002435 * opaque ticket<1..2^16-1>;
Jerry Yu438ddd82022-07-07 06:55:50 +00002436 * } ClientOnlyData;
2437 *
2438 * struct {
2439 * uint8 endpoint;
2440 * uint8 ciphersuite[2];
2441 * uint32 ticket_age_add;
2442 * uint8 ticket_flags;
2443 * opaque resumption_key<0..255>;
2444 * select ( endpoint ) {
2445 * case client: ClientOnlyData;
2446 * case server: uint64 start_time;
2447 * };
2448 * } serialized_session_tls13;
2449 *
2450 */
2451#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yue36fdd62022-08-17 21:31:36 +08002452MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002453static int ssl_tls13_session_save(const mbedtls_ssl_session *session,
2454 unsigned char *buf,
2455 size_t buf_len,
2456 size_t *olen)
Jerry Yu438ddd82022-07-07 06:55:50 +00002457{
2458 unsigned char *p = buf;
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00002459#if defined(MBEDTLS_SSL_CLI_C) && \
2460 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01002461 size_t hostname_len = (session->hostname == NULL) ?
2462 0 : strlen(session->hostname) + 1;
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00002463#endif
Jerry Yubc7c1a42022-07-21 22:57:37 +08002464 size_t needed = 1 /* endpoint */
2465 + 2 /* ciphersuite */
2466 + 4 /* ticket_age_add */
Jerry Yu34191072022-08-18 10:32:09 +08002467 + 1 /* ticket_flags */
2468 + 1; /* resumption_key length */
Jerry Yue36fdd62022-08-17 21:31:36 +08002469 *olen = 0;
Jerry Yu34191072022-08-18 10:32:09 +08002470
Gilles Peskine449bd832023-01-11 14:50:10 +01002471 if (session->resumption_key_len > MBEDTLS_SSL_TLS1_3_TICKET_RESUMPTION_KEY_LEN) {
2472 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2473 }
Jerry Yu34191072022-08-18 10:32:09 +08002474 needed += session->resumption_key_len; /* resumption_key */
2475
Jerry Yu438ddd82022-07-07 06:55:50 +00002476#if defined(MBEDTLS_HAVE_TIME)
2477 needed += 8; /* start_time or ticket_received */
2478#endif
2479
2480#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002481 if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Xiaokang Qian126bf8e2022-10-13 02:22:40 +00002482#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaokang Qianbc663a02022-10-09 11:14:39 +00002483 needed += 2 /* hostname_len */
Gilles Peskine449bd832023-01-11 14:50:10 +01002484 + hostname_len; /* hostname */
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00002485#endif
2486
Jerry Yu438ddd82022-07-07 06:55:50 +00002487 needed += 4 /* ticket_lifetime */
Jerry Yue36fdd62022-08-17 21:31:36 +08002488 + 2; /* ticket_len */
Jerry Yu34191072022-08-18 10:32:09 +08002489
2490 /* Check size_t overflow */
Gilles Peskine449bd832023-01-11 14:50:10 +01002491 if (session->ticket_len > SIZE_MAX - needed) {
2492 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2493 }
Jerry Yu34191072022-08-18 10:32:09 +08002494
Jerry Yue28d9742022-08-18 15:44:03 +08002495 needed += session->ticket_len; /* ticket */
Jerry Yu438ddd82022-07-07 06:55:50 +00002496 }
2497#endif /* MBEDTLS_SSL_CLI_C */
2498
Jerry Yue36fdd62022-08-17 21:31:36 +08002499 *olen = needed;
Gilles Peskine449bd832023-01-11 14:50:10 +01002500 if (needed > buf_len) {
2501 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
2502 }
Jerry Yu438ddd82022-07-07 06:55:50 +00002503
2504 p[0] = session->endpoint;
Gilles Peskine449bd832023-01-11 14:50:10 +01002505 MBEDTLS_PUT_UINT16_BE(session->ciphersuite, p, 1);
2506 MBEDTLS_PUT_UINT32_BE(session->ticket_age_add, p, 3);
Jerry Yu438ddd82022-07-07 06:55:50 +00002507 p[7] = session->ticket_flags;
2508
2509 /* save resumption_key */
Jerry Yubc7c1a42022-07-21 22:57:37 +08002510 p[8] = session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002511 p += 9;
Gilles Peskine449bd832023-01-11 14:50:10 +01002512 memcpy(p, session->resumption_key, session->resumption_key_len);
Jerry Yubc7c1a42022-07-21 22:57:37 +08002513 p += session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002514
2515#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002516 if (session->endpoint == MBEDTLS_SSL_IS_SERVER) {
2517 MBEDTLS_PUT_UINT64_BE((uint64_t) session->start, p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002518 p += 8;
2519 }
2520#endif /* MBEDTLS_HAVE_TIME */
2521
2522#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002523 if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002524#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01002525 MBEDTLS_PUT_UINT16_BE(hostname_len, p, 0);
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002526 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002527 if (hostname_len > 0) {
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002528 /* save host name */
Gilles Peskine449bd832023-01-11 14:50:10 +01002529 memcpy(p, session->hostname, hostname_len);
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002530 p += hostname_len;
2531 }
2532#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
2533
Jerry Yu438ddd82022-07-07 06:55:50 +00002534#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01002535 MBEDTLS_PUT_UINT64_BE((uint64_t) session->ticket_received, p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002536 p += 8;
2537#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002538 MBEDTLS_PUT_UINT32_BE(session->ticket_lifetime, p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002539 p += 4;
2540
Gilles Peskine449bd832023-01-11 14:50:10 +01002541 MBEDTLS_PUT_UINT16_BE(session->ticket_len, p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002542 p += 2;
Jerry Yu34191072022-08-18 10:32:09 +08002543
Gilles Peskine449bd832023-01-11 14:50:10 +01002544 if (session->ticket != NULL && session->ticket_len > 0) {
2545 memcpy(p, session->ticket, session->ticket_len);
Jerry Yu438ddd82022-07-07 06:55:50 +00002546 p += session->ticket_len;
2547 }
2548 }
2549#endif /* MBEDTLS_SSL_CLI_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01002550 return 0;
Jerry Yu438ddd82022-07-07 06:55:50 +00002551}
2552
2553MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002554static int ssl_tls13_session_load(mbedtls_ssl_session *session,
2555 const unsigned char *buf,
2556 size_t len)
Jerry Yu438ddd82022-07-07 06:55:50 +00002557{
2558 const unsigned char *p = buf;
2559 const unsigned char *end = buf + len;
2560
Gilles Peskine449bd832023-01-11 14:50:10 +01002561 if (end - p < 9) {
2562 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2563 }
Jerry Yu438ddd82022-07-07 06:55:50 +00002564 session->endpoint = p[0];
Gilles Peskine449bd832023-01-11 14:50:10 +01002565 session->ciphersuite = MBEDTLS_GET_UINT16_BE(p, 1);
2566 session->ticket_age_add = MBEDTLS_GET_UINT32_BE(p, 3);
Jerry Yu438ddd82022-07-07 06:55:50 +00002567 session->ticket_flags = p[7];
2568
2569 /* load resumption_key */
Jerry Yubc7c1a42022-07-21 22:57:37 +08002570 session->resumption_key_len = p[8];
Jerry Yu438ddd82022-07-07 06:55:50 +00002571 p += 9;
2572
Gilles Peskine449bd832023-01-11 14:50:10 +01002573 if (end - p < session->resumption_key_len) {
2574 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2575 }
Jerry Yu438ddd82022-07-07 06:55:50 +00002576
Gilles Peskine449bd832023-01-11 14:50:10 +01002577 if (sizeof(session->resumption_key) < session->resumption_key_len) {
2578 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2579 }
2580 memcpy(session->resumption_key, p, session->resumption_key_len);
Jerry Yubc7c1a42022-07-21 22:57:37 +08002581 p += session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002582
2583#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002584 if (session->endpoint == MBEDTLS_SSL_IS_SERVER) {
2585 if (end - p < 8) {
2586 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2587 }
2588 session->start = MBEDTLS_GET_UINT64_BE(p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002589 p += 8;
2590 }
2591#endif /* MBEDTLS_HAVE_TIME */
2592
2593#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002594 if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002595#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01002596 defined(MBEDTLS_SSL_SESSION_TICKETS)
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002597 size_t hostname_len;
2598 /* load host name */
Gilles Peskine449bd832023-01-11 14:50:10 +01002599 if (end - p < 2) {
2600 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2601 }
2602 hostname_len = MBEDTLS_GET_UINT16_BE(p, 0);
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002603 p += 2;
2604
Gilles Peskine449bd832023-01-11 14:50:10 +01002605 if (end - p < (long int) hostname_len) {
2606 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2607 }
2608 if (hostname_len > 0) {
2609 session->hostname = mbedtls_calloc(1, hostname_len);
2610 if (session->hostname == NULL) {
2611 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2612 }
2613 memcpy(session->hostname, p, hostname_len);
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002614 p += hostname_len;
2615 }
2616#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION &&
2617 MBEDTLS_SSL_SESSION_TICKETS */
2618
Jerry Yu438ddd82022-07-07 06:55:50 +00002619#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01002620 if (end - p < 8) {
2621 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2622 }
2623 session->ticket_received = MBEDTLS_GET_UINT64_BE(p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002624 p += 8;
2625#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002626 if (end - p < 4) {
2627 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2628 }
2629 session->ticket_lifetime = MBEDTLS_GET_UINT32_BE(p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002630 p += 4;
2631
Gilles Peskine449bd832023-01-11 14:50:10 +01002632 if (end - p < 2) {
2633 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2634 }
2635 session->ticket_len = MBEDTLS_GET_UINT16_BE(p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002636 p += 2;
2637
Gilles Peskine449bd832023-01-11 14:50:10 +01002638 if (end - p < (long int) session->ticket_len) {
2639 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2640 }
2641 if (session->ticket_len > 0) {
2642 session->ticket = mbedtls_calloc(1, session->ticket_len);
2643 if (session->ticket == NULL) {
2644 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2645 }
2646 memcpy(session->ticket, p, session->ticket_len);
Jerry Yu438ddd82022-07-07 06:55:50 +00002647 p += session->ticket_len;
2648 }
2649 }
2650#endif /* MBEDTLS_SSL_CLI_C */
2651
Gilles Peskine449bd832023-01-11 14:50:10 +01002652 return 0;
Jerry Yu438ddd82022-07-07 06:55:50 +00002653
2654}
2655#else /* MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yue36fdd62022-08-17 21:31:36 +08002656MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002657static int ssl_tls13_session_save(const mbedtls_ssl_session *session,
2658 unsigned char *buf,
2659 size_t buf_len,
2660 size_t *olen)
Jerry Yu251a12e2022-07-13 15:15:48 +08002661{
2662 ((void) session);
2663 ((void) buf);
2664 ((void) buf_len);
Jerry Yue36fdd62022-08-17 21:31:36 +08002665 *olen = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002666 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Jerry Yu251a12e2022-07-13 15:15:48 +08002667}
Jerry Yu438ddd82022-07-07 06:55:50 +00002668
Gilles Peskine449bd832023-01-11 14:50:10 +01002669static int ssl_tls13_session_load(const mbedtls_ssl_session *session,
2670 unsigned char *buf,
2671 size_t buf_len)
Jerry Yu438ddd82022-07-07 06:55:50 +00002672{
2673 ((void) session);
2674 ((void) buf);
2675 ((void) buf_len);
Gilles Peskine449bd832023-01-11 14:50:10 +01002676 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Jerry Yu438ddd82022-07-07 06:55:50 +00002677}
2678#endif /* !MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yu251a12e2022-07-13 15:15:48 +08002679#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
2680
Gilles Peskine449bd832023-01-11 14:50:10 +01002681psa_status_t mbedtls_ssl_cipher_to_psa(mbedtls_cipher_type_t mbedtls_cipher_type,
2682 size_t taglen,
2683 psa_algorithm_t *alg,
2684 psa_key_type_t *key_type,
2685 size_t *key_size)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002686{
Gilles Peskine449bd832023-01-11 14:50:10 +01002687 switch (mbedtls_cipher_type) {
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002688 case MBEDTLS_CIPHER_AES_128_CBC:
2689 *alg = PSA_ALG_CBC_NO_PADDING;
2690 *key_type = PSA_KEY_TYPE_AES;
2691 *key_size = 128;
2692 break;
2693 case MBEDTLS_CIPHER_AES_128_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002694 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002695 *key_type = PSA_KEY_TYPE_AES;
2696 *key_size = 128;
2697 break;
2698 case MBEDTLS_CIPHER_AES_128_GCM:
2699 *alg = PSA_ALG_GCM;
2700 *key_type = PSA_KEY_TYPE_AES;
2701 *key_size = 128;
2702 break;
2703 case MBEDTLS_CIPHER_AES_192_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002704 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002705 *key_type = PSA_KEY_TYPE_AES;
2706 *key_size = 192;
2707 break;
2708 case MBEDTLS_CIPHER_AES_192_GCM:
2709 *alg = PSA_ALG_GCM;
2710 *key_type = PSA_KEY_TYPE_AES;
2711 *key_size = 192;
2712 break;
2713 case MBEDTLS_CIPHER_AES_256_CBC:
2714 *alg = PSA_ALG_CBC_NO_PADDING;
2715 *key_type = PSA_KEY_TYPE_AES;
2716 *key_size = 256;
2717 break;
2718 case MBEDTLS_CIPHER_AES_256_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 = 256;
2722 break;
2723 case MBEDTLS_CIPHER_AES_256_GCM:
2724 *alg = PSA_ALG_GCM;
2725 *key_type = PSA_KEY_TYPE_AES;
2726 *key_size = 256;
2727 break;
2728 case MBEDTLS_CIPHER_ARIA_128_CBC:
2729 *alg = PSA_ALG_CBC_NO_PADDING;
2730 *key_type = PSA_KEY_TYPE_ARIA;
2731 *key_size = 128;
2732 break;
2733 case MBEDTLS_CIPHER_ARIA_128_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_ARIA;
2736 *key_size = 128;
2737 break;
2738 case MBEDTLS_CIPHER_ARIA_128_GCM:
2739 *alg = PSA_ALG_GCM;
2740 *key_type = PSA_KEY_TYPE_ARIA;
2741 *key_size = 128;
2742 break;
2743 case MBEDTLS_CIPHER_ARIA_192_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002744 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002745 *key_type = PSA_KEY_TYPE_ARIA;
2746 *key_size = 192;
2747 break;
2748 case MBEDTLS_CIPHER_ARIA_192_GCM:
2749 *alg = PSA_ALG_GCM;
2750 *key_type = PSA_KEY_TYPE_ARIA;
2751 *key_size = 192;
2752 break;
2753 case MBEDTLS_CIPHER_ARIA_256_CBC:
2754 *alg = PSA_ALG_CBC_NO_PADDING;
2755 *key_type = PSA_KEY_TYPE_ARIA;
2756 *key_size = 256;
2757 break;
2758 case MBEDTLS_CIPHER_ARIA_256_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 = 256;
2762 break;
2763 case MBEDTLS_CIPHER_ARIA_256_GCM:
2764 *alg = PSA_ALG_GCM;
2765 *key_type = PSA_KEY_TYPE_ARIA;
2766 *key_size = 256;
2767 break;
2768 case MBEDTLS_CIPHER_CAMELLIA_128_CBC:
2769 *alg = PSA_ALG_CBC_NO_PADDING;
2770 *key_type = PSA_KEY_TYPE_CAMELLIA;
2771 *key_size = 128;
2772 break;
2773 case MBEDTLS_CIPHER_CAMELLIA_128_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_CAMELLIA;
2776 *key_size = 128;
2777 break;
2778 case MBEDTLS_CIPHER_CAMELLIA_128_GCM:
2779 *alg = PSA_ALG_GCM;
2780 *key_type = PSA_KEY_TYPE_CAMELLIA;
2781 *key_size = 128;
2782 break;
2783 case MBEDTLS_CIPHER_CAMELLIA_192_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002784 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002785 *key_type = PSA_KEY_TYPE_CAMELLIA;
2786 *key_size = 192;
2787 break;
2788 case MBEDTLS_CIPHER_CAMELLIA_192_GCM:
2789 *alg = PSA_ALG_GCM;
2790 *key_type = PSA_KEY_TYPE_CAMELLIA;
2791 *key_size = 192;
2792 break;
2793 case MBEDTLS_CIPHER_CAMELLIA_256_CBC:
2794 *alg = PSA_ALG_CBC_NO_PADDING;
2795 *key_type = PSA_KEY_TYPE_CAMELLIA;
2796 *key_size = 256;
2797 break;
2798 case MBEDTLS_CIPHER_CAMELLIA_256_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 = 256;
2802 break;
2803 case MBEDTLS_CIPHER_CAMELLIA_256_GCM:
2804 *alg = PSA_ALG_GCM;
2805 *key_type = PSA_KEY_TYPE_CAMELLIA;
2806 *key_size = 256;
2807 break;
2808 case MBEDTLS_CIPHER_CHACHA20_POLY1305:
2809 *alg = PSA_ALG_CHACHA20_POLY1305;
2810 *key_type = PSA_KEY_TYPE_CHACHA20;
2811 *key_size = 256;
2812 break;
2813 case MBEDTLS_CIPHER_NULL:
2814 *alg = MBEDTLS_SSL_NULL_CIPHER;
2815 *key_type = 0;
2816 *key_size = 0;
2817 break;
2818 default:
2819 return PSA_ERROR_NOT_SUPPORTED;
2820 }
2821
2822 return PSA_SUCCESS;
2823}
Neil Armstrong8395d7a2022-05-18 11:44:56 +02002824#endif /* MBEDTLS_USE_PSA_CRYPTO || MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002825
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002826#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002827int mbedtls_ssl_conf_dh_param_bin(mbedtls_ssl_config *conf,
2828 const unsigned char *dhm_P, size_t P_len,
2829 const unsigned char *dhm_G, size_t G_len)
Hanno Beckera90658f2017-10-04 15:29:08 +01002830{
Janos Follath865b3eb2019-12-16 11:46:15 +00002831 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckera90658f2017-10-04 15:29:08 +01002832
Gilles Peskine449bd832023-01-11 14:50:10 +01002833 mbedtls_mpi_free(&conf->dhm_P);
2834 mbedtls_mpi_free(&conf->dhm_G);
Glenn Strausscee11292021-12-20 01:43:17 -05002835
Gilles Peskine449bd832023-01-11 14:50:10 +01002836 if ((ret = mbedtls_mpi_read_binary(&conf->dhm_P, dhm_P, P_len)) != 0 ||
2837 (ret = mbedtls_mpi_read_binary(&conf->dhm_G, dhm_G, G_len)) != 0) {
2838 mbedtls_mpi_free(&conf->dhm_P);
2839 mbedtls_mpi_free(&conf->dhm_G);
2840 return ret;
Hanno Beckera90658f2017-10-04 15:29:08 +01002841 }
2842
Gilles Peskine449bd832023-01-11 14:50:10 +01002843 return 0;
Hanno Beckera90658f2017-10-04 15:29:08 +01002844}
Paul Bakker5121ce52009-01-03 21:22:43 +00002845
Gilles Peskine449bd832023-01-11 14:50:10 +01002846int mbedtls_ssl_conf_dh_param_ctx(mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx)
Paul Bakker1b57b062011-01-06 15:48:19 +00002847{
Janos Follath865b3eb2019-12-16 11:46:15 +00002848 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1b57b062011-01-06 15:48:19 +00002849
Gilles Peskine449bd832023-01-11 14:50:10 +01002850 mbedtls_mpi_free(&conf->dhm_P);
2851 mbedtls_mpi_free(&conf->dhm_G);
Glenn Strausscee11292021-12-20 01:43:17 -05002852
Gilles Peskine449bd832023-01-11 14:50:10 +01002853 if ((ret = mbedtls_dhm_get_value(dhm_ctx, MBEDTLS_DHM_PARAM_P,
2854 &conf->dhm_P)) != 0 ||
2855 (ret = mbedtls_dhm_get_value(dhm_ctx, MBEDTLS_DHM_PARAM_G,
2856 &conf->dhm_G)) != 0) {
2857 mbedtls_mpi_free(&conf->dhm_P);
2858 mbedtls_mpi_free(&conf->dhm_G);
2859 return ret;
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01002860 }
Paul Bakker1b57b062011-01-06 15:48:19 +00002861
Gilles Peskine449bd832023-01-11 14:50:10 +01002862 return 0;
Paul Bakker1b57b062011-01-06 15:48:19 +00002863}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002864#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00002865
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02002866#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
2867/*
2868 * Set the minimum length for Diffie-Hellman parameters
2869 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002870void mbedtls_ssl_conf_dhm_min_bitlen(mbedtls_ssl_config *conf,
2871 unsigned int bitlen)
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02002872{
2873 conf->dhm_min_bitlen = bitlen;
2874}
2875#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
2876
Ronald Crone68ab4f2022-10-05 12:46:29 +02002877#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002878#if !defined(MBEDTLS_DEPRECATED_REMOVED) && defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002879/*
2880 * Set allowed/preferred hashes for handshake signatures
2881 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002882void mbedtls_ssl_conf_sig_hashes(mbedtls_ssl_config *conf,
2883 const int *hashes)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002884{
2885 conf->sig_hashes = hashes;
2886}
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002887#endif /* !MBEDTLS_DEPRECATED_REMOVED && MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker1cd6e002021-08-10 13:27:10 +01002888
Jerry Yuf017ee42022-01-12 15:49:48 +08002889/* Configure allowed signature algorithms for handshake */
Gilles Peskine449bd832023-01-11 14:50:10 +01002890void mbedtls_ssl_conf_sig_algs(mbedtls_ssl_config *conf,
2891 const uint16_t *sig_algs)
Hanno Becker1cd6e002021-08-10 13:27:10 +01002892{
Jerry Yuf017ee42022-01-12 15:49:48 +08002893#if !defined(MBEDTLS_DEPRECATED_REMOVED)
2894 conf->sig_hashes = NULL;
2895#endif /* !MBEDTLS_DEPRECATED_REMOVED */
2896 conf->sig_algs = sig_algs;
Hanno Becker1cd6e002021-08-10 13:27:10 +01002897}
Ronald Crone68ab4f2022-10-05 12:46:29 +02002898#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002899
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02002900#if defined(MBEDTLS_ECP_C)
Brett Warrene0edc842021-08-17 09:53:13 +01002901#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002902/*
2903 * Set the allowed elliptic curves
Brett Warrene0edc842021-08-17 09:53:13 +01002904 *
2905 * mbedtls_ssl_setup() takes the provided list
2906 * and translates it to a list of IANA TLS group identifiers,
2907 * stored in ssl->handshake->group_list.
2908 *
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002909 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002910void mbedtls_ssl_conf_curves(mbedtls_ssl_config *conf,
2911 const mbedtls_ecp_group_id *curve_list)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002912{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002913 conf->curve_list = curve_list;
Brett Warrene0edc842021-08-17 09:53:13 +01002914 conf->group_list = NULL;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002915}
Brett Warrene0edc842021-08-17 09:53:13 +01002916#endif /* MBEDTLS_DEPRECATED_REMOVED */
Hanno Becker947194e2017-04-07 13:25:49 +01002917#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002918
Brett Warrene0edc842021-08-17 09:53:13 +01002919/*
2920 * Set the allowed groups
2921 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002922void mbedtls_ssl_conf_groups(mbedtls_ssl_config *conf,
2923 const uint16_t *group_list)
Brett Warrene0edc842021-08-17 09:53:13 +01002924{
2925#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
2926 conf->curve_list = NULL;
2927#endif
2928 conf->group_list = group_list;
2929}
2930
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002931#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002932int mbedtls_ssl_set_hostname(mbedtls_ssl_context *ssl, const char *hostname)
Paul Bakker5121ce52009-01-03 21:22:43 +00002933{
Hanno Becker947194e2017-04-07 13:25:49 +01002934 /* Initialize to suppress unnecessary compiler warning */
2935 size_t hostname_len = 0;
2936
2937 /* Check if new hostname is valid before
2938 * making any change to current one */
Gilles Peskine449bd832023-01-11 14:50:10 +01002939 if (hostname != NULL) {
2940 hostname_len = strlen(hostname);
Hanno Becker947194e2017-04-07 13:25:49 +01002941
Gilles Peskine449bd832023-01-11 14:50:10 +01002942 if (hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN) {
2943 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2944 }
Hanno Becker947194e2017-04-07 13:25:49 +01002945 }
2946
2947 /* Now it's clear that we will overwrite the old hostname,
2948 * so we can free it safely */
2949
Gilles Peskine449bd832023-01-11 14:50:10 +01002950 if (ssl->hostname != NULL) {
2951 mbedtls_platform_zeroize(ssl->hostname, strlen(ssl->hostname));
2952 mbedtls_free(ssl->hostname);
Hanno Becker947194e2017-04-07 13:25:49 +01002953 }
2954
2955 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01002956
Gilles Peskine449bd832023-01-11 14:50:10 +01002957 if (hostname == NULL) {
Hanno Becker947194e2017-04-07 13:25:49 +01002958 ssl->hostname = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01002959 } else {
2960 ssl->hostname = mbedtls_calloc(1, hostname_len + 1);
2961 if (ssl->hostname == NULL) {
2962 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2963 }
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002964
Gilles Peskine449bd832023-01-11 14:50:10 +01002965 memcpy(ssl->hostname, hostname, hostname_len);
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002966
Hanno Becker947194e2017-04-07 13:25:49 +01002967 ssl->hostname[hostname_len] = '\0';
2968 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002969
Gilles Peskine449bd832023-01-11 14:50:10 +01002970 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002971}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01002972#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002973
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002974#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01002975void mbedtls_ssl_conf_sni(mbedtls_ssl_config *conf,
2976 int (*f_sni)(void *, mbedtls_ssl_context *,
2977 const unsigned char *, size_t),
2978 void *p_sni)
Paul Bakker5701cdc2012-09-27 21:49:42 +00002979{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002980 conf->f_sni = f_sni;
2981 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00002982}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002983#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00002984
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002985#if defined(MBEDTLS_SSL_ALPN)
Gilles Peskine449bd832023-01-11 14:50:10 +01002986int mbedtls_ssl_conf_alpn_protocols(mbedtls_ssl_config *conf, const char **protos)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002987{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002988 size_t cur_len, tot_len;
2989 const char **p;
2990
2991 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08002992 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
2993 * MUST NOT be truncated."
2994 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002995 */
2996 tot_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002997 for (p = protos; *p != NULL; p++) {
2998 cur_len = strlen(*p);
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002999 tot_len += cur_len;
3000
Gilles Peskine449bd832023-01-11 14:50:10 +01003001 if ((cur_len == 0) ||
3002 (cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN) ||
3003 (tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN)) {
3004 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3005 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02003006 }
3007
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003008 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02003009
Gilles Peskine449bd832023-01-11 14:50:10 +01003010 return 0;
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003011}
3012
Gilles Peskine449bd832023-01-11 14:50:10 +01003013const char *mbedtls_ssl_get_alpn_protocol(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003014{
Gilles Peskine449bd832023-01-11 14:50:10 +01003015 return ssl->alpn_chosen;
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003016}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003017#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003018
Johan Pascalb62bb512015-12-03 21:56:45 +01003019#if defined(MBEDTLS_SSL_DTLS_SRTP)
Gilles Peskine449bd832023-01-11 14:50:10 +01003020void mbedtls_ssl_conf_srtp_mki_value_supported(mbedtls_ssl_config *conf,
3021 int support_mki_value)
Ron Eldor591f1622018-01-22 12:30:04 +02003022{
3023 conf->dtls_srtp_mki_support = support_mki_value;
3024}
3025
Gilles Peskine449bd832023-01-11 14:50:10 +01003026int mbedtls_ssl_dtls_srtp_set_mki_value(mbedtls_ssl_context *ssl,
3027 unsigned char *mki_value,
3028 uint16_t mki_len)
Ron Eldor591f1622018-01-22 12:30:04 +02003029{
Gilles Peskine449bd832023-01-11 14:50:10 +01003030 if (mki_len > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH) {
3031 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Ron Eldora9788042018-12-05 11:04:31 +02003032 }
Ron Eldor591f1622018-01-22 12:30:04 +02003033
Gilles Peskine449bd832023-01-11 14:50:10 +01003034 if (ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_UNSUPPORTED) {
3035 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Ron Eldora9788042018-12-05 11:04:31 +02003036 }
Ron Eldor591f1622018-01-22 12:30:04 +02003037
Gilles Peskine449bd832023-01-11 14:50:10 +01003038 memcpy(ssl->dtls_srtp_info.mki_value, mki_value, mki_len);
Ron Eldor591f1622018-01-22 12:30:04 +02003039 ssl->dtls_srtp_info.mki_len = mki_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01003040 return 0;
Ron Eldor591f1622018-01-22 12:30:04 +02003041}
3042
Gilles Peskine449bd832023-01-11 14:50:10 +01003043int mbedtls_ssl_conf_dtls_srtp_protection_profiles(mbedtls_ssl_config *conf,
3044 const mbedtls_ssl_srtp_profile *profiles)
Johan Pascalb62bb512015-12-03 21:56:45 +01003045{
Johan Pascal253d0262020-09-22 13:04:45 +02003046 const mbedtls_ssl_srtp_profile *p;
3047 size_t list_size = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01003048
Johan Pascal253d0262020-09-22 13:04:45 +02003049 /* check the profiles list: all entry must be valid,
3050 * its size cannot be more than the total number of supported profiles, currently 4 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003051 for (p = profiles; *p != MBEDTLS_TLS_SRTP_UNSET &&
3052 list_size <= MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH;
3053 p++) {
3054 if (mbedtls_ssl_check_srtp_profile_value(*p) != MBEDTLS_TLS_SRTP_UNSET) {
Johan Pascal76fdf1d2020-10-22 23:31:00 +02003055 list_size++;
Gilles Peskine449bd832023-01-11 14:50:10 +01003056 } else {
Johan Pascal76fdf1d2020-10-22 23:31:00 +02003057 /* unsupported value, stop parsing and set the size to an error value */
3058 list_size = MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH + 1;
Johan Pascalb62bb512015-12-03 21:56:45 +01003059 }
3060 }
3061
Gilles Peskine449bd832023-01-11 14:50:10 +01003062 if (list_size > MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH) {
3063 conf->dtls_srtp_profile_list = NULL;
3064 conf->dtls_srtp_profile_list_len = 0;
3065 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Johan Pascal253d0262020-09-22 13:04:45 +02003066 }
3067
Johan Pascal9bc97ca2020-09-21 23:44:45 +02003068 conf->dtls_srtp_profile_list = profiles;
Johan Pascal253d0262020-09-22 13:04:45 +02003069 conf->dtls_srtp_profile_list_len = list_size;
Johan Pascalb62bb512015-12-03 21:56:45 +01003070
Gilles Peskine449bd832023-01-11 14:50:10 +01003071 return 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01003072}
3073
Gilles Peskine449bd832023-01-11 14:50:10 +01003074void mbedtls_ssl_get_dtls_srtp_negotiation_result(const mbedtls_ssl_context *ssl,
3075 mbedtls_dtls_srtp_info *dtls_srtp_info)
Johan Pascalb62bb512015-12-03 21:56:45 +01003076{
Johan Pascal2258a4f2020-10-28 13:53:09 +01003077 dtls_srtp_info->chosen_dtls_srtp_profile = ssl->dtls_srtp_info.chosen_dtls_srtp_profile;
3078 /* do not copy the mki value if there is no chosen profile */
Gilles Peskine449bd832023-01-11 14:50:10 +01003079 if (dtls_srtp_info->chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET) {
Johan Pascal2258a4f2020-10-28 13:53:09 +01003080 dtls_srtp_info->mki_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003081 } else {
Johan Pascal2258a4f2020-10-28 13:53:09 +01003082 dtls_srtp_info->mki_len = ssl->dtls_srtp_info.mki_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01003083 memcpy(dtls_srtp_info->mki_value, ssl->dtls_srtp_info.mki_value,
3084 ssl->dtls_srtp_info.mki_len);
Johan Pascal2258a4f2020-10-28 13:53:09 +01003085 }
Johan Pascalb62bb512015-12-03 21:56:45 +01003086}
Johan Pascalb62bb512015-12-03 21:56:45 +01003087#endif /* MBEDTLS_SSL_DTLS_SRTP */
3088
Aditya Patwardhan3096f332022-07-26 14:31:46 +05303089#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003090void mbedtls_ssl_conf_max_version(mbedtls_ssl_config *conf, int major, int minor)
Paul Bakker490ecc82011-10-06 13:04:09 +00003091{
Glenn Strauss2dfcea22022-03-14 17:26:42 -04003092 conf->max_tls_version = (major << 8) | minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00003093}
3094
Gilles Peskine449bd832023-01-11 14:50:10 +01003095void mbedtls_ssl_conf_min_version(mbedtls_ssl_config *conf, int major, int minor)
Paul Bakker1d29fb52012-09-28 13:28:45 +00003096{
Glenn Strauss2dfcea22022-03-14 17:26:42 -04003097 conf->min_tls_version = (major << 8) | minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00003098}
Aditya Patwardhan3096f332022-07-26 14:31:46 +05303099#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker1d29fb52012-09-28 13:28:45 +00003100
Janos Follath088ce432017-04-10 12:42:31 +01003101#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003102void mbedtls_ssl_conf_cert_req_ca_list(mbedtls_ssl_config *conf,
3103 char cert_req_ca_list)
Janos Follath088ce432017-04-10 12:42:31 +01003104{
3105 conf->cert_req_ca_list = cert_req_ca_list;
3106}
3107#endif
3108
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003109#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01003110void mbedtls_ssl_conf_encrypt_then_mac(mbedtls_ssl_config *conf, char etm)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01003111{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003112 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01003113}
3114#endif
3115
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003116#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Gilles Peskine449bd832023-01-11 14:50:10 +01003117void mbedtls_ssl_conf_extended_master_secret(mbedtls_ssl_config *conf, char ems)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02003118{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003119 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02003120}
3121#endif
3122
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003123#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01003124int mbedtls_ssl_conf_max_frag_len(mbedtls_ssl_config *conf, unsigned char mfl_code)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003125{
Gilles Peskine449bd832023-01-11 14:50:10 +01003126 if (mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
3127 ssl_mfl_code_to_length(mfl_code) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN) {
3128 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003129 }
3130
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01003131 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003132
Gilles Peskine449bd832023-01-11 14:50:10 +01003133 return 0;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003134}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003135#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003136
Gilles Peskine449bd832023-01-11 14:50:10 +01003137void mbedtls_ssl_conf_legacy_renegotiation(mbedtls_ssl_config *conf, int allow_legacy)
Paul Bakker48916f92012-09-16 19:57:18 +00003138{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003139 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00003140}
3141
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003142#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01003143void mbedtls_ssl_conf_renegotiation(mbedtls_ssl_config *conf, int renegotiation)
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003144{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003145 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003146}
3147
Gilles Peskine449bd832023-01-11 14:50:10 +01003148void mbedtls_ssl_conf_renegotiation_enforced(mbedtls_ssl_config *conf, int max_records)
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003149{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003150 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003151}
3152
Gilles Peskine449bd832023-01-11 14:50:10 +01003153void mbedtls_ssl_conf_renegotiation_period(mbedtls_ssl_config *conf,
3154 const unsigned char period[8])
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01003155{
Gilles Peskine449bd832023-01-11 14:50:10 +01003156 memcpy(conf->renego_period, period, 8);
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01003157}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003158#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00003159
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003160#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003161#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003162void mbedtls_ssl_conf_session_tickets(mbedtls_ssl_config *conf, int use_tickets)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003163{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01003164 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003165}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003166#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02003167
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003168#if defined(MBEDTLS_SSL_SRV_C)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08003169
Jerry Yud0766ec2022-09-22 10:46:57 +08003170#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003171void mbedtls_ssl_conf_new_session_tickets(mbedtls_ssl_config *conf,
3172 uint16_t num_tickets)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08003173{
Jerry Yud0766ec2022-09-22 10:46:57 +08003174 conf->new_session_tickets_count = num_tickets;
Jerry Yu1ad7ace2022-08-09 13:28:39 +08003175}
3176#endif
3177
Gilles Peskine449bd832023-01-11 14:50:10 +01003178void mbedtls_ssl_conf_session_tickets_cb(mbedtls_ssl_config *conf,
3179 mbedtls_ssl_ticket_write_t *f_ticket_write,
3180 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
3181 void *p_ticket)
Paul Bakker606b4ba2013-08-14 16:52:14 +02003182{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02003183 conf->f_ticket_write = f_ticket_write;
3184 conf->f_ticket_parse = f_ticket_parse;
3185 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02003186}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003187#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003188#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003189
Gilles Peskine449bd832023-01-11 14:50:10 +01003190void mbedtls_ssl_set_export_keys_cb(mbedtls_ssl_context *ssl,
3191 mbedtls_ssl_export_keys_t *f_export_keys,
3192 void *p_export_keys)
Robert Cragie4feb7ae2015-10-02 13:33:37 +01003193{
Hanno Becker7e6c1782021-06-08 09:24:55 +01003194 ssl->f_export_keys = f_export_keys;
3195 ssl->p_export_keys = p_export_keys;
Ron Eldorf5cc10d2019-05-07 18:33:40 +03003196}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01003197
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003198#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003199void mbedtls_ssl_conf_async_private_cb(
3200 mbedtls_ssl_config *conf,
3201 mbedtls_ssl_async_sign_t *f_async_sign,
3202 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
3203 mbedtls_ssl_async_resume_t *f_async_resume,
3204 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskine449bd832023-01-11 14:50:10 +01003205 void *async_config_data)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003206{
3207 conf->f_async_sign_start = f_async_sign;
3208 conf->f_async_decrypt_start = f_async_decrypt;
3209 conf->f_async_resume = f_async_resume;
3210 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003211 conf->p_async_config_data = async_config_data;
3212}
3213
Gilles Peskine449bd832023-01-11 14:50:10 +01003214void *mbedtls_ssl_conf_get_async_config_data(const mbedtls_ssl_config *conf)
Gilles Peskine8f97af72018-04-26 11:46:10 +02003215{
Gilles Peskine449bd832023-01-11 14:50:10 +01003216 return conf->p_async_config_data;
Gilles Peskine8f97af72018-04-26 11:46:10 +02003217}
3218
Gilles Peskine449bd832023-01-11 14:50:10 +01003219void *mbedtls_ssl_get_async_operation_data(const mbedtls_ssl_context *ssl)
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003220{
Gilles Peskine449bd832023-01-11 14:50:10 +01003221 if (ssl->handshake == NULL) {
3222 return NULL;
3223 } else {
3224 return ssl->handshake->user_async_ctx;
3225 }
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003226}
3227
Gilles Peskine449bd832023-01-11 14:50:10 +01003228void mbedtls_ssl_set_async_operation_data(mbedtls_ssl_context *ssl,
3229 void *ctx)
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003230{
Gilles Peskine449bd832023-01-11 14:50:10 +01003231 if (ssl->handshake != NULL) {
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003232 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +01003233 }
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003234}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003235#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003236
Paul Bakker5121ce52009-01-03 21:22:43 +00003237/*
3238 * SSL get accessors
3239 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003240uint32_t mbedtls_ssl_get_verify_result(const mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00003241{
Gilles Peskine449bd832023-01-11 14:50:10 +01003242 if (ssl->session != NULL) {
3243 return ssl->session->verify_result;
3244 }
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00003245
Gilles Peskine449bd832023-01-11 14:50:10 +01003246 if (ssl->session_negotiate != NULL) {
3247 return ssl->session_negotiate->verify_result;
3248 }
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00003249
Gilles Peskine449bd832023-01-11 14:50:10 +01003250 return 0xFFFFFFFF;
Paul Bakker5121ce52009-01-03 21:22:43 +00003251}
3252
Gilles Peskine449bd832023-01-11 14:50:10 +01003253int mbedtls_ssl_get_ciphersuite_id_from_ssl(const mbedtls_ssl_context *ssl)
Glenn Strauss8f526902022-01-13 00:04:49 -05003254{
Gilles Peskine449bd832023-01-11 14:50:10 +01003255 if (ssl == NULL || ssl->session == NULL) {
3256 return 0;
3257 }
Glenn Strauss8f526902022-01-13 00:04:49 -05003258
Gilles Peskine449bd832023-01-11 14:50:10 +01003259 return ssl->session->ciphersuite;
Glenn Strauss8f526902022-01-13 00:04:49 -05003260}
3261
Gilles Peskine449bd832023-01-11 14:50:10 +01003262const char *mbedtls_ssl_get_ciphersuite(const mbedtls_ssl_context *ssl)
Paul Bakker72f62662011-01-16 21:27:44 +00003263{
Gilles Peskine449bd832023-01-11 14:50:10 +01003264 if (ssl == NULL || ssl->session == NULL) {
3265 return NULL;
3266 }
Paul Bakker926c8e42013-03-06 10:23:34 +01003267
Gilles Peskine449bd832023-01-11 14:50:10 +01003268 return mbedtls_ssl_get_ciphersuite_name(ssl->session->ciphersuite);
Paul Bakker72f62662011-01-16 21:27:44 +00003269}
3270
Gilles Peskine449bd832023-01-11 14:50:10 +01003271const char *mbedtls_ssl_get_version(const mbedtls_ssl_context *ssl)
Paul Bakker43ca69c2011-01-15 17:35:19 +00003272{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003273#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003274 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
3275 switch (ssl->tls_version) {
Glenn Strauss60bfe602022-03-14 19:04:24 -04003276 case MBEDTLS_SSL_VERSION_TLS1_2:
Gilles Peskine449bd832023-01-11 14:50:10 +01003277 return "DTLSv1.2";
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003278 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01003279 return "unknown (DTLS)";
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003280 }
3281 }
3282#endif
3283
Gilles Peskine449bd832023-01-11 14:50:10 +01003284 switch (ssl->tls_version) {
Glenn Strauss60bfe602022-03-14 19:04:24 -04003285 case MBEDTLS_SSL_VERSION_TLS1_2:
Gilles Peskine449bd832023-01-11 14:50:10 +01003286 return "TLSv1.2";
Glenn Strauss60bfe602022-03-14 19:04:24 -04003287 case MBEDTLS_SSL_VERSION_TLS1_3:
Gilles Peskine449bd832023-01-11 14:50:10 +01003288 return "TLSv1.3";
Paul Bakker43ca69c2011-01-15 17:35:19 +00003289 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01003290 return "unknown";
Paul Bakker43ca69c2011-01-15 17:35:19 +00003291 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00003292}
3293
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003294#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01003295size_t mbedtls_ssl_get_input_max_frag_len(const mbedtls_ssl_context *ssl)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003296{
David Horstmann95d516f2021-05-04 18:36:56 +01003297 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003298 size_t read_mfl;
3299
Jerry Yuddda0502022-12-01 19:43:12 +08003300#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003301 /* Use the configured MFL for the client if we're past SERVER_HELLO_DONE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003302 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
3303 ssl->state >= MBEDTLS_SSL_SERVER_HELLO_DONE) {
3304 return ssl_mfl_code_to_length(ssl->conf->mfl_code);
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003305 }
Jerry Yuddda0502022-12-01 19:43:12 +08003306#endif
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003307
3308 /* Check if a smaller max length was negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01003309 if (ssl->session_out != NULL) {
3310 read_mfl = ssl_mfl_code_to_length(ssl->session_out->mfl_code);
3311 if (read_mfl < max_len) {
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003312 max_len = read_mfl;
3313 }
3314 }
3315
Jerry Yuddda0502022-12-01 19:43:12 +08003316 /* During a handshake, use the value being negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01003317 if (ssl->session_negotiate != NULL) {
3318 read_mfl = ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code);
3319 if (read_mfl < max_len) {
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003320 max_len = read_mfl;
3321 }
3322 }
3323
Gilles Peskine449bd832023-01-11 14:50:10 +01003324 return max_len;
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003325}
3326
Gilles Peskine449bd832023-01-11 14:50:10 +01003327size_t mbedtls_ssl_get_output_max_frag_len(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003328{
3329 size_t max_len;
3330
3331 /*
3332 * Assume mfl_code is correct since it was checked when set
3333 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003334 max_len = ssl_mfl_code_to_length(ssl->conf->mfl_code);
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003335
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02003336 /* Check if a smaller max length was negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01003337 if (ssl->session_out != NULL &&
3338 ssl_mfl_code_to_length(ssl->session_out->mfl_code) < max_len) {
3339 max_len = ssl_mfl_code_to_length(ssl->session_out->mfl_code);
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003340 }
3341
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02003342 /* During a handshake, use the value being negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01003343 if (ssl->session_negotiate != NULL &&
3344 ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code) < max_len) {
3345 max_len = ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code);
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02003346 }
3347
Gilles Peskine449bd832023-01-11 14:50:10 +01003348 return max_len;
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003349}
3350#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
3351
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003352#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003353size_t mbedtls_ssl_get_current_mtu(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003354{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04003355 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003356 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
3357 (ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
3358 ssl->state == MBEDTLS_SSL_SERVER_HELLO)) {
3359 return 0;
3360 }
Andrzej Kurekef43ce62018-10-09 08:24:12 -04003361
Gilles Peskine449bd832023-01-11 14:50:10 +01003362 if (ssl->handshake == NULL || ssl->handshake->mtu == 0) {
3363 return ssl->mtu;
3364 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003365
Gilles Peskine449bd832023-01-11 14:50:10 +01003366 if (ssl->mtu == 0) {
3367 return ssl->handshake->mtu;
3368 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003369
Gilles Peskine449bd832023-01-11 14:50:10 +01003370 return ssl->mtu < ssl->handshake->mtu ?
3371 ssl->mtu : ssl->handshake->mtu;
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003372}
3373#endif /* MBEDTLS_SSL_PROTO_DTLS */
3374
Gilles Peskine449bd832023-01-11 14:50:10 +01003375int mbedtls_ssl_get_max_out_record_payload(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003376{
3377 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
3378
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02003379#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
3380 !defined(MBEDTLS_SSL_PROTO_DTLS)
3381 (void) ssl;
3382#endif
3383
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003384#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01003385 const size_t mfl = mbedtls_ssl_get_output_max_frag_len(ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003386
Gilles Peskine449bd832023-01-11 14:50:10 +01003387 if (max_len > mfl) {
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003388 max_len = mfl;
Gilles Peskine449bd832023-01-11 14:50:10 +01003389 }
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003390#endif
3391
3392#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003393 if (mbedtls_ssl_get_current_mtu(ssl) != 0) {
3394 const size_t mtu = mbedtls_ssl_get_current_mtu(ssl);
3395 const int ret = mbedtls_ssl_get_record_expansion(ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003396 const size_t overhead = (size_t) ret;
3397
Gilles Peskine449bd832023-01-11 14:50:10 +01003398 if (ret < 0) {
3399 return ret;
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003400 }
3401
Gilles Peskine449bd832023-01-11 14:50:10 +01003402 if (mtu <= overhead) {
3403 MBEDTLS_SSL_DEBUG_MSG(1, ("MTU too low for record expansion"));
3404 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
3405 }
3406
3407 if (max_len > mtu - overhead) {
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003408 max_len = mtu - overhead;
Gilles Peskine449bd832023-01-11 14:50:10 +01003409 }
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003410 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003411#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003412
Hanno Becker0defedb2018-08-10 12:35:02 +01003413#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
3414 !defined(MBEDTLS_SSL_PROTO_DTLS)
3415 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003416#endif
3417
Gilles Peskine449bd832023-01-11 14:50:10 +01003418 return (int) max_len;
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003419}
3420
Gilles Peskine449bd832023-01-11 14:50:10 +01003421int mbedtls_ssl_get_max_in_record_payload(const mbedtls_ssl_context *ssl)
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003422{
3423 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
3424
3425#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
3426 (void) ssl;
3427#endif
3428
3429#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01003430 const size_t mfl = mbedtls_ssl_get_input_max_frag_len(ssl);
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003431
Gilles Peskine449bd832023-01-11 14:50:10 +01003432 if (max_len > mfl) {
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003433 max_len = mfl;
Gilles Peskine449bd832023-01-11 14:50:10 +01003434 }
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003435#endif
3436
Gilles Peskine449bd832023-01-11 14:50:10 +01003437 return (int) max_len;
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003438}
3439
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003440#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003441const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert(const mbedtls_ssl_context *ssl)
Paul Bakkerb0550d92012-10-30 07:51:03 +00003442{
Gilles Peskine449bd832023-01-11 14:50:10 +01003443 if (ssl == NULL || ssl->session == NULL) {
3444 return NULL;
3445 }
Paul Bakkerb0550d92012-10-30 07:51:03 +00003446
Hanno Beckere6824572019-02-07 13:18:46 +00003447#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01003448 return ssl->session->peer_cert;
Hanno Beckere6824572019-02-07 13:18:46 +00003449#else
Gilles Peskine449bd832023-01-11 14:50:10 +01003450 return NULL;
Hanno Beckere6824572019-02-07 13:18:46 +00003451#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00003452}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003453#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00003454
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003455#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003456int mbedtls_ssl_get_session(const mbedtls_ssl_context *ssl,
3457 mbedtls_ssl_session *dst)
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003458{
Hanno Beckere810bbc2021-05-14 16:01:05 +01003459 int ret;
3460
Gilles Peskine449bd832023-01-11 14:50:10 +01003461 if (ssl == NULL ||
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003462 dst == NULL ||
3463 ssl->session == NULL ||
Gilles Peskine449bd832023-01-11 14:50:10 +01003464 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT) {
3465 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003466 }
3467
Hanno Beckere810bbc2021-05-14 16:01:05 +01003468 /* Since Mbed TLS 3.0, mbedtls_ssl_get_session() is no longer
3469 * idempotent: Each session can only be exported once.
3470 *
3471 * (This is in preparation for TLS 1.3 support where we will
3472 * need the ability to export multiple sessions (aka tickets),
3473 * which will be achieved by calling mbedtls_ssl_get_session()
3474 * multiple times until it fails.)
3475 *
3476 * Check whether we have already exported the current session,
3477 * and fail if so.
3478 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003479 if (ssl->session->exported == 1) {
3480 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
3481 }
Hanno Beckere810bbc2021-05-14 16:01:05 +01003482
Gilles Peskine449bd832023-01-11 14:50:10 +01003483 ret = mbedtls_ssl_session_copy(dst, ssl->session);
3484 if (ret != 0) {
3485 return ret;
3486 }
Hanno Beckere810bbc2021-05-14 16:01:05 +01003487
3488 /* Remember that we've exported the session. */
3489 ssl->session->exported = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003490 return 0;
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003491}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003492#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003493
Paul Bakker5121ce52009-01-03 21:22:43 +00003494/*
Hanno Beckera835da52019-05-16 12:39:07 +01003495 * Define ticket header determining Mbed TLS version
3496 * and structure of the ticket.
3497 */
3498
Hanno Becker94ef3b32019-05-16 12:50:45 +01003499/*
Hanno Becker50b59662019-05-28 14:30:45 +01003500 * Define bitflag determining compile-time settings influencing
3501 * structure of serialized SSL sessions.
Hanno Becker94ef3b32019-05-16 12:50:45 +01003502 */
3503
Hanno Becker50b59662019-05-28 14:30:45 +01003504#if defined(MBEDTLS_HAVE_TIME)
Hanno Becker3e088662019-05-29 11:10:18 +01003505#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker50b59662019-05-28 14:30:45 +01003506#else
Hanno Becker3e088662019-05-29 11:10:18 +01003507#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003508#endif /* MBEDTLS_HAVE_TIME */
3509
3510#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker3e088662019-05-29 11:10:18 +01003511#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003512#else
Hanno Becker3e088662019-05-29 11:10:18 +01003513#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003514#endif /* MBEDTLS_X509_CRT_PARSE_C */
3515
3516#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Becker3e088662019-05-29 11:10:18 +01003517#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003518#else
Hanno Becker3e088662019-05-29 11:10:18 +01003519#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003520#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
3521
3522#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Becker3e088662019-05-29 11:10:18 +01003523#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003524#else
Hanno Becker3e088662019-05-29 11:10:18 +01003525#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003526#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
3527
Hanno Becker94ef3b32019-05-16 12:50:45 +01003528#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3e088662019-05-29 11:10:18 +01003529#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003530#else
Hanno Becker3e088662019-05-29 11:10:18 +01003531#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003532#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
3533
Hanno Becker94ef3b32019-05-16 12:50:45 +01003534#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3535#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
3536#else
3537#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
3538#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3539
Hanno Becker3e088662019-05-29 11:10:18 +01003540#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
3541#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
3542#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
3543#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
Hanno Becker37bdbe62021-08-01 05:38:58 +01003544#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 4
3545#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 5
Hanno Becker3e088662019-05-29 11:10:18 +01003546
Hanno Becker50b59662019-05-28 14:30:45 +01003547#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Gilles Peskine449bd832023-01-11 14:50:10 +01003548 ((uint16_t) ( \
3549 (SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT) | \
3550 (SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT) | \
3551 (SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << \
3552 SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT) | \
3553 (SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT) | \
3554 (SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT) | \
3555 (SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT)))
Hanno Becker94ef3b32019-05-16 12:50:45 +01003556
Hanno Beckerf8787072019-05-16 12:41:07 +01003557static unsigned char ssl_serialized_session_header[] = {
Hanno Becker94ef3b32019-05-16 12:50:45 +01003558 MBEDTLS_VERSION_MAJOR,
3559 MBEDTLS_VERSION_MINOR,
3560 MBEDTLS_VERSION_PATCH,
Gilles Peskine449bd832023-01-11 14:50:10 +01003561 MBEDTLS_BYTE_1(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
3562 MBEDTLS_BYTE_0(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
Hanno Beckerf8787072019-05-16 12:41:07 +01003563};
Hanno Beckera835da52019-05-16 12:39:07 +01003564
3565/*
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003566 * Serialize a session in the following format:
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02003567 * (in the presentation language of TLS, RFC 8446 section 3)
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003568 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003569 * struct {
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01003570 *
Hanno Beckerdce50972021-08-01 05:39:23 +01003571 * opaque mbedtls_version[3]; // library version: major, minor, patch
3572 * opaque session_format[2]; // library-version specific 16-bit field
3573 * // determining the format of the remaining
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003574 * // serialized data.
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01003575 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003576 * Note: When updating the format, remember to keep
3577 * these version+format bytes.
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003578 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003579 * // In this version, `session_format` determines
3580 * // the setting of those compile-time
3581 * // configuration options which influence
3582 * // the structure of mbedtls_ssl_session.
3583 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003584 * uint8_t minor_ver; // Protocol minor version. Possible values:
Jerry Yu0c2a7382022-12-06 13:27:25 +08003585 * // - TLS 1.2 (0x0303)
3586 * // - TLS 1.3 (0x0304)
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003587 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003588 * select (serialized_session.tls_version) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003589 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003590 * case MBEDTLS_SSL_VERSION_TLS1_2:
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003591 * serialized_session_tls12 data;
Jerry Yu0c2a7382022-12-06 13:27:25 +08003592 * case MBEDTLS_SSL_VERSION_TLS1_3:
Jerry Yuddda0502022-12-01 19:43:12 +08003593 * serialized_session_tls13 data;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003594 *
3595 * };
3596 *
3597 * } serialized_session;
3598 *
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003599 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003600
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003601MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003602static int ssl_session_save(const mbedtls_ssl_session *session,
3603 unsigned char omit_header,
3604 unsigned char *buf,
3605 size_t buf_len,
3606 size_t *olen)
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003607{
3608 unsigned char *p = buf;
3609 size_t used = 0;
Jerry Yu251a12e2022-07-13 15:15:48 +08003610 size_t remaining_len;
Jerry Yue36fdd62022-08-17 21:31:36 +08003611#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3612 size_t out_len;
3613 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3614#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01003615 if (session == NULL) {
3616 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
3617 }
Jerry Yu438ddd82022-07-07 06:55:50 +00003618
Gilles Peskine449bd832023-01-11 14:50:10 +01003619 if (!omit_header) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003620 /*
3621 * Add Mbed TLS version identifier
3622 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003623 used += sizeof(ssl_serialized_session_header);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003624
Gilles Peskine449bd832023-01-11 14:50:10 +01003625 if (used <= buf_len) {
3626 memcpy(p, ssl_serialized_session_header,
3627 sizeof(ssl_serialized_session_header));
3628 p += sizeof(ssl_serialized_session_header);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003629 }
3630 }
3631
3632 /*
3633 * TLS version identifier
3634 */
3635 used += 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003636 if (used <= buf_len) {
3637 *p++ = MBEDTLS_BYTE_0(session->tls_version);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003638 }
3639
3640 /* Forward to version-specific serialization routine. */
Jerry Yufca4d572022-07-21 10:37:48 +08003641 remaining_len = (buf_len >= used) ? buf_len - used : 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003642 switch (session->tls_version) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003643#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01003644 case MBEDTLS_SSL_VERSION_TLS1_2:
3645 used += ssl_tls12_session_save(session, p, remaining_len);
3646 break;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003647#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3648
Jerry Yu251a12e2022-07-13 15:15:48 +08003649#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01003650 case MBEDTLS_SSL_VERSION_TLS1_3:
3651 ret = ssl_tls13_session_save(session, p, remaining_len, &out_len);
3652 if (ret != 0 && ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL) {
3653 return ret;
3654 }
3655 used += out_len;
3656 break;
Jerry Yu251a12e2022-07-13 15:15:48 +08003657#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3658
Gilles Peskine449bd832023-01-11 14:50:10 +01003659 default:
3660 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003661 }
3662
3663 *olen = used;
Gilles Peskine449bd832023-01-11 14:50:10 +01003664 if (used > buf_len) {
3665 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
3666 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003667
Gilles Peskine449bd832023-01-11 14:50:10 +01003668 return 0;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003669}
3670
3671/*
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003672 * Public wrapper for ssl_session_save()
3673 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003674int mbedtls_ssl_session_save(const mbedtls_ssl_session *session,
3675 unsigned char *buf,
3676 size_t buf_len,
3677 size_t *olen)
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003678{
Gilles Peskine449bd832023-01-11 14:50:10 +01003679 return ssl_session_save(session, 0, buf, buf_len, olen);
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003680}
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003681
Jerry Yuf1b23ca2022-02-18 11:48:47 +08003682/*
3683 * Deserialize session, see mbedtls_ssl_session_save() for format.
3684 *
3685 * This internal version is wrapped by a public function that cleans up in
3686 * case of error, and has an extra option omit_header.
3687 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003688MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003689static int ssl_session_load(mbedtls_ssl_session *session,
3690 unsigned char omit_header,
3691 const unsigned char *buf,
3692 size_t len)
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003693{
3694 const unsigned char *p = buf;
3695 const unsigned char * const end = buf + len;
Jerry Yu438ddd82022-07-07 06:55:50 +00003696 size_t remaining_len;
3697
3698
Gilles Peskine449bd832023-01-11 14:50:10 +01003699 if (session == NULL) {
3700 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
3701 }
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003702
Gilles Peskine449bd832023-01-11 14:50:10 +01003703 if (!omit_header) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003704 /*
3705 * Check Mbed TLS version identifier
3706 */
3707
Gilles Peskine449bd832023-01-11 14:50:10 +01003708 if ((size_t) (end - p) < sizeof(ssl_serialized_session_header)) {
3709 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003710 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003711
3712 if (memcmp(p, ssl_serialized_session_header,
3713 sizeof(ssl_serialized_session_header)) != 0) {
3714 return MBEDTLS_ERR_SSL_VERSION_MISMATCH;
3715 }
3716 p += sizeof(ssl_serialized_session_header);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003717 }
3718
3719 /*
3720 * TLS version identifier
3721 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003722 if (1 > (size_t) (end - p)) {
3723 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3724 }
Glenn Straussda7851c2022-03-14 13:29:48 -04003725 session->tls_version = 0x0300 | *p++;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003726
3727 /* Dispatch according to TLS version. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003728 remaining_len = (end - p);
3729 switch (session->tls_version) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003730#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01003731 case MBEDTLS_SSL_VERSION_TLS1_2:
3732 return ssl_tls12_session_load(session, p, remaining_len);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003733#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3734
Jerry Yu438ddd82022-07-07 06:55:50 +00003735#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01003736 case MBEDTLS_SSL_VERSION_TLS1_3:
3737 return ssl_tls13_session_load(session, p, remaining_len);
Jerry Yu438ddd82022-07-07 06:55:50 +00003738#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3739
Gilles Peskine449bd832023-01-11 14:50:10 +01003740 default:
3741 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003742 }
3743}
3744
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003745/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02003746 * Deserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003747 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003748int mbedtls_ssl_session_load(mbedtls_ssl_session *session,
3749 const unsigned char *buf,
3750 size_t len)
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003751{
Gilles Peskine449bd832023-01-11 14:50:10 +01003752 int ret = ssl_session_load(session, 0, buf, len);
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003753
Gilles Peskine449bd832023-01-11 14:50:10 +01003754 if (ret != 0) {
3755 mbedtls_ssl_session_free(session);
3756 }
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003757
Gilles Peskine449bd832023-01-11 14:50:10 +01003758 return ret;
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003759}
3760
3761/*
Paul Bakker1961b702013-01-25 14:49:24 +01003762 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00003763 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003764MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003765static int ssl_prepare_handshake_step(mbedtls_ssl_context *ssl)
Hanno Becker41934dd2021-08-07 19:13:43 +01003766{
3767 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3768
Ronald Cron66dbf912022-02-02 15:33:46 +01003769 /*
3770 * We may have not been able to send to the peer all the handshake data
Ronald Cron3f20b772022-03-08 16:00:02 +01003771 * that were written into the output buffer by the previous handshake step,
3772 * if the write to the network callback returned with the
Ronald Cron66dbf912022-02-02 15:33:46 +01003773 * #MBEDTLS_ERR_SSL_WANT_WRITE error code.
3774 * We proceed to the next handshake step only when all data from the
3775 * previous one have been sent to the peer, thus we make sure that this is
3776 * the case here by calling `mbedtls_ssl_flush_output()`. The function may
3777 * return with the #MBEDTLS_ERR_SSL_WANT_WRITE error code in which case
3778 * we have to wait before to go ahead.
3779 * In the case of TLS 1.3, handshake step handlers do not send data to the
3780 * peer. Data are only sent here and through
3781 * `mbedtls_ssl_handle_pending_alert` in case an error that triggered an
Andrzej Kurek5c65c572022-04-13 14:28:52 -04003782 * alert occurred.
Ronald Cron66dbf912022-02-02 15:33:46 +01003783 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003784 if ((ret = mbedtls_ssl_flush_output(ssl)) != 0) {
3785 return ret;
3786 }
Hanno Becker41934dd2021-08-07 19:13:43 +01003787
3788#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003789 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3790 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING) {
3791 if ((ret = mbedtls_ssl_flight_transmit(ssl)) != 0) {
3792 return ret;
3793 }
Hanno Becker41934dd2021-08-07 19:13:43 +01003794 }
3795#endif /* MBEDTLS_SSL_PROTO_DTLS */
3796
Gilles Peskine449bd832023-01-11 14:50:10 +01003797 return ret;
Hanno Becker41934dd2021-08-07 19:13:43 +01003798}
3799
Gilles Peskine449bd832023-01-11 14:50:10 +01003800int mbedtls_ssl_handshake_step(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00003801{
Hanno Becker41934dd2021-08-07 19:13:43 +01003802 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003803
Gilles Peskine449bd832023-01-11 14:50:10 +01003804 if (ssl == NULL ||
Hanno Becker41934dd2021-08-07 19:13:43 +01003805 ssl->conf == NULL ||
3806 ssl->handshake == NULL ||
Gilles Peskine449bd832023-01-11 14:50:10 +01003807 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER) {
3808 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Becker41934dd2021-08-07 19:13:43 +01003809 }
3810
Gilles Peskine449bd832023-01-11 14:50:10 +01003811 ret = ssl_prepare_handshake_step(ssl);
3812 if (ret != 0) {
3813 return ret;
3814 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003815
Gilles Peskine449bd832023-01-11 14:50:10 +01003816 ret = mbedtls_ssl_handle_pending_alert(ssl);
3817 if (ret != 0) {
Jerry Yue7047812021-09-13 19:26:39 +08003818 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01003819 }
Jerry Yue7047812021-09-13 19:26:39 +08003820
Tom Cosgrove2fdc7b32022-09-21 12:33:17 +01003821 /* If ssl->conf->endpoint is not one of MBEDTLS_SSL_IS_CLIENT or
3822 * MBEDTLS_SSL_IS_SERVER, this is the return code we give */
3823 ret = MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3824
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003825#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003826 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
3827 MBEDTLS_SSL_DEBUG_MSG(2, ("client state: %s",
3828 mbedtls_ssl_states_str(ssl->state)));
Jerry Yub9930e72021-08-06 17:11:51 +08003829
Gilles Peskine449bd832023-01-11 14:50:10 +01003830 switch (ssl->state) {
Ronald Cron9f0fba32022-02-10 16:45:15 +01003831 case MBEDTLS_SSL_HELLO_REQUEST:
3832 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
Tom Cosgrove87d9c6c2022-09-22 09:27:56 +01003833 ret = 0;
Ronald Cron9f0fba32022-02-10 16:45:15 +01003834 break;
Jerry Yub9930e72021-08-06 17:11:51 +08003835
Ronald Cron9f0fba32022-02-10 16:45:15 +01003836 case MBEDTLS_SSL_CLIENT_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01003837 ret = mbedtls_ssl_write_client_hello(ssl);
Ronald Cron9f0fba32022-02-10 16:45:15 +01003838 break;
3839
3840 default:
3841#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01003842 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
3843 ret = mbedtls_ssl_tls13_handshake_client_step(ssl);
3844 } else {
3845 ret = mbedtls_ssl_handshake_client_step(ssl);
3846 }
Ronald Cron9f0fba32022-02-10 16:45:15 +01003847#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01003848 ret = mbedtls_ssl_handshake_client_step(ssl);
Ronald Cron9f0fba32022-02-10 16:45:15 +01003849#else
Gilles Peskine449bd832023-01-11 14:50:10 +01003850 ret = mbedtls_ssl_tls13_handshake_client_step(ssl);
Ronald Cron9f0fba32022-02-10 16:45:15 +01003851#endif
3852 }
Jerry Yub9930e72021-08-06 17:11:51 +08003853 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003854#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003855#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003856 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Ronald Cron6f135e12021-12-08 16:57:54 +01003857#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01003858 if (mbedtls_ssl_conf_is_tls13_only(ssl->conf)) {
3859 ret = mbedtls_ssl_tls13_handshake_server_step(ssl);
3860 }
Ronald Cron6f135e12021-12-08 16:57:54 +01003861#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yub9930e72021-08-06 17:11:51 +08003862
3863#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01003864 if (mbedtls_ssl_conf_is_tls12_only(ssl->conf)) {
3865 ret = mbedtls_ssl_handshake_server_step(ssl);
3866 }
Jerry Yub9930e72021-08-06 17:11:51 +08003867#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3868 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003869#endif
3870
Gilles Peskine449bd832023-01-11 14:50:10 +01003871 if (ret != 0) {
Jerry Yubbd5a3f2021-09-18 20:50:22 +08003872 /* handshake_step return error. And it is same
3873 * with alert_reason.
3874 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003875 if (ssl->send_alert) {
3876 ret = mbedtls_ssl_handle_pending_alert(ssl);
Jerry Yue7047812021-09-13 19:26:39 +08003877 goto cleanup;
3878 }
3879 }
3880
3881cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01003882 return ret;
Paul Bakker1961b702013-01-25 14:49:24 +01003883}
3884
3885/*
3886 * Perform the SSL handshake
3887 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003888int mbedtls_ssl_handshake(mbedtls_ssl_context *ssl)
Paul Bakker1961b702013-01-25 14:49:24 +01003889{
3890 int ret = 0;
3891
Hanno Beckera817ea42020-10-20 15:20:23 +01003892 /* Sanity checks */
3893
Gilles Peskine449bd832023-01-11 14:50:10 +01003894 if (ssl == NULL || ssl->conf == NULL) {
3895 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3896 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003897
Hanno Beckera817ea42020-10-20 15:20:23 +01003898#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003899 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3900 (ssl->f_set_timer == NULL || ssl->f_get_timer == NULL)) {
3901 MBEDTLS_SSL_DEBUG_MSG(1, ("You must use "
3902 "mbedtls_ssl_set_timer_cb() for DTLS"));
3903 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Beckera817ea42020-10-20 15:20:23 +01003904 }
3905#endif /* MBEDTLS_SSL_PROTO_DTLS */
3906
Gilles Peskine449bd832023-01-11 14:50:10 +01003907 MBEDTLS_SSL_DEBUG_MSG(2, ("=> handshake"));
Paul Bakker1961b702013-01-25 14:49:24 +01003908
Hanno Beckera817ea42020-10-20 15:20:23 +01003909 /* Main handshake loop */
Gilles Peskine449bd832023-01-11 14:50:10 +01003910 while (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER) {
3911 ret = mbedtls_ssl_handshake_step(ssl);
Paul Bakker1961b702013-01-25 14:49:24 +01003912
Gilles Peskine449bd832023-01-11 14:50:10 +01003913 if (ret != 0) {
Paul Bakker1961b702013-01-25 14:49:24 +01003914 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01003915 }
Paul Bakker1961b702013-01-25 14:49:24 +01003916 }
3917
Gilles Peskine449bd832023-01-11 14:50:10 +01003918 MBEDTLS_SSL_DEBUG_MSG(2, ("<= handshake"));
Paul Bakker5121ce52009-01-03 21:22:43 +00003919
Gilles Peskine449bd832023-01-11 14:50:10 +01003920 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003921}
3922
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003923#if defined(MBEDTLS_SSL_RENEGOTIATION)
3924#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003925/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003926 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00003927 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003928MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003929static int ssl_write_hello_request(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003930{
Janos Follath865b3eb2019-12-16 11:46:15 +00003931 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003932
Gilles Peskine449bd832023-01-11 14:50:10 +01003933 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write hello request"));
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003934
3935 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003936 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3937 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003938
Gilles Peskine449bd832023-01-11 14:50:10 +01003939 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
3940 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
3941 return ret;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003942 }
3943
Gilles Peskine449bd832023-01-11 14:50:10 +01003944 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write hello request"));
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003945
Gilles Peskine449bd832023-01-11 14:50:10 +01003946 return 0;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003947}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003948#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003949
3950/*
3951 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003952 * - any side: calling mbedtls_ssl_renegotiate(),
3953 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
3954 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02003955 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003956 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003957 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003958 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003959int mbedtls_ssl_start_renegotiation(mbedtls_ssl_context *ssl)
Paul Bakker48916f92012-09-16 19:57:18 +00003960{
Janos Follath865b3eb2019-12-16 11:46:15 +00003961 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker48916f92012-09-16 19:57:18 +00003962
Gilles Peskine449bd832023-01-11 14:50:10 +01003963 MBEDTLS_SSL_DEBUG_MSG(2, ("=> renegotiate"));
Paul Bakker48916f92012-09-16 19:57:18 +00003964
Gilles Peskine449bd832023-01-11 14:50:10 +01003965 if ((ret = ssl_handshake_init(ssl)) != 0) {
3966 return ret;
3967 }
Paul Bakker48916f92012-09-16 19:57:18 +00003968
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003969 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
3970 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003971#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003972 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3973 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING) {
3974 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003975 ssl->handshake->out_msg_seq = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003976 } else {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003977 ssl->handshake->in_msg_seq = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003978 }
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003979 }
3980#endif
3981
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003982 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
3983 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00003984
Gilles Peskine449bd832023-01-11 14:50:10 +01003985 if ((ret = mbedtls_ssl_handshake(ssl)) != 0) {
3986 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_handshake", ret);
3987 return ret;
Paul Bakker48916f92012-09-16 19:57:18 +00003988 }
3989
Gilles Peskine449bd832023-01-11 14:50:10 +01003990 MBEDTLS_SSL_DEBUG_MSG(2, ("<= renegotiate"));
Paul Bakker48916f92012-09-16 19:57:18 +00003991
Gilles Peskine449bd832023-01-11 14:50:10 +01003992 return 0;
Paul Bakker48916f92012-09-16 19:57:18 +00003993}
3994
3995/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003996 * Renegotiate current connection on client,
3997 * or request renegotiation on server
3998 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003999int mbedtls_ssl_renegotiate(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004000{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004001 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004002
Gilles Peskine449bd832023-01-11 14:50:10 +01004003 if (ssl == NULL || ssl->conf == NULL) {
4004 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4005 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02004006
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004007#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004008 /* On server, just send the request */
Gilles Peskine449bd832023-01-11 14:50:10 +01004009 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
4010 if (mbedtls_ssl_is_handshake_over(ssl) == 0) {
4011 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4012 }
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004013
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004014 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02004015
4016 /* Did we already try/start sending HelloRequest? */
Gilles Peskine449bd832023-01-11 14:50:10 +01004017 if (ssl->out_left != 0) {
4018 return mbedtls_ssl_flush_output(ssl);
4019 }
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02004020
Gilles Peskine449bd832023-01-11 14:50:10 +01004021 return ssl_write_hello_request(ssl);
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004022 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004023#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004024
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004025#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004026 /*
4027 * On client, either start the renegotiation process or,
4028 * if already in progress, continue the handshake
4029 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004030 if (ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
4031 if (mbedtls_ssl_is_handshake_over(ssl) == 0) {
4032 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004033 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004034
4035 if ((ret = mbedtls_ssl_start_renegotiation(ssl)) != 0) {
4036 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_start_renegotiation", ret);
4037 return ret;
4038 }
4039 } else {
4040 if ((ret = mbedtls_ssl_handshake(ssl)) != 0) {
4041 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_handshake", ret);
4042 return ret;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004043 }
4044 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004045#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004046
Gilles Peskine449bd832023-01-11 14:50:10 +01004047 return ret;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004048}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004049#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004050
Gilles Peskine449bd832023-01-11 14:50:10 +01004051void mbedtls_ssl_handshake_free(mbedtls_ssl_context *ssl)
Paul Bakker48916f92012-09-16 19:57:18 +00004052{
Gilles Peskine9b562d52018-04-25 20:32:43 +02004053 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
4054
Gilles Peskine449bd832023-01-11 14:50:10 +01004055 if (handshake == NULL) {
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004056 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01004057 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004058
Brett Warrene0edc842021-08-17 09:53:13 +01004059#if defined(MBEDTLS_ECP_C)
4060#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01004061 if (ssl->handshake->group_list_heap_allocated) {
4062 mbedtls_free((void *) handshake->group_list);
4063 }
Brett Warrene0edc842021-08-17 09:53:13 +01004064 handshake->group_list = NULL;
4065#endif /* MBEDTLS_DEPRECATED_REMOVED */
4066#endif /* MBEDTLS_ECP_C */
4067
Ronald Crone68ab4f2022-10-05 12:46:29 +02004068#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +08004069#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01004070 if (ssl->handshake->sig_algs_heap_allocated) {
4071 mbedtls_free((void *) handshake->sig_algs);
4072 }
Jerry Yuf017ee42022-01-12 15:49:48 +08004073 handshake->sig_algs = NULL;
4074#endif /* MBEDTLS_DEPRECATED_REMOVED */
Xiaofei Baic234ecf2022-02-08 09:59:23 +00004075#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01004076 if (ssl->handshake->certificate_request_context) {
4077 mbedtls_free((void *) handshake->certificate_request_context);
Xiaofei Baic234ecf2022-02-08 09:59:23 +00004078 }
4079#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Ronald Crone68ab4f2022-10-05 12:46:29 +02004080#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yuf017ee42022-01-12 15:49:48 +08004081
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02004082#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01004083 if (ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0) {
4084 ssl->conf->f_async_cancel(ssl);
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02004085 handshake->async_in_progress = 0;
4086 }
4087#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
4088
Andrzej Kurek25f27152022-08-17 16:09:31 -04004089#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -05004090#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01004091 psa_hash_abort(&handshake->fin_sha256_psa);
Andrzej Kurekeb342242019-01-29 09:14:33 -05004092#else
Gilles Peskine449bd832023-01-11 14:50:10 +01004093 mbedtls_sha256_free(&handshake->fin_sha256);
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02004094#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05004095#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004096#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -05004097#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01004098 psa_hash_abort(&handshake->fin_sha384_psa);
Andrzej Kurekeb342242019-01-29 09:14:33 -05004099#else
Gilles Peskine449bd832023-01-11 14:50:10 +01004100 mbedtls_sha512_free(&handshake->fin_sha384);
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02004101#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05004102#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02004103
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004104#if defined(MBEDTLS_DHM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004105 mbedtls_dhm_free(&handshake->dhm_ctx);
Paul Bakker48916f92012-09-16 19:57:18 +00004106#endif
Neil Armstrongf3f46412022-04-12 14:43:39 +02004107#if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004108 mbedtls_ecdh_free(&handshake->ecdh_ctx);
Paul Bakker61d113b2013-07-04 11:51:43 +02004109#endif
Valerio Setti02c25b52022-11-15 14:08:42 +01004110
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02004111#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Neil Armstrongca7d5062022-05-31 14:43:23 +02004112#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01004113 psa_pake_abort(&handshake->psa_pake_ctx);
Valerio Settieb3f7882022-12-08 18:42:58 +01004114 /*
4115 * Opaque keys are not stored in the handshake's data and it's the user
4116 * responsibility to destroy them. Clear ones, instead, are created by
4117 * the TLS library and should be destroyed at the same level
4118 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004119 if (!mbedtls_svc_key_id_is_null(handshake->psa_pake_password)) {
4120 psa_destroy_key(handshake->psa_pake_password);
Valerio Settieb3f7882022-12-08 18:42:58 +01004121 }
Neil Armstrongca7d5062022-05-31 14:43:23 +02004122 handshake->psa_pake_password = MBEDTLS_SVC_KEY_ID_INIT;
4123#else
Gilles Peskine449bd832023-01-11 14:50:10 +01004124 mbedtls_ecjpake_free(&handshake->ecjpake_ctx);
Neil Armstrongca7d5062022-05-31 14:43:23 +02004125#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02004126#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004127 mbedtls_free(handshake->ecjpake_cache);
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02004128 handshake->ecjpake_cache = NULL;
4129 handshake->ecjpake_cache_len = 0;
4130#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02004131#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02004132
Janos Follath4ae5c292016-02-10 11:27:43 +00004133#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
4134 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02004135 /* explicit void pointer cast for buggy MS compiler */
Gilles Peskine449bd832023-01-11 14:50:10 +01004136 mbedtls_free((void *) handshake->curves_tls_id);
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004137#endif
4138
Ronald Cron73fe8df2022-10-05 14:31:43 +02004139#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02004140#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01004141 if (!mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque)) {
Neil Armstrong501c9322022-05-03 09:35:09 +02004142 /* The maintenance of the external PSK key slot is the
4143 * user's responsibility. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004144 if (ssl->handshake->psk_opaque_is_internal) {
4145 psa_destroy_key(ssl->handshake->psk_opaque);
Neil Armstrong501c9322022-05-03 09:35:09 +02004146 ssl->handshake->psk_opaque_is_internal = 0;
4147 }
4148 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
4149 }
Neil Armstronge952a302022-05-03 10:22:14 +02004150#else
Gilles Peskine449bd832023-01-11 14:50:10 +01004151 if (handshake->psk != NULL) {
4152 mbedtls_platform_zeroize(handshake->psk, handshake->psk_len);
4153 mbedtls_free(handshake->psk);
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004154 }
Neil Armstronge952a302022-05-03 10:22:14 +02004155#endif /* MBEDTLS_USE_PSA_CRYPTO */
Ronald Cron73fe8df2022-10-05 14:31:43 +02004156#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004157
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004158#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
4159 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02004160 /*
4161 * Free only the linked list wrapper, not the keys themselves
4162 * since the belong to the SNI callback
4163 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004164 ssl_key_cert_free(handshake->sni_key_cert);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004165#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004166
Gilles Peskineeccd8882020-03-10 12:19:08 +01004167#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01004168 mbedtls_x509_crt_restart_free(&handshake->ecrs_ctx);
4169 if (handshake->ecrs_peer_cert != NULL) {
4170 mbedtls_x509_crt_free(handshake->ecrs_peer_cert);
4171 mbedtls_free(handshake->ecrs_peer_cert);
Hanno Becker3dad3112019-02-05 17:19:52 +00004172 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02004173#endif
4174
Hanno Becker75173122019-02-06 16:18:31 +00004175#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
4176 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01004177 mbedtls_pk_free(&handshake->peer_pubkey);
Hanno Becker75173122019-02-06 16:18:31 +00004178#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
4179
XiaokangQian9b93c0d2022-02-09 06:02:25 +00004180#if defined(MBEDTLS_SSL_CLI_C) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01004181 (defined(MBEDTLS_SSL_PROTO_DTLS) || defined(MBEDTLS_SSL_PROTO_TLS1_3))
4182 mbedtls_free(handshake->cookie);
XiaokangQian9b93c0d2022-02-09 06:02:25 +00004183#endif /* MBEDTLS_SSL_CLI_C &&
4184 ( MBEDTLS_SSL_PROTO_DTLS || MBEDTLS_SSL_PROTO_TLS1_3 ) */
XiaokangQian8499b6c2022-01-27 09:00:11 +00004185
4186#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004187 mbedtls_ssl_flight_free(handshake->flight);
4188 mbedtls_ssl_buffering_free(ssl);
XiaokangQian8499b6c2022-01-27 09:00:11 +00004189#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02004190
Ronald Cronf12b81d2022-03-15 10:42:41 +01004191#if defined(MBEDTLS_ECDH_C) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01004192 (defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3))
4193 if (handshake->ecdh_psa_privkey_is_external == 0) {
4194 psa_destroy_key(handshake->ecdh_psa_privkey);
4195 }
Hanno Becker4a63ed42019-01-08 11:39:35 +00004196#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
4197
Ronald Cron6f135e12021-12-08 16:57:54 +01004198#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01004199 mbedtls_ssl_transform_free(handshake->transform_handshake);
4200 mbedtls_free(handshake->transform_handshake);
Jerry Yu3d9b5902022-11-04 14:07:25 +08004201#if defined(MBEDTLS_SSL_EARLY_DATA)
Gilles Peskine449bd832023-01-11 14:50:10 +01004202 mbedtls_ssl_transform_free(handshake->transform_earlydata);
4203 mbedtls_free(handshake->transform_earlydata);
Jerry Yu3d9b5902022-11-04 14:07:25 +08004204#endif
Ronald Cron6f135e12021-12-08 16:57:54 +01004205#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yuba9c7272021-10-30 11:54:10 +08004206
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004207
4208#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4209 /* If the buffers are too big - reallocate. Because of the way Mbed TLS
4210 * processes datagrams and the fact that a datagram is allowed to have
4211 * several records in it, it is possible that the I/O buffers are not
4212 * empty at this stage */
Gilles Peskine449bd832023-01-11 14:50:10 +01004213 handle_buffer_resizing(ssl, 1, mbedtls_ssl_get_input_buflen(ssl),
4214 mbedtls_ssl_get_output_buflen(ssl));
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004215#endif
Hanno Becker3aa186f2021-08-10 09:24:19 +01004216
Jerry Yuba9c7272021-10-30 11:54:10 +08004217 /* mbedtls_platform_zeroize MUST be last one in this function */
Gilles Peskine449bd832023-01-11 14:50:10 +01004218 mbedtls_platform_zeroize(handshake,
4219 sizeof(mbedtls_ssl_handshake_params));
Paul Bakker48916f92012-09-16 19:57:18 +00004220}
4221
Gilles Peskine449bd832023-01-11 14:50:10 +01004222void mbedtls_ssl_session_free(mbedtls_ssl_session *session)
Paul Bakker48916f92012-09-16 19:57:18 +00004223{
Gilles Peskine449bd832023-01-11 14:50:10 +01004224 if (session == NULL) {
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004225 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01004226 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004227
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004228#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004229 ssl_clear_peer_cert(session);
Paul Bakkered27a042013-04-18 22:46:23 +02004230#endif
Paul Bakker0a597072012-09-25 21:55:46 +00004231
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02004232#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Xiaokang Qianbc663a02022-10-09 11:14:39 +00004233#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
4234 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01004235 mbedtls_free(session->hostname);
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00004236#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01004237 mbedtls_free(session->ticket);
Paul Bakkera503a632013-08-14 13:48:06 +02004238#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004239
Gilles Peskine449bd832023-01-11 14:50:10 +01004240 mbedtls_platform_zeroize(session, sizeof(mbedtls_ssl_session));
Paul Bakker48916f92012-09-16 19:57:18 +00004241}
4242
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02004243#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004244
4245#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4246#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u
4247#else
4248#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u
4249#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4250
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004251#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004252
4253#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4254#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u
4255#else
4256#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u
4257#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4258
4259#if defined(MBEDTLS_SSL_ALPN)
4260#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u
4261#else
4262#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u
4263#endif /* MBEDTLS_SSL_ALPN */
4264
4265#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0
4266#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1
4267#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2
4268#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3
4269
4270#define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \
Gilles Peskine449bd832023-01-11 14:50:10 +01004271 ((uint32_t) ( \
4272 (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << \
4273 SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT) | \
4274 (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << \
4275 SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT) | \
4276 (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << \
4277 SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT) | \
4278 (SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT) | \
4279 0u))
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004280
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004281static unsigned char ssl_serialized_context_header[] = {
4282 MBEDTLS_VERSION_MAJOR,
4283 MBEDTLS_VERSION_MINOR,
4284 MBEDTLS_VERSION_PATCH,
Gilles Peskine449bd832023-01-11 14:50:10 +01004285 MBEDTLS_BYTE_1(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
4286 MBEDTLS_BYTE_0(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
4287 MBEDTLS_BYTE_2(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG),
4288 MBEDTLS_BYTE_1(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG),
4289 MBEDTLS_BYTE_0(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG),
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004290};
4291
Paul Bakker5121ce52009-01-03 21:22:43 +00004292/*
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004293 * Serialize a full SSL context
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02004294 *
4295 * The format of the serialized data is:
4296 * (in the presentation language of TLS, RFC 8446 section 3)
4297 *
4298 * // header
4299 * opaque mbedtls_version[3]; // major, minor, patch
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004300 * opaque context_format[5]; // version-specific field determining
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02004301 * // the format of the remaining
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004302 * // serialized data.
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004303 * Note: When updating the format, remember to keep these
4304 * version+format bytes. (We may make their size part of the API.)
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02004305 *
4306 * // session sub-structure
4307 * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save()
4308 * // transform sub-structure
4309 * uint8 random[64]; // ServerHello.random+ClientHello.random
4310 * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value
4311 * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use
4312 * // fields from ssl_context
4313 * uint32 badmac_seen; // DTLS: number of records with failing MAC
4314 * uint64 in_window_top; // DTLS: last validated record seq_num
4315 * uint64 in_window; // DTLS: bitmask for replay protection
4316 * uint8 disable_datagram_packing; // DTLS: only one record per datagram
4317 * uint64 cur_out_ctr; // Record layer: outgoing sequence number
4318 * uint16 mtu; // DTLS: path mtu (max outgoing fragment size)
4319 * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol
4320 *
4321 * Note that many fields of the ssl_context or sub-structures are not
4322 * serialized, as they fall in one of the following categories:
4323 *
4324 * 1. forced value (eg in_left must be 0)
4325 * 2. pointer to dynamically-allocated memory (eg session, transform)
4326 * 3. value can be re-derived from other data (eg session keys from MS)
4327 * 4. value was temporary (eg content of input buffer)
4328 * 5. value will be provided by the user again (eg I/O callbacks and context)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004329 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004330int mbedtls_ssl_context_save(mbedtls_ssl_context *ssl,
4331 unsigned char *buf,
4332 size_t buf_len,
4333 size_t *olen)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004334{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004335 unsigned char *p = buf;
4336 size_t used = 0;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004337 size_t session_len;
4338 int ret = 0;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004339
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02004340 /*
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004341 * Enforce usage restrictions, see "return BAD_INPUT_DATA" in
4342 * this function's documentation.
4343 *
4344 * These are due to assumptions/limitations in the implementation. Some of
4345 * them are likely to stay (no handshake in progress) some might go away
4346 * (only DTLS) but are currently used to simplify the implementation.
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02004347 */
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004348 /* The initial handshake must be over */
Gilles Peskine449bd832023-01-11 14:50:10 +01004349 if (mbedtls_ssl_is_handshake_over(ssl) == 0) {
4350 MBEDTLS_SSL_DEBUG_MSG(1, ("Initial handshake isn't over"));
4351 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004352 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004353 if (ssl->handshake != NULL) {
4354 MBEDTLS_SSL_DEBUG_MSG(1, ("Handshake isn't completed"));
4355 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004356 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004357 /* Double-check that sub-structures are indeed ready */
Gilles Peskine449bd832023-01-11 14:50:10 +01004358 if (ssl->transform == NULL || ssl->session == NULL) {
4359 MBEDTLS_SSL_DEBUG_MSG(1, ("Serialised structures aren't ready"));
4360 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004361 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004362 /* There must be no pending incoming or outgoing data */
Gilles Peskine449bd832023-01-11 14:50:10 +01004363 if (mbedtls_ssl_check_pending(ssl) != 0) {
4364 MBEDTLS_SSL_DEBUG_MSG(1, ("There is pending incoming data"));
4365 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004366 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004367 if (ssl->out_left != 0) {
4368 MBEDTLS_SSL_DEBUG_MSG(1, ("There is pending outgoing data"));
4369 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004370 }
Dave Rodgman556e8a32022-12-06 16:31:25 +00004371 /* Protocol must be DTLS, not TLS */
Gilles Peskine449bd832023-01-11 14:50:10 +01004372 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
4373 MBEDTLS_SSL_DEBUG_MSG(1, ("Only DTLS is supported"));
4374 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004375 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004376 /* Version must be 1.2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004377 if (ssl->tls_version != MBEDTLS_SSL_VERSION_TLS1_2) {
4378 MBEDTLS_SSL_DEBUG_MSG(1, ("Only version 1.2 supported"));
4379 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004380 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004381 /* We must be using an AEAD ciphersuite */
Gilles Peskine449bd832023-01-11 14:50:10 +01004382 if (mbedtls_ssl_transform_uses_aead(ssl->transform) != 1) {
4383 MBEDTLS_SSL_DEBUG_MSG(1, ("Only AEAD ciphersuites supported"));
4384 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004385 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004386 /* Renegotiation must not be enabled */
4387#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01004388 if (ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED) {
4389 MBEDTLS_SSL_DEBUG_MSG(1, ("Renegotiation must not be enabled"));
4390 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004391 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004392#endif
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004393
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004394 /*
4395 * Version and format identifier
4396 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004397 used += sizeof(ssl_serialized_context_header);
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004398
Gilles Peskine449bd832023-01-11 14:50:10 +01004399 if (used <= buf_len) {
4400 memcpy(p, ssl_serialized_context_header,
4401 sizeof(ssl_serialized_context_header));
4402 p += sizeof(ssl_serialized_context_header);
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004403 }
4404
4405 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004406 * Session (length + data)
4407 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004408 ret = ssl_session_save(ssl->session, 1, NULL, 0, &session_len);
4409 if (ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL) {
4410 return ret;
4411 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004412
4413 used += 4 + session_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004414 if (used <= buf_len) {
4415 MBEDTLS_PUT_UINT32_BE(session_len, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004416 p += 4;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004417
Gilles Peskine449bd832023-01-11 14:50:10 +01004418 ret = ssl_session_save(ssl->session, 1,
4419 p, session_len, &session_len);
4420 if (ret != 0) {
4421 return ret;
4422 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004423
4424 p += session_len;
4425 }
4426
4427 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004428 * Transform
4429 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004430 used += sizeof(ssl->transform->randbytes);
4431 if (used <= buf_len) {
4432 memcpy(p, ssl->transform->randbytes,
4433 sizeof(ssl->transform->randbytes));
4434 p += sizeof(ssl->transform->randbytes);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004435 }
4436
4437#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4438 used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004439 if (used <= buf_len) {
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004440 *p++ = ssl->transform->in_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004441 memcpy(p, ssl->transform->in_cid, ssl->transform->in_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004442 p += ssl->transform->in_cid_len;
4443
4444 *p++ = ssl->transform->out_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004445 memcpy(p, ssl->transform->out_cid, ssl->transform->out_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004446 p += ssl->transform->out_cid_len;
4447 }
4448#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4449
4450 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004451 * Saved fields from top-level ssl_context structure
4452 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004453 used += 4;
Gilles Peskine449bd832023-01-11 14:50:10 +01004454 if (used <= buf_len) {
4455 MBEDTLS_PUT_UINT32_BE(ssl->badmac_seen, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004456 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004457 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004458
4459#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4460 used += 16;
Gilles Peskine449bd832023-01-11 14:50:10 +01004461 if (used <= buf_len) {
4462 MBEDTLS_PUT_UINT64_BE(ssl->in_window_top, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004463 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004464
Gilles Peskine449bd832023-01-11 14:50:10 +01004465 MBEDTLS_PUT_UINT64_BE(ssl->in_window, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004466 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004467 }
4468#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4469
4470#if defined(MBEDTLS_SSL_PROTO_DTLS)
4471 used += 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004472 if (used <= buf_len) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004473 *p++ = ssl->disable_datagram_packing;
4474 }
4475#endif /* MBEDTLS_SSL_PROTO_DTLS */
4476
Jerry Yuae0b2e22021-10-08 15:21:19 +08004477 used += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Gilles Peskine449bd832023-01-11 14:50:10 +01004478 if (used <= buf_len) {
4479 memcpy(p, ssl->cur_out_ctr, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN);
Jerry Yuae0b2e22021-10-08 15:21:19 +08004480 p += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004481 }
4482
4483#if defined(MBEDTLS_SSL_PROTO_DTLS)
4484 used += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01004485 if (used <= buf_len) {
4486 MBEDTLS_PUT_UINT16_BE(ssl->mtu, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004487 p += 2;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004488 }
4489#endif /* MBEDTLS_SSL_PROTO_DTLS */
4490
4491#if defined(MBEDTLS_SSL_ALPN)
4492 {
4493 const uint8_t alpn_len = ssl->alpn_chosen
Gilles Peskine449bd832023-01-11 14:50:10 +01004494 ? (uint8_t) strlen(ssl->alpn_chosen)
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004495 : 0;
4496
4497 used += 1 + alpn_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004498 if (used <= buf_len) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004499 *p++ = alpn_len;
4500
Gilles Peskine449bd832023-01-11 14:50:10 +01004501 if (ssl->alpn_chosen != NULL) {
4502 memcpy(p, ssl->alpn_chosen, alpn_len);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004503 p += alpn_len;
4504 }
4505 }
4506 }
4507#endif /* MBEDTLS_SSL_ALPN */
4508
4509 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004510 * Done
4511 */
4512 *olen = used;
4513
Gilles Peskine449bd832023-01-11 14:50:10 +01004514 if (used > buf_len) {
4515 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
4516 }
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004517
Gilles Peskine449bd832023-01-11 14:50:10 +01004518 MBEDTLS_SSL_DEBUG_BUF(4, "saved context", buf, used);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004519
Gilles Peskine449bd832023-01-11 14:50:10 +01004520 return mbedtls_ssl_session_reset_int(ssl, 0);
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004521}
4522
4523/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02004524 * Deserialize context, see mbedtls_ssl_context_save() for format.
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004525 *
4526 * This internal version is wrapped by a public function that cleans up in
4527 * case of error.
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004528 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004529MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01004530static int ssl_context_load(mbedtls_ssl_context *ssl,
4531 const unsigned char *buf,
4532 size_t len)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004533{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004534 const unsigned char *p = buf;
4535 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004536 size_t session_len;
Janos Follath865b3eb2019-12-16 11:46:15 +00004537 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004538#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek894edde2022-09-29 06:31:14 -04004539 tls_prf_fn prf_func = NULL;
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004540#endif
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004541
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004542 /*
4543 * The context should have been freshly setup or reset.
4544 * Give the user an error in case of obvious misuse.
Manuel Pégourié-Gonnard4ca930f2019-07-26 16:31:53 +02004545 * (Checking session is useful because it won't be NULL if we're
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004546 * renegotiating, or if the user mistakenly loaded a session first.)
4547 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004548 if (ssl->state != MBEDTLS_SSL_HELLO_REQUEST ||
4549 ssl->session != NULL) {
4550 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004551 }
4552
4553 /*
4554 * We can't check that the config matches the initial one, but we can at
4555 * least check it matches the requirements for serializing.
4556 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004557 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
Glenn Strauss2dfcea22022-03-14 17:26:42 -04004558 ssl->conf->max_tls_version < MBEDTLS_SSL_VERSION_TLS1_2 ||
4559 ssl->conf->min_tls_version > MBEDTLS_SSL_VERSION_TLS1_2 ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004560#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02004561 ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004562#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01004563 0) {
4564 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004565 }
4566
Gilles Peskine449bd832023-01-11 14:50:10 +01004567 MBEDTLS_SSL_DEBUG_BUF(4, "context to load", buf, len);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004568
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004569 /*
4570 * Check version identifier
4571 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004572 if ((size_t) (end - p) < sizeof(ssl_serialized_context_header)) {
4573 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004574 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004575
4576 if (memcmp(p, ssl_serialized_context_header,
4577 sizeof(ssl_serialized_context_header)) != 0) {
4578 return MBEDTLS_ERR_SSL_VERSION_MISMATCH;
4579 }
4580 p += sizeof(ssl_serialized_context_header);
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004581
4582 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004583 * Session
4584 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004585 if ((size_t) (end - p) < 4) {
4586 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4587 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004588
Gilles Peskine449bd832023-01-11 14:50:10 +01004589 session_len = ((size_t) p[0] << 24) |
4590 ((size_t) p[1] << 16) |
4591 ((size_t) p[2] << 8) |
4592 ((size_t) p[3]);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004593 p += 4;
4594
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004595 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00004596 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004597 ssl->session = ssl->session_negotiate;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004598 ssl->session_in = ssl->session;
4599 ssl->session_out = ssl->session;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004600 ssl->session_negotiate = NULL;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004601
Gilles Peskine449bd832023-01-11 14:50:10 +01004602 if ((size_t) (end - p) < session_len) {
4603 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4604 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004605
Gilles Peskine449bd832023-01-11 14:50:10 +01004606 ret = ssl_session_load(ssl->session, 1, p, session_len);
4607 if (ret != 0) {
4608 mbedtls_ssl_session_free(ssl->session);
4609 return ret;
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004610 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004611
4612 p += session_len;
4613
4614 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004615 * Transform
4616 */
4617
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004618 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00004619 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Jerry Yu2e199812022-12-01 18:57:19 +08004620#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004621 ssl->transform = ssl->transform_negotiate;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004622 ssl->transform_in = ssl->transform;
4623 ssl->transform_out = ssl->transform;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004624 ssl->transform_negotiate = NULL;
Jerry Yu2e199812022-12-01 18:57:19 +08004625#endif
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004626
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004627#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01004628 prf_func = ssl_tls12prf_from_cs(ssl->session->ciphersuite);
4629 if (prf_func == NULL) {
4630 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4631 }
Andrzej Kurek894edde2022-09-29 06:31:14 -04004632
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004633 /* Read random bytes and populate structure */
Gilles Peskine449bd832023-01-11 14:50:10 +01004634 if ((size_t) (end - p) < sizeof(ssl->transform->randbytes)) {
4635 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4636 }
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004637
Gilles Peskine449bd832023-01-11 14:50:10 +01004638 ret = ssl_tls12_populate_transform(ssl->transform,
4639 ssl->session->ciphersuite,
4640 ssl->session->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02004641#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004642 ssl->session->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02004643#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01004644 prf_func,
4645 p, /* currently pointing to randbytes */
4646 MBEDTLS_SSL_VERSION_TLS1_2, /* (D)TLS 1.2 is forced */
4647 ssl->conf->endpoint,
4648 ssl);
4649 if (ret != 0) {
4650 return ret;
4651 }
Jerry Yu840fbb22022-02-17 14:59:29 +08004652#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004653 p += sizeof(ssl->transform->randbytes);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004654
4655#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4656 /* Read connection IDs and store them */
Gilles Peskine449bd832023-01-11 14:50:10 +01004657 if ((size_t) (end - p) < 1) {
4658 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4659 }
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004660
4661 ssl->transform->in_cid_len = *p++;
4662
Gilles Peskine449bd832023-01-11 14:50:10 +01004663 if ((size_t) (end - p) < ssl->transform->in_cid_len + 1u) {
4664 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4665 }
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004666
Gilles Peskine449bd832023-01-11 14:50:10 +01004667 memcpy(ssl->transform->in_cid, p, ssl->transform->in_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004668 p += ssl->transform->in_cid_len;
4669
4670 ssl->transform->out_cid_len = *p++;
4671
Gilles Peskine449bd832023-01-11 14:50:10 +01004672 if ((size_t) (end - p) < ssl->transform->out_cid_len) {
4673 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4674 }
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004675
Gilles Peskine449bd832023-01-11 14:50:10 +01004676 memcpy(ssl->transform->out_cid, p, ssl->transform->out_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004677 p += ssl->transform->out_cid_len;
4678#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4679
4680 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004681 * Saved fields from top-level ssl_context structure
4682 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004683 if ((size_t) (end - p) < 4) {
4684 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4685 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004686
Gilles Peskine449bd832023-01-11 14:50:10 +01004687 ssl->badmac_seen = ((uint32_t) p[0] << 24) |
4688 ((uint32_t) p[1] << 16) |
4689 ((uint32_t) p[2] << 8) |
4690 ((uint32_t) p[3]);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004691 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004692
4693#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine449bd832023-01-11 14:50:10 +01004694 if ((size_t) (end - p) < 16) {
4695 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4696 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004697
Gilles Peskine449bd832023-01-11 14:50:10 +01004698 ssl->in_window_top = ((uint64_t) p[0] << 56) |
4699 ((uint64_t) p[1] << 48) |
4700 ((uint64_t) p[2] << 40) |
4701 ((uint64_t) p[3] << 32) |
4702 ((uint64_t) p[4] << 24) |
4703 ((uint64_t) p[5] << 16) |
4704 ((uint64_t) p[6] << 8) |
4705 ((uint64_t) p[7]);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004706 p += 8;
4707
Gilles Peskine449bd832023-01-11 14:50:10 +01004708 ssl->in_window = ((uint64_t) p[0] << 56) |
4709 ((uint64_t) p[1] << 48) |
4710 ((uint64_t) p[2] << 40) |
4711 ((uint64_t) p[3] << 32) |
4712 ((uint64_t) p[4] << 24) |
4713 ((uint64_t) p[5] << 16) |
4714 ((uint64_t) p[6] << 8) |
4715 ((uint64_t) p[7]);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004716 p += 8;
4717#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4718
4719#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004720 if ((size_t) (end - p) < 1) {
4721 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4722 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004723
4724 ssl->disable_datagram_packing = *p++;
4725#endif /* MBEDTLS_SSL_PROTO_DTLS */
4726
Gilles Peskine449bd832023-01-11 14:50:10 +01004727 if ((size_t) (end - p) < sizeof(ssl->cur_out_ctr)) {
4728 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4729 }
4730 memcpy(ssl->cur_out_ctr, p, sizeof(ssl->cur_out_ctr));
4731 p += sizeof(ssl->cur_out_ctr);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004732
4733#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004734 if ((size_t) (end - p) < 2) {
4735 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4736 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004737
Gilles Peskine449bd832023-01-11 14:50:10 +01004738 ssl->mtu = (p[0] << 8) | p[1];
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004739 p += 2;
4740#endif /* MBEDTLS_SSL_PROTO_DTLS */
4741
4742#if defined(MBEDTLS_SSL_ALPN)
4743 {
4744 uint8_t alpn_len;
4745 const char **cur;
4746
Gilles Peskine449bd832023-01-11 14:50:10 +01004747 if ((size_t) (end - p) < 1) {
4748 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4749 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004750
4751 alpn_len = *p++;
4752
Gilles Peskine449bd832023-01-11 14:50:10 +01004753 if (alpn_len != 0 && ssl->conf->alpn_list != NULL) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004754 /* alpn_chosen should point to an item in the configured list */
Gilles Peskine449bd832023-01-11 14:50:10 +01004755 for (cur = ssl->conf->alpn_list; *cur != NULL; cur++) {
4756 if (strlen(*cur) == alpn_len &&
4757 memcmp(p, cur, alpn_len) == 0) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004758 ssl->alpn_chosen = *cur;
4759 break;
4760 }
4761 }
4762 }
4763
4764 /* can only happen on conf mismatch */
Gilles Peskine449bd832023-01-11 14:50:10 +01004765 if (alpn_len != 0 && ssl->alpn_chosen == NULL) {
4766 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4767 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004768
4769 p += alpn_len;
4770 }
4771#endif /* MBEDTLS_SSL_ALPN */
4772
4773 /*
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004774 * Forced fields from top-level ssl_context structure
4775 *
4776 * Most of them already set to the correct value by mbedtls_ssl_init() and
4777 * mbedtls_ssl_reset(), so we only need to set the remaining ones.
4778 */
4779 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
Glenn Strauss60bfe602022-03-14 19:04:24 -04004780 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004781
Hanno Becker361b10d2019-08-30 10:42:49 +01004782 /* Adjust pointers for header fields of outgoing records to
4783 * the given transform, accounting for explicit IV and CID. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004784 mbedtls_ssl_update_out_pointers(ssl, ssl->transform);
Hanno Becker361b10d2019-08-30 10:42:49 +01004785
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004786#if defined(MBEDTLS_SSL_PROTO_DTLS)
4787 ssl->in_epoch = 1;
4788#endif
4789
4790 /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated,
4791 * which we don't want - otherwise we'd end up freeing the wrong transform
Hanno Beckerce5f5fd2020-02-05 10:47:44 +00004792 * by calling mbedtls_ssl_handshake_wrapup_free_hs_transform()
4793 * inappropriately. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004794 if (ssl->handshake != NULL) {
4795 mbedtls_ssl_handshake_free(ssl);
4796 mbedtls_free(ssl->handshake);
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004797 ssl->handshake = NULL;
4798 }
4799
4800 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004801 * Done - should have consumed entire buffer
4802 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004803 if (p != end) {
4804 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4805 }
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004806
Gilles Peskine449bd832023-01-11 14:50:10 +01004807 return 0;
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004808}
4809
4810/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02004811 * Deserialize context: public wrapper for error cleaning
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004812 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004813int mbedtls_ssl_context_load(mbedtls_ssl_context *context,
4814 const unsigned char *buf,
4815 size_t len)
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004816{
Gilles Peskine449bd832023-01-11 14:50:10 +01004817 int ret = ssl_context_load(context, buf, len);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004818
Gilles Peskine449bd832023-01-11 14:50:10 +01004819 if (ret != 0) {
4820 mbedtls_ssl_free(context);
4821 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004822
Gilles Peskine449bd832023-01-11 14:50:10 +01004823 return ret;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004824}
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02004825#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004826
4827/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004828 * Free an SSL context
4829 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004830void mbedtls_ssl_free(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00004831{
Gilles Peskine449bd832023-01-11 14:50:10 +01004832 if (ssl == NULL) {
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004833 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01004834 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004835
Gilles Peskine449bd832023-01-11 14:50:10 +01004836 MBEDTLS_SSL_DEBUG_MSG(2, ("=> free"));
Paul Bakker5121ce52009-01-03 21:22:43 +00004837
Gilles Peskine449bd832023-01-11 14:50:10 +01004838 if (ssl->out_buf != NULL) {
sander-visserb8aa2072020-05-06 22:05:13 +02004839#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4840 size_t out_buf_len = ssl->out_buf_len;
4841#else
4842 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
4843#endif
4844
Gilles Peskine449bd832023-01-11 14:50:10 +01004845 mbedtls_platform_zeroize(ssl->out_buf, out_buf_len);
4846 mbedtls_free(ssl->out_buf);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004847 ssl->out_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004848 }
4849
Gilles Peskine449bd832023-01-11 14:50:10 +01004850 if (ssl->in_buf != NULL) {
sander-visserb8aa2072020-05-06 22:05:13 +02004851#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4852 size_t in_buf_len = ssl->in_buf_len;
4853#else
4854 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
4855#endif
4856
Gilles Peskine449bd832023-01-11 14:50:10 +01004857 mbedtls_platform_zeroize(ssl->in_buf, in_buf_len);
4858 mbedtls_free(ssl->in_buf);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004859 ssl->in_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004860 }
4861
Gilles Peskine449bd832023-01-11 14:50:10 +01004862 if (ssl->transform) {
4863 mbedtls_ssl_transform_free(ssl->transform);
4864 mbedtls_free(ssl->transform);
Paul Bakker48916f92012-09-16 19:57:18 +00004865 }
4866
Gilles Peskine449bd832023-01-11 14:50:10 +01004867 if (ssl->handshake) {
4868 mbedtls_ssl_handshake_free(ssl);
4869 mbedtls_free(ssl->handshake);
Jerry Yu2e199812022-12-01 18:57:19 +08004870
4871#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01004872 mbedtls_ssl_transform_free(ssl->transform_negotiate);
4873 mbedtls_free(ssl->transform_negotiate);
Jerry Yu2e199812022-12-01 18:57:19 +08004874#endif
4875
Gilles Peskine449bd832023-01-11 14:50:10 +01004876 mbedtls_ssl_session_free(ssl->session_negotiate);
4877 mbedtls_free(ssl->session_negotiate);
Paul Bakker48916f92012-09-16 19:57:18 +00004878 }
4879
Ronald Cron6f135e12021-12-08 16:57:54 +01004880#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01004881 mbedtls_ssl_transform_free(ssl->transform_application);
4882 mbedtls_free(ssl->transform_application);
Ronald Cron6f135e12021-12-08 16:57:54 +01004883#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker3aa186f2021-08-10 09:24:19 +01004884
Gilles Peskine449bd832023-01-11 14:50:10 +01004885 if (ssl->session) {
4886 mbedtls_ssl_session_free(ssl->session);
4887 mbedtls_free(ssl->session);
Paul Bakkerc0463502013-02-14 11:19:38 +01004888 }
4889
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02004890#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004891 if (ssl->hostname != NULL) {
4892 mbedtls_platform_zeroize(ssl->hostname, strlen(ssl->hostname));
4893 mbedtls_free(ssl->hostname);
Paul Bakker5121ce52009-01-03 21:22:43 +00004894 }
Paul Bakker0be444a2013-08-27 21:55:01 +02004895#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004896
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02004897#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004898 mbedtls_free(ssl->cli_id);
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02004899#endif
4900
Gilles Peskine449bd832023-01-11 14:50:10 +01004901 MBEDTLS_SSL_DEBUG_MSG(2, ("<= free"));
Paul Bakker2da561c2009-02-05 18:00:28 +00004902
Paul Bakker86f04f42013-02-14 11:20:09 +01004903 /* Actually clear after last debug message */
Gilles Peskine449bd832023-01-11 14:50:10 +01004904 mbedtls_platform_zeroize(ssl, sizeof(mbedtls_ssl_context));
Paul Bakker5121ce52009-01-03 21:22:43 +00004905}
4906
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004907/*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08004908 * Initialize mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004909 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004910void mbedtls_ssl_config_init(mbedtls_ssl_config *conf)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004911{
Gilles Peskine449bd832023-01-11 14:50:10 +01004912 memset(conf, 0, sizeof(mbedtls_ssl_config));
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004913}
4914
Gilles Peskineae270bf2021-06-02 00:05:29 +02004915/* The selection should be the same as mbedtls_x509_crt_profile_default in
4916 * x509_crt.c, plus Montgomery curves for ECDHE. Here, the order matters:
Gilles Peskineb1940a72021-06-02 15:18:12 +02004917 * curves with a lower resource usage come first.
Gilles Peskineae270bf2021-06-02 00:05:29 +02004918 * See the documentation of mbedtls_ssl_conf_curves() for what we promise
Gilles Peskineb1940a72021-06-02 15:18:12 +02004919 * about this list.
4920 */
Brett Warrene0edc842021-08-17 09:53:13 +01004921static uint16_t ssl_preset_default_groups[] = {
Gilles Peskineae270bf2021-06-02 00:05:29 +02004922#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004923 MBEDTLS_SSL_IANA_TLS_GROUP_X25519,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004924#endif
4925#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004926 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004927#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02004928#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004929 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004930#endif
4931#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004932 MBEDTLS_SSL_IANA_TLS_GROUP_X448,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004933#endif
4934#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004935 MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004936#endif
Gilles Peskineae270bf2021-06-02 00:05:29 +02004937#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004938 MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004939#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02004940#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004941 MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004942#endif
4943#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004944 MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004945#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004946 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Gilles Peskineae270bf2021-06-02 00:05:29 +02004947};
Gilles Peskineae270bf2021-06-02 00:05:29 +02004948
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004949static int ssl_preset_suiteb_ciphersuites[] = {
4950 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
4951 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
4952 0
4953};
4954
Ronald Crone68ab4f2022-10-05 12:46:29 +02004955#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004956
Jerry Yu909df7b2022-01-22 11:56:27 +08004957/* NOTICE:
Jerry Yu0b994b82022-01-25 17:22:12 +08004958 * For ssl_preset_*_sig_algs and ssl_tls12_preset_*_sig_algs, the following
Jerry Yu370e1462022-01-25 10:36:53 +08004959 * rules SHOULD be upheld.
4960 * - No duplicate entries.
4961 * - But if there is a good reason, do not change the order of the algorithms.
Jerry Yu09a99fc2022-07-28 14:22:17 +08004962 * - ssl_tls12_preset* is for TLS 1.2 use only.
Jerry Yu370e1462022-01-25 10:36:53 +08004963 * - ssl_preset_* is for TLS 1.3 only or hybrid TLS 1.3/1.2 handshakes.
Jerry Yu1a8b4812022-01-20 17:56:50 +08004964 */
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004965static uint16_t ssl_preset_default_sig_algs[] = {
Jerry Yu1a8b4812022-01-20 17:56:50 +08004966
Andrzej Kurek25f27152022-08-17 16:09:31 -04004967#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 +08004968 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
4969 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004970#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA &&
Jerry Yued5e9f42022-01-26 11:21:34 +08004971 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004972
Andrzej Kurek25f27152022-08-17 16:09:31 -04004973#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 +08004974 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
4975 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004976#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu909df7b2022-01-22 11:56:27 +08004977 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
4978
Andrzej Kurek25f27152022-08-17 16:09:31 -04004979#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 +08004980 defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
4981 MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004982#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yued5e9f42022-01-26 11:21:34 +08004983 MBEDTLS_ECP_DP_SECP521R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004984
Gilles Peskine449bd832023-01-11 14:50:10 +01004985#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && \
4986 defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu9bb3ee42022-06-23 10:16:33 +08004987 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
Gilles Peskine449bd832023-01-11 14:50:10 +01004988#endif \
4989 /* 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 +08004990
Gilles Peskine449bd832023-01-11 14:50:10 +01004991#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && \
4992 defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu9bb3ee42022-06-23 10:16:33 +08004993 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
Gilles Peskine449bd832023-01-11 14:50:10 +01004994#endif \
4995 /* 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 +08004996
Gilles Peskine449bd832023-01-11 14:50:10 +01004997#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && \
4998 defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu9bb3ee42022-06-23 10:16:33 +08004999 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Gilles Peskine449bd832023-01-11 14:50:10 +01005000#endif \
5001 /* 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 +08005002
Andrzej Kurek25f27152022-08-17 16:09:31 -04005003#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 +08005004 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512,
5005#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA512_C */
5006
Andrzej Kurek25f27152022-08-17 16:09:31 -04005007#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 +08005008 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384,
5009#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA384_C */
5010
Andrzej Kurek25f27152022-08-17 16:09:31 -04005011#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 +08005012 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
5013#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA256_C */
5014
Gabor Mezei15b95a62022-05-09 16:37:58 +02005015 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08005016};
5017
5018/* NOTICE: see above */
5019#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5020static uint16_t ssl_tls12_preset_default_sig_algs[] = {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005021#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02005022#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005023 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA512),
Jerry Yu713013f2022-01-17 18:16:35 +08005024#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08005025#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
5026 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
5027#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02005028#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005029 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA512),
Gabor Mezeic1051b62022-05-10 13:13:58 +02005030#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005031#endif /* MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04005032#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02005033#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005034 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08005035#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08005036#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
5037 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
5038#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02005039#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005040 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384),
Gabor Mezeic1051b62022-05-10 13:13:58 +02005041#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005042#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04005043#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02005044#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005045 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08005046#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08005047#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
5048 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
5049#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02005050#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005051 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256),
Gabor Mezeic1051b62022-05-10 13:13:58 +02005052#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005053#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Gabor Mezei15b95a62022-05-09 16:37:58 +02005054 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08005055};
5056#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5057/* NOTICE: see above */
5058static uint16_t ssl_preset_suiteb_sig_algs[] = {
5059
Andrzej Kurek25f27152022-08-17 16:09:31 -04005060#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 +08005061 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
5062 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005063#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu909df7b2022-01-22 11:56:27 +08005064 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
5065
Andrzej Kurek25f27152022-08-17 16:09:31 -04005066#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 +08005067 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
5068 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005069#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu53037892022-01-25 11:02:06 +08005070 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08005071
Gilles Peskine449bd832023-01-11 14:50:10 +01005072#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && \
5073 defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu909df7b2022-01-22 11:56:27 +08005074 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Gilles Peskine449bd832023-01-11 14:50:10 +01005075#endif \
5076 /* 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 +08005077
Andrzej Kurek25f27152022-08-17 16:09:31 -04005078#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 +08005079 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005080#endif /* MBEDTLS_RSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yu53037892022-01-25 11:02:06 +08005081
Gabor Mezei15b95a62022-05-09 16:37:58 +02005082 MBEDTLS_TLS_SIG_NONE
Jerry Yu713013f2022-01-17 18:16:35 +08005083};
Jerry Yu6106fdc2022-01-12 16:36:14 +08005084
Jerry Yu909df7b2022-01-22 11:56:27 +08005085/* NOTICE: see above */
5086#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5087static uint16_t ssl_tls12_preset_suiteb_sig_algs[] = {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005088#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02005089#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005090 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256),
Jerry Yu713013f2022-01-17 18:16:35 +08005091#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02005092#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005093 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256),
Gabor Mezeic1051b62022-05-10 13:13:58 +02005094#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005095#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04005096#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02005097#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005098 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384),
Jerry Yu18c833e2022-01-25 10:55:47 +08005099#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02005100#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005101 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384),
Gabor Mezeic1051b62022-05-10 13:13:58 +02005102#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005103#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Gabor Mezei15b95a62022-05-09 16:37:58 +02005104 MBEDTLS_TLS_SIG_NONE
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01005105};
Jerry Yu909df7b2022-01-22 11:56:27 +08005106#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5107
Ronald Crone68ab4f2022-10-05 12:46:29 +02005108#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005109
Brett Warrene0edc842021-08-17 09:53:13 +01005110static uint16_t ssl_preset_suiteb_groups[] = {
Jaeden Amerod4311042019-06-03 08:27:16 +01005111#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01005112 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01005113#endif
5114#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01005115 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01005116#endif
Brett Warrene0edc842021-08-17 09:53:13 +01005117 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005118};
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005119
Ronald Crone68ab4f2022-10-05 12:46:29 +02005120#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08005121/* Function for checking `ssl_preset_*_sig_algs` and `ssl_tls12_preset_*_sig_algs`
Jerry Yu370e1462022-01-25 10:36:53 +08005122 * to make sure there are no duplicated signature algorithm entries. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005123MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01005124static int ssl_check_no_sig_alg_duplication(uint16_t *sig_algs)
Jerry Yu1a8b4812022-01-20 17:56:50 +08005125{
5126 size_t i, j;
5127 int ret = 0;
Jerry Yu909df7b2022-01-22 11:56:27 +08005128
Gilles Peskine449bd832023-01-11 14:50:10 +01005129 for (i = 0; sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++) {
5130 for (j = 0; j < i; j++) {
5131 if (sig_algs[i] != sig_algs[j]) {
Jerry Yuf377d642022-01-25 10:43:59 +08005132 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01005133 }
5134 mbedtls_printf(" entry(%04x,%" MBEDTLS_PRINTF_SIZET
5135 ") is duplicated at %" MBEDTLS_PRINTF_SIZET "\n",
5136 sig_algs[i], j, i);
Jerry Yuf377d642022-01-25 10:43:59 +08005137 ret = -1;
Jerry Yu1a8b4812022-01-20 17:56:50 +08005138 }
5139 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005140 return ret;
Jerry Yu1a8b4812022-01-20 17:56:50 +08005141}
5142
Ronald Crone68ab4f2022-10-05 12:46:29 +02005143#endif /* MBEDTLS_DEBUG_C && MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yu1a8b4812022-01-20 17:56:50 +08005144
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005145/*
Tillmann Karras588ad502015-09-25 04:27:22 +02005146 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005147 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005148int mbedtls_ssl_config_defaults(mbedtls_ssl_config *conf,
5149 int endpoint, int transport, int preset)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005150{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005151#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Janos Follath865b3eb2019-12-16 11:46:15 +00005152 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005153#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005154
Ronald Crone68ab4f2022-10-05 12:46:29 +02005155#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01005156 if (ssl_check_no_sig_alg_duplication(ssl_preset_suiteb_sig_algs)) {
5157 mbedtls_printf("ssl_preset_suiteb_sig_algs has duplicated entries\n");
5158 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu1a8b4812022-01-20 17:56:50 +08005159 }
5160
Gilles Peskine449bd832023-01-11 14:50:10 +01005161 if (ssl_check_no_sig_alg_duplication(ssl_preset_default_sig_algs)) {
5162 mbedtls_printf("ssl_preset_default_sig_algs has duplicated entries\n");
5163 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu1a8b4812022-01-20 17:56:50 +08005164 }
Jerry Yu909df7b2022-01-22 11:56:27 +08005165
5166#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01005167 if (ssl_check_no_sig_alg_duplication(ssl_tls12_preset_suiteb_sig_algs)) {
5168 mbedtls_printf("ssl_tls12_preset_suiteb_sig_algs has duplicated entries\n");
5169 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu909df7b2022-01-22 11:56:27 +08005170 }
5171
Gilles Peskine449bd832023-01-11 14:50:10 +01005172 if (ssl_check_no_sig_alg_duplication(ssl_tls12_preset_default_sig_algs)) {
5173 mbedtls_printf("ssl_tls12_preset_default_sig_algs has duplicated entries\n");
5174 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu909df7b2022-01-22 11:56:27 +08005175 }
5176#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Ronald Crone68ab4f2022-10-05 12:46:29 +02005177#endif /* MBEDTLS_DEBUG_C && MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yu1a8b4812022-01-20 17:56:50 +08005178
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02005179 /* Use the functions here so that they are covered in tests,
5180 * but otherwise access member directly for efficiency */
Gilles Peskine449bd832023-01-11 14:50:10 +01005181 mbedtls_ssl_conf_endpoint(conf, endpoint);
5182 mbedtls_ssl_conf_transport(conf, transport);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005183
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005184 /*
5185 * Things that are common to all presets
5186 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02005187#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005188 if (endpoint == MBEDTLS_SSL_IS_CLIENT) {
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02005189 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
5190#if defined(MBEDTLS_SSL_SESSION_TICKETS)
5191 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
5192#endif
5193 }
5194#endif
5195
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005196#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
5197 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
5198#endif
5199
5200#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
5201 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
5202#endif
5203
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02005204#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005205 conf->f_cookie_write = ssl_cookie_write_dummy;
5206 conf->f_cookie_check = ssl_cookie_check_dummy;
5207#endif
5208
5209#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
5210 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
5211#endif
5212
Janos Follath088ce432017-04-10 12:42:31 +01005213#if defined(MBEDTLS_SSL_SRV_C)
5214 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
TRodziewicz3946f792021-06-14 12:11:18 +02005215 conf->respect_cli_pref = MBEDTLS_SSL_SRV_CIPHERSUITE_ORDER_SERVER;
Janos Follath088ce432017-04-10 12:42:31 +01005216#endif
5217
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005218#if defined(MBEDTLS_SSL_PROTO_DTLS)
5219 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
5220 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
5221#endif
5222
5223#if defined(MBEDTLS_SSL_RENEGOTIATION)
5224 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005225 memset(conf->renego_period, 0x00, 2);
5226 memset(conf->renego_period + 2, 0xFF, 6);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005227#endif
5228
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005229#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005230 if (endpoint == MBEDTLS_SSL_IS_SERVER) {
Hanno Beckere2defad2021-07-24 05:59:17 +01005231 const unsigned char dhm_p[] =
5232 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
5233 const unsigned char dhm_g[] =
5234 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
Hanno Becker00d0a682017-10-04 13:14:29 +01005235
Gilles Peskine449bd832023-01-11 14:50:10 +01005236 if ((ret = mbedtls_ssl_conf_dh_param_bin(conf,
5237 dhm_p, sizeof(dhm_p),
5238 dhm_g, sizeof(dhm_g))) != 0) {
5239 return ret;
Hanno Beckere2defad2021-07-24 05:59:17 +01005240 }
5241 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02005242#endif
5243
Ronald Cron6f135e12021-12-08 16:57:54 +01005244#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu6ee56aa2022-12-06 17:47:22 +08005245
5246#if defined(MBEDTLS_SSL_EARLY_DATA)
Gilles Peskine449bd832023-01-11 14:50:10 +01005247 mbedtls_ssl_tls13_conf_early_data(conf, MBEDTLS_SSL_EARLY_DATA_DISABLED);
Jerry Yu6ee56aa2022-12-06 17:47:22 +08005248#if defined(MBEDTLS_SSL_SRV_C)
5249 mbedtls_ssl_tls13_conf_max_early_data_size(
Gilles Peskine449bd832023-01-11 14:50:10 +01005250 conf, MBEDTLS_SSL_MAX_EARLY_DATA_SIZE);
Jerry Yu6ee56aa2022-12-06 17:47:22 +08005251#endif
5252#endif /* MBEDTLS_SSL_EARLY_DATA */
5253
Jerry Yud0766ec2022-09-22 10:46:57 +08005254#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08005255 mbedtls_ssl_conf_new_session_tickets(
Gilles Peskine449bd832023-01-11 14:50:10 +01005256 conf, MBEDTLS_SSL_TLS1_3_DEFAULT_NEW_SESSION_TICKETS);
Jerry Yu1ad7ace2022-08-09 13:28:39 +08005257#endif
Hanno Becker71f1ed62021-07-24 06:01:47 +01005258 /*
5259 * Allow all TLS 1.3 key exchange modes by default.
5260 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00005261 conf->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Ronald Cron6f135e12021-12-08 16:57:54 +01005262#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01005263
Gilles Peskine449bd832023-01-11 14:50:10 +01005264 if (transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Glenn Strauss2dfcea22022-03-14 17:26:42 -04005265#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
XiaokangQian4d3a6042022-04-21 13:46:17 +00005266 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian060d8672022-04-21 09:24:56 +00005267 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian4d3a6042022-04-21 13:46:17 +00005268#else
Gilles Peskine449bd832023-01-11 14:50:10 +01005269 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQian060d8672022-04-21 09:24:56 +00005270#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005271 } else {
XiaokangQian4d3a6042022-04-21 13:46:17 +00005272#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01005273 if (endpoint == MBEDTLS_SSL_IS_CLIENT) {
XiaokangQian4d3a6042022-04-21 13:46:17 +00005274 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5275 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
Gilles Peskine449bd832023-01-11 14:50:10 +01005276 } else {
5277 /* Hybrid TLS 1.2 / 1.3 is not supported on server side yet */
XiaokangQian4d3a6042022-04-21 13:46:17 +00005278 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5279 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5280 }
5281#elif defined(MBEDTLS_SSL_PROTO_TLS1_3)
5282 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
5283 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
5284#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
5285 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5286 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5287#else
Gilles Peskine449bd832023-01-11 14:50:10 +01005288 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQian4d3a6042022-04-21 13:46:17 +00005289#endif
5290 }
Glenn Strauss2dfcea22022-03-14 17:26:42 -04005291
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005292 /*
5293 * Preset-specific defaults
5294 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005295 switch (preset) {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005296 /*
5297 * NSA Suite B
5298 */
5299 case MBEDTLS_SSL_PRESET_SUITEB:
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005300
Hanno Beckerd60b6c62021-04-29 12:04:11 +01005301 conf->ciphersuite_list = ssl_preset_suiteb_ciphersuites;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005302
5303#if defined(MBEDTLS_X509_CRT_PARSE_C)
5304 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005305#endif
5306
Ronald Crone68ab4f2022-10-05 12:46:29 +02005307#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08005308#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01005309 if (mbedtls_ssl_conf_is_tls12_only(conf)) {
Jerry Yu909df7b2022-01-22 11:56:27 +08005310 conf->sig_algs = ssl_tls12_preset_suiteb_sig_algs;
Gilles Peskine449bd832023-01-11 14:50:10 +01005311 } else
Jerry Yu909df7b2022-01-22 11:56:27 +08005312#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005313 conf->sig_algs = ssl_preset_suiteb_sig_algs;
Ronald Crone68ab4f2022-10-05 12:46:29 +02005314#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005315
Brett Warrene0edc842021-08-17 09:53:13 +01005316#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
5317 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005318#endif
Brett Warrene0edc842021-08-17 09:53:13 +01005319 conf->group_list = ssl_preset_suiteb_groups;
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02005320 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005321
5322 /*
5323 * Default
5324 */
5325 default:
Ronald Cronf6606552022-03-15 11:23:25 +01005326
Hanno Beckerd60b6c62021-04-29 12:04:11 +01005327 conf->ciphersuite_list = mbedtls_ssl_list_ciphersuites();
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005328
5329#if defined(MBEDTLS_X509_CRT_PARSE_C)
5330 conf->cert_profile = &mbedtls_x509_crt_profile_default;
5331#endif
5332
Ronald Crone68ab4f2022-10-05 12:46:29 +02005333#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08005334#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01005335 if (mbedtls_ssl_conf_is_tls12_only(conf)) {
Jerry Yu909df7b2022-01-22 11:56:27 +08005336 conf->sig_algs = ssl_tls12_preset_default_sig_algs;
Gilles Peskine449bd832023-01-11 14:50:10 +01005337 } else
Jerry Yu909df7b2022-01-22 11:56:27 +08005338#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005339 conf->sig_algs = ssl_preset_default_sig_algs;
Ronald Crone68ab4f2022-10-05 12:46:29 +02005340#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005341
Brett Warrene0edc842021-08-17 09:53:13 +01005342#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
5343 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005344#endif
Brett Warrene0edc842021-08-17 09:53:13 +01005345 conf->group_list = ssl_preset_default_groups;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005346
5347#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
5348 conf->dhm_min_bitlen = 1024;
5349#endif
5350 }
5351
Gilles Peskine449bd832023-01-11 14:50:10 +01005352 return 0;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005353}
5354
5355/*
5356 * Free mbedtls_ssl_config
5357 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005358void mbedtls_ssl_config_free(mbedtls_ssl_config *conf)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005359{
5360#if defined(MBEDTLS_DHM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005361 mbedtls_mpi_free(&conf->dhm_P);
5362 mbedtls_mpi_free(&conf->dhm_G);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005363#endif
5364
Ronald Cron73fe8df2022-10-05 14:31:43 +02005365#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02005366#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01005367 if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) {
Neil Armstrong501c9322022-05-03 09:35:09 +02005368 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
5369 }
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005370#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01005371 if (conf->psk != NULL) {
5372 mbedtls_platform_zeroize(conf->psk, conf->psk_len);
5373 mbedtls_free(conf->psk);
Azim Khan27e8a122018-03-21 14:24:11 +00005374 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005375 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09005376 }
5377
Gilles Peskine449bd832023-01-11 14:50:10 +01005378 if (conf->psk_identity != NULL) {
5379 mbedtls_platform_zeroize(conf->psk_identity, conf->psk_identity_len);
5380 mbedtls_free(conf->psk_identity);
Azim Khan27e8a122018-03-21 14:24:11 +00005381 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005382 conf->psk_identity_len = 0;
5383 }
Ronald Cron73fe8df2022-10-05 14:31:43 +02005384#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005385
5386#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005387 ssl_key_cert_free(conf->key_cert);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005388#endif
5389
Gilles Peskine449bd832023-01-11 14:50:10 +01005390 mbedtls_platform_zeroize(conf, sizeof(mbedtls_ssl_config));
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005391}
5392
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02005393#if defined(MBEDTLS_PK_C) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01005394 (defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C))
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005395/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005396 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005397 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005398unsigned char mbedtls_ssl_sig_from_pk(mbedtls_pk_context *pk)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005399{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005400#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005401 if (mbedtls_pk_can_do(pk, MBEDTLS_PK_RSA)) {
5402 return MBEDTLS_SSL_SIG_RSA;
5403 }
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005404#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005405#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005406 if (mbedtls_pk_can_do(pk, MBEDTLS_PK_ECDSA)) {
5407 return MBEDTLS_SSL_SIG_ECDSA;
5408 }
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005409#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005410 return MBEDTLS_SSL_SIG_ANON;
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005411}
5412
Gilles Peskine449bd832023-01-11 14:50:10 +01005413unsigned char mbedtls_ssl_sig_from_pk_alg(mbedtls_pk_type_t type)
Hanno Becker7e5437a2017-04-28 17:15:26 +01005414{
Gilles Peskine449bd832023-01-11 14:50:10 +01005415 switch (type) {
Hanno Becker7e5437a2017-04-28 17:15:26 +01005416 case MBEDTLS_PK_RSA:
Gilles Peskine449bd832023-01-11 14:50:10 +01005417 return MBEDTLS_SSL_SIG_RSA;
Hanno Becker7e5437a2017-04-28 17:15:26 +01005418 case MBEDTLS_PK_ECDSA:
5419 case MBEDTLS_PK_ECKEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01005420 return MBEDTLS_SSL_SIG_ECDSA;
Hanno Becker7e5437a2017-04-28 17:15:26 +01005421 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005422 return MBEDTLS_SSL_SIG_ANON;
Hanno Becker7e5437a2017-04-28 17:15:26 +01005423 }
5424}
5425
Gilles Peskine449bd832023-01-11 14:50:10 +01005426mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig(unsigned char sig)
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005427{
Gilles Peskine449bd832023-01-11 14:50:10 +01005428 switch (sig) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005429#if defined(MBEDTLS_RSA_C)
5430 case MBEDTLS_SSL_SIG_RSA:
Gilles Peskine449bd832023-01-11 14:50:10 +01005431 return MBEDTLS_PK_RSA;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005432#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005433#if defined(MBEDTLS_ECDSA_C)
5434 case MBEDTLS_SSL_SIG_ECDSA:
Gilles Peskine449bd832023-01-11 14:50:10 +01005435 return MBEDTLS_PK_ECDSA;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005436#endif
5437 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005438 return MBEDTLS_PK_NONE;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005439 }
5440}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02005441#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005442
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02005443/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005444 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02005445 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005446mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash(unsigned char hash)
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005447{
Gilles Peskine449bd832023-01-11 14:50:10 +01005448 switch (hash) {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005449#if defined(MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005450 case MBEDTLS_SSL_HASH_MD5:
Gilles Peskine449bd832023-01-11 14:50:10 +01005451 return MBEDTLS_MD_MD5;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005452#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005453#if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005454 case MBEDTLS_SSL_HASH_SHA1:
Gilles Peskine449bd832023-01-11 14:50:10 +01005455 return MBEDTLS_MD_SHA1;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005456#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005457#if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005458 case MBEDTLS_SSL_HASH_SHA224:
Gilles Peskine449bd832023-01-11 14:50:10 +01005459 return MBEDTLS_MD_SHA224;
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02005460#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005461#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005462 case MBEDTLS_SSL_HASH_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01005463 return MBEDTLS_MD_SHA256;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005464#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005465#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005466 case MBEDTLS_SSL_HASH_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01005467 return MBEDTLS_MD_SHA384;
Mateusz Starzyk3352a532021-04-06 14:28:22 +02005468#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005469#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005470 case MBEDTLS_SSL_HASH_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01005471 return MBEDTLS_MD_SHA512;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005472#endif
5473 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005474 return MBEDTLS_MD_NONE;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005475 }
5476}
5477
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005478/*
5479 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
5480 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005481unsigned char mbedtls_ssl_hash_from_md_alg(int md)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005482{
Gilles Peskine449bd832023-01-11 14:50:10 +01005483 switch (md) {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005484#if defined(MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005485 case MBEDTLS_MD_MD5:
Gilles Peskine449bd832023-01-11 14:50:10 +01005486 return MBEDTLS_SSL_HASH_MD5;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005487#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005488#if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005489 case MBEDTLS_MD_SHA1:
Gilles Peskine449bd832023-01-11 14:50:10 +01005490 return MBEDTLS_SSL_HASH_SHA1;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005491#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005492#if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005493 case MBEDTLS_MD_SHA224:
Gilles Peskine449bd832023-01-11 14:50:10 +01005494 return MBEDTLS_SSL_HASH_SHA224;
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02005495#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005496#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005497 case MBEDTLS_MD_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01005498 return MBEDTLS_SSL_HASH_SHA256;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005499#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005500#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005501 case MBEDTLS_MD_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01005502 return MBEDTLS_SSL_HASH_SHA384;
Mateusz Starzyk3352a532021-04-06 14:28:22 +02005503#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005504#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005505 case MBEDTLS_MD_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01005506 return MBEDTLS_SSL_HASH_SHA512;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005507#endif
5508 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005509 return MBEDTLS_SSL_HASH_NONE;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005510 }
5511}
5512
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005513/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005514 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005515 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005516 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005517int mbedtls_ssl_check_curve_tls_id(const mbedtls_ssl_context *ssl, uint16_t tls_id)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005518{
Gilles Peskine449bd832023-01-11 14:50:10 +01005519 const uint16_t *group_list = mbedtls_ssl_get_groups(ssl);
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005520
Gilles Peskine449bd832023-01-11 14:50:10 +01005521 if (group_list == NULL) {
5522 return -1;
Brett Warrene0edc842021-08-17 09:53:13 +01005523 }
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005524
Gilles Peskine449bd832023-01-11 14:50:10 +01005525 for (; *group_list != 0; group_list++) {
5526 if (*group_list == tls_id) {
5527 return 0;
5528 }
5529 }
5530
5531 return -1;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005532}
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005533
5534#if defined(MBEDTLS_ECP_C)
5535/*
5536 * Same as mbedtls_ssl_check_curve_tls_id() but with a mbedtls_ecp_group_id.
5537 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005538int mbedtls_ssl_check_curve(const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id)
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005539{
Gilles Peskine449bd832023-01-11 14:50:10 +01005540 uint16_t tls_id = mbedtls_ssl_get_tls_id_from_ecp_group_id(grp_id);
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005541
Gilles Peskine449bd832023-01-11 14:50:10 +01005542 if (tls_id == 0) {
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005543 return -1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005544 }
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005545
Gilles Peskine449bd832023-01-11 14:50:10 +01005546 return mbedtls_ssl_check_curve_tls_id(ssl, tls_id);
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005547}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02005548#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005549
Gilles Peskine449bd832023-01-11 14:50:10 +01005550#if defined(MBEDTLS_DEBUG_C)
Valerio Setti67419f02023-01-04 16:12:42 +01005551#define EC_NAME(_name_) _name_
5552#else
5553#define EC_NAME(_name_) NULL
5554#endif
5555
Valerio Setti18c9fed2022-12-30 17:44:24 +01005556static const struct {
5557 uint16_t tls_id;
5558 mbedtls_ecp_group_id ecp_group_id;
5559 psa_ecc_family_t psa_family;
5560 uint16_t bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01005561 const char *name;
Valerio Setti18c9fed2022-12-30 17:44:24 +01005562} tls_id_match_table[] =
5563{
5564#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_521)
Gilles Peskine449bd832023-01-11 14:50:10 +01005565 { 25, MBEDTLS_ECP_DP_SECP521R1, PSA_ECC_FAMILY_SECP_R1, 521, EC_NAME("secp521r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005566#endif
5567#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED) || defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512)
Gilles Peskine449bd832023-01-11 14:50:10 +01005568 { 28, MBEDTLS_ECP_DP_BP512R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 512, EC_NAME("brainpoolP512r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005569#endif
Valerio Setti67419f02023-01-04 16:12:42 +01005570#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_384)
Gilles Peskine449bd832023-01-11 14:50:10 +01005571 { 24, MBEDTLS_ECP_DP_SECP384R1, PSA_ECC_FAMILY_SECP_R1, 384, EC_NAME("secp384r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005572#endif
Valerio Setti67419f02023-01-04 16:12:42 +01005573#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED) || defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384)
Gilles Peskine449bd832023-01-11 14:50:10 +01005574 { 27, MBEDTLS_ECP_DP_BP384R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 384, EC_NAME("brainpoolP384r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005575#endif
Valerio Setti67419f02023-01-04 16:12:42 +01005576#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_256)
Gilles Peskine449bd832023-01-11 14:50:10 +01005577 { 23, MBEDTLS_ECP_DP_SECP256R1, PSA_ECC_FAMILY_SECP_R1, 256, EC_NAME("secp256r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005578#endif
5579#if defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED) || defined(PSA_WANT_ECC_SECP_K1_256)
Gilles Peskine449bd832023-01-11 14:50:10 +01005580 { 22, MBEDTLS_ECP_DP_SECP256K1, PSA_ECC_FAMILY_SECP_K1, 256, EC_NAME("secp256k1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005581#endif
Valerio Setti67419f02023-01-04 16:12:42 +01005582#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED) || defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256)
Gilles Peskine449bd832023-01-11 14:50:10 +01005583 { 26, MBEDTLS_ECP_DP_BP256R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 256, EC_NAME("brainpoolP256r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005584#endif
5585#if defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_224)
Gilles Peskine449bd832023-01-11 14:50:10 +01005586 { 21, MBEDTLS_ECP_DP_SECP224R1, PSA_ECC_FAMILY_SECP_R1, 224, EC_NAME("secp224r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005587#endif
5588#if defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED) || defined(PSA_WANT_ECC_SECP_K1_224)
Gilles Peskine449bd832023-01-11 14:50:10 +01005589 { 20, MBEDTLS_ECP_DP_SECP224K1, PSA_ECC_FAMILY_SECP_K1, 224, EC_NAME("secp224k1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005590#endif
5591#if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_192)
Gilles Peskine449bd832023-01-11 14:50:10 +01005592 { 19, MBEDTLS_ECP_DP_SECP192R1, PSA_ECC_FAMILY_SECP_R1, 192, EC_NAME("secp192r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005593#endif
5594#if defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED) || defined(PSA_WANT_ECC_SECP_K1_192)
Gilles Peskine449bd832023-01-11 14:50:10 +01005595 { 18, MBEDTLS_ECP_DP_SECP192K1, PSA_ECC_FAMILY_SECP_K1, 192, EC_NAME("secp192k1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005596#endif
5597#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED) || defined(PSA_WANT_ECC_MONTGOMERY_255)
Gilles Peskine449bd832023-01-11 14:50:10 +01005598 { 29, MBEDTLS_ECP_DP_CURVE25519, PSA_ECC_FAMILY_MONTGOMERY, 255, EC_NAME("x25519") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005599#endif
5600#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED) || defined(PSA_WANT_ECC_MONTGOMERY_448)
Gilles Peskine449bd832023-01-11 14:50:10 +01005601 { 30, MBEDTLS_ECP_DP_CURVE448, PSA_ECC_FAMILY_MONTGOMERY, 448, EC_NAME("x448") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005602#endif
5603 { 0, MBEDTLS_ECP_DP_NONE, 0, 0, NULL },
5604};
5605
Gilles Peskine449bd832023-01-11 14:50:10 +01005606int mbedtls_ssl_get_psa_curve_info_from_tls_id(uint16_t tls_id,
5607 psa_ecc_family_t *family,
5608 size_t *bits)
Valerio Setti18c9fed2022-12-30 17:44:24 +01005609{
Gilles Peskine449bd832023-01-11 14:50:10 +01005610 for (int i = 0; tls_id_match_table[i].tls_id != 0; i++) {
5611 if (tls_id_match_table[i].tls_id == tls_id) {
5612 if (family != NULL) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01005613 *family = tls_id_match_table[i].psa_family;
Gilles Peskine449bd832023-01-11 14:50:10 +01005614 }
5615 if (bits != NULL) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01005616 *bits = tls_id_match_table[i].bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01005617 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01005618 return PSA_SUCCESS;
5619 }
5620 }
5621
5622 return PSA_ERROR_NOT_SUPPORTED;
5623}
5624
Gilles Peskine449bd832023-01-11 14:50:10 +01005625mbedtls_ecp_group_id mbedtls_ssl_get_ecp_group_id_from_tls_id(uint16_t tls_id)
Valerio Setti18c9fed2022-12-30 17:44:24 +01005626{
Gilles Peskine449bd832023-01-11 14:50:10 +01005627 for (int i = 0; tls_id_match_table[i].tls_id != 0; i++) {
5628 if (tls_id_match_table[i].tls_id == tls_id) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01005629 return tls_id_match_table[i].ecp_group_id;
Gilles Peskine449bd832023-01-11 14:50:10 +01005630 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01005631 }
5632
5633 return MBEDTLS_ECP_DP_NONE;
5634}
5635
Gilles Peskine449bd832023-01-11 14:50:10 +01005636uint16_t mbedtls_ssl_get_tls_id_from_ecp_group_id(mbedtls_ecp_group_id grp_id)
Valerio Setti18c9fed2022-12-30 17:44:24 +01005637{
Gilles Peskine449bd832023-01-11 14:50:10 +01005638 for (int i = 0; tls_id_match_table[i].ecp_group_id != MBEDTLS_ECP_DP_NONE;
5639 i++) {
5640 if (tls_id_match_table[i].ecp_group_id == grp_id) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01005641 return tls_id_match_table[i].tls_id;
Gilles Peskine449bd832023-01-11 14:50:10 +01005642 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01005643 }
5644
5645 return 0;
5646}
5647
Valerio Setti67419f02023-01-04 16:12:42 +01005648#if defined(MBEDTLS_DEBUG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005649const char *mbedtls_ssl_get_curve_name_from_tls_id(uint16_t tls_id)
Valerio Setti18c9fed2022-12-30 17:44:24 +01005650{
Gilles Peskine449bd832023-01-11 14:50:10 +01005651 for (int i = 0; tls_id_match_table[i].tls_id != 0; i++) {
5652 if (tls_id_match_table[i].tls_id == tls_id) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01005653 return tls_id_match_table[i].name;
Gilles Peskine449bd832023-01-11 14:50:10 +01005654 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01005655 }
5656
5657 return NULL;
5658}
Valerio Setti67419f02023-01-04 16:12:42 +01005659#endif
Valerio Setti18c9fed2022-12-30 17:44:24 +01005660
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005661#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005662int mbedtls_ssl_check_cert_usage(const mbedtls_x509_crt *cert,
5663 const mbedtls_ssl_ciphersuite_t *ciphersuite,
5664 int cert_endpoint,
5665 uint32_t *flags)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005666{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005667 int ret = 0;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005668 int usage = 0;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005669 const char *ext_oid;
5670 size_t ext_len;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005671
Gilles Peskine449bd832023-01-11 14:50:10 +01005672 if (cert_endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005673 /* Server part of the key exchange */
Gilles Peskine449bd832023-01-11 14:50:10 +01005674 switch (ciphersuite->key_exchange) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005675 case MBEDTLS_KEY_EXCHANGE_RSA:
5676 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005677 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005678 break;
5679
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005680 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
5681 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
5682 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
5683 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005684 break;
5685
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005686 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
5687 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005688 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005689 break;
5690
5691 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005692 case MBEDTLS_KEY_EXCHANGE_NONE:
5693 case MBEDTLS_KEY_EXCHANGE_PSK:
5694 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
5695 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02005696 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005697 usage = 0;
5698 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005699 } else {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005700 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
5701 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005702 }
5703
Gilles Peskine449bd832023-01-11 14:50:10 +01005704 if (mbedtls_x509_crt_check_key_usage(cert, usage) != 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005705 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005706 ret = -1;
5707 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005708
Gilles Peskine449bd832023-01-11 14:50:10 +01005709 if (cert_endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005710 ext_oid = MBEDTLS_OID_SERVER_AUTH;
Gilles Peskine449bd832023-01-11 14:50:10 +01005711 ext_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_SERVER_AUTH);
5712 } else {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005713 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
Gilles Peskine449bd832023-01-11 14:50:10 +01005714 ext_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_CLIENT_AUTH);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005715 }
5716
Gilles Peskine449bd832023-01-11 14:50:10 +01005717 if (mbedtls_x509_crt_check_extended_key_usage(cert, ext_oid, ext_len) != 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005718 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005719 ret = -1;
5720 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005721
Gilles Peskine449bd832023-01-11 14:50:10 +01005722 return ret;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005723}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005724#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02005725
Jerry Yu148165c2021-09-24 23:20:59 +08005726#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01005727int mbedtls_ssl_get_handshake_transcript(mbedtls_ssl_context *ssl,
5728 const mbedtls_md_type_t md,
5729 unsigned char *dst,
5730 size_t dst_len,
5731 size_t *olen)
Jerry Yu148165c2021-09-24 23:20:59 +08005732{
Ronald Cronf6893e12022-01-07 22:09:01 +01005733 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5734 psa_hash_operation_t *hash_operation_to_clone;
5735 psa_hash_operation_t hash_operation = psa_hash_operation_init();
5736
Jerry Yu148165c2021-09-24 23:20:59 +08005737 *olen = 0;
Ronald Cronf6893e12022-01-07 22:09:01 +01005738
Gilles Peskine449bd832023-01-11 14:50:10 +01005739 switch (md) {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005740#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01005741 case MBEDTLS_MD_SHA384:
5742 hash_operation_to_clone = &ssl->handshake->fin_sha384_psa;
5743 break;
Ronald Cronf6893e12022-01-07 22:09:01 +01005744#endif
5745
Andrzej Kurek25f27152022-08-17 16:09:31 -04005746#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01005747 case MBEDTLS_MD_SHA256:
5748 hash_operation_to_clone = &ssl->handshake->fin_sha256_psa;
5749 break;
Ronald Cronf6893e12022-01-07 22:09:01 +01005750#endif
5751
Gilles Peskine449bd832023-01-11 14:50:10 +01005752 default:
5753 goto exit;
5754 }
5755
5756 status = psa_hash_clone(hash_operation_to_clone, &hash_operation);
5757 if (status != PSA_SUCCESS) {
Ronald Cronf6893e12022-01-07 22:09:01 +01005758 goto exit;
5759 }
5760
Gilles Peskine449bd832023-01-11 14:50:10 +01005761 status = psa_hash_finish(&hash_operation, dst, dst_len, olen);
5762 if (status != PSA_SUCCESS) {
Ronald Cronf6893e12022-01-07 22:09:01 +01005763 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005764 }
Ronald Cronf6893e12022-01-07 22:09:01 +01005765
5766exit:
Andrzej Kurekeabeb302022-10-17 07:52:51 -04005767#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
5768 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
5769 (void) ssl;
5770#endif
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05005771 return PSA_TO_MBEDTLS_ERR(status);
Jerry Yu148165c2021-09-24 23:20:59 +08005772}
5773#else /* MBEDTLS_USE_PSA_CRYPTO */
5774
Andrzej Kurek25f27152022-08-17 16:09:31 -04005775#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005776MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01005777static int ssl_get_handshake_transcript_sha384(mbedtls_ssl_context *ssl,
5778 unsigned char *dst,
5779 size_t dst_len,
5780 size_t *olen)
Jerry Yu24c0ec32021-09-09 14:21:07 +08005781{
Jerry Yu24c0ec32021-09-09 14:21:07 +08005782 int ret;
5783 mbedtls_sha512_context sha512;
5784
Gilles Peskine449bd832023-01-11 14:50:10 +01005785 if (dst_len < 48) {
5786 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
5787 }
Jerry Yu24c0ec32021-09-09 14:21:07 +08005788
Gilles Peskine449bd832023-01-11 14:50:10 +01005789 mbedtls_sha512_init(&sha512);
5790 mbedtls_sha512_clone(&sha512, &ssl->handshake->fin_sha384);
Jerry Yu24c0ec32021-09-09 14:21:07 +08005791
Gilles Peskine449bd832023-01-11 14:50:10 +01005792 if ((ret = mbedtls_sha512_finish(&sha512, dst)) != 0) {
5793 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_sha512_finish", ret);
Jerry Yu24c0ec32021-09-09 14:21:07 +08005794 goto exit;
5795 }
5796
5797 *olen = 48;
5798
5799exit:
5800
Gilles Peskine449bd832023-01-11 14:50:10 +01005801 mbedtls_sha512_free(&sha512);
5802 return ret;
Jerry Yu24c0ec32021-09-09 14:21:07 +08005803}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005804#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005805
Andrzej Kurek25f27152022-08-17 16:09:31 -04005806#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005807MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01005808static int ssl_get_handshake_transcript_sha256(mbedtls_ssl_context *ssl,
5809 unsigned char *dst,
5810 size_t dst_len,
5811 size_t *olen)
Jerry Yu24c0ec32021-09-09 14:21:07 +08005812{
Jerry Yu24c0ec32021-09-09 14:21:07 +08005813 int ret;
5814 mbedtls_sha256_context sha256;
5815
Gilles Peskine449bd832023-01-11 14:50:10 +01005816 if (dst_len < 32) {
5817 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
5818 }
Jerry Yu24c0ec32021-09-09 14:21:07 +08005819
Gilles Peskine449bd832023-01-11 14:50:10 +01005820 mbedtls_sha256_init(&sha256);
5821 mbedtls_sha256_clone(&sha256, &ssl->handshake->fin_sha256);
Jerry Yuc5aef882021-12-23 20:15:02 +08005822
Gilles Peskine449bd832023-01-11 14:50:10 +01005823 if ((ret = mbedtls_sha256_finish(&sha256, dst)) != 0) {
5824 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_sha256_finish", ret);
Jerry Yu24c0ec32021-09-09 14:21:07 +08005825 goto exit;
5826 }
5827
5828 *olen = 32;
5829
5830exit:
5831
Gilles Peskine449bd832023-01-11 14:50:10 +01005832 mbedtls_sha256_free(&sha256);
5833 return ret;
Jerry Yu24c0ec32021-09-09 14:21:07 +08005834}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005835#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005836
Gilles Peskine449bd832023-01-11 14:50:10 +01005837int mbedtls_ssl_get_handshake_transcript(mbedtls_ssl_context *ssl,
5838 const mbedtls_md_type_t md,
5839 unsigned char *dst,
5840 size_t dst_len,
5841 size_t *olen)
Jerry Yu24c0ec32021-09-09 14:21:07 +08005842{
Gilles Peskine449bd832023-01-11 14:50:10 +01005843 switch (md) {
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005844
Andrzej Kurek25f27152022-08-17 16:09:31 -04005845#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01005846 case MBEDTLS_MD_SHA384:
5847 return ssl_get_handshake_transcript_sha384(ssl, dst, dst_len, olen);
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005848#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005849
Andrzej Kurek25f27152022-08-17 16:09:31 -04005850#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01005851 case MBEDTLS_MD_SHA256:
5852 return ssl_get_handshake_transcript_sha256(ssl, dst, dst_len, olen);
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005853#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005854
Gilles Peskine449bd832023-01-11 14:50:10 +01005855 default:
Andrzej Kurek409248a2022-10-24 10:33:21 -04005856#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01005857 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
5858 (void) ssl;
5859 (void) dst;
5860 (void) dst_len;
5861 (void) olen;
Andrzej Kurek409248a2022-10-24 10:33:21 -04005862#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005863 break;
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005864 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005865 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu24c0ec32021-09-09 14:21:07 +08005866}
XiaokangQian647719a2021-12-07 09:16:29 +00005867
Jerry Yu148165c2021-09-24 23:20:59 +08005868#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005869
Ronald Crone68ab4f2022-10-05 12:46:29 +02005870#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Gabor Mezei078e8032022-04-27 21:17:56 +02005871/* mbedtls_ssl_parse_sig_alg_ext()
5872 *
5873 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
5874 * value (TLS 1.3 RFC8446):
5875 * enum {
5876 * ....
5877 * ecdsa_secp256r1_sha256( 0x0403 ),
5878 * ecdsa_secp384r1_sha384( 0x0503 ),
5879 * ecdsa_secp521r1_sha512( 0x0603 ),
5880 * ....
5881 * } SignatureScheme;
5882 *
5883 * struct {
5884 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
5885 * } SignatureSchemeList;
5886 *
5887 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
5888 * value (TLS 1.2 RFC5246):
5889 * enum {
5890 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
5891 * sha512(6), (255)
5892 * } HashAlgorithm;
5893 *
5894 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
5895 * SignatureAlgorithm;
5896 *
5897 * struct {
5898 * HashAlgorithm hash;
5899 * SignatureAlgorithm signature;
5900 * } SignatureAndHashAlgorithm;
5901 *
5902 * SignatureAndHashAlgorithm
5903 * supported_signature_algorithms<2..2^16-2>;
5904 *
5905 * The TLS 1.3 signature algorithm extension was defined to be a compatible
5906 * generalization of the TLS 1.2 signature algorithm extension.
5907 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
5908 * `SignatureScheme` field of TLS 1.3
5909 *
5910 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005911int mbedtls_ssl_parse_sig_alg_ext(mbedtls_ssl_context *ssl,
5912 const unsigned char *buf,
5913 const unsigned char *end)
Gabor Mezei078e8032022-04-27 21:17:56 +02005914{
5915 const unsigned char *p = buf;
5916 size_t supported_sig_algs_len = 0;
5917 const unsigned char *supported_sig_algs_end;
5918 uint16_t sig_alg;
5919 uint32_t common_idx = 0;
5920
Gilles Peskine449bd832023-01-11 14:50:10 +01005921 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
5922 supported_sig_algs_len = MBEDTLS_GET_UINT16_BE(p, 0);
Gabor Mezei078e8032022-04-27 21:17:56 +02005923 p += 2;
5924
Gilles Peskine449bd832023-01-11 14:50:10 +01005925 memset(ssl->handshake->received_sig_algs, 0,
5926 sizeof(ssl->handshake->received_sig_algs));
Gabor Mezei078e8032022-04-27 21:17:56 +02005927
Gilles Peskine449bd832023-01-11 14:50:10 +01005928 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, supported_sig_algs_len);
Gabor Mezei078e8032022-04-27 21:17:56 +02005929 supported_sig_algs_end = p + supported_sig_algs_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01005930 while (p < supported_sig_algs_end) {
5931 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, supported_sig_algs_end, 2);
5932 sig_alg = MBEDTLS_GET_UINT16_BE(p, 0);
Gabor Mezei078e8032022-04-27 21:17:56 +02005933 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01005934 MBEDTLS_SSL_DEBUG_MSG(4, ("received signature algorithm: 0x%x %s",
5935 sig_alg,
5936 mbedtls_ssl_sig_alg_to_str(sig_alg)));
Jerry Yu2fe6c632022-06-29 10:02:38 +08005937#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01005938 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 &&
5939 (!(mbedtls_ssl_sig_alg_is_supported(ssl, sig_alg) &&
5940 mbedtls_ssl_sig_alg_is_offered(ssl, sig_alg)))) {
Gabor Mezei078e8032022-04-27 21:17:56 +02005941 continue;
Jerry Yu2fe6c632022-06-29 10:02:38 +08005942 }
5943#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gabor Mezei078e8032022-04-27 21:17:56 +02005944
Gilles Peskine449bd832023-01-11 14:50:10 +01005945 MBEDTLS_SSL_DEBUG_MSG(4, ("valid signature algorithm: %s",
5946 mbedtls_ssl_sig_alg_to_str(sig_alg)));
Gabor Mezei078e8032022-04-27 21:17:56 +02005947
Gilles Peskine449bd832023-01-11 14:50:10 +01005948 if (common_idx + 1 < MBEDTLS_RECEIVED_SIG_ALGS_SIZE) {
Gabor Mezei078e8032022-04-27 21:17:56 +02005949 ssl->handshake->received_sig_algs[common_idx] = sig_alg;
5950 common_idx += 1;
5951 }
5952 }
5953 /* Check that we consumed all the message. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005954 if (p != end) {
5955 MBEDTLS_SSL_DEBUG_MSG(1,
5956 ("Signature algorithms extension length misaligned"));
5957 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
5958 MBEDTLS_ERR_SSL_DECODE_ERROR);
5959 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Gabor Mezei078e8032022-04-27 21:17:56 +02005960 }
5961
Gilles Peskine449bd832023-01-11 14:50:10 +01005962 if (common_idx == 0) {
5963 MBEDTLS_SSL_DEBUG_MSG(3, ("no signature algorithm in common"));
5964 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
5965 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
5966 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Gabor Mezei078e8032022-04-27 21:17:56 +02005967 }
5968
Gabor Mezei15b95a62022-05-09 16:37:58 +02005969 ssl->handshake->received_sig_algs[common_idx] = MBEDTLS_TLS_SIG_NONE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005970 return 0;
Gabor Mezei078e8032022-04-27 21:17:56 +02005971}
5972
Ronald Crone68ab4f2022-10-05 12:46:29 +02005973#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Gabor Mezei078e8032022-04-27 21:17:56 +02005974
Jerry Yudc7bd172022-02-17 13:44:15 +08005975#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5976
5977#if defined(MBEDTLS_USE_PSA_CRYPTO)
5978
Gilles Peskine449bd832023-01-11 14:50:10 +01005979static psa_status_t setup_psa_key_derivation(psa_key_derivation_operation_t *derivation,
5980 mbedtls_svc_key_id_t key,
5981 psa_algorithm_t alg,
5982 const unsigned char *raw_psk, size_t raw_psk_length,
5983 const unsigned char *seed, size_t seed_length,
5984 const unsigned char *label, size_t label_length,
5985 const unsigned char *other_secret,
5986 size_t other_secret_length,
5987 size_t capacity)
Jerry Yudc7bd172022-02-17 13:44:15 +08005988{
5989 psa_status_t status;
5990
Gilles Peskine449bd832023-01-11 14:50:10 +01005991 status = psa_key_derivation_setup(derivation, alg);
5992 if (status != PSA_SUCCESS) {
5993 return status;
5994 }
Jerry Yudc7bd172022-02-17 13:44:15 +08005995
Gilles Peskine449bd832023-01-11 14:50:10 +01005996 if (PSA_ALG_IS_TLS12_PRF(alg) || PSA_ALG_IS_TLS12_PSK_TO_MS(alg)) {
5997 status = psa_key_derivation_input_bytes(derivation,
5998 PSA_KEY_DERIVATION_INPUT_SEED,
5999 seed, seed_length);
6000 if (status != PSA_SUCCESS) {
6001 return status;
Przemek Stekiel1f027032022-04-05 17:12:11 +02006002 }
6003
Gilles Peskine449bd832023-01-11 14:50:10 +01006004 if (other_secret != NULL) {
6005 status = psa_key_derivation_input_bytes(derivation,
6006 PSA_KEY_DERIVATION_INPUT_OTHER_SECRET,
6007 other_secret, other_secret_length);
6008 if (status != PSA_SUCCESS) {
6009 return status;
6010 }
6011 }
6012
6013 if (mbedtls_svc_key_id_is_null(key)) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006014 status = psa_key_derivation_input_bytes(
6015 derivation, PSA_KEY_DERIVATION_INPUT_SECRET,
Gilles Peskine449bd832023-01-11 14:50:10 +01006016 raw_psk, raw_psk_length);
6017 } else {
Jerry Yudc7bd172022-02-17 13:44:15 +08006018 status = psa_key_derivation_input_key(
Gilles Peskine449bd832023-01-11 14:50:10 +01006019 derivation, PSA_KEY_DERIVATION_INPUT_SECRET, key);
Jerry Yudc7bd172022-02-17 13:44:15 +08006020 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006021 if (status != PSA_SUCCESS) {
6022 return status;
6023 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006024
Gilles Peskine449bd832023-01-11 14:50:10 +01006025 status = psa_key_derivation_input_bytes(derivation,
6026 PSA_KEY_DERIVATION_INPUT_LABEL,
6027 label, label_length);
6028 if (status != PSA_SUCCESS) {
6029 return status;
6030 }
6031 } else {
6032 return PSA_ERROR_NOT_SUPPORTED;
Jerry Yudc7bd172022-02-17 13:44:15 +08006033 }
6034
Gilles Peskine449bd832023-01-11 14:50:10 +01006035 status = psa_key_derivation_set_capacity(derivation, capacity);
6036 if (status != PSA_SUCCESS) {
6037 return status;
6038 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006039
Gilles Peskine449bd832023-01-11 14:50:10 +01006040 return PSA_SUCCESS;
Jerry Yudc7bd172022-02-17 13:44:15 +08006041}
6042
Andrzej Kurek57d10632022-10-24 10:32:01 -04006043#if defined(PSA_WANT_ALG_SHA_384) || \
6044 defined(PSA_WANT_ALG_SHA_256)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006045MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006046static int tls_prf_generic(mbedtls_md_type_t md_type,
6047 const unsigned char *secret, size_t slen,
6048 const char *label,
6049 const unsigned char *random, size_t rlen,
6050 unsigned char *dstbuf, size_t dlen)
Jerry Yudc7bd172022-02-17 13:44:15 +08006051{
6052 psa_status_t status;
6053 psa_algorithm_t alg;
6054 mbedtls_svc_key_id_t master_key = MBEDTLS_SVC_KEY_ID_INIT;
6055 psa_key_derivation_operation_t derivation =
6056 PSA_KEY_DERIVATION_OPERATION_INIT;
6057
Gilles Peskine449bd832023-01-11 14:50:10 +01006058 if (md_type == MBEDTLS_MD_SHA384) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006059 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
Gilles Peskine449bd832023-01-11 14:50:10 +01006060 } else {
Jerry Yudc7bd172022-02-17 13:44:15 +08006061 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
Gilles Peskine449bd832023-01-11 14:50:10 +01006062 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006063
6064 /* Normally a "secret" should be long enough to be impossible to
6065 * find by brute force, and in particular should not be empty. But
6066 * this PRF is also used to derive an IV, in particular in EAP-TLS,
6067 * and for this use case it makes sense to have a 0-length "secret".
6068 * Since the key API doesn't allow importing a key of length 0,
6069 * keep master_key=0, which setup_psa_key_derivation() understands
6070 * to mean a 0-length "secret" input. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006071 if (slen != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006072 psa_key_attributes_t key_attributes = psa_key_attributes_init();
Gilles Peskine449bd832023-01-11 14:50:10 +01006073 psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE);
6074 psa_set_key_algorithm(&key_attributes, alg);
6075 psa_set_key_type(&key_attributes, PSA_KEY_TYPE_DERIVE);
Jerry Yudc7bd172022-02-17 13:44:15 +08006076
Gilles Peskine449bd832023-01-11 14:50:10 +01006077 status = psa_import_key(&key_attributes, secret, slen, &master_key);
6078 if (status != PSA_SUCCESS) {
6079 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
6080 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006081 }
6082
Gilles Peskine449bd832023-01-11 14:50:10 +01006083 status = setup_psa_key_derivation(&derivation,
6084 master_key, alg,
6085 NULL, 0,
6086 random, rlen,
6087 (unsigned char const *) label,
6088 (size_t) strlen(label),
6089 NULL, 0,
6090 dlen);
6091 if (status != PSA_SUCCESS) {
6092 psa_key_derivation_abort(&derivation);
6093 psa_destroy_key(master_key);
6094 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yudc7bd172022-02-17 13:44:15 +08006095 }
6096
Gilles Peskine449bd832023-01-11 14:50:10 +01006097 status = psa_key_derivation_output_bytes(&derivation, dstbuf, dlen);
6098 if (status != PSA_SUCCESS) {
6099 psa_key_derivation_abort(&derivation);
6100 psa_destroy_key(master_key);
6101 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yudc7bd172022-02-17 13:44:15 +08006102 }
6103
Gilles Peskine449bd832023-01-11 14:50:10 +01006104 status = psa_key_derivation_abort(&derivation);
6105 if (status != PSA_SUCCESS) {
6106 psa_destroy_key(master_key);
6107 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yudc7bd172022-02-17 13:44:15 +08006108 }
6109
Gilles Peskine449bd832023-01-11 14:50:10 +01006110 if (!mbedtls_svc_key_id_is_null(master_key)) {
6111 status = psa_destroy_key(master_key);
6112 }
6113 if (status != PSA_SUCCESS) {
6114 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
6115 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006116
Gilles Peskine449bd832023-01-11 14:50:10 +01006117 return 0;
Jerry Yudc7bd172022-02-17 13:44:15 +08006118}
Andrzej Kurek57d10632022-10-24 10:32:01 -04006119#endif /* PSA_WANT_ALG_SHA_256 || PSA_WANT_ALG_SHA_384 */
Jerry Yudc7bd172022-02-17 13:44:15 +08006120#else /* MBEDTLS_USE_PSA_CRYPTO */
6121
Andrzej Kurek57d10632022-10-24 10:32:01 -04006122#if defined(MBEDTLS_MD_C) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01006123 (defined(MBEDTLS_SHA256_C) || \
6124 defined(MBEDTLS_SHA384_C))
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006125MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006126static int tls_prf_generic(mbedtls_md_type_t md_type,
6127 const unsigned char *secret, size_t slen,
6128 const char *label,
6129 const unsigned char *random, size_t rlen,
6130 unsigned char *dstbuf, size_t dlen)
Jerry Yudc7bd172022-02-17 13:44:15 +08006131{
6132 size_t nb;
6133 size_t i, j, k, md_len;
6134 unsigned char *tmp;
6135 size_t tmp_len = 0;
6136 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
6137 const mbedtls_md_info_t *md_info;
6138 mbedtls_md_context_t md_ctx;
6139 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6140
Gilles Peskine449bd832023-01-11 14:50:10 +01006141 mbedtls_md_init(&md_ctx);
Jerry Yudc7bd172022-02-17 13:44:15 +08006142
Gilles Peskine449bd832023-01-11 14:50:10 +01006143 if ((md_info = mbedtls_md_info_from_type(md_type)) == NULL) {
6144 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
6145 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006146
Gilles Peskine449bd832023-01-11 14:50:10 +01006147 md_len = mbedtls_md_get_size(md_info);
Jerry Yudc7bd172022-02-17 13:44:15 +08006148
Gilles Peskine449bd832023-01-11 14:50:10 +01006149 tmp_len = md_len + strlen(label) + rlen;
6150 tmp = mbedtls_calloc(1, tmp_len);
6151 if (tmp == NULL) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006152 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
6153 goto exit;
6154 }
6155
Gilles Peskine449bd832023-01-11 14:50:10 +01006156 nb = strlen(label);
6157 memcpy(tmp + md_len, label, nb);
6158 memcpy(tmp + md_len + nb, random, rlen);
Jerry Yudc7bd172022-02-17 13:44:15 +08006159 nb += rlen;
6160
6161 /*
6162 * Compute P_<hash>(secret, label + random)[0..dlen]
6163 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006164 if ((ret = mbedtls_md_setup(&md_ctx, md_info, 1)) != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006165 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006166 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006167
Gilles Peskine449bd832023-01-11 14:50:10 +01006168 ret = mbedtls_md_hmac_starts(&md_ctx, secret, slen);
6169 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006170 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006171 }
6172 ret = mbedtls_md_hmac_update(&md_ctx, tmp + md_len, nb);
6173 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006174 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006175 }
6176 ret = mbedtls_md_hmac_finish(&md_ctx, tmp);
6177 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006178 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006179 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006180
Gilles Peskine449bd832023-01-11 14:50:10 +01006181 for (i = 0; i < dlen; i += md_len) {
6182 ret = mbedtls_md_hmac_reset(&md_ctx);
6183 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006184 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006185 }
6186 ret = mbedtls_md_hmac_update(&md_ctx, tmp, md_len + nb);
6187 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006188 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006189 }
6190 ret = mbedtls_md_hmac_finish(&md_ctx, h_i);
6191 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006192 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006193 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006194
Gilles Peskine449bd832023-01-11 14:50:10 +01006195 ret = mbedtls_md_hmac_reset(&md_ctx);
6196 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006197 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006198 }
6199 ret = mbedtls_md_hmac_update(&md_ctx, tmp, md_len);
6200 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006201 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006202 }
6203 ret = mbedtls_md_hmac_finish(&md_ctx, tmp);
6204 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006205 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006206 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006207
Gilles Peskine449bd832023-01-11 14:50:10 +01006208 k = (i + md_len > dlen) ? dlen % md_len : md_len;
Jerry Yudc7bd172022-02-17 13:44:15 +08006209
Gilles Peskine449bd832023-01-11 14:50:10 +01006210 for (j = 0; j < k; j++) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006211 dstbuf[i + j] = h_i[j];
Gilles Peskine449bd832023-01-11 14:50:10 +01006212 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006213 }
6214
6215exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006216 mbedtls_md_free(&md_ctx);
Jerry Yudc7bd172022-02-17 13:44:15 +08006217
Gilles Peskine449bd832023-01-11 14:50:10 +01006218 if (tmp != NULL) {
6219 mbedtls_platform_zeroize(tmp, tmp_len);
6220 }
Dave Rodgman29b9b2b2022-11-01 16:08:14 +00006221
Gilles Peskine449bd832023-01-11 14:50:10 +01006222 mbedtls_platform_zeroize(h_i, sizeof(h_i));
Jerry Yudc7bd172022-02-17 13:44:15 +08006223
Gilles Peskine449bd832023-01-11 14:50:10 +01006224 mbedtls_free(tmp);
Jerry Yudc7bd172022-02-17 13:44:15 +08006225
Gilles Peskine449bd832023-01-11 14:50:10 +01006226 return ret;
Jerry Yudc7bd172022-02-17 13:44:15 +08006227}
Andrzej Kurek57d10632022-10-24 10:32:01 -04006228#endif /* MBEDTLS_MD_C && ( MBEDTLS_SHA256_C || MBEDTLS_SHA384_C ) */
Jerry Yudc7bd172022-02-17 13:44:15 +08006229#endif /* MBEDTLS_USE_PSA_CRYPTO */
6230
Andrzej Kurek25f27152022-08-17 16:09:31 -04006231#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006232MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006233static int tls_prf_sha256(const unsigned char *secret, size_t slen,
6234 const char *label,
6235 const unsigned char *random, size_t rlen,
6236 unsigned char *dstbuf, size_t dlen)
Jerry Yudc7bd172022-02-17 13:44:15 +08006237{
Gilles Peskine449bd832023-01-11 14:50:10 +01006238 return tls_prf_generic(MBEDTLS_MD_SHA256, secret, slen,
6239 label, random, rlen, dstbuf, dlen);
Jerry Yudc7bd172022-02-17 13:44:15 +08006240}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04006241#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudc7bd172022-02-17 13:44:15 +08006242
Andrzej Kurek25f27152022-08-17 16:09:31 -04006243#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006244MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006245static int tls_prf_sha384(const unsigned char *secret, size_t slen,
6246 const char *label,
6247 const unsigned char *random, size_t rlen,
6248 unsigned char *dstbuf, size_t dlen)
Jerry Yudc7bd172022-02-17 13:44:15 +08006249{
Gilles Peskine449bd832023-01-11 14:50:10 +01006250 return tls_prf_generic(MBEDTLS_MD_SHA384, secret, slen,
6251 label, random, rlen, dstbuf, dlen);
Jerry Yudc7bd172022-02-17 13:44:15 +08006252}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04006253#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudc7bd172022-02-17 13:44:15 +08006254
Jerry Yuf009d862022-02-17 14:01:37 +08006255/*
6256 * Set appropriate PRF function and other SSL / TLS1.2 functions
6257 *
6258 * Inputs:
Jerry Yuf009d862022-02-17 14:01:37 +08006259 * - hash associated with the ciphersuite (only used by TLS 1.2)
6260 *
6261 * Outputs:
6262 * - the tls_prf, calc_verify and calc_finished members of handshake structure
6263 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006264MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006265static int ssl_set_handshake_prfs(mbedtls_ssl_handshake_params *handshake,
6266 mbedtls_md_type_t hash)
Jerry Yuf009d862022-02-17 14:01:37 +08006267{
Andrzej Kurek25f27152022-08-17 16:09:31 -04006268#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01006269 if (hash == MBEDTLS_MD_SHA384) {
Jerry Yuf009d862022-02-17 14:01:37 +08006270 handshake->tls_prf = tls_prf_sha384;
6271 handshake->calc_verify = ssl_calc_verify_tls_sha384;
6272 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Gilles Peskine449bd832023-01-11 14:50:10 +01006273 } else
Jerry Yuf009d862022-02-17 14:01:37 +08006274#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04006275#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuf009d862022-02-17 14:01:37 +08006276 {
Ronald Cron81591aa2022-03-07 09:05:51 +01006277 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08006278 handshake->tls_prf = tls_prf_sha256;
6279 handshake->calc_verify = ssl_calc_verify_tls_sha256;
6280 handshake->calc_finished = ssl_calc_finished_tls_sha256;
6281 }
Ronald Cron81591aa2022-03-07 09:05:51 +01006282#else
Jerry Yuf009d862022-02-17 14:01:37 +08006283 {
Jerry Yuf009d862022-02-17 14:01:37 +08006284 (void) handshake;
Ronald Cron81591aa2022-03-07 09:05:51 +01006285 (void) hash;
Gilles Peskine449bd832023-01-11 14:50:10 +01006286 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yuf009d862022-02-17 14:01:37 +08006287 }
Ronald Cron81591aa2022-03-07 09:05:51 +01006288#endif
Jerry Yuf009d862022-02-17 14:01:37 +08006289
Gilles Peskine449bd832023-01-11 14:50:10 +01006290 return 0;
Jerry Yuf009d862022-02-17 14:01:37 +08006291}
Jerry Yud6ab2352022-02-17 14:03:43 +08006292
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006293/*
6294 * Compute master secret if needed
6295 *
6296 * Parameters:
6297 * [in/out] handshake
6298 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
6299 * (PSA-PSK) ciphersuite_info, psk_opaque
6300 * [out] premaster (cleared)
6301 * [out] master
6302 * [in] ssl: optionally used for debugging, EMS and PSA-PSK
6303 * debug: conf->f_dbg, conf->p_dbg
6304 * EMS: passed to calc_verify (debug + session_negotiate)
Ronald Crona25cf582022-03-07 11:10:36 +01006305 * PSA-PSA: conf
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006306 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006307MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006308static int ssl_compute_master(mbedtls_ssl_handshake_params *handshake,
6309 unsigned char *master,
6310 const mbedtls_ssl_context *ssl)
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006311{
6312 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6313
6314 /* cf. RFC 5246, Section 8.1:
6315 * "The master secret is always exactly 48 bytes in length." */
6316 size_t const master_secret_len = 48;
6317
6318#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
6319 unsigned char session_hash[48];
6320#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
6321
6322 /* The label for the KDF used for key expansion.
6323 * This is either "master secret" or "extended master secret"
6324 * depending on whether the Extended Master Secret extension
6325 * is used. */
6326 char const *lbl = "master secret";
6327
Przemek Stekielae4ed302022-04-05 17:15:55 +02006328 /* The seed for the KDF used for key expansion.
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006329 * - If the Extended Master Secret extension is not used,
6330 * this is ClientHello.Random + ServerHello.Random
6331 * (see Sect. 8.1 in RFC 5246).
6332 * - If the Extended Master Secret extension is used,
6333 * this is the transcript of the handshake so far.
6334 * (see Sect. 4 in RFC 7627). */
Przemek Stekielae4ed302022-04-05 17:15:55 +02006335 unsigned char const *seed = handshake->randbytes;
6336 size_t seed_len = 64;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006337
6338#if !defined(MBEDTLS_DEBUG_C) && \
6339 !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \
6340 !(defined(MBEDTLS_USE_PSA_CRYPTO) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01006341 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED))
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006342 ssl = NULL; /* make sure we don't use it except for those cases */
6343 (void) ssl;
6344#endif
6345
Gilles Peskine449bd832023-01-11 14:50:10 +01006346 if (handshake->resume != 0) {
6347 MBEDTLS_SSL_DEBUG_MSG(3, ("no premaster (session resumed)"));
6348 return 0;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006349 }
6350
6351#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Gilles Peskine449bd832023-01-11 14:50:10 +01006352 if (handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED) {
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006353 lbl = "extended master secret";
Przemek Stekielae4ed302022-04-05 17:15:55 +02006354 seed = session_hash;
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01006355 ret = handshake->calc_verify(ssl, session_hash, &seed_len);
6356 if (ret != 0) {
6357 MBEDTLS_SSL_DEBUG_RET(1, "calc_verify", ret);
6358 }
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006359
Gilles Peskine449bd832023-01-11 14:50:10 +01006360 MBEDTLS_SSL_DEBUG_BUF(3, "session hash for extended master secret",
6361 session_hash, seed_len);
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006362 }
Przemek Stekiel169bf0b2022-04-29 07:53:29 +02006363#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006364
Przemek Stekiel99114f32022-04-22 11:20:09 +02006365#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Przemek Stekiel8a4b7fd2022-04-28 09:22:22 +02006366 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006367 if (mbedtls_ssl_ciphersuite_uses_psk(handshake->ciphersuite_info) == 1) {
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006368 /* Perform PSK-to-MS expansion in a single step. */
6369 psa_status_t status;
6370 psa_algorithm_t alg;
6371 mbedtls_svc_key_id_t psk;
6372 psa_key_derivation_operation_t derivation =
6373 PSA_KEY_DERIVATION_OPERATION_INIT;
6374 mbedtls_md_type_t hash_alg = handshake->ciphersuite_info->mac;
6375
Gilles Peskine449bd832023-01-11 14:50:10 +01006376 MBEDTLS_SSL_DEBUG_MSG(2, ("perform PSA-based PSK-to-MS expansion"));
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006377
Gilles Peskine449bd832023-01-11 14:50:10 +01006378 psk = mbedtls_ssl_get_opaque_psk(ssl);
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006379
Gilles Peskine449bd832023-01-11 14:50:10 +01006380 if (hash_alg == MBEDTLS_MD_SHA384) {
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006381 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
Gilles Peskine449bd832023-01-11 14:50:10 +01006382 } else {
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006383 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
Gilles Peskine449bd832023-01-11 14:50:10 +01006384 }
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006385
Przemek Stekiel51a1f362022-04-13 08:57:06 +02006386 size_t other_secret_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006387 unsigned char *other_secret = NULL;
Przemek Stekielc2033402022-04-05 17:19:41 +02006388
Gilles Peskine449bd832023-01-11 14:50:10 +01006389 switch (handshake->ciphersuite_info->key_exchange) {
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006390 /* Provide other secret.
Przemek Stekiel51a1f362022-04-13 08:57:06 +02006391 * Other secret is stored in premaster, where first 2 bytes hold the
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006392 * length of the other key.
Przemek Stekielc2033402022-04-05 17:19:41 +02006393 */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006394 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Przemek Stekiel8abcee92022-04-28 09:16:28 +02006395 /* For RSA-PSK other key length is always 48 bytes. */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006396 other_secret_len = 48;
6397 other_secret = handshake->premaster + 2;
6398 break;
6399 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006400 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006401 other_secret_len = MBEDTLS_GET_UINT16_BE(handshake->premaster, 0);
6402 other_secret = handshake->premaster + 2;
6403 break;
6404 default:
6405 break;
Przemek Stekielc2033402022-04-05 17:19:41 +02006406 }
6407
Gilles Peskine449bd832023-01-11 14:50:10 +01006408 status = setup_psa_key_derivation(&derivation, psk, alg,
6409 ssl->conf->psk, ssl->conf->psk_len,
6410 seed, seed_len,
6411 (unsigned char const *) lbl,
6412 (size_t) strlen(lbl),
6413 other_secret, other_secret_len,
6414 master_secret_len);
6415 if (status != PSA_SUCCESS) {
6416 psa_key_derivation_abort(&derivation);
6417 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006418 }
6419
Gilles Peskine449bd832023-01-11 14:50:10 +01006420 status = psa_key_derivation_output_bytes(&derivation,
6421 master,
6422 master_secret_len);
6423 if (status != PSA_SUCCESS) {
6424 psa_key_derivation_abort(&derivation);
6425 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006426 }
6427
Gilles Peskine449bd832023-01-11 14:50:10 +01006428 status = psa_key_derivation_abort(&derivation);
6429 if (status != PSA_SUCCESS) {
6430 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
6431 }
6432 } else
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006433#endif
6434 {
Neil Armstrongca7d5062022-05-31 14:43:23 +02006435#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01006436 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
6437 if (handshake->ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE) {
Neil Armstrongca7d5062022-05-31 14:43:23 +02006438 psa_status_t status;
6439 psa_algorithm_t alg = PSA_ALG_TLS12_ECJPAKE_TO_PMS;
6440 psa_key_derivation_operation_t derivation =
6441 PSA_KEY_DERIVATION_OPERATION_INIT;
6442
Gilles Peskine449bd832023-01-11 14:50:10 +01006443 MBEDTLS_SSL_DEBUG_MSG(2, ("perform PSA-based PMS KDF for ECJPAKE"));
Neil Armstrongca7d5062022-05-31 14:43:23 +02006444
6445 handshake->pmslen = PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE;
6446
Gilles Peskine449bd832023-01-11 14:50:10 +01006447 status = psa_key_derivation_setup(&derivation, alg);
6448 if (status != PSA_SUCCESS) {
6449 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Neil Armstrongca7d5062022-05-31 14:43:23 +02006450 }
6451
Gilles Peskine449bd832023-01-11 14:50:10 +01006452 status = psa_key_derivation_set_capacity(&derivation,
6453 PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE);
6454 if (status != PSA_SUCCESS) {
6455 psa_key_derivation_abort(&derivation);
6456 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Neil Armstrongca7d5062022-05-31 14:43:23 +02006457 }
6458
Gilles Peskine449bd832023-01-11 14:50:10 +01006459 status = psa_pake_get_implicit_key(&handshake->psa_pake_ctx,
6460 &derivation);
6461 if (status != PSA_SUCCESS) {
6462 psa_key_derivation_abort(&derivation);
6463 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Neil Armstrongca7d5062022-05-31 14:43:23 +02006464 }
6465
Gilles Peskine449bd832023-01-11 14:50:10 +01006466 status = psa_key_derivation_output_bytes(&derivation,
6467 handshake->premaster,
6468 handshake->pmslen);
6469 if (status != PSA_SUCCESS) {
6470 psa_key_derivation_abort(&derivation);
6471 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
6472 }
6473
6474 status = psa_key_derivation_abort(&derivation);
6475 if (status != PSA_SUCCESS) {
6476 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Neil Armstrongca7d5062022-05-31 14:43:23 +02006477 }
6478 }
6479#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01006480 ret = handshake->tls_prf(handshake->premaster, handshake->pmslen,
6481 lbl, seed, seed_len,
6482 master,
6483 master_secret_len);
6484 if (ret != 0) {
6485 MBEDTLS_SSL_DEBUG_RET(1, "prf", ret);
6486 return ret;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006487 }
6488
Gilles Peskine449bd832023-01-11 14:50:10 +01006489 MBEDTLS_SSL_DEBUG_BUF(3, "premaster secret",
6490 handshake->premaster,
6491 handshake->pmslen);
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006492
Gilles Peskine449bd832023-01-11 14:50:10 +01006493 mbedtls_platform_zeroize(handshake->premaster,
6494 sizeof(handshake->premaster));
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006495 }
6496
Gilles Peskine449bd832023-01-11 14:50:10 +01006497 return 0;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006498}
6499
Gilles Peskine449bd832023-01-11 14:50:10 +01006500int mbedtls_ssl_derive_keys(mbedtls_ssl_context *ssl)
Jerry Yud62f87e2022-02-17 14:09:02 +08006501{
6502 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6503 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
6504 ssl->handshake->ciphersuite_info;
6505
Gilles Peskine449bd832023-01-11 14:50:10 +01006506 MBEDTLS_SSL_DEBUG_MSG(2, ("=> derive keys"));
Jerry Yud62f87e2022-02-17 14:09:02 +08006507
6508 /* Set PRF, calc_verify and calc_finished function pointers */
Gilles Peskine449bd832023-01-11 14:50:10 +01006509 ret = ssl_set_handshake_prfs(ssl->handshake,
6510 ciphersuite_info->mac);
6511 if (ret != 0) {
6512 MBEDTLS_SSL_DEBUG_RET(1, "ssl_set_handshake_prfs", ret);
6513 return ret;
Jerry Yud62f87e2022-02-17 14:09:02 +08006514 }
6515
6516 /* Compute master secret if needed */
Gilles Peskine449bd832023-01-11 14:50:10 +01006517 ret = ssl_compute_master(ssl->handshake,
6518 ssl->session_negotiate->master,
6519 ssl);
6520 if (ret != 0) {
6521 MBEDTLS_SSL_DEBUG_RET(1, "ssl_compute_master", ret);
6522 return ret;
Jerry Yud62f87e2022-02-17 14:09:02 +08006523 }
6524
6525 /* Swap the client and server random values:
6526 * - MS derivation wanted client+server (RFC 5246 8.1)
6527 * - key derivation wants server+client (RFC 5246 6.3) */
6528 {
6529 unsigned char tmp[64];
Gilles Peskine449bd832023-01-11 14:50:10 +01006530 memcpy(tmp, ssl->handshake->randbytes, 64);
6531 memcpy(ssl->handshake->randbytes, tmp + 32, 32);
6532 memcpy(ssl->handshake->randbytes + 32, tmp, 32);
6533 mbedtls_platform_zeroize(tmp, sizeof(tmp));
Jerry Yud62f87e2022-02-17 14:09:02 +08006534 }
6535
6536 /* Populate transform structure */
Gilles Peskine449bd832023-01-11 14:50:10 +01006537 ret = ssl_tls12_populate_transform(ssl->transform_negotiate,
6538 ssl->session_negotiate->ciphersuite,
6539 ssl->session_negotiate->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02006540#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01006541 ssl->session_negotiate->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02006542#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01006543 ssl->handshake->tls_prf,
6544 ssl->handshake->randbytes,
6545 ssl->tls_version,
6546 ssl->conf->endpoint,
6547 ssl);
6548 if (ret != 0) {
6549 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls12_populate_transform", ret);
6550 return ret;
Jerry Yud62f87e2022-02-17 14:09:02 +08006551 }
6552
6553 /* We no longer need Server/ClientHello.random values */
Gilles Peskine449bd832023-01-11 14:50:10 +01006554 mbedtls_platform_zeroize(ssl->handshake->randbytes,
6555 sizeof(ssl->handshake->randbytes));
Jerry Yud62f87e2022-02-17 14:09:02 +08006556
Gilles Peskine449bd832023-01-11 14:50:10 +01006557 MBEDTLS_SSL_DEBUG_MSG(2, ("<= derive keys"));
Jerry Yud62f87e2022-02-17 14:09:02 +08006558
Gilles Peskine449bd832023-01-11 14:50:10 +01006559 return 0;
Jerry Yud62f87e2022-02-17 14:09:02 +08006560}
Jerry Yu8392e0d2022-02-17 14:10:24 +08006561
Gilles Peskine449bd832023-01-11 14:50:10 +01006562int mbedtls_ssl_set_calc_verify_md(mbedtls_ssl_context *ssl, int md)
Ronald Cron4dcbca92022-03-07 10:21:40 +01006563{
Gilles Peskine449bd832023-01-11 14:50:10 +01006564 switch (md) {
Andrzej Kurek25f27152022-08-17 16:09:31 -04006565#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron4dcbca92022-03-07 10:21:40 +01006566 case MBEDTLS_SSL_HASH_SHA384:
6567 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
6568 break;
6569#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04006570#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron4dcbca92022-03-07 10:21:40 +01006571 case MBEDTLS_SSL_HASH_SHA256:
6572 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
6573 break;
6574#endif
6575 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006576 return -1;
Ronald Cron4dcbca92022-03-07 10:21:40 +01006577 }
Andrzej Kurekeabeb302022-10-17 07:52:51 -04006578#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
6579 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
6580 (void) ssl;
6581#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01006582 return 0;
Ronald Cron4dcbca92022-03-07 10:21:40 +01006583}
6584
Andrzej Kurek25f27152022-08-17 16:09:31 -04006585#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01006586int ssl_calc_verify_tls_sha256(const mbedtls_ssl_context *ssl,
6587 unsigned char *hash,
6588 size_t *hlen)
Jerry Yu8392e0d2022-02-17 14:10:24 +08006589{
6590#if defined(MBEDTLS_USE_PSA_CRYPTO)
6591 size_t hash_size;
6592 psa_status_t status;
6593 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
6594
Gilles Peskine449bd832023-01-11 14:50:10 +01006595 MBEDTLS_SSL_DEBUG_MSG(2, ("=> PSA calc verify sha256"));
6596 status = psa_hash_clone(&ssl->handshake->fin_sha256_psa, &sha256_psa);
6597 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006598 goto exit;
Jerry Yu8392e0d2022-02-17 14:10:24 +08006599 }
6600
Gilles Peskine449bd832023-01-11 14:50:10 +01006601 status = psa_hash_finish(&sha256_psa, hash, 32, &hash_size);
6602 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006603 goto exit;
Jerry Yu8392e0d2022-02-17 14:10:24 +08006604 }
6605
6606 *hlen = 32;
Gilles Peskine449bd832023-01-11 14:50:10 +01006607 MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated verify result", hash, *hlen);
6608 MBEDTLS_SSL_DEBUG_MSG(2, ("<= PSA calc verify"));
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006609
6610exit:
6611 psa_hash_abort(&sha256_psa);
Andrzej Kurekdaf5b562023-03-03 05:52:28 -05006612 return PSA_TO_MD_ERR(status);
Jerry Yu8392e0d2022-02-17 14:10:24 +08006613#else
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006614 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu8392e0d2022-02-17 14:10:24 +08006615 mbedtls_sha256_context sha256;
6616
Gilles Peskine449bd832023-01-11 14:50:10 +01006617 mbedtls_sha256_init(&sha256);
Jerry Yu8392e0d2022-02-17 14:10:24 +08006618
Gilles Peskine449bd832023-01-11 14:50:10 +01006619 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc verify sha256"));
Jerry Yu8392e0d2022-02-17 14:10:24 +08006620
Gilles Peskine449bd832023-01-11 14:50:10 +01006621 mbedtls_sha256_clone(&sha256, &ssl->handshake->fin_sha256);
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006622
6623 ret = mbedtls_sha256_finish(&sha256, hash);
6624 if (ret != 0) {
6625 goto exit;
6626 }
Jerry Yu8392e0d2022-02-17 14:10:24 +08006627
6628 *hlen = 32;
6629
Gilles Peskine449bd832023-01-11 14:50:10 +01006630 MBEDTLS_SSL_DEBUG_BUF(3, "calculated verify result", hash, *hlen);
6631 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc verify"));
Jerry Yu8392e0d2022-02-17 14:10:24 +08006632
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006633exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006634 mbedtls_sha256_free(&sha256);
Manuel Pégourié-Gonnard8e176f72023-02-09 10:33:54 +01006635 return ret;
Jerry Yu8392e0d2022-02-17 14:10:24 +08006636#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu8392e0d2022-02-17 14:10:24 +08006637}
Andrzej Kurek25f27152022-08-17 16:09:31 -04006638#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu8392e0d2022-02-17 14:10:24 +08006639
Andrzej Kurek25f27152022-08-17 16:09:31 -04006640#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01006641int ssl_calc_verify_tls_sha384(const mbedtls_ssl_context *ssl,
6642 unsigned char *hash,
6643 size_t *hlen)
Jerry Yuc1cb3842022-02-17 14:13:48 +08006644{
6645#if defined(MBEDTLS_USE_PSA_CRYPTO)
6646 size_t hash_size;
6647 psa_status_t status;
6648 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
6649
Gilles Peskine449bd832023-01-11 14:50:10 +01006650 MBEDTLS_SSL_DEBUG_MSG(2, ("=> PSA calc verify sha384"));
6651 status = psa_hash_clone(&ssl->handshake->fin_sha384_psa, &sha384_psa);
6652 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006653 goto exit;
Jerry Yuc1cb3842022-02-17 14:13:48 +08006654 }
6655
Gilles Peskine449bd832023-01-11 14:50:10 +01006656 status = psa_hash_finish(&sha384_psa, hash, 48, &hash_size);
6657 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006658 goto exit;
Jerry Yuc1cb3842022-02-17 14:13:48 +08006659 }
6660
6661 *hlen = 48;
Gilles Peskine449bd832023-01-11 14:50:10 +01006662 MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated verify result", hash, *hlen);
6663 MBEDTLS_SSL_DEBUG_MSG(2, ("<= PSA calc verify"));
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006664
6665exit:
6666 psa_hash_abort(&sha384_psa);
Andrzej Kurekdaf5b562023-03-03 05:52:28 -05006667 return PSA_TO_MD_ERR(status);
Jerry Yuc1cb3842022-02-17 14:13:48 +08006668#else
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006669 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yuc1cb3842022-02-17 14:13:48 +08006670 mbedtls_sha512_context sha512;
6671
Gilles Peskine449bd832023-01-11 14:50:10 +01006672 mbedtls_sha512_init(&sha512);
Jerry Yuc1cb3842022-02-17 14:13:48 +08006673
Gilles Peskine449bd832023-01-11 14:50:10 +01006674 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc verify sha384"));
Jerry Yuc1cb3842022-02-17 14:13:48 +08006675
Gilles Peskine449bd832023-01-11 14:50:10 +01006676 mbedtls_sha512_clone(&sha512, &ssl->handshake->fin_sha384);
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006677
6678 ret = mbedtls_sha512_finish(&sha512, hash);
6679 if (ret != 0) {
6680 goto exit;
6681 }
Jerry Yuc1cb3842022-02-17 14:13:48 +08006682
6683 *hlen = 48;
6684
Gilles Peskine449bd832023-01-11 14:50:10 +01006685 MBEDTLS_SSL_DEBUG_BUF(3, "calculated verify result", hash, *hlen);
6686 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc verify"));
Jerry Yuc1cb3842022-02-17 14:13:48 +08006687
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006688exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006689 mbedtls_sha512_free(&sha512);
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006690 return ret;
Jerry Yuc1cb3842022-02-17 14:13:48 +08006691#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yuc1cb3842022-02-17 14:13:48 +08006692}
Andrzej Kurek25f27152022-08-17 16:09:31 -04006693#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yuc1cb3842022-02-17 14:13:48 +08006694
Neil Armstrong80f6f322022-05-03 17:56:38 +02006695#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
6696 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006697int mbedtls_ssl_psk_derive_premaster(mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex)
Jerry Yuce3dca42022-02-17 14:16:37 +08006698{
6699 unsigned char *p = ssl->handshake->premaster;
Gilles Peskine449bd832023-01-11 14:50:10 +01006700 unsigned char *end = p + sizeof(ssl->handshake->premaster);
Jerry Yuce3dca42022-02-17 14:16:37 +08006701 const unsigned char *psk = NULL;
6702 size_t psk_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006703 int psk_ret = mbedtls_ssl_get_psk(ssl, &psk, &psk_len);
Jerry Yuce3dca42022-02-17 14:16:37 +08006704
Gilles Peskine449bd832023-01-11 14:50:10 +01006705 if (psk_ret == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED) {
Jerry Yuce3dca42022-02-17 14:16:37 +08006706 /*
6707 * This should never happen because the existence of a PSK is always
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006708 * checked before calling this function.
6709 *
6710 * The exception is opaque DHE-PSK. For DHE-PSK fill premaster with
Przemek Stekiel8abcee92022-04-28 09:16:28 +02006711 * the shared secret without PSK.
Jerry Yuce3dca42022-02-17 14:16:37 +08006712 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006713 if (key_ex != MBEDTLS_KEY_EXCHANGE_DHE_PSK) {
6714 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
6715 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006716 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006717 }
6718
6719 /*
6720 * PMS = struct {
6721 * opaque other_secret<0..2^16-1>;
6722 * opaque psk<0..2^16-1>;
6723 * };
6724 * with "other_secret" depending on the particular key exchange
6725 */
6726#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006727 if (key_ex == MBEDTLS_KEY_EXCHANGE_PSK) {
6728 if (end - p < 2) {
6729 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6730 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006731
Gilles Peskine449bd832023-01-11 14:50:10 +01006732 MBEDTLS_PUT_UINT16_BE(psk_len, p, 0);
Jerry Yuce3dca42022-02-17 14:16:37 +08006733 p += 2;
6734
Gilles Peskine449bd832023-01-11 14:50:10 +01006735 if (end < p || (size_t) (end - p) < psk_len) {
6736 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6737 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006738
Gilles Peskine449bd832023-01-11 14:50:10 +01006739 memset(p, 0, psk_len);
Jerry Yuce3dca42022-02-17 14:16:37 +08006740 p += psk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01006741 } else
Jerry Yuce3dca42022-02-17 14:16:37 +08006742#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
6743#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006744 if (key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK) {
Jerry Yuce3dca42022-02-17 14:16:37 +08006745 /*
6746 * other_secret already set by the ClientKeyExchange message,
6747 * and is 48 bytes long
6748 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006749 if (end - p < 2) {
6750 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6751 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006752
6753 *p++ = 0;
6754 *p++ = 48;
6755 p += 48;
Gilles Peskine449bd832023-01-11 14:50:10 +01006756 } else
Jerry Yuce3dca42022-02-17 14:16:37 +08006757#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
6758#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006759 if (key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK) {
Jerry Yuce3dca42022-02-17 14:16:37 +08006760 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6761 size_t len;
6762
6763 /* Write length only when we know the actual value */
Gilles Peskine449bd832023-01-11 14:50:10 +01006764 if ((ret = mbedtls_dhm_calc_secret(&ssl->handshake->dhm_ctx,
6765 p + 2, end - (p + 2), &len,
6766 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
6767 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_calc_secret", ret);
6768 return ret;
Jerry Yuce3dca42022-02-17 14:16:37 +08006769 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006770 MBEDTLS_PUT_UINT16_BE(len, p, 0);
Jerry Yuce3dca42022-02-17 14:16:37 +08006771 p += 2 + len;
6772
Gilles Peskine449bd832023-01-11 14:50:10 +01006773 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: K ", &ssl->handshake->dhm_ctx.K);
6774 } else
Jerry Yuce3dca42022-02-17 14:16:37 +08006775#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Neil Armstrong80f6f322022-05-03 17:56:38 +02006776#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006777 if (key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK) {
Jerry Yuce3dca42022-02-17 14:16:37 +08006778 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6779 size_t zlen;
6780
Gilles Peskine449bd832023-01-11 14:50:10 +01006781 if ((ret = mbedtls_ecdh_calc_secret(&ssl->handshake->ecdh_ctx, &zlen,
6782 p + 2, end - (p + 2),
6783 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
6784 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecdh_calc_secret", ret);
6785 return ret;
Jerry Yuce3dca42022-02-17 14:16:37 +08006786 }
6787
Gilles Peskine449bd832023-01-11 14:50:10 +01006788 MBEDTLS_PUT_UINT16_BE(zlen, p, 0);
Jerry Yuce3dca42022-02-17 14:16:37 +08006789 p += 2 + zlen;
6790
Gilles Peskine449bd832023-01-11 14:50:10 +01006791 MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx,
6792 MBEDTLS_DEBUG_ECDH_Z);
6793 } else
Neil Armstrong80f6f322022-05-03 17:56:38 +02006794#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Jerry Yuce3dca42022-02-17 14:16:37 +08006795 {
Gilles Peskine449bd832023-01-11 14:50:10 +01006796 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
6797 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yuce3dca42022-02-17 14:16:37 +08006798 }
6799
6800 /* opaque psk<0..2^16-1>; */
Gilles Peskine449bd832023-01-11 14:50:10 +01006801 if (end - p < 2) {
6802 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6803 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006804
Gilles Peskine449bd832023-01-11 14:50:10 +01006805 MBEDTLS_PUT_UINT16_BE(psk_len, p, 0);
Jerry Yuce3dca42022-02-17 14:16:37 +08006806 p += 2;
6807
Gilles Peskine449bd832023-01-11 14:50:10 +01006808 if (end < p || (size_t) (end - p) < psk_len) {
6809 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6810 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006811
Gilles Peskine449bd832023-01-11 14:50:10 +01006812 memcpy(p, psk, psk_len);
Jerry Yuce3dca42022-02-17 14:16:37 +08006813 p += psk_len;
6814
6815 ssl->handshake->pmslen = p - ssl->handshake->premaster;
6816
Gilles Peskine449bd832023-01-11 14:50:10 +01006817 return 0;
Jerry Yuce3dca42022-02-17 14:16:37 +08006818}
Neil Armstrong80f6f322022-05-03 17:56:38 +02006819#endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Jerry Yuc2c673d2022-02-17 14:20:39 +08006820
6821#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006822MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006823static int ssl_write_hello_request(mbedtls_ssl_context *ssl);
Jerry Yuc2c673d2022-02-17 14:20:39 +08006824
6825#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006826int mbedtls_ssl_resend_hello_request(mbedtls_ssl_context *ssl)
Jerry Yuc2c673d2022-02-17 14:20:39 +08006827{
6828 /* If renegotiation is not enforced, retransmit until we would reach max
6829 * timeout if we were using the usual handshake doubling scheme */
Gilles Peskine449bd832023-01-11 14:50:10 +01006830 if (ssl->conf->renego_max_records < 0) {
Jerry Yuc2c673d2022-02-17 14:20:39 +08006831 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
6832 unsigned char doublings = 1;
6833
Gilles Peskine449bd832023-01-11 14:50:10 +01006834 while (ratio != 0) {
Jerry Yuc2c673d2022-02-17 14:20:39 +08006835 ++doublings;
6836 ratio >>= 1;
6837 }
6838
Gilles Peskine449bd832023-01-11 14:50:10 +01006839 if (++ssl->renego_records_seen > doublings) {
6840 MBEDTLS_SSL_DEBUG_MSG(2, ("no longer retransmitting hello request"));
6841 return 0;
Jerry Yuc2c673d2022-02-17 14:20:39 +08006842 }
6843 }
6844
Gilles Peskine449bd832023-01-11 14:50:10 +01006845 return ssl_write_hello_request(ssl);
Jerry Yuc2c673d2022-02-17 14:20:39 +08006846}
6847#endif
6848#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Jerry Yud9526692022-02-17 14:23:47 +08006849
Jerry Yud9526692022-02-17 14:23:47 +08006850/*
6851 * Handshake functions
6852 */
6853#if !defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
6854/* No certificate support -> dummy functions */
Gilles Peskine449bd832023-01-11 14:50:10 +01006855int mbedtls_ssl_write_certificate(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08006856{
6857 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6858 ssl->handshake->ciphersuite_info;
6859
Gilles Peskine449bd832023-01-11 14:50:10 +01006860 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006861
Gilles Peskine449bd832023-01-11 14:50:10 +01006862 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
6863 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006864 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01006865 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08006866 }
6867
Gilles Peskine449bd832023-01-11 14:50:10 +01006868 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
6869 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08006870}
6871
Gilles Peskine449bd832023-01-11 14:50:10 +01006872int mbedtls_ssl_parse_certificate(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08006873{
6874 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6875 ssl->handshake->ciphersuite_info;
6876
Gilles Peskine449bd832023-01-11 14:50:10 +01006877 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006878
Gilles Peskine449bd832023-01-11 14:50:10 +01006879 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
6880 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006881 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01006882 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08006883 }
6884
Gilles Peskine449bd832023-01-11 14:50:10 +01006885 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
6886 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08006887}
6888
6889#else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
6890/* Some certificate support -> implement write and parse */
6891
Gilles Peskine449bd832023-01-11 14:50:10 +01006892int mbedtls_ssl_write_certificate(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08006893{
6894 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
6895 size_t i, n;
6896 const mbedtls_x509_crt *crt;
6897 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6898 ssl->handshake->ciphersuite_info;
6899
Gilles Peskine449bd832023-01-11 14:50:10 +01006900 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006901
Gilles Peskine449bd832023-01-11 14:50:10 +01006902 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
6903 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006904 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01006905 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08006906 }
6907
6908#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01006909 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
6910 if (ssl->handshake->client_auth == 0) {
6911 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006912 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01006913 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08006914 }
6915 }
6916#endif /* MBEDTLS_SSL_CLI_C */
6917#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01006918 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
6919 if (mbedtls_ssl_own_cert(ssl) == NULL) {
Jerry Yud9526692022-02-17 14:23:47 +08006920 /* Should never happen because we shouldn't have picked the
6921 * ciphersuite if we don't have a certificate. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006922 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08006923 }
6924 }
6925#endif
6926
Gilles Peskine449bd832023-01-11 14:50:10 +01006927 MBEDTLS_SSL_DEBUG_CRT(3, "own certificate", mbedtls_ssl_own_cert(ssl));
Jerry Yud9526692022-02-17 14:23:47 +08006928
6929 /*
6930 * 0 . 0 handshake type
6931 * 1 . 3 handshake length
6932 * 4 . 6 length of all certs
6933 * 7 . 9 length of cert. 1
6934 * 10 . n-1 peer certificate
6935 * n . n+2 length of cert. 2
6936 * n+3 . ... upper level cert, etc.
6937 */
6938 i = 7;
Gilles Peskine449bd832023-01-11 14:50:10 +01006939 crt = mbedtls_ssl_own_cert(ssl);
Jerry Yud9526692022-02-17 14:23:47 +08006940
Gilles Peskine449bd832023-01-11 14:50:10 +01006941 while (crt != NULL) {
Jerry Yud9526692022-02-17 14:23:47 +08006942 n = crt->raw.len;
Gilles Peskine449bd832023-01-11 14:50:10 +01006943 if (n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i) {
6944 MBEDTLS_SSL_DEBUG_MSG(1, ("certificate too large, %" MBEDTLS_PRINTF_SIZET
6945 " > %" MBEDTLS_PRINTF_SIZET,
6946 i + 3 + n, (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN));
6947 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
Jerry Yud9526692022-02-17 14:23:47 +08006948 }
6949
Gilles Peskine449bd832023-01-11 14:50:10 +01006950 ssl->out_msg[i] = MBEDTLS_BYTE_2(n);
6951 ssl->out_msg[i + 1] = MBEDTLS_BYTE_1(n);
6952 ssl->out_msg[i + 2] = MBEDTLS_BYTE_0(n);
Jerry Yud9526692022-02-17 14:23:47 +08006953
Gilles Peskine449bd832023-01-11 14:50:10 +01006954 i += 3; memcpy(ssl->out_msg + i, crt->raw.p, n);
Jerry Yud9526692022-02-17 14:23:47 +08006955 i += n; crt = crt->next;
6956 }
6957
Gilles Peskine449bd832023-01-11 14:50:10 +01006958 ssl->out_msg[4] = MBEDTLS_BYTE_2(i - 7);
6959 ssl->out_msg[5] = MBEDTLS_BYTE_1(i - 7);
6960 ssl->out_msg[6] = MBEDTLS_BYTE_0(i - 7);
Jerry Yud9526692022-02-17 14:23:47 +08006961
6962 ssl->out_msglen = i;
6963 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6964 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
6965
6966 ssl->state++;
6967
Gilles Peskine449bd832023-01-11 14:50:10 +01006968 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
6969 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
6970 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08006971 }
6972
Gilles Peskine449bd832023-01-11 14:50:10 +01006973 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006974
Gilles Peskine449bd832023-01-11 14:50:10 +01006975 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08006976}
6977
6978#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6979
6980#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006981MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006982static int ssl_check_peer_crt_unchanged(mbedtls_ssl_context *ssl,
6983 unsigned char *crt_buf,
6984 size_t crt_buf_len)
Jerry Yud9526692022-02-17 14:23:47 +08006985{
6986 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6987
Gilles Peskine449bd832023-01-11 14:50:10 +01006988 if (peer_crt == NULL) {
6989 return -1;
6990 }
Jerry Yud9526692022-02-17 14:23:47 +08006991
Gilles Peskine449bd832023-01-11 14:50:10 +01006992 if (peer_crt->raw.len != crt_buf_len) {
6993 return -1;
6994 }
Jerry Yud9526692022-02-17 14:23:47 +08006995
Gilles Peskine449bd832023-01-11 14:50:10 +01006996 return memcmp(peer_crt->raw.p, crt_buf, peer_crt->raw.len);
Jerry Yud9526692022-02-17 14:23:47 +08006997}
6998#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006999MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007000static int ssl_check_peer_crt_unchanged(mbedtls_ssl_context *ssl,
7001 unsigned char *crt_buf,
7002 size_t crt_buf_len)
Jerry Yud9526692022-02-17 14:23:47 +08007003{
7004 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7005 unsigned char const * const peer_cert_digest =
7006 ssl->session->peer_cert_digest;
7007 mbedtls_md_type_t const peer_cert_digest_type =
7008 ssl->session->peer_cert_digest_type;
7009 mbedtls_md_info_t const * const digest_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01007010 mbedtls_md_info_from_type(peer_cert_digest_type);
Jerry Yud9526692022-02-17 14:23:47 +08007011 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
7012 size_t digest_len;
7013
Gilles Peskine449bd832023-01-11 14:50:10 +01007014 if (peer_cert_digest == NULL || digest_info == NULL) {
7015 return -1;
7016 }
Jerry Yud9526692022-02-17 14:23:47 +08007017
Gilles Peskine449bd832023-01-11 14:50:10 +01007018 digest_len = mbedtls_md_get_size(digest_info);
7019 if (digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN) {
7020 return -1;
7021 }
Jerry Yud9526692022-02-17 14:23:47 +08007022
Gilles Peskine449bd832023-01-11 14:50:10 +01007023 ret = mbedtls_md(digest_info, crt_buf, crt_buf_len, tmp_digest);
7024 if (ret != 0) {
7025 return -1;
7026 }
Jerry Yud9526692022-02-17 14:23:47 +08007027
Gilles Peskine449bd832023-01-11 14:50:10 +01007028 return memcmp(tmp_digest, peer_cert_digest, digest_len);
Jerry Yud9526692022-02-17 14:23:47 +08007029}
7030#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7031#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
7032
7033/*
7034 * Once the certificate message is read, parse it into a cert chain and
7035 * perform basic checks, but leave actual verification to the caller
7036 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007037MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007038static int ssl_parse_certificate_chain(mbedtls_ssl_context *ssl,
7039 mbedtls_x509_crt *chain)
Jerry Yud9526692022-02-17 14:23:47 +08007040{
7041 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7042#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007043 int crt_cnt = 0;
Jerry Yud9526692022-02-17 14:23:47 +08007044#endif
7045 size_t i, n;
7046 uint8_t alert;
7047
Gilles Peskine449bd832023-01-11 14:50:10 +01007048 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
7049 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7050 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7051 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
7052 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Jerry Yud9526692022-02-17 14:23:47 +08007053 }
7054
Gilles Peskine449bd832023-01-11 14:50:10 +01007055 if (ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE) {
7056 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7057 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
7058 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Jerry Yud9526692022-02-17 14:23:47 +08007059 }
7060
Gilles Peskine449bd832023-01-11 14:50:10 +01007061 if (ssl->in_hslen < mbedtls_ssl_hs_hdr_len(ssl) + 3 + 3) {
7062 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7063 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7064 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
7065 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007066 }
7067
Gilles Peskine449bd832023-01-11 14:50:10 +01007068 i = mbedtls_ssl_hs_hdr_len(ssl);
Jerry Yud9526692022-02-17 14:23:47 +08007069
7070 /*
7071 * Same message structure as in mbedtls_ssl_write_certificate()
7072 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007073 n = (ssl->in_msg[i+1] << 8) | ssl->in_msg[i+2];
Jerry Yud9526692022-02-17 14:23:47 +08007074
Gilles Peskine449bd832023-01-11 14:50:10 +01007075 if (ssl->in_msg[i] != 0 ||
7076 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len(ssl)) {
7077 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7078 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7079 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
7080 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007081 }
7082
7083 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
7084 i += 3;
7085
7086 /* Iterate through and parse the CRTs in the provided chain. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007087 while (i < ssl->in_hslen) {
Jerry Yud9526692022-02-17 14:23:47 +08007088 /* Check that there's room for the next CRT's length fields. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007089 if (i + 3 > ssl->in_hslen) {
7090 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7091 mbedtls_ssl_send_alert_message(ssl,
7092 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7093 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
7094 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007095 }
7096 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
7097 * anything beyond 2**16 ~ 64K. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007098 if (ssl->in_msg[i] != 0) {
7099 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7100 mbedtls_ssl_send_alert_message(ssl,
7101 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7102 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT);
7103 return MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Jerry Yud9526692022-02-17 14:23:47 +08007104 }
7105
7106 /* Read length of the next CRT in the chain. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007107 n = ((unsigned int) ssl->in_msg[i + 1] << 8)
Jerry Yud9526692022-02-17 14:23:47 +08007108 | (unsigned int) ssl->in_msg[i + 2];
7109 i += 3;
7110
Gilles Peskine449bd832023-01-11 14:50:10 +01007111 if (n < 128 || i + n > ssl->in_hslen) {
7112 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7113 mbedtls_ssl_send_alert_message(ssl,
7114 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7115 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
7116 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007117 }
7118
7119 /* Check if we're handling the first CRT in the chain. */
7120#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007121 if (crt_cnt++ == 0 &&
Jerry Yud9526692022-02-17 14:23:47 +08007122 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Gilles Peskine449bd832023-01-11 14:50:10 +01007123 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
Jerry Yud9526692022-02-17 14:23:47 +08007124 /* During client-side renegotiation, check that the server's
7125 * end-CRTs hasn't changed compared to the initial handshake,
7126 * mitigating the triple handshake attack. On success, reuse
7127 * the original end-CRT instead of parsing it again. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007128 MBEDTLS_SSL_DEBUG_MSG(3, ("Check that peer CRT hasn't changed during renegotiation"));
7129 if (ssl_check_peer_crt_unchanged(ssl,
7130 &ssl->in_msg[i],
7131 n) != 0) {
7132 MBEDTLS_SSL_DEBUG_MSG(1, ("new server cert during renegotiation"));
7133 mbedtls_ssl_send_alert_message(ssl,
7134 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7135 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED);
7136 return MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Jerry Yud9526692022-02-17 14:23:47 +08007137 }
7138
7139 /* Now we can safely free the original chain. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007140 ssl_clear_peer_cert(ssl->session);
Jerry Yud9526692022-02-17 14:23:47 +08007141 }
7142#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
7143
7144 /* Parse the next certificate in the chain. */
7145#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007146 ret = mbedtls_x509_crt_parse_der(chain, ssl->in_msg + i, n);
Jerry Yud9526692022-02-17 14:23:47 +08007147#else
7148 /* If we don't need to store the CRT chain permanently, parse
7149 * it in-place from the input buffer instead of making a copy. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007150 ret = mbedtls_x509_crt_parse_der_nocopy(chain, ssl->in_msg + i, n);
Jerry Yud9526692022-02-17 14:23:47 +08007151#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007152 switch (ret) {
Jerry Yud9526692022-02-17 14:23:47 +08007153 case 0: /*ok*/
7154 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
7155 /* Ignore certificate with an unknown algorithm: maybe a
7156 prior certificate was already trusted. */
7157 break;
7158
7159 case MBEDTLS_ERR_X509_ALLOC_FAILED:
7160 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
7161 goto crt_parse_der_failed;
7162
7163 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
7164 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7165 goto crt_parse_der_failed;
7166
7167 default:
7168 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007169crt_parse_der_failed:
7170 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert);
7171 MBEDTLS_SSL_DEBUG_RET(1, " mbedtls_x509_crt_parse_der", ret);
7172 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007173 }
7174
7175 i += n;
7176 }
7177
Gilles Peskine449bd832023-01-11 14:50:10 +01007178 MBEDTLS_SSL_DEBUG_CRT(3, "peer certificate", chain);
7179 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08007180}
7181
7182#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007183MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007184static int ssl_srv_check_client_no_crt_notification(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08007185{
Gilles Peskine449bd832023-01-11 14:50:10 +01007186 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
7187 return -1;
7188 }
Jerry Yud9526692022-02-17 14:23:47 +08007189
Gilles Peskine449bd832023-01-11 14:50:10 +01007190 if (ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len(ssl) &&
Jerry Yud9526692022-02-17 14:23:47 +08007191 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
7192 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
Gilles Peskine449bd832023-01-11 14:50:10 +01007193 memcmp(ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl), "\0\0\0", 3) == 0) {
7194 MBEDTLS_SSL_DEBUG_MSG(1, ("peer has no certificate"));
7195 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08007196 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007197 return -1;
Jerry Yud9526692022-02-17 14:23:47 +08007198}
7199#endif /* MBEDTLS_SSL_SRV_C */
7200
7201/* Check if a certificate message is expected.
7202 * Return either
7203 * - SSL_CERTIFICATE_EXPECTED, or
7204 * - SSL_CERTIFICATE_SKIP
7205 * indicating whether a Certificate message is expected or not.
7206 */
7207#define SSL_CERTIFICATE_EXPECTED 0
7208#define SSL_CERTIFICATE_SKIP 1
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007209MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007210static int ssl_parse_certificate_coordinate(mbedtls_ssl_context *ssl,
7211 int authmode)
Jerry Yud9526692022-02-17 14:23:47 +08007212{
7213 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
7214 ssl->handshake->ciphersuite_info;
7215
Gilles Peskine449bd832023-01-11 14:50:10 +01007216 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
7217 return SSL_CERTIFICATE_SKIP;
7218 }
Jerry Yud9526692022-02-17 14:23:47 +08007219
7220#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007221 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
7222 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK) {
7223 return SSL_CERTIFICATE_SKIP;
7224 }
Jerry Yud9526692022-02-17 14:23:47 +08007225
Gilles Peskine449bd832023-01-11 14:50:10 +01007226 if (authmode == MBEDTLS_SSL_VERIFY_NONE) {
Jerry Yud9526692022-02-17 14:23:47 +08007227 ssl->session_negotiate->verify_result =
7228 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
Gilles Peskine449bd832023-01-11 14:50:10 +01007229 return SSL_CERTIFICATE_SKIP;
Jerry Yud9526692022-02-17 14:23:47 +08007230 }
7231 }
7232#else
7233 ((void) authmode);
7234#endif /* MBEDTLS_SSL_SRV_C */
7235
Gilles Peskine449bd832023-01-11 14:50:10 +01007236 return SSL_CERTIFICATE_EXPECTED;
Jerry Yud9526692022-02-17 14:23:47 +08007237}
7238
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007239MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007240static int ssl_parse_certificate_verify(mbedtls_ssl_context *ssl,
7241 int authmode,
7242 mbedtls_x509_crt *chain,
7243 void *rs_ctx)
Jerry Yud9526692022-02-17 14:23:47 +08007244{
7245 int ret = 0;
7246 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
7247 ssl->handshake->ciphersuite_info;
7248 int have_ca_chain = 0;
7249
7250 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
7251 void *p_vrfy;
7252
Gilles Peskine449bd832023-01-11 14:50:10 +01007253 if (authmode == MBEDTLS_SSL_VERIFY_NONE) {
7254 return 0;
7255 }
Jerry Yud9526692022-02-17 14:23:47 +08007256
Gilles Peskine449bd832023-01-11 14:50:10 +01007257 if (ssl->f_vrfy != NULL) {
7258 MBEDTLS_SSL_DEBUG_MSG(3, ("Use context-specific verification callback"));
Jerry Yud9526692022-02-17 14:23:47 +08007259 f_vrfy = ssl->f_vrfy;
7260 p_vrfy = ssl->p_vrfy;
Gilles Peskine449bd832023-01-11 14:50:10 +01007261 } else {
7262 MBEDTLS_SSL_DEBUG_MSG(3, ("Use configuration-specific verification callback"));
Jerry Yud9526692022-02-17 14:23:47 +08007263 f_vrfy = ssl->conf->f_vrfy;
7264 p_vrfy = ssl->conf->p_vrfy;
7265 }
7266
7267 /*
7268 * Main check: verify certificate
7269 */
7270#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Gilles Peskine449bd832023-01-11 14:50:10 +01007271 if (ssl->conf->f_ca_cb != NULL) {
Jerry Yud9526692022-02-17 14:23:47 +08007272 ((void) rs_ctx);
7273 have_ca_chain = 1;
7274
Gilles Peskine449bd832023-01-11 14:50:10 +01007275 MBEDTLS_SSL_DEBUG_MSG(3, ("use CA callback for X.509 CRT verification"));
Jerry Yud9526692022-02-17 14:23:47 +08007276 ret = mbedtls_x509_crt_verify_with_ca_cb(
7277 chain,
7278 ssl->conf->f_ca_cb,
7279 ssl->conf->p_ca_cb,
7280 ssl->conf->cert_profile,
7281 ssl->hostname,
7282 &ssl->session_negotiate->verify_result,
Gilles Peskine449bd832023-01-11 14:50:10 +01007283 f_vrfy, p_vrfy);
7284 } else
Jerry Yud9526692022-02-17 14:23:47 +08007285#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
7286 {
7287 mbedtls_x509_crt *ca_chain;
7288 mbedtls_x509_crl *ca_crl;
7289
7290#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01007291 if (ssl->handshake->sni_ca_chain != NULL) {
Jerry Yud9526692022-02-17 14:23:47 +08007292 ca_chain = ssl->handshake->sni_ca_chain;
7293 ca_crl = ssl->handshake->sni_ca_crl;
Gilles Peskine449bd832023-01-11 14:50:10 +01007294 } else
Jerry Yud9526692022-02-17 14:23:47 +08007295#endif
7296 {
7297 ca_chain = ssl->conf->ca_chain;
7298 ca_crl = ssl->conf->ca_crl;
7299 }
7300
Gilles Peskine449bd832023-01-11 14:50:10 +01007301 if (ca_chain != NULL) {
Jerry Yud9526692022-02-17 14:23:47 +08007302 have_ca_chain = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01007303 }
Jerry Yud9526692022-02-17 14:23:47 +08007304
7305 ret = mbedtls_x509_crt_verify_restartable(
7306 chain,
7307 ca_chain, ca_crl,
7308 ssl->conf->cert_profile,
7309 ssl->hostname,
7310 &ssl->session_negotiate->verify_result,
Gilles Peskine449bd832023-01-11 14:50:10 +01007311 f_vrfy, p_vrfy, rs_ctx);
Jerry Yud9526692022-02-17 14:23:47 +08007312 }
7313
Gilles Peskine449bd832023-01-11 14:50:10 +01007314 if (ret != 0) {
7315 MBEDTLS_SSL_DEBUG_RET(1, "x509_verify_cert", ret);
Jerry Yud9526692022-02-17 14:23:47 +08007316 }
7317
7318#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007319 if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
7320 return MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
7321 }
Jerry Yud9526692022-02-17 14:23:47 +08007322#endif
7323
7324 /*
7325 * Secondary checks: always done, but change 'ret' only if it was 0
7326 */
7327
7328#if defined(MBEDTLS_ECP_C)
7329 {
7330 const mbedtls_pk_context *pk = &chain->pk;
7331
Manuel Pégourié-Gonnard66b0d612022-06-17 10:49:29 +02007332 /* If certificate uses an EC key, make sure the curve is OK.
7333 * This is a public key, so it can't be opaque, so can_do() is a good
7334 * enough check to ensure pk_ec() is safe to use here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007335 if (mbedtls_pk_can_do(pk, MBEDTLS_PK_ECKEY)) {
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007336 /* and in the unlikely case the above assumption no longer holds
7337 * we are making sure that pk_ec() here does not return a NULL
7338 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007339 const mbedtls_ecp_keypair *ec = mbedtls_pk_ec(*pk);
7340 if (ec == NULL) {
7341 MBEDTLS_SSL_DEBUG_MSG(1, ("mbedtls_pk_ec() returned NULL"));
7342 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007343 }
Jerry Yud9526692022-02-17 14:23:47 +08007344
Gilles Peskine449bd832023-01-11 14:50:10 +01007345 if (mbedtls_ssl_check_curve(ssl, ec->grp.id) != 0) {
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007346 ssl->session_negotiate->verify_result |=
7347 MBEDTLS_X509_BADCERT_BAD_KEY;
7348
Gilles Peskine449bd832023-01-11 14:50:10 +01007349 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate (EC key curve)"));
7350 if (ret == 0) {
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007351 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Gilles Peskine449bd832023-01-11 14:50:10 +01007352 }
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007353 }
Jerry Yud9526692022-02-17 14:23:47 +08007354 }
7355 }
7356#endif /* MBEDTLS_ECP_C */
7357
Gilles Peskine449bd832023-01-11 14:50:10 +01007358 if (mbedtls_ssl_check_cert_usage(chain,
7359 ciphersuite_info,
7360 !ssl->conf->endpoint,
7361 &ssl->session_negotiate->verify_result) != 0) {
7362 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate (usage extensions)"));
7363 if (ret == 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007364 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Gilles Peskine449bd832023-01-11 14:50:10 +01007365 }
Jerry Yud9526692022-02-17 14:23:47 +08007366 }
7367
7368 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
7369 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
7370 * with details encoded in the verification flags. All other kinds
7371 * of error codes, including those from the user provided f_vrfy
7372 * functions, are treated as fatal and lead to a failure of
7373 * ssl_parse_certificate even if verification was optional. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007374 if (authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
7375 (ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
7376 ret == MBEDTLS_ERR_SSL_BAD_CERTIFICATE)) {
Jerry Yud9526692022-02-17 14:23:47 +08007377 ret = 0;
7378 }
7379
Gilles Peskine449bd832023-01-11 14:50:10 +01007380 if (have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED) {
7381 MBEDTLS_SSL_DEBUG_MSG(1, ("got no CA chain"));
Jerry Yud9526692022-02-17 14:23:47 +08007382 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
7383 }
7384
Gilles Peskine449bd832023-01-11 14:50:10 +01007385 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007386 uint8_t alert;
7387
7388 /* The certificate may have been rejected for several reasons.
7389 Pick one and send the corresponding alert. Which alert to send
7390 may be a subject of debate in some cases. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007391 if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER) {
Jerry Yud9526692022-02-17 14:23:47 +08007392 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
Gilles Peskine449bd832023-01-11 14:50:10 +01007393 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH) {
Jerry Yud9526692022-02-17 14:23:47 +08007394 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007395 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE) {
Jerry Yud9526692022-02-17 14:23:47 +08007396 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007397 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE) {
Jerry Yud9526692022-02-17 14:23:47 +08007398 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007399 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE) {
Jerry Yud9526692022-02-17 14:23:47 +08007400 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007401 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK) {
Jerry Yud9526692022-02-17 14:23:47 +08007402 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007403 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY) {
Jerry Yud9526692022-02-17 14:23:47 +08007404 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007405 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED) {
Jerry Yud9526692022-02-17 14:23:47 +08007406 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
Gilles Peskine449bd832023-01-11 14:50:10 +01007407 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED) {
Jerry Yud9526692022-02-17 14:23:47 +08007408 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
Gilles Peskine449bd832023-01-11 14:50:10 +01007409 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED) {
Jerry Yud9526692022-02-17 14:23:47 +08007410 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
Gilles Peskine449bd832023-01-11 14:50:10 +01007411 } else {
Jerry Yud9526692022-02-17 14:23:47 +08007412 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
Gilles Peskine449bd832023-01-11 14:50:10 +01007413 }
7414 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7415 alert);
Jerry Yud9526692022-02-17 14:23:47 +08007416 }
7417
7418#if defined(MBEDTLS_DEBUG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007419 if (ssl->session_negotiate->verify_result != 0) {
7420 MBEDTLS_SSL_DEBUG_MSG(3, ("! Certificate verification flags %08x",
7421 (unsigned int) ssl->session_negotiate->verify_result));
7422 } else {
7423 MBEDTLS_SSL_DEBUG_MSG(3, ("Certificate verification flags clear"));
Jerry Yud9526692022-02-17 14:23:47 +08007424 }
7425#endif /* MBEDTLS_DEBUG_C */
7426
Gilles Peskine449bd832023-01-11 14:50:10 +01007427 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007428}
7429
7430#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007431MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007432static int ssl_remember_peer_crt_digest(mbedtls_ssl_context *ssl,
7433 unsigned char *start, size_t len)
Jerry Yud9526692022-02-17 14:23:47 +08007434{
7435 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7436 /* Remember digest of the peer's end-CRT. */
7437 ssl->session_negotiate->peer_cert_digest =
Gilles Peskine449bd832023-01-11 14:50:10 +01007438 mbedtls_calloc(1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN);
7439 if (ssl->session_negotiate->peer_cert_digest == NULL) {
7440 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%d bytes) failed",
7441 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN));
7442 mbedtls_ssl_send_alert_message(ssl,
7443 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7444 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
Jerry Yud9526692022-02-17 14:23:47 +08007445
Gilles Peskine449bd832023-01-11 14:50:10 +01007446 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Jerry Yud9526692022-02-17 14:23:47 +08007447 }
7448
Gilles Peskine449bd832023-01-11 14:50:10 +01007449 ret = mbedtls_md(mbedtls_md_info_from_type(
7450 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE),
7451 start, len,
7452 ssl->session_negotiate->peer_cert_digest);
Jerry Yud9526692022-02-17 14:23:47 +08007453
7454 ssl->session_negotiate->peer_cert_digest_type =
7455 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
7456 ssl->session_negotiate->peer_cert_digest_len =
7457 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
7458
Gilles Peskine449bd832023-01-11 14:50:10 +01007459 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007460}
7461
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007462MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007463static int ssl_remember_peer_pubkey(mbedtls_ssl_context *ssl,
7464 unsigned char *start, size_t len)
Jerry Yud9526692022-02-17 14:23:47 +08007465{
7466 unsigned char *end = start + len;
7467 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7468
7469 /* Make a copy of the peer's raw public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007470 mbedtls_pk_init(&ssl->handshake->peer_pubkey);
7471 ret = mbedtls_pk_parse_subpubkey(&start, end,
7472 &ssl->handshake->peer_pubkey);
7473 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007474 /* We should have parsed the public key before. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007475 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007476 }
7477
Gilles Peskine449bd832023-01-11 14:50:10 +01007478 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08007479}
7480#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7481
Gilles Peskine449bd832023-01-11 14:50:10 +01007482int mbedtls_ssl_parse_certificate(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08007483{
7484 int ret = 0;
7485 int crt_expected;
7486#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7487 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
7488 ? ssl->handshake->sni_authmode
7489 : ssl->conf->authmode;
7490#else
7491 const int authmode = ssl->conf->authmode;
7492#endif
7493 void *rs_ctx = NULL;
7494 mbedtls_x509_crt *chain = NULL;
7495
Gilles Peskine449bd832023-01-11 14:50:10 +01007496 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007497
Gilles Peskine449bd832023-01-11 14:50:10 +01007498 crt_expected = ssl_parse_certificate_coordinate(ssl, authmode);
7499 if (crt_expected == SSL_CERTIFICATE_SKIP) {
7500 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007501 goto exit;
7502 }
7503
7504#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007505 if (ssl->handshake->ecrs_enabled &&
7506 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify) {
Jerry Yud9526692022-02-17 14:23:47 +08007507 chain = ssl->handshake->ecrs_peer_cert;
7508 ssl->handshake->ecrs_peer_cert = NULL;
7509 goto crt_verify;
7510 }
7511#endif
7512
Gilles Peskine449bd832023-01-11 14:50:10 +01007513 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007514 /* mbedtls_ssl_read_record may have sent an alert already. We
7515 let it decide whether to alert. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007516 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
Jerry Yud9526692022-02-17 14:23:47 +08007517 goto exit;
7518 }
7519
7520#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007521 if (ssl_srv_check_client_no_crt_notification(ssl) == 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007522 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
7523
Gilles Peskine449bd832023-01-11 14:50:10 +01007524 if (authmode != MBEDTLS_SSL_VERIFY_OPTIONAL) {
Jerry Yud9526692022-02-17 14:23:47 +08007525 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Gilles Peskine449bd832023-01-11 14:50:10 +01007526 }
Jerry Yud9526692022-02-17 14:23:47 +08007527
7528 goto exit;
7529 }
7530#endif /* MBEDTLS_SSL_SRV_C */
7531
7532 /* Clear existing peer CRT structure in case we tried to
7533 * reuse a session but it failed, and allocate a new one. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007534 ssl_clear_peer_cert(ssl->session_negotiate);
Jerry Yud9526692022-02-17 14:23:47 +08007535
Gilles Peskine449bd832023-01-11 14:50:10 +01007536 chain = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
7537 if (chain == NULL) {
7538 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed",
7539 sizeof(mbedtls_x509_crt)));
7540 mbedtls_ssl_send_alert_message(ssl,
7541 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7542 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
Jerry Yud9526692022-02-17 14:23:47 +08007543
7544 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
7545 goto exit;
7546 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007547 mbedtls_x509_crt_init(chain);
Jerry Yud9526692022-02-17 14:23:47 +08007548
Gilles Peskine449bd832023-01-11 14:50:10 +01007549 ret = ssl_parse_certificate_chain(ssl, chain);
7550 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007551 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007552 }
Jerry Yud9526692022-02-17 14:23:47 +08007553
7554#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007555 if (ssl->handshake->ecrs_enabled) {
Jerry Yud9526692022-02-17 14:23:47 +08007556 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Gilles Peskine449bd832023-01-11 14:50:10 +01007557 }
Jerry Yud9526692022-02-17 14:23:47 +08007558
7559crt_verify:
Gilles Peskine449bd832023-01-11 14:50:10 +01007560 if (ssl->handshake->ecrs_enabled) {
Jerry Yud9526692022-02-17 14:23:47 +08007561 rs_ctx = &ssl->handshake->ecrs_ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +01007562 }
Jerry Yud9526692022-02-17 14:23:47 +08007563#endif
7564
Gilles Peskine449bd832023-01-11 14:50:10 +01007565 ret = ssl_parse_certificate_verify(ssl, authmode,
7566 chain, rs_ctx);
7567 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007568 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007569 }
Jerry Yud9526692022-02-17 14:23:47 +08007570
7571#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7572 {
7573 unsigned char *crt_start, *pk_start;
7574 size_t crt_len, pk_len;
7575
7576 /* We parse the CRT chain without copying, so
7577 * these pointers point into the input buffer,
7578 * and are hence still valid after freeing the
7579 * CRT chain. */
7580
7581 crt_start = chain->raw.p;
7582 crt_len = chain->raw.len;
7583
7584 pk_start = chain->pk_raw.p;
7585 pk_len = chain->pk_raw.len;
7586
7587 /* Free the CRT structures before computing
7588 * digest and copying the peer's public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007589 mbedtls_x509_crt_free(chain);
7590 mbedtls_free(chain);
Jerry Yud9526692022-02-17 14:23:47 +08007591 chain = NULL;
7592
Gilles Peskine449bd832023-01-11 14:50:10 +01007593 ret = ssl_remember_peer_crt_digest(ssl, crt_start, crt_len);
7594 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007595 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007596 }
Jerry Yud9526692022-02-17 14:23:47 +08007597
Gilles Peskine449bd832023-01-11 14:50:10 +01007598 ret = ssl_remember_peer_pubkey(ssl, pk_start, pk_len);
7599 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007600 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007601 }
Jerry Yud9526692022-02-17 14:23:47 +08007602 }
7603#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7604 /* Pass ownership to session structure. */
7605 ssl->session_negotiate->peer_cert = chain;
7606 chain = NULL;
7607#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7608
Gilles Peskine449bd832023-01-11 14:50:10 +01007609 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007610
7611exit:
7612
Gilles Peskine449bd832023-01-11 14:50:10 +01007613 if (ret == 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007614 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01007615 }
Jerry Yud9526692022-02-17 14:23:47 +08007616
7617#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007618 if (ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS) {
Jerry Yud9526692022-02-17 14:23:47 +08007619 ssl->handshake->ecrs_peer_cert = chain;
7620 chain = NULL;
7621 }
7622#endif
7623
Gilles Peskine449bd832023-01-11 14:50:10 +01007624 if (chain != NULL) {
7625 mbedtls_x509_crt_free(chain);
7626 mbedtls_free(chain);
Jerry Yud9526692022-02-17 14:23:47 +08007627 }
7628
Gilles Peskine449bd832023-01-11 14:50:10 +01007629 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007630}
7631#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
7632
Andrzej Kurek25f27152022-08-17 16:09:31 -04007633#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01007634static int ssl_calc_finished_tls_sha256(
Gilles Peskine449bd832023-01-11 14:50:10 +01007635 mbedtls_ssl_context *ssl, unsigned char *buf, int from)
Jerry Yu615bd6f2022-02-17 14:25:15 +08007636{
7637 int len = 12;
7638 const char *sender;
7639 unsigned char padbuf[32];
7640#if defined(MBEDTLS_USE_PSA_CRYPTO)
7641 size_t hash_size;
7642 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
7643 psa_status_t status;
7644#else
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007645 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu615bd6f2022-02-17 14:25:15 +08007646 mbedtls_sha256_context sha256;
7647#endif
7648
7649 mbedtls_ssl_session *session = ssl->session_negotiate;
Gilles Peskine449bd832023-01-11 14:50:10 +01007650 if (!session) {
Jerry Yu615bd6f2022-02-17 14:25:15 +08007651 session = ssl->session;
Gilles Peskine449bd832023-01-11 14:50:10 +01007652 }
Jerry Yu615bd6f2022-02-17 14:25:15 +08007653
Gilles Peskine449bd832023-01-11 14:50:10 +01007654 sender = (from == MBEDTLS_SSL_IS_CLIENT)
Jerry Yu615bd6f2022-02-17 14:25:15 +08007655 ? "client finished"
7656 : "server finished";
7657
7658#if defined(MBEDTLS_USE_PSA_CRYPTO)
7659 sha256_psa = psa_hash_operation_init();
7660
Gilles Peskine449bd832023-01-11 14:50:10 +01007661 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc PSA finished tls sha256"));
Jerry Yu615bd6f2022-02-17 14:25:15 +08007662
Gilles Peskine449bd832023-01-11 14:50:10 +01007663 status = psa_hash_clone(&ssl->handshake->fin_sha256_psa, &sha256_psa);
7664 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007665 goto exit;
Jerry Yu615bd6f2022-02-17 14:25:15 +08007666 }
7667
Gilles Peskine449bd832023-01-11 14:50:10 +01007668 status = psa_hash_finish(&sha256_psa, padbuf, sizeof(padbuf), &hash_size);
7669 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007670 goto exit;
Jerry Yu615bd6f2022-02-17 14:25:15 +08007671 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007672 MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated padbuf", padbuf, 32);
Jerry Yu615bd6f2022-02-17 14:25:15 +08007673#else
7674
Gilles Peskine449bd832023-01-11 14:50:10 +01007675 mbedtls_sha256_init(&sha256);
Jerry Yu615bd6f2022-02-17 14:25:15 +08007676
Gilles Peskine449bd832023-01-11 14:50:10 +01007677 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc finished tls sha256"));
Jerry Yu615bd6f2022-02-17 14:25:15 +08007678
Gilles Peskine449bd832023-01-11 14:50:10 +01007679 mbedtls_sha256_clone(&sha256, &ssl->handshake->fin_sha256);
Jerry Yu615bd6f2022-02-17 14:25:15 +08007680
7681 /*
7682 * TLSv1.2:
7683 * hash = PRF( master, finished_label,
7684 * Hash( handshake ) )[0.11]
7685 */
7686
7687#if !defined(MBEDTLS_SHA256_ALT)
Gilles Peskine449bd832023-01-11 14:50:10 +01007688 MBEDTLS_SSL_DEBUG_BUF(4, "finished sha2 state", (unsigned char *)
7689 sha256.state, sizeof(sha256.state));
Jerry Yu615bd6f2022-02-17 14:25:15 +08007690#endif
7691
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007692 ret = mbedtls_sha256_finish(&sha256, padbuf);
7693 if (ret != 0) {
7694 goto exit;
7695 }
Jerry Yu615bd6f2022-02-17 14:25:15 +08007696#endif /* MBEDTLS_USE_PSA_CRYPTO */
7697
Gilles Peskine449bd832023-01-11 14:50:10 +01007698 ssl->handshake->tls_prf(session->master, 48, sender,
7699 padbuf, 32, buf, len);
Jerry Yu615bd6f2022-02-17 14:25:15 +08007700
Gilles Peskine449bd832023-01-11 14:50:10 +01007701 MBEDTLS_SSL_DEBUG_BUF(3, "calc finished result", buf, len);
Jerry Yu615bd6f2022-02-17 14:25:15 +08007702
Gilles Peskine449bd832023-01-11 14:50:10 +01007703 mbedtls_platform_zeroize(padbuf, sizeof(padbuf));
Jerry Yu615bd6f2022-02-17 14:25:15 +08007704
Gilles Peskine449bd832023-01-11 14:50:10 +01007705 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc finished"));
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007706
7707exit:
7708#if defined(MBEDTLS_USE_PSA_CRYPTO)
7709 psa_hash_abort(&sha256_psa);
Andrzej Kurekdaf5b562023-03-03 05:52:28 -05007710 return PSA_TO_MD_ERR(status);
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007711#else
7712 mbedtls_sha256_free(&sha256);
7713 return ret;
7714#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu615bd6f2022-02-17 14:25:15 +08007715}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04007716#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yu615bd6f2022-02-17 14:25:15 +08007717
Jerry Yub7ba49e2022-02-17 14:25:53 +08007718
Andrzej Kurek25f27152022-08-17 16:09:31 -04007719#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01007720static int ssl_calc_finished_tls_sha384(
Gilles Peskine449bd832023-01-11 14:50:10 +01007721 mbedtls_ssl_context *ssl, unsigned char *buf, int from)
Jerry Yub7ba49e2022-02-17 14:25:53 +08007722{
7723 int len = 12;
7724 const char *sender;
7725 unsigned char padbuf[48];
7726#if defined(MBEDTLS_USE_PSA_CRYPTO)
7727 size_t hash_size;
7728 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
7729 psa_status_t status;
7730#else
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007731 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yub7ba49e2022-02-17 14:25:53 +08007732 mbedtls_sha512_context sha512;
7733#endif
7734
7735 mbedtls_ssl_session *session = ssl->session_negotiate;
Gilles Peskine449bd832023-01-11 14:50:10 +01007736 if (!session) {
Jerry Yub7ba49e2022-02-17 14:25:53 +08007737 session = ssl->session;
Gilles Peskine449bd832023-01-11 14:50:10 +01007738 }
Jerry Yub7ba49e2022-02-17 14:25:53 +08007739
Gilles Peskine449bd832023-01-11 14:50:10 +01007740 sender = (from == MBEDTLS_SSL_IS_CLIENT)
Jerry Yub7ba49e2022-02-17 14:25:53 +08007741 ? "client finished"
7742 : "server finished";
7743
7744#if defined(MBEDTLS_USE_PSA_CRYPTO)
7745 sha384_psa = psa_hash_operation_init();
7746
Gilles Peskine449bd832023-01-11 14:50:10 +01007747 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc PSA finished tls sha384"));
Jerry Yub7ba49e2022-02-17 14:25:53 +08007748
Gilles Peskine449bd832023-01-11 14:50:10 +01007749 status = psa_hash_clone(&ssl->handshake->fin_sha384_psa, &sha384_psa);
7750 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007751 goto exit;
Jerry Yub7ba49e2022-02-17 14:25:53 +08007752 }
7753
Gilles Peskine449bd832023-01-11 14:50:10 +01007754 status = psa_hash_finish(&sha384_psa, padbuf, sizeof(padbuf), &hash_size);
7755 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007756 goto exit;
Jerry Yub7ba49e2022-02-17 14:25:53 +08007757 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007758 MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated padbuf", padbuf, 48);
Jerry Yub7ba49e2022-02-17 14:25:53 +08007759#else
Gilles Peskine449bd832023-01-11 14:50:10 +01007760 mbedtls_sha512_init(&sha512);
Jerry Yub7ba49e2022-02-17 14:25:53 +08007761
Gilles Peskine449bd832023-01-11 14:50:10 +01007762 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc finished tls sha384"));
Jerry Yub7ba49e2022-02-17 14:25:53 +08007763
Gilles Peskine449bd832023-01-11 14:50:10 +01007764 mbedtls_sha512_clone(&sha512, &ssl->handshake->fin_sha384);
Jerry Yub7ba49e2022-02-17 14:25:53 +08007765
7766 /*
7767 * TLSv1.2:
7768 * hash = PRF( master, finished_label,
7769 * Hash( handshake ) )[0.11]
7770 */
7771
7772#if !defined(MBEDTLS_SHA512_ALT)
Gilles Peskine449bd832023-01-11 14:50:10 +01007773 MBEDTLS_SSL_DEBUG_BUF(4, "finished sha512 state", (unsigned char *)
7774 sha512.state, sizeof(sha512.state));
Jerry Yub7ba49e2022-02-17 14:25:53 +08007775#endif
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007776 ret = mbedtls_sha512_finish(&sha512, padbuf);
7777 if (ret != 0) {
7778 goto exit;
7779 }
Jerry Yub7ba49e2022-02-17 14:25:53 +08007780#endif
7781
Gilles Peskine449bd832023-01-11 14:50:10 +01007782 ssl->handshake->tls_prf(session->master, 48, sender,
7783 padbuf, 48, buf, len);
Jerry Yub7ba49e2022-02-17 14:25:53 +08007784
Gilles Peskine449bd832023-01-11 14:50:10 +01007785 MBEDTLS_SSL_DEBUG_BUF(3, "calc finished result", buf, len);
Jerry Yub7ba49e2022-02-17 14:25:53 +08007786
Gilles Peskine449bd832023-01-11 14:50:10 +01007787 mbedtls_platform_zeroize(padbuf, sizeof(padbuf));
Jerry Yub7ba49e2022-02-17 14:25:53 +08007788
Gilles Peskine449bd832023-01-11 14:50:10 +01007789 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc finished"));
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007790
7791exit:
7792#if defined(MBEDTLS_USE_PSA_CRYPTO)
7793 psa_hash_abort(&sha384_psa);
Andrzej Kurekdaf5b562023-03-03 05:52:28 -05007794 return PSA_TO_MD_ERR(status);
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007795#else
7796 mbedtls_sha512_free(&sha512);
7797 return ret;
7798#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yub7ba49e2022-02-17 14:25:53 +08007799}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04007800#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yub7ba49e2022-02-17 14:25:53 +08007801
Gilles Peskine449bd832023-01-11 14:50:10 +01007802void mbedtls_ssl_handshake_wrapup_free_hs_transform(mbedtls_ssl_context *ssl)
Jerry Yuaef00152022-02-17 14:27:31 +08007803{
Gilles Peskine449bd832023-01-11 14:50:10 +01007804 MBEDTLS_SSL_DEBUG_MSG(3, ("=> handshake wrapup: final free"));
Jerry Yuaef00152022-02-17 14:27:31 +08007805
7806 /*
7807 * Free our handshake params
7808 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007809 mbedtls_ssl_handshake_free(ssl);
7810 mbedtls_free(ssl->handshake);
Jerry Yuaef00152022-02-17 14:27:31 +08007811 ssl->handshake = NULL;
7812
7813 /*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08007814 * Free the previous transform and switch in the current one
Jerry Yuaef00152022-02-17 14:27:31 +08007815 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007816 if (ssl->transform) {
7817 mbedtls_ssl_transform_free(ssl->transform);
7818 mbedtls_free(ssl->transform);
Jerry Yuaef00152022-02-17 14:27:31 +08007819 }
7820 ssl->transform = ssl->transform_negotiate;
7821 ssl->transform_negotiate = NULL;
7822
Gilles Peskine449bd832023-01-11 14:50:10 +01007823 MBEDTLS_SSL_DEBUG_MSG(3, ("<= handshake wrapup: final free"));
Jerry Yuaef00152022-02-17 14:27:31 +08007824}
7825
Gilles Peskine449bd832023-01-11 14:50:10 +01007826void mbedtls_ssl_handshake_wrapup(mbedtls_ssl_context *ssl)
Jerry Yu2a9fff52022-02-17 14:28:51 +08007827{
7828 int resume = ssl->handshake->resume;
7829
Gilles Peskine449bd832023-01-11 14:50:10 +01007830 MBEDTLS_SSL_DEBUG_MSG(3, ("=> handshake wrapup"));
Jerry Yu2a9fff52022-02-17 14:28:51 +08007831
7832#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01007833 if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
Jerry Yu2a9fff52022-02-17 14:28:51 +08007834 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
7835 ssl->renego_records_seen = 0;
7836 }
7837#endif
7838
7839 /*
7840 * Free the previous session and switch in the current one
7841 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007842 if (ssl->session) {
Jerry Yu2a9fff52022-02-17 14:28:51 +08007843#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
7844 /* RFC 7366 3.1: keep the EtM state */
7845 ssl->session_negotiate->encrypt_then_mac =
Gilles Peskine449bd832023-01-11 14:50:10 +01007846 ssl->session->encrypt_then_mac;
Jerry Yu2a9fff52022-02-17 14:28:51 +08007847#endif
7848
Gilles Peskine449bd832023-01-11 14:50:10 +01007849 mbedtls_ssl_session_free(ssl->session);
7850 mbedtls_free(ssl->session);
Jerry Yu2a9fff52022-02-17 14:28:51 +08007851 }
7852 ssl->session = ssl->session_negotiate;
7853 ssl->session_negotiate = NULL;
7854
7855 /*
7856 * Add cache entry
7857 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007858 if (ssl->conf->f_set_cache != NULL &&
Jerry Yu2a9fff52022-02-17 14:28:51 +08007859 ssl->session->id_len != 0 &&
Gilles Peskine449bd832023-01-11 14:50:10 +01007860 resume == 0) {
7861 if (ssl->conf->f_set_cache(ssl->conf->p_cache,
7862 ssl->session->id,
7863 ssl->session->id_len,
7864 ssl->session) != 0) {
7865 MBEDTLS_SSL_DEBUG_MSG(1, ("cache did not store session"));
7866 }
Jerry Yu2a9fff52022-02-17 14:28:51 +08007867 }
7868
7869#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007870 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7871 ssl->handshake->flight != NULL) {
Jerry Yu2a9fff52022-02-17 14:28:51 +08007872 /* Cancel handshake timer */
Gilles Peskine449bd832023-01-11 14:50:10 +01007873 mbedtls_ssl_set_timer(ssl, 0);
Jerry Yu2a9fff52022-02-17 14:28:51 +08007874
7875 /* Keep last flight around in case we need to resend it:
7876 * we need the handshake and transform structures for that */
Gilles Peskine449bd832023-01-11 14:50:10 +01007877 MBEDTLS_SSL_DEBUG_MSG(3, ("skip freeing handshake and transform"));
7878 } else
Jerry Yu2a9fff52022-02-17 14:28:51 +08007879#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01007880 mbedtls_ssl_handshake_wrapup_free_hs_transform(ssl);
Jerry Yu2a9fff52022-02-17 14:28:51 +08007881
Jerry Yu5ed73ff2022-10-27 13:08:42 +08007882 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
Jerry Yu2a9fff52022-02-17 14:28:51 +08007883
Gilles Peskine449bd832023-01-11 14:50:10 +01007884 MBEDTLS_SSL_DEBUG_MSG(3, ("<= handshake wrapup"));
Jerry Yu2a9fff52022-02-17 14:28:51 +08007885}
7886
Gilles Peskine449bd832023-01-11 14:50:10 +01007887int mbedtls_ssl_write_finished(mbedtls_ssl_context *ssl)
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007888{
7889 int ret, hash_len;
7890
Gilles Peskine449bd832023-01-11 14:50:10 +01007891 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write finished"));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007892
Gilles Peskine449bd832023-01-11 14:50:10 +01007893 mbedtls_ssl_update_out_pointers(ssl, ssl->transform_negotiate);
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007894
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01007895 ret = ssl->handshake->calc_finished(ssl, ssl->out_msg + 4, ssl->conf->endpoint);
7896 if (ret != 0) {
7897 MBEDTLS_SSL_DEBUG_RET(1, "calc_finished", ret);
7898 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007899
7900 /*
7901 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7902 * may define some other value. Currently (early 2016), no defined
7903 * ciphersuite does this (and this is unlikely to change as activity has
7904 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7905 */
7906 hash_len = 12;
7907
7908#if defined(MBEDTLS_SSL_RENEGOTIATION)
7909 ssl->verify_data_len = hash_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01007910 memcpy(ssl->own_verify_data, ssl->out_msg + 4, hash_len);
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007911#endif
7912
7913 ssl->out_msglen = 4 + hash_len;
7914 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7915 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
7916
7917 /*
7918 * In case of session resuming, invert the client and server
7919 * ChangeCipherSpec messages order.
7920 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007921 if (ssl->handshake->resume != 0) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007922#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007923 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007924 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Gilles Peskine449bd832023-01-11 14:50:10 +01007925 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007926#endif
7927#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007928 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007929 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Gilles Peskine449bd832023-01-11 14:50:10 +01007930 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007931#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01007932 } else {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007933 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01007934 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007935
7936 /*
7937 * Switch to our negotiated transform and session parameters for outbound
7938 * data.
7939 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007940 MBEDTLS_SSL_DEBUG_MSG(3, ("switching to new transform spec for outbound data"));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007941
7942#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007943 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007944 unsigned char i;
7945
7946 /* Remember current epoch settings for resending */
7947 ssl->handshake->alt_transform_out = ssl->transform_out;
Gilles Peskine449bd832023-01-11 14:50:10 +01007948 memcpy(ssl->handshake->alt_out_ctr, ssl->cur_out_ctr,
7949 sizeof(ssl->handshake->alt_out_ctr));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007950
7951 /* Set sequence_number to zero */
Gilles Peskine449bd832023-01-11 14:50:10 +01007952 memset(&ssl->cur_out_ctr[2], 0, sizeof(ssl->cur_out_ctr) - 2);
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007953
7954
7955 /* Increment epoch */
Gilles Peskine449bd832023-01-11 14:50:10 +01007956 for (i = 2; i > 0; i--) {
7957 if (++ssl->cur_out_ctr[i - 1] != 0) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007958 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01007959 }
7960 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007961
7962 /* The loop goes to its end iff the counter is wrapping */
Gilles Peskine449bd832023-01-11 14:50:10 +01007963 if (i == 0) {
7964 MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS epoch would wrap"));
7965 return MBEDTLS_ERR_SSL_COUNTER_WRAPPING;
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007966 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007967 } else
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007968#endif /* MBEDTLS_SSL_PROTO_DTLS */
Gilles Peskine449bd832023-01-11 14:50:10 +01007969 memset(ssl->cur_out_ctr, 0, sizeof(ssl->cur_out_ctr));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007970
7971 ssl->transform_out = ssl->transform_negotiate;
7972 ssl->session_out = ssl->session_negotiate;
7973
7974#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007975 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
7976 mbedtls_ssl_send_flight_completed(ssl);
7977 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007978#endif
7979
Gilles Peskine449bd832023-01-11 14:50:10 +01007980 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
7981 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
7982 return ret;
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007983 }
7984
7985#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007986 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7987 (ret = mbedtls_ssl_flight_transmit(ssl)) != 0) {
7988 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_flight_transmit", ret);
7989 return ret;
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007990 }
7991#endif
7992
Gilles Peskine449bd832023-01-11 14:50:10 +01007993 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write finished"));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007994
Gilles Peskine449bd832023-01-11 14:50:10 +01007995 return 0;
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007996}
7997
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007998#define SSL_MAX_HASH_LEN 12
7999
Gilles Peskine449bd832023-01-11 14:50:10 +01008000int mbedtls_ssl_parse_finished(mbedtls_ssl_context *ssl)
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008001{
8002 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
8003 unsigned int hash_len = 12;
8004 unsigned char buf[SSL_MAX_HASH_LEN];
8005
Gilles Peskine449bd832023-01-11 14:50:10 +01008006 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse finished"));
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008007
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01008008 ret = ssl->handshake->calc_finished(ssl, buf, ssl->conf->endpoint ^ 1);
8009 if (ret != 0) {
8010 MBEDTLS_SSL_DEBUG_RET(1, "calc_finished", ret);
8011 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008012
Gilles Peskine449bd832023-01-11 14:50:10 +01008013 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
8014 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008015 goto exit;
8016 }
8017
Gilles Peskine449bd832023-01-11 14:50:10 +01008018 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
8019 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
8020 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8021 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008022 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
8023 goto exit;
8024 }
8025
Gilles Peskine449bd832023-01-11 14:50:10 +01008026 if (ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED) {
8027 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8028 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008029 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
8030 goto exit;
8031 }
8032
Gilles Peskine449bd832023-01-11 14:50:10 +01008033 if (ssl->in_hslen != mbedtls_ssl_hs_hdr_len(ssl) + hash_len) {
8034 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
8035 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8036 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008037 ret = MBEDTLS_ERR_SSL_DECODE_ERROR;
8038 goto exit;
8039 }
8040
Gilles Peskine449bd832023-01-11 14:50:10 +01008041 if (mbedtls_ct_memcmp(ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl),
8042 buf, hash_len) != 0) {
8043 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
8044 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8045 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008046 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
8047 goto exit;
8048 }
8049
8050#if defined(MBEDTLS_SSL_RENEGOTIATION)
8051 ssl->verify_data_len = hash_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008052 memcpy(ssl->peer_verify_data, buf, hash_len);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008053#endif
8054
Gilles Peskine449bd832023-01-11 14:50:10 +01008055 if (ssl->handshake->resume != 0) {
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008056#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008057 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008058 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Gilles Peskine449bd832023-01-11 14:50:10 +01008059 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008060#endif
8061#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008062 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008063 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Gilles Peskine449bd832023-01-11 14:50:10 +01008064 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008065#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01008066 } else {
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008067 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01008068 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008069
8070#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01008071 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
8072 mbedtls_ssl_recv_flight_completed(ssl);
8073 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008074#endif
8075
Gilles Peskine449bd832023-01-11 14:50:10 +01008076 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse finished"));
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008077
8078exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008079 mbedtls_platform_zeroize(buf, hash_len);
8080 return ret;
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008081}
8082
Jerry Yu392112c2022-02-17 14:34:10 +08008083#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
8084/*
8085 * Helper to get TLS 1.2 PRF from ciphersuite
8086 * (Duplicates bits of logic from ssl_set_handshake_prfs().)
8087 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008088static tls_prf_fn ssl_tls12prf_from_cs(int ciphersuite_id)
Jerry Yu392112c2022-02-17 14:34:10 +08008089{
Jerry Yu392112c2022-02-17 14:34:10 +08008090 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01008091 mbedtls_ssl_ciphersuite_from_id(ciphersuite_id);
Andrzej Kurek68327742022-10-03 06:18:18 -04008092#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01008093 if (ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA384) {
8094 return tls_prf_sha384;
8095 } else
Jerry Yu392112c2022-02-17 14:34:10 +08008096#endif
Andrzej Kurek894edde2022-09-29 06:31:14 -04008097#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
8098 {
Gilles Peskine449bd832023-01-11 14:50:10 +01008099 if (ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA256) {
8100 return tls_prf_sha256;
8101 }
Andrzej Kurek894edde2022-09-29 06:31:14 -04008102 }
8103#endif
8104#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
8105 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
8106 (void) ciphersuite_info;
8107#endif
Andrzej Kurekeabeb302022-10-17 07:52:51 -04008108
Gilles Peskine449bd832023-01-11 14:50:10 +01008109 return NULL;
Jerry Yu392112c2022-02-17 14:34:10 +08008110}
8111#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Jerry Yue93ffcd2022-02-17 14:37:06 +08008112
Gilles Peskine449bd832023-01-11 14:50:10 +01008113static mbedtls_tls_prf_types tls_prf_get_type(mbedtls_ssl_tls_prf_cb *tls_prf)
Jerry Yue93ffcd2022-02-17 14:37:06 +08008114{
8115 ((void) tls_prf);
Andrzej Kurek25f27152022-08-17 16:09:31 -04008116#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01008117 if (tls_prf == tls_prf_sha384) {
8118 return MBEDTLS_SSL_TLS_PRF_SHA384;
8119 } else
Jerry Yue93ffcd2022-02-17 14:37:06 +08008120#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04008121#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01008122 if (tls_prf == tls_prf_sha256) {
8123 return MBEDTLS_SSL_TLS_PRF_SHA256;
8124 } else
Jerry Yue93ffcd2022-02-17 14:37:06 +08008125#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01008126 return MBEDTLS_SSL_TLS_PRF_NONE;
Jerry Yue93ffcd2022-02-17 14:37:06 +08008127}
8128
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008129/*
8130 * Populate a transform structure with session keys and all the other
8131 * necessary information.
8132 *
8133 * Parameters:
8134 * - [in/out]: transform: structure to populate
8135 * [in] must be just initialised with mbedtls_ssl_transform_init()
8136 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
8137 * - [in] ciphersuite
8138 * - [in] master
8139 * - [in] encrypt_then_mac
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008140 * - [in] tls_prf: pointer to PRF to use for key derivation
8141 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
Glenn Strauss07c64162022-03-14 12:34:51 -04008142 * - [in] tls_version: TLS version
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008143 * - [in] endpoint: client or server
8144 * - [in] ssl: used for:
8145 * - ssl->conf->{f,p}_export_keys
8146 * [in] optionally used for:
8147 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
8148 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02008149MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01008150static int ssl_tls12_populate_transform(mbedtls_ssl_transform *transform,
8151 int ciphersuite,
8152 const unsigned char master[48],
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008153#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01008154 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008155#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01008156 ssl_tls_prf_t tls_prf,
8157 const unsigned char randbytes[64],
8158 mbedtls_ssl_protocol_version tls_version,
8159 unsigned endpoint,
8160 const mbedtls_ssl_context *ssl)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008161{
8162 int ret = 0;
8163 unsigned char keyblk[256];
8164 unsigned char *key1;
8165 unsigned char *key2;
8166 unsigned char *mac_enc;
8167 unsigned char *mac_dec;
8168 size_t mac_key_len = 0;
8169 size_t iv_copy_len;
8170 size_t keylen;
8171 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008172 mbedtls_ssl_mode_t ssl_mode;
Neil Armstronge4512952022-03-08 09:08:22 +01008173#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008174 const mbedtls_cipher_info_t *cipher_info;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008175 const mbedtls_md_info_t *md_info;
Neil Armstronge4512952022-03-08 09:08:22 +01008176#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008177
8178#if defined(MBEDTLS_USE_PSA_CRYPTO)
8179 psa_key_type_t key_type;
8180 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8181 psa_algorithm_t alg;
Neil Armstronge4512952022-03-08 09:08:22 +01008182 psa_algorithm_t mac_alg = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008183 size_t key_bits;
8184 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
8185#endif
8186
8187#if !defined(MBEDTLS_DEBUG_C) && \
8188 !defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Gilles Peskine449bd832023-01-11 14:50:10 +01008189 if (ssl->f_export_keys == NULL) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008190 ssl = NULL; /* make sure we don't use it except for these cases */
8191 (void) ssl;
8192 }
8193#endif
8194
8195 /*
8196 * Some data just needs copying into the structure
8197 */
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008198#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008199 transform->encrypt_then_mac = encrypt_then_mac;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008200#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Glenn Strauss07c64162022-03-14 12:34:51 -04008201 transform->tls_version = tls_version;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008202
8203#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01008204 memcpy(transform->randbytes, randbytes, sizeof(transform->randbytes));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008205#endif
8206
8207#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01008208 if (tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008209 /* At the moment, we keep TLS <= 1.2 and TLS 1.3 transform
8210 * generation separate. This should never happen. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008211 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008212 }
8213#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
8214
8215 /*
8216 * Get various info structures
8217 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008218 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(ciphersuite);
8219 if (ciphersuite_info == NULL) {
8220 MBEDTLS_SSL_DEBUG_MSG(1, ("ciphersuite info for %d not found",
8221 ciphersuite));
8222 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008223 }
8224
Neil Armstrongab555e02022-04-04 11:07:59 +02008225 ssl_mode = mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008226#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01008227 encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008228#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01008229 ciphersuite_info);
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008230
Gilles Peskine449bd832023-01-11 14:50:10 +01008231 if (ssl_mode == MBEDTLS_SSL_MODE_AEAD) {
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008232 transform->taglen =
8233 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Gilles Peskine449bd832023-01-11 14:50:10 +01008234 }
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008235
8236#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008237 if ((status = mbedtls_ssl_cipher_to_psa(ciphersuite_info->cipher,
8238 transform->taglen,
8239 &alg,
8240 &key_type,
8241 &key_bits)) != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05008242 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01008243 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_cipher_to_psa", ret);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008244 goto end;
8245 }
8246#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008247 cipher_info = mbedtls_cipher_info_from_type(ciphersuite_info->cipher);
8248 if (cipher_info == NULL) {
8249 MBEDTLS_SSL_DEBUG_MSG(1, ("cipher info for %u not found",
8250 ciphersuite_info->cipher));
8251 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008252 }
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008253#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008254
Neil Armstronge4512952022-03-08 09:08:22 +01008255#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008256 mac_alg = mbedtls_hash_info_psa_from_md(ciphersuite_info->mac);
8257 if (mac_alg == 0) {
8258 MBEDTLS_SSL_DEBUG_MSG(1, ("mbedtls_hash_info_psa_from_md for %u not found",
8259 (unsigned) ciphersuite_info->mac));
8260 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Neil Armstronge4512952022-03-08 09:08:22 +01008261 }
8262#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008263 md_info = mbedtls_md_info_from_type(ciphersuite_info->mac);
8264 if (md_info == NULL) {
8265 MBEDTLS_SSL_DEBUG_MSG(1, ("mbedtls_md info for %u not found",
8266 (unsigned) ciphersuite_info->mac));
8267 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008268 }
Neil Armstronge4512952022-03-08 09:08:22 +01008269#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008270
8271#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
8272 /* Copy own and peer's CID if the use of the CID
8273 * extension has been negotiated. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008274 if (ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED) {
8275 MBEDTLS_SSL_DEBUG_MSG(3, ("Copy CIDs into SSL transform"));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008276
8277 transform->in_cid_len = ssl->own_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008278 memcpy(transform->in_cid, ssl->own_cid, ssl->own_cid_len);
8279 MBEDTLS_SSL_DEBUG_BUF(3, "Incoming CID", transform->in_cid,
8280 transform->in_cid_len);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008281
8282 transform->out_cid_len = ssl->handshake->peer_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008283 memcpy(transform->out_cid, ssl->handshake->peer_cid,
8284 ssl->handshake->peer_cid_len);
8285 MBEDTLS_SSL_DEBUG_BUF(3, "Outgoing CID", transform->out_cid,
8286 transform->out_cid_len);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008287 }
8288#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
8289
8290 /*
8291 * Compute key block using the PRF
8292 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008293 ret = tls_prf(master, 48, "key expansion", randbytes, 64, keyblk, 256);
8294 if (ret != 0) {
8295 MBEDTLS_SSL_DEBUG_RET(1, "prf", ret);
8296 return ret;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008297 }
8298
Gilles Peskine449bd832023-01-11 14:50:10 +01008299 MBEDTLS_SSL_DEBUG_MSG(3, ("ciphersuite = %s",
8300 mbedtls_ssl_get_ciphersuite_name(ciphersuite)));
8301 MBEDTLS_SSL_DEBUG_BUF(3, "master secret", master, 48);
8302 MBEDTLS_SSL_DEBUG_BUF(4, "random bytes", randbytes, 64);
8303 MBEDTLS_SSL_DEBUG_BUF(4, "key block", keyblk, 256);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008304
8305 /*
8306 * Determine the appropriate key, IV and MAC length.
8307 */
8308
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008309#if defined(MBEDTLS_USE_PSA_CRYPTO)
8310 keylen = PSA_BITS_TO_BYTES(key_bits);
8311#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008312 keylen = mbedtls_cipher_info_get_key_bitlen(cipher_info) / 8;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008313#endif
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008314
8315#if defined(MBEDTLS_GCM_C) || \
8316 defined(MBEDTLS_CCM_C) || \
8317 defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008318 if (ssl_mode == MBEDTLS_SSL_MODE_AEAD) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008319 size_t explicit_ivlen;
8320
8321 transform->maclen = 0;
8322 mac_key_len = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008323
8324 /* All modes haves 96-bit IVs, but the length of the static parts vary
8325 * with mode and version:
8326 * - For GCM and CCM in TLS 1.2, there's a static IV of 4 Bytes
8327 * (to be concatenated with a dynamically chosen IV of 8 Bytes)
8328 * - For ChaChaPoly in TLS 1.2, and all modes in TLS 1.3, there's
8329 * a static IV of 12 Bytes (to be XOR'ed with the 8 Byte record
8330 * sequence number).
8331 */
8332 transform->ivlen = 12;
David Horstmann3b2276a2022-10-06 14:49:08 +01008333
8334 int is_chachapoly = 0;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008335#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008336 is_chachapoly = (key_type == PSA_KEY_TYPE_CHACHA20);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008337#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008338 is_chachapoly = (mbedtls_cipher_info_get_mode(cipher_info)
8339 == MBEDTLS_MODE_CHACHAPOLY);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008340#endif /* MBEDTLS_USE_PSA_CRYPTO */
David Horstmann3b2276a2022-10-06 14:49:08 +01008341
Gilles Peskine449bd832023-01-11 14:50:10 +01008342 if (is_chachapoly) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008343 transform->fixed_ivlen = 12;
Gilles Peskine449bd832023-01-11 14:50:10 +01008344 } else {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008345 transform->fixed_ivlen = 4;
Gilles Peskine449bd832023-01-11 14:50:10 +01008346 }
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008347
8348 /* Minimum length of encrypted record */
8349 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
8350 transform->minlen = explicit_ivlen + transform->taglen;
Gilles Peskine449bd832023-01-11 14:50:10 +01008351 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008352#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
8353#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01008354 if (ssl_mode == MBEDTLS_SSL_MODE_STREAM ||
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008355 ssl_mode == MBEDTLS_SSL_MODE_CBC ||
Gilles Peskine449bd832023-01-11 14:50:10 +01008356 ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM) {
Neil Armstronge4512952022-03-08 09:08:22 +01008357#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008358 size_t block_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008359#else
8360 size_t block_size = cipher_info->block_size;
8361#endif /* MBEDTLS_USE_PSA_CRYPTO */
8362
8363#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronge4512952022-03-08 09:08:22 +01008364 /* Get MAC length */
8365 mac_key_len = PSA_HASH_LENGTH(mac_alg);
8366#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008367 /* Initialize HMAC contexts */
Gilles Peskine449bd832023-01-11 14:50:10 +01008368 if ((ret = mbedtls_md_setup(&transform->md_ctx_enc, md_info, 1)) != 0 ||
8369 (ret = mbedtls_md_setup(&transform->md_ctx_dec, md_info, 1)) != 0) {
8370 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_setup", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008371 goto end;
8372 }
8373
8374 /* Get MAC length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008375 mac_key_len = mbedtls_md_get_size(md_info);
Neil Armstronge4512952022-03-08 09:08:22 +01008376#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008377 transform->maclen = mac_key_len;
8378
8379 /* IV length */
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008380#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008381 transform->ivlen = PSA_CIPHER_IV_LENGTH(key_type, alg);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008382#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008383 transform->ivlen = cipher_info->iv_size;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008384#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008385
8386 /* Minimum length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008387 if (ssl_mode == MBEDTLS_SSL_MODE_STREAM) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008388 transform->minlen = transform->maclen;
Gilles Peskine449bd832023-01-11 14:50:10 +01008389 } else {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008390 /*
8391 * GenericBlockCipher:
8392 * 1. if EtM is in use: one block plus MAC
8393 * otherwise: * first multiple of blocklen greater than maclen
8394 * 2. IV
8395 */
8396#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01008397 if (ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008398 transform->minlen = transform->maclen
Gilles Peskine449bd832023-01-11 14:50:10 +01008399 + block_size;
8400 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008401#endif
8402 {
8403 transform->minlen = transform->maclen
Gilles Peskine449bd832023-01-11 14:50:10 +01008404 + block_size
8405 - transform->maclen % block_size;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008406 }
8407
Gilles Peskine449bd832023-01-11 14:50:10 +01008408 if (tls_version == MBEDTLS_SSL_VERSION_TLS1_2) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008409 transform->minlen += transform->ivlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01008410 } else {
8411 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008412 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
8413 goto end;
8414 }
8415 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008416 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008417#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
8418 {
Gilles Peskine449bd832023-01-11 14:50:10 +01008419 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
8420 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008421 }
8422
Gilles Peskine449bd832023-01-11 14:50:10 +01008423 MBEDTLS_SSL_DEBUG_MSG(3, ("keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
8424 (unsigned) keylen,
8425 (unsigned) transform->minlen,
8426 (unsigned) transform->ivlen,
8427 (unsigned) transform->maclen));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008428
8429 /*
8430 * Finally setup the cipher contexts, IVs and MAC secrets.
8431 */
8432#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008433 if (endpoint == MBEDTLS_SSL_IS_CLIENT) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008434 key1 = keyblk + mac_key_len * 2;
8435 key2 = keyblk + mac_key_len * 2 + keylen;
8436
8437 mac_enc = keyblk;
8438 mac_dec = keyblk + mac_key_len;
8439
Gilles Peskine449bd832023-01-11 14:50:10 +01008440 iv_copy_len = (transform->fixed_ivlen) ?
8441 transform->fixed_ivlen : transform->ivlen;
8442 memcpy(transform->iv_enc, key2 + keylen, iv_copy_len);
8443 memcpy(transform->iv_dec, key2 + keylen + iv_copy_len,
8444 iv_copy_len);
8445 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008446#endif /* MBEDTLS_SSL_CLI_C */
8447#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008448 if (endpoint == MBEDTLS_SSL_IS_SERVER) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008449 key1 = keyblk + mac_key_len * 2 + keylen;
8450 key2 = keyblk + mac_key_len * 2;
8451
8452 mac_enc = keyblk + mac_key_len;
8453 mac_dec = keyblk;
8454
Gilles Peskine449bd832023-01-11 14:50:10 +01008455 iv_copy_len = (transform->fixed_ivlen) ?
8456 transform->fixed_ivlen : transform->ivlen;
8457 memcpy(transform->iv_dec, key1 + keylen, iv_copy_len);
8458 memcpy(transform->iv_enc, key1 + keylen + iv_copy_len,
8459 iv_copy_len);
8460 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008461#endif /* MBEDTLS_SSL_SRV_C */
8462 {
Gilles Peskine449bd832023-01-11 14:50:10 +01008463 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 if (ssl != NULL && ssl->f_export_keys != NULL) {
8469 ssl->f_export_keys(ssl->p_export_keys,
8470 MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET,
8471 master, 48,
8472 randbytes + 32,
8473 randbytes,
8474 tls_prf_get_type(tls_prf));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008475 }
8476
8477#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008478 transform->psa_alg = alg;
8479
Gilles Peskine449bd832023-01-11 14:50:10 +01008480 if (alg != MBEDTLS_SSL_NULL_CIPHER) {
8481 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
8482 psa_set_key_algorithm(&attributes, alg);
8483 psa_set_key_type(&attributes, key_type);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008484
Gilles Peskine449bd832023-01-11 14:50:10 +01008485 if ((status = psa_import_key(&attributes,
8486 key1,
8487 PSA_BITS_TO_BYTES(key_bits),
8488 &transform->psa_key_enc)) != PSA_SUCCESS) {
8489 MBEDTLS_SSL_DEBUG_RET(3, "psa_import_key", (int) status);
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05008490 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01008491 MBEDTLS_SSL_DEBUG_RET(1, "psa_import_key", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008492 goto end;
8493 }
8494
Gilles Peskine449bd832023-01-11 14:50:10 +01008495 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008496
Gilles Peskine449bd832023-01-11 14:50:10 +01008497 if ((status = psa_import_key(&attributes,
8498 key2,
8499 PSA_BITS_TO_BYTES(key_bits),
8500 &transform->psa_key_dec)) != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05008501 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01008502 MBEDTLS_SSL_DEBUG_RET(1, "psa_import_key", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008503 goto end;
8504 }
8505 }
8506#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008507 if ((ret = mbedtls_cipher_setup(&transform->cipher_ctx_enc,
8508 cipher_info)) != 0) {
8509 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008510 goto end;
8511 }
8512
Gilles Peskine449bd832023-01-11 14:50:10 +01008513 if ((ret = mbedtls_cipher_setup(&transform->cipher_ctx_dec,
8514 cipher_info)) != 0) {
8515 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008516 goto end;
8517 }
8518
Gilles Peskine449bd832023-01-11 14:50:10 +01008519 if ((ret = mbedtls_cipher_setkey(&transform->cipher_ctx_enc, key1,
8520 (int) mbedtls_cipher_info_get_key_bitlen(cipher_info),
8521 MBEDTLS_ENCRYPT)) != 0) {
8522 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setkey", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008523 goto end;
8524 }
8525
Gilles Peskine449bd832023-01-11 14:50:10 +01008526 if ((ret = mbedtls_cipher_setkey(&transform->cipher_ctx_dec, key2,
8527 (int) mbedtls_cipher_info_get_key_bitlen(cipher_info),
8528 MBEDTLS_DECRYPT)) != 0) {
8529 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setkey", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008530 goto end;
8531 }
8532
8533#if defined(MBEDTLS_CIPHER_MODE_CBC)
Gilles Peskine449bd832023-01-11 14:50:10 +01008534 if (mbedtls_cipher_info_get_mode(cipher_info) == MBEDTLS_MODE_CBC) {
8535 if ((ret = mbedtls_cipher_set_padding_mode(&transform->cipher_ctx_enc,
8536 MBEDTLS_PADDING_NONE)) != 0) {
8537 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_set_padding_mode", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008538 goto end;
8539 }
8540
Gilles Peskine449bd832023-01-11 14:50:10 +01008541 if ((ret = mbedtls_cipher_set_padding_mode(&transform->cipher_ctx_dec,
8542 MBEDTLS_PADDING_NONE)) != 0) {
8543 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_set_padding_mode", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008544 goto end;
8545 }
8546 }
8547#endif /* MBEDTLS_CIPHER_MODE_CBC */
8548#endif /* MBEDTLS_USE_PSA_CRYPTO */
8549
Neil Armstrong29c0c042022-03-17 17:47:28 +01008550#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
8551 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
8552 For AEAD-based ciphersuites, there is nothing to do here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008553 if (mac_key_len != 0) {
Neil Armstrong29c0c042022-03-17 17:47:28 +01008554#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008555 transform->psa_mac_alg = PSA_ALG_HMAC(mac_alg);
Neil Armstrong29c0c042022-03-17 17:47:28 +01008556
Gilles Peskine449bd832023-01-11 14:50:10 +01008557 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
8558 psa_set_key_algorithm(&attributes, PSA_ALG_HMAC(mac_alg));
8559 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
Neil Armstrong29c0c042022-03-17 17:47:28 +01008560
Gilles Peskine449bd832023-01-11 14:50:10 +01008561 if ((status = psa_import_key(&attributes,
8562 mac_enc, mac_key_len,
8563 &transform->psa_mac_enc)) != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05008564 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01008565 MBEDTLS_SSL_DEBUG_RET(1, "psa_import_mac_key", ret);
Neil Armstrong29c0c042022-03-17 17:47:28 +01008566 goto end;
8567 }
8568
Gilles Peskine449bd832023-01-11 14:50:10 +01008569 if ((transform->psa_alg == MBEDTLS_SSL_NULL_CIPHER) ||
8570 ((transform->psa_alg == PSA_ALG_CBC_NO_PADDING)
Andrzej Kurek8c95ac42022-08-17 16:17:00 -04008571#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01008572 && (transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED)
Andrzej Kurek8c95ac42022-08-17 16:17:00 -04008573#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01008574 )) {
Neil Armstrong29c0c042022-03-17 17:47:28 +01008575 /* mbedtls_ct_hmac() requires the key to be exportable */
Gilles Peskine449bd832023-01-11 14:50:10 +01008576 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT |
8577 PSA_KEY_USAGE_VERIFY_HASH);
8578 } else {
8579 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
8580 }
Neil Armstrong29c0c042022-03-17 17:47:28 +01008581
Gilles Peskine449bd832023-01-11 14:50:10 +01008582 if ((status = psa_import_key(&attributes,
8583 mac_dec, mac_key_len,
8584 &transform->psa_mac_dec)) != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05008585 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01008586 MBEDTLS_SSL_DEBUG_RET(1, "psa_import_mac_key", ret);
Neil Armstrong29c0c042022-03-17 17:47:28 +01008587 goto end;
8588 }
8589#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008590 ret = mbedtls_md_hmac_starts(&transform->md_ctx_enc, mac_enc, mac_key_len);
8591 if (ret != 0) {
Neil Armstrong29c0c042022-03-17 17:47:28 +01008592 goto end;
Gilles Peskine449bd832023-01-11 14:50:10 +01008593 }
8594 ret = mbedtls_md_hmac_starts(&transform->md_ctx_dec, mac_dec, mac_key_len);
8595 if (ret != 0) {
Neil Armstrong29c0c042022-03-17 17:47:28 +01008596 goto end;
Gilles Peskine449bd832023-01-11 14:50:10 +01008597 }
Neil Armstrong29c0c042022-03-17 17:47:28 +01008598#endif /* MBEDTLS_USE_PSA_CRYPTO */
8599 }
8600#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
8601
8602 ((void) mac_dec);
8603 ((void) mac_enc);
8604
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008605end:
Gilles Peskine449bd832023-01-11 14:50:10 +01008606 mbedtls_platform_zeroize(keyblk, sizeof(keyblk));
8607 return ret;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008608}
8609
Valerio Settia08b1a42022-11-17 15:10:02 +01008610#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) && \
8611 defined(MBEDTLS_USE_PSA_CRYPTO)
Valerio Setti6b3dab02022-11-17 17:14:54 +01008612int mbedtls_psa_ecjpake_read_round(
Gilles Peskine449bd832023-01-11 14:50:10 +01008613 psa_pake_operation_t *pake_ctx,
8614 const unsigned char *buf,
8615 size_t len, mbedtls_ecjpake_rounds_t round)
Valerio Settia08b1a42022-11-17 15:10:02 +01008616{
8617 psa_status_t status;
8618 size_t input_offset = 0;
Valerio Setti819de862022-11-17 18:05:19 +01008619 /*
Valerio Setti6b3dab02022-11-17 17:14:54 +01008620 * At round one repeat the KEY_SHARE, ZK_PUBLIC & ZF_PROOF twice
8621 * At round two perform a single cycle
8622 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008623 unsigned int remaining_steps = (round == MBEDTLS_ECJPAKE_ROUND_ONE) ? 2 : 1;
Valerio Settia08b1a42022-11-17 15:10:02 +01008624
Gilles Peskine449bd832023-01-11 14:50:10 +01008625 for (; remaining_steps > 0; remaining_steps--) {
8626 for (psa_pake_step_t step = PSA_PAKE_STEP_KEY_SHARE;
Valerio Settia08b1a42022-11-17 15:10:02 +01008627 step <= PSA_PAKE_STEP_ZK_PROOF;
Gilles Peskine449bd832023-01-11 14:50:10 +01008628 ++step) {
Valerio Settia08b1a42022-11-17 15:10:02 +01008629 /* Length is stored at the first byte */
8630 size_t length = buf[input_offset];
8631 input_offset += 1;
8632
Gilles Peskine449bd832023-01-11 14:50:10 +01008633 if (input_offset + length > len) {
Valerio Settia08b1a42022-11-17 15:10:02 +01008634 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
8635 }
8636
Gilles Peskine449bd832023-01-11 14:50:10 +01008637 status = psa_pake_input(pake_ctx, step,
8638 buf + input_offset, length);
8639 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05008640 return PSA_TO_MBEDTLS_ERR(status);
Valerio Settia08b1a42022-11-17 15:10:02 +01008641 }
8642
8643 input_offset += length;
8644 }
8645 }
8646
Gilles Peskine449bd832023-01-11 14:50:10 +01008647 if (input_offset != len) {
Valerio Setti61ea17d2022-11-18 12:11:00 +01008648 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01008649 }
Valerio Setti30ebe112022-11-17 16:23:34 +01008650
Gilles Peskine449bd832023-01-11 14:50:10 +01008651 return 0;
Valerio Settia08b1a42022-11-17 15:10:02 +01008652}
8653
Valerio Setti6b3dab02022-11-17 17:14:54 +01008654int mbedtls_psa_ecjpake_write_round(
Gilles Peskine449bd832023-01-11 14:50:10 +01008655 psa_pake_operation_t *pake_ctx,
8656 unsigned char *buf,
8657 size_t len, size_t *olen,
8658 mbedtls_ecjpake_rounds_t round)
Valerio Settia08b1a42022-11-17 15:10:02 +01008659{
8660 psa_status_t status;
8661 size_t output_offset = 0;
8662 size_t output_len;
Valerio Setti819de862022-11-17 18:05:19 +01008663 /*
Valerio Setti6b3dab02022-11-17 17:14:54 +01008664 * At round one repeat the KEY_SHARE, ZK_PUBLIC & ZF_PROOF twice
8665 * At round two perform a single cycle
8666 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008667 unsigned int remaining_steps = (round == MBEDTLS_ECJPAKE_ROUND_ONE) ? 2 : 1;
Valerio Settia08b1a42022-11-17 15:10:02 +01008668
Gilles Peskine449bd832023-01-11 14:50:10 +01008669 for (; remaining_steps > 0; remaining_steps--) {
8670 for (psa_pake_step_t step = PSA_PAKE_STEP_KEY_SHARE;
8671 step <= PSA_PAKE_STEP_ZK_PROOF;
8672 ++step) {
Valerio Setti79f6b6b2022-11-21 14:17:03 +01008673 /*
Valerio Settid4a9b1a2022-11-22 11:11:10 +01008674 * For each step, prepend 1 byte with the length of the data as
8675 * given by psa_pake_output().
Valerio Setti79f6b6b2022-11-21 14:17:03 +01008676 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008677 status = psa_pake_output(pake_ctx, step,
8678 buf + output_offset + 1,
8679 len - output_offset - 1,
8680 &output_len);
8681 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05008682 return PSA_TO_MBEDTLS_ERR(status);
Valerio Settia08b1a42022-11-17 15:10:02 +01008683 }
8684
Valerio Setti99d88c12022-11-22 16:03:43 +01008685 *(buf + output_offset) = (uint8_t) output_len;
Valerio Setti79f6b6b2022-11-21 14:17:03 +01008686
8687 output_offset += output_len + 1;
Valerio Settia08b1a42022-11-17 15:10:02 +01008688 }
8689 }
8690
8691 *olen = output_offset;
8692
Gilles Peskine449bd832023-01-11 14:50:10 +01008693 return 0;
Valerio Settia08b1a42022-11-17 15:10:02 +01008694}
Valerio Settia08b1a42022-11-17 15:10:02 +01008695#endif //MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED && MBEDTLS_USE_PSA_CRYPTO
8696
Jerry Yuee40f9d2022-02-17 14:55:16 +08008697#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008698int mbedtls_ssl_get_key_exchange_md_tls1_2(mbedtls_ssl_context *ssl,
8699 unsigned char *hash, size_t *hashlen,
8700 unsigned char *data, size_t data_len,
8701 mbedtls_md_type_t md_alg)
Jerry Yuee40f9d2022-02-17 14:55:16 +08008702{
8703 psa_status_t status;
8704 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01008705 psa_algorithm_t hash_alg = mbedtls_hash_info_psa_from_md(md_alg);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008706
Gilles Peskine449bd832023-01-11 14:50:10 +01008707 MBEDTLS_SSL_DEBUG_MSG(3, ("Perform PSA-based computation of digest of ServerKeyExchange"));
Jerry Yuee40f9d2022-02-17 14:55:16 +08008708
Gilles Peskine449bd832023-01-11 14:50:10 +01008709 if ((status = psa_hash_setup(&hash_operation,
8710 hash_alg)) != PSA_SUCCESS) {
8711 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_setup", status);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008712 goto exit;
8713 }
8714
Gilles Peskine449bd832023-01-11 14:50:10 +01008715 if ((status = psa_hash_update(&hash_operation, ssl->handshake->randbytes,
8716 64)) != PSA_SUCCESS) {
8717 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_update", status);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008718 goto exit;
8719 }
8720
Gilles Peskine449bd832023-01-11 14:50:10 +01008721 if ((status = psa_hash_update(&hash_operation,
8722 data, data_len)) != PSA_SUCCESS) {
8723 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_update", status);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008724 goto exit;
8725 }
8726
Gilles Peskine449bd832023-01-11 14:50:10 +01008727 if ((status = psa_hash_finish(&hash_operation, hash, PSA_HASH_MAX_SIZE,
8728 hashlen)) != PSA_SUCCESS) {
8729 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_finish", status);
8730 goto exit;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008731 }
8732
8733exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008734 if (status != PSA_SUCCESS) {
8735 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8736 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
8737 switch (status) {
Jerry Yuee40f9d2022-02-17 14:55:16 +08008738 case PSA_ERROR_NOT_SUPPORTED:
Gilles Peskine449bd832023-01-11 14:50:10 +01008739 return MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008740 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
8741 case PSA_ERROR_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +01008742 return MBEDTLS_ERR_MD_BAD_INPUT_DATA;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008743 case PSA_ERROR_INSUFFICIENT_MEMORY:
Gilles Peskine449bd832023-01-11 14:50:10 +01008744 return MBEDTLS_ERR_MD_ALLOC_FAILED;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008745 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01008746 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008747 }
8748 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008749 return 0;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008750}
8751
8752#else
8753
Gilles Peskine449bd832023-01-11 14:50:10 +01008754int mbedtls_ssl_get_key_exchange_md_tls1_2(mbedtls_ssl_context *ssl,
8755 unsigned char *hash, size_t *hashlen,
8756 unsigned char *data, size_t data_len,
8757 mbedtls_md_type_t md_alg)
Jerry Yuee40f9d2022-02-17 14:55:16 +08008758{
8759 int ret = 0;
8760 mbedtls_md_context_t ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +01008761 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type(md_alg);
8762 *hashlen = mbedtls_md_get_size(md_info);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008763
Gilles Peskine449bd832023-01-11 14:50:10 +01008764 MBEDTLS_SSL_DEBUG_MSG(3, ("Perform mbedtls-based computation of digest of ServerKeyExchange"));
Jerry Yuee40f9d2022-02-17 14:55:16 +08008765
Gilles Peskine449bd832023-01-11 14:50:10 +01008766 mbedtls_md_init(&ctx);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008767
8768 /*
8769 * digitally-signed struct {
8770 * opaque client_random[32];
8771 * opaque server_random[32];
8772 * ServerDHParams params;
8773 * };
8774 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008775 if ((ret = mbedtls_md_setup(&ctx, md_info, 0)) != 0) {
8776 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_setup", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008777 goto exit;
8778 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008779 if ((ret = mbedtls_md_starts(&ctx)) != 0) {
8780 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_starts", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008781 goto exit;
8782 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008783 if ((ret = mbedtls_md_update(&ctx, ssl->handshake->randbytes, 64)) != 0) {
8784 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_update", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008785 goto exit;
8786 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008787 if ((ret = mbedtls_md_update(&ctx, data, data_len)) != 0) {
8788 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_update", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008789 goto exit;
8790 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008791 if ((ret = mbedtls_md_finish(&ctx, hash)) != 0) {
8792 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_finish", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008793 goto exit;
8794 }
8795
8796exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008797 mbedtls_md_free(&ctx);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008798
Gilles Peskine449bd832023-01-11 14:50:10 +01008799 if (ret != 0) {
8800 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8801 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
8802 }
Jerry Yuee40f9d2022-02-17 14:55:16 +08008803
Gilles Peskine449bd832023-01-11 14:50:10 +01008804 return ret;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008805}
8806#endif /* MBEDTLS_USE_PSA_CRYPTO */
8807
Jerry Yud9d91da2022-02-17 14:57:06 +08008808#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
8809
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008810/* Find the preferred hash for a given signature algorithm. */
8811unsigned int mbedtls_ssl_tls12_get_preferred_hash_for_sig_alg(
Gilles Peskine449bd832023-01-11 14:50:10 +01008812 mbedtls_ssl_context *ssl,
8813 unsigned int sig_alg)
Jerry Yud9d91da2022-02-17 14:57:06 +08008814{
Gabor Mezei078e8032022-04-27 21:17:56 +02008815 unsigned int i;
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008816 uint16_t *received_sig_algs = ssl->handshake->received_sig_algs;
Gabor Mezei078e8032022-04-27 21:17:56 +02008817
Gilles Peskine449bd832023-01-11 14:50:10 +01008818 if (sig_alg == MBEDTLS_SSL_SIG_ANON) {
8819 return MBEDTLS_SSL_HASH_NONE;
8820 }
Gabor Mezei078e8032022-04-27 21:17:56 +02008821
Gilles Peskine449bd832023-01-11 14:50:10 +01008822 for (i = 0; received_sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++) {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008823 unsigned int hash_alg_received =
Gilles Peskine449bd832023-01-11 14:50:10 +01008824 MBEDTLS_SSL_TLS12_HASH_ALG_FROM_SIG_AND_HASH_ALG(
8825 received_sig_algs[i]);
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008826 unsigned int sig_alg_received =
Gilles Peskine449bd832023-01-11 14:50:10 +01008827 MBEDTLS_SSL_TLS12_SIG_ALG_FROM_SIG_AND_HASH_ALG(
8828 received_sig_algs[i]);
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008829
Gilles Peskine449bd832023-01-11 14:50:10 +01008830 if (sig_alg == sig_alg_received) {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008831#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008832 if (ssl->handshake->key_cert && ssl->handshake->key_cert->key) {
Neil Armstrong96eceb82022-06-30 18:05:05 +02008833 psa_algorithm_t psa_hash_alg =
Gilles Peskine449bd832023-01-11 14:50:10 +01008834 mbedtls_hash_info_psa_from_md(hash_alg_received);
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008835
Gilles Peskine449bd832023-01-11 14:50:10 +01008836 if (sig_alg_received == MBEDTLS_SSL_SIG_ECDSA &&
8837 !mbedtls_pk_can_do_ext(ssl->handshake->key_cert->key,
8838 PSA_ALG_ECDSA(psa_hash_alg),
8839 PSA_KEY_USAGE_SIGN_HASH)) {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008840 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01008841 }
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008842
Gilles Peskine449bd832023-01-11 14:50:10 +01008843 if (sig_alg_received == MBEDTLS_SSL_SIG_RSA &&
8844 !mbedtls_pk_can_do_ext(ssl->handshake->key_cert->key,
8845 PSA_ALG_RSA_PKCS1V15_SIGN(
8846 psa_hash_alg),
8847 PSA_KEY_USAGE_SIGN_HASH)) {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008848 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01008849 }
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008850 }
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008851#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong96eceb82022-06-30 18:05:05 +02008852
Gilles Peskine449bd832023-01-11 14:50:10 +01008853 return hash_alg_received;
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008854 }
Jerry Yud9d91da2022-02-17 14:57:06 +08008855 }
Jerry Yud9d91da2022-02-17 14:57:06 +08008856
Gilles Peskine449bd832023-01-11 14:50:10 +01008857 return MBEDTLS_SSL_HASH_NONE;
Jerry Yud9d91da2022-02-17 14:57:06 +08008858}
8859
8860#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
8861
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008862/* Serialization of TLS 1.2 sessions:
8863 *
8864 * struct {
8865 * uint64 start_time;
8866 * uint8 ciphersuite[2]; // defined by the standard
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008867 * uint8 session_id_len; // at most 32
8868 * opaque session_id[32];
8869 * opaque master[48]; // fixed length in the standard
8870 * uint32 verify_result;
8871 * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert
8872 * opaque ticket<0..2^24-1>; // length 0 means no ticket
8873 * uint32 ticket_lifetime;
8874 * uint8 mfl_code; // up to 255 according to standard
8875 * uint8 encrypt_then_mac; // 0 or 1
8876 * } serialized_session_tls12;
8877 *
8878 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008879static size_t ssl_tls12_session_save(const mbedtls_ssl_session *session,
8880 unsigned char *buf,
8881 size_t buf_len)
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008882{
8883 unsigned char *p = buf;
8884 size_t used = 0;
8885
8886#if defined(MBEDTLS_HAVE_TIME)
8887 uint64_t start;
8888#endif
8889#if defined(MBEDTLS_X509_CRT_PARSE_C)
8890#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8891 size_t cert_len;
8892#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8893#endif /* MBEDTLS_X509_CRT_PARSE_C */
8894
8895 /*
8896 * Time
8897 */
8898#if defined(MBEDTLS_HAVE_TIME)
8899 used += 8;
8900
Gilles Peskine449bd832023-01-11 14:50:10 +01008901 if (used <= buf_len) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008902 start = (uint64_t) session->start;
8903
Gilles Peskine449bd832023-01-11 14:50:10 +01008904 MBEDTLS_PUT_UINT64_BE(start, p, 0);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008905 p += 8;
8906 }
8907#endif /* MBEDTLS_HAVE_TIME */
8908
8909 /*
8910 * Basic mandatory fields
8911 */
8912 used += 2 /* ciphersuite */
Gilles Peskine449bd832023-01-11 14:50:10 +01008913 + 1 /* id_len */
8914 + sizeof(session->id)
8915 + sizeof(session->master)
8916 + 4; /* verify_result */
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008917
Gilles Peskine449bd832023-01-11 14:50:10 +01008918 if (used <= buf_len) {
8919 MBEDTLS_PUT_UINT16_BE(session->ciphersuite, p, 0);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008920 p += 2;
8921
Gilles Peskine449bd832023-01-11 14:50:10 +01008922 *p++ = MBEDTLS_BYTE_0(session->id_len);
8923 memcpy(p, session->id, 32);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008924 p += 32;
8925
Gilles Peskine449bd832023-01-11 14:50:10 +01008926 memcpy(p, session->master, 48);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008927 p += 48;
8928
Gilles Peskine449bd832023-01-11 14:50:10 +01008929 MBEDTLS_PUT_UINT32_BE(session->verify_result, p, 0);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008930 p += 4;
8931 }
8932
8933 /*
8934 * Peer's end-entity certificate
8935 */
8936#if defined(MBEDTLS_X509_CRT_PARSE_C)
8937#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01008938 if (session->peer_cert == NULL) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008939 cert_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01008940 } else {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008941 cert_len = session->peer_cert->raw.len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008942 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008943
8944 used += 3 + cert_len;
8945
Gilles Peskine449bd832023-01-11 14:50:10 +01008946 if (used <= buf_len) {
8947 *p++ = MBEDTLS_BYTE_2(cert_len);
8948 *p++ = MBEDTLS_BYTE_1(cert_len);
8949 *p++ = MBEDTLS_BYTE_0(cert_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008950
Gilles Peskine449bd832023-01-11 14:50:10 +01008951 if (session->peer_cert != NULL) {
8952 memcpy(p, session->peer_cert->raw.p, cert_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008953 p += cert_len;
8954 }
8955 }
8956#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008957 if (session->peer_cert_digest != NULL) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008958 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008959 if (used <= buf_len) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008960 *p++ = (unsigned char) session->peer_cert_digest_type;
8961 *p++ = (unsigned char) session->peer_cert_digest_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008962 memcpy(p, session->peer_cert_digest,
8963 session->peer_cert_digest_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008964 p += session->peer_cert_digest_len;
8965 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008966 } else {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008967 used += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01008968 if (used <= buf_len) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008969 *p++ = (unsigned char) MBEDTLS_MD_NONE;
8970 *p++ = 0;
8971 }
8972 }
8973#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8974#endif /* MBEDTLS_X509_CRT_PARSE_C */
8975
8976 /*
8977 * Session ticket if any, plus associated data
8978 */
8979#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8980 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
8981
Gilles Peskine449bd832023-01-11 14:50:10 +01008982 if (used <= buf_len) {
8983 *p++ = MBEDTLS_BYTE_2(session->ticket_len);
8984 *p++ = MBEDTLS_BYTE_1(session->ticket_len);
8985 *p++ = MBEDTLS_BYTE_0(session->ticket_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008986
Gilles Peskine449bd832023-01-11 14:50:10 +01008987 if (session->ticket != NULL) {
8988 memcpy(p, session->ticket, session->ticket_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008989 p += session->ticket_len;
8990 }
8991
Gilles Peskine449bd832023-01-11 14:50:10 +01008992 MBEDTLS_PUT_UINT32_BE(session->ticket_lifetime, p, 0);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008993 p += 4;
8994 }
8995#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8996
8997 /*
8998 * Misc extension-related info
8999 */
9000#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9001 used += 1;
9002
Gilles Peskine449bd832023-01-11 14:50:10 +01009003 if (used <= buf_len) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009004 *p++ = session->mfl_code;
Gilles Peskine449bd832023-01-11 14:50:10 +01009005 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009006#endif
9007
9008#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
9009 used += 1;
9010
Gilles Peskine449bd832023-01-11 14:50:10 +01009011 if (used <= buf_len) {
9012 *p++ = MBEDTLS_BYTE_0(session->encrypt_then_mac);
9013 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009014#endif
9015
Gilles Peskine449bd832023-01-11 14:50:10 +01009016 return used;
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009017}
9018
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02009019MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01009020static int ssl_tls12_session_load(mbedtls_ssl_session *session,
9021 const unsigned char *buf,
9022 size_t len)
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009023{
9024#if defined(MBEDTLS_HAVE_TIME)
9025 uint64_t start;
9026#endif
9027#if defined(MBEDTLS_X509_CRT_PARSE_C)
9028#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
9029 size_t cert_len;
9030#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9031#endif /* MBEDTLS_X509_CRT_PARSE_C */
9032
9033 const unsigned char *p = buf;
9034 const unsigned char * const end = buf + len;
9035
9036 /*
9037 * Time
9038 */
9039#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01009040 if (8 > (size_t) (end - p)) {
9041 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9042 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009043
Gilles Peskine449bd832023-01-11 14:50:10 +01009044 start = ((uint64_t) p[0] << 56) |
9045 ((uint64_t) p[1] << 48) |
9046 ((uint64_t) p[2] << 40) |
9047 ((uint64_t) p[3] << 32) |
9048 ((uint64_t) p[4] << 24) |
9049 ((uint64_t) p[5] << 16) |
9050 ((uint64_t) p[6] << 8) |
9051 ((uint64_t) p[7]);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009052 p += 8;
9053
9054 session->start = (time_t) start;
9055#endif /* MBEDTLS_HAVE_TIME */
9056
9057 /*
9058 * Basic mandatory fields
9059 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009060 if (2 + 1 + 32 + 48 + 4 > (size_t) (end - p)) {
9061 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9062 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009063
Gilles Peskine449bd832023-01-11 14:50:10 +01009064 session->ciphersuite = (p[0] << 8) | p[1];
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009065 p += 2;
9066
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009067 session->id_len = *p++;
Gilles Peskine449bd832023-01-11 14:50:10 +01009068 memcpy(session->id, p, 32);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009069 p += 32;
9070
Gilles Peskine449bd832023-01-11 14:50:10 +01009071 memcpy(session->master, p, 48);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009072 p += 48;
9073
Gilles Peskine449bd832023-01-11 14:50:10 +01009074 session->verify_result = ((uint32_t) p[0] << 24) |
9075 ((uint32_t) p[1] << 16) |
9076 ((uint32_t) p[2] << 8) |
9077 ((uint32_t) p[3]);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009078 p += 4;
9079
9080 /* Immediately clear invalid pointer values that have been read, in case
9081 * we exit early before we replaced them with valid ones. */
9082#if defined(MBEDTLS_X509_CRT_PARSE_C)
9083#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
9084 session->peer_cert = NULL;
9085#else
9086 session->peer_cert_digest = NULL;
9087#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9088#endif /* MBEDTLS_X509_CRT_PARSE_C */
9089#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
9090 session->ticket = NULL;
9091#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
9092
9093 /*
9094 * Peer certificate
9095 */
9096#if defined(MBEDTLS_X509_CRT_PARSE_C)
9097#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
9098 /* Deserialize CRT from the end of the ticket. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009099 if (3 > (size_t) (end - p)) {
9100 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9101 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009102
Gilles Peskine449bd832023-01-11 14:50:10 +01009103 cert_len = (p[0] << 16) | (p[1] << 8) | p[2];
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009104 p += 3;
9105
Gilles Peskine449bd832023-01-11 14:50:10 +01009106 if (cert_len != 0) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009107 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
9108
Gilles Peskine449bd832023-01-11 14:50:10 +01009109 if (cert_len > (size_t) (end - p)) {
9110 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9111 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009112
Gilles Peskine449bd832023-01-11 14:50:10 +01009113 session->peer_cert = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009114
Gilles Peskine449bd832023-01-11 14:50:10 +01009115 if (session->peer_cert == NULL) {
9116 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
9117 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009118
Gilles Peskine449bd832023-01-11 14:50:10 +01009119 mbedtls_x509_crt_init(session->peer_cert);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009120
Gilles Peskine449bd832023-01-11 14:50:10 +01009121 if ((ret = mbedtls_x509_crt_parse_der(session->peer_cert,
9122 p, cert_len)) != 0) {
9123 mbedtls_x509_crt_free(session->peer_cert);
9124 mbedtls_free(session->peer_cert);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009125 session->peer_cert = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01009126 return ret;
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009127 }
9128
9129 p += cert_len;
9130 }
9131#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9132 /* Deserialize CRT digest from the end of the ticket. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009133 if (2 > (size_t) (end - p)) {
9134 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9135 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009136
9137 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
9138 session->peer_cert_digest_len = (size_t) *p++;
9139
Gilles Peskine449bd832023-01-11 14:50:10 +01009140 if (session->peer_cert_digest_len != 0) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009141 const mbedtls_md_info_t *md_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01009142 mbedtls_md_info_from_type(session->peer_cert_digest_type);
9143 if (md_info == NULL) {
9144 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9145 }
9146 if (session->peer_cert_digest_len != mbedtls_md_get_size(md_info)) {
9147 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9148 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009149
Gilles Peskine449bd832023-01-11 14:50:10 +01009150 if (session->peer_cert_digest_len > (size_t) (end - p)) {
9151 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9152 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009153
9154 session->peer_cert_digest =
Gilles Peskine449bd832023-01-11 14:50:10 +01009155 mbedtls_calloc(1, session->peer_cert_digest_len);
9156 if (session->peer_cert_digest == NULL) {
9157 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
9158 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009159
Gilles Peskine449bd832023-01-11 14:50:10 +01009160 memcpy(session->peer_cert_digest, p,
9161 session->peer_cert_digest_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009162 p += session->peer_cert_digest_len;
9163 }
9164#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9165#endif /* MBEDTLS_X509_CRT_PARSE_C */
9166
9167 /*
9168 * Session ticket and associated data
9169 */
9170#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01009171 if (3 > (size_t) (end - p)) {
9172 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9173 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009174
Gilles Peskine449bd832023-01-11 14:50:10 +01009175 session->ticket_len = (p[0] << 16) | (p[1] << 8) | p[2];
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009176 p += 3;
9177
Gilles Peskine449bd832023-01-11 14:50:10 +01009178 if (session->ticket_len != 0) {
9179 if (session->ticket_len > (size_t) (end - p)) {
9180 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9181 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009182
Gilles Peskine449bd832023-01-11 14:50:10 +01009183 session->ticket = mbedtls_calloc(1, session->ticket_len);
9184 if (session->ticket == NULL) {
9185 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
9186 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009187
Gilles Peskine449bd832023-01-11 14:50:10 +01009188 memcpy(session->ticket, p, session->ticket_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009189 p += session->ticket_len;
9190 }
9191
Gilles Peskine449bd832023-01-11 14:50:10 +01009192 if (4 > (size_t) (end - p)) {
9193 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9194 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009195
Gilles Peskine449bd832023-01-11 14:50:10 +01009196 session->ticket_lifetime = ((uint32_t) p[0] << 24) |
9197 ((uint32_t) p[1] << 16) |
9198 ((uint32_t) p[2] << 8) |
9199 ((uint32_t) p[3]);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009200 p += 4;
9201#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
9202
9203 /*
9204 * Misc extension-related info
9205 */
9206#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01009207 if (1 > (size_t) (end - p)) {
9208 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9209 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009210
9211 session->mfl_code = *p++;
9212#endif
9213
9214#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01009215 if (1 > (size_t) (end - p)) {
9216 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9217 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009218
9219 session->encrypt_then_mac = *p++;
9220#endif
9221
9222 /* Done, should have consumed entire buffer */
Gilles Peskine449bd832023-01-11 14:50:10 +01009223 if (p != end) {
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 return 0;
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009228}
Jerry Yudc7bd172022-02-17 13:44:15 +08009229#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9230
XiaokangQian75d40ef2022-04-20 11:05:24 +00009231int mbedtls_ssl_validate_ciphersuite(
9232 const mbedtls_ssl_context *ssl,
9233 const mbedtls_ssl_ciphersuite_t *suite_info,
9234 mbedtls_ssl_protocol_version min_tls_version,
Gilles Peskine449bd832023-01-11 14:50:10 +01009235 mbedtls_ssl_protocol_version max_tls_version)
XiaokangQian75d40ef2022-04-20 11:05:24 +00009236{
9237 (void) ssl;
9238
Gilles Peskine449bd832023-01-11 14:50:10 +01009239 if (suite_info == NULL) {
9240 return -1;
9241 }
XiaokangQian75d40ef2022-04-20 11:05:24 +00009242
Gilles Peskine449bd832023-01-11 14:50:10 +01009243 if ((suite_info->min_tls_version > max_tls_version) ||
9244 (suite_info->max_tls_version < min_tls_version)) {
9245 return -1;
XiaokangQian75d40ef2022-04-20 11:05:24 +00009246 }
9247
XiaokangQian060d8672022-04-21 09:24:56 +00009248#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_CLI_C)
XiaokangQian75d40ef2022-04-20 11:05:24 +00009249#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Neil Armstrongca7d5062022-05-31 14:43:23 +02009250#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01009251 if (suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
9252 ssl->handshake->psa_pake_ctx_is_ok != 1)
Neil Armstrongca7d5062022-05-31 14:43:23 +02009253#else
Gilles Peskine449bd832023-01-11 14:50:10 +01009254 if (suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
9255 mbedtls_ecjpake_check(&ssl->handshake->ecjpake_ctx) != 0)
Neil Armstrongca7d5062022-05-31 14:43:23 +02009256#endif /* MBEDTLS_USE_PSA_CRYPTO */
XiaokangQian75d40ef2022-04-20 11:05:24 +00009257 {
Gilles Peskine449bd832023-01-11 14:50:10 +01009258 return -1;
XiaokangQian75d40ef2022-04-20 11:05:24 +00009259 }
9260#endif
9261
9262 /* Don't suggest PSK-based ciphersuite if no PSK is available. */
9263#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01009264 if (mbedtls_ssl_ciphersuite_uses_psk(suite_info) &&
9265 mbedtls_ssl_conf_has_static_psk(ssl->conf) == 0) {
9266 return -1;
XiaokangQian75d40ef2022-04-20 11:05:24 +00009267 }
9268#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
9269#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9270
Gilles Peskine449bd832023-01-11 14:50:10 +01009271 return 0;
XiaokangQian75d40ef2022-04-20 11:05:24 +00009272}
9273
Ronald Crone68ab4f2022-10-05 12:46:29 +02009274#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
XiaokangQianeaf36512022-04-24 09:07:44 +00009275/*
9276 * Function for writing a signature algorithm extension.
9277 *
9278 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
9279 * value (TLS 1.3 RFC8446):
9280 * enum {
9281 * ....
9282 * ecdsa_secp256r1_sha256( 0x0403 ),
9283 * ecdsa_secp384r1_sha384( 0x0503 ),
9284 * ecdsa_secp521r1_sha512( 0x0603 ),
9285 * ....
9286 * } SignatureScheme;
9287 *
9288 * struct {
9289 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
9290 * } SignatureSchemeList;
9291 *
9292 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
9293 * value (TLS 1.2 RFC5246):
9294 * enum {
9295 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
9296 * sha512(6), (255)
9297 * } HashAlgorithm;
9298 *
9299 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
9300 * SignatureAlgorithm;
9301 *
9302 * struct {
9303 * HashAlgorithm hash;
9304 * SignatureAlgorithm signature;
9305 * } SignatureAndHashAlgorithm;
9306 *
9307 * SignatureAndHashAlgorithm
9308 * supported_signature_algorithms<2..2^16-2>;
9309 *
9310 * The TLS 1.3 signature algorithm extension was defined to be a compatible
9311 * generalization of the TLS 1.2 signature algorithm extension.
9312 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
9313 * `SignatureScheme` field of TLS 1.3
9314 *
9315 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009316int mbedtls_ssl_write_sig_alg_ext(mbedtls_ssl_context *ssl, unsigned char *buf,
9317 const unsigned char *end, size_t *out_len)
XiaokangQianeaf36512022-04-24 09:07:44 +00009318{
9319 unsigned char *p = buf;
9320 unsigned char *supported_sig_alg; /* Start of supported_signature_algorithms */
9321 size_t supported_sig_alg_len = 0; /* Length of supported_signature_algorithms */
9322
9323 *out_len = 0;
9324
Gilles Peskine449bd832023-01-11 14:50:10 +01009325 MBEDTLS_SSL_DEBUG_MSG(3, ("adding signature_algorithms extension"));
XiaokangQianeaf36512022-04-24 09:07:44 +00009326
9327 /* Check if we have space for header and length field:
9328 * - extension_type (2 bytes)
9329 * - extension_data_length (2 bytes)
9330 * - supported_signature_algorithms_length (2 bytes)
9331 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009332 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 6);
XiaokangQianeaf36512022-04-24 09:07:44 +00009333 p += 6;
9334
9335 /*
9336 * Write supported_signature_algorithms
9337 */
9338 supported_sig_alg = p;
Gilles Peskine449bd832023-01-11 14:50:10 +01009339 const uint16_t *sig_alg = mbedtls_ssl_get_sig_algs(ssl);
9340 if (sig_alg == NULL) {
9341 return MBEDTLS_ERR_SSL_BAD_CONFIG;
9342 }
XiaokangQianeaf36512022-04-24 09:07:44 +00009343
Gilles Peskine449bd832023-01-11 14:50:10 +01009344 for (; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++) {
9345 MBEDTLS_SSL_DEBUG_MSG(3, ("got signature scheme [%x] %s",
9346 *sig_alg,
9347 mbedtls_ssl_sig_alg_to_str(*sig_alg)));
9348 if (!mbedtls_ssl_sig_alg_is_supported(ssl, *sig_alg)) {
XiaokangQianeaf36512022-04-24 09:07:44 +00009349 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01009350 }
9351 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
9352 MBEDTLS_PUT_UINT16_BE(*sig_alg, p, 0);
XiaokangQianeaf36512022-04-24 09:07:44 +00009353 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01009354 MBEDTLS_SSL_DEBUG_MSG(3, ("sent signature scheme [%x] %s",
9355 *sig_alg,
9356 mbedtls_ssl_sig_alg_to_str(*sig_alg)));
XiaokangQianeaf36512022-04-24 09:07:44 +00009357 }
9358
9359 /* Length of supported_signature_algorithms */
9360 supported_sig_alg_len = p - supported_sig_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01009361 if (supported_sig_alg_len == 0) {
9362 MBEDTLS_SSL_DEBUG_MSG(1, ("No signature algorithms defined."));
9363 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
XiaokangQianeaf36512022-04-24 09:07:44 +00009364 }
9365
Gilles Peskine449bd832023-01-11 14:50:10 +01009366 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SIG_ALG, buf, 0);
9367 MBEDTLS_PUT_UINT16_BE(supported_sig_alg_len + 2, buf, 2);
9368 MBEDTLS_PUT_UINT16_BE(supported_sig_alg_len, buf, 4);
XiaokangQianeaf36512022-04-24 09:07:44 +00009369
XiaokangQianeaf36512022-04-24 09:07:44 +00009370 *out_len = p - buf;
9371
9372#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01009373 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_SIG_ALG);
XiaokangQianeaf36512022-04-24 09:07:44 +00009374#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu0c354a22022-08-29 15:25:36 +08009375
Gilles Peskine449bd832023-01-11 14:50:10 +01009376 return 0;
XiaokangQianeaf36512022-04-24 09:07:44 +00009377}
Ronald Crone68ab4f2022-10-05 12:46:29 +02009378#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
XiaokangQianeaf36512022-04-24 09:07:44 +00009379
XiaokangQian40a35232022-05-07 09:02:40 +00009380#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
XiaokangQian9b2b7712022-05-17 02:57:00 +00009381/*
9382 * mbedtls_ssl_parse_server_name_ext
9383 *
9384 * Structure of server_name extension:
9385 *
9386 * enum {
9387 * host_name(0), (255)
9388 * } NameType;
9389 * opaque HostName<1..2^16-1>;
9390 *
9391 * struct {
9392 * NameType name_type;
9393 * select (name_type) {
9394 * case host_name: HostName;
9395 * } name;
9396 * } ServerName;
9397 * struct {
9398 * ServerName server_name_list<1..2^16-1>
9399 * } ServerNameList;
9400 */
Ronald Cronce7d76e2022-07-08 18:56:49 +02009401MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01009402int mbedtls_ssl_parse_server_name_ext(mbedtls_ssl_context *ssl,
9403 const unsigned char *buf,
9404 const unsigned char *end)
XiaokangQian40a35232022-05-07 09:02:40 +00009405{
9406 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
9407 const unsigned char *p = buf;
XiaokangQian9b2b7712022-05-17 02:57:00 +00009408 size_t server_name_list_len, hostname_len;
9409 const unsigned char *server_name_list_end;
XiaokangQian40a35232022-05-07 09:02:40 +00009410
Gilles Peskine449bd832023-01-11 14:50:10 +01009411 MBEDTLS_SSL_DEBUG_MSG(3, ("parse ServerName extension"));
XiaokangQian40a35232022-05-07 09:02:40 +00009412
Gilles Peskine449bd832023-01-11 14:50:10 +01009413 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
9414 server_name_list_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian40a35232022-05-07 09:02:40 +00009415 p += 2;
9416
Gilles Peskine449bd832023-01-11 14:50:10 +01009417 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, server_name_list_len);
XiaokangQian9b2b7712022-05-17 02:57:00 +00009418 server_name_list_end = p + server_name_list_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01009419 while (p < server_name_list_end) {
9420 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, server_name_list_end, 3);
9421 hostname_len = MBEDTLS_GET_UINT16_BE(p, 1);
9422 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, server_name_list_end,
9423 hostname_len + 3);
XiaokangQian40a35232022-05-07 09:02:40 +00009424
Gilles Peskine449bd832023-01-11 14:50:10 +01009425 if (p[0] == MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME) {
XiaokangQian75fe8c72022-06-15 09:42:45 +00009426 /* sni_name is intended to be used only during the parsing of the
9427 * ClientHello message (it is reset to NULL before the end of
9428 * the message parsing). Thus it is ok to just point to the
9429 * reception buffer and not make a copy of it.
9430 */
XiaokangQianf2a94202022-05-20 06:44:24 +00009431 ssl->handshake->sni_name = p + 3;
9432 ssl->handshake->sni_name_len = hostname_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01009433 if (ssl->conf->f_sni == NULL) {
9434 return 0;
XiaokangQian40a35232022-05-07 09:02:40 +00009435 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009436 ret = ssl->conf->f_sni(ssl->conf->p_sni,
9437 ssl, p + 3, hostname_len);
9438 if (ret != 0) {
9439 MBEDTLS_SSL_DEBUG_RET(1, "ssl_sni_wrapper", ret);
9440 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_UNRECOGNIZED_NAME,
9441 MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME);
9442 return MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME;
9443 }
9444 return 0;
XiaokangQian40a35232022-05-07 09:02:40 +00009445 }
9446
9447 p += hostname_len + 3;
9448 }
9449
Gilles Peskine449bd832023-01-11 14:50:10 +01009450 return 0;
XiaokangQian40a35232022-05-07 09:02:40 +00009451}
9452#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
9453
XiaokangQianacb39922022-06-17 10:18:48 +00009454#if defined(MBEDTLS_SSL_ALPN)
Ronald Cronce7d76e2022-07-08 18:56:49 +02009455MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01009456int mbedtls_ssl_parse_alpn_ext(mbedtls_ssl_context *ssl,
9457 const unsigned char *buf,
9458 const unsigned char *end)
XiaokangQianacb39922022-06-17 10:18:48 +00009459{
9460 const unsigned char *p = buf;
XiaokangQianc7403452022-06-23 03:24:12 +00009461 size_t protocol_name_list_len;
XiaokangQian95d5f542022-06-24 02:29:26 +00009462 const unsigned char *protocol_name_list;
9463 const unsigned char *protocol_name_list_end;
XiaokangQianc7403452022-06-23 03:24:12 +00009464 size_t protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009465
9466 /* If ALPN not configured, just ignore the extension */
Gilles Peskine449bd832023-01-11 14:50:10 +01009467 if (ssl->conf->alpn_list == NULL) {
9468 return 0;
9469 }
XiaokangQianacb39922022-06-17 10:18:48 +00009470
9471 /*
XiaokangQianc7403452022-06-23 03:24:12 +00009472 * RFC7301, section 3.1
9473 * opaque ProtocolName<1..2^8-1>;
XiaokangQianacb39922022-06-17 10:18:48 +00009474 *
XiaokangQianc7403452022-06-23 03:24:12 +00009475 * struct {
9476 * ProtocolName protocol_name_list<2..2^16-1>
9477 * } ProtocolNameList;
XiaokangQianacb39922022-06-17 10:18:48 +00009478 */
9479
XiaokangQianc7403452022-06-23 03:24:12 +00009480 /*
XiaokangQian0b776e22022-06-24 09:04:59 +00009481 * protocol_name_list_len 2 bytes
9482 * protocol_name_len 1 bytes
9483 * protocol_name >=1 byte
XiaokangQianc7403452022-06-23 03:24:12 +00009484 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009485 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 4);
XiaokangQianacb39922022-06-17 10:18:48 +00009486
Gilles Peskine449bd832023-01-11 14:50:10 +01009487 protocol_name_list_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQianacb39922022-06-17 10:18:48 +00009488 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01009489 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, protocol_name_list_len);
XiaokangQian95d5f542022-06-24 02:29:26 +00009490 protocol_name_list = p;
9491 protocol_name_list_end = p + protocol_name_list_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009492
9493 /* Validate peer's list (lengths) */
Gilles Peskine449bd832023-01-11 14:50:10 +01009494 while (p < protocol_name_list_end) {
XiaokangQian95d5f542022-06-24 02:29:26 +00009495 protocol_name_len = *p++;
Gilles Peskine449bd832023-01-11 14:50:10 +01009496 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, protocol_name_list_end,
9497 protocol_name_len);
9498 if (protocol_name_len == 0) {
XiaokangQian95d5f542022-06-24 02:29:26 +00009499 MBEDTLS_SSL_PEND_FATAL_ALERT(
9500 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
Gilles Peskine449bd832023-01-11 14:50:10 +01009501 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
9502 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
XiaokangQian95d5f542022-06-24 02:29:26 +00009503 }
9504
9505 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009506 }
9507
9508 /* Use our order of preference */
Gilles Peskine449bd832023-01-11 14:50:10 +01009509 for (const char **alpn = ssl->conf->alpn_list; *alpn != NULL; alpn++) {
9510 size_t const alpn_len = strlen(*alpn);
XiaokangQian95d5f542022-06-24 02:29:26 +00009511 p = protocol_name_list;
Gilles Peskine449bd832023-01-11 14:50:10 +01009512 while (p < protocol_name_list_end) {
XiaokangQian95d5f542022-06-24 02:29:26 +00009513 protocol_name_len = *p++;
Gilles Peskine449bd832023-01-11 14:50:10 +01009514 if (protocol_name_len == alpn_len &&
9515 memcmp(p, *alpn, alpn_len) == 0) {
XiaokangQianacb39922022-06-17 10:18:48 +00009516 ssl->alpn_chosen = *alpn;
Gilles Peskine449bd832023-01-11 14:50:10 +01009517 return 0;
XiaokangQianacb39922022-06-17 10:18:48 +00009518 }
XiaokangQian95d5f542022-06-24 02:29:26 +00009519
9520 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009521 }
9522 }
9523
XiaokangQian95d5f542022-06-24 02:29:26 +00009524 /* If we get here, no match was found */
XiaokangQianacb39922022-06-17 10:18:48 +00009525 MBEDTLS_SSL_PEND_FATAL_ALERT(
Gilles Peskine449bd832023-01-11 14:50:10 +01009526 MBEDTLS_SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL,
9527 MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL);
9528 return MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL;
XiaokangQianacb39922022-06-17 10:18:48 +00009529}
9530
Gilles Peskine449bd832023-01-11 14:50:10 +01009531int mbedtls_ssl_write_alpn_ext(mbedtls_ssl_context *ssl,
9532 unsigned char *buf,
9533 unsigned char *end,
9534 size_t *out_len)
XiaokangQianacb39922022-06-17 10:18:48 +00009535{
9536 unsigned char *p = buf;
XiaokangQian95d5f542022-06-24 02:29:26 +00009537 size_t protocol_name_len;
XiaokangQianc7403452022-06-23 03:24:12 +00009538 *out_len = 0;
XiaokangQianacb39922022-06-17 10:18:48 +00009539
Gilles Peskine449bd832023-01-11 14:50:10 +01009540 if (ssl->alpn_chosen == NULL) {
9541 return 0;
XiaokangQianacb39922022-06-17 10:18:48 +00009542 }
9543
Gilles Peskine449bd832023-01-11 14:50:10 +01009544 protocol_name_len = strlen(ssl->alpn_chosen);
9545 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 7 + protocol_name_len);
XiaokangQianacb39922022-06-17 10:18:48 +00009546
Gilles Peskine449bd832023-01-11 14:50:10 +01009547 MBEDTLS_SSL_DEBUG_MSG(3, ("server side, adding alpn extension"));
XiaokangQianacb39922022-06-17 10:18:48 +00009548 /*
9549 * 0 . 1 ext identifier
9550 * 2 . 3 ext length
9551 * 4 . 5 protocol list length
9552 * 6 . 6 protocol name length
9553 * 7 . 7+n protocol name
9554 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009555 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_ALPN, p, 0);
XiaokangQianacb39922022-06-17 10:18:48 +00009556
XiaokangQian95d5f542022-06-24 02:29:26 +00009557 *out_len = 7 + protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009558
Gilles Peskine449bd832023-01-11 14:50:10 +01009559 MBEDTLS_PUT_UINT16_BE(protocol_name_len + 3, p, 2);
9560 MBEDTLS_PUT_UINT16_BE(protocol_name_len + 1, p, 4);
XiaokangQian0b776e22022-06-24 09:04:59 +00009561 /* Note: the length of the chosen protocol has been checked to be less
9562 * than 255 bytes in `mbedtls_ssl_conf_alpn_protocols`.
9563 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009564 p[6] = MBEDTLS_BYTE_0(protocol_name_len);
XiaokangQianacb39922022-06-17 10:18:48 +00009565
Gilles Peskine449bd832023-01-11 14:50:10 +01009566 memcpy(p + 7, ssl->alpn_chosen, protocol_name_len);
Jerry Yub95dd362022-11-08 21:19:34 +08009567
9568#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01009569 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_ALPN);
Jerry Yub95dd362022-11-08 21:19:34 +08009570#endif
9571
Gilles Peskine449bd832023-01-11 14:50:10 +01009572 return 0;
XiaokangQianacb39922022-06-17 10:18:48 +00009573}
9574#endif /* MBEDTLS_SSL_ALPN */
9575
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009576#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
Xiaokang Qian03409292022-10-12 02:49:52 +00009577 defined(MBEDTLS_SSL_SESSION_TICKETS) && \
Xiaokang Qianed0620c2022-10-12 06:58:13 +00009578 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \
Xiaokang Qianed3afcd2022-10-12 08:31:11 +00009579 defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01009580int mbedtls_ssl_session_set_hostname(mbedtls_ssl_session *session,
9581 const char *hostname)
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009582{
9583 /* Initialize to suppress unnecessary compiler warning */
9584 size_t hostname_len = 0;
9585
9586 /* Check if new hostname is valid before
9587 * making any change to current one */
Gilles Peskine449bd832023-01-11 14:50:10 +01009588 if (hostname != NULL) {
9589 hostname_len = strlen(hostname);
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009590
Gilles Peskine449bd832023-01-11 14:50:10 +01009591 if (hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN) {
9592 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9593 }
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009594 }
9595
9596 /* Now it's clear that we will overwrite the old hostname,
9597 * so we can free it safely */
Gilles Peskine449bd832023-01-11 14:50:10 +01009598 if (session->hostname != NULL) {
9599 mbedtls_platform_zeroize(session->hostname,
9600 strlen(session->hostname));
9601 mbedtls_free(session->hostname);
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009602 }
9603
9604 /* Passing NULL as hostname shall clear the old one */
Gilles Peskine449bd832023-01-11 14:50:10 +01009605 if (hostname == NULL) {
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009606 session->hostname = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01009607 } else {
9608 session->hostname = mbedtls_calloc(1, hostname_len + 1);
9609 if (session->hostname == NULL) {
9610 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
9611 }
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009612
Gilles Peskine449bd832023-01-11 14:50:10 +01009613 memcpy(session->hostname, hostname, hostname_len);
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009614 }
9615
Gilles Peskine449bd832023-01-11 14:50:10 +01009616 return 0;
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009617}
Xiaokang Qian03409292022-10-12 02:49:52 +00009618#endif /* MBEDTLS_SSL_PROTO_TLS1_3 &&
9619 MBEDTLS_SSL_SESSION_TICKETS &&
Xiaokang Qianed0620c2022-10-12 06:58:13 +00009620 MBEDTLS_SSL_SERVER_NAME_INDICATION &&
Xiaokang Qianed3afcd2022-10-12 08:31:11 +00009621 MBEDTLS_SSL_CLI_C */
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009622
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009623#endif /* MBEDTLS_SSL_TLS_C */