blob: 5d8a761db085812f8e2476d702f0fabd1d249834 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01002 * TLS shared functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakker5121ce52009-01-03 21:22:43 +000018 */
19/*
Paul Bakker5121ce52009-01-03 21:22:43 +000020 * http://www.ietf.org/rfc/rfc2246.txt
21 * http://www.ietf.org/rfc/rfc4346.txt
22 */
23
Gilles Peskinedb09ef62020-06-03 01:43:33 +020024#include "common.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#if defined(MBEDTLS_SSL_TLS_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000027
Jerry Yub476a442022-01-21 18:14:45 +080028#include <assert.h>
29
SimonBd5800b72016-04-26 07:43:27 +010030#include "mbedtls/platform.h"
SimonBd5800b72016-04-26 07:43:27 +010031
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000032#include "mbedtls/ssl.h"
Ronald Cron9f0fba32022-02-10 16:45:15 +010033#include "ssl_client.h"
Ronald Cron27c85e72022-03-08 11:37:55 +010034#include "ssl_debug_helpers.h"
Chris Jones84a773f2021-03-05 18:38:47 +000035#include "ssl_misc.h"
Andrzej Kurek25f27152022-08-17 16:09:31 -040036
Janos Follath73c616b2019-12-18 15:07:04 +000037#include "mbedtls/debug.h"
38#include "mbedtls/error.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050039#include "mbedtls/platform_util.h"
Hanno Beckera835da52019-05-16 12:39:07 +010040#include "mbedtls/version.h"
Gabor Mezei765862c2021-10-19 12:22:25 +020041#include "mbedtls/constant_time.h"
Paul Bakker0be444a2013-08-27 21:55:01 +020042
Rich Evans00ab4702015-02-06 13:43:58 +000043#include <string.h>
44
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050045#if defined(MBEDTLS_USE_PSA_CRYPTO)
46#include "mbedtls/psa_util.h"
47#include "psa/crypto.h"
48#endif
Manuel Pégourié-Gonnard07018f92022-09-15 11:29:35 +020049#include "mbedtls/legacy_or_psa.h"
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050050
Janos Follath23bdca02016-10-07 14:47:14 +010051#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000052#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020053#endif
54
Andrzej Kurek8a045ce2022-12-23 11:00:06 -050055#if defined(MBEDTLS_USE_PSA_CRYPTO)
56#define PSA_TO_MBEDTLS_ERR(status) PSA_TO_MBEDTLS_ERR_LIST(status, \
57 psa_to_ssl_errors, \
58 psa_generic_status_to_mbedtls)
Andrzej Kurekdaf5b562023-03-03 05:52:28 -050059#define PSA_TO_MD_ERR(status) PSA_TO_MBEDTLS_ERR_LIST(status, \
60 psa_to_md_errors, \
61 psa_generic_status_to_mbedtls)
Andrzej Kurek8a045ce2022-12-23 11:00:06 -050062#endif
63
Ronald Cronad8c17b2022-06-10 17:18:09 +020064#if defined(MBEDTLS_TEST_HOOKS)
65static mbedtls_ssl_chk_buf_ptr_args chk_buf_ptr_fail_args;
66
67void mbedtls_ssl_set_chk_buf_ptr_fail_args(
Gilles Peskine449bd832023-01-11 14:50:10 +010068 const uint8_t *cur, const uint8_t *end, size_t need)
Ronald Cronad8c17b2022-06-10 17:18:09 +020069{
70 chk_buf_ptr_fail_args.cur = cur;
71 chk_buf_ptr_fail_args.end = end;
72 chk_buf_ptr_fail_args.need = need;
73}
74
Gilles Peskine449bd832023-01-11 14:50:10 +010075void mbedtls_ssl_reset_chk_buf_ptr_fail_args(void)
Ronald Cronad8c17b2022-06-10 17:18:09 +020076{
Gilles Peskine449bd832023-01-11 14:50:10 +010077 memset(&chk_buf_ptr_fail_args, 0, sizeof(chk_buf_ptr_fail_args));
Ronald Cronad8c17b2022-06-10 17:18:09 +020078}
79
Gilles Peskine449bd832023-01-11 14:50:10 +010080int mbedtls_ssl_cmp_chk_buf_ptr_fail_args(mbedtls_ssl_chk_buf_ptr_args *args)
Ronald Cronad8c17b2022-06-10 17:18:09 +020081{
Gilles Peskine449bd832023-01-11 14:50:10 +010082 return (chk_buf_ptr_fail_args.cur != args->cur) ||
83 (chk_buf_ptr_fail_args.end != args->end) ||
84 (chk_buf_ptr_fail_args.need != args->need);
Ronald Cronad8c17b2022-06-10 17:18:09 +020085}
86#endif /* MBEDTLS_TEST_HOOKS */
87
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020088#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +010089
Hanno Beckera0e20d02019-05-15 14:03:01 +010090#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf8542cf2019-04-09 15:22:03 +010091/* Top-level Connection ID API */
92
Gilles Peskine449bd832023-01-11 14:50:10 +010093int mbedtls_ssl_conf_cid(mbedtls_ssl_config *conf,
94 size_t len,
95 int ignore_other_cid)
Hanno Beckerad4a1372019-05-03 13:06:44 +010096{
Gilles Peskine449bd832023-01-11 14:50:10 +010097 if (len > MBEDTLS_SSL_CID_IN_LEN_MAX) {
98 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
99 }
Hanno Beckerad4a1372019-05-03 13:06:44 +0100100
Gilles Peskine449bd832023-01-11 14:50:10 +0100101 if (ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_FAIL &&
102 ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE) {
103 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Becker611ac772019-05-14 11:45:26 +0100104 }
105
106 conf->ignore_unexpected_cid = ignore_other_cid;
Hanno Beckerad4a1372019-05-03 13:06:44 +0100107 conf->cid_len = len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100108 return 0;
Hanno Beckerad4a1372019-05-03 13:06:44 +0100109}
110
Gilles Peskine449bd832023-01-11 14:50:10 +0100111int mbedtls_ssl_set_cid(mbedtls_ssl_context *ssl,
112 int enable,
113 unsigned char const *own_cid,
114 size_t own_cid_len)
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100115{
Gilles Peskine449bd832023-01-11 14:50:10 +0100116 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
117 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
118 }
Hanno Becker76a79ab2019-05-03 14:38:32 +0100119
Hanno Beckerca092242019-04-25 16:01:49 +0100120 ssl->negotiate_cid = enable;
Gilles Peskine449bd832023-01-11 14:50:10 +0100121 if (enable == MBEDTLS_SSL_CID_DISABLED) {
122 MBEDTLS_SSL_DEBUG_MSG(3, ("Disable use of CID extension."));
123 return 0;
Hanno Beckerca092242019-04-25 16:01:49 +0100124 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100125 MBEDTLS_SSL_DEBUG_MSG(3, ("Enable use of CID extension."));
126 MBEDTLS_SSL_DEBUG_BUF(3, "Own CID", own_cid, own_cid_len);
Hanno Beckerca092242019-04-25 16:01:49 +0100127
Gilles Peskine449bd832023-01-11 14:50:10 +0100128 if (own_cid_len != ssl->conf->cid_len) {
129 MBEDTLS_SSL_DEBUG_MSG(3, ("CID length %u does not match CID length %u in config",
130 (unsigned) own_cid_len,
131 (unsigned) ssl->conf->cid_len));
132 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Beckerca092242019-04-25 16:01:49 +0100133 }
134
Gilles Peskine449bd832023-01-11 14:50:10 +0100135 memcpy(ssl->own_cid, own_cid, own_cid_len);
Hanno Beckerb7ee0cf2019-04-30 14:07:31 +0100136 /* Truncation is not an issue here because
137 * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */
138 ssl->own_cid_len = (uint8_t) own_cid_len;
Hanno Beckerca092242019-04-25 16:01:49 +0100139
Gilles Peskine449bd832023-01-11 14:50:10 +0100140 return 0;
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100141}
142
Gilles Peskine449bd832023-01-11 14:50:10 +0100143int mbedtls_ssl_get_own_cid(mbedtls_ssl_context *ssl,
144 int *enabled,
145 unsigned char own_cid[MBEDTLS_SSL_CID_OUT_LEN_MAX],
146 size_t *own_cid_len)
Paul Elliott0113cf12022-03-11 20:26:47 +0000147{
148 *enabled = MBEDTLS_SSL_CID_DISABLED;
149
Gilles Peskine449bd832023-01-11 14:50:10 +0100150 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
151 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
152 }
Paul Elliott0113cf12022-03-11 20:26:47 +0000153
154 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID length is
155 * zero as this is indistinguishable from not requesting to use
156 * the CID extension. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100157 if (ssl->own_cid_len == 0 || ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED) {
158 return 0;
159 }
Paul Elliott0113cf12022-03-11 20:26:47 +0000160
Gilles Peskine449bd832023-01-11 14:50:10 +0100161 if (own_cid_len != NULL) {
Paul Elliott0113cf12022-03-11 20:26:47 +0000162 *own_cid_len = ssl->own_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100163 if (own_cid != NULL) {
164 memcpy(own_cid, ssl->own_cid, ssl->own_cid_len);
165 }
Paul Elliott0113cf12022-03-11 20:26:47 +0000166 }
167
168 *enabled = MBEDTLS_SSL_CID_ENABLED;
169
Gilles Peskine449bd832023-01-11 14:50:10 +0100170 return 0;
Paul Elliott0113cf12022-03-11 20:26:47 +0000171}
172
Gilles Peskine449bd832023-01-11 14:50:10 +0100173int mbedtls_ssl_get_peer_cid(mbedtls_ssl_context *ssl,
174 int *enabled,
175 unsigned char peer_cid[MBEDTLS_SSL_CID_OUT_LEN_MAX],
176 size_t *peer_cid_len)
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100177{
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100178 *enabled = MBEDTLS_SSL_CID_DISABLED;
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100179
Gilles Peskine449bd832023-01-11 14:50:10 +0100180 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
181 mbedtls_ssl_is_handshake_over(ssl) == 0) {
182 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Becker76a79ab2019-05-03 14:38:32 +0100183 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100184
Hanno Beckerc5f24222019-05-03 12:54:52 +0100185 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions
186 * were used, but client and server requested the empty CID.
187 * This is indistinguishable from not using the CID extension
188 * in the first place. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100189 if (ssl->transform_in->in_cid_len == 0 &&
190 ssl->transform_in->out_cid_len == 0) {
191 return 0;
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100192 }
193
Gilles Peskine449bd832023-01-11 14:50:10 +0100194 if (peer_cid_len != NULL) {
Hanno Becker615ef172019-05-22 16:50:35 +0100195 *peer_cid_len = ssl->transform_in->out_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100196 if (peer_cid != NULL) {
197 memcpy(peer_cid, ssl->transform_in->out_cid,
198 ssl->transform_in->out_cid_len);
Hanno Becker615ef172019-05-22 16:50:35 +0100199 }
200 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100201
202 *enabled = MBEDTLS_SSL_CID_ENABLED;
203
Gilles Peskine449bd832023-01-11 14:50:10 +0100204 return 0;
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100205}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100206#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100207
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200208#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200209
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200210#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200211/*
212 * Convert max_fragment_length codes to length.
213 * RFC 6066 says:
214 * enum{
215 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
216 * } MaxFragmentLength;
217 * and we add 0 -> extension unused
218 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100219static unsigned int ssl_mfl_code_to_length(int mfl)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200220{
Gilles Peskine449bd832023-01-11 14:50:10 +0100221 switch (mfl) {
222 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
223 return MBEDTLS_TLS_EXT_ADV_CONTENT_LEN;
224 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
225 return 512;
226 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
227 return 1024;
228 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
229 return 2048;
230 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
231 return 4096;
232 default:
233 return MBEDTLS_TLS_EXT_ADV_CONTENT_LEN;
Angus Grattond8213d02016-05-25 20:56:48 +1000234 }
235}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200236#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200237
Gilles Peskine449bd832023-01-11 14:50:10 +0100238int mbedtls_ssl_session_copy(mbedtls_ssl_session *dst,
239 const mbedtls_ssl_session *src)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200240{
Gilles Peskine449bd832023-01-11 14:50:10 +0100241 mbedtls_ssl_session_free(dst);
242 memcpy(dst, src, sizeof(mbedtls_ssl_session));
吴敬辉0b716112021-11-29 10:46:35 +0800243#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
244 dst->ticket = NULL;
Xiaokang Qian87306442022-10-12 09:47:38 +0000245#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
246 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaokang Qian126bf8e2022-10-13 02:22:40 +0000247 dst->hostname = NULL;
吴敬辉0b716112021-11-29 10:46:35 +0800248#endif
Xiaokang Qian87306442022-10-12 09:47:38 +0000249#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
吴敬辉0b716112021-11-29 10:46:35 +0800250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200251#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker6d1986e2019-02-07 12:27:42 +0000252
253#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100254 if (src->peer_cert != NULL) {
Janos Follath865b3eb2019-12-16 11:46:15 +0000255 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker2292d1f2013-09-15 17:06:49 +0200256
Gilles Peskine449bd832023-01-11 14:50:10 +0100257 dst->peer_cert = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
258 if (dst->peer_cert == NULL) {
259 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
260 }
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200261
Gilles Peskine449bd832023-01-11 14:50:10 +0100262 mbedtls_x509_crt_init(dst->peer_cert);
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200263
Gilles Peskine449bd832023-01-11 14:50:10 +0100264 if ((ret = mbedtls_x509_crt_parse_der(dst->peer_cert, src->peer_cert->raw.p,
265 src->peer_cert->raw.len)) != 0) {
266 mbedtls_free(dst->peer_cert);
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200267 dst->peer_cert = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100268 return ret;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200269 }
270 }
Hanno Becker6d1986e2019-02-07 12:27:42 +0000271#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100272 if (src->peer_cert_digest != NULL) {
Hanno Becker9198ad12019-02-05 17:00:50 +0000273 dst->peer_cert_digest =
Gilles Peskine449bd832023-01-11 14:50:10 +0100274 mbedtls_calloc(1, src->peer_cert_digest_len);
275 if (dst->peer_cert_digest == NULL) {
276 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
277 }
Hanno Becker9198ad12019-02-05 17:00:50 +0000278
Gilles Peskine449bd832023-01-11 14:50:10 +0100279 memcpy(dst->peer_cert_digest, src->peer_cert_digest,
280 src->peer_cert_digest_len);
Hanno Becker9198ad12019-02-05 17:00:50 +0000281 dst->peer_cert_digest_type = src->peer_cert_digest_type;
Hanno Beckeraccc5992019-02-25 10:06:59 +0000282 dst->peer_cert_digest_len = src->peer_cert_digest_len;
Hanno Becker9198ad12019-02-05 17:00:50 +0000283 }
284#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
285
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200286#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200287
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200288#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100289 if (src->ticket != NULL) {
290 dst->ticket = mbedtls_calloc(1, src->ticket_len);
291 if (dst->ticket == NULL) {
292 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
293 }
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200294
Gilles Peskine449bd832023-01-11 14:50:10 +0100295 memcpy(dst->ticket, src->ticket, src->ticket_len);
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200296 }
Xiaokang Qian126bf8e2022-10-13 02:22:40 +0000297
298#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
299 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +0100300 if (src->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Xiaokang Qian126bf8e2022-10-13 02:22:40 +0000301 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100302 ret = mbedtls_ssl_session_set_hostname(dst, src->hostname);
303 if (ret != 0) {
304 return ret;
305 }
Xiaokang Qian126bf8e2022-10-13 02:22:40 +0000306 }
307#endif /* MBEDTLS_SSL_PROTO_TLS1_3 &&
308 MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200309#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200310
Gilles Peskine449bd832023-01-11 14:50:10 +0100311 return 0;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200312}
313
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500314#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200315MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100316static int resize_buffer(unsigned char **buffer, size_t len_new, size_t *len_old)
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500317{
Gilles Peskine449bd832023-01-11 14:50:10 +0100318 unsigned char *resized_buffer = mbedtls_calloc(1, len_new);
319 if (resized_buffer == NULL) {
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500320 return -1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100321 }
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500322
323 /* We want to copy len_new bytes when downsizing the buffer, and
324 * len_old bytes when upsizing, so we choose the smaller of two sizes,
325 * to fit one buffer into another. Size checks, ensuring that no data is
326 * lost, are done outside of this function. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100327 memcpy(resized_buffer, *buffer,
328 (len_new < *len_old) ? len_new : *len_old);
329 mbedtls_platform_zeroize(*buffer, *len_old);
330 mbedtls_free(*buffer);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500331
332 *buffer = resized_buffer;
333 *len_old = len_new;
334
335 return 0;
336}
Andrzej Kurek4a063792020-10-21 15:08:44 +0200337
Gilles Peskine449bd832023-01-11 14:50:10 +0100338static void handle_buffer_resizing(mbedtls_ssl_context *ssl, int downsizing,
339 size_t in_buf_new_len,
340 size_t out_buf_new_len)
Andrzej Kurek4a063792020-10-21 15:08:44 +0200341{
342 int modified = 0;
343 size_t written_in = 0, iv_offset_in = 0, len_offset_in = 0;
344 size_t written_out = 0, iv_offset_out = 0, len_offset_out = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100345 if (ssl->in_buf != NULL) {
Andrzej Kurek4a063792020-10-21 15:08:44 +0200346 written_in = ssl->in_msg - ssl->in_buf;
347 iv_offset_in = ssl->in_iv - ssl->in_buf;
348 len_offset_in = ssl->in_len - ssl->in_buf;
Gilles Peskine449bd832023-01-11 14:50:10 +0100349 if (downsizing ?
Andrzej Kurek4a063792020-10-21 15:08:44 +0200350 ssl->in_buf_len > in_buf_new_len && ssl->in_left < in_buf_new_len :
Gilles Peskine449bd832023-01-11 14:50:10 +0100351 ssl->in_buf_len < in_buf_new_len) {
352 if (resize_buffer(&ssl->in_buf, in_buf_new_len, &ssl->in_buf_len) != 0) {
353 MBEDTLS_SSL_DEBUG_MSG(1, ("input buffer resizing failed - out of memory"));
354 } else {
355 MBEDTLS_SSL_DEBUG_MSG(2, ("Reallocating in_buf to %" MBEDTLS_PRINTF_SIZET,
356 in_buf_new_len));
Andrzej Kurek4a063792020-10-21 15:08:44 +0200357 modified = 1;
358 }
359 }
360 }
361
Gilles Peskine449bd832023-01-11 14:50:10 +0100362 if (ssl->out_buf != NULL) {
Andrzej Kurek4a063792020-10-21 15:08:44 +0200363 written_out = ssl->out_msg - ssl->out_buf;
364 iv_offset_out = ssl->out_iv - ssl->out_buf;
365 len_offset_out = ssl->out_len - ssl->out_buf;
Gilles Peskine449bd832023-01-11 14:50:10 +0100366 if (downsizing ?
Andrzej Kurek4a063792020-10-21 15:08:44 +0200367 ssl->out_buf_len > out_buf_new_len && ssl->out_left < out_buf_new_len :
Gilles Peskine449bd832023-01-11 14:50:10 +0100368 ssl->out_buf_len < out_buf_new_len) {
369 if (resize_buffer(&ssl->out_buf, out_buf_new_len, &ssl->out_buf_len) != 0) {
370 MBEDTLS_SSL_DEBUG_MSG(1, ("output buffer resizing failed - out of memory"));
371 } else {
372 MBEDTLS_SSL_DEBUG_MSG(2, ("Reallocating out_buf to %" MBEDTLS_PRINTF_SIZET,
373 out_buf_new_len));
Andrzej Kurek4a063792020-10-21 15:08:44 +0200374 modified = 1;
375 }
376 }
377 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100378 if (modified) {
Andrzej Kurek4a063792020-10-21 15:08:44 +0200379 /* Update pointers here to avoid doing it twice. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100380 mbedtls_ssl_reset_in_out_pointers(ssl);
Andrzej Kurek4a063792020-10-21 15:08:44 +0200381 /* Fields below might not be properly updated with record
382 * splitting or with CID, so they are manually updated here. */
383 ssl->out_msg = ssl->out_buf + written_out;
384 ssl->out_len = ssl->out_buf + len_offset_out;
385 ssl->out_iv = ssl->out_buf + iv_offset_out;
386
387 ssl->in_msg = ssl->in_buf + written_in;
388 ssl->in_len = ssl->in_buf + len_offset_in;
389 ssl->in_iv = ssl->in_buf + iv_offset_in;
390 }
391}
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500392#endif /* MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH */
393
Jerry Yudb8c48a2022-01-27 14:54:54 +0800394#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yued14c932022-02-17 13:40:45 +0800395
396#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Gilles Peskine449bd832023-01-11 14:50:10 +0100397typedef int (*tls_prf_fn)(const unsigned char *secret, size_t slen,
398 const char *label,
399 const unsigned char *random, size_t rlen,
400 unsigned char *dstbuf, size_t dlen);
Jerry Yued14c932022-02-17 13:40:45 +0800401
Gilles Peskine449bd832023-01-11 14:50:10 +0100402static tls_prf_fn ssl_tls12prf_from_cs(int ciphersuite_id);
Jerry Yued14c932022-02-17 13:40:45 +0800403
404#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
405
406/* Type for the TLS PRF */
407typedef int ssl_tls_prf_t(const unsigned char *, size_t, const char *,
408 const unsigned char *, size_t,
409 unsigned char *, size_t);
410
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200411MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100412static int ssl_tls12_populate_transform(mbedtls_ssl_transform *transform,
413 int ciphersuite,
414 const unsigned char master[48],
Neil Armstrongf2c82f02022-04-05 11:16:53 +0200415#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +0100416 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +0200417#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +0100418 ssl_tls_prf_t tls_prf,
419 const unsigned char randbytes[64],
420 mbedtls_ssl_protocol_version tls_version,
421 unsigned endpoint,
422 const mbedtls_ssl_context *ssl);
Jerry Yued14c932022-02-17 13:40:45 +0800423
Andrzej Kurek25f27152022-08-17 16:09:31 -0400424#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200425MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100426static int tls_prf_sha256(const unsigned char *secret, size_t slen,
Ron Eldor51d3ab52019-05-12 14:54:30 +0300427 const char *label,
428 const unsigned char *random, size_t rlen,
Gilles Peskine449bd832023-01-11 14:50:10 +0100429 unsigned char *dstbuf, size_t dlen);
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100430static int ssl_calc_verify_tls_sha256(const mbedtls_ssl_context *, unsigned char *, size_t *);
431static int ssl_calc_finished_tls_sha256(mbedtls_ssl_context *, unsigned char *, int);
Gilles Peskine449bd832023-01-11 14:50:10 +0100432
433#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
434
435#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
436MBEDTLS_CHECK_RETURN_CRITICAL
437static int tls_prf_sha384(const unsigned char *secret, size_t slen,
438 const char *label,
439 const unsigned char *random, size_t rlen,
440 unsigned char *dstbuf, size_t dlen);
441
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100442static int ssl_calc_verify_tls_sha384(const mbedtls_ssl_context *, unsigned char *, size_t *);
443static int ssl_calc_finished_tls_sha384(mbedtls_ssl_context *, unsigned char *, int);
Gilles Peskine449bd832023-01-11 14:50:10 +0100444#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
445
446static size_t ssl_tls12_session_save(const mbedtls_ssl_session *session,
447 unsigned char *buf,
448 size_t buf_len);
449
450MBEDTLS_CHECK_RETURN_CRITICAL
451static int ssl_tls12_session_load(mbedtls_ssl_session *session,
452 const unsigned char *buf,
453 size_t len);
454#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
455
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100456static int ssl_update_checksum_start(mbedtls_ssl_context *, const unsigned char *, size_t);
Gilles Peskine449bd832023-01-11 14:50:10 +0100457
458#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100459static int ssl_update_checksum_sha256(mbedtls_ssl_context *, const unsigned char *, size_t);
Gilles Peskine449bd832023-01-11 14:50:10 +0100460#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
461
462#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100463static int ssl_update_checksum_sha384(mbedtls_ssl_context *, const unsigned char *, size_t);
Gilles Peskine449bd832023-01-11 14:50:10 +0100464#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
465
466int mbedtls_ssl_tls_prf(const mbedtls_tls_prf_types prf,
467 const unsigned char *secret, size_t slen,
468 const char *label,
469 const unsigned char *random, size_t rlen,
470 unsigned char *dstbuf, size_t dlen)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300471{
472 mbedtls_ssl_tls_prf_cb *tls_prf = NULL;
473
Gilles Peskine449bd832023-01-11 14:50:10 +0100474 switch (prf) {
Ron Eldord2f25f72019-05-15 14:54:22 +0300475#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek25f27152022-08-17 16:09:31 -0400476#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300477 case MBEDTLS_SSL_TLS_PRF_SHA384:
478 tls_prf = tls_prf_sha384;
Gilles Peskine449bd832023-01-11 14:50:10 +0100479 break;
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400480#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -0400481#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300482 case MBEDTLS_SSL_TLS_PRF_SHA256:
483 tls_prf = tls_prf_sha256;
Gilles Peskine449bd832023-01-11 14:50:10 +0100484 break;
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400485#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Ron Eldord2f25f72019-05-15 14:54:22 +0300486#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100487 default:
488 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Ron Eldor51d3ab52019-05-12 14:54:30 +0300489 }
490
Gilles Peskine449bd832023-01-11 14:50:10 +0100491 return tls_prf(secret, slen, label, random, rlen, dstbuf, dlen);
Ron Eldor51d3ab52019-05-12 14:54:30 +0300492}
493
Jerry Yuc73c6182022-02-08 20:29:25 +0800494#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100495static void ssl_clear_peer_cert(mbedtls_ssl_session *session)
Jerry Yuc73c6182022-02-08 20:29:25 +0800496{
497#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100498 if (session->peer_cert != NULL) {
499 mbedtls_x509_crt_free(session->peer_cert);
500 mbedtls_free(session->peer_cert);
Jerry Yuc73c6182022-02-08 20:29:25 +0800501 session->peer_cert = NULL;
502 }
503#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100504 if (session->peer_cert_digest != NULL) {
Jerry Yuc73c6182022-02-08 20:29:25 +0800505 /* Zeroization is not necessary. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100506 mbedtls_free(session->peer_cert_digest);
Jerry Yuc73c6182022-02-08 20:29:25 +0800507 session->peer_cert_digest = NULL;
508 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
509 session->peer_cert_digest_len = 0;
510 }
511#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
512}
513#endif /* MBEDTLS_X509_CRT_PARSE_C */
514
Gilles Peskine449bd832023-01-11 14:50:10 +0100515uint32_t mbedtls_ssl_get_extension_id(unsigned int extension_type)
Jerry Yu7a485c12022-10-31 13:08:18 +0800516{
Gilles Peskine449bd832023-01-11 14:50:10 +0100517 switch (extension_type) {
Jerry Yu7a485c12022-10-31 13:08:18 +0800518 case MBEDTLS_TLS_EXT_SERVERNAME:
Gilles Peskine449bd832023-01-11 14:50:10 +0100519 return MBEDTLS_SSL_EXT_ID_SERVERNAME;
Jerry Yu7a485c12022-10-31 13:08:18 +0800520
521 case MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100522 return MBEDTLS_SSL_EXT_ID_MAX_FRAGMENT_LENGTH;
Jerry Yu7a485c12022-10-31 13:08:18 +0800523
524 case MBEDTLS_TLS_EXT_STATUS_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +0100525 return MBEDTLS_SSL_EXT_ID_STATUS_REQUEST;
Jerry Yu7a485c12022-10-31 13:08:18 +0800526
527 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
Gilles Peskine449bd832023-01-11 14:50:10 +0100528 return MBEDTLS_SSL_EXT_ID_SUPPORTED_GROUPS;
Jerry Yu7a485c12022-10-31 13:08:18 +0800529
530 case MBEDTLS_TLS_EXT_SIG_ALG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100531 return MBEDTLS_SSL_EXT_ID_SIG_ALG;
Jerry Yu7a485c12022-10-31 13:08:18 +0800532
533 case MBEDTLS_TLS_EXT_USE_SRTP:
Gilles Peskine449bd832023-01-11 14:50:10 +0100534 return MBEDTLS_SSL_EXT_ID_USE_SRTP;
Jerry Yu7a485c12022-10-31 13:08:18 +0800535
536 case MBEDTLS_TLS_EXT_HEARTBEAT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100537 return MBEDTLS_SSL_EXT_ID_HEARTBEAT;
Jerry Yu7a485c12022-10-31 13:08:18 +0800538
539 case MBEDTLS_TLS_EXT_ALPN:
Gilles Peskine449bd832023-01-11 14:50:10 +0100540 return MBEDTLS_SSL_EXT_ID_ALPN;
Jerry Yu7a485c12022-10-31 13:08:18 +0800541
542 case MBEDTLS_TLS_EXT_SCT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100543 return MBEDTLS_SSL_EXT_ID_SCT;
Jerry Yu7a485c12022-10-31 13:08:18 +0800544
545 case MBEDTLS_TLS_EXT_CLI_CERT_TYPE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100546 return MBEDTLS_SSL_EXT_ID_CLI_CERT_TYPE;
Jerry Yu7a485c12022-10-31 13:08:18 +0800547
548 case MBEDTLS_TLS_EXT_SERV_CERT_TYPE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100549 return MBEDTLS_SSL_EXT_ID_SERV_CERT_TYPE;
Jerry Yu7a485c12022-10-31 13:08:18 +0800550
551 case MBEDTLS_TLS_EXT_PADDING:
Gilles Peskine449bd832023-01-11 14:50:10 +0100552 return MBEDTLS_SSL_EXT_ID_PADDING;
Jerry Yu7a485c12022-10-31 13:08:18 +0800553
554 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100555 return MBEDTLS_SSL_EXT_ID_PRE_SHARED_KEY;
Jerry Yu7a485c12022-10-31 13:08:18 +0800556
557 case MBEDTLS_TLS_EXT_EARLY_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100558 return MBEDTLS_SSL_EXT_ID_EARLY_DATA;
Jerry Yu7a485c12022-10-31 13:08:18 +0800559
560 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
Gilles Peskine449bd832023-01-11 14:50:10 +0100561 return MBEDTLS_SSL_EXT_ID_SUPPORTED_VERSIONS;
Jerry Yu7a485c12022-10-31 13:08:18 +0800562
563 case MBEDTLS_TLS_EXT_COOKIE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100564 return MBEDTLS_SSL_EXT_ID_COOKIE;
Jerry Yu7a485c12022-10-31 13:08:18 +0800565
566 case MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES:
Gilles Peskine449bd832023-01-11 14:50:10 +0100567 return MBEDTLS_SSL_EXT_ID_PSK_KEY_EXCHANGE_MODES;
Jerry Yu7a485c12022-10-31 13:08:18 +0800568
569 case MBEDTLS_TLS_EXT_CERT_AUTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100570 return MBEDTLS_SSL_EXT_ID_CERT_AUTH;
Jerry Yu7a485c12022-10-31 13:08:18 +0800571
572 case MBEDTLS_TLS_EXT_OID_FILTERS:
Gilles Peskine449bd832023-01-11 14:50:10 +0100573 return MBEDTLS_SSL_EXT_ID_OID_FILTERS;
Jerry Yu7a485c12022-10-31 13:08:18 +0800574
575 case MBEDTLS_TLS_EXT_POST_HANDSHAKE_AUTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100576 return MBEDTLS_SSL_EXT_ID_POST_HANDSHAKE_AUTH;
Jerry Yu7a485c12022-10-31 13:08:18 +0800577
578 case MBEDTLS_TLS_EXT_SIG_ALG_CERT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100579 return MBEDTLS_SSL_EXT_ID_SIG_ALG_CERT;
Jerry Yu7a485c12022-10-31 13:08:18 +0800580
581 case MBEDTLS_TLS_EXT_KEY_SHARE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100582 return MBEDTLS_SSL_EXT_ID_KEY_SHARE;
Jerry Yu7a485c12022-10-31 13:08:18 +0800583
584 case MBEDTLS_TLS_EXT_TRUNCATED_HMAC:
Gilles Peskine449bd832023-01-11 14:50:10 +0100585 return MBEDTLS_SSL_EXT_ID_TRUNCATED_HMAC;
Jerry Yu7a485c12022-10-31 13:08:18 +0800586
587 case MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS:
Gilles Peskine449bd832023-01-11 14:50:10 +0100588 return MBEDTLS_SSL_EXT_ID_SUPPORTED_POINT_FORMATS;
Jerry Yu7a485c12022-10-31 13:08:18 +0800589
590 case MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC:
Gilles Peskine449bd832023-01-11 14:50:10 +0100591 return MBEDTLS_SSL_EXT_ID_ENCRYPT_THEN_MAC;
Jerry Yu7a485c12022-10-31 13:08:18 +0800592
593 case MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +0100594 return MBEDTLS_SSL_EXT_ID_EXTENDED_MASTER_SECRET;
Jerry Yu7a485c12022-10-31 13:08:18 +0800595
596 case MBEDTLS_TLS_EXT_SESSION_TICKET:
Gilles Peskine449bd832023-01-11 14:50:10 +0100597 return MBEDTLS_SSL_EXT_ID_SESSION_TICKET;
Jerry Yu7a485c12022-10-31 13:08:18 +0800598
599 }
600
Gilles Peskine449bd832023-01-11 14:50:10 +0100601 return MBEDTLS_SSL_EXT_ID_UNRECOGNIZED;
Jerry Yu7a485c12022-10-31 13:08:18 +0800602}
603
Gilles Peskine449bd832023-01-11 14:50:10 +0100604uint32_t mbedtls_ssl_get_extension_mask(unsigned int extension_type)
Jerry Yu7a485c12022-10-31 13:08:18 +0800605{
Gilles Peskine449bd832023-01-11 14:50:10 +0100606 return 1 << mbedtls_ssl_get_extension_id(extension_type);
Jerry Yu7a485c12022-10-31 13:08:18 +0800607}
608
Jerry Yud25cab02022-10-31 12:48:30 +0800609#if defined(MBEDTLS_DEBUG_C)
610static const char *extension_name_table[] = {
Jerry Yuea52ed92022-11-08 21:01:17 +0800611 [MBEDTLS_SSL_EXT_ID_UNRECOGNIZED] = "unrecognized",
Jerry Yud25cab02022-10-31 12:48:30 +0800612 [MBEDTLS_SSL_EXT_ID_SERVERNAME] = "server_name",
613 [MBEDTLS_SSL_EXT_ID_MAX_FRAGMENT_LENGTH] = "max_fragment_length",
614 [MBEDTLS_SSL_EXT_ID_STATUS_REQUEST] = "status_request",
615 [MBEDTLS_SSL_EXT_ID_SUPPORTED_GROUPS] = "supported_groups",
616 [MBEDTLS_SSL_EXT_ID_SIG_ALG] = "signature_algorithms",
617 [MBEDTLS_SSL_EXT_ID_USE_SRTP] = "use_srtp",
618 [MBEDTLS_SSL_EXT_ID_HEARTBEAT] = "heartbeat",
619 [MBEDTLS_SSL_EXT_ID_ALPN] = "application_layer_protocol_negotiation",
620 [MBEDTLS_SSL_EXT_ID_SCT] = "signed_certificate_timestamp",
621 [MBEDTLS_SSL_EXT_ID_CLI_CERT_TYPE] = "client_certificate_type",
622 [MBEDTLS_SSL_EXT_ID_SERV_CERT_TYPE] = "server_certificate_type",
623 [MBEDTLS_SSL_EXT_ID_PADDING] = "padding",
624 [MBEDTLS_SSL_EXT_ID_PRE_SHARED_KEY] = "pre_shared_key",
625 [MBEDTLS_SSL_EXT_ID_EARLY_DATA] = "early_data",
626 [MBEDTLS_SSL_EXT_ID_SUPPORTED_VERSIONS] = "supported_versions",
627 [MBEDTLS_SSL_EXT_ID_COOKIE] = "cookie",
628 [MBEDTLS_SSL_EXT_ID_PSK_KEY_EXCHANGE_MODES] = "psk_key_exchange_modes",
629 [MBEDTLS_SSL_EXT_ID_CERT_AUTH] = "certificate_authorities",
630 [MBEDTLS_SSL_EXT_ID_OID_FILTERS] = "oid_filters",
631 [MBEDTLS_SSL_EXT_ID_POST_HANDSHAKE_AUTH] = "post_handshake_auth",
632 [MBEDTLS_SSL_EXT_ID_SIG_ALG_CERT] = "signature_algorithms_cert",
633 [MBEDTLS_SSL_EXT_ID_KEY_SHARE] = "key_share",
634 [MBEDTLS_SSL_EXT_ID_TRUNCATED_HMAC] = "truncated_hmac",
635 [MBEDTLS_SSL_EXT_ID_SUPPORTED_POINT_FORMATS] = "supported_point_formats",
636 [MBEDTLS_SSL_EXT_ID_ENCRYPT_THEN_MAC] = "encrypt_then_mac",
637 [MBEDTLS_SSL_EXT_ID_EXTENDED_MASTER_SECRET] = "extended_master_secret",
638 [MBEDTLS_SSL_EXT_ID_SESSION_TICKET] = "session_ticket"
639};
640
Gilles Peskine449bd832023-01-11 14:50:10 +0100641static unsigned int extension_type_table[] = {
Jerry Yud25cab02022-10-31 12:48:30 +0800642 [MBEDTLS_SSL_EXT_ID_UNRECOGNIZED] = 0xff,
643 [MBEDTLS_SSL_EXT_ID_SERVERNAME] = MBEDTLS_TLS_EXT_SERVERNAME,
644 [MBEDTLS_SSL_EXT_ID_MAX_FRAGMENT_LENGTH] = MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH,
645 [MBEDTLS_SSL_EXT_ID_STATUS_REQUEST] = MBEDTLS_TLS_EXT_STATUS_REQUEST,
646 [MBEDTLS_SSL_EXT_ID_SUPPORTED_GROUPS] = MBEDTLS_TLS_EXT_SUPPORTED_GROUPS,
647 [MBEDTLS_SSL_EXT_ID_SIG_ALG] = MBEDTLS_TLS_EXT_SIG_ALG,
648 [MBEDTLS_SSL_EXT_ID_USE_SRTP] = MBEDTLS_TLS_EXT_USE_SRTP,
649 [MBEDTLS_SSL_EXT_ID_HEARTBEAT] = MBEDTLS_TLS_EXT_HEARTBEAT,
650 [MBEDTLS_SSL_EXT_ID_ALPN] = MBEDTLS_TLS_EXT_ALPN,
651 [MBEDTLS_SSL_EXT_ID_SCT] = MBEDTLS_TLS_EXT_SCT,
652 [MBEDTLS_SSL_EXT_ID_CLI_CERT_TYPE] = MBEDTLS_TLS_EXT_CLI_CERT_TYPE,
653 [MBEDTLS_SSL_EXT_ID_SERV_CERT_TYPE] = MBEDTLS_TLS_EXT_SERV_CERT_TYPE,
654 [MBEDTLS_SSL_EXT_ID_PADDING] = MBEDTLS_TLS_EXT_PADDING,
655 [MBEDTLS_SSL_EXT_ID_PRE_SHARED_KEY] = MBEDTLS_TLS_EXT_PRE_SHARED_KEY,
656 [MBEDTLS_SSL_EXT_ID_EARLY_DATA] = MBEDTLS_TLS_EXT_EARLY_DATA,
657 [MBEDTLS_SSL_EXT_ID_SUPPORTED_VERSIONS] = MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS,
658 [MBEDTLS_SSL_EXT_ID_COOKIE] = MBEDTLS_TLS_EXT_COOKIE,
659 [MBEDTLS_SSL_EXT_ID_PSK_KEY_EXCHANGE_MODES] = MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES,
660 [MBEDTLS_SSL_EXT_ID_CERT_AUTH] = MBEDTLS_TLS_EXT_CERT_AUTH,
661 [MBEDTLS_SSL_EXT_ID_OID_FILTERS] = MBEDTLS_TLS_EXT_OID_FILTERS,
662 [MBEDTLS_SSL_EXT_ID_POST_HANDSHAKE_AUTH] = MBEDTLS_TLS_EXT_POST_HANDSHAKE_AUTH,
663 [MBEDTLS_SSL_EXT_ID_SIG_ALG_CERT] = MBEDTLS_TLS_EXT_SIG_ALG_CERT,
664 [MBEDTLS_SSL_EXT_ID_KEY_SHARE] = MBEDTLS_TLS_EXT_KEY_SHARE,
665 [MBEDTLS_SSL_EXT_ID_TRUNCATED_HMAC] = MBEDTLS_TLS_EXT_TRUNCATED_HMAC,
666 [MBEDTLS_SSL_EXT_ID_SUPPORTED_POINT_FORMATS] = MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS,
667 [MBEDTLS_SSL_EXT_ID_ENCRYPT_THEN_MAC] = MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC,
668 [MBEDTLS_SSL_EXT_ID_EXTENDED_MASTER_SECRET] = MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET,
669 [MBEDTLS_SSL_EXT_ID_SESSION_TICKET] = MBEDTLS_TLS_EXT_SESSION_TICKET
670};
671
Gilles Peskine449bd832023-01-11 14:50:10 +0100672const char *mbedtls_ssl_get_extension_name(unsigned int extension_type)
Jerry Yud25cab02022-10-31 12:48:30 +0800673{
Gilles Peskine449bd832023-01-11 14:50:10 +0100674 return extension_name_table[
675 mbedtls_ssl_get_extension_id(extension_type)];
Jerry Yud25cab02022-10-31 12:48:30 +0800676}
677
Gilles Peskine449bd832023-01-11 14:50:10 +0100678static const char *ssl_tls13_get_hs_msg_name(int hs_msg_type)
Jerry Yud25cab02022-10-31 12:48:30 +0800679{
Gilles Peskine449bd832023-01-11 14:50:10 +0100680 switch (hs_msg_type) {
Jerry Yud25cab02022-10-31 12:48:30 +0800681 case MBEDTLS_SSL_HS_CLIENT_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +0100682 return "ClientHello";
Jerry Yud25cab02022-10-31 12:48:30 +0800683 case MBEDTLS_SSL_HS_SERVER_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +0100684 return "ServerHello";
Jerry Yud25cab02022-10-31 12:48:30 +0800685 case MBEDTLS_SSL_TLS1_3_HS_HELLO_RETRY_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +0100686 return "HelloRetryRequest";
Jerry Yud25cab02022-10-31 12:48:30 +0800687 case MBEDTLS_SSL_HS_NEW_SESSION_TICKET:
Gilles Peskine449bd832023-01-11 14:50:10 +0100688 return "NewSessionTicket";
Jerry Yud25cab02022-10-31 12:48:30 +0800689 case MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS:
Gilles Peskine449bd832023-01-11 14:50:10 +0100690 return "EncryptedExtensions";
Jerry Yud25cab02022-10-31 12:48:30 +0800691 case MBEDTLS_SSL_HS_CERTIFICATE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100692 return "Certificate";
Jerry Yud25cab02022-10-31 12:48:30 +0800693 case MBEDTLS_SSL_HS_CERTIFICATE_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +0100694 return "CertificateRequest";
Jerry Yud25cab02022-10-31 12:48:30 +0800695 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100696 return "Unknown";
Jerry Yud25cab02022-10-31 12:48:30 +0800697}
698
Gilles Peskine449bd832023-01-11 14:50:10 +0100699void mbedtls_ssl_print_extension(const mbedtls_ssl_context *ssl,
700 int level, const char *file, int line,
701 int hs_msg_type, unsigned int extension_type,
702 const char *extra_msg0, const char *extra_msg1)
Jerry Yud25cab02022-10-31 12:48:30 +0800703{
704 const char *extra_msg;
Gilles Peskine449bd832023-01-11 14:50:10 +0100705 if (extra_msg0 && extra_msg1) {
Jerry Yud25cab02022-10-31 12:48:30 +0800706 mbedtls_debug_print_msg(
707 ssl, level, file, line,
708 "%s: %s(%u) extension %s %s.",
Gilles Peskine449bd832023-01-11 14:50:10 +0100709 ssl_tls13_get_hs_msg_name(hs_msg_type),
710 mbedtls_ssl_get_extension_name(extension_type),
Jerry Yud25cab02022-10-31 12:48:30 +0800711 extension_type,
Gilles Peskine449bd832023-01-11 14:50:10 +0100712 extra_msg0, extra_msg1);
Jerry Yud25cab02022-10-31 12:48:30 +0800713 return;
714 }
715
716 extra_msg = extra_msg0 ? extra_msg0 : extra_msg1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100717 if (extra_msg) {
Jerry Yud25cab02022-10-31 12:48:30 +0800718 mbedtls_debug_print_msg(
719 ssl, level, file, line,
Gilles Peskine449bd832023-01-11 14:50:10 +0100720 "%s: %s(%u) extension %s.", ssl_tls13_get_hs_msg_name(hs_msg_type),
721 mbedtls_ssl_get_extension_name(extension_type), extension_type,
722 extra_msg);
Jerry Yud25cab02022-10-31 12:48:30 +0800723 return;
724 }
725
726 mbedtls_debug_print_msg(
727 ssl, level, file, line,
Gilles Peskine449bd832023-01-11 14:50:10 +0100728 "%s: %s(%u) extension.", ssl_tls13_get_hs_msg_name(hs_msg_type),
729 mbedtls_ssl_get_extension_name(extension_type), extension_type);
Jerry Yud25cab02022-10-31 12:48:30 +0800730}
731
Gilles Peskine449bd832023-01-11 14:50:10 +0100732void mbedtls_ssl_print_extensions(const mbedtls_ssl_context *ssl,
733 int level, const char *file, int line,
734 int hs_msg_type, uint32_t extensions_mask,
735 const char *extra)
Jerry Yud25cab02022-10-31 12:48:30 +0800736{
737
Gilles Peskine449bd832023-01-11 14:50:10 +0100738 for (unsigned i = 0;
739 i < sizeof(extension_name_table) / sizeof(extension_name_table[0]);
740 i++) {
Jerry Yu79aa7212022-11-08 21:30:21 +0800741 mbedtls_ssl_print_extension(
Jerry Yuea52ed92022-11-08 21:01:17 +0800742 ssl, level, file, line, hs_msg_type, extension_type_table[i],
Gilles Peskine449bd832023-01-11 14:50:10 +0100743 extensions_mask & (1 << i) ? "exists" : "does not exist", extra);
Jerry Yud25cab02022-10-31 12:48:30 +0800744 }
745}
746
Pengyu Lvee455c02023-01-12 14:37:24 +0800747#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS)
748#define ARRAY_LENGTH(a) (sizeof(a) / sizeof(*(a)))
749
750static const char *ticket_flag_name_table[] =
751{
752 [0] = "ALLOW_PSK_RESUMPTION",
753 [2] = "ALLOW_PSK_EPHEMERAL_RESUMPTION",
754 [3] = "ALLOW_EARLY_DATA",
755};
756
Pengyu Lv4938a562023-01-16 11:28:49 +0800757void mbedtls_ssl_print_ticket_flags(const mbedtls_ssl_context *ssl,
758 int level, const char *file, int line,
759 unsigned int flags)
Pengyu Lvee455c02023-01-12 14:37:24 +0800760{
761 size_t i;
762
763 mbedtls_debug_print_msg(ssl, level, file, line,
Pengyu Lv4938a562023-01-16 11:28:49 +0800764 "print ticket_flags (0x%02x)", flags);
765
766 flags = flags & MBEDTLS_SSL_TLS1_3_TICKET_FLAGS_MASK;
Pengyu Lvee455c02023-01-12 14:37:24 +0800767
768 for (i = 0; i < ARRAY_LENGTH(ticket_flag_name_table); i++) {
Pengyu Lv4938a562023-01-16 11:28:49 +0800769 if ((flags & (1 << i))) {
Pengyu Lvee455c02023-01-12 14:37:24 +0800770 mbedtls_debug_print_msg(ssl, level, file, line, "- %s is set.",
771 ticket_flag_name_table[i]);
772 }
773 }
774}
775#endif /* MBEDTLS_SSL_PROTO_TLS1_3 && MBEDTLS_SSL_SESSION_TICKETS */
776
Jerry Yud25cab02022-10-31 12:48:30 +0800777#endif /* MBEDTLS_DEBUG_C */
778
Gilles Peskine449bd832023-01-11 14:50:10 +0100779void mbedtls_ssl_optimize_checksum(mbedtls_ssl_context *ssl,
780 const mbedtls_ssl_ciphersuite_t *ciphersuite_info)
Jerry Yuc73c6182022-02-08 20:29:25 +0800781{
782 ((void) ciphersuite_info);
783
Andrzej Kurek25f27152022-08-17 16:09:31 -0400784#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +0100785 if (ciphersuite_info->mac == MBEDTLS_MD_SHA384) {
Jerry Yuc73c6182022-02-08 20:29:25 +0800786 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
Gilles Peskine449bd832023-01-11 14:50:10 +0100787 } else
Jerry Yuc73c6182022-02-08 20:29:25 +0800788#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400789#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +0100790 if (ciphersuite_info->mac != MBEDTLS_MD_SHA384) {
Jerry Yuc73c6182022-02-08 20:29:25 +0800791 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Gilles Peskine449bd832023-01-11 14:50:10 +0100792 } else
Jerry Yuc73c6182022-02-08 20:29:25 +0800793#endif
794 {
Gilles Peskine449bd832023-01-11 14:50:10 +0100795 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
Jerry Yuc73c6182022-02-08 20:29:25 +0800796 return;
797 }
798}
799
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100800int mbedtls_ssl_add_hs_hdr_to_checksum(mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100801 unsigned hs_type,
802 size_t total_hs_len)
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100803{
804 unsigned char hs_hdr[4];
805
806 /* Build HS header for checksum update. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100807 hs_hdr[0] = MBEDTLS_BYTE_0(hs_type);
808 hs_hdr[1] = MBEDTLS_BYTE_2(total_hs_len);
809 hs_hdr[2] = MBEDTLS_BYTE_1(total_hs_len);
810 hs_hdr[3] = MBEDTLS_BYTE_0(total_hs_len);
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100811
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100812 return ssl->handshake->update_checksum(ssl, hs_hdr, sizeof(hs_hdr));
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100813}
814
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100815int mbedtls_ssl_add_hs_msg_to_checksum(mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100816 unsigned hs_type,
817 unsigned char const *msg,
818 size_t msg_len)
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100819{
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100820 int ret;
821 ret = mbedtls_ssl_add_hs_hdr_to_checksum(ssl, hs_type, msg_len);
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100822 if (ret != 0) {
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100823 return ret;
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100824 }
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100825 return ssl->handshake->update_checksum(ssl, msg, msg_len);
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100826}
827
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100828int mbedtls_ssl_reset_checksum(mbedtls_ssl_context *ssl)
Jerry Yuc73c6182022-02-08 20:29:25 +0800829{
Manuel Pégourié-Gonnard626aaed2023-02-06 22:03:06 +0100830#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) || \
831 defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100832#if defined(MBEDTLS_USE_PSA_CRYPTO)
833 psa_status_t status;
834#else
835 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
836#endif
Manuel Pégourié-Gonnard626aaed2023-02-06 22:03:06 +0100837#else /* SHA-256 or SHA-384 */
Jerry Yuc73c6182022-02-08 20:29:25 +0800838 ((void) ssl);
Manuel Pégourié-Gonnard626aaed2023-02-06 22:03:06 +0100839#endif /* SHA-256 or SHA-384 */
Andrzej Kurek25f27152022-08-17 16:09:31 -0400840#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800841#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100842 status = psa_hash_abort(&ssl->handshake->fin_sha256_psa);
843 if (status != PSA_SUCCESS) {
Andrzej Kurekdaf5b562023-03-03 05:52:28 -0500844 return PSA_TO_MD_ERR(status);
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100845 }
846 status = psa_hash_setup(&ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256);
847 if (status != PSA_SUCCESS) {
Andrzej Kurekdaf5b562023-03-03 05:52:28 -0500848 return PSA_TO_MD_ERR(status);
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100849 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800850#else
Manuel Pégourié-Gonnard947cee12023-03-06 11:59:59 +0100851 mbedtls_md_free(&ssl->handshake->fin_sha256);
852 mbedtls_md_init(&ssl->handshake->fin_sha256);
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +0100853 ret = mbedtls_md_setup(&ssl->handshake->fin_sha256,
854 mbedtls_md_info_from_type(MBEDTLS_MD_SHA256),
855 0);
856 if (ret != 0) {
857 return ret;
858 }
859 ret = mbedtls_md_starts(&ssl->handshake->fin_sha256);
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100860 if (ret != 0) {
861 return ret;
862 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800863#endif
864#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400865#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800866#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100867 status = psa_hash_abort(&ssl->handshake->fin_sha384_psa);
868 if (status != PSA_SUCCESS) {
Andrzej Kurekdaf5b562023-03-03 05:52:28 -0500869 return PSA_TO_MD_ERR(status);
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100870 }
871 status = psa_hash_setup(&ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384);
872 if (status != PSA_SUCCESS) {
Andrzej Kurekdaf5b562023-03-03 05:52:28 -0500873 return PSA_TO_MD_ERR(status);
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100874 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800875#else
Manuel Pégourié-Gonnard947cee12023-03-06 11:59:59 +0100876 mbedtls_md_free(&ssl->handshake->fin_sha384);
877 mbedtls_md_init(&ssl->handshake->fin_sha384);
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +0100878 ret = mbedtls_md_setup(&ssl->handshake->fin_sha384,
879 mbedtls_md_info_from_type(MBEDTLS_MD_SHA384), 0);
880 if (ret != 0) {
881 return ret;
882 }
883 ret = mbedtls_md_starts(&ssl->handshake->fin_sha384);
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100884 if (ret != 0) {
885 return ret;
886 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800887#endif
888#endif
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100889 return 0;
Jerry Yuc73c6182022-02-08 20:29:25 +0800890}
891
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100892static int ssl_update_checksum_start(mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100893 const unsigned char *buf, size_t len)
Jerry Yuc73c6182022-02-08 20:29:25 +0800894{
Manuel Pégourié-Gonnard626aaed2023-02-06 22:03:06 +0100895#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) || \
896 defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100897#if defined(MBEDTLS_USE_PSA_CRYPTO)
898 psa_status_t status;
899#else
900 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
901#endif
Manuel Pégourié-Gonnard626aaed2023-02-06 22:03:06 +0100902#else /* SHA-256 or SHA-384 */
903 ((void) ssl);
904 (void) buf;
905 (void) len;
906#endif /* SHA-256 or SHA-384 */
Andrzej Kurek25f27152022-08-17 16:09:31 -0400907#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800908#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100909 status = psa_hash_update(&ssl->handshake->fin_sha256_psa, buf, len);
910 if (status != PSA_SUCCESS) {
Andrzej Kurekdaf5b562023-03-03 05:52:28 -0500911 return PSA_TO_MD_ERR(status);
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100912 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800913#else
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +0100914 ret = mbedtls_md_update(&ssl->handshake->fin_sha256, buf, len);
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100915 if (ret != 0) {
916 return ret;
917 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800918#endif
919#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400920#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800921#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100922 status = psa_hash_update(&ssl->handshake->fin_sha384_psa, buf, len);
923 if (status != PSA_SUCCESS) {
Andrzej Kurekdaf5b562023-03-03 05:52:28 -0500924 return PSA_TO_MD_ERR(status);
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100925 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800926#else
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +0100927 ret = mbedtls_md_update(&ssl->handshake->fin_sha384, buf, len);
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100928 if (ret != 0) {
929 return ret;
930 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800931#endif
932#endif
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100933 return 0;
Jerry Yuc73c6182022-02-08 20:29:25 +0800934}
935
Andrzej Kurek25f27152022-08-17 16:09:31 -0400936#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100937static int ssl_update_checksum_sha256(mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100938 const unsigned char *buf, size_t len)
Jerry Yuc73c6182022-02-08 20:29:25 +0800939{
940#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurekdaf5b562023-03-03 05:52:28 -0500941 return PSA_TO_MD_ERR(psa_hash_update(
942 &ssl->handshake->fin_sha256_psa, buf, len));
Jerry Yuc73c6182022-02-08 20:29:25 +0800943#else
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +0100944 return mbedtls_md_update(&ssl->handshake->fin_sha256, buf, len);
Jerry Yuc73c6182022-02-08 20:29:25 +0800945#endif
946}
947#endif
948
Andrzej Kurek25f27152022-08-17 16:09:31 -0400949#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100950static int ssl_update_checksum_sha384(mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100951 const unsigned char *buf, size_t len)
Jerry Yuc73c6182022-02-08 20:29:25 +0800952{
953#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurekdaf5b562023-03-03 05:52:28 -0500954 return PSA_TO_MD_ERR(psa_hash_update(
955 &ssl->handshake->fin_sha384_psa, buf, len));
Jerry Yuc73c6182022-02-08 20:29:25 +0800956#else
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +0100957 return mbedtls_md_update(&ssl->handshake->fin_sha384, buf, len);
Jerry Yuc73c6182022-02-08 20:29:25 +0800958#endif
959}
960#endif
961
Gilles Peskine449bd832023-01-11 14:50:10 +0100962static void ssl_handshake_params_init(mbedtls_ssl_handshake_params *handshake)
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200963{
Gilles Peskine449bd832023-01-11 14:50:10 +0100964 memset(handshake, 0, sizeof(mbedtls_ssl_handshake_params));
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200965
Andrzej Kurek25f27152022-08-17 16:09:31 -0400966#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -0500967#if defined(MBEDTLS_USE_PSA_CRYPTO)
968 handshake->fin_sha256_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -0500969#else
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +0100970 mbedtls_md_init(&handshake->fin_sha256);
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200971#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -0500972#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400973#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -0500974#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -0500975 handshake->fin_sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -0500976#else
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +0100977 mbedtls_md_init(&handshake->fin_sha384);
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200978#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -0500979#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200980
981 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +0100982
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200983#if defined(MBEDTLS_DHM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100984 mbedtls_dhm_init(&handshake->dhm_ctx);
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200985#endif
Neil Armstrongf3f46412022-04-12 14:43:39 +0200986#if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100987 mbedtls_ecdh_init(&handshake->ecdh_ctx);
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200988#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +0200989#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Neil Armstrongca7d5062022-05-31 14:43:23 +0200990#if defined(MBEDTLS_USE_PSA_CRYPTO)
991 handshake->psa_pake_ctx = psa_pake_operation_init();
992 handshake->psa_pake_password = MBEDTLS_SVC_KEY_ID_INIT;
993#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100994 mbedtls_ecjpake_init(&handshake->ecjpake_ctx);
Neil Armstrongca7d5062022-05-31 14:43:23 +0200995#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +0200996#if defined(MBEDTLS_SSL_CLI_C)
997 handshake->ecjpake_cache = NULL;
998 handshake->ecjpake_cache_len = 0;
999#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02001000#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001001
Gilles Peskineeccd8882020-03-10 12:19:08 +01001002#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001003 mbedtls_x509_crt_restart_init(&handshake->ecrs_ctx);
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02001004#endif
1005
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001006#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1007 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
1008#endif
Hanno Becker75173122019-02-06 16:18:31 +00001009
1010#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
1011 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01001012 mbedtls_pk_init(&handshake->peer_pubkey);
Hanno Becker75173122019-02-06 16:18:31 +00001013#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001014}
1015
Gilles Peskine449bd832023-01-11 14:50:10 +01001016void mbedtls_ssl_transform_init(mbedtls_ssl_transform *transform)
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001017{
Gilles Peskine449bd832023-01-11 14:50:10 +01001018 memset(transform, 0, sizeof(mbedtls_ssl_transform));
Paul Bakker84bbeb52014-07-01 14:53:22 +02001019
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +01001020#if defined(MBEDTLS_USE_PSA_CRYPTO)
1021 transform->psa_key_enc = MBEDTLS_SVC_KEY_ID_INIT;
1022 transform->psa_key_dec = MBEDTLS_SVC_KEY_ID_INIT;
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01001023#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001024 mbedtls_cipher_init(&transform->cipher_ctx_enc);
1025 mbedtls_cipher_init(&transform->cipher_ctx_dec);
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +01001026#endif
1027
Hanno Beckerfd86ca82020-11-30 08:54:23 +00001028#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong39b8e7d2022-02-23 09:24:45 +01001029#if defined(MBEDTLS_USE_PSA_CRYPTO)
1030 transform->psa_mac_enc = MBEDTLS_SVC_KEY_ID_INIT;
1031 transform->psa_mac_dec = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrongcf8841a2022-02-24 11:17:45 +01001032#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001033 mbedtls_md_init(&transform->md_ctx_enc);
1034 mbedtls_md_init(&transform->md_ctx_dec);
Hanno Beckerd56ed242018-01-03 15:32:51 +00001035#endif
Neil Armstrongcf8841a2022-02-24 11:17:45 +01001036#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001037}
1038
Gilles Peskine449bd832023-01-11 14:50:10 +01001039void mbedtls_ssl_session_init(mbedtls_ssl_session *session)
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001040{
Gilles Peskine449bd832023-01-11 14:50:10 +01001041 memset(session, 0, sizeof(mbedtls_ssl_session));
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001042}
1043
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001044MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001045static int ssl_handshake_init(mbedtls_ssl_context *ssl)
Paul Bakker48916f92012-09-16 19:57:18 +00001046{
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001047 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1048
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001049 /* Clear old handshake information if present */
Jerry Yu2e199812022-12-01 18:57:19 +08001050#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01001051 if (ssl->transform_negotiate) {
1052 mbedtls_ssl_transform_free(ssl->transform_negotiate);
1053 }
Jerry Yu2e199812022-12-01 18:57:19 +08001054#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001055 if (ssl->session_negotiate) {
1056 mbedtls_ssl_session_free(ssl->session_negotiate);
1057 }
1058 if (ssl->handshake) {
1059 mbedtls_ssl_handshake_free(ssl);
1060 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001061
Jerry Yu2e199812022-12-01 18:57:19 +08001062#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001063 /*
1064 * Either the pointers are now NULL or cleared properly and can be freed.
1065 * Now allocate missing structures.
1066 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001067 if (ssl->transform_negotiate == NULL) {
1068 ssl->transform_negotiate = mbedtls_calloc(1, sizeof(mbedtls_ssl_transform));
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001069 }
Jerry Yu2e199812022-12-01 18:57:19 +08001070#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker48916f92012-09-16 19:57:18 +00001071
Gilles Peskine449bd832023-01-11 14:50:10 +01001072 if (ssl->session_negotiate == NULL) {
1073 ssl->session_negotiate = mbedtls_calloc(1, sizeof(mbedtls_ssl_session));
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001074 }
Paul Bakker48916f92012-09-16 19:57:18 +00001075
Gilles Peskine449bd832023-01-11 14:50:10 +01001076 if (ssl->handshake == NULL) {
1077 ssl->handshake = mbedtls_calloc(1, sizeof(mbedtls_ssl_handshake_params));
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001078 }
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05001079#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1080 /* If the buffers are too small - reallocate */
Andrzej Kurek8ea68722020-04-03 06:40:47 -04001081
Gilles Peskine449bd832023-01-11 14:50:10 +01001082 handle_buffer_resizing(ssl, 0, MBEDTLS_SSL_IN_BUFFER_LEN,
1083 MBEDTLS_SSL_OUT_BUFFER_LEN);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05001084#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001085
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001086 /* All pointers should exist and can be directly freed without issue */
Gilles Peskine449bd832023-01-11 14:50:10 +01001087 if (ssl->handshake == NULL ||
Jerry Yu2e199812022-12-01 18:57:19 +08001088#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker48916f92012-09-16 19:57:18 +00001089 ssl->transform_negotiate == NULL ||
Jerry Yu2e199812022-12-01 18:57:19 +08001090#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001091 ssl->session_negotiate == NULL) {
1092 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc() of ssl sub-contexts failed"));
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001093
Gilles Peskine449bd832023-01-11 14:50:10 +01001094 mbedtls_free(ssl->handshake);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001095 ssl->handshake = NULL;
Jerry Yu2e199812022-12-01 18:57:19 +08001096
1097#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01001098 mbedtls_free(ssl->transform_negotiate);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001099 ssl->transform_negotiate = NULL;
Jerry Yu2e199812022-12-01 18:57:19 +08001100#endif
1101
Gilles Peskine449bd832023-01-11 14:50:10 +01001102 mbedtls_free(ssl->session_negotiate);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001103 ssl->session_negotiate = NULL;
1104
Gilles Peskine449bd832023-01-11 14:50:10 +01001105 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Paul Bakker48916f92012-09-16 19:57:18 +00001106 }
1107
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001108 /* Initialize structures */
Gilles Peskine449bd832023-01-11 14:50:10 +01001109 mbedtls_ssl_session_init(ssl->session_negotiate);
1110 ssl_handshake_params_init(ssl->handshake);
Paul Bakker968afaa2014-07-09 11:09:24 +02001111
Jerry Yu2e199812022-12-01 18:57:19 +08001112#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01001113 mbedtls_ssl_transform_init(ssl->transform_negotiate);
Jerry Yu2e199812022-12-01 18:57:19 +08001114#endif
1115
Manuel Pégourié-Gonnard537f2312023-02-05 10:17:45 +01001116 /* Setup handshake checksums */
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001117 ret = mbedtls_ssl_reset_checksum(ssl);
1118 if (ret != 0) {
1119 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_reset_checksum", ret);
1120 return ret;
1121 }
Manuel Pégourié-Gonnard537f2312023-02-05 10:17:45 +01001122
Jerry Yud0766ec2022-09-22 10:46:57 +08001123#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
1124 defined(MBEDTLS_SSL_SRV_C) && \
1125 defined(MBEDTLS_SSL_SESSION_TICKETS)
1126 ssl->handshake->new_session_tickets_count =
Gilles Peskine449bd832023-01-11 14:50:10 +01001127 ssl->conf->new_session_tickets_count;
Jerry Yud0766ec2022-09-22 10:46:57 +08001128#endif
1129
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001130#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001131 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001132 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001133
Gilles Peskine449bd832023-01-11 14:50:10 +01001134 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001135 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Gilles Peskine449bd832023-01-11 14:50:10 +01001136 } else {
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001137 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Gilles Peskine449bd832023-01-11 14:50:10 +01001138 }
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001139
Gilles Peskine449bd832023-01-11 14:50:10 +01001140 mbedtls_ssl_set_timer(ssl, 0);
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001141 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001142#endif
1143
Brett Warrene0edc842021-08-17 09:53:13 +01001144/*
1145 * curve_list is translated to IANA TLS group identifiers here because
1146 * mbedtls_ssl_conf_curves returns void and so can't return
1147 * any error codes.
1148 */
1149#if defined(MBEDTLS_ECP_C)
1150#if !defined(MBEDTLS_DEPRECATED_REMOVED)
1151 /* Heap allocate and translate curve_list from internal to IANA group ids */
Gilles Peskine449bd832023-01-11 14:50:10 +01001152 if (ssl->conf->curve_list != NULL) {
Brett Warrene0edc842021-08-17 09:53:13 +01001153 size_t length;
1154 const mbedtls_ecp_group_id *curve_list = ssl->conf->curve_list;
1155
Gilles Peskine449bd832023-01-11 14:50:10 +01001156 for (length = 0; (curve_list[length] != MBEDTLS_ECP_DP_NONE) &&
1157 (length < MBEDTLS_ECP_DP_MAX); length++) {
1158 }
Brett Warrene0edc842021-08-17 09:53:13 +01001159
1160 /* Leave room for zero termination */
Gilles Peskine449bd832023-01-11 14:50:10 +01001161 uint16_t *group_list = mbedtls_calloc(length + 1, sizeof(uint16_t));
1162 if (group_list == NULL) {
1163 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
1164 }
Brett Warrene0edc842021-08-17 09:53:13 +01001165
Gilles Peskine449bd832023-01-11 14:50:10 +01001166 for (size_t i = 0; i < length; i++) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01001167 uint16_t tls_id = mbedtls_ssl_get_tls_id_from_ecp_group_id(
Gilles Peskine449bd832023-01-11 14:50:10 +01001168 curve_list[i]);
1169 if (tls_id == 0) {
1170 mbedtls_free(group_list);
1171 return MBEDTLS_ERR_SSL_BAD_CONFIG;
Brett Warrene0edc842021-08-17 09:53:13 +01001172 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01001173 group_list[i] = tls_id;
Brett Warrene0edc842021-08-17 09:53:13 +01001174 }
1175
1176 group_list[length] = 0;
1177
1178 ssl->handshake->group_list = group_list;
1179 ssl->handshake->group_list_heap_allocated = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001180 } else {
Brett Warrene0edc842021-08-17 09:53:13 +01001181 ssl->handshake->group_list = ssl->conf->group_list;
1182 ssl->handshake->group_list_heap_allocated = 0;
1183 }
1184#endif /* MBEDTLS_DEPRECATED_REMOVED */
1185#endif /* MBEDTLS_ECP_C */
1186
Ronald Crone68ab4f2022-10-05 12:46:29 +02001187#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +08001188#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Jerry Yua69269a2022-01-17 21:06:01 +08001189#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yu713013f2022-01-17 18:16:35 +08001190 /* Heap allocate and translate sig_hashes from internal hash identifiers to
1191 signature algorithms IANA identifiers. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001192 if (mbedtls_ssl_conf_is_tls12_only(ssl->conf) &&
1193 ssl->conf->sig_hashes != NULL) {
Jerry Yuf017ee42022-01-12 15:49:48 +08001194 const int *md;
1195 const int *sig_hashes = ssl->conf->sig_hashes;
Jerry Yub476a442022-01-21 18:14:45 +08001196 size_t sig_algs_len = 0;
Jerry Yuf017ee42022-01-12 15:49:48 +08001197 uint16_t *p;
1198
Jerry Yub476a442022-01-21 18:14:45 +08001199#if defined(static_assert)
Gilles Peskine449bd832023-01-11 14:50:10 +01001200 static_assert(MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN
1201 <= (SIZE_MAX - (2 * sizeof(uint16_t))),
1202 "MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN too big");
Jerry Yua68dca22022-01-20 16:28:27 +08001203#endif
1204
Gilles Peskine449bd832023-01-11 14:50:10 +01001205 for (md = sig_hashes; *md != MBEDTLS_MD_NONE; md++) {
1206 if (mbedtls_ssl_hash_from_md_alg(*md) == MBEDTLS_SSL_HASH_NONE) {
Jerry Yuf017ee42022-01-12 15:49:48 +08001207 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001208 }
Valerio Setti75fba322023-02-23 17:35:09 +01001209#if defined(MBEDTLS_PK_CAN_ECDSA_SOME)
Gilles Peskine449bd832023-01-11 14:50:10 +01001210 sig_algs_len += sizeof(uint16_t);
Jerry Yub476a442022-01-21 18:14:45 +08001211#endif
Jerry Yua68dca22022-01-20 16:28:27 +08001212
Jerry Yub476a442022-01-21 18:14:45 +08001213#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001214 sig_algs_len += sizeof(uint16_t);
Jerry Yub476a442022-01-21 18:14:45 +08001215#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001216 if (sig_algs_len > MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN) {
1217 return MBEDTLS_ERR_SSL_BAD_CONFIG;
1218 }
Jerry Yuf017ee42022-01-12 15:49:48 +08001219 }
1220
Gilles Peskine449bd832023-01-11 14:50:10 +01001221 if (sig_algs_len < MBEDTLS_SSL_MIN_SIG_ALG_LIST_LEN) {
1222 return MBEDTLS_ERR_SSL_BAD_CONFIG;
1223 }
Jerry Yuf017ee42022-01-12 15:49:48 +08001224
Gilles Peskine449bd832023-01-11 14:50:10 +01001225 ssl->handshake->sig_algs = mbedtls_calloc(1, sig_algs_len +
1226 sizeof(uint16_t));
1227 if (ssl->handshake->sig_algs == NULL) {
1228 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
1229 }
Jerry Yuf017ee42022-01-12 15:49:48 +08001230
Gilles Peskine449bd832023-01-11 14:50:10 +01001231 p = (uint16_t *) ssl->handshake->sig_algs;
1232 for (md = sig_hashes; *md != MBEDTLS_MD_NONE; md++) {
1233 unsigned char hash = mbedtls_ssl_hash_from_md_alg(*md);
1234 if (hash == MBEDTLS_SSL_HASH_NONE) {
Jerry Yuf017ee42022-01-12 15:49:48 +08001235 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001236 }
Valerio Setti75fba322023-02-23 17:35:09 +01001237#if defined(MBEDTLS_PK_CAN_ECDSA_SOME)
Gilles Peskine449bd832023-01-11 14:50:10 +01001238 *p = ((hash << 8) | MBEDTLS_SSL_SIG_ECDSA);
Jerry Yuf017ee42022-01-12 15:49:48 +08001239 p++;
Jerry Yu6106fdc2022-01-12 16:36:14 +08001240#endif
1241#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001242 *p = ((hash << 8) | MBEDTLS_SSL_SIG_RSA);
Jerry Yuf017ee42022-01-12 15:49:48 +08001243 p++;
Jerry Yu6106fdc2022-01-12 16:36:14 +08001244#endif
Jerry Yuf017ee42022-01-12 15:49:48 +08001245 }
Gabor Mezei15b95a62022-05-09 16:37:58 +02001246 *p = MBEDTLS_TLS_SIG_NONE;
Jerry Yuf017ee42022-01-12 15:49:48 +08001247 ssl->handshake->sig_algs_heap_allocated = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001248 } else
Jerry Yua69269a2022-01-17 21:06:01 +08001249#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Jerry Yuf017ee42022-01-12 15:49:48 +08001250 {
Jerry Yuf017ee42022-01-12 15:49:48 +08001251 ssl->handshake->sig_algs_heap_allocated = 0;
1252 }
Jerry Yucc539102022-06-27 16:27:35 +08001253#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Ronald Crone68ab4f2022-10-05 12:46:29 +02001254#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Gilles Peskine449bd832023-01-11 14:50:10 +01001255 return 0;
Paul Bakker48916f92012-09-16 19:57:18 +00001256}
1257
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001258#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001259/* Dummy cookie callbacks for defaults */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001260MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001261static int ssl_cookie_write_dummy(void *ctx,
1262 unsigned char **p, unsigned char *end,
1263 const unsigned char *cli_id, size_t cli_id_len)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001264{
1265 ((void) ctx);
1266 ((void) p);
1267 ((void) end);
1268 ((void) cli_id);
1269 ((void) cli_id_len);
1270
Gilles Peskine449bd832023-01-11 14:50:10 +01001271 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001272}
1273
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001274MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001275static int ssl_cookie_check_dummy(void *ctx,
1276 const unsigned char *cookie, size_t cookie_len,
1277 const unsigned char *cli_id, size_t cli_id_len)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001278{
1279 ((void) ctx);
1280 ((void) cookie);
1281 ((void) cookie_len);
1282 ((void) cli_id);
1283 ((void) cli_id_len);
1284
Gilles Peskine449bd832023-01-11 14:50:10 +01001285 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001286}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001287#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001288
Paul Bakker5121ce52009-01-03 21:22:43 +00001289/*
1290 * Initialize an SSL context
1291 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001292void mbedtls_ssl_init(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001293{
Gilles Peskine449bd832023-01-11 14:50:10 +01001294 memset(ssl, 0, sizeof(mbedtls_ssl_context));
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001295}
1296
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001297MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001298static int ssl_conf_version_check(const mbedtls_ssl_context *ssl)
Jerry Yu60835a82021-08-04 10:13:52 +08001299{
Ronald Cron086ee0b2022-03-15 15:18:51 +01001300 const mbedtls_ssl_config *conf = ssl->conf;
1301
Ronald Cron6f135e12021-12-08 16:57:54 +01001302#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001303 if (mbedtls_ssl_conf_is_tls13_only(conf)) {
1304 if (conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
1305 MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS 1.3 is not yet supported."));
1306 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQianed582dd2022-04-13 08:21:05 +00001307 }
1308
Gilles Peskine449bd832023-01-11 14:50:10 +01001309 MBEDTLS_SSL_DEBUG_MSG(4, ("The SSL configuration is tls13 only."));
1310 return 0;
Jerry Yu60835a82021-08-04 10:13:52 +08001311 }
1312#endif
1313
1314#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01001315 if (mbedtls_ssl_conf_is_tls12_only(conf)) {
1316 MBEDTLS_SSL_DEBUG_MSG(4, ("The SSL configuration is tls12 only."));
1317 return 0;
Jerry Yu60835a82021-08-04 10:13:52 +08001318 }
1319#endif
1320
Ronald Cron6f135e12021-12-08 16:57:54 +01001321#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001322 if (mbedtls_ssl_conf_is_hybrid_tls12_tls13(conf)) {
1323 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
1324 MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS not yet supported in Hybrid TLS 1.3 + TLS 1.2"));
1325 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQianed582dd2022-04-13 08:21:05 +00001326 }
1327
Gilles Peskine449bd832023-01-11 14:50:10 +01001328 if (conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
1329 MBEDTLS_SSL_DEBUG_MSG(1, ("TLS 1.3 server is not supported yet."));
1330 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQian8f9dfe42022-04-15 02:52:39 +00001331 }
1332
1333
Gilles Peskine449bd832023-01-11 14:50:10 +01001334 MBEDTLS_SSL_DEBUG_MSG(4, ("The SSL configuration is TLS 1.3 or TLS 1.2."));
1335 return 0;
Jerry Yu60835a82021-08-04 10:13:52 +08001336 }
1337#endif
1338
Gilles Peskine449bd832023-01-11 14:50:10 +01001339 MBEDTLS_SSL_DEBUG_MSG(1, ("The SSL configuration is invalid."));
1340 return MBEDTLS_ERR_SSL_BAD_CONFIG;
Jerry Yu60835a82021-08-04 10:13:52 +08001341}
1342
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001343MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu60835a82021-08-04 10:13:52 +08001344static int ssl_conf_check(const mbedtls_ssl_context *ssl)
1345{
1346 int ret;
Gilles Peskine449bd832023-01-11 14:50:10 +01001347 ret = ssl_conf_version_check(ssl);
1348 if (ret != 0) {
1349 return ret;
1350 }
Jerry Yu60835a82021-08-04 10:13:52 +08001351
Jerry Yudef7ae42022-10-30 14:13:19 +08001352#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1353 /* RFC 8446 section 4.4.3
1354 *
1355 * If the verification fails, the receiver MUST terminate the handshake with
1356 * a "decrypt_error" alert.
1357 *
1358 * If the client is configured as TLS 1.3 only with optional verify, return
1359 * bad config.
1360 *
1361 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001362 if (mbedtls_ssl_conf_tls13_ephemeral_enabled(
1363 (mbedtls_ssl_context *) ssl) &&
Jerry Yudef7ae42022-10-30 14:13:19 +08001364 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
1365 ssl->conf->max_tls_version == MBEDTLS_SSL_VERSION_TLS1_3 &&
1366 ssl->conf->min_tls_version == MBEDTLS_SSL_VERSION_TLS1_3 &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001367 ssl->conf->authmode == MBEDTLS_SSL_VERIFY_OPTIONAL) {
Jerry Yudef7ae42022-10-30 14:13:19 +08001368 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +01001369 1, ("Optional verify auth mode "
1370 "is not available for TLS 1.3 client"));
1371 return MBEDTLS_ERR_SSL_BAD_CONFIG;
Jerry Yudef7ae42022-10-30 14:13:19 +08001372 }
1373#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
1374
Jerry Yu60835a82021-08-04 10:13:52 +08001375 /* Space for further checks */
1376
Gilles Peskine449bd832023-01-11 14:50:10 +01001377 return 0;
Jerry Yu60835a82021-08-04 10:13:52 +08001378}
1379
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001380/*
1381 * Setup an SSL context
1382 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01001383
Gilles Peskine449bd832023-01-11 14:50:10 +01001384int mbedtls_ssl_setup(mbedtls_ssl_context *ssl,
1385 const mbedtls_ssl_config *conf)
Paul Bakker5121ce52009-01-03 21:22:43 +00001386{
Janos Follath865b3eb2019-12-16 11:46:15 +00001387 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Darryl Greenb33cc762019-11-28 14:29:44 +00001388 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1389 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00001390
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001391 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00001392
Gilles Peskine449bd832023-01-11 14:50:10 +01001393 if ((ret = ssl_conf_check(ssl)) != 0) {
1394 return ret;
1395 }
Jerry Yu60835a82021-08-04 10:13:52 +08001396
Paul Bakker62f2dee2012-09-28 07:31:51 +00001397 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001398 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00001399 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02001400
1401 /* Set to NULL in case of an error condition */
1402 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02001403
Darryl Greenb33cc762019-11-28 14:29:44 +00001404#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1405 ssl->in_buf_len = in_buf_len;
1406#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001407 ssl->in_buf = mbedtls_calloc(1, in_buf_len);
1408 if (ssl->in_buf == NULL) {
1409 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", in_buf_len));
k-stachowiak9f7798e2018-07-31 16:52:32 +02001410 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02001411 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10001412 }
1413
Darryl Greenb33cc762019-11-28 14:29:44 +00001414#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1415 ssl->out_buf_len = out_buf_len;
1416#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001417 ssl->out_buf = mbedtls_calloc(1, out_buf_len);
1418 if (ssl->out_buf == NULL) {
1419 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", out_buf_len));
k-stachowiak9f7798e2018-07-31 16:52:32 +02001420 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02001421 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00001422 }
1423
Gilles Peskine449bd832023-01-11 14:50:10 +01001424 mbedtls_ssl_reset_in_out_pointers(ssl);
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02001425
Johan Pascalb62bb512015-12-03 21:56:45 +01001426#if defined(MBEDTLS_SSL_DTLS_SRTP)
Gilles Peskine449bd832023-01-11 14:50:10 +01001427 memset(&ssl->dtls_srtp_info, 0, sizeof(ssl->dtls_srtp_info));
Johan Pascalb62bb512015-12-03 21:56:45 +01001428#endif
1429
Gilles Peskine449bd832023-01-11 14:50:10 +01001430 if ((ret = ssl_handshake_init(ssl)) != 0) {
k-stachowiaka47911c2018-07-04 17:41:58 +02001431 goto error;
Gilles Peskine449bd832023-01-11 14:50:10 +01001432 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001433
Gilles Peskine449bd832023-01-11 14:50:10 +01001434 return 0;
k-stachowiaka47911c2018-07-04 17:41:58 +02001435
1436error:
Gilles Peskine449bd832023-01-11 14:50:10 +01001437 mbedtls_free(ssl->in_buf);
1438 mbedtls_free(ssl->out_buf);
k-stachowiaka47911c2018-07-04 17:41:58 +02001439
1440 ssl->conf = NULL;
1441
Darryl Greenb33cc762019-11-28 14:29:44 +00001442#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1443 ssl->in_buf_len = 0;
1444 ssl->out_buf_len = 0;
1445#endif
k-stachowiaka47911c2018-07-04 17:41:58 +02001446 ssl->in_buf = NULL;
1447 ssl->out_buf = NULL;
1448
1449 ssl->in_hdr = NULL;
1450 ssl->in_ctr = NULL;
1451 ssl->in_len = NULL;
1452 ssl->in_iv = NULL;
1453 ssl->in_msg = NULL;
1454
1455 ssl->out_hdr = NULL;
1456 ssl->out_ctr = NULL;
1457 ssl->out_len = NULL;
1458 ssl->out_iv = NULL;
1459 ssl->out_msg = NULL;
1460
Gilles Peskine449bd832023-01-11 14:50:10 +01001461 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001462}
1463
1464/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00001465 * Reset an initialized and used SSL context for re-use while retaining
1466 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001467 *
1468 * If partial is non-zero, keep data in the input buffer and client ID.
1469 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00001470 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001471void mbedtls_ssl_session_reset_msg_layer(mbedtls_ssl_context *ssl,
1472 int partial)
Paul Bakker7eb013f2011-10-06 12:37:39 +00001473{
Darryl Greenb33cc762019-11-28 14:29:44 +00001474#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1475 size_t in_buf_len = ssl->in_buf_len;
1476 size_t out_buf_len = ssl->out_buf_len;
1477#else
1478 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1479 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
1480#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001481
Hanno Beckerb0302c42021-08-03 09:39:42 +01001482#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || !defined(MBEDTLS_SSL_SRV_C)
1483 partial = 0;
Hanno Becker7e772132018-08-10 12:38:21 +01001484#endif
1485
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001486 /* Cancel any possibly running timer */
Gilles Peskine449bd832023-01-11 14:50:10 +01001487 mbedtls_ssl_set_timer(ssl, 0);
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001488
Gilles Peskine449bd832023-01-11 14:50:10 +01001489 mbedtls_ssl_reset_in_out_pointers(ssl);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001490
1491 /* Reset incoming message parsing */
1492 ssl->in_offt = NULL;
1493 ssl->nb_zero = 0;
1494 ssl->in_msgtype = 0;
1495 ssl->in_msglen = 0;
1496 ssl->in_hslen = 0;
1497 ssl->keep_current_message = 0;
1498 ssl->transform_in = NULL;
1499
1500#if defined(MBEDTLS_SSL_PROTO_DTLS)
1501 ssl->next_record_offset = 0;
1502 ssl->in_epoch = 0;
1503#endif
1504
1505 /* Keep current datagram if partial == 1 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001506 if (partial == 0) {
Hanno Beckerb0302c42021-08-03 09:39:42 +01001507 ssl->in_left = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001508 memset(ssl->in_buf, 0, in_buf_len);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001509 }
1510
Ronald Cronad8c17b2022-06-10 17:18:09 +02001511 ssl->send_alert = 0;
1512
Hanno Beckerb0302c42021-08-03 09:39:42 +01001513 /* Reset outgoing message writing */
1514 ssl->out_msgtype = 0;
1515 ssl->out_msglen = 0;
1516 ssl->out_left = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001517 memset(ssl->out_buf, 0, out_buf_len);
1518 memset(ssl->cur_out_ctr, 0, sizeof(ssl->cur_out_ctr));
Hanno Beckerb0302c42021-08-03 09:39:42 +01001519 ssl->transform_out = NULL;
1520
1521#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine449bd832023-01-11 14:50:10 +01001522 mbedtls_ssl_dtls_replay_reset(ssl);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001523#endif
1524
XiaokangQian2b01dc32022-01-21 02:53:13 +00001525#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01001526 if (ssl->transform) {
1527 mbedtls_ssl_transform_free(ssl->transform);
1528 mbedtls_free(ssl->transform);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001529 ssl->transform = NULL;
1530 }
XiaokangQian2b01dc32022-01-21 02:53:13 +00001531#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1532
XiaokangQian2b01dc32022-01-21 02:53:13 +00001533#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001534 mbedtls_ssl_transform_free(ssl->transform_application);
1535 mbedtls_free(ssl->transform_application);
XiaokangQian2b01dc32022-01-21 02:53:13 +00001536 ssl->transform_application = NULL;
1537
Gilles Peskine449bd832023-01-11 14:50:10 +01001538 if (ssl->handshake != NULL) {
Jerry Yu3d9b5902022-11-04 14:07:25 +08001539#if defined(MBEDTLS_SSL_EARLY_DATA)
Gilles Peskine449bd832023-01-11 14:50:10 +01001540 mbedtls_ssl_transform_free(ssl->handshake->transform_earlydata);
1541 mbedtls_free(ssl->handshake->transform_earlydata);
XiaokangQian2b01dc32022-01-21 02:53:13 +00001542 ssl->handshake->transform_earlydata = NULL;
Jerry Yu3d9b5902022-11-04 14:07:25 +08001543#endif
XiaokangQian2b01dc32022-01-21 02:53:13 +00001544
Gilles Peskine449bd832023-01-11 14:50:10 +01001545 mbedtls_ssl_transform_free(ssl->handshake->transform_handshake);
1546 mbedtls_free(ssl->handshake->transform_handshake);
XiaokangQian2b01dc32022-01-21 02:53:13 +00001547 ssl->handshake->transform_handshake = NULL;
1548 }
1549
XiaokangQian2b01dc32022-01-21 02:53:13 +00001550#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerb0302c42021-08-03 09:39:42 +01001551}
1552
Gilles Peskine449bd832023-01-11 14:50:10 +01001553int mbedtls_ssl_session_reset_int(mbedtls_ssl_context *ssl, int partial)
Hanno Beckerb0302c42021-08-03 09:39:42 +01001554{
1555 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1556
1557 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
1558
Gilles Peskine449bd832023-01-11 14:50:10 +01001559 mbedtls_ssl_session_reset_msg_layer(ssl, partial);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001560
1561 /* Reset renegotiation state */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001562#if defined(MBEDTLS_SSL_RENEGOTIATION)
1563 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001564 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00001565
1566 ssl->verify_data_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001567 memset(ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN);
1568 memset(ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN);
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001569#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001570 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00001571
Hanno Beckerb0302c42021-08-03 09:39:42 +01001572 ssl->session_in = NULL;
Hanno Becker78640902018-08-13 16:35:15 +01001573 ssl->session_out = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001574 if (ssl->session) {
1575 mbedtls_ssl_session_free(ssl->session);
1576 mbedtls_free(ssl->session);
Paul Bakkerc0463502013-02-14 11:19:38 +01001577 ssl->session = NULL;
1578 }
1579
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001580#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02001581 ssl->alpn_chosen = NULL;
1582#endif
1583
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001584#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
David Horstmann3b2276a2022-10-06 14:49:08 +01001585 int free_cli_id = 1;
Hanno Becker4ccbf062018-08-10 11:20:38 +01001586#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Gilles Peskine449bd832023-01-11 14:50:10 +01001587 free_cli_id = (partial == 0);
Hanno Becker4ccbf062018-08-10 11:20:38 +01001588#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001589 if (free_cli_id) {
1590 mbedtls_free(ssl->cli_id);
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001591 ssl->cli_id = NULL;
1592 ssl->cli_id_len = 0;
1593 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02001594#endif
1595
Gilles Peskine449bd832023-01-11 14:50:10 +01001596 if ((ret = ssl_handshake_init(ssl)) != 0) {
1597 return ret;
1598 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00001599
Gilles Peskine449bd832023-01-11 14:50:10 +01001600 return 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00001601}
1602
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02001603/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001604 * Reset an initialized and used SSL context for re-use while retaining
1605 * all application-set variables, function pointers and data.
1606 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001607int mbedtls_ssl_session_reset(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001608{
Gilles Peskine449bd832023-01-11 14:50:10 +01001609 return mbedtls_ssl_session_reset_int(ssl, 0);
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001610}
1611
1612/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001613 * SSL set accessors
1614 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001615void mbedtls_ssl_conf_endpoint(mbedtls_ssl_config *conf, int endpoint)
Paul Bakker5121ce52009-01-03 21:22:43 +00001616{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001617 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00001618}
1619
Gilles Peskine449bd832023-01-11 14:50:10 +01001620void mbedtls_ssl_conf_transport(mbedtls_ssl_config *conf, int transport)
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001621{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001622 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001623}
1624
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001625#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine449bd832023-01-11 14:50:10 +01001626void mbedtls_ssl_conf_dtls_anti_replay(mbedtls_ssl_config *conf, char mode)
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001627{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001628 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001629}
1630#endif
1631
Gilles Peskine449bd832023-01-11 14:50:10 +01001632void mbedtls_ssl_conf_dtls_badmac_limit(mbedtls_ssl_config *conf, unsigned limit)
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001633{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001634 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001635}
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001636
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001637#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01001638
Gilles Peskine449bd832023-01-11 14:50:10 +01001639void mbedtls_ssl_set_datagram_packing(mbedtls_ssl_context *ssl,
1640 unsigned allow_packing)
Hanno Becker04da1892018-08-14 13:22:10 +01001641{
1642 ssl->disable_datagram_packing = !allow_packing;
1643}
1644
Gilles Peskine449bd832023-01-11 14:50:10 +01001645void mbedtls_ssl_conf_handshake_timeout(mbedtls_ssl_config *conf,
1646 uint32_t min, uint32_t max)
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001647{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001648 conf->hs_timeout_min = min;
1649 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001650}
1651#endif
1652
Gilles Peskine449bd832023-01-11 14:50:10 +01001653void mbedtls_ssl_conf_authmode(mbedtls_ssl_config *conf, int authmode)
Paul Bakker5121ce52009-01-03 21:22:43 +00001654{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001655 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00001656}
1657
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001658#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001659void mbedtls_ssl_conf_verify(mbedtls_ssl_config *conf,
1660 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
1661 void *p_vrfy)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001662{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001663 conf->f_vrfy = f_vrfy;
1664 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001665}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001666#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001667
Gilles Peskine449bd832023-01-11 14:50:10 +01001668void mbedtls_ssl_conf_rng(mbedtls_ssl_config *conf,
1669 int (*f_rng)(void *, unsigned char *, size_t),
1670 void *p_rng)
Paul Bakker5121ce52009-01-03 21:22:43 +00001671{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001672 conf->f_rng = f_rng;
1673 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00001674}
1675
Gilles Peskine449bd832023-01-11 14:50:10 +01001676void mbedtls_ssl_conf_dbg(mbedtls_ssl_config *conf,
1677 void (*f_dbg)(void *, int, const char *, int, const char *),
1678 void *p_dbg)
Paul Bakker5121ce52009-01-03 21:22:43 +00001679{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001680 conf->f_dbg = f_dbg;
1681 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00001682}
1683
Gilles Peskine449bd832023-01-11 14:50:10 +01001684void mbedtls_ssl_set_bio(mbedtls_ssl_context *ssl,
1685 void *p_bio,
1686 mbedtls_ssl_send_t *f_send,
1687 mbedtls_ssl_recv_t *f_recv,
1688 mbedtls_ssl_recv_timeout_t *f_recv_timeout)
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001689{
1690 ssl->p_bio = p_bio;
1691 ssl->f_send = f_send;
1692 ssl->f_recv = f_recv;
1693 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001694}
1695
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02001696#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001697void mbedtls_ssl_set_mtu(mbedtls_ssl_context *ssl, uint16_t mtu)
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02001698{
1699 ssl->mtu = mtu;
1700}
1701#endif
1702
Gilles Peskine449bd832023-01-11 14:50:10 +01001703void mbedtls_ssl_conf_read_timeout(mbedtls_ssl_config *conf, uint32_t timeout)
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001704{
1705 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001706}
1707
Gilles Peskine449bd832023-01-11 14:50:10 +01001708void mbedtls_ssl_set_timer_cb(mbedtls_ssl_context *ssl,
1709 void *p_timer,
1710 mbedtls_ssl_set_timer_t *f_set_timer,
1711 mbedtls_ssl_get_timer_t *f_get_timer)
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001712{
1713 ssl->p_timer = p_timer;
1714 ssl->f_set_timer = f_set_timer;
1715 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001716
1717 /* Make sure we start with no timer running */
Gilles Peskine449bd832023-01-11 14:50:10 +01001718 mbedtls_ssl_set_timer(ssl, 0);
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001719}
1720
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001721#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001722void mbedtls_ssl_conf_session_cache(mbedtls_ssl_config *conf,
1723 void *p_cache,
1724 mbedtls_ssl_cache_get_t *f_get_cache,
1725 mbedtls_ssl_cache_set_t *f_set_cache)
Paul Bakker5121ce52009-01-03 21:22:43 +00001726{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01001727 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001728 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001729 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00001730}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001731#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001732
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001733#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001734int mbedtls_ssl_set_session(mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session)
Paul Bakker5121ce52009-01-03 21:22:43 +00001735{
Janos Follath865b3eb2019-12-16 11:46:15 +00001736 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001737
Gilles Peskine449bd832023-01-11 14:50:10 +01001738 if (ssl == NULL ||
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001739 session == NULL ||
1740 ssl->session_negotiate == NULL ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001741 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT) {
1742 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001743 }
1744
Gilles Peskine449bd832023-01-11 14:50:10 +01001745 if (ssl->handshake->resume == 1) {
1746 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
1747 }
Hanno Beckere810bbc2021-05-14 16:01:05 +01001748
Jerry Yu21092062022-10-10 21:21:31 +08001749#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001750 if (session->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
Jerry Yu21092062022-10-10 21:21:31 +08001751 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01001752 mbedtls_ssl_ciphersuite_from_id(session->ciphersuite);
Jerry Yu21092062022-10-10 21:21:31 +08001753
Gilles Peskine449bd832023-01-11 14:50:10 +01001754 if (mbedtls_ssl_validate_ciphersuite(
Jerry Yu21092062022-10-10 21:21:31 +08001755 ssl, ciphersuite_info, MBEDTLS_SSL_VERSION_TLS1_3,
Gilles Peskine449bd832023-01-11 14:50:10 +01001756 MBEDTLS_SSL_VERSION_TLS1_3) != 0) {
1757 MBEDTLS_SSL_DEBUG_MSG(4, ("%d is not a valid TLS 1.3 ciphersuite.",
1758 session->ciphersuite));
1759 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jerry Yu21092062022-10-10 21:21:31 +08001760 }
Jerry Yu40afab62022-10-08 10:42:13 +08001761 }
Jerry Yu21092062022-10-10 21:21:31 +08001762#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu40afab62022-10-08 10:42:13 +08001763
Gilles Peskine449bd832023-01-11 14:50:10 +01001764 if ((ret = mbedtls_ssl_session_copy(ssl->session_negotiate,
1765 session)) != 0) {
1766 return ret;
1767 }
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001768
Paul Bakker0a597072012-09-25 21:55:46 +00001769 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001770
Gilles Peskine449bd832023-01-11 14:50:10 +01001771 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001772}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001773#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001774
Gilles Peskine449bd832023-01-11 14:50:10 +01001775void mbedtls_ssl_conf_ciphersuites(mbedtls_ssl_config *conf,
1776 const int *ciphersuites)
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001777{
Hanno Beckerd60b6c62021-04-29 12:04:11 +01001778 conf->ciphersuite_list = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00001779}
1780
Ronald Cron6f135e12021-12-08 16:57:54 +01001781#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001782void mbedtls_ssl_conf_tls13_key_exchange_modes(mbedtls_ssl_config *conf,
1783 const int kex_modes)
Hanno Becker71f1ed62021-07-24 06:01:47 +01001784{
Xiaofei Bai746f9482021-11-12 08:53:56 +00001785 conf->tls13_kex_modes = kex_modes & MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Hanno Becker71f1ed62021-07-24 06:01:47 +01001786}
Xiaokang Qian72de95d2022-10-25 02:54:33 +00001787
1788#if defined(MBEDTLS_SSL_EARLY_DATA)
Gilles Peskine449bd832023-01-11 14:50:10 +01001789void mbedtls_ssl_tls13_conf_early_data(mbedtls_ssl_config *conf,
1790 int early_data_enabled)
Xiaokang Qian72de95d2022-10-25 02:54:33 +00001791{
1792 conf->early_data_enabled = early_data_enabled;
1793}
Jerry Yucc4e0072022-11-22 17:22:22 +08001794
1795#if defined(MBEDTLS_SSL_SRV_C)
1796void mbedtls_ssl_tls13_conf_max_early_data_size(
Gilles Peskine449bd832023-01-11 14:50:10 +01001797 mbedtls_ssl_config *conf, uint32_t max_early_data_size)
Jerry Yucc4e0072022-11-22 17:22:22 +08001798{
Jerry Yu39da9852022-12-06 16:58:36 +08001799 conf->max_early_data_size = max_early_data_size;
Jerry Yucc4e0072022-11-22 17:22:22 +08001800}
1801#endif /* MBEDTLS_SSL_SRV_C */
1802
Xiaokang Qian72de95d2022-10-25 02:54:33 +00001803#endif /* MBEDTLS_SSL_EARLY_DATA */
Ronald Cron6f135e12021-12-08 16:57:54 +01001804#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01001805
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001806#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001807void mbedtls_ssl_conf_cert_profile(mbedtls_ssl_config *conf,
1808 const mbedtls_x509_crt_profile *profile)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02001809{
1810 conf->cert_profile = profile;
1811}
1812
Gilles Peskine449bd832023-01-11 14:50:10 +01001813static void ssl_key_cert_free(mbedtls_ssl_key_cert *key_cert)
Glenn Strauss36872db2022-01-22 05:06:31 -05001814{
1815 mbedtls_ssl_key_cert *cur = key_cert, *next;
1816
Gilles Peskine449bd832023-01-11 14:50:10 +01001817 while (cur != NULL) {
Glenn Strauss36872db2022-01-22 05:06:31 -05001818 next = cur->next;
Gilles Peskine449bd832023-01-11 14:50:10 +01001819 mbedtls_free(cur);
Glenn Strauss36872db2022-01-22 05:06:31 -05001820 cur = next;
1821 }
1822}
1823
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001824/* Append a new keycert entry to a (possibly empty) list */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001825MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001826static int ssl_append_key_cert(mbedtls_ssl_key_cert **head,
1827 mbedtls_x509_crt *cert,
1828 mbedtls_pk_context *key)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001829{
niisato8ee24222018-06-25 19:05:48 +09001830 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001831
Gilles Peskine449bd832023-01-11 14:50:10 +01001832 if (cert == NULL) {
Glenn Strauss36872db2022-01-22 05:06:31 -05001833 /* Free list if cert is null */
Gilles Peskine449bd832023-01-11 14:50:10 +01001834 ssl_key_cert_free(*head);
Glenn Strauss36872db2022-01-22 05:06:31 -05001835 *head = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001836 return 0;
Glenn Strauss36872db2022-01-22 05:06:31 -05001837 }
1838
Gilles Peskine449bd832023-01-11 14:50:10 +01001839 new_cert = mbedtls_calloc(1, sizeof(mbedtls_ssl_key_cert));
1840 if (new_cert == NULL) {
1841 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
1842 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001843
niisato8ee24222018-06-25 19:05:48 +09001844 new_cert->cert = cert;
1845 new_cert->key = key;
1846 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001847
Glenn Strauss36872db2022-01-22 05:06:31 -05001848 /* Update head if the list was null, else add to the end */
Gilles Peskine449bd832023-01-11 14:50:10 +01001849 if (*head == NULL) {
niisato8ee24222018-06-25 19:05:48 +09001850 *head = new_cert;
Gilles Peskine449bd832023-01-11 14:50:10 +01001851 } else {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001852 mbedtls_ssl_key_cert *cur = *head;
Gilles Peskine449bd832023-01-11 14:50:10 +01001853 while (cur->next != NULL) {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001854 cur = cur->next;
Gilles Peskine449bd832023-01-11 14:50:10 +01001855 }
niisato8ee24222018-06-25 19:05:48 +09001856 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001857 }
1858
Gilles Peskine449bd832023-01-11 14:50:10 +01001859 return 0;
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001860}
1861
Gilles Peskine449bd832023-01-11 14:50:10 +01001862int mbedtls_ssl_conf_own_cert(mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001863 mbedtls_x509_crt *own_cert,
Gilles Peskine449bd832023-01-11 14:50:10 +01001864 mbedtls_pk_context *pk_key)
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001865{
Gilles Peskine449bd832023-01-11 14:50:10 +01001866 return ssl_append_key_cert(&conf->key_cert, own_cert, pk_key);
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001867}
1868
Gilles Peskine449bd832023-01-11 14:50:10 +01001869void mbedtls_ssl_conf_ca_chain(mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001870 mbedtls_x509_crt *ca_chain,
Gilles Peskine449bd832023-01-11 14:50:10 +01001871 mbedtls_x509_crl *ca_crl)
Paul Bakker5121ce52009-01-03 21:22:43 +00001872{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001873 conf->ca_chain = ca_chain;
1874 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00001875
1876#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1877 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1878 * cannot be used together. */
1879 conf->f_ca_cb = NULL;
1880 conf->p_ca_cb = NULL;
1881#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00001882}
Hanno Becker5adaad92019-03-27 16:54:37 +00001883
1884#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Gilles Peskine449bd832023-01-11 14:50:10 +01001885void mbedtls_ssl_conf_ca_cb(mbedtls_ssl_config *conf,
1886 mbedtls_x509_crt_ca_cb_t f_ca_cb,
1887 void *p_ca_cb)
Hanno Becker5adaad92019-03-27 16:54:37 +00001888{
1889 conf->f_ca_cb = f_ca_cb;
1890 conf->p_ca_cb = p_ca_cb;
1891
1892 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1893 * cannot be used together. */
1894 conf->ca_chain = NULL;
1895 conf->ca_crl = NULL;
1896}
1897#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001898#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00001899
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001900#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01001901const unsigned char *mbedtls_ssl_get_hs_sni(mbedtls_ssl_context *ssl,
1902 size_t *name_len)
Glenn Strauss69894072022-01-24 12:58:00 -05001903{
1904 *name_len = ssl->handshake->sni_name_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001905 return ssl->handshake->sni_name;
Glenn Strauss69894072022-01-24 12:58:00 -05001906}
1907
Gilles Peskine449bd832023-01-11 14:50:10 +01001908int mbedtls_ssl_set_hs_own_cert(mbedtls_ssl_context *ssl,
1909 mbedtls_x509_crt *own_cert,
1910 mbedtls_pk_context *pk_key)
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001911{
Gilles Peskine449bd832023-01-11 14:50:10 +01001912 return ssl_append_key_cert(&ssl->handshake->sni_key_cert,
1913 own_cert, pk_key);
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001914}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02001915
Gilles Peskine449bd832023-01-11 14:50:10 +01001916void mbedtls_ssl_set_hs_ca_chain(mbedtls_ssl_context *ssl,
1917 mbedtls_x509_crt *ca_chain,
1918 mbedtls_x509_crl *ca_crl)
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02001919{
1920 ssl->handshake->sni_ca_chain = ca_chain;
1921 ssl->handshake->sni_ca_crl = ca_crl;
1922}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001923
Glenn Strauss999ef702022-03-11 01:37:23 -05001924#if defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001925void mbedtls_ssl_set_hs_dn_hints(mbedtls_ssl_context *ssl,
1926 const mbedtls_x509_crt *crt)
Glenn Strauss999ef702022-03-11 01:37:23 -05001927{
1928 ssl->handshake->dn_hints = crt;
1929}
1930#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
1931
Gilles Peskine449bd832023-01-11 14:50:10 +01001932void mbedtls_ssl_set_hs_authmode(mbedtls_ssl_context *ssl,
1933 int authmode)
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001934{
1935 ssl->handshake->sni_authmode = authmode;
1936}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001937#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
1938
Hanno Becker8927c832019-04-03 12:52:50 +01001939#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001940void mbedtls_ssl_set_verify(mbedtls_ssl_context *ssl,
1941 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
1942 void *p_vrfy)
Hanno Becker8927c832019-04-03 12:52:50 +01001943{
1944 ssl->f_vrfy = f_vrfy;
1945 ssl->p_vrfy = p_vrfy;
1946}
1947#endif
1948
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02001949#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001950
Valerio Setti016f6822022-12-09 14:17:50 +01001951#if defined(MBEDTLS_USE_PSA_CRYPTO)
1952static psa_status_t mbedtls_ssl_set_hs_ecjpake_password_common(
Gilles Peskine449bd832023-01-11 14:50:10 +01001953 mbedtls_ssl_context *ssl,
1954 mbedtls_svc_key_id_t pwd)
Valerio Setti016f6822022-12-09 14:17:50 +01001955{
1956 psa_status_t status;
1957 psa_pake_role_t psa_role;
1958 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
1959
Gilles Peskine449bd832023-01-11 14:50:10 +01001960 psa_pake_cs_set_algorithm(&cipher_suite, PSA_ALG_JPAKE);
1961 psa_pake_cs_set_primitive(&cipher_suite,
1962 PSA_PAKE_PRIMITIVE(PSA_PAKE_PRIMITIVE_TYPE_ECC,
1963 PSA_ECC_FAMILY_SECP_R1,
1964 256));
1965 psa_pake_cs_set_hash(&cipher_suite, PSA_ALG_SHA_256);
Valerio Setti016f6822022-12-09 14:17:50 +01001966
Gilles Peskine449bd832023-01-11 14:50:10 +01001967 status = psa_pake_setup(&ssl->handshake->psa_pake_ctx, &cipher_suite);
1968 if (status != PSA_SUCCESS) {
Valerio Setti016f6822022-12-09 14:17:50 +01001969 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001970 }
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001971
Gilles Peskine449bd832023-01-11 14:50:10 +01001972 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Neil Armstrongca7d5062022-05-31 14:43:23 +02001973 psa_role = PSA_PAKE_ROLE_SERVER;
Gilles Peskine449bd832023-01-11 14:50:10 +01001974 } else {
Neil Armstrongca7d5062022-05-31 14:43:23 +02001975 psa_role = PSA_PAKE_ROLE_CLIENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001976 }
Neil Armstrongca7d5062022-05-31 14:43:23 +02001977
Gilles Peskine449bd832023-01-11 14:50:10 +01001978 status = psa_pake_set_role(&ssl->handshake->psa_pake_ctx, psa_role);
1979 if (status != PSA_SUCCESS) {
Valerio Setti016f6822022-12-09 14:17:50 +01001980 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001981 }
Valerio Setti016f6822022-12-09 14:17:50 +01001982
Gilles Peskine449bd832023-01-11 14:50:10 +01001983 status = psa_pake_set_password_key(&ssl->handshake->psa_pake_ctx, pwd);
1984 if (status != PSA_SUCCESS) {
Valerio Setti016f6822022-12-09 14:17:50 +01001985 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001986 }
Valerio Setti016f6822022-12-09 14:17:50 +01001987
1988 ssl->handshake->psa_pake_ctx_is_ok = 1;
1989
Gilles Peskine449bd832023-01-11 14:50:10 +01001990 return PSA_SUCCESS;
Valerio Setti016f6822022-12-09 14:17:50 +01001991}
1992
Gilles Peskine449bd832023-01-11 14:50:10 +01001993int mbedtls_ssl_set_hs_ecjpake_password(mbedtls_ssl_context *ssl,
1994 const unsigned char *pw,
1995 size_t pw_len)
Valerio Setti016f6822022-12-09 14:17:50 +01001996{
1997 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1998 psa_status_t status;
1999
Gilles Peskine449bd832023-01-11 14:50:10 +01002000 if (ssl->handshake == NULL || ssl->conf == NULL) {
2001 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Neil Armstrongca7d5062022-05-31 14:43:23 +02002002 }
2003
Gilles Peskine449bd832023-01-11 14:50:10 +01002004 /* Empty password is not valid */
2005 if ((pw == NULL) || (pw_len == 0)) {
2006 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2007 }
2008
2009 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
2010 psa_set_key_algorithm(&attributes, PSA_ALG_JPAKE);
2011 psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD);
2012
2013 status = psa_import_key(&attributes, pw, pw_len,
2014 &ssl->handshake->psa_pake_password);
2015 if (status != PSA_SUCCESS) {
2016 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2017 }
2018
2019 status = mbedtls_ssl_set_hs_ecjpake_password_common(ssl,
2020 ssl->handshake->psa_pake_password);
2021 if (status != PSA_SUCCESS) {
2022 psa_destroy_key(ssl->handshake->psa_pake_password);
2023 psa_pake_abort(&ssl->handshake->psa_pake_ctx);
2024 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2025 }
2026
2027 return 0;
Valerio Setti02c25b52022-11-15 14:08:42 +01002028}
Valerio Settia9a97dc2022-11-28 18:26:16 +01002029
Gilles Peskine449bd832023-01-11 14:50:10 +01002030int mbedtls_ssl_set_hs_ecjpake_password_opaque(mbedtls_ssl_context *ssl,
2031 mbedtls_svc_key_id_t pwd)
Valerio Settia9a97dc2022-11-28 18:26:16 +01002032{
Valerio Settia9a97dc2022-11-28 18:26:16 +01002033 psa_status_t status;
2034
Gilles Peskine449bd832023-01-11 14:50:10 +01002035 if (ssl->handshake == NULL || ssl->conf == NULL) {
2036 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Neil Armstrongca7d5062022-05-31 14:43:23 +02002037 }
2038
Gilles Peskine449bd832023-01-11 14:50:10 +01002039 if (mbedtls_svc_key_id_is_null(pwd)) {
2040 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2041 }
2042
2043 status = mbedtls_ssl_set_hs_ecjpake_password_common(ssl, pwd);
2044 if (status != PSA_SUCCESS) {
2045 psa_pake_abort(&ssl->handshake->psa_pake_ctx);
2046 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2047 }
2048
2049 return 0;
Valerio Setti02c25b52022-11-15 14:08:42 +01002050}
2051#else /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01002052int mbedtls_ssl_set_hs_ecjpake_password(mbedtls_ssl_context *ssl,
2053 const unsigned char *pw,
2054 size_t pw_len)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002055{
2056 mbedtls_ecjpake_role role;
2057
Gilles Peskine449bd832023-01-11 14:50:10 +01002058 if (ssl->handshake == NULL || ssl->conf == NULL) {
2059 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2060 }
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002061
Valerio Settic689ed82022-12-07 14:40:38 +01002062 /* Empty password is not valid */
Gilles Peskine449bd832023-01-11 14:50:10 +01002063 if ((pw == NULL) || (pw_len == 0)) {
2064 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2065 }
Valerio Settic689ed82022-12-07 14:40:38 +01002066
Gilles Peskine449bd832023-01-11 14:50:10 +01002067 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002068 role = MBEDTLS_ECJPAKE_SERVER;
Gilles Peskine449bd832023-01-11 14:50:10 +01002069 } else {
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002070 role = MBEDTLS_ECJPAKE_CLIENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01002071 }
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002072
Gilles Peskine449bd832023-01-11 14:50:10 +01002073 return mbedtls_ecjpake_setup(&ssl->handshake->ecjpake_ctx,
2074 role,
2075 MBEDTLS_MD_SHA256,
2076 MBEDTLS_ECP_DP_SECP256R1,
2077 pw, pw_len);
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002078}
Valerio Setti02c25b52022-11-15 14:08:42 +01002079#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02002080#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002081
Ronald Cron73fe8df2022-10-05 14:31:43 +02002082#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002083int mbedtls_ssl_conf_has_static_psk(mbedtls_ssl_config const *conf)
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002084{
Gilles Peskine449bd832023-01-11 14:50:10 +01002085 if (conf->psk_identity == NULL ||
2086 conf->psk_identity_len == 0) {
2087 return 0;
Ronald Crond29e13e2022-10-19 10:33:48 +02002088 }
2089
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002090#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002091 if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) {
2092 return 1;
2093 }
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002094#endif /* MBEDTLS_USE_PSA_CRYPTO */
Ronald Crond29e13e2022-10-19 10:33:48 +02002095
Gilles Peskine449bd832023-01-11 14:50:10 +01002096 if (conf->psk != NULL && conf->psk_len != 0) {
2097 return 1;
2098 }
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002099
Gilles Peskine449bd832023-01-11 14:50:10 +01002100 return 0;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002101}
2102
Gilles Peskine449bd832023-01-11 14:50:10 +01002103static void ssl_conf_remove_psk(mbedtls_ssl_config *conf)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002104{
2105 /* Remove reference to existing PSK, if any. */
2106#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002107 if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) {
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002108 /* The maintenance of the PSK key slot is the
2109 * user's responsibility. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002110 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002111 }
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002112#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01002113 if (conf->psk != NULL) {
2114 mbedtls_platform_zeroize(conf->psk, conf->psk_len);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002115
Gilles Peskine449bd832023-01-11 14:50:10 +01002116 mbedtls_free(conf->psk);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002117 conf->psk = NULL;
2118 conf->psk_len = 0;
2119 }
2120
2121 /* Remove reference to PSK identity, if any. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002122 if (conf->psk_identity != NULL) {
2123 mbedtls_free(conf->psk_identity);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002124 conf->psk_identity = NULL;
2125 conf->psk_identity_len = 0;
2126 }
2127}
2128
Hanno Becker7390c712018-11-15 13:33:04 +00002129/* This function assumes that PSK identity in the SSL config is unset.
2130 * It checks that the provided identity is well-formed and attempts
2131 * to make a copy of it in the SSL config.
2132 * On failure, the PSK identity in the config remains unset. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002133MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002134static int ssl_conf_set_psk_identity(mbedtls_ssl_config *conf,
2135 unsigned char const *psk_identity,
2136 size_t psk_identity_len)
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002137{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02002138 /* Identity len will be encoded on two bytes */
Gilles Peskine449bd832023-01-11 14:50:10 +01002139 if (psk_identity == NULL ||
Ronald Cron2a87e9b2022-10-19 10:55:26 +02002140 psk_identity_len == 0 ||
Gilles Peskine449bd832023-01-11 14:50:10 +01002141 (psk_identity_len >> 16) != 0 ||
2142 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN) {
2143 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02002144 }
2145
Gilles Peskine449bd832023-01-11 14:50:10 +01002146 conf->psk_identity = mbedtls_calloc(1, psk_identity_len);
2147 if (conf->psk_identity == NULL) {
2148 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2149 }
Paul Bakker6db455e2013-09-18 17:29:31 +02002150
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01002151 conf->psk_identity_len = psk_identity_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002152 memcpy(conf->psk_identity, psk_identity, conf->psk_identity_len);
Paul Bakker5ad403f2013-09-18 21:21:30 +02002153
Gilles Peskine449bd832023-01-11 14:50:10 +01002154 return 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02002155}
2156
Gilles Peskine449bd832023-01-11 14:50:10 +01002157int mbedtls_ssl_conf_psk(mbedtls_ssl_config *conf,
2158 const unsigned char *psk, size_t psk_len,
2159 const unsigned char *psk_identity, size_t psk_identity_len)
Hanno Becker7390c712018-11-15 13:33:04 +00002160{
Janos Follath865b3eb2019-12-16 11:46:15 +00002161 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002162
2163 /* We currently only support one PSK, raw or opaque. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002164 if (mbedtls_ssl_conf_has_static_psk(conf)) {
2165 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
2166 }
Hanno Becker7390c712018-11-15 13:33:04 +00002167
2168 /* Check and set raw PSK */
Gilles Peskine449bd832023-01-11 14:50:10 +01002169 if (psk == NULL) {
2170 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2171 }
2172 if (psk_len == 0) {
2173 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2174 }
2175 if (psk_len > MBEDTLS_PSK_MAX_LEN) {
2176 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2177 }
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01002178
Gilles Peskine449bd832023-01-11 14:50:10 +01002179 if ((conf->psk = mbedtls_calloc(1, psk_len)) == NULL) {
2180 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2181 }
Hanno Becker7390c712018-11-15 13:33:04 +00002182 conf->psk_len = psk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002183 memcpy(conf->psk, psk, conf->psk_len);
Hanno Becker7390c712018-11-15 13:33:04 +00002184
2185 /* Check and set PSK Identity */
Gilles Peskine449bd832023-01-11 14:50:10 +01002186 ret = ssl_conf_set_psk_identity(conf, psk_identity, psk_identity_len);
2187 if (ret != 0) {
2188 ssl_conf_remove_psk(conf);
2189 }
Hanno Becker7390c712018-11-15 13:33:04 +00002190
Gilles Peskine449bd832023-01-11 14:50:10 +01002191 return ret;
Hanno Becker7390c712018-11-15 13:33:04 +00002192}
2193
Gilles Peskine449bd832023-01-11 14:50:10 +01002194static void ssl_remove_psk(mbedtls_ssl_context *ssl)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002195{
2196#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002197 if (!mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque)) {
Neil Armstrong501c9322022-05-03 09:35:09 +02002198 /* The maintenance of the external PSK key slot is the
2199 * user's responsibility. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002200 if (ssl->handshake->psk_opaque_is_internal) {
2201 psa_destroy_key(ssl->handshake->psk_opaque);
Neil Armstrong501c9322022-05-03 09:35:09 +02002202 ssl->handshake->psk_opaque_is_internal = 0;
2203 }
Ronald Croncf56a0a2020-08-04 09:51:30 +02002204 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002205 }
Neil Armstronge952a302022-05-03 10:22:14 +02002206#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002207 if (ssl->handshake->psk != NULL) {
2208 mbedtls_platform_zeroize(ssl->handshake->psk,
2209 ssl->handshake->psk_len);
2210 mbedtls_free(ssl->handshake->psk);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002211 ssl->handshake->psk_len = 0;
2212 }
Neil Armstronge952a302022-05-03 10:22:14 +02002213#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002214}
2215
Gilles Peskine449bd832023-01-11 14:50:10 +01002216int mbedtls_ssl_set_hs_psk(mbedtls_ssl_context *ssl,
2217 const unsigned char *psk, size_t psk_len)
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002218{
Neil Armstrong501c9322022-05-03 09:35:09 +02002219#if defined(MBEDTLS_USE_PSA_CRYPTO)
2220 psa_key_attributes_t key_attributes = psa_key_attributes_init();
Jerry Yu5d01c052022-08-17 10:18:10 +08002221 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jerry Yu24b8c812022-08-20 19:06:56 +08002222 psa_algorithm_t alg = PSA_ALG_NONE;
Jerry Yu5d01c052022-08-17 10:18:10 +08002223 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrong501c9322022-05-03 09:35:09 +02002224#endif /* MBEDTLS_USE_PSA_CRYPTO */
2225
Gilles Peskine449bd832023-01-11 14:50:10 +01002226 if (psk == NULL || ssl->handshake == NULL) {
2227 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2228 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002229
Gilles Peskine449bd832023-01-11 14:50:10 +01002230 if (psk_len > MBEDTLS_PSK_MAX_LEN) {
2231 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2232 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002233
Gilles Peskine449bd832023-01-11 14:50:10 +01002234 ssl_remove_psk(ssl);
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002235
Neil Armstrong501c9322022-05-03 09:35:09 +02002236#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yuccc68a42022-07-26 16:39:20 +08002237#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01002238 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2) {
2239 if (ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384) {
2240 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
2241 } else {
2242 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
2243 }
2244 psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE);
Jerry Yuccc68a42022-07-26 16:39:20 +08002245 }
2246#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Neil Armstrong501c9322022-05-03 09:35:09 +02002247
Jerry Yu568ec252022-07-22 21:27:34 +08002248#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01002249 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
2250 alg = PSA_ALG_HKDF_EXTRACT(PSA_ALG_ANY_HASH);
2251 psa_set_key_usage_flags(&key_attributes,
2252 PSA_KEY_USAGE_DERIVE | PSA_KEY_USAGE_EXPORT);
Jerry Yuccc68a42022-07-26 16:39:20 +08002253 }
2254#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
2255
Gilles Peskine449bd832023-01-11 14:50:10 +01002256 psa_set_key_algorithm(&key_attributes, alg);
2257 psa_set_key_type(&key_attributes, PSA_KEY_TYPE_DERIVE);
Neil Armstrong501c9322022-05-03 09:35:09 +02002258
Gilles Peskine449bd832023-01-11 14:50:10 +01002259 status = psa_import_key(&key_attributes, psk, psk_len, &key);
2260 if (status != PSA_SUCCESS) {
2261 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2262 }
Neil Armstrong501c9322022-05-03 09:35:09 +02002263
2264 /* Allow calling psa_destroy_key() on psk remove */
2265 ssl->handshake->psk_opaque_is_internal = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01002266 return mbedtls_ssl_set_hs_psk_opaque(ssl, key);
Neil Armstrong501c9322022-05-03 09:35:09 +02002267#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002268 if ((ssl->handshake->psk = mbedtls_calloc(1, psk_len)) == NULL) {
2269 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2270 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002271
2272 ssl->handshake->psk_len = psk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002273 memcpy(ssl->handshake->psk, psk, ssl->handshake->psk_len);
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002274
Gilles Peskine449bd832023-01-11 14:50:10 +01002275 return 0;
Neil Armstrong501c9322022-05-03 09:35:09 +02002276#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002277}
2278
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002279#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002280int mbedtls_ssl_conf_psk_opaque(mbedtls_ssl_config *conf,
2281 mbedtls_svc_key_id_t psk,
2282 const unsigned char *psk_identity,
2283 size_t psk_identity_len)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002284{
Janos Follath865b3eb2019-12-16 11:46:15 +00002285 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002286
2287 /* We currently only support one PSK, raw or opaque. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002288 if (mbedtls_ssl_conf_has_static_psk(conf)) {
2289 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
2290 }
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002291
Hanno Becker7390c712018-11-15 13:33:04 +00002292 /* Check and set opaque PSK */
Gilles Peskine449bd832023-01-11 14:50:10 +01002293 if (mbedtls_svc_key_id_is_null(psk)) {
2294 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2295 }
Ronald Croncf56a0a2020-08-04 09:51:30 +02002296 conf->psk_opaque = psk;
Hanno Becker7390c712018-11-15 13:33:04 +00002297
2298 /* Check and set PSK Identity */
Gilles Peskine449bd832023-01-11 14:50:10 +01002299 ret = ssl_conf_set_psk_identity(conf, psk_identity,
2300 psk_identity_len);
2301 if (ret != 0) {
2302 ssl_conf_remove_psk(conf);
2303 }
Hanno Becker7390c712018-11-15 13:33:04 +00002304
Gilles Peskine449bd832023-01-11 14:50:10 +01002305 return ret;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002306}
2307
Gilles Peskine449bd832023-01-11 14:50:10 +01002308int mbedtls_ssl_set_hs_psk_opaque(mbedtls_ssl_context *ssl,
2309 mbedtls_svc_key_id_t psk)
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002310{
Gilles Peskine449bd832023-01-11 14:50:10 +01002311 if ((mbedtls_svc_key_id_is_null(psk)) ||
2312 (ssl->handshake == NULL)) {
2313 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2314 }
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002315
Gilles Peskine449bd832023-01-11 14:50:10 +01002316 ssl_remove_psk(ssl);
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002317 ssl->handshake->psk_opaque = psk;
Gilles Peskine449bd832023-01-11 14:50:10 +01002318 return 0;
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002319}
2320#endif /* MBEDTLS_USE_PSA_CRYPTO */
2321
Jerry Yu8897c072022-08-12 13:56:53 +08002322#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002323void mbedtls_ssl_conf_psk_cb(mbedtls_ssl_config *conf,
2324 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
2325 size_t),
2326 void *p_psk)
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002327{
2328 conf->f_psk = f_psk;
2329 conf->p_psk = p_psk;
2330}
Jerry Yu8897c072022-08-12 13:56:53 +08002331#endif /* MBEDTLS_SSL_SRV_C */
2332
Ronald Cron73fe8df2022-10-05 14:31:43 +02002333#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002334
2335#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine301711e2022-04-26 16:57:05 +02002336static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
Gilles Peskine449bd832023-01-11 14:50:10 +01002337 psa_algorithm_t alg)
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002338{
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002339#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01002340 if (alg == PSA_ALG_CBC_NO_PADDING) {
2341 return MBEDTLS_SSL_MODE_CBC;
2342 }
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002343#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
Gilles Peskine449bd832023-01-11 14:50:10 +01002344 if (PSA_ALG_IS_AEAD(alg)) {
2345 return MBEDTLS_SSL_MODE_AEAD;
2346 }
2347 return MBEDTLS_SSL_MODE_STREAM;
Gilles Peskine301711e2022-04-26 16:57:05 +02002348}
2349
2350#else /* MBEDTLS_USE_PSA_CRYPTO */
2351
2352static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
Gilles Peskine449bd832023-01-11 14:50:10 +01002353 mbedtls_cipher_mode_t mode)
Gilles Peskine301711e2022-04-26 16:57:05 +02002354{
2355#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01002356 if (mode == MBEDTLS_MODE_CBC) {
2357 return MBEDTLS_SSL_MODE_CBC;
2358 }
Gilles Peskine301711e2022-04-26 16:57:05 +02002359#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
2360
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002361#if defined(MBEDTLS_GCM_C) || \
2362 defined(MBEDTLS_CCM_C) || \
2363 defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002364 if (mode == MBEDTLS_MODE_GCM ||
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002365 mode == MBEDTLS_MODE_CCM ||
Gilles Peskine449bd832023-01-11 14:50:10 +01002366 mode == MBEDTLS_MODE_CHACHAPOLY) {
2367 return MBEDTLS_SSL_MODE_AEAD;
2368 }
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002369#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002370
Gilles Peskine449bd832023-01-11 14:50:10 +01002371 return MBEDTLS_SSL_MODE_STREAM;
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002372}
Gilles Peskine301711e2022-04-26 16:57:05 +02002373#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002374
Gilles Peskinee108d982022-04-26 16:50:40 +02002375static mbedtls_ssl_mode_t mbedtls_ssl_get_actual_mode(
2376 mbedtls_ssl_mode_t base_mode,
Gilles Peskine449bd832023-01-11 14:50:10 +01002377 int encrypt_then_mac)
Gilles Peskinee108d982022-04-26 16:50:40 +02002378{
2379#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01002380 if (encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED &&
2381 base_mode == MBEDTLS_SSL_MODE_CBC) {
2382 return MBEDTLS_SSL_MODE_CBC_ETM;
Gilles Peskinee108d982022-04-26 16:50:40 +02002383 }
2384#else
2385 (void) encrypt_then_mac;
2386#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002387 return base_mode;
Gilles Peskinee108d982022-04-26 16:50:40 +02002388}
2389
Neil Armstrongab555e02022-04-04 11:07:59 +02002390mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_transform(
Gilles Peskine449bd832023-01-11 14:50:10 +01002391 const mbedtls_ssl_transform *transform)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002392{
Gilles Peskinee108d982022-04-26 16:50:40 +02002393 mbedtls_ssl_mode_t base_mode = mbedtls_ssl_get_base_mode(
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002394#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002395 transform->psa_alg
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002396#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002397 mbedtls_cipher_get_cipher_mode(&transform->cipher_ctx_enc)
Gilles Peskinee108d982022-04-26 16:50:40 +02002398#endif
2399 );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002400
Gilles Peskinee108d982022-04-26 16:50:40 +02002401 int encrypt_then_mac = 0;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002402#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskinee108d982022-04-26 16:50:40 +02002403 encrypt_then_mac = transform->encrypt_then_mac;
2404#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002405 return mbedtls_ssl_get_actual_mode(base_mode, encrypt_then_mac);
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002406}
2407
Neil Armstrongab555e02022-04-04 11:07:59 +02002408mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002409#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01002410 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002411#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01002412 const mbedtls_ssl_ciphersuite_t *suite)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002413{
Gilles Peskinee108d982022-04-26 16:50:40 +02002414 mbedtls_ssl_mode_t base_mode = MBEDTLS_SSL_MODE_STREAM;
2415
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002416#if defined(MBEDTLS_USE_PSA_CRYPTO)
2417 psa_status_t status;
2418 psa_algorithm_t alg;
2419 psa_key_type_t type;
2420 size_t size;
Gilles Peskine449bd832023-01-11 14:50:10 +01002421 status = mbedtls_ssl_cipher_to_psa(suite->cipher, 0, &alg, &type, &size);
2422 if (status == PSA_SUCCESS) {
2423 base_mode = mbedtls_ssl_get_base_mode(alg);
2424 }
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002425#else
2426 const mbedtls_cipher_info_t *cipher =
Gilles Peskine449bd832023-01-11 14:50:10 +01002427 mbedtls_cipher_info_from_type(suite->cipher);
2428 if (cipher != NULL) {
Gilles Peskinee108d982022-04-26 16:50:40 +02002429 base_mode =
2430 mbedtls_ssl_get_base_mode(
Gilles Peskine449bd832023-01-11 14:50:10 +01002431 mbedtls_cipher_info_get_mode(cipher));
Gilles Peskinee108d982022-04-26 16:50:40 +02002432 }
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002433#endif /* MBEDTLS_USE_PSA_CRYPTO */
2434
Gilles Peskinee108d982022-04-26 16:50:40 +02002435#if !defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
2436 int encrypt_then_mac = 0;
2437#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002438 return mbedtls_ssl_get_actual_mode(base_mode, encrypt_then_mac);
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002439}
2440
Neil Armstrong8395d7a2022-05-18 11:44:56 +02002441#if defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu251a12e2022-07-13 15:15:48 +08002442
2443#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu438ddd82022-07-07 06:55:50 +00002444/* Serialization of TLS 1.3 sessions:
2445 *
2446 * struct {
Xiaokang Qian126bf8e2022-10-13 02:22:40 +00002447 * opaque hostname<0..2^16-1>;
Jerry Yu438ddd82022-07-07 06:55:50 +00002448 * uint64 ticket_received;
2449 * uint32 ticket_lifetime;
Jerry Yu34191072022-08-18 10:32:09 +08002450 * opaque ticket<1..2^16-1>;
Jerry Yu438ddd82022-07-07 06:55:50 +00002451 * } ClientOnlyData;
2452 *
2453 * struct {
2454 * uint8 endpoint;
2455 * uint8 ciphersuite[2];
2456 * uint32 ticket_age_add;
2457 * uint8 ticket_flags;
2458 * opaque resumption_key<0..255>;
2459 * select ( endpoint ) {
2460 * case client: ClientOnlyData;
2461 * case server: uint64 start_time;
2462 * };
2463 * } serialized_session_tls13;
2464 *
2465 */
2466#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yue36fdd62022-08-17 21:31:36 +08002467MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002468static int ssl_tls13_session_save(const mbedtls_ssl_session *session,
2469 unsigned char *buf,
2470 size_t buf_len,
2471 size_t *olen)
Jerry Yu438ddd82022-07-07 06:55:50 +00002472{
2473 unsigned char *p = buf;
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00002474#if defined(MBEDTLS_SSL_CLI_C) && \
2475 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01002476 size_t hostname_len = (session->hostname == NULL) ?
2477 0 : strlen(session->hostname) + 1;
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00002478#endif
Jerry Yubc7c1a42022-07-21 22:57:37 +08002479 size_t needed = 1 /* endpoint */
2480 + 2 /* ciphersuite */
2481 + 4 /* ticket_age_add */
Jerry Yu34191072022-08-18 10:32:09 +08002482 + 1 /* ticket_flags */
2483 + 1; /* resumption_key length */
Jerry Yue36fdd62022-08-17 21:31:36 +08002484 *olen = 0;
Jerry Yu34191072022-08-18 10:32:09 +08002485
Gilles Peskine449bd832023-01-11 14:50:10 +01002486 if (session->resumption_key_len > MBEDTLS_SSL_TLS1_3_TICKET_RESUMPTION_KEY_LEN) {
2487 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2488 }
Jerry Yu34191072022-08-18 10:32:09 +08002489 needed += session->resumption_key_len; /* resumption_key */
2490
Jerry Yu438ddd82022-07-07 06:55:50 +00002491#if defined(MBEDTLS_HAVE_TIME)
2492 needed += 8; /* start_time or ticket_received */
2493#endif
2494
2495#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002496 if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Xiaokang Qian126bf8e2022-10-13 02:22:40 +00002497#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaokang Qianbc663a02022-10-09 11:14:39 +00002498 needed += 2 /* hostname_len */
Gilles Peskine449bd832023-01-11 14:50:10 +01002499 + hostname_len; /* hostname */
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00002500#endif
2501
Jerry Yu438ddd82022-07-07 06:55:50 +00002502 needed += 4 /* ticket_lifetime */
Jerry Yue36fdd62022-08-17 21:31:36 +08002503 + 2; /* ticket_len */
Jerry Yu34191072022-08-18 10:32:09 +08002504
2505 /* Check size_t overflow */
Gilles Peskine449bd832023-01-11 14:50:10 +01002506 if (session->ticket_len > SIZE_MAX - needed) {
2507 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2508 }
Jerry Yu34191072022-08-18 10:32:09 +08002509
Jerry Yue28d9742022-08-18 15:44:03 +08002510 needed += session->ticket_len; /* ticket */
Jerry Yu438ddd82022-07-07 06:55:50 +00002511 }
2512#endif /* MBEDTLS_SSL_CLI_C */
2513
Jerry Yue36fdd62022-08-17 21:31:36 +08002514 *olen = needed;
Gilles Peskine449bd832023-01-11 14:50:10 +01002515 if (needed > buf_len) {
2516 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
2517 }
Jerry Yu438ddd82022-07-07 06:55:50 +00002518
2519 p[0] = session->endpoint;
Gilles Peskine449bd832023-01-11 14:50:10 +01002520 MBEDTLS_PUT_UINT16_BE(session->ciphersuite, p, 1);
2521 MBEDTLS_PUT_UINT32_BE(session->ticket_age_add, p, 3);
Jerry Yu438ddd82022-07-07 06:55:50 +00002522 p[7] = session->ticket_flags;
2523
2524 /* save resumption_key */
Jerry Yubc7c1a42022-07-21 22:57:37 +08002525 p[8] = session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002526 p += 9;
Gilles Peskine449bd832023-01-11 14:50:10 +01002527 memcpy(p, session->resumption_key, session->resumption_key_len);
Jerry Yubc7c1a42022-07-21 22:57:37 +08002528 p += session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002529
2530#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002531 if (session->endpoint == MBEDTLS_SSL_IS_SERVER) {
2532 MBEDTLS_PUT_UINT64_BE((uint64_t) session->start, p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002533 p += 8;
2534 }
2535#endif /* MBEDTLS_HAVE_TIME */
2536
2537#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002538 if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002539#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01002540 MBEDTLS_PUT_UINT16_BE(hostname_len, p, 0);
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002541 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002542 if (hostname_len > 0) {
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002543 /* save host name */
Gilles Peskine449bd832023-01-11 14:50:10 +01002544 memcpy(p, session->hostname, hostname_len);
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002545 p += hostname_len;
2546 }
2547#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
2548
Jerry Yu438ddd82022-07-07 06:55:50 +00002549#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01002550 MBEDTLS_PUT_UINT64_BE((uint64_t) session->ticket_received, p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002551 p += 8;
2552#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002553 MBEDTLS_PUT_UINT32_BE(session->ticket_lifetime, p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002554 p += 4;
2555
Gilles Peskine449bd832023-01-11 14:50:10 +01002556 MBEDTLS_PUT_UINT16_BE(session->ticket_len, p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002557 p += 2;
Jerry Yu34191072022-08-18 10:32:09 +08002558
Gilles Peskine449bd832023-01-11 14:50:10 +01002559 if (session->ticket != NULL && session->ticket_len > 0) {
2560 memcpy(p, session->ticket, session->ticket_len);
Jerry Yu438ddd82022-07-07 06:55:50 +00002561 p += session->ticket_len;
2562 }
2563 }
2564#endif /* MBEDTLS_SSL_CLI_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01002565 return 0;
Jerry Yu438ddd82022-07-07 06:55:50 +00002566}
2567
2568MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002569static int ssl_tls13_session_load(mbedtls_ssl_session *session,
2570 const unsigned char *buf,
2571 size_t len)
Jerry Yu438ddd82022-07-07 06:55:50 +00002572{
2573 const unsigned char *p = buf;
2574 const unsigned char *end = buf + len;
2575
Gilles Peskine449bd832023-01-11 14:50:10 +01002576 if (end - p < 9) {
2577 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2578 }
Jerry Yu438ddd82022-07-07 06:55:50 +00002579 session->endpoint = p[0];
Gilles Peskine449bd832023-01-11 14:50:10 +01002580 session->ciphersuite = MBEDTLS_GET_UINT16_BE(p, 1);
2581 session->ticket_age_add = MBEDTLS_GET_UINT32_BE(p, 3);
Jerry Yu438ddd82022-07-07 06:55:50 +00002582 session->ticket_flags = p[7];
2583
2584 /* load resumption_key */
Jerry Yubc7c1a42022-07-21 22:57:37 +08002585 session->resumption_key_len = p[8];
Jerry Yu438ddd82022-07-07 06:55:50 +00002586 p += 9;
2587
Gilles Peskine449bd832023-01-11 14:50:10 +01002588 if (end - p < session->resumption_key_len) {
2589 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2590 }
Jerry Yu438ddd82022-07-07 06:55:50 +00002591
Gilles Peskine449bd832023-01-11 14:50:10 +01002592 if (sizeof(session->resumption_key) < session->resumption_key_len) {
2593 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2594 }
2595 memcpy(session->resumption_key, p, session->resumption_key_len);
Jerry Yubc7c1a42022-07-21 22:57:37 +08002596 p += session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002597
2598#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002599 if (session->endpoint == MBEDTLS_SSL_IS_SERVER) {
2600 if (end - p < 8) {
2601 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2602 }
2603 session->start = MBEDTLS_GET_UINT64_BE(p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002604 p += 8;
2605 }
2606#endif /* MBEDTLS_HAVE_TIME */
2607
2608#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002609 if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002610#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01002611 defined(MBEDTLS_SSL_SESSION_TICKETS)
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002612 size_t hostname_len;
2613 /* load host name */
Gilles Peskine449bd832023-01-11 14:50:10 +01002614 if (end - p < 2) {
2615 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2616 }
2617 hostname_len = MBEDTLS_GET_UINT16_BE(p, 0);
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002618 p += 2;
2619
Gilles Peskine449bd832023-01-11 14:50:10 +01002620 if (end - p < (long int) hostname_len) {
2621 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2622 }
2623 if (hostname_len > 0) {
2624 session->hostname = mbedtls_calloc(1, hostname_len);
2625 if (session->hostname == NULL) {
2626 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2627 }
2628 memcpy(session->hostname, p, hostname_len);
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002629 p += hostname_len;
2630 }
2631#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION &&
2632 MBEDTLS_SSL_SESSION_TICKETS */
2633
Jerry Yu438ddd82022-07-07 06:55:50 +00002634#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01002635 if (end - p < 8) {
2636 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2637 }
2638 session->ticket_received = MBEDTLS_GET_UINT64_BE(p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002639 p += 8;
2640#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002641 if (end - p < 4) {
2642 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2643 }
2644 session->ticket_lifetime = MBEDTLS_GET_UINT32_BE(p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002645 p += 4;
2646
Gilles Peskine449bd832023-01-11 14:50:10 +01002647 if (end - p < 2) {
2648 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2649 }
2650 session->ticket_len = MBEDTLS_GET_UINT16_BE(p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002651 p += 2;
2652
Gilles Peskine449bd832023-01-11 14:50:10 +01002653 if (end - p < (long int) session->ticket_len) {
2654 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2655 }
2656 if (session->ticket_len > 0) {
2657 session->ticket = mbedtls_calloc(1, session->ticket_len);
2658 if (session->ticket == NULL) {
2659 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2660 }
2661 memcpy(session->ticket, p, session->ticket_len);
Jerry Yu438ddd82022-07-07 06:55:50 +00002662 p += session->ticket_len;
2663 }
2664 }
2665#endif /* MBEDTLS_SSL_CLI_C */
2666
Gilles Peskine449bd832023-01-11 14:50:10 +01002667 return 0;
Jerry Yu438ddd82022-07-07 06:55:50 +00002668
2669}
2670#else /* MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yue36fdd62022-08-17 21:31:36 +08002671MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002672static int ssl_tls13_session_save(const mbedtls_ssl_session *session,
2673 unsigned char *buf,
2674 size_t buf_len,
2675 size_t *olen)
Jerry Yu251a12e2022-07-13 15:15:48 +08002676{
2677 ((void) session);
2678 ((void) buf);
2679 ((void) buf_len);
Jerry Yue36fdd62022-08-17 21:31:36 +08002680 *olen = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002681 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Jerry Yu251a12e2022-07-13 15:15:48 +08002682}
Jerry Yu438ddd82022-07-07 06:55:50 +00002683
Gilles Peskine449bd832023-01-11 14:50:10 +01002684static int ssl_tls13_session_load(const mbedtls_ssl_session *session,
2685 unsigned char *buf,
2686 size_t buf_len)
Jerry Yu438ddd82022-07-07 06:55:50 +00002687{
2688 ((void) session);
2689 ((void) buf);
2690 ((void) buf_len);
Gilles Peskine449bd832023-01-11 14:50:10 +01002691 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Jerry Yu438ddd82022-07-07 06:55:50 +00002692}
2693#endif /* !MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yu251a12e2022-07-13 15:15:48 +08002694#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
2695
Gilles Peskine449bd832023-01-11 14:50:10 +01002696psa_status_t mbedtls_ssl_cipher_to_psa(mbedtls_cipher_type_t mbedtls_cipher_type,
2697 size_t taglen,
2698 psa_algorithm_t *alg,
2699 psa_key_type_t *key_type,
2700 size_t *key_size)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002701{
Gilles Peskine449bd832023-01-11 14:50:10 +01002702 switch (mbedtls_cipher_type) {
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002703 case MBEDTLS_CIPHER_AES_128_CBC:
2704 *alg = PSA_ALG_CBC_NO_PADDING;
2705 *key_type = PSA_KEY_TYPE_AES;
2706 *key_size = 128;
2707 break;
2708 case MBEDTLS_CIPHER_AES_128_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002709 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002710 *key_type = PSA_KEY_TYPE_AES;
2711 *key_size = 128;
2712 break;
2713 case MBEDTLS_CIPHER_AES_128_GCM:
2714 *alg = PSA_ALG_GCM;
2715 *key_type = PSA_KEY_TYPE_AES;
2716 *key_size = 128;
2717 break;
2718 case MBEDTLS_CIPHER_AES_192_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002719 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002720 *key_type = PSA_KEY_TYPE_AES;
2721 *key_size = 192;
2722 break;
2723 case MBEDTLS_CIPHER_AES_192_GCM:
2724 *alg = PSA_ALG_GCM;
2725 *key_type = PSA_KEY_TYPE_AES;
2726 *key_size = 192;
2727 break;
2728 case MBEDTLS_CIPHER_AES_256_CBC:
2729 *alg = PSA_ALG_CBC_NO_PADDING;
2730 *key_type = PSA_KEY_TYPE_AES;
2731 *key_size = 256;
2732 break;
2733 case MBEDTLS_CIPHER_AES_256_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002734 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002735 *key_type = PSA_KEY_TYPE_AES;
2736 *key_size = 256;
2737 break;
2738 case MBEDTLS_CIPHER_AES_256_GCM:
2739 *alg = PSA_ALG_GCM;
2740 *key_type = PSA_KEY_TYPE_AES;
2741 *key_size = 256;
2742 break;
2743 case MBEDTLS_CIPHER_ARIA_128_CBC:
2744 *alg = PSA_ALG_CBC_NO_PADDING;
2745 *key_type = PSA_KEY_TYPE_ARIA;
2746 *key_size = 128;
2747 break;
2748 case MBEDTLS_CIPHER_ARIA_128_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002749 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002750 *key_type = PSA_KEY_TYPE_ARIA;
2751 *key_size = 128;
2752 break;
2753 case MBEDTLS_CIPHER_ARIA_128_GCM:
2754 *alg = PSA_ALG_GCM;
2755 *key_type = PSA_KEY_TYPE_ARIA;
2756 *key_size = 128;
2757 break;
2758 case MBEDTLS_CIPHER_ARIA_192_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002759 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002760 *key_type = PSA_KEY_TYPE_ARIA;
2761 *key_size = 192;
2762 break;
2763 case MBEDTLS_CIPHER_ARIA_192_GCM:
2764 *alg = PSA_ALG_GCM;
2765 *key_type = PSA_KEY_TYPE_ARIA;
2766 *key_size = 192;
2767 break;
2768 case MBEDTLS_CIPHER_ARIA_256_CBC:
2769 *alg = PSA_ALG_CBC_NO_PADDING;
2770 *key_type = PSA_KEY_TYPE_ARIA;
2771 *key_size = 256;
2772 break;
2773 case MBEDTLS_CIPHER_ARIA_256_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002774 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002775 *key_type = PSA_KEY_TYPE_ARIA;
2776 *key_size = 256;
2777 break;
2778 case MBEDTLS_CIPHER_ARIA_256_GCM:
2779 *alg = PSA_ALG_GCM;
2780 *key_type = PSA_KEY_TYPE_ARIA;
2781 *key_size = 256;
2782 break;
2783 case MBEDTLS_CIPHER_CAMELLIA_128_CBC:
2784 *alg = PSA_ALG_CBC_NO_PADDING;
2785 *key_type = PSA_KEY_TYPE_CAMELLIA;
2786 *key_size = 128;
2787 break;
2788 case MBEDTLS_CIPHER_CAMELLIA_128_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002789 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002790 *key_type = PSA_KEY_TYPE_CAMELLIA;
2791 *key_size = 128;
2792 break;
2793 case MBEDTLS_CIPHER_CAMELLIA_128_GCM:
2794 *alg = PSA_ALG_GCM;
2795 *key_type = PSA_KEY_TYPE_CAMELLIA;
2796 *key_size = 128;
2797 break;
2798 case MBEDTLS_CIPHER_CAMELLIA_192_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002799 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002800 *key_type = PSA_KEY_TYPE_CAMELLIA;
2801 *key_size = 192;
2802 break;
2803 case MBEDTLS_CIPHER_CAMELLIA_192_GCM:
2804 *alg = PSA_ALG_GCM;
2805 *key_type = PSA_KEY_TYPE_CAMELLIA;
2806 *key_size = 192;
2807 break;
2808 case MBEDTLS_CIPHER_CAMELLIA_256_CBC:
2809 *alg = PSA_ALG_CBC_NO_PADDING;
2810 *key_type = PSA_KEY_TYPE_CAMELLIA;
2811 *key_size = 256;
2812 break;
2813 case MBEDTLS_CIPHER_CAMELLIA_256_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002814 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002815 *key_type = PSA_KEY_TYPE_CAMELLIA;
2816 *key_size = 256;
2817 break;
2818 case MBEDTLS_CIPHER_CAMELLIA_256_GCM:
2819 *alg = PSA_ALG_GCM;
2820 *key_type = PSA_KEY_TYPE_CAMELLIA;
2821 *key_size = 256;
2822 break;
2823 case MBEDTLS_CIPHER_CHACHA20_POLY1305:
2824 *alg = PSA_ALG_CHACHA20_POLY1305;
2825 *key_type = PSA_KEY_TYPE_CHACHA20;
2826 *key_size = 256;
2827 break;
2828 case MBEDTLS_CIPHER_NULL:
2829 *alg = MBEDTLS_SSL_NULL_CIPHER;
2830 *key_type = 0;
2831 *key_size = 0;
2832 break;
2833 default:
2834 return PSA_ERROR_NOT_SUPPORTED;
2835 }
2836
2837 return PSA_SUCCESS;
2838}
Neil Armstrong8395d7a2022-05-18 11:44:56 +02002839#endif /* MBEDTLS_USE_PSA_CRYPTO || MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002840
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002841#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002842int mbedtls_ssl_conf_dh_param_bin(mbedtls_ssl_config *conf,
2843 const unsigned char *dhm_P, size_t P_len,
2844 const unsigned char *dhm_G, size_t G_len)
Hanno Beckera90658f2017-10-04 15:29:08 +01002845{
Janos Follath865b3eb2019-12-16 11:46:15 +00002846 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckera90658f2017-10-04 15:29:08 +01002847
Gilles Peskine449bd832023-01-11 14:50:10 +01002848 mbedtls_mpi_free(&conf->dhm_P);
2849 mbedtls_mpi_free(&conf->dhm_G);
Glenn Strausscee11292021-12-20 01:43:17 -05002850
Gilles Peskine449bd832023-01-11 14:50:10 +01002851 if ((ret = mbedtls_mpi_read_binary(&conf->dhm_P, dhm_P, P_len)) != 0 ||
2852 (ret = mbedtls_mpi_read_binary(&conf->dhm_G, dhm_G, G_len)) != 0) {
2853 mbedtls_mpi_free(&conf->dhm_P);
2854 mbedtls_mpi_free(&conf->dhm_G);
2855 return ret;
Hanno Beckera90658f2017-10-04 15:29:08 +01002856 }
2857
Gilles Peskine449bd832023-01-11 14:50:10 +01002858 return 0;
Hanno Beckera90658f2017-10-04 15:29:08 +01002859}
Paul Bakker5121ce52009-01-03 21:22:43 +00002860
Gilles Peskine449bd832023-01-11 14:50:10 +01002861int mbedtls_ssl_conf_dh_param_ctx(mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx)
Paul Bakker1b57b062011-01-06 15:48:19 +00002862{
Janos Follath865b3eb2019-12-16 11:46:15 +00002863 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1b57b062011-01-06 15:48:19 +00002864
Gilles Peskine449bd832023-01-11 14:50:10 +01002865 mbedtls_mpi_free(&conf->dhm_P);
2866 mbedtls_mpi_free(&conf->dhm_G);
Glenn Strausscee11292021-12-20 01:43:17 -05002867
Gilles Peskine449bd832023-01-11 14:50:10 +01002868 if ((ret = mbedtls_dhm_get_value(dhm_ctx, MBEDTLS_DHM_PARAM_P,
2869 &conf->dhm_P)) != 0 ||
2870 (ret = mbedtls_dhm_get_value(dhm_ctx, MBEDTLS_DHM_PARAM_G,
2871 &conf->dhm_G)) != 0) {
2872 mbedtls_mpi_free(&conf->dhm_P);
2873 mbedtls_mpi_free(&conf->dhm_G);
2874 return ret;
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01002875 }
Paul Bakker1b57b062011-01-06 15:48:19 +00002876
Gilles Peskine449bd832023-01-11 14:50:10 +01002877 return 0;
Paul Bakker1b57b062011-01-06 15:48:19 +00002878}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002879#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00002880
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02002881#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
2882/*
2883 * Set the minimum length for Diffie-Hellman parameters
2884 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002885void mbedtls_ssl_conf_dhm_min_bitlen(mbedtls_ssl_config *conf,
2886 unsigned int bitlen)
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02002887{
2888 conf->dhm_min_bitlen = bitlen;
2889}
2890#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
2891
Ronald Crone68ab4f2022-10-05 12:46:29 +02002892#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002893#if !defined(MBEDTLS_DEPRECATED_REMOVED) && defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002894/*
2895 * Set allowed/preferred hashes for handshake signatures
2896 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002897void mbedtls_ssl_conf_sig_hashes(mbedtls_ssl_config *conf,
2898 const int *hashes)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002899{
2900 conf->sig_hashes = hashes;
2901}
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002902#endif /* !MBEDTLS_DEPRECATED_REMOVED && MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker1cd6e002021-08-10 13:27:10 +01002903
Jerry Yuf017ee42022-01-12 15:49:48 +08002904/* Configure allowed signature algorithms for handshake */
Gilles Peskine449bd832023-01-11 14:50:10 +01002905void mbedtls_ssl_conf_sig_algs(mbedtls_ssl_config *conf,
2906 const uint16_t *sig_algs)
Hanno Becker1cd6e002021-08-10 13:27:10 +01002907{
Jerry Yuf017ee42022-01-12 15:49:48 +08002908#if !defined(MBEDTLS_DEPRECATED_REMOVED)
2909 conf->sig_hashes = NULL;
2910#endif /* !MBEDTLS_DEPRECATED_REMOVED */
2911 conf->sig_algs = sig_algs;
Hanno Becker1cd6e002021-08-10 13:27:10 +01002912}
Ronald Crone68ab4f2022-10-05 12:46:29 +02002913#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002914
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02002915#if defined(MBEDTLS_ECP_C)
Brett Warrene0edc842021-08-17 09:53:13 +01002916#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002917/*
2918 * Set the allowed elliptic curves
Brett Warrene0edc842021-08-17 09:53:13 +01002919 *
2920 * mbedtls_ssl_setup() takes the provided list
2921 * and translates it to a list of IANA TLS group identifiers,
2922 * stored in ssl->handshake->group_list.
2923 *
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002924 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002925void mbedtls_ssl_conf_curves(mbedtls_ssl_config *conf,
2926 const mbedtls_ecp_group_id *curve_list)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002927{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002928 conf->curve_list = curve_list;
Brett Warrene0edc842021-08-17 09:53:13 +01002929 conf->group_list = NULL;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002930}
Brett Warrene0edc842021-08-17 09:53:13 +01002931#endif /* MBEDTLS_DEPRECATED_REMOVED */
Hanno Becker947194e2017-04-07 13:25:49 +01002932#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002933
Brett Warrene0edc842021-08-17 09:53:13 +01002934/*
2935 * Set the allowed groups
2936 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002937void mbedtls_ssl_conf_groups(mbedtls_ssl_config *conf,
2938 const uint16_t *group_list)
Brett Warrene0edc842021-08-17 09:53:13 +01002939{
2940#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
2941 conf->curve_list = NULL;
2942#endif
2943 conf->group_list = group_list;
2944}
2945
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002946#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002947int mbedtls_ssl_set_hostname(mbedtls_ssl_context *ssl, const char *hostname)
Paul Bakker5121ce52009-01-03 21:22:43 +00002948{
Hanno Becker947194e2017-04-07 13:25:49 +01002949 /* Initialize to suppress unnecessary compiler warning */
2950 size_t hostname_len = 0;
2951
2952 /* Check if new hostname is valid before
2953 * making any change to current one */
Gilles Peskine449bd832023-01-11 14:50:10 +01002954 if (hostname != NULL) {
2955 hostname_len = strlen(hostname);
Hanno Becker947194e2017-04-07 13:25:49 +01002956
Gilles Peskine449bd832023-01-11 14:50:10 +01002957 if (hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN) {
2958 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2959 }
Hanno Becker947194e2017-04-07 13:25:49 +01002960 }
2961
2962 /* Now it's clear that we will overwrite the old hostname,
2963 * so we can free it safely */
2964
Gilles Peskine449bd832023-01-11 14:50:10 +01002965 if (ssl->hostname != NULL) {
2966 mbedtls_platform_zeroize(ssl->hostname, strlen(ssl->hostname));
2967 mbedtls_free(ssl->hostname);
Hanno Becker947194e2017-04-07 13:25:49 +01002968 }
2969
2970 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01002971
Gilles Peskine449bd832023-01-11 14:50:10 +01002972 if (hostname == NULL) {
Hanno Becker947194e2017-04-07 13:25:49 +01002973 ssl->hostname = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01002974 } else {
2975 ssl->hostname = mbedtls_calloc(1, hostname_len + 1);
2976 if (ssl->hostname == NULL) {
2977 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2978 }
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002979
Gilles Peskine449bd832023-01-11 14:50:10 +01002980 memcpy(ssl->hostname, hostname, hostname_len);
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002981
Hanno Becker947194e2017-04-07 13:25:49 +01002982 ssl->hostname[hostname_len] = '\0';
2983 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002984
Gilles Peskine449bd832023-01-11 14:50:10 +01002985 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002986}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01002987#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002988
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002989#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01002990void mbedtls_ssl_conf_sni(mbedtls_ssl_config *conf,
2991 int (*f_sni)(void *, mbedtls_ssl_context *,
2992 const unsigned char *, size_t),
2993 void *p_sni)
Paul Bakker5701cdc2012-09-27 21:49:42 +00002994{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002995 conf->f_sni = f_sni;
2996 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00002997}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002998#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00002999
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003000#if defined(MBEDTLS_SSL_ALPN)
Gilles Peskine449bd832023-01-11 14:50:10 +01003001int mbedtls_ssl_conf_alpn_protocols(mbedtls_ssl_config *conf, const char **protos)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003002{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02003003 size_t cur_len, tot_len;
3004 const char **p;
3005
3006 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08003007 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
3008 * MUST NOT be truncated."
3009 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02003010 */
3011 tot_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003012 for (p = protos; *p != NULL; p++) {
3013 cur_len = strlen(*p);
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02003014 tot_len += cur_len;
3015
Gilles Peskine449bd832023-01-11 14:50:10 +01003016 if ((cur_len == 0) ||
3017 (cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN) ||
3018 (tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN)) {
3019 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3020 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02003021 }
3022
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003023 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02003024
Gilles Peskine449bd832023-01-11 14:50:10 +01003025 return 0;
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003026}
3027
Gilles Peskine449bd832023-01-11 14:50:10 +01003028const char *mbedtls_ssl_get_alpn_protocol(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003029{
Gilles Peskine449bd832023-01-11 14:50:10 +01003030 return ssl->alpn_chosen;
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003031}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003032#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003033
Johan Pascalb62bb512015-12-03 21:56:45 +01003034#if defined(MBEDTLS_SSL_DTLS_SRTP)
Gilles Peskine449bd832023-01-11 14:50:10 +01003035void mbedtls_ssl_conf_srtp_mki_value_supported(mbedtls_ssl_config *conf,
3036 int support_mki_value)
Ron Eldor591f1622018-01-22 12:30:04 +02003037{
3038 conf->dtls_srtp_mki_support = support_mki_value;
3039}
3040
Gilles Peskine449bd832023-01-11 14:50:10 +01003041int mbedtls_ssl_dtls_srtp_set_mki_value(mbedtls_ssl_context *ssl,
3042 unsigned char *mki_value,
3043 uint16_t mki_len)
Ron Eldor591f1622018-01-22 12:30:04 +02003044{
Gilles Peskine449bd832023-01-11 14:50:10 +01003045 if (mki_len > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH) {
3046 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Ron Eldora9788042018-12-05 11:04:31 +02003047 }
Ron Eldor591f1622018-01-22 12:30:04 +02003048
Gilles Peskine449bd832023-01-11 14:50:10 +01003049 if (ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_UNSUPPORTED) {
3050 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Ron Eldora9788042018-12-05 11:04:31 +02003051 }
Ron Eldor591f1622018-01-22 12:30:04 +02003052
Gilles Peskine449bd832023-01-11 14:50:10 +01003053 memcpy(ssl->dtls_srtp_info.mki_value, mki_value, mki_len);
Ron Eldor591f1622018-01-22 12:30:04 +02003054 ssl->dtls_srtp_info.mki_len = mki_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01003055 return 0;
Ron Eldor591f1622018-01-22 12:30:04 +02003056}
3057
Gilles Peskine449bd832023-01-11 14:50:10 +01003058int mbedtls_ssl_conf_dtls_srtp_protection_profiles(mbedtls_ssl_config *conf,
3059 const mbedtls_ssl_srtp_profile *profiles)
Johan Pascalb62bb512015-12-03 21:56:45 +01003060{
Johan Pascal253d0262020-09-22 13:04:45 +02003061 const mbedtls_ssl_srtp_profile *p;
3062 size_t list_size = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01003063
Johan Pascal253d0262020-09-22 13:04:45 +02003064 /* check the profiles list: all entry must be valid,
3065 * its size cannot be more than the total number of supported profiles, currently 4 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003066 for (p = profiles; *p != MBEDTLS_TLS_SRTP_UNSET &&
3067 list_size <= MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH;
3068 p++) {
3069 if (mbedtls_ssl_check_srtp_profile_value(*p) != MBEDTLS_TLS_SRTP_UNSET) {
Johan Pascal76fdf1d2020-10-22 23:31:00 +02003070 list_size++;
Gilles Peskine449bd832023-01-11 14:50:10 +01003071 } else {
Johan Pascal76fdf1d2020-10-22 23:31:00 +02003072 /* unsupported value, stop parsing and set the size to an error value */
3073 list_size = MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH + 1;
Johan Pascalb62bb512015-12-03 21:56:45 +01003074 }
3075 }
3076
Gilles Peskine449bd832023-01-11 14:50:10 +01003077 if (list_size > MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH) {
3078 conf->dtls_srtp_profile_list = NULL;
3079 conf->dtls_srtp_profile_list_len = 0;
3080 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Johan Pascal253d0262020-09-22 13:04:45 +02003081 }
3082
Johan Pascal9bc97ca2020-09-21 23:44:45 +02003083 conf->dtls_srtp_profile_list = profiles;
Johan Pascal253d0262020-09-22 13:04:45 +02003084 conf->dtls_srtp_profile_list_len = list_size;
Johan Pascalb62bb512015-12-03 21:56:45 +01003085
Gilles Peskine449bd832023-01-11 14:50:10 +01003086 return 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01003087}
3088
Gilles Peskine449bd832023-01-11 14:50:10 +01003089void mbedtls_ssl_get_dtls_srtp_negotiation_result(const mbedtls_ssl_context *ssl,
3090 mbedtls_dtls_srtp_info *dtls_srtp_info)
Johan Pascalb62bb512015-12-03 21:56:45 +01003091{
Johan Pascal2258a4f2020-10-28 13:53:09 +01003092 dtls_srtp_info->chosen_dtls_srtp_profile = ssl->dtls_srtp_info.chosen_dtls_srtp_profile;
3093 /* do not copy the mki value if there is no chosen profile */
Gilles Peskine449bd832023-01-11 14:50:10 +01003094 if (dtls_srtp_info->chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET) {
Johan Pascal2258a4f2020-10-28 13:53:09 +01003095 dtls_srtp_info->mki_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003096 } else {
Johan Pascal2258a4f2020-10-28 13:53:09 +01003097 dtls_srtp_info->mki_len = ssl->dtls_srtp_info.mki_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01003098 memcpy(dtls_srtp_info->mki_value, ssl->dtls_srtp_info.mki_value,
3099 ssl->dtls_srtp_info.mki_len);
Johan Pascal2258a4f2020-10-28 13:53:09 +01003100 }
Johan Pascalb62bb512015-12-03 21:56:45 +01003101}
Johan Pascalb62bb512015-12-03 21:56:45 +01003102#endif /* MBEDTLS_SSL_DTLS_SRTP */
3103
Aditya Patwardhan3096f332022-07-26 14:31:46 +05303104#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003105void mbedtls_ssl_conf_max_version(mbedtls_ssl_config *conf, int major, int minor)
Paul Bakker490ecc82011-10-06 13:04:09 +00003106{
Glenn Strauss2dfcea22022-03-14 17:26:42 -04003107 conf->max_tls_version = (major << 8) | minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00003108}
3109
Gilles Peskine449bd832023-01-11 14:50:10 +01003110void mbedtls_ssl_conf_min_version(mbedtls_ssl_config *conf, int major, int minor)
Paul Bakker1d29fb52012-09-28 13:28:45 +00003111{
Glenn Strauss2dfcea22022-03-14 17:26:42 -04003112 conf->min_tls_version = (major << 8) | minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00003113}
Aditya Patwardhan3096f332022-07-26 14:31:46 +05303114#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker1d29fb52012-09-28 13:28:45 +00003115
Janos Follath088ce432017-04-10 12:42:31 +01003116#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003117void mbedtls_ssl_conf_cert_req_ca_list(mbedtls_ssl_config *conf,
3118 char cert_req_ca_list)
Janos Follath088ce432017-04-10 12:42:31 +01003119{
3120 conf->cert_req_ca_list = cert_req_ca_list;
3121}
3122#endif
3123
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003124#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01003125void mbedtls_ssl_conf_encrypt_then_mac(mbedtls_ssl_config *conf, char etm)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01003126{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003127 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01003128}
3129#endif
3130
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003131#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Gilles Peskine449bd832023-01-11 14:50:10 +01003132void mbedtls_ssl_conf_extended_master_secret(mbedtls_ssl_config *conf, char ems)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02003133{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003134 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02003135}
3136#endif
3137
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003138#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01003139int mbedtls_ssl_conf_max_frag_len(mbedtls_ssl_config *conf, unsigned char mfl_code)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003140{
Gilles Peskine449bd832023-01-11 14:50:10 +01003141 if (mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
3142 ssl_mfl_code_to_length(mfl_code) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN) {
3143 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003144 }
3145
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01003146 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003147
Gilles Peskine449bd832023-01-11 14:50:10 +01003148 return 0;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003149}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003150#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003151
Gilles Peskine449bd832023-01-11 14:50:10 +01003152void mbedtls_ssl_conf_legacy_renegotiation(mbedtls_ssl_config *conf, int allow_legacy)
Paul Bakker48916f92012-09-16 19:57:18 +00003153{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003154 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00003155}
3156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003157#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01003158void mbedtls_ssl_conf_renegotiation(mbedtls_ssl_config *conf, int renegotiation)
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003159{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003160 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003161}
3162
Gilles Peskine449bd832023-01-11 14:50:10 +01003163void mbedtls_ssl_conf_renegotiation_enforced(mbedtls_ssl_config *conf, int max_records)
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003164{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003165 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003166}
3167
Gilles Peskine449bd832023-01-11 14:50:10 +01003168void mbedtls_ssl_conf_renegotiation_period(mbedtls_ssl_config *conf,
3169 const unsigned char period[8])
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01003170{
Gilles Peskine449bd832023-01-11 14:50:10 +01003171 memcpy(conf->renego_period, period, 8);
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01003172}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003173#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00003174
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003175#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003176#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003177void mbedtls_ssl_conf_session_tickets(mbedtls_ssl_config *conf, int use_tickets)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003178{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01003179 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003180}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003181#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02003182
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003183#if defined(MBEDTLS_SSL_SRV_C)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08003184
Jerry Yud0766ec2022-09-22 10:46:57 +08003185#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003186void mbedtls_ssl_conf_new_session_tickets(mbedtls_ssl_config *conf,
3187 uint16_t num_tickets)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08003188{
Jerry Yud0766ec2022-09-22 10:46:57 +08003189 conf->new_session_tickets_count = num_tickets;
Jerry Yu1ad7ace2022-08-09 13:28:39 +08003190}
3191#endif
3192
Gilles Peskine449bd832023-01-11 14:50:10 +01003193void mbedtls_ssl_conf_session_tickets_cb(mbedtls_ssl_config *conf,
3194 mbedtls_ssl_ticket_write_t *f_ticket_write,
3195 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
3196 void *p_ticket)
Paul Bakker606b4ba2013-08-14 16:52:14 +02003197{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02003198 conf->f_ticket_write = f_ticket_write;
3199 conf->f_ticket_parse = f_ticket_parse;
3200 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02003201}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003202#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003203#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003204
Gilles Peskine449bd832023-01-11 14:50:10 +01003205void mbedtls_ssl_set_export_keys_cb(mbedtls_ssl_context *ssl,
3206 mbedtls_ssl_export_keys_t *f_export_keys,
3207 void *p_export_keys)
Robert Cragie4feb7ae2015-10-02 13:33:37 +01003208{
Hanno Becker7e6c1782021-06-08 09:24:55 +01003209 ssl->f_export_keys = f_export_keys;
3210 ssl->p_export_keys = p_export_keys;
Ron Eldorf5cc10d2019-05-07 18:33:40 +03003211}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01003212
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003213#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003214void mbedtls_ssl_conf_async_private_cb(
3215 mbedtls_ssl_config *conf,
3216 mbedtls_ssl_async_sign_t *f_async_sign,
3217 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
3218 mbedtls_ssl_async_resume_t *f_async_resume,
3219 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskine449bd832023-01-11 14:50:10 +01003220 void *async_config_data)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003221{
3222 conf->f_async_sign_start = f_async_sign;
3223 conf->f_async_decrypt_start = f_async_decrypt;
3224 conf->f_async_resume = f_async_resume;
3225 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003226 conf->p_async_config_data = async_config_data;
3227}
3228
Gilles Peskine449bd832023-01-11 14:50:10 +01003229void *mbedtls_ssl_conf_get_async_config_data(const mbedtls_ssl_config *conf)
Gilles Peskine8f97af72018-04-26 11:46:10 +02003230{
Gilles Peskine449bd832023-01-11 14:50:10 +01003231 return conf->p_async_config_data;
Gilles Peskine8f97af72018-04-26 11:46:10 +02003232}
3233
Gilles Peskine449bd832023-01-11 14:50:10 +01003234void *mbedtls_ssl_get_async_operation_data(const mbedtls_ssl_context *ssl)
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003235{
Gilles Peskine449bd832023-01-11 14:50:10 +01003236 if (ssl->handshake == NULL) {
3237 return NULL;
3238 } else {
3239 return ssl->handshake->user_async_ctx;
3240 }
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003241}
3242
Gilles Peskine449bd832023-01-11 14:50:10 +01003243void mbedtls_ssl_set_async_operation_data(mbedtls_ssl_context *ssl,
3244 void *ctx)
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003245{
Gilles Peskine449bd832023-01-11 14:50:10 +01003246 if (ssl->handshake != NULL) {
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003247 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +01003248 }
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003249}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003250#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003251
Paul Bakker5121ce52009-01-03 21:22:43 +00003252/*
3253 * SSL get accessors
3254 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003255uint32_t mbedtls_ssl_get_verify_result(const mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00003256{
Gilles Peskine449bd832023-01-11 14:50:10 +01003257 if (ssl->session != NULL) {
3258 return ssl->session->verify_result;
3259 }
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00003260
Gilles Peskine449bd832023-01-11 14:50:10 +01003261 if (ssl->session_negotiate != NULL) {
3262 return ssl->session_negotiate->verify_result;
3263 }
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00003264
Gilles Peskine449bd832023-01-11 14:50:10 +01003265 return 0xFFFFFFFF;
Paul Bakker5121ce52009-01-03 21:22:43 +00003266}
3267
Gilles Peskine449bd832023-01-11 14:50:10 +01003268int mbedtls_ssl_get_ciphersuite_id_from_ssl(const mbedtls_ssl_context *ssl)
Glenn Strauss8f526902022-01-13 00:04:49 -05003269{
Gilles Peskine449bd832023-01-11 14:50:10 +01003270 if (ssl == NULL || ssl->session == NULL) {
3271 return 0;
3272 }
Glenn Strauss8f526902022-01-13 00:04:49 -05003273
Gilles Peskine449bd832023-01-11 14:50:10 +01003274 return ssl->session->ciphersuite;
Glenn Strauss8f526902022-01-13 00:04:49 -05003275}
3276
Gilles Peskine449bd832023-01-11 14:50:10 +01003277const char *mbedtls_ssl_get_ciphersuite(const mbedtls_ssl_context *ssl)
Paul Bakker72f62662011-01-16 21:27:44 +00003278{
Gilles Peskine449bd832023-01-11 14:50:10 +01003279 if (ssl == NULL || ssl->session == NULL) {
3280 return NULL;
3281 }
Paul Bakker926c8e42013-03-06 10:23:34 +01003282
Gilles Peskine449bd832023-01-11 14:50:10 +01003283 return mbedtls_ssl_get_ciphersuite_name(ssl->session->ciphersuite);
Paul Bakker72f62662011-01-16 21:27:44 +00003284}
3285
Gilles Peskine449bd832023-01-11 14:50:10 +01003286const char *mbedtls_ssl_get_version(const mbedtls_ssl_context *ssl)
Paul Bakker43ca69c2011-01-15 17:35:19 +00003287{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003288#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003289 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
3290 switch (ssl->tls_version) {
Glenn Strauss60bfe602022-03-14 19:04:24 -04003291 case MBEDTLS_SSL_VERSION_TLS1_2:
Gilles Peskine449bd832023-01-11 14:50:10 +01003292 return "DTLSv1.2";
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003293 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01003294 return "unknown (DTLS)";
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003295 }
3296 }
3297#endif
3298
Gilles Peskine449bd832023-01-11 14:50:10 +01003299 switch (ssl->tls_version) {
Glenn Strauss60bfe602022-03-14 19:04:24 -04003300 case MBEDTLS_SSL_VERSION_TLS1_2:
Gilles Peskine449bd832023-01-11 14:50:10 +01003301 return "TLSv1.2";
Glenn Strauss60bfe602022-03-14 19:04:24 -04003302 case MBEDTLS_SSL_VERSION_TLS1_3:
Gilles Peskine449bd832023-01-11 14:50:10 +01003303 return "TLSv1.3";
Paul Bakker43ca69c2011-01-15 17:35:19 +00003304 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01003305 return "unknown";
Paul Bakker43ca69c2011-01-15 17:35:19 +00003306 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00003307}
3308
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003309#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01003310size_t mbedtls_ssl_get_input_max_frag_len(const mbedtls_ssl_context *ssl)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003311{
David Horstmann95d516f2021-05-04 18:36:56 +01003312 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003313 size_t read_mfl;
3314
Jerry Yuddda0502022-12-01 19:43:12 +08003315#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003316 /* Use the configured MFL for the client if we're past SERVER_HELLO_DONE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003317 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
3318 ssl->state >= MBEDTLS_SSL_SERVER_HELLO_DONE) {
3319 return ssl_mfl_code_to_length(ssl->conf->mfl_code);
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003320 }
Jerry Yuddda0502022-12-01 19:43:12 +08003321#endif
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003322
3323 /* Check if a smaller max length was negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01003324 if (ssl->session_out != NULL) {
3325 read_mfl = ssl_mfl_code_to_length(ssl->session_out->mfl_code);
3326 if (read_mfl < max_len) {
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003327 max_len = read_mfl;
3328 }
3329 }
3330
Jerry Yuddda0502022-12-01 19:43:12 +08003331 /* During a handshake, use the value being negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01003332 if (ssl->session_negotiate != NULL) {
3333 read_mfl = ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code);
3334 if (read_mfl < max_len) {
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003335 max_len = read_mfl;
3336 }
3337 }
3338
Gilles Peskine449bd832023-01-11 14:50:10 +01003339 return max_len;
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003340}
3341
Gilles Peskine449bd832023-01-11 14:50:10 +01003342size_t mbedtls_ssl_get_output_max_frag_len(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003343{
3344 size_t max_len;
3345
3346 /*
3347 * Assume mfl_code is correct since it was checked when set
3348 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003349 max_len = ssl_mfl_code_to_length(ssl->conf->mfl_code);
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003350
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02003351 /* Check if a smaller max length was negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01003352 if (ssl->session_out != NULL &&
3353 ssl_mfl_code_to_length(ssl->session_out->mfl_code) < max_len) {
3354 max_len = ssl_mfl_code_to_length(ssl->session_out->mfl_code);
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003355 }
3356
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02003357 /* During a handshake, use the value being negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01003358 if (ssl->session_negotiate != NULL &&
3359 ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code) < max_len) {
3360 max_len = ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code);
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02003361 }
3362
Gilles Peskine449bd832023-01-11 14:50:10 +01003363 return max_len;
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003364}
3365#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
3366
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003367#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003368size_t mbedtls_ssl_get_current_mtu(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003369{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04003370 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003371 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
3372 (ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
3373 ssl->state == MBEDTLS_SSL_SERVER_HELLO)) {
3374 return 0;
3375 }
Andrzej Kurekef43ce62018-10-09 08:24:12 -04003376
Gilles Peskine449bd832023-01-11 14:50:10 +01003377 if (ssl->handshake == NULL || ssl->handshake->mtu == 0) {
3378 return ssl->mtu;
3379 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003380
Gilles Peskine449bd832023-01-11 14:50:10 +01003381 if (ssl->mtu == 0) {
3382 return ssl->handshake->mtu;
3383 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003384
Gilles Peskine449bd832023-01-11 14:50:10 +01003385 return ssl->mtu < ssl->handshake->mtu ?
3386 ssl->mtu : ssl->handshake->mtu;
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003387}
3388#endif /* MBEDTLS_SSL_PROTO_DTLS */
3389
Gilles Peskine449bd832023-01-11 14:50:10 +01003390int mbedtls_ssl_get_max_out_record_payload(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003391{
3392 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
3393
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02003394#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
3395 !defined(MBEDTLS_SSL_PROTO_DTLS)
3396 (void) ssl;
3397#endif
3398
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003399#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01003400 const size_t mfl = mbedtls_ssl_get_output_max_frag_len(ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003401
Gilles Peskine449bd832023-01-11 14:50:10 +01003402 if (max_len > mfl) {
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003403 max_len = mfl;
Gilles Peskine449bd832023-01-11 14:50:10 +01003404 }
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003405#endif
3406
3407#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003408 if (mbedtls_ssl_get_current_mtu(ssl) != 0) {
3409 const size_t mtu = mbedtls_ssl_get_current_mtu(ssl);
3410 const int ret = mbedtls_ssl_get_record_expansion(ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003411 const size_t overhead = (size_t) ret;
3412
Gilles Peskine449bd832023-01-11 14:50:10 +01003413 if (ret < 0) {
3414 return ret;
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003415 }
3416
Gilles Peskine449bd832023-01-11 14:50:10 +01003417 if (mtu <= overhead) {
3418 MBEDTLS_SSL_DEBUG_MSG(1, ("MTU too low for record expansion"));
3419 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
3420 }
3421
3422 if (max_len > mtu - overhead) {
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003423 max_len = mtu - overhead;
Gilles Peskine449bd832023-01-11 14:50:10 +01003424 }
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003425 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003426#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003427
Hanno Becker0defedb2018-08-10 12:35:02 +01003428#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
3429 !defined(MBEDTLS_SSL_PROTO_DTLS)
3430 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003431#endif
3432
Gilles Peskine449bd832023-01-11 14:50:10 +01003433 return (int) max_len;
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003434}
3435
Gilles Peskine449bd832023-01-11 14:50:10 +01003436int mbedtls_ssl_get_max_in_record_payload(const mbedtls_ssl_context *ssl)
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003437{
3438 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
3439
3440#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
3441 (void) ssl;
3442#endif
3443
3444#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01003445 const size_t mfl = mbedtls_ssl_get_input_max_frag_len(ssl);
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003446
Gilles Peskine449bd832023-01-11 14:50:10 +01003447 if (max_len > mfl) {
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003448 max_len = mfl;
Gilles Peskine449bd832023-01-11 14:50:10 +01003449 }
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003450#endif
3451
Gilles Peskine449bd832023-01-11 14:50:10 +01003452 return (int) max_len;
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003453}
3454
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003455#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003456const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert(const mbedtls_ssl_context *ssl)
Paul Bakkerb0550d92012-10-30 07:51:03 +00003457{
Gilles Peskine449bd832023-01-11 14:50:10 +01003458 if (ssl == NULL || ssl->session == NULL) {
3459 return NULL;
3460 }
Paul Bakkerb0550d92012-10-30 07:51:03 +00003461
Hanno Beckere6824572019-02-07 13:18:46 +00003462#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01003463 return ssl->session->peer_cert;
Hanno Beckere6824572019-02-07 13:18:46 +00003464#else
Gilles Peskine449bd832023-01-11 14:50:10 +01003465 return NULL;
Hanno Beckere6824572019-02-07 13:18:46 +00003466#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00003467}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003468#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00003469
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003470#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003471int mbedtls_ssl_get_session(const mbedtls_ssl_context *ssl,
3472 mbedtls_ssl_session *dst)
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003473{
Hanno Beckere810bbc2021-05-14 16:01:05 +01003474 int ret;
3475
Gilles Peskine449bd832023-01-11 14:50:10 +01003476 if (ssl == NULL ||
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003477 dst == NULL ||
3478 ssl->session == NULL ||
Gilles Peskine449bd832023-01-11 14:50:10 +01003479 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT) {
3480 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003481 }
3482
Hanno Beckere810bbc2021-05-14 16:01:05 +01003483 /* Since Mbed TLS 3.0, mbedtls_ssl_get_session() is no longer
3484 * idempotent: Each session can only be exported once.
3485 *
3486 * (This is in preparation for TLS 1.3 support where we will
3487 * need the ability to export multiple sessions (aka tickets),
3488 * which will be achieved by calling mbedtls_ssl_get_session()
3489 * multiple times until it fails.)
3490 *
3491 * Check whether we have already exported the current session,
3492 * and fail if so.
3493 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003494 if (ssl->session->exported == 1) {
3495 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
3496 }
Hanno Beckere810bbc2021-05-14 16:01:05 +01003497
Gilles Peskine449bd832023-01-11 14:50:10 +01003498 ret = mbedtls_ssl_session_copy(dst, ssl->session);
3499 if (ret != 0) {
3500 return ret;
3501 }
Hanno Beckere810bbc2021-05-14 16:01:05 +01003502
3503 /* Remember that we've exported the session. */
3504 ssl->session->exported = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003505 return 0;
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003506}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003507#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003508
Paul Bakker5121ce52009-01-03 21:22:43 +00003509/*
Hanno Beckera835da52019-05-16 12:39:07 +01003510 * Define ticket header determining Mbed TLS version
3511 * and structure of the ticket.
3512 */
3513
Hanno Becker94ef3b32019-05-16 12:50:45 +01003514/*
Hanno Becker50b59662019-05-28 14:30:45 +01003515 * Define bitflag determining compile-time settings influencing
3516 * structure of serialized SSL sessions.
Hanno Becker94ef3b32019-05-16 12:50:45 +01003517 */
3518
Hanno Becker50b59662019-05-28 14:30:45 +01003519#if defined(MBEDTLS_HAVE_TIME)
Hanno Becker3e088662019-05-29 11:10:18 +01003520#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker50b59662019-05-28 14:30:45 +01003521#else
Hanno Becker3e088662019-05-29 11:10:18 +01003522#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003523#endif /* MBEDTLS_HAVE_TIME */
3524
3525#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker3e088662019-05-29 11:10:18 +01003526#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003527#else
Hanno Becker3e088662019-05-29 11:10:18 +01003528#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003529#endif /* MBEDTLS_X509_CRT_PARSE_C */
3530
3531#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Becker3e088662019-05-29 11:10:18 +01003532#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003533#else
Hanno Becker3e088662019-05-29 11:10:18 +01003534#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003535#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
3536
3537#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Becker3e088662019-05-29 11:10:18 +01003538#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003539#else
Hanno Becker3e088662019-05-29 11:10:18 +01003540#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003541#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
3542
Hanno Becker94ef3b32019-05-16 12:50:45 +01003543#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3e088662019-05-29 11:10:18 +01003544#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003545#else
Hanno Becker3e088662019-05-29 11:10:18 +01003546#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003547#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
3548
Hanno Becker94ef3b32019-05-16 12:50:45 +01003549#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3550#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
3551#else
3552#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
3553#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3554
Hanno Becker3e088662019-05-29 11:10:18 +01003555#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
3556#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
3557#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
3558#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
Hanno Becker37bdbe62021-08-01 05:38:58 +01003559#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 4
3560#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 5
Hanno Becker3e088662019-05-29 11:10:18 +01003561
Hanno Becker50b59662019-05-28 14:30:45 +01003562#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Gilles Peskine449bd832023-01-11 14:50:10 +01003563 ((uint16_t) ( \
3564 (SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT) | \
3565 (SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT) | \
3566 (SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << \
3567 SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT) | \
3568 (SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT) | \
3569 (SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT) | \
3570 (SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT)))
Hanno Becker94ef3b32019-05-16 12:50:45 +01003571
Hanno Beckerf8787072019-05-16 12:41:07 +01003572static unsigned char ssl_serialized_session_header[] = {
Hanno Becker94ef3b32019-05-16 12:50:45 +01003573 MBEDTLS_VERSION_MAJOR,
3574 MBEDTLS_VERSION_MINOR,
3575 MBEDTLS_VERSION_PATCH,
Gilles Peskine449bd832023-01-11 14:50:10 +01003576 MBEDTLS_BYTE_1(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
3577 MBEDTLS_BYTE_0(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
Hanno Beckerf8787072019-05-16 12:41:07 +01003578};
Hanno Beckera835da52019-05-16 12:39:07 +01003579
3580/*
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003581 * Serialize a session in the following format:
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02003582 * (in the presentation language of TLS, RFC 8446 section 3)
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003583 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003584 * struct {
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01003585 *
Hanno Beckerdce50972021-08-01 05:39:23 +01003586 * opaque mbedtls_version[3]; // library version: major, minor, patch
3587 * opaque session_format[2]; // library-version specific 16-bit field
3588 * // determining the format of the remaining
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003589 * // serialized data.
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01003590 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003591 * Note: When updating the format, remember to keep
3592 * these version+format bytes.
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003593 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003594 * // In this version, `session_format` determines
3595 * // the setting of those compile-time
3596 * // configuration options which influence
3597 * // the structure of mbedtls_ssl_session.
3598 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003599 * uint8_t minor_ver; // Protocol minor version. Possible values:
Jerry Yu0c2a7382022-12-06 13:27:25 +08003600 * // - TLS 1.2 (0x0303)
3601 * // - TLS 1.3 (0x0304)
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003602 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003603 * select (serialized_session.tls_version) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003604 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003605 * case MBEDTLS_SSL_VERSION_TLS1_2:
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003606 * serialized_session_tls12 data;
Jerry Yu0c2a7382022-12-06 13:27:25 +08003607 * case MBEDTLS_SSL_VERSION_TLS1_3:
Jerry Yuddda0502022-12-01 19:43:12 +08003608 * serialized_session_tls13 data;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003609 *
3610 * };
3611 *
3612 * } serialized_session;
3613 *
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003614 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003615
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003616MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003617static int ssl_session_save(const mbedtls_ssl_session *session,
3618 unsigned char omit_header,
3619 unsigned char *buf,
3620 size_t buf_len,
3621 size_t *olen)
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003622{
3623 unsigned char *p = buf;
3624 size_t used = 0;
Jerry Yu251a12e2022-07-13 15:15:48 +08003625 size_t remaining_len;
Jerry Yue36fdd62022-08-17 21:31:36 +08003626#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3627 size_t out_len;
3628 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3629#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01003630 if (session == NULL) {
3631 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
3632 }
Jerry Yu438ddd82022-07-07 06:55:50 +00003633
Gilles Peskine449bd832023-01-11 14:50:10 +01003634 if (!omit_header) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003635 /*
3636 * Add Mbed TLS version identifier
3637 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003638 used += sizeof(ssl_serialized_session_header);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003639
Gilles Peskine449bd832023-01-11 14:50:10 +01003640 if (used <= buf_len) {
3641 memcpy(p, ssl_serialized_session_header,
3642 sizeof(ssl_serialized_session_header));
3643 p += sizeof(ssl_serialized_session_header);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003644 }
3645 }
3646
3647 /*
3648 * TLS version identifier
3649 */
3650 used += 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003651 if (used <= buf_len) {
3652 *p++ = MBEDTLS_BYTE_0(session->tls_version);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003653 }
3654
3655 /* Forward to version-specific serialization routine. */
Jerry Yufca4d572022-07-21 10:37:48 +08003656 remaining_len = (buf_len >= used) ? buf_len - used : 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003657 switch (session->tls_version) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003658#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01003659 case MBEDTLS_SSL_VERSION_TLS1_2:
3660 used += ssl_tls12_session_save(session, p, remaining_len);
3661 break;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003662#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3663
Jerry Yu251a12e2022-07-13 15:15:48 +08003664#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01003665 case MBEDTLS_SSL_VERSION_TLS1_3:
3666 ret = ssl_tls13_session_save(session, p, remaining_len, &out_len);
3667 if (ret != 0 && ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL) {
3668 return ret;
3669 }
3670 used += out_len;
3671 break;
Jerry Yu251a12e2022-07-13 15:15:48 +08003672#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3673
Gilles Peskine449bd832023-01-11 14:50:10 +01003674 default:
3675 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003676 }
3677
3678 *olen = used;
Gilles Peskine449bd832023-01-11 14:50:10 +01003679 if (used > buf_len) {
3680 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
3681 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003682
Gilles Peskine449bd832023-01-11 14:50:10 +01003683 return 0;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003684}
3685
3686/*
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003687 * Public wrapper for ssl_session_save()
3688 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003689int mbedtls_ssl_session_save(const mbedtls_ssl_session *session,
3690 unsigned char *buf,
3691 size_t buf_len,
3692 size_t *olen)
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003693{
Gilles Peskine449bd832023-01-11 14:50:10 +01003694 return ssl_session_save(session, 0, buf, buf_len, olen);
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003695}
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003696
Jerry Yuf1b23ca2022-02-18 11:48:47 +08003697/*
3698 * Deserialize session, see mbedtls_ssl_session_save() for format.
3699 *
3700 * This internal version is wrapped by a public function that cleans up in
3701 * case of error, and has an extra option omit_header.
3702 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003703MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003704static int ssl_session_load(mbedtls_ssl_session *session,
3705 unsigned char omit_header,
3706 const unsigned char *buf,
3707 size_t len)
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003708{
3709 const unsigned char *p = buf;
3710 const unsigned char * const end = buf + len;
Jerry Yu438ddd82022-07-07 06:55:50 +00003711 size_t remaining_len;
3712
3713
Gilles Peskine449bd832023-01-11 14:50:10 +01003714 if (session == NULL) {
3715 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
3716 }
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003717
Gilles Peskine449bd832023-01-11 14:50:10 +01003718 if (!omit_header) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003719 /*
3720 * Check Mbed TLS version identifier
3721 */
3722
Gilles Peskine449bd832023-01-11 14:50:10 +01003723 if ((size_t) (end - p) < sizeof(ssl_serialized_session_header)) {
3724 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003725 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003726
3727 if (memcmp(p, ssl_serialized_session_header,
3728 sizeof(ssl_serialized_session_header)) != 0) {
3729 return MBEDTLS_ERR_SSL_VERSION_MISMATCH;
3730 }
3731 p += sizeof(ssl_serialized_session_header);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003732 }
3733
3734 /*
3735 * TLS version identifier
3736 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003737 if (1 > (size_t) (end - p)) {
3738 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3739 }
Glenn Straussda7851c2022-03-14 13:29:48 -04003740 session->tls_version = 0x0300 | *p++;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003741
3742 /* Dispatch according to TLS version. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003743 remaining_len = (end - p);
3744 switch (session->tls_version) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003745#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01003746 case MBEDTLS_SSL_VERSION_TLS1_2:
3747 return ssl_tls12_session_load(session, p, remaining_len);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003748#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3749
Jerry Yu438ddd82022-07-07 06:55:50 +00003750#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01003751 case MBEDTLS_SSL_VERSION_TLS1_3:
3752 return ssl_tls13_session_load(session, p, remaining_len);
Jerry Yu438ddd82022-07-07 06:55:50 +00003753#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3754
Gilles Peskine449bd832023-01-11 14:50:10 +01003755 default:
3756 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003757 }
3758}
3759
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003760/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02003761 * Deserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003762 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003763int mbedtls_ssl_session_load(mbedtls_ssl_session *session,
3764 const unsigned char *buf,
3765 size_t len)
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003766{
Gilles Peskine449bd832023-01-11 14:50:10 +01003767 int ret = ssl_session_load(session, 0, buf, len);
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003768
Gilles Peskine449bd832023-01-11 14:50:10 +01003769 if (ret != 0) {
3770 mbedtls_ssl_session_free(session);
3771 }
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003772
Gilles Peskine449bd832023-01-11 14:50:10 +01003773 return ret;
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003774}
3775
3776/*
Paul Bakker1961b702013-01-25 14:49:24 +01003777 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00003778 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003779MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003780static int ssl_prepare_handshake_step(mbedtls_ssl_context *ssl)
Hanno Becker41934dd2021-08-07 19:13:43 +01003781{
3782 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3783
Ronald Cron66dbf912022-02-02 15:33:46 +01003784 /*
3785 * We may have not been able to send to the peer all the handshake data
Ronald Cron3f20b772022-03-08 16:00:02 +01003786 * that were written into the output buffer by the previous handshake step,
3787 * if the write to the network callback returned with the
Ronald Cron66dbf912022-02-02 15:33:46 +01003788 * #MBEDTLS_ERR_SSL_WANT_WRITE error code.
3789 * We proceed to the next handshake step only when all data from the
3790 * previous one have been sent to the peer, thus we make sure that this is
3791 * the case here by calling `mbedtls_ssl_flush_output()`. The function may
3792 * return with the #MBEDTLS_ERR_SSL_WANT_WRITE error code in which case
3793 * we have to wait before to go ahead.
3794 * In the case of TLS 1.3, handshake step handlers do not send data to the
3795 * peer. Data are only sent here and through
3796 * `mbedtls_ssl_handle_pending_alert` in case an error that triggered an
Andrzej Kurek5c65c572022-04-13 14:28:52 -04003797 * alert occurred.
Ronald Cron66dbf912022-02-02 15:33:46 +01003798 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003799 if ((ret = mbedtls_ssl_flush_output(ssl)) != 0) {
3800 return ret;
3801 }
Hanno Becker41934dd2021-08-07 19:13:43 +01003802
3803#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003804 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3805 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING) {
3806 if ((ret = mbedtls_ssl_flight_transmit(ssl)) != 0) {
3807 return ret;
3808 }
Hanno Becker41934dd2021-08-07 19:13:43 +01003809 }
3810#endif /* MBEDTLS_SSL_PROTO_DTLS */
3811
Gilles Peskine449bd832023-01-11 14:50:10 +01003812 return ret;
Hanno Becker41934dd2021-08-07 19:13:43 +01003813}
3814
Gilles Peskine449bd832023-01-11 14:50:10 +01003815int mbedtls_ssl_handshake_step(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00003816{
Hanno Becker41934dd2021-08-07 19:13:43 +01003817 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003818
Gilles Peskine449bd832023-01-11 14:50:10 +01003819 if (ssl == NULL ||
Hanno Becker41934dd2021-08-07 19:13:43 +01003820 ssl->conf == NULL ||
3821 ssl->handshake == NULL ||
Gilles Peskine449bd832023-01-11 14:50:10 +01003822 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER) {
3823 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Becker41934dd2021-08-07 19:13:43 +01003824 }
3825
Gilles Peskine449bd832023-01-11 14:50:10 +01003826 ret = ssl_prepare_handshake_step(ssl);
3827 if (ret != 0) {
3828 return ret;
3829 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003830
Gilles Peskine449bd832023-01-11 14:50:10 +01003831 ret = mbedtls_ssl_handle_pending_alert(ssl);
3832 if (ret != 0) {
Jerry Yue7047812021-09-13 19:26:39 +08003833 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01003834 }
Jerry Yue7047812021-09-13 19:26:39 +08003835
Tom Cosgrove2fdc7b32022-09-21 12:33:17 +01003836 /* If ssl->conf->endpoint is not one of MBEDTLS_SSL_IS_CLIENT or
3837 * MBEDTLS_SSL_IS_SERVER, this is the return code we give */
3838 ret = MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3839
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003840#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003841 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
3842 MBEDTLS_SSL_DEBUG_MSG(2, ("client state: %s",
3843 mbedtls_ssl_states_str(ssl->state)));
Jerry Yub9930e72021-08-06 17:11:51 +08003844
Gilles Peskine449bd832023-01-11 14:50:10 +01003845 switch (ssl->state) {
Ronald Cron9f0fba32022-02-10 16:45:15 +01003846 case MBEDTLS_SSL_HELLO_REQUEST:
3847 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
Tom Cosgrove87d9c6c2022-09-22 09:27:56 +01003848 ret = 0;
Ronald Cron9f0fba32022-02-10 16:45:15 +01003849 break;
Jerry Yub9930e72021-08-06 17:11:51 +08003850
Ronald Cron9f0fba32022-02-10 16:45:15 +01003851 case MBEDTLS_SSL_CLIENT_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01003852 ret = mbedtls_ssl_write_client_hello(ssl);
Ronald Cron9f0fba32022-02-10 16:45:15 +01003853 break;
3854
3855 default:
3856#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01003857 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
3858 ret = mbedtls_ssl_tls13_handshake_client_step(ssl);
3859 } else {
3860 ret = mbedtls_ssl_handshake_client_step(ssl);
3861 }
Ronald Cron9f0fba32022-02-10 16:45:15 +01003862#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01003863 ret = mbedtls_ssl_handshake_client_step(ssl);
Ronald Cron9f0fba32022-02-10 16:45:15 +01003864#else
Gilles Peskine449bd832023-01-11 14:50:10 +01003865 ret = mbedtls_ssl_tls13_handshake_client_step(ssl);
Ronald Cron9f0fba32022-02-10 16:45:15 +01003866#endif
3867 }
Jerry Yub9930e72021-08-06 17:11:51 +08003868 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003869#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003870#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003871 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Ronald Cron6f135e12021-12-08 16:57:54 +01003872#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01003873 if (mbedtls_ssl_conf_is_tls13_only(ssl->conf)) {
3874 ret = mbedtls_ssl_tls13_handshake_server_step(ssl);
3875 }
Ronald Cron6f135e12021-12-08 16:57:54 +01003876#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yub9930e72021-08-06 17:11:51 +08003877
3878#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01003879 if (mbedtls_ssl_conf_is_tls12_only(ssl->conf)) {
3880 ret = mbedtls_ssl_handshake_server_step(ssl);
3881 }
Jerry Yub9930e72021-08-06 17:11:51 +08003882#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3883 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003884#endif
3885
Gilles Peskine449bd832023-01-11 14:50:10 +01003886 if (ret != 0) {
Jerry Yubbd5a3f2021-09-18 20:50:22 +08003887 /* handshake_step return error. And it is same
3888 * with alert_reason.
3889 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003890 if (ssl->send_alert) {
3891 ret = mbedtls_ssl_handle_pending_alert(ssl);
Jerry Yue7047812021-09-13 19:26:39 +08003892 goto cleanup;
3893 }
3894 }
3895
3896cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01003897 return ret;
Paul Bakker1961b702013-01-25 14:49:24 +01003898}
3899
3900/*
3901 * Perform the SSL handshake
3902 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003903int mbedtls_ssl_handshake(mbedtls_ssl_context *ssl)
Paul Bakker1961b702013-01-25 14:49:24 +01003904{
3905 int ret = 0;
3906
Hanno Beckera817ea42020-10-20 15:20:23 +01003907 /* Sanity checks */
3908
Gilles Peskine449bd832023-01-11 14:50:10 +01003909 if (ssl == NULL || ssl->conf == NULL) {
3910 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3911 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003912
Hanno Beckera817ea42020-10-20 15:20:23 +01003913#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003914 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3915 (ssl->f_set_timer == NULL || ssl->f_get_timer == NULL)) {
3916 MBEDTLS_SSL_DEBUG_MSG(1, ("You must use "
3917 "mbedtls_ssl_set_timer_cb() for DTLS"));
3918 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Beckera817ea42020-10-20 15:20:23 +01003919 }
3920#endif /* MBEDTLS_SSL_PROTO_DTLS */
3921
Gilles Peskine449bd832023-01-11 14:50:10 +01003922 MBEDTLS_SSL_DEBUG_MSG(2, ("=> handshake"));
Paul Bakker1961b702013-01-25 14:49:24 +01003923
Hanno Beckera817ea42020-10-20 15:20:23 +01003924 /* Main handshake loop */
Gilles Peskine449bd832023-01-11 14:50:10 +01003925 while (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER) {
3926 ret = mbedtls_ssl_handshake_step(ssl);
Paul Bakker1961b702013-01-25 14:49:24 +01003927
Gilles Peskine449bd832023-01-11 14:50:10 +01003928 if (ret != 0) {
Paul Bakker1961b702013-01-25 14:49:24 +01003929 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01003930 }
Paul Bakker1961b702013-01-25 14:49:24 +01003931 }
3932
Gilles Peskine449bd832023-01-11 14:50:10 +01003933 MBEDTLS_SSL_DEBUG_MSG(2, ("<= handshake"));
Paul Bakker5121ce52009-01-03 21:22:43 +00003934
Gilles Peskine449bd832023-01-11 14:50:10 +01003935 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003936}
3937
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003938#if defined(MBEDTLS_SSL_RENEGOTIATION)
3939#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003940/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003941 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00003942 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003943MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003944static int ssl_write_hello_request(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003945{
Janos Follath865b3eb2019-12-16 11:46:15 +00003946 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003947
Gilles Peskine449bd832023-01-11 14:50:10 +01003948 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write hello request"));
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003949
3950 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003951 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3952 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003953
Gilles Peskine449bd832023-01-11 14:50:10 +01003954 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
3955 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
3956 return ret;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003957 }
3958
Gilles Peskine449bd832023-01-11 14:50:10 +01003959 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write hello request"));
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003960
Gilles Peskine449bd832023-01-11 14:50:10 +01003961 return 0;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003962}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003963#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003964
3965/*
3966 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003967 * - any side: calling mbedtls_ssl_renegotiate(),
3968 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
3969 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02003970 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003971 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003972 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003973 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003974int mbedtls_ssl_start_renegotiation(mbedtls_ssl_context *ssl)
Paul Bakker48916f92012-09-16 19:57:18 +00003975{
Janos Follath865b3eb2019-12-16 11:46:15 +00003976 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker48916f92012-09-16 19:57:18 +00003977
Gilles Peskine449bd832023-01-11 14:50:10 +01003978 MBEDTLS_SSL_DEBUG_MSG(2, ("=> renegotiate"));
Paul Bakker48916f92012-09-16 19:57:18 +00003979
Gilles Peskine449bd832023-01-11 14:50:10 +01003980 if ((ret = ssl_handshake_init(ssl)) != 0) {
3981 return ret;
3982 }
Paul Bakker48916f92012-09-16 19:57:18 +00003983
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003984 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
3985 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003986#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003987 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3988 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING) {
3989 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003990 ssl->handshake->out_msg_seq = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003991 } else {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003992 ssl->handshake->in_msg_seq = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003993 }
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003994 }
3995#endif
3996
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003997 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
3998 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00003999
Gilles Peskine449bd832023-01-11 14:50:10 +01004000 if ((ret = mbedtls_ssl_handshake(ssl)) != 0) {
4001 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_handshake", ret);
4002 return ret;
Paul Bakker48916f92012-09-16 19:57:18 +00004003 }
4004
Gilles Peskine449bd832023-01-11 14:50:10 +01004005 MBEDTLS_SSL_DEBUG_MSG(2, ("<= renegotiate"));
Paul Bakker48916f92012-09-16 19:57:18 +00004006
Gilles Peskine449bd832023-01-11 14:50:10 +01004007 return 0;
Paul Bakker48916f92012-09-16 19:57:18 +00004008}
4009
4010/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004011 * Renegotiate current connection on client,
4012 * or request renegotiation on server
4013 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004014int mbedtls_ssl_renegotiate(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004015{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004016 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004017
Gilles Peskine449bd832023-01-11 14:50:10 +01004018 if (ssl == NULL || ssl->conf == NULL) {
4019 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4020 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02004021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004022#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004023 /* On server, just send the request */
Gilles Peskine449bd832023-01-11 14:50:10 +01004024 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
4025 if (mbedtls_ssl_is_handshake_over(ssl) == 0) {
4026 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4027 }
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004028
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004029 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02004030
4031 /* Did we already try/start sending HelloRequest? */
Gilles Peskine449bd832023-01-11 14:50:10 +01004032 if (ssl->out_left != 0) {
4033 return mbedtls_ssl_flush_output(ssl);
4034 }
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02004035
Gilles Peskine449bd832023-01-11 14:50:10 +01004036 return ssl_write_hello_request(ssl);
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004037 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004038#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004040#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004041 /*
4042 * On client, either start the renegotiation process or,
4043 * if already in progress, continue the handshake
4044 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004045 if (ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
4046 if (mbedtls_ssl_is_handshake_over(ssl) == 0) {
4047 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004048 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004049
4050 if ((ret = mbedtls_ssl_start_renegotiation(ssl)) != 0) {
4051 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_start_renegotiation", ret);
4052 return ret;
4053 }
4054 } else {
4055 if ((ret = mbedtls_ssl_handshake(ssl)) != 0) {
4056 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_handshake", ret);
4057 return ret;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004058 }
4059 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004060#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004061
Gilles Peskine449bd832023-01-11 14:50:10 +01004062 return ret;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004063}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004064#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004065
Gilles Peskine449bd832023-01-11 14:50:10 +01004066void mbedtls_ssl_handshake_free(mbedtls_ssl_context *ssl)
Paul Bakker48916f92012-09-16 19:57:18 +00004067{
Gilles Peskine9b562d52018-04-25 20:32:43 +02004068 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
4069
Gilles Peskine449bd832023-01-11 14:50:10 +01004070 if (handshake == NULL) {
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004071 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01004072 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004073
Brett Warrene0edc842021-08-17 09:53:13 +01004074#if defined(MBEDTLS_ECP_C)
4075#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01004076 if (ssl->handshake->group_list_heap_allocated) {
4077 mbedtls_free((void *) handshake->group_list);
4078 }
Brett Warrene0edc842021-08-17 09:53:13 +01004079 handshake->group_list = NULL;
4080#endif /* MBEDTLS_DEPRECATED_REMOVED */
4081#endif /* MBEDTLS_ECP_C */
4082
Ronald Crone68ab4f2022-10-05 12:46:29 +02004083#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +08004084#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01004085 if (ssl->handshake->sig_algs_heap_allocated) {
4086 mbedtls_free((void *) handshake->sig_algs);
4087 }
Jerry Yuf017ee42022-01-12 15:49:48 +08004088 handshake->sig_algs = NULL;
4089#endif /* MBEDTLS_DEPRECATED_REMOVED */
Xiaofei Baic234ecf2022-02-08 09:59:23 +00004090#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01004091 if (ssl->handshake->certificate_request_context) {
4092 mbedtls_free((void *) handshake->certificate_request_context);
Xiaofei Baic234ecf2022-02-08 09:59:23 +00004093 }
4094#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Ronald Crone68ab4f2022-10-05 12:46:29 +02004095#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yuf017ee42022-01-12 15:49:48 +08004096
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02004097#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01004098 if (ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0) {
4099 ssl->conf->f_async_cancel(ssl);
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02004100 handshake->async_in_progress = 0;
4101 }
4102#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
4103
Andrzej Kurek25f27152022-08-17 16:09:31 -04004104#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -05004105#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01004106 psa_hash_abort(&handshake->fin_sha256_psa);
Andrzej Kurekeb342242019-01-29 09:14:33 -05004107#else
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01004108 mbedtls_md_free(&handshake->fin_sha256);
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02004109#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05004110#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004111#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -05004112#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01004113 psa_hash_abort(&handshake->fin_sha384_psa);
Andrzej Kurekeb342242019-01-29 09:14:33 -05004114#else
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01004115 mbedtls_md_free(&handshake->fin_sha384);
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02004116#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05004117#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02004118
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004119#if defined(MBEDTLS_DHM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004120 mbedtls_dhm_free(&handshake->dhm_ctx);
Paul Bakker48916f92012-09-16 19:57:18 +00004121#endif
Neil Armstrongf3f46412022-04-12 14:43:39 +02004122#if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004123 mbedtls_ecdh_free(&handshake->ecdh_ctx);
Paul Bakker61d113b2013-07-04 11:51:43 +02004124#endif
Valerio Setti02c25b52022-11-15 14:08:42 +01004125
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02004126#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Neil Armstrongca7d5062022-05-31 14:43:23 +02004127#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01004128 psa_pake_abort(&handshake->psa_pake_ctx);
Valerio Settieb3f7882022-12-08 18:42:58 +01004129 /*
4130 * Opaque keys are not stored in the handshake's data and it's the user
4131 * responsibility to destroy them. Clear ones, instead, are created by
4132 * the TLS library and should be destroyed at the same level
4133 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004134 if (!mbedtls_svc_key_id_is_null(handshake->psa_pake_password)) {
4135 psa_destroy_key(handshake->psa_pake_password);
Valerio Settieb3f7882022-12-08 18:42:58 +01004136 }
Neil Armstrongca7d5062022-05-31 14:43:23 +02004137 handshake->psa_pake_password = MBEDTLS_SVC_KEY_ID_INIT;
4138#else
Gilles Peskine449bd832023-01-11 14:50:10 +01004139 mbedtls_ecjpake_free(&handshake->ecjpake_ctx);
Neil Armstrongca7d5062022-05-31 14:43:23 +02004140#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02004141#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004142 mbedtls_free(handshake->ecjpake_cache);
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02004143 handshake->ecjpake_cache = NULL;
4144 handshake->ecjpake_cache_len = 0;
4145#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02004146#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02004147
Janos Follath4ae5c292016-02-10 11:27:43 +00004148#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
4149 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02004150 /* explicit void pointer cast for buggy MS compiler */
Gilles Peskine449bd832023-01-11 14:50:10 +01004151 mbedtls_free((void *) handshake->curves_tls_id);
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004152#endif
4153
Ronald Cron73fe8df2022-10-05 14:31:43 +02004154#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02004155#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01004156 if (!mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque)) {
Neil Armstrong501c9322022-05-03 09:35:09 +02004157 /* The maintenance of the external PSK key slot is the
4158 * user's responsibility. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004159 if (ssl->handshake->psk_opaque_is_internal) {
4160 psa_destroy_key(ssl->handshake->psk_opaque);
Neil Armstrong501c9322022-05-03 09:35:09 +02004161 ssl->handshake->psk_opaque_is_internal = 0;
4162 }
4163 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
4164 }
Neil Armstronge952a302022-05-03 10:22:14 +02004165#else
Gilles Peskine449bd832023-01-11 14:50:10 +01004166 if (handshake->psk != NULL) {
4167 mbedtls_platform_zeroize(handshake->psk, handshake->psk_len);
4168 mbedtls_free(handshake->psk);
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004169 }
Neil Armstronge952a302022-05-03 10:22:14 +02004170#endif /* MBEDTLS_USE_PSA_CRYPTO */
Ronald Cron73fe8df2022-10-05 14:31:43 +02004171#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004172
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004173#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
4174 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02004175 /*
4176 * Free only the linked list wrapper, not the keys themselves
4177 * since the belong to the SNI callback
4178 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004179 ssl_key_cert_free(handshake->sni_key_cert);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004180#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004181
Gilles Peskineeccd8882020-03-10 12:19:08 +01004182#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01004183 mbedtls_x509_crt_restart_free(&handshake->ecrs_ctx);
4184 if (handshake->ecrs_peer_cert != NULL) {
4185 mbedtls_x509_crt_free(handshake->ecrs_peer_cert);
4186 mbedtls_free(handshake->ecrs_peer_cert);
Hanno Becker3dad3112019-02-05 17:19:52 +00004187 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02004188#endif
4189
Hanno Becker75173122019-02-06 16:18:31 +00004190#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
4191 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01004192 mbedtls_pk_free(&handshake->peer_pubkey);
Hanno Becker75173122019-02-06 16:18:31 +00004193#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
4194
XiaokangQian9b93c0d2022-02-09 06:02:25 +00004195#if defined(MBEDTLS_SSL_CLI_C) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01004196 (defined(MBEDTLS_SSL_PROTO_DTLS) || defined(MBEDTLS_SSL_PROTO_TLS1_3))
4197 mbedtls_free(handshake->cookie);
XiaokangQian9b93c0d2022-02-09 06:02:25 +00004198#endif /* MBEDTLS_SSL_CLI_C &&
4199 ( MBEDTLS_SSL_PROTO_DTLS || MBEDTLS_SSL_PROTO_TLS1_3 ) */
XiaokangQian8499b6c2022-01-27 09:00:11 +00004200
4201#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004202 mbedtls_ssl_flight_free(handshake->flight);
4203 mbedtls_ssl_buffering_free(ssl);
XiaokangQian8499b6c2022-01-27 09:00:11 +00004204#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02004205
Ronald Cronf12b81d2022-03-15 10:42:41 +01004206#if defined(MBEDTLS_ECDH_C) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01004207 (defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3))
4208 if (handshake->ecdh_psa_privkey_is_external == 0) {
4209 psa_destroy_key(handshake->ecdh_psa_privkey);
4210 }
Hanno Becker4a63ed42019-01-08 11:39:35 +00004211#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
4212
Ronald Cron6f135e12021-12-08 16:57:54 +01004213#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01004214 mbedtls_ssl_transform_free(handshake->transform_handshake);
4215 mbedtls_free(handshake->transform_handshake);
Jerry Yu3d9b5902022-11-04 14:07:25 +08004216#if defined(MBEDTLS_SSL_EARLY_DATA)
Gilles Peskine449bd832023-01-11 14:50:10 +01004217 mbedtls_ssl_transform_free(handshake->transform_earlydata);
4218 mbedtls_free(handshake->transform_earlydata);
Jerry Yu3d9b5902022-11-04 14:07:25 +08004219#endif
Ronald Cron6f135e12021-12-08 16:57:54 +01004220#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yuba9c7272021-10-30 11:54:10 +08004221
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004222
4223#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4224 /* If the buffers are too big - reallocate. Because of the way Mbed TLS
4225 * processes datagrams and the fact that a datagram is allowed to have
4226 * several records in it, it is possible that the I/O buffers are not
4227 * empty at this stage */
Gilles Peskine449bd832023-01-11 14:50:10 +01004228 handle_buffer_resizing(ssl, 1, mbedtls_ssl_get_input_buflen(ssl),
4229 mbedtls_ssl_get_output_buflen(ssl));
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004230#endif
Hanno Becker3aa186f2021-08-10 09:24:19 +01004231
Jerry Yuba9c7272021-10-30 11:54:10 +08004232 /* mbedtls_platform_zeroize MUST be last one in this function */
Gilles Peskine449bd832023-01-11 14:50:10 +01004233 mbedtls_platform_zeroize(handshake,
4234 sizeof(mbedtls_ssl_handshake_params));
Paul Bakker48916f92012-09-16 19:57:18 +00004235}
4236
Gilles Peskine449bd832023-01-11 14:50:10 +01004237void mbedtls_ssl_session_free(mbedtls_ssl_session *session)
Paul Bakker48916f92012-09-16 19:57:18 +00004238{
Gilles Peskine449bd832023-01-11 14:50:10 +01004239 if (session == NULL) {
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004240 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01004241 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004242
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004243#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004244 ssl_clear_peer_cert(session);
Paul Bakkered27a042013-04-18 22:46:23 +02004245#endif
Paul Bakker0a597072012-09-25 21:55:46 +00004246
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02004247#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Xiaokang Qianbc663a02022-10-09 11:14:39 +00004248#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
4249 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01004250 mbedtls_free(session->hostname);
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00004251#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01004252 mbedtls_free(session->ticket);
Paul Bakkera503a632013-08-14 13:48:06 +02004253#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004254
Gilles Peskine449bd832023-01-11 14:50:10 +01004255 mbedtls_platform_zeroize(session, sizeof(mbedtls_ssl_session));
Paul Bakker48916f92012-09-16 19:57:18 +00004256}
4257
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02004258#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004259
4260#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4261#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u
4262#else
4263#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u
4264#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4265
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004266#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004267
4268#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4269#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u
4270#else
4271#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u
4272#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4273
4274#if defined(MBEDTLS_SSL_ALPN)
4275#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u
4276#else
4277#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u
4278#endif /* MBEDTLS_SSL_ALPN */
4279
4280#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0
4281#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1
4282#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2
4283#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3
4284
4285#define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \
Gilles Peskine449bd832023-01-11 14:50:10 +01004286 ((uint32_t) ( \
4287 (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << \
4288 SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT) | \
4289 (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << \
4290 SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT) | \
4291 (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << \
4292 SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT) | \
4293 (SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT) | \
4294 0u))
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004295
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004296static unsigned char ssl_serialized_context_header[] = {
4297 MBEDTLS_VERSION_MAJOR,
4298 MBEDTLS_VERSION_MINOR,
4299 MBEDTLS_VERSION_PATCH,
Gilles Peskine449bd832023-01-11 14:50:10 +01004300 MBEDTLS_BYTE_1(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
4301 MBEDTLS_BYTE_0(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
4302 MBEDTLS_BYTE_2(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG),
4303 MBEDTLS_BYTE_1(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG),
4304 MBEDTLS_BYTE_0(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG),
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004305};
4306
Paul Bakker5121ce52009-01-03 21:22:43 +00004307/*
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004308 * Serialize a full SSL context
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02004309 *
4310 * The format of the serialized data is:
4311 * (in the presentation language of TLS, RFC 8446 section 3)
4312 *
4313 * // header
4314 * opaque mbedtls_version[3]; // major, minor, patch
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004315 * opaque context_format[5]; // version-specific field determining
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02004316 * // the format of the remaining
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004317 * // serialized data.
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004318 * Note: When updating the format, remember to keep these
4319 * version+format bytes. (We may make their size part of the API.)
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02004320 *
4321 * // session sub-structure
4322 * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save()
4323 * // transform sub-structure
4324 * uint8 random[64]; // ServerHello.random+ClientHello.random
4325 * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value
4326 * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use
4327 * // fields from ssl_context
4328 * uint32 badmac_seen; // DTLS: number of records with failing MAC
4329 * uint64 in_window_top; // DTLS: last validated record seq_num
4330 * uint64 in_window; // DTLS: bitmask for replay protection
4331 * uint8 disable_datagram_packing; // DTLS: only one record per datagram
4332 * uint64 cur_out_ctr; // Record layer: outgoing sequence number
4333 * uint16 mtu; // DTLS: path mtu (max outgoing fragment size)
4334 * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol
4335 *
4336 * Note that many fields of the ssl_context or sub-structures are not
4337 * serialized, as they fall in one of the following categories:
4338 *
4339 * 1. forced value (eg in_left must be 0)
4340 * 2. pointer to dynamically-allocated memory (eg session, transform)
4341 * 3. value can be re-derived from other data (eg session keys from MS)
4342 * 4. value was temporary (eg content of input buffer)
4343 * 5. value will be provided by the user again (eg I/O callbacks and context)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004344 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004345int mbedtls_ssl_context_save(mbedtls_ssl_context *ssl,
4346 unsigned char *buf,
4347 size_t buf_len,
4348 size_t *olen)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004349{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004350 unsigned char *p = buf;
4351 size_t used = 0;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004352 size_t session_len;
4353 int ret = 0;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004354
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02004355 /*
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004356 * Enforce usage restrictions, see "return BAD_INPUT_DATA" in
4357 * this function's documentation.
4358 *
4359 * These are due to assumptions/limitations in the implementation. Some of
4360 * them are likely to stay (no handshake in progress) some might go away
4361 * (only DTLS) but are currently used to simplify the implementation.
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02004362 */
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004363 /* The initial handshake must be over */
Gilles Peskine449bd832023-01-11 14:50:10 +01004364 if (mbedtls_ssl_is_handshake_over(ssl) == 0) {
4365 MBEDTLS_SSL_DEBUG_MSG(1, ("Initial handshake isn't over"));
4366 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004367 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004368 if (ssl->handshake != NULL) {
4369 MBEDTLS_SSL_DEBUG_MSG(1, ("Handshake isn't completed"));
4370 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004371 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004372 /* Double-check that sub-structures are indeed ready */
Gilles Peskine449bd832023-01-11 14:50:10 +01004373 if (ssl->transform == NULL || ssl->session == NULL) {
4374 MBEDTLS_SSL_DEBUG_MSG(1, ("Serialised structures aren't ready"));
4375 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004376 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004377 /* There must be no pending incoming or outgoing data */
Gilles Peskine449bd832023-01-11 14:50:10 +01004378 if (mbedtls_ssl_check_pending(ssl) != 0) {
4379 MBEDTLS_SSL_DEBUG_MSG(1, ("There is pending incoming data"));
4380 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004381 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004382 if (ssl->out_left != 0) {
4383 MBEDTLS_SSL_DEBUG_MSG(1, ("There is pending outgoing data"));
4384 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004385 }
Dave Rodgman556e8a32022-12-06 16:31:25 +00004386 /* Protocol must be DTLS, not TLS */
Gilles Peskine449bd832023-01-11 14:50:10 +01004387 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
4388 MBEDTLS_SSL_DEBUG_MSG(1, ("Only DTLS is supported"));
4389 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004390 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004391 /* Version must be 1.2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004392 if (ssl->tls_version != MBEDTLS_SSL_VERSION_TLS1_2) {
4393 MBEDTLS_SSL_DEBUG_MSG(1, ("Only version 1.2 supported"));
4394 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004395 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004396 /* We must be using an AEAD ciphersuite */
Gilles Peskine449bd832023-01-11 14:50:10 +01004397 if (mbedtls_ssl_transform_uses_aead(ssl->transform) != 1) {
4398 MBEDTLS_SSL_DEBUG_MSG(1, ("Only AEAD ciphersuites supported"));
4399 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004400 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004401 /* Renegotiation must not be enabled */
4402#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01004403 if (ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED) {
4404 MBEDTLS_SSL_DEBUG_MSG(1, ("Renegotiation must not be enabled"));
4405 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004406 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004407#endif
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004408
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004409 /*
4410 * Version and format identifier
4411 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004412 used += sizeof(ssl_serialized_context_header);
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004413
Gilles Peskine449bd832023-01-11 14:50:10 +01004414 if (used <= buf_len) {
4415 memcpy(p, ssl_serialized_context_header,
4416 sizeof(ssl_serialized_context_header));
4417 p += sizeof(ssl_serialized_context_header);
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004418 }
4419
4420 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004421 * Session (length + data)
4422 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004423 ret = ssl_session_save(ssl->session, 1, NULL, 0, &session_len);
4424 if (ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL) {
4425 return ret;
4426 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004427
4428 used += 4 + session_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004429 if (used <= buf_len) {
4430 MBEDTLS_PUT_UINT32_BE(session_len, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004431 p += 4;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004432
Gilles Peskine449bd832023-01-11 14:50:10 +01004433 ret = ssl_session_save(ssl->session, 1,
4434 p, session_len, &session_len);
4435 if (ret != 0) {
4436 return ret;
4437 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004438
4439 p += session_len;
4440 }
4441
4442 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004443 * Transform
4444 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004445 used += sizeof(ssl->transform->randbytes);
4446 if (used <= buf_len) {
4447 memcpy(p, ssl->transform->randbytes,
4448 sizeof(ssl->transform->randbytes));
4449 p += sizeof(ssl->transform->randbytes);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004450 }
4451
4452#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4453 used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004454 if (used <= buf_len) {
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004455 *p++ = ssl->transform->in_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004456 memcpy(p, ssl->transform->in_cid, ssl->transform->in_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004457 p += ssl->transform->in_cid_len;
4458
4459 *p++ = ssl->transform->out_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004460 memcpy(p, ssl->transform->out_cid, ssl->transform->out_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004461 p += ssl->transform->out_cid_len;
4462 }
4463#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4464
4465 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004466 * Saved fields from top-level ssl_context structure
4467 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004468 used += 4;
Gilles Peskine449bd832023-01-11 14:50:10 +01004469 if (used <= buf_len) {
4470 MBEDTLS_PUT_UINT32_BE(ssl->badmac_seen, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004471 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004472 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004473
4474#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4475 used += 16;
Gilles Peskine449bd832023-01-11 14:50:10 +01004476 if (used <= buf_len) {
4477 MBEDTLS_PUT_UINT64_BE(ssl->in_window_top, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004478 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004479
Gilles Peskine449bd832023-01-11 14:50:10 +01004480 MBEDTLS_PUT_UINT64_BE(ssl->in_window, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004481 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004482 }
4483#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4484
4485#if defined(MBEDTLS_SSL_PROTO_DTLS)
4486 used += 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004487 if (used <= buf_len) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004488 *p++ = ssl->disable_datagram_packing;
4489 }
4490#endif /* MBEDTLS_SSL_PROTO_DTLS */
4491
Jerry Yuae0b2e22021-10-08 15:21:19 +08004492 used += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Gilles Peskine449bd832023-01-11 14:50:10 +01004493 if (used <= buf_len) {
4494 memcpy(p, ssl->cur_out_ctr, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN);
Jerry Yuae0b2e22021-10-08 15:21:19 +08004495 p += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004496 }
4497
4498#if defined(MBEDTLS_SSL_PROTO_DTLS)
4499 used += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01004500 if (used <= buf_len) {
4501 MBEDTLS_PUT_UINT16_BE(ssl->mtu, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004502 p += 2;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004503 }
4504#endif /* MBEDTLS_SSL_PROTO_DTLS */
4505
4506#if defined(MBEDTLS_SSL_ALPN)
4507 {
4508 const uint8_t alpn_len = ssl->alpn_chosen
Gilles Peskine449bd832023-01-11 14:50:10 +01004509 ? (uint8_t) strlen(ssl->alpn_chosen)
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004510 : 0;
4511
4512 used += 1 + alpn_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004513 if (used <= buf_len) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004514 *p++ = alpn_len;
4515
Gilles Peskine449bd832023-01-11 14:50:10 +01004516 if (ssl->alpn_chosen != NULL) {
4517 memcpy(p, ssl->alpn_chosen, alpn_len);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004518 p += alpn_len;
4519 }
4520 }
4521 }
4522#endif /* MBEDTLS_SSL_ALPN */
4523
4524 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004525 * Done
4526 */
4527 *olen = used;
4528
Gilles Peskine449bd832023-01-11 14:50:10 +01004529 if (used > buf_len) {
4530 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
4531 }
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004532
Gilles Peskine449bd832023-01-11 14:50:10 +01004533 MBEDTLS_SSL_DEBUG_BUF(4, "saved context", buf, used);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004534
Gilles Peskine449bd832023-01-11 14:50:10 +01004535 return mbedtls_ssl_session_reset_int(ssl, 0);
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004536}
4537
4538/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02004539 * Deserialize context, see mbedtls_ssl_context_save() for format.
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004540 *
4541 * This internal version is wrapped by a public function that cleans up in
4542 * case of error.
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004543 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004544MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01004545static int ssl_context_load(mbedtls_ssl_context *ssl,
4546 const unsigned char *buf,
4547 size_t len)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004548{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004549 const unsigned char *p = buf;
4550 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004551 size_t session_len;
Janos Follath865b3eb2019-12-16 11:46:15 +00004552 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004553#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek894edde2022-09-29 06:31:14 -04004554 tls_prf_fn prf_func = NULL;
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004555#endif
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004556
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004557 /*
4558 * The context should have been freshly setup or reset.
4559 * Give the user an error in case of obvious misuse.
Manuel Pégourié-Gonnard4ca930f2019-07-26 16:31:53 +02004560 * (Checking session is useful because it won't be NULL if we're
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004561 * renegotiating, or if the user mistakenly loaded a session first.)
4562 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004563 if (ssl->state != MBEDTLS_SSL_HELLO_REQUEST ||
4564 ssl->session != NULL) {
4565 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004566 }
4567
4568 /*
4569 * We can't check that the config matches the initial one, but we can at
4570 * least check it matches the requirements for serializing.
4571 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004572 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
Glenn Strauss2dfcea22022-03-14 17:26:42 -04004573 ssl->conf->max_tls_version < MBEDTLS_SSL_VERSION_TLS1_2 ||
4574 ssl->conf->min_tls_version > MBEDTLS_SSL_VERSION_TLS1_2 ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004575#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02004576 ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004577#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01004578 0) {
4579 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004580 }
4581
Gilles Peskine449bd832023-01-11 14:50:10 +01004582 MBEDTLS_SSL_DEBUG_BUF(4, "context to load", buf, len);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004583
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004584 /*
4585 * Check version identifier
4586 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004587 if ((size_t) (end - p) < sizeof(ssl_serialized_context_header)) {
4588 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004589 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004590
4591 if (memcmp(p, ssl_serialized_context_header,
4592 sizeof(ssl_serialized_context_header)) != 0) {
4593 return MBEDTLS_ERR_SSL_VERSION_MISMATCH;
4594 }
4595 p += sizeof(ssl_serialized_context_header);
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004596
4597 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004598 * Session
4599 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004600 if ((size_t) (end - p) < 4) {
4601 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4602 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004603
Gilles Peskine449bd832023-01-11 14:50:10 +01004604 session_len = ((size_t) p[0] << 24) |
4605 ((size_t) p[1] << 16) |
4606 ((size_t) p[2] << 8) |
4607 ((size_t) p[3]);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004608 p += 4;
4609
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004610 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00004611 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004612 ssl->session = ssl->session_negotiate;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004613 ssl->session_in = ssl->session;
4614 ssl->session_out = ssl->session;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004615 ssl->session_negotiate = NULL;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004616
Gilles Peskine449bd832023-01-11 14:50:10 +01004617 if ((size_t) (end - p) < session_len) {
4618 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4619 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004620
Gilles Peskine449bd832023-01-11 14:50:10 +01004621 ret = ssl_session_load(ssl->session, 1, p, session_len);
4622 if (ret != 0) {
4623 mbedtls_ssl_session_free(ssl->session);
4624 return ret;
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004625 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004626
4627 p += session_len;
4628
4629 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004630 * Transform
4631 */
4632
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004633 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00004634 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Jerry Yu2e199812022-12-01 18:57:19 +08004635#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004636 ssl->transform = ssl->transform_negotiate;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004637 ssl->transform_in = ssl->transform;
4638 ssl->transform_out = ssl->transform;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004639 ssl->transform_negotiate = NULL;
Jerry Yu2e199812022-12-01 18:57:19 +08004640#endif
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004641
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004642#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01004643 prf_func = ssl_tls12prf_from_cs(ssl->session->ciphersuite);
4644 if (prf_func == NULL) {
4645 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4646 }
Andrzej Kurek894edde2022-09-29 06:31:14 -04004647
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004648 /* Read random bytes and populate structure */
Gilles Peskine449bd832023-01-11 14:50:10 +01004649 if ((size_t) (end - p) < sizeof(ssl->transform->randbytes)) {
4650 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4651 }
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004652
Gilles Peskine449bd832023-01-11 14:50:10 +01004653 ret = ssl_tls12_populate_transform(ssl->transform,
4654 ssl->session->ciphersuite,
4655 ssl->session->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02004656#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004657 ssl->session->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02004658#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01004659 prf_func,
4660 p, /* currently pointing to randbytes */
4661 MBEDTLS_SSL_VERSION_TLS1_2, /* (D)TLS 1.2 is forced */
4662 ssl->conf->endpoint,
4663 ssl);
4664 if (ret != 0) {
4665 return ret;
4666 }
Jerry Yu840fbb22022-02-17 14:59:29 +08004667#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004668 p += sizeof(ssl->transform->randbytes);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004669
4670#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4671 /* Read connection IDs and store them */
Gilles Peskine449bd832023-01-11 14:50:10 +01004672 if ((size_t) (end - p) < 1) {
4673 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4674 }
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004675
4676 ssl->transform->in_cid_len = *p++;
4677
Gilles Peskine449bd832023-01-11 14:50:10 +01004678 if ((size_t) (end - p) < ssl->transform->in_cid_len + 1u) {
4679 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4680 }
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004681
Gilles Peskine449bd832023-01-11 14:50:10 +01004682 memcpy(ssl->transform->in_cid, p, ssl->transform->in_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004683 p += ssl->transform->in_cid_len;
4684
4685 ssl->transform->out_cid_len = *p++;
4686
Gilles Peskine449bd832023-01-11 14:50:10 +01004687 if ((size_t) (end - p) < ssl->transform->out_cid_len) {
4688 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4689 }
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004690
Gilles Peskine449bd832023-01-11 14:50:10 +01004691 memcpy(ssl->transform->out_cid, p, ssl->transform->out_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004692 p += ssl->transform->out_cid_len;
4693#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4694
4695 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004696 * Saved fields from top-level ssl_context structure
4697 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004698 if ((size_t) (end - p) < 4) {
4699 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4700 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004701
Gilles Peskine449bd832023-01-11 14:50:10 +01004702 ssl->badmac_seen = ((uint32_t) p[0] << 24) |
4703 ((uint32_t) p[1] << 16) |
4704 ((uint32_t) p[2] << 8) |
4705 ((uint32_t) p[3]);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004706 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004707
4708#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine449bd832023-01-11 14:50:10 +01004709 if ((size_t) (end - p) < 16) {
4710 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4711 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004712
Gilles Peskine449bd832023-01-11 14:50:10 +01004713 ssl->in_window_top = ((uint64_t) p[0] << 56) |
4714 ((uint64_t) p[1] << 48) |
4715 ((uint64_t) p[2] << 40) |
4716 ((uint64_t) p[3] << 32) |
4717 ((uint64_t) p[4] << 24) |
4718 ((uint64_t) p[5] << 16) |
4719 ((uint64_t) p[6] << 8) |
4720 ((uint64_t) p[7]);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004721 p += 8;
4722
Gilles Peskine449bd832023-01-11 14:50:10 +01004723 ssl->in_window = ((uint64_t) p[0] << 56) |
4724 ((uint64_t) p[1] << 48) |
4725 ((uint64_t) p[2] << 40) |
4726 ((uint64_t) p[3] << 32) |
4727 ((uint64_t) p[4] << 24) |
4728 ((uint64_t) p[5] << 16) |
4729 ((uint64_t) p[6] << 8) |
4730 ((uint64_t) p[7]);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004731 p += 8;
4732#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4733
4734#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004735 if ((size_t) (end - p) < 1) {
4736 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4737 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004738
4739 ssl->disable_datagram_packing = *p++;
4740#endif /* MBEDTLS_SSL_PROTO_DTLS */
4741
Gilles Peskine449bd832023-01-11 14:50:10 +01004742 if ((size_t) (end - p) < sizeof(ssl->cur_out_ctr)) {
4743 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4744 }
4745 memcpy(ssl->cur_out_ctr, p, sizeof(ssl->cur_out_ctr));
4746 p += sizeof(ssl->cur_out_ctr);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004747
4748#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004749 if ((size_t) (end - p) < 2) {
4750 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4751 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004752
Gilles Peskine449bd832023-01-11 14:50:10 +01004753 ssl->mtu = (p[0] << 8) | p[1];
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004754 p += 2;
4755#endif /* MBEDTLS_SSL_PROTO_DTLS */
4756
4757#if defined(MBEDTLS_SSL_ALPN)
4758 {
4759 uint8_t alpn_len;
4760 const char **cur;
4761
Gilles Peskine449bd832023-01-11 14:50:10 +01004762 if ((size_t) (end - p) < 1) {
4763 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4764 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004765
4766 alpn_len = *p++;
4767
Gilles Peskine449bd832023-01-11 14:50:10 +01004768 if (alpn_len != 0 && ssl->conf->alpn_list != NULL) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004769 /* alpn_chosen should point to an item in the configured list */
Gilles Peskine449bd832023-01-11 14:50:10 +01004770 for (cur = ssl->conf->alpn_list; *cur != NULL; cur++) {
4771 if (strlen(*cur) == alpn_len &&
4772 memcmp(p, cur, alpn_len) == 0) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004773 ssl->alpn_chosen = *cur;
4774 break;
4775 }
4776 }
4777 }
4778
4779 /* can only happen on conf mismatch */
Gilles Peskine449bd832023-01-11 14:50:10 +01004780 if (alpn_len != 0 && ssl->alpn_chosen == NULL) {
4781 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4782 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004783
4784 p += alpn_len;
4785 }
4786#endif /* MBEDTLS_SSL_ALPN */
4787
4788 /*
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004789 * Forced fields from top-level ssl_context structure
4790 *
4791 * Most of them already set to the correct value by mbedtls_ssl_init() and
4792 * mbedtls_ssl_reset(), so we only need to set the remaining ones.
4793 */
4794 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
Glenn Strauss60bfe602022-03-14 19:04:24 -04004795 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004796
Hanno Becker361b10d2019-08-30 10:42:49 +01004797 /* Adjust pointers for header fields of outgoing records to
4798 * the given transform, accounting for explicit IV and CID. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004799 mbedtls_ssl_update_out_pointers(ssl, ssl->transform);
Hanno Becker361b10d2019-08-30 10:42:49 +01004800
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004801#if defined(MBEDTLS_SSL_PROTO_DTLS)
4802 ssl->in_epoch = 1;
4803#endif
4804
4805 /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated,
4806 * which we don't want - otherwise we'd end up freeing the wrong transform
Hanno Beckerce5f5fd2020-02-05 10:47:44 +00004807 * by calling mbedtls_ssl_handshake_wrapup_free_hs_transform()
4808 * inappropriately. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004809 if (ssl->handshake != NULL) {
4810 mbedtls_ssl_handshake_free(ssl);
4811 mbedtls_free(ssl->handshake);
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004812 ssl->handshake = NULL;
4813 }
4814
4815 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004816 * Done - should have consumed entire buffer
4817 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004818 if (p != end) {
4819 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4820 }
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004821
Gilles Peskine449bd832023-01-11 14:50:10 +01004822 return 0;
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004823}
4824
4825/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02004826 * Deserialize context: public wrapper for error cleaning
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004827 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004828int mbedtls_ssl_context_load(mbedtls_ssl_context *context,
4829 const unsigned char *buf,
4830 size_t len)
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004831{
Gilles Peskine449bd832023-01-11 14:50:10 +01004832 int ret = ssl_context_load(context, buf, len);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004833
Gilles Peskine449bd832023-01-11 14:50:10 +01004834 if (ret != 0) {
4835 mbedtls_ssl_free(context);
4836 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004837
Gilles Peskine449bd832023-01-11 14:50:10 +01004838 return ret;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004839}
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02004840#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004841
4842/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004843 * Free an SSL context
4844 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004845void mbedtls_ssl_free(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00004846{
Gilles Peskine449bd832023-01-11 14:50:10 +01004847 if (ssl == NULL) {
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004848 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01004849 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004850
Gilles Peskine449bd832023-01-11 14:50:10 +01004851 MBEDTLS_SSL_DEBUG_MSG(2, ("=> free"));
Paul Bakker5121ce52009-01-03 21:22:43 +00004852
Gilles Peskine449bd832023-01-11 14:50:10 +01004853 if (ssl->out_buf != NULL) {
sander-visserb8aa2072020-05-06 22:05:13 +02004854#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4855 size_t out_buf_len = ssl->out_buf_len;
4856#else
4857 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
4858#endif
4859
Gilles Peskine449bd832023-01-11 14:50:10 +01004860 mbedtls_platform_zeroize(ssl->out_buf, out_buf_len);
4861 mbedtls_free(ssl->out_buf);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004862 ssl->out_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004863 }
4864
Gilles Peskine449bd832023-01-11 14:50:10 +01004865 if (ssl->in_buf != NULL) {
sander-visserb8aa2072020-05-06 22:05:13 +02004866#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4867 size_t in_buf_len = ssl->in_buf_len;
4868#else
4869 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
4870#endif
4871
Gilles Peskine449bd832023-01-11 14:50:10 +01004872 mbedtls_platform_zeroize(ssl->in_buf, in_buf_len);
4873 mbedtls_free(ssl->in_buf);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004874 ssl->in_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004875 }
4876
Gilles Peskine449bd832023-01-11 14:50:10 +01004877 if (ssl->transform) {
4878 mbedtls_ssl_transform_free(ssl->transform);
4879 mbedtls_free(ssl->transform);
Paul Bakker48916f92012-09-16 19:57:18 +00004880 }
4881
Gilles Peskine449bd832023-01-11 14:50:10 +01004882 if (ssl->handshake) {
4883 mbedtls_ssl_handshake_free(ssl);
4884 mbedtls_free(ssl->handshake);
Jerry Yu2e199812022-12-01 18:57:19 +08004885
4886#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01004887 mbedtls_ssl_transform_free(ssl->transform_negotiate);
4888 mbedtls_free(ssl->transform_negotiate);
Jerry Yu2e199812022-12-01 18:57:19 +08004889#endif
4890
Gilles Peskine449bd832023-01-11 14:50:10 +01004891 mbedtls_ssl_session_free(ssl->session_negotiate);
4892 mbedtls_free(ssl->session_negotiate);
Paul Bakker48916f92012-09-16 19:57:18 +00004893 }
4894
Ronald Cron6f135e12021-12-08 16:57:54 +01004895#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01004896 mbedtls_ssl_transform_free(ssl->transform_application);
4897 mbedtls_free(ssl->transform_application);
Ronald Cron6f135e12021-12-08 16:57:54 +01004898#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker3aa186f2021-08-10 09:24:19 +01004899
Gilles Peskine449bd832023-01-11 14:50:10 +01004900 if (ssl->session) {
4901 mbedtls_ssl_session_free(ssl->session);
4902 mbedtls_free(ssl->session);
Paul Bakkerc0463502013-02-14 11:19:38 +01004903 }
4904
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02004905#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004906 if (ssl->hostname != NULL) {
4907 mbedtls_platform_zeroize(ssl->hostname, strlen(ssl->hostname));
4908 mbedtls_free(ssl->hostname);
Paul Bakker5121ce52009-01-03 21:22:43 +00004909 }
Paul Bakker0be444a2013-08-27 21:55:01 +02004910#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004911
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02004912#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004913 mbedtls_free(ssl->cli_id);
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02004914#endif
4915
Gilles Peskine449bd832023-01-11 14:50:10 +01004916 MBEDTLS_SSL_DEBUG_MSG(2, ("<= free"));
Paul Bakker2da561c2009-02-05 18:00:28 +00004917
Paul Bakker86f04f42013-02-14 11:20:09 +01004918 /* Actually clear after last debug message */
Gilles Peskine449bd832023-01-11 14:50:10 +01004919 mbedtls_platform_zeroize(ssl, sizeof(mbedtls_ssl_context));
Paul Bakker5121ce52009-01-03 21:22:43 +00004920}
4921
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004922/*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08004923 * Initialize mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004924 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004925void mbedtls_ssl_config_init(mbedtls_ssl_config *conf)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004926{
Gilles Peskine449bd832023-01-11 14:50:10 +01004927 memset(conf, 0, sizeof(mbedtls_ssl_config));
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004928}
4929
Gilles Peskineae270bf2021-06-02 00:05:29 +02004930/* The selection should be the same as mbedtls_x509_crt_profile_default in
4931 * x509_crt.c, plus Montgomery curves for ECDHE. Here, the order matters:
Gilles Peskineb1940a72021-06-02 15:18:12 +02004932 * curves with a lower resource usage come first.
Gilles Peskineae270bf2021-06-02 00:05:29 +02004933 * See the documentation of mbedtls_ssl_conf_curves() for what we promise
Gilles Peskineb1940a72021-06-02 15:18:12 +02004934 * about this list.
4935 */
Brett Warrene0edc842021-08-17 09:53:13 +01004936static uint16_t ssl_preset_default_groups[] = {
Gilles Peskineae270bf2021-06-02 00:05:29 +02004937#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004938 MBEDTLS_SSL_IANA_TLS_GROUP_X25519,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004939#endif
4940#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004941 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004942#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02004943#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004944 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004945#endif
4946#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004947 MBEDTLS_SSL_IANA_TLS_GROUP_X448,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004948#endif
4949#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004950 MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004951#endif
Gilles Peskineae270bf2021-06-02 00:05:29 +02004952#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004953 MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004954#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02004955#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004956 MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004957#endif
4958#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004959 MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004960#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004961 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Gilles Peskineae270bf2021-06-02 00:05:29 +02004962};
Gilles Peskineae270bf2021-06-02 00:05:29 +02004963
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004964static int ssl_preset_suiteb_ciphersuites[] = {
4965 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
4966 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
4967 0
4968};
4969
Ronald Crone68ab4f2022-10-05 12:46:29 +02004970#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004971
Jerry Yu909df7b2022-01-22 11:56:27 +08004972/* NOTICE:
Jerry Yu0b994b82022-01-25 17:22:12 +08004973 * For ssl_preset_*_sig_algs and ssl_tls12_preset_*_sig_algs, the following
Jerry Yu370e1462022-01-25 10:36:53 +08004974 * rules SHOULD be upheld.
4975 * - No duplicate entries.
4976 * - But if there is a good reason, do not change the order of the algorithms.
Jerry Yu09a99fc2022-07-28 14:22:17 +08004977 * - ssl_tls12_preset* is for TLS 1.2 use only.
Jerry Yu370e1462022-01-25 10:36:53 +08004978 * - ssl_preset_* is for TLS 1.3 only or hybrid TLS 1.3/1.2 handshakes.
Jerry Yu1a8b4812022-01-20 17:56:50 +08004979 */
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004980static uint16_t ssl_preset_default_sig_algs[] = {
Jerry Yu1a8b4812022-01-20 17:56:50 +08004981
Valerio Setti75fba322023-02-23 17:35:09 +01004982#if defined(MBEDTLS_PK_CAN_ECDSA_SOME) && \
4983 defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
Jerry Yued5e9f42022-01-26 11:21:34 +08004984 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
4985 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Valerio Setti75fba322023-02-23 17:35:09 +01004986#endif /* MBEDTLS_PK_CAN_ECDSA_SOME && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA &&
Jerry Yued5e9f42022-01-26 11:21:34 +08004987 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004988
Valerio Setti75fba322023-02-23 17:35:09 +01004989#if defined(MBEDTLS_PK_CAN_ECDSA_SOME) && \
4990 defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
Jerry Yu909df7b2022-01-22 11:56:27 +08004991 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
4992 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Valerio Setti75fba322023-02-23 17:35:09 +01004993#endif /* MBEDTLS_PK_CAN_ECDSA_SOME && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu909df7b2022-01-22 11:56:27 +08004994 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
4995
Valerio Setti75fba322023-02-23 17:35:09 +01004996#if defined(MBEDTLS_PK_CAN_ECDSA_SOME) && \
4997 defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
Jerry Yued5e9f42022-01-26 11:21:34 +08004998 defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
4999 MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512,
Valerio Setti75fba322023-02-23 17:35:09 +01005000#endif /* MBEDTLS_PK_CAN_ECDSA_SOME && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yued5e9f42022-01-26 11:21:34 +08005001 MBEDTLS_ECP_DP_SECP521R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08005002
Gilles Peskine449bd832023-01-11 14:50:10 +01005003#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && \
5004 defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu9bb3ee42022-06-23 10:16:33 +08005005 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
Gilles Peskine449bd832023-01-11 14:50:10 +01005006#endif \
5007 /* 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 +08005008
Gilles Peskine449bd832023-01-11 14:50:10 +01005009#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && \
5010 defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu9bb3ee42022-06-23 10:16:33 +08005011 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
Gilles Peskine449bd832023-01-11 14:50:10 +01005012#endif \
5013 /* 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 +08005014
Gilles Peskine449bd832023-01-11 14:50:10 +01005015#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && \
5016 defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu9bb3ee42022-06-23 10:16:33 +08005017 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Gilles Peskine449bd832023-01-11 14:50:10 +01005018#endif \
5019 /* 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 +08005020
Andrzej Kurek25f27152022-08-17 16:09:31 -04005021#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 +08005022 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512,
5023#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA512_C */
5024
Andrzej Kurek25f27152022-08-17 16:09:31 -04005025#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 +08005026 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384,
5027#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA384_C */
5028
Andrzej Kurek25f27152022-08-17 16:09:31 -04005029#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 +08005030 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
5031#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA256_C */
5032
Gabor Mezei15b95a62022-05-09 16:37:58 +02005033 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08005034};
5035
5036/* NOTICE: see above */
5037#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5038static uint16_t ssl_tls12_preset_default_sig_algs[] = {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005039#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Valerio Setti75fba322023-02-23 17:35:09 +01005040#if defined(MBEDTLS_PK_CAN_ECDSA_SOME)
Gilles Peskine449bd832023-01-11 14:50:10 +01005041 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA512),
Jerry Yu713013f2022-01-17 18:16:35 +08005042#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08005043#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
5044 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
5045#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02005046#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005047 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA512),
Gabor Mezeic1051b62022-05-10 13:13:58 +02005048#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005049#endif /* MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04005050#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Valerio Setti75fba322023-02-23 17:35:09 +01005051#if defined(MBEDTLS_PK_CAN_ECDSA_SOME)
Gilles Peskine449bd832023-01-11 14:50:10 +01005052 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08005053#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08005054#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
5055 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
5056#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02005057#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005058 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384),
Gabor Mezeic1051b62022-05-10 13:13:58 +02005059#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005060#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04005061#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Valerio Setti75fba322023-02-23 17:35:09 +01005062#if defined(MBEDTLS_PK_CAN_ECDSA_SOME)
Gilles Peskine449bd832023-01-11 14:50:10 +01005063 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08005064#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08005065#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
5066 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
5067#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02005068#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005069 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256),
Gabor Mezeic1051b62022-05-10 13:13:58 +02005070#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005071#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Gabor Mezei15b95a62022-05-09 16:37:58 +02005072 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08005073};
5074#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5075/* NOTICE: see above */
5076static uint16_t ssl_preset_suiteb_sig_algs[] = {
5077
Andrzej Kurek25f27152022-08-17 16:09:31 -04005078#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 +08005079 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
5080 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005081#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu909df7b2022-01-22 11:56:27 +08005082 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
5083
Andrzej Kurek25f27152022-08-17 16:09:31 -04005084#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 +08005085 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
5086 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005087#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu53037892022-01-25 11:02:06 +08005088 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08005089
Gilles Peskine449bd832023-01-11 14:50:10 +01005090#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && \
5091 defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu909df7b2022-01-22 11:56:27 +08005092 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Gilles Peskine449bd832023-01-11 14:50:10 +01005093#endif \
5094 /* 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 +08005095
Andrzej Kurek25f27152022-08-17 16:09:31 -04005096#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 +08005097 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005098#endif /* MBEDTLS_RSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yu53037892022-01-25 11:02:06 +08005099
Gabor Mezei15b95a62022-05-09 16:37:58 +02005100 MBEDTLS_TLS_SIG_NONE
Jerry Yu713013f2022-01-17 18:16:35 +08005101};
Jerry Yu6106fdc2022-01-12 16:36:14 +08005102
Jerry Yu909df7b2022-01-22 11:56:27 +08005103/* NOTICE: see above */
5104#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5105static uint16_t ssl_tls12_preset_suiteb_sig_algs[] = {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005106#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02005107#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005108 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256),
Jerry Yu713013f2022-01-17 18:16:35 +08005109#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02005110#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005111 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256),
Gabor Mezeic1051b62022-05-10 13:13:58 +02005112#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005113#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04005114#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02005115#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005116 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384),
Jerry Yu18c833e2022-01-25 10:55:47 +08005117#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02005118#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005119 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384),
Gabor Mezeic1051b62022-05-10 13:13:58 +02005120#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005121#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Gabor Mezei15b95a62022-05-09 16:37:58 +02005122 MBEDTLS_TLS_SIG_NONE
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01005123};
Jerry Yu909df7b2022-01-22 11:56:27 +08005124#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5125
Ronald Crone68ab4f2022-10-05 12:46:29 +02005126#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005127
Brett Warrene0edc842021-08-17 09:53:13 +01005128static uint16_t ssl_preset_suiteb_groups[] = {
Jaeden Amerod4311042019-06-03 08:27:16 +01005129#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01005130 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01005131#endif
5132#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01005133 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01005134#endif
Brett Warrene0edc842021-08-17 09:53:13 +01005135 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005136};
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005137
Ronald Crone68ab4f2022-10-05 12:46:29 +02005138#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08005139/* Function for checking `ssl_preset_*_sig_algs` and `ssl_tls12_preset_*_sig_algs`
Jerry Yu370e1462022-01-25 10:36:53 +08005140 * to make sure there are no duplicated signature algorithm entries. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005141MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01005142static int ssl_check_no_sig_alg_duplication(uint16_t *sig_algs)
Jerry Yu1a8b4812022-01-20 17:56:50 +08005143{
5144 size_t i, j;
5145 int ret = 0;
Jerry Yu909df7b2022-01-22 11:56:27 +08005146
Gilles Peskine449bd832023-01-11 14:50:10 +01005147 for (i = 0; sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++) {
5148 for (j = 0; j < i; j++) {
5149 if (sig_algs[i] != sig_algs[j]) {
Jerry Yuf377d642022-01-25 10:43:59 +08005150 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01005151 }
5152 mbedtls_printf(" entry(%04x,%" MBEDTLS_PRINTF_SIZET
5153 ") is duplicated at %" MBEDTLS_PRINTF_SIZET "\n",
5154 sig_algs[i], j, i);
Jerry Yuf377d642022-01-25 10:43:59 +08005155 ret = -1;
Jerry Yu1a8b4812022-01-20 17:56:50 +08005156 }
5157 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005158 return ret;
Jerry Yu1a8b4812022-01-20 17:56:50 +08005159}
5160
Ronald Crone68ab4f2022-10-05 12:46:29 +02005161#endif /* MBEDTLS_DEBUG_C && MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yu1a8b4812022-01-20 17:56:50 +08005162
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005163/*
Tillmann Karras588ad502015-09-25 04:27:22 +02005164 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005165 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005166int mbedtls_ssl_config_defaults(mbedtls_ssl_config *conf,
5167 int endpoint, int transport, int preset)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005168{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005169#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Janos Follath865b3eb2019-12-16 11:46:15 +00005170 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005171#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005172
Ronald Crone68ab4f2022-10-05 12:46:29 +02005173#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01005174 if (ssl_check_no_sig_alg_duplication(ssl_preset_suiteb_sig_algs)) {
5175 mbedtls_printf("ssl_preset_suiteb_sig_algs has duplicated entries\n");
5176 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu1a8b4812022-01-20 17:56:50 +08005177 }
5178
Gilles Peskine449bd832023-01-11 14:50:10 +01005179 if (ssl_check_no_sig_alg_duplication(ssl_preset_default_sig_algs)) {
5180 mbedtls_printf("ssl_preset_default_sig_algs has duplicated entries\n");
5181 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu1a8b4812022-01-20 17:56:50 +08005182 }
Jerry Yu909df7b2022-01-22 11:56:27 +08005183
5184#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01005185 if (ssl_check_no_sig_alg_duplication(ssl_tls12_preset_suiteb_sig_algs)) {
5186 mbedtls_printf("ssl_tls12_preset_suiteb_sig_algs has duplicated entries\n");
5187 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu909df7b2022-01-22 11:56:27 +08005188 }
5189
Gilles Peskine449bd832023-01-11 14:50:10 +01005190 if (ssl_check_no_sig_alg_duplication(ssl_tls12_preset_default_sig_algs)) {
5191 mbedtls_printf("ssl_tls12_preset_default_sig_algs has duplicated entries\n");
5192 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu909df7b2022-01-22 11:56:27 +08005193 }
5194#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Ronald Crone68ab4f2022-10-05 12:46:29 +02005195#endif /* MBEDTLS_DEBUG_C && MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yu1a8b4812022-01-20 17:56:50 +08005196
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02005197 /* Use the functions here so that they are covered in tests,
5198 * but otherwise access member directly for efficiency */
Gilles Peskine449bd832023-01-11 14:50:10 +01005199 mbedtls_ssl_conf_endpoint(conf, endpoint);
5200 mbedtls_ssl_conf_transport(conf, transport);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005201
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005202 /*
5203 * Things that are common to all presets
5204 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02005205#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005206 if (endpoint == MBEDTLS_SSL_IS_CLIENT) {
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02005207 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
5208#if defined(MBEDTLS_SSL_SESSION_TICKETS)
5209 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
5210#endif
5211 }
5212#endif
5213
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005214#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
5215 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
5216#endif
5217
5218#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
5219 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
5220#endif
5221
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02005222#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005223 conf->f_cookie_write = ssl_cookie_write_dummy;
5224 conf->f_cookie_check = ssl_cookie_check_dummy;
5225#endif
5226
5227#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
5228 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
5229#endif
5230
Janos Follath088ce432017-04-10 12:42:31 +01005231#if defined(MBEDTLS_SSL_SRV_C)
5232 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
TRodziewicz3946f792021-06-14 12:11:18 +02005233 conf->respect_cli_pref = MBEDTLS_SSL_SRV_CIPHERSUITE_ORDER_SERVER;
Janos Follath088ce432017-04-10 12:42:31 +01005234#endif
5235
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005236#if defined(MBEDTLS_SSL_PROTO_DTLS)
5237 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
5238 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
5239#endif
5240
5241#if defined(MBEDTLS_SSL_RENEGOTIATION)
5242 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005243 memset(conf->renego_period, 0x00, 2);
5244 memset(conf->renego_period + 2, 0xFF, 6);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005245#endif
5246
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005247#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005248 if (endpoint == MBEDTLS_SSL_IS_SERVER) {
Hanno Beckere2defad2021-07-24 05:59:17 +01005249 const unsigned char dhm_p[] =
5250 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
5251 const unsigned char dhm_g[] =
5252 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
Hanno Becker00d0a682017-10-04 13:14:29 +01005253
Gilles Peskine449bd832023-01-11 14:50:10 +01005254 if ((ret = mbedtls_ssl_conf_dh_param_bin(conf,
5255 dhm_p, sizeof(dhm_p),
5256 dhm_g, sizeof(dhm_g))) != 0) {
5257 return ret;
Hanno Beckere2defad2021-07-24 05:59:17 +01005258 }
5259 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02005260#endif
5261
Ronald Cron6f135e12021-12-08 16:57:54 +01005262#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu6ee56aa2022-12-06 17:47:22 +08005263
5264#if defined(MBEDTLS_SSL_EARLY_DATA)
Gilles Peskine449bd832023-01-11 14:50:10 +01005265 mbedtls_ssl_tls13_conf_early_data(conf, MBEDTLS_SSL_EARLY_DATA_DISABLED);
Jerry Yu6ee56aa2022-12-06 17:47:22 +08005266#if defined(MBEDTLS_SSL_SRV_C)
5267 mbedtls_ssl_tls13_conf_max_early_data_size(
Gilles Peskine449bd832023-01-11 14:50:10 +01005268 conf, MBEDTLS_SSL_MAX_EARLY_DATA_SIZE);
Jerry Yu6ee56aa2022-12-06 17:47:22 +08005269#endif
5270#endif /* MBEDTLS_SSL_EARLY_DATA */
5271
Jerry Yud0766ec2022-09-22 10:46:57 +08005272#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08005273 mbedtls_ssl_conf_new_session_tickets(
Gilles Peskine449bd832023-01-11 14:50:10 +01005274 conf, MBEDTLS_SSL_TLS1_3_DEFAULT_NEW_SESSION_TICKETS);
Jerry Yu1ad7ace2022-08-09 13:28:39 +08005275#endif
Hanno Becker71f1ed62021-07-24 06:01:47 +01005276 /*
5277 * Allow all TLS 1.3 key exchange modes by default.
5278 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00005279 conf->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Ronald Cron6f135e12021-12-08 16:57:54 +01005280#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01005281
Gilles Peskine449bd832023-01-11 14:50:10 +01005282 if (transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Glenn Strauss2dfcea22022-03-14 17:26:42 -04005283#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
XiaokangQian4d3a6042022-04-21 13:46:17 +00005284 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian060d8672022-04-21 09:24:56 +00005285 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian4d3a6042022-04-21 13:46:17 +00005286#else
Gilles Peskine449bd832023-01-11 14:50:10 +01005287 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQian060d8672022-04-21 09:24:56 +00005288#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005289 } else {
XiaokangQian4d3a6042022-04-21 13:46:17 +00005290#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01005291 if (endpoint == MBEDTLS_SSL_IS_CLIENT) {
XiaokangQian4d3a6042022-04-21 13:46:17 +00005292 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5293 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
Gilles Peskine449bd832023-01-11 14:50:10 +01005294 } else {
5295 /* Hybrid TLS 1.2 / 1.3 is not supported on server side yet */
XiaokangQian4d3a6042022-04-21 13:46:17 +00005296 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5297 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5298 }
5299#elif defined(MBEDTLS_SSL_PROTO_TLS1_3)
5300 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
5301 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
5302#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
5303 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5304 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5305#else
Gilles Peskine449bd832023-01-11 14:50:10 +01005306 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQian4d3a6042022-04-21 13:46:17 +00005307#endif
5308 }
Glenn Strauss2dfcea22022-03-14 17:26:42 -04005309
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005310 /*
5311 * Preset-specific defaults
5312 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005313 switch (preset) {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005314 /*
5315 * NSA Suite B
5316 */
5317 case MBEDTLS_SSL_PRESET_SUITEB:
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005318
Hanno Beckerd60b6c62021-04-29 12:04:11 +01005319 conf->ciphersuite_list = ssl_preset_suiteb_ciphersuites;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005320
5321#if defined(MBEDTLS_X509_CRT_PARSE_C)
5322 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005323#endif
5324
Ronald Crone68ab4f2022-10-05 12:46:29 +02005325#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08005326#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01005327 if (mbedtls_ssl_conf_is_tls12_only(conf)) {
Jerry Yu909df7b2022-01-22 11:56:27 +08005328 conf->sig_algs = ssl_tls12_preset_suiteb_sig_algs;
Gilles Peskine449bd832023-01-11 14:50:10 +01005329 } else
Jerry Yu909df7b2022-01-22 11:56:27 +08005330#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005331 conf->sig_algs = ssl_preset_suiteb_sig_algs;
Ronald Crone68ab4f2022-10-05 12:46:29 +02005332#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005333
Brett Warrene0edc842021-08-17 09:53:13 +01005334#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
5335 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005336#endif
Brett Warrene0edc842021-08-17 09:53:13 +01005337 conf->group_list = ssl_preset_suiteb_groups;
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02005338 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005339
5340 /*
5341 * Default
5342 */
5343 default:
Ronald Cronf6606552022-03-15 11:23:25 +01005344
Hanno Beckerd60b6c62021-04-29 12:04:11 +01005345 conf->ciphersuite_list = mbedtls_ssl_list_ciphersuites();
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005346
5347#if defined(MBEDTLS_X509_CRT_PARSE_C)
5348 conf->cert_profile = &mbedtls_x509_crt_profile_default;
5349#endif
5350
Ronald Crone68ab4f2022-10-05 12:46:29 +02005351#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08005352#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01005353 if (mbedtls_ssl_conf_is_tls12_only(conf)) {
Jerry Yu909df7b2022-01-22 11:56:27 +08005354 conf->sig_algs = ssl_tls12_preset_default_sig_algs;
Gilles Peskine449bd832023-01-11 14:50:10 +01005355 } else
Jerry Yu909df7b2022-01-22 11:56:27 +08005356#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005357 conf->sig_algs = ssl_preset_default_sig_algs;
Ronald Crone68ab4f2022-10-05 12:46:29 +02005358#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005359
Brett Warrene0edc842021-08-17 09:53:13 +01005360#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
5361 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005362#endif
Brett Warrene0edc842021-08-17 09:53:13 +01005363 conf->group_list = ssl_preset_default_groups;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005364
5365#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
5366 conf->dhm_min_bitlen = 1024;
5367#endif
5368 }
5369
Gilles Peskine449bd832023-01-11 14:50:10 +01005370 return 0;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005371}
5372
5373/*
5374 * Free mbedtls_ssl_config
5375 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005376void mbedtls_ssl_config_free(mbedtls_ssl_config *conf)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005377{
5378#if defined(MBEDTLS_DHM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005379 mbedtls_mpi_free(&conf->dhm_P);
5380 mbedtls_mpi_free(&conf->dhm_G);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005381#endif
5382
Ronald Cron73fe8df2022-10-05 14:31:43 +02005383#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02005384#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01005385 if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) {
Neil Armstrong501c9322022-05-03 09:35:09 +02005386 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
5387 }
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005388#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01005389 if (conf->psk != NULL) {
5390 mbedtls_platform_zeroize(conf->psk, conf->psk_len);
5391 mbedtls_free(conf->psk);
Azim Khan27e8a122018-03-21 14:24:11 +00005392 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005393 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09005394 }
5395
Gilles Peskine449bd832023-01-11 14:50:10 +01005396 if (conf->psk_identity != NULL) {
5397 mbedtls_platform_zeroize(conf->psk_identity, conf->psk_identity_len);
5398 mbedtls_free(conf->psk_identity);
Azim Khan27e8a122018-03-21 14:24:11 +00005399 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005400 conf->psk_identity_len = 0;
5401 }
Ronald Cron73fe8df2022-10-05 14:31:43 +02005402#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005403
5404#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005405 ssl_key_cert_free(conf->key_cert);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005406#endif
5407
Gilles Peskine449bd832023-01-11 14:50:10 +01005408 mbedtls_platform_zeroize(conf, sizeof(mbedtls_ssl_config));
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005409}
5410
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02005411#if defined(MBEDTLS_PK_C) && \
Valerio Setti1ad9ef22023-02-22 12:38:07 +01005412 (defined(MBEDTLS_RSA_C) || defined(MBEDTLS_PK_CAN_ECDSA_SOME))
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005413/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005414 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005415 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005416unsigned char mbedtls_ssl_sig_from_pk(mbedtls_pk_context *pk)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005417{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005418#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005419 if (mbedtls_pk_can_do(pk, MBEDTLS_PK_RSA)) {
5420 return MBEDTLS_SSL_SIG_RSA;
5421 }
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005422#endif
Valerio Setti1ad9ef22023-02-22 12:38:07 +01005423#if defined(MBEDTLS_PK_CAN_ECDSA_SOME)
Gilles Peskine449bd832023-01-11 14:50:10 +01005424 if (mbedtls_pk_can_do(pk, MBEDTLS_PK_ECDSA)) {
5425 return MBEDTLS_SSL_SIG_ECDSA;
5426 }
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005427#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005428 return MBEDTLS_SSL_SIG_ANON;
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005429}
5430
Gilles Peskine449bd832023-01-11 14:50:10 +01005431unsigned char mbedtls_ssl_sig_from_pk_alg(mbedtls_pk_type_t type)
Hanno Becker7e5437a2017-04-28 17:15:26 +01005432{
Gilles Peskine449bd832023-01-11 14:50:10 +01005433 switch (type) {
Hanno Becker7e5437a2017-04-28 17:15:26 +01005434 case MBEDTLS_PK_RSA:
Gilles Peskine449bd832023-01-11 14:50:10 +01005435 return MBEDTLS_SSL_SIG_RSA;
Hanno Becker7e5437a2017-04-28 17:15:26 +01005436 case MBEDTLS_PK_ECDSA:
5437 case MBEDTLS_PK_ECKEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01005438 return MBEDTLS_SSL_SIG_ECDSA;
Hanno Becker7e5437a2017-04-28 17:15:26 +01005439 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005440 return MBEDTLS_SSL_SIG_ANON;
Hanno Becker7e5437a2017-04-28 17:15:26 +01005441 }
5442}
5443
Gilles Peskine449bd832023-01-11 14:50:10 +01005444mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig(unsigned char sig)
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005445{
Gilles Peskine449bd832023-01-11 14:50:10 +01005446 switch (sig) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005447#if defined(MBEDTLS_RSA_C)
5448 case MBEDTLS_SSL_SIG_RSA:
Gilles Peskine449bd832023-01-11 14:50:10 +01005449 return MBEDTLS_PK_RSA;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005450#endif
Valerio Setti1ad9ef22023-02-22 12:38:07 +01005451#if defined(MBEDTLS_PK_CAN_ECDSA_SOME)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005452 case MBEDTLS_SSL_SIG_ECDSA:
Gilles Peskine449bd832023-01-11 14:50:10 +01005453 return MBEDTLS_PK_ECDSA;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005454#endif
5455 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005456 return MBEDTLS_PK_NONE;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005457 }
5458}
Valerio Setti1ad9ef22023-02-22 12:38:07 +01005459#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_PK_CAN_ECDSA_SOME ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005460
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02005461/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005462 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02005463 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005464mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash(unsigned char hash)
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005465{
Gilles Peskine449bd832023-01-11 14:50:10 +01005466 switch (hash) {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005467#if defined(MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005468 case MBEDTLS_SSL_HASH_MD5:
Gilles Peskine449bd832023-01-11 14:50:10 +01005469 return MBEDTLS_MD_MD5;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005470#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005471#if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005472 case MBEDTLS_SSL_HASH_SHA1:
Gilles Peskine449bd832023-01-11 14:50:10 +01005473 return MBEDTLS_MD_SHA1;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005474#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005475#if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005476 case MBEDTLS_SSL_HASH_SHA224:
Gilles Peskine449bd832023-01-11 14:50:10 +01005477 return MBEDTLS_MD_SHA224;
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02005478#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005479#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005480 case MBEDTLS_SSL_HASH_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01005481 return MBEDTLS_MD_SHA256;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005482#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005483#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005484 case MBEDTLS_SSL_HASH_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01005485 return MBEDTLS_MD_SHA384;
Mateusz Starzyk3352a532021-04-06 14:28:22 +02005486#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005487#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005488 case MBEDTLS_SSL_HASH_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01005489 return MBEDTLS_MD_SHA512;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005490#endif
5491 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005492 return MBEDTLS_MD_NONE;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005493 }
5494}
5495
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005496/*
5497 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
5498 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005499unsigned char mbedtls_ssl_hash_from_md_alg(int md)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005500{
Gilles Peskine449bd832023-01-11 14:50:10 +01005501 switch (md) {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005502#if defined(MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005503 case MBEDTLS_MD_MD5:
Gilles Peskine449bd832023-01-11 14:50:10 +01005504 return MBEDTLS_SSL_HASH_MD5;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005505#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005506#if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005507 case MBEDTLS_MD_SHA1:
Gilles Peskine449bd832023-01-11 14:50:10 +01005508 return MBEDTLS_SSL_HASH_SHA1;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005509#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005510#if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005511 case MBEDTLS_MD_SHA224:
Gilles Peskine449bd832023-01-11 14:50:10 +01005512 return MBEDTLS_SSL_HASH_SHA224;
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02005513#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005514#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005515 case MBEDTLS_MD_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01005516 return MBEDTLS_SSL_HASH_SHA256;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005517#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005518#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005519 case MBEDTLS_MD_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01005520 return MBEDTLS_SSL_HASH_SHA384;
Mateusz Starzyk3352a532021-04-06 14:28:22 +02005521#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005522#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005523 case MBEDTLS_MD_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01005524 return MBEDTLS_SSL_HASH_SHA512;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005525#endif
5526 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005527 return MBEDTLS_SSL_HASH_NONE;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005528 }
5529}
5530
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005531/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005532 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005533 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005534 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005535int mbedtls_ssl_check_curve_tls_id(const mbedtls_ssl_context *ssl, uint16_t tls_id)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005536{
Gilles Peskine449bd832023-01-11 14:50:10 +01005537 const uint16_t *group_list = mbedtls_ssl_get_groups(ssl);
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005538
Gilles Peskine449bd832023-01-11 14:50:10 +01005539 if (group_list == NULL) {
5540 return -1;
Brett Warrene0edc842021-08-17 09:53:13 +01005541 }
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005542
Gilles Peskine449bd832023-01-11 14:50:10 +01005543 for (; *group_list != 0; group_list++) {
5544 if (*group_list == tls_id) {
5545 return 0;
5546 }
5547 }
5548
5549 return -1;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005550}
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005551
5552#if defined(MBEDTLS_ECP_C)
5553/*
5554 * Same as mbedtls_ssl_check_curve_tls_id() but with a mbedtls_ecp_group_id.
5555 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005556int mbedtls_ssl_check_curve(const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id)
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005557{
Gilles Peskine449bd832023-01-11 14:50:10 +01005558 uint16_t tls_id = mbedtls_ssl_get_tls_id_from_ecp_group_id(grp_id);
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005559
Gilles Peskine449bd832023-01-11 14:50:10 +01005560 if (tls_id == 0) {
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005561 return -1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005562 }
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005563
Gilles Peskine449bd832023-01-11 14:50:10 +01005564 return mbedtls_ssl_check_curve_tls_id(ssl, tls_id);
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005565}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02005566#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005567
Gilles Peskine449bd832023-01-11 14:50:10 +01005568#if defined(MBEDTLS_DEBUG_C)
Valerio Setti67419f02023-01-04 16:12:42 +01005569#define EC_NAME(_name_) _name_
5570#else
5571#define EC_NAME(_name_) NULL
5572#endif
5573
Valerio Setti18c9fed2022-12-30 17:44:24 +01005574static const struct {
5575 uint16_t tls_id;
5576 mbedtls_ecp_group_id ecp_group_id;
5577 psa_ecc_family_t psa_family;
5578 uint16_t bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01005579 const char *name;
Valerio Setti18c9fed2022-12-30 17:44:24 +01005580} tls_id_match_table[] =
5581{
5582#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_521)
Gilles Peskine449bd832023-01-11 14:50:10 +01005583 { 25, MBEDTLS_ECP_DP_SECP521R1, PSA_ECC_FAMILY_SECP_R1, 521, EC_NAME("secp521r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005584#endif
5585#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED) || defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512)
Gilles Peskine449bd832023-01-11 14:50:10 +01005586 { 28, MBEDTLS_ECP_DP_BP512R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 512, EC_NAME("brainpoolP512r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005587#endif
Valerio Setti67419f02023-01-04 16:12:42 +01005588#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_384)
Gilles Peskine449bd832023-01-11 14:50:10 +01005589 { 24, MBEDTLS_ECP_DP_SECP384R1, PSA_ECC_FAMILY_SECP_R1, 384, EC_NAME("secp384r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005590#endif
Valerio Setti67419f02023-01-04 16:12:42 +01005591#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED) || defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384)
Gilles Peskine449bd832023-01-11 14:50:10 +01005592 { 27, MBEDTLS_ECP_DP_BP384R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 384, EC_NAME("brainpoolP384r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005593#endif
Valerio Setti67419f02023-01-04 16:12:42 +01005594#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_256)
Gilles Peskine449bd832023-01-11 14:50:10 +01005595 { 23, MBEDTLS_ECP_DP_SECP256R1, PSA_ECC_FAMILY_SECP_R1, 256, EC_NAME("secp256r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005596#endif
5597#if defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED) || defined(PSA_WANT_ECC_SECP_K1_256)
Gilles Peskine449bd832023-01-11 14:50:10 +01005598 { 22, MBEDTLS_ECP_DP_SECP256K1, PSA_ECC_FAMILY_SECP_K1, 256, EC_NAME("secp256k1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005599#endif
Valerio Setti67419f02023-01-04 16:12:42 +01005600#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED) || defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256)
Gilles Peskine449bd832023-01-11 14:50:10 +01005601 { 26, MBEDTLS_ECP_DP_BP256R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 256, EC_NAME("brainpoolP256r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005602#endif
5603#if defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_224)
Gilles Peskine449bd832023-01-11 14:50:10 +01005604 { 21, MBEDTLS_ECP_DP_SECP224R1, PSA_ECC_FAMILY_SECP_R1, 224, EC_NAME("secp224r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005605#endif
5606#if defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED) || defined(PSA_WANT_ECC_SECP_K1_224)
Gilles Peskine449bd832023-01-11 14:50:10 +01005607 { 20, MBEDTLS_ECP_DP_SECP224K1, PSA_ECC_FAMILY_SECP_K1, 224, EC_NAME("secp224k1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005608#endif
5609#if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_192)
Gilles Peskine449bd832023-01-11 14:50:10 +01005610 { 19, MBEDTLS_ECP_DP_SECP192R1, PSA_ECC_FAMILY_SECP_R1, 192, EC_NAME("secp192r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005611#endif
5612#if defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED) || defined(PSA_WANT_ECC_SECP_K1_192)
Gilles Peskine449bd832023-01-11 14:50:10 +01005613 { 18, MBEDTLS_ECP_DP_SECP192K1, PSA_ECC_FAMILY_SECP_K1, 192, EC_NAME("secp192k1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005614#endif
5615#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED) || defined(PSA_WANT_ECC_MONTGOMERY_255)
Gilles Peskine449bd832023-01-11 14:50:10 +01005616 { 29, MBEDTLS_ECP_DP_CURVE25519, PSA_ECC_FAMILY_MONTGOMERY, 255, EC_NAME("x25519") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005617#endif
5618#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED) || defined(PSA_WANT_ECC_MONTGOMERY_448)
Gilles Peskine449bd832023-01-11 14:50:10 +01005619 { 30, MBEDTLS_ECP_DP_CURVE448, PSA_ECC_FAMILY_MONTGOMERY, 448, EC_NAME("x448") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005620#endif
5621 { 0, MBEDTLS_ECP_DP_NONE, 0, 0, NULL },
5622};
5623
Gilles Peskine449bd832023-01-11 14:50:10 +01005624int mbedtls_ssl_get_psa_curve_info_from_tls_id(uint16_t tls_id,
5625 psa_ecc_family_t *family,
5626 size_t *bits)
Valerio Setti18c9fed2022-12-30 17:44:24 +01005627{
Gilles Peskine449bd832023-01-11 14:50:10 +01005628 for (int i = 0; tls_id_match_table[i].tls_id != 0; i++) {
5629 if (tls_id_match_table[i].tls_id == tls_id) {
5630 if (family != NULL) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01005631 *family = tls_id_match_table[i].psa_family;
Gilles Peskine449bd832023-01-11 14:50:10 +01005632 }
5633 if (bits != NULL) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01005634 *bits = tls_id_match_table[i].bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01005635 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01005636 return PSA_SUCCESS;
5637 }
5638 }
5639
5640 return PSA_ERROR_NOT_SUPPORTED;
5641}
5642
Gilles Peskine449bd832023-01-11 14:50:10 +01005643mbedtls_ecp_group_id mbedtls_ssl_get_ecp_group_id_from_tls_id(uint16_t tls_id)
Valerio Setti18c9fed2022-12-30 17:44:24 +01005644{
Gilles Peskine449bd832023-01-11 14:50:10 +01005645 for (int i = 0; tls_id_match_table[i].tls_id != 0; i++) {
5646 if (tls_id_match_table[i].tls_id == tls_id) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01005647 return tls_id_match_table[i].ecp_group_id;
Gilles Peskine449bd832023-01-11 14:50:10 +01005648 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01005649 }
5650
5651 return MBEDTLS_ECP_DP_NONE;
5652}
5653
Gilles Peskine449bd832023-01-11 14:50:10 +01005654uint16_t mbedtls_ssl_get_tls_id_from_ecp_group_id(mbedtls_ecp_group_id grp_id)
Valerio Setti18c9fed2022-12-30 17:44:24 +01005655{
Gilles Peskine449bd832023-01-11 14:50:10 +01005656 for (int i = 0; tls_id_match_table[i].ecp_group_id != MBEDTLS_ECP_DP_NONE;
5657 i++) {
5658 if (tls_id_match_table[i].ecp_group_id == grp_id) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01005659 return tls_id_match_table[i].tls_id;
Gilles Peskine449bd832023-01-11 14:50:10 +01005660 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01005661 }
5662
5663 return 0;
5664}
5665
Valerio Setti67419f02023-01-04 16:12:42 +01005666#if defined(MBEDTLS_DEBUG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005667const char *mbedtls_ssl_get_curve_name_from_tls_id(uint16_t tls_id)
Valerio Setti18c9fed2022-12-30 17:44:24 +01005668{
Gilles Peskine449bd832023-01-11 14:50:10 +01005669 for (int i = 0; tls_id_match_table[i].tls_id != 0; i++) {
5670 if (tls_id_match_table[i].tls_id == tls_id) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01005671 return tls_id_match_table[i].name;
Gilles Peskine449bd832023-01-11 14:50:10 +01005672 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01005673 }
5674
5675 return NULL;
5676}
Valerio Setti67419f02023-01-04 16:12:42 +01005677#endif
Valerio Setti18c9fed2022-12-30 17:44:24 +01005678
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005679#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005680int mbedtls_ssl_check_cert_usage(const mbedtls_x509_crt *cert,
5681 const mbedtls_ssl_ciphersuite_t *ciphersuite,
5682 int cert_endpoint,
5683 uint32_t *flags)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005684{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005685 int ret = 0;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005686 int usage = 0;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005687 const char *ext_oid;
5688 size_t ext_len;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005689
Gilles Peskine449bd832023-01-11 14:50:10 +01005690 if (cert_endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005691 /* Server part of the key exchange */
Gilles Peskine449bd832023-01-11 14:50:10 +01005692 switch (ciphersuite->key_exchange) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005693 case MBEDTLS_KEY_EXCHANGE_RSA:
5694 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005695 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005696 break;
5697
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005698 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
5699 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
5700 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
5701 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005702 break;
5703
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005704 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
5705 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005706 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005707 break;
5708
5709 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005710 case MBEDTLS_KEY_EXCHANGE_NONE:
5711 case MBEDTLS_KEY_EXCHANGE_PSK:
5712 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
5713 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02005714 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005715 usage = 0;
5716 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005717 } else {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005718 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
5719 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005720 }
5721
Gilles Peskine449bd832023-01-11 14:50:10 +01005722 if (mbedtls_x509_crt_check_key_usage(cert, usage) != 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005723 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005724 ret = -1;
5725 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005726
Gilles Peskine449bd832023-01-11 14:50:10 +01005727 if (cert_endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005728 ext_oid = MBEDTLS_OID_SERVER_AUTH;
Gilles Peskine449bd832023-01-11 14:50:10 +01005729 ext_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_SERVER_AUTH);
5730 } else {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005731 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
Gilles Peskine449bd832023-01-11 14:50:10 +01005732 ext_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_CLIENT_AUTH);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005733 }
5734
Gilles Peskine449bd832023-01-11 14:50:10 +01005735 if (mbedtls_x509_crt_check_extended_key_usage(cert, ext_oid, ext_len) != 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005736 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005737 ret = -1;
5738 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005739
Gilles Peskine449bd832023-01-11 14:50:10 +01005740 return ret;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005741}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005742#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02005743
Jerry Yu148165c2021-09-24 23:20:59 +08005744#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01005745int mbedtls_ssl_get_handshake_transcript(mbedtls_ssl_context *ssl,
5746 const mbedtls_md_type_t md,
5747 unsigned char *dst,
5748 size_t dst_len,
5749 size_t *olen)
Jerry Yu148165c2021-09-24 23:20:59 +08005750{
Ronald Cronf6893e12022-01-07 22:09:01 +01005751 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5752 psa_hash_operation_t *hash_operation_to_clone;
5753 psa_hash_operation_t hash_operation = psa_hash_operation_init();
5754
Jerry Yu148165c2021-09-24 23:20:59 +08005755 *olen = 0;
Ronald Cronf6893e12022-01-07 22:09:01 +01005756
Gilles Peskine449bd832023-01-11 14:50:10 +01005757 switch (md) {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005758#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01005759 case MBEDTLS_MD_SHA384:
5760 hash_operation_to_clone = &ssl->handshake->fin_sha384_psa;
5761 break;
Ronald Cronf6893e12022-01-07 22:09:01 +01005762#endif
5763
Andrzej Kurek25f27152022-08-17 16:09:31 -04005764#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01005765 case MBEDTLS_MD_SHA256:
5766 hash_operation_to_clone = &ssl->handshake->fin_sha256_psa;
5767 break;
Ronald Cronf6893e12022-01-07 22:09:01 +01005768#endif
5769
Gilles Peskine449bd832023-01-11 14:50:10 +01005770 default:
5771 goto exit;
5772 }
5773
5774 status = psa_hash_clone(hash_operation_to_clone, &hash_operation);
5775 if (status != PSA_SUCCESS) {
Ronald Cronf6893e12022-01-07 22:09:01 +01005776 goto exit;
5777 }
5778
Gilles Peskine449bd832023-01-11 14:50:10 +01005779 status = psa_hash_finish(&hash_operation, dst, dst_len, olen);
5780 if (status != PSA_SUCCESS) {
Ronald Cronf6893e12022-01-07 22:09:01 +01005781 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005782 }
Ronald Cronf6893e12022-01-07 22:09:01 +01005783
5784exit:
Andrzej Kurekeabeb302022-10-17 07:52:51 -04005785#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
5786 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
5787 (void) ssl;
5788#endif
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05005789 return PSA_TO_MBEDTLS_ERR(status);
Jerry Yu148165c2021-09-24 23:20:59 +08005790}
5791#else /* MBEDTLS_USE_PSA_CRYPTO */
5792
Andrzej Kurek25f27152022-08-17 16:09:31 -04005793#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005794MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01005795static int ssl_get_handshake_transcript_sha384(mbedtls_ssl_context *ssl,
5796 unsigned char *dst,
5797 size_t dst_len,
5798 size_t *olen)
Jerry Yu24c0ec32021-09-09 14:21:07 +08005799{
Jerry Yu24c0ec32021-09-09 14:21:07 +08005800 int ret;
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01005801 mbedtls_md_context_t sha384;
Jerry Yu24c0ec32021-09-09 14:21:07 +08005802
Gilles Peskine449bd832023-01-11 14:50:10 +01005803 if (dst_len < 48) {
5804 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
5805 }
Jerry Yu24c0ec32021-09-09 14:21:07 +08005806
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01005807 mbedtls_md_init(&sha384);
5808 ret = mbedtls_md_setup(&sha384, mbedtls_md_info_from_type(MBEDTLS_MD_SHA384), 0);
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01005809 if (ret != 0) {
5810 goto exit;
5811 }
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01005812 ret = mbedtls_md_clone(&sha384, &ssl->handshake->fin_sha384);
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01005813 if (ret != 0) {
5814 goto exit;
5815 }
Jerry Yu24c0ec32021-09-09 14:21:07 +08005816
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01005817 if ((ret = mbedtls_md_finish(&sha384, dst)) != 0) {
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01005818 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_finish", ret);
Jerry Yu24c0ec32021-09-09 14:21:07 +08005819 goto exit;
5820 }
5821
5822 *olen = 48;
5823
5824exit:
5825
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01005826 mbedtls_md_free(&sha384);
Gilles Peskine449bd832023-01-11 14:50:10 +01005827 return ret;
Jerry Yu24c0ec32021-09-09 14:21:07 +08005828}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005829#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005830
Andrzej Kurek25f27152022-08-17 16:09:31 -04005831#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005832MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01005833static int ssl_get_handshake_transcript_sha256(mbedtls_ssl_context *ssl,
5834 unsigned char *dst,
5835 size_t dst_len,
5836 size_t *olen)
Jerry Yu24c0ec32021-09-09 14:21:07 +08005837{
Jerry Yu24c0ec32021-09-09 14:21:07 +08005838 int ret;
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01005839 mbedtls_md_context_t sha256;
Jerry Yu24c0ec32021-09-09 14:21:07 +08005840
Gilles Peskine449bd832023-01-11 14:50:10 +01005841 if (dst_len < 32) {
5842 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
5843 }
Jerry Yu24c0ec32021-09-09 14:21:07 +08005844
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01005845 mbedtls_md_init(&sha256);
5846 ret = mbedtls_md_setup(&sha256, mbedtls_md_info_from_type(MBEDTLS_MD_SHA256), 0);
5847 if (ret != 0) {
5848 goto exit;
5849 }
5850 ret = mbedtls_md_clone(&sha256, &ssl->handshake->fin_sha256);
5851 if (ret != 0) {
5852 goto exit;
5853 }
Jerry Yuc5aef882021-12-23 20:15:02 +08005854
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01005855 if ((ret = mbedtls_md_finish(&sha256, dst)) != 0) {
5856 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_finish", ret);
Jerry Yu24c0ec32021-09-09 14:21:07 +08005857 goto exit;
5858 }
5859
5860 *olen = 32;
5861
5862exit:
5863
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01005864 mbedtls_md_free(&sha256);
Gilles Peskine449bd832023-01-11 14:50:10 +01005865 return ret;
Jerry Yu24c0ec32021-09-09 14:21:07 +08005866}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005867#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005868
Gilles Peskine449bd832023-01-11 14:50:10 +01005869int mbedtls_ssl_get_handshake_transcript(mbedtls_ssl_context *ssl,
5870 const mbedtls_md_type_t md,
5871 unsigned char *dst,
5872 size_t dst_len,
5873 size_t *olen)
Jerry Yu24c0ec32021-09-09 14:21:07 +08005874{
Gilles Peskine449bd832023-01-11 14:50:10 +01005875 switch (md) {
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005876
Andrzej Kurek25f27152022-08-17 16:09:31 -04005877#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01005878 case MBEDTLS_MD_SHA384:
5879 return ssl_get_handshake_transcript_sha384(ssl, dst, dst_len, olen);
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005880#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005881
Andrzej Kurek25f27152022-08-17 16:09:31 -04005882#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01005883 case MBEDTLS_MD_SHA256:
5884 return ssl_get_handshake_transcript_sha256(ssl, dst, dst_len, olen);
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005885#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005886
Gilles Peskine449bd832023-01-11 14:50:10 +01005887 default:
Andrzej Kurek409248a2022-10-24 10:33:21 -04005888#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01005889 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
5890 (void) ssl;
5891 (void) dst;
5892 (void) dst_len;
5893 (void) olen;
Andrzej Kurek409248a2022-10-24 10:33:21 -04005894#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005895 break;
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005896 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005897 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu24c0ec32021-09-09 14:21:07 +08005898}
XiaokangQian647719a2021-12-07 09:16:29 +00005899
Jerry Yu148165c2021-09-24 23:20:59 +08005900#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005901
Ronald Crone68ab4f2022-10-05 12:46:29 +02005902#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Gabor Mezei078e8032022-04-27 21:17:56 +02005903/* mbedtls_ssl_parse_sig_alg_ext()
5904 *
5905 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
5906 * value (TLS 1.3 RFC8446):
5907 * enum {
5908 * ....
5909 * ecdsa_secp256r1_sha256( 0x0403 ),
5910 * ecdsa_secp384r1_sha384( 0x0503 ),
5911 * ecdsa_secp521r1_sha512( 0x0603 ),
5912 * ....
5913 * } SignatureScheme;
5914 *
5915 * struct {
5916 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
5917 * } SignatureSchemeList;
5918 *
5919 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
5920 * value (TLS 1.2 RFC5246):
5921 * enum {
5922 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
5923 * sha512(6), (255)
5924 * } HashAlgorithm;
5925 *
5926 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
5927 * SignatureAlgorithm;
5928 *
5929 * struct {
5930 * HashAlgorithm hash;
5931 * SignatureAlgorithm signature;
5932 * } SignatureAndHashAlgorithm;
5933 *
5934 * SignatureAndHashAlgorithm
5935 * supported_signature_algorithms<2..2^16-2>;
5936 *
5937 * The TLS 1.3 signature algorithm extension was defined to be a compatible
5938 * generalization of the TLS 1.2 signature algorithm extension.
5939 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
5940 * `SignatureScheme` field of TLS 1.3
5941 *
5942 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005943int mbedtls_ssl_parse_sig_alg_ext(mbedtls_ssl_context *ssl,
5944 const unsigned char *buf,
5945 const unsigned char *end)
Gabor Mezei078e8032022-04-27 21:17:56 +02005946{
5947 const unsigned char *p = buf;
5948 size_t supported_sig_algs_len = 0;
5949 const unsigned char *supported_sig_algs_end;
5950 uint16_t sig_alg;
5951 uint32_t common_idx = 0;
5952
Gilles Peskine449bd832023-01-11 14:50:10 +01005953 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
5954 supported_sig_algs_len = MBEDTLS_GET_UINT16_BE(p, 0);
Gabor Mezei078e8032022-04-27 21:17:56 +02005955 p += 2;
5956
Gilles Peskine449bd832023-01-11 14:50:10 +01005957 memset(ssl->handshake->received_sig_algs, 0,
5958 sizeof(ssl->handshake->received_sig_algs));
Gabor Mezei078e8032022-04-27 21:17:56 +02005959
Gilles Peskine449bd832023-01-11 14:50:10 +01005960 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, supported_sig_algs_len);
Gabor Mezei078e8032022-04-27 21:17:56 +02005961 supported_sig_algs_end = p + supported_sig_algs_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01005962 while (p < supported_sig_algs_end) {
5963 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, supported_sig_algs_end, 2);
5964 sig_alg = MBEDTLS_GET_UINT16_BE(p, 0);
Gabor Mezei078e8032022-04-27 21:17:56 +02005965 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01005966 MBEDTLS_SSL_DEBUG_MSG(4, ("received signature algorithm: 0x%x %s",
5967 sig_alg,
5968 mbedtls_ssl_sig_alg_to_str(sig_alg)));
Jerry Yu2fe6c632022-06-29 10:02:38 +08005969#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01005970 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 &&
5971 (!(mbedtls_ssl_sig_alg_is_supported(ssl, sig_alg) &&
5972 mbedtls_ssl_sig_alg_is_offered(ssl, sig_alg)))) {
Gabor Mezei078e8032022-04-27 21:17:56 +02005973 continue;
Jerry Yu2fe6c632022-06-29 10:02:38 +08005974 }
5975#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gabor Mezei078e8032022-04-27 21:17:56 +02005976
Gilles Peskine449bd832023-01-11 14:50:10 +01005977 MBEDTLS_SSL_DEBUG_MSG(4, ("valid signature algorithm: %s",
5978 mbedtls_ssl_sig_alg_to_str(sig_alg)));
Gabor Mezei078e8032022-04-27 21:17:56 +02005979
Gilles Peskine449bd832023-01-11 14:50:10 +01005980 if (common_idx + 1 < MBEDTLS_RECEIVED_SIG_ALGS_SIZE) {
Gabor Mezei078e8032022-04-27 21:17:56 +02005981 ssl->handshake->received_sig_algs[common_idx] = sig_alg;
5982 common_idx += 1;
5983 }
5984 }
5985 /* Check that we consumed all the message. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005986 if (p != end) {
5987 MBEDTLS_SSL_DEBUG_MSG(1,
5988 ("Signature algorithms extension length misaligned"));
5989 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
5990 MBEDTLS_ERR_SSL_DECODE_ERROR);
5991 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Gabor Mezei078e8032022-04-27 21:17:56 +02005992 }
5993
Gilles Peskine449bd832023-01-11 14:50:10 +01005994 if (common_idx == 0) {
5995 MBEDTLS_SSL_DEBUG_MSG(3, ("no signature algorithm in common"));
5996 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
5997 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
5998 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Gabor Mezei078e8032022-04-27 21:17:56 +02005999 }
6000
Gabor Mezei15b95a62022-05-09 16:37:58 +02006001 ssl->handshake->received_sig_algs[common_idx] = MBEDTLS_TLS_SIG_NONE;
Gilles Peskine449bd832023-01-11 14:50:10 +01006002 return 0;
Gabor Mezei078e8032022-04-27 21:17:56 +02006003}
6004
Ronald Crone68ab4f2022-10-05 12:46:29 +02006005#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Gabor Mezei078e8032022-04-27 21:17:56 +02006006
Jerry Yudc7bd172022-02-17 13:44:15 +08006007#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6008
6009#if defined(MBEDTLS_USE_PSA_CRYPTO)
6010
Gilles Peskine449bd832023-01-11 14:50:10 +01006011static psa_status_t setup_psa_key_derivation(psa_key_derivation_operation_t *derivation,
6012 mbedtls_svc_key_id_t key,
6013 psa_algorithm_t alg,
6014 const unsigned char *raw_psk, size_t raw_psk_length,
6015 const unsigned char *seed, size_t seed_length,
6016 const unsigned char *label, size_t label_length,
6017 const unsigned char *other_secret,
6018 size_t other_secret_length,
6019 size_t capacity)
Jerry Yudc7bd172022-02-17 13:44:15 +08006020{
6021 psa_status_t status;
6022
Gilles Peskine449bd832023-01-11 14:50:10 +01006023 status = psa_key_derivation_setup(derivation, alg);
6024 if (status != PSA_SUCCESS) {
6025 return status;
6026 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006027
Gilles Peskine449bd832023-01-11 14:50:10 +01006028 if (PSA_ALG_IS_TLS12_PRF(alg) || PSA_ALG_IS_TLS12_PSK_TO_MS(alg)) {
6029 status = psa_key_derivation_input_bytes(derivation,
6030 PSA_KEY_DERIVATION_INPUT_SEED,
6031 seed, seed_length);
6032 if (status != PSA_SUCCESS) {
6033 return status;
Przemek Stekiel1f027032022-04-05 17:12:11 +02006034 }
6035
Gilles Peskine449bd832023-01-11 14:50:10 +01006036 if (other_secret != NULL) {
6037 status = psa_key_derivation_input_bytes(derivation,
6038 PSA_KEY_DERIVATION_INPUT_OTHER_SECRET,
6039 other_secret, other_secret_length);
6040 if (status != PSA_SUCCESS) {
6041 return status;
6042 }
6043 }
6044
6045 if (mbedtls_svc_key_id_is_null(key)) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006046 status = psa_key_derivation_input_bytes(
6047 derivation, PSA_KEY_DERIVATION_INPUT_SECRET,
Gilles Peskine449bd832023-01-11 14:50:10 +01006048 raw_psk, raw_psk_length);
6049 } else {
Jerry Yudc7bd172022-02-17 13:44:15 +08006050 status = psa_key_derivation_input_key(
Gilles Peskine449bd832023-01-11 14:50:10 +01006051 derivation, PSA_KEY_DERIVATION_INPUT_SECRET, key);
Jerry Yudc7bd172022-02-17 13:44:15 +08006052 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006053 if (status != PSA_SUCCESS) {
6054 return status;
6055 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006056
Gilles Peskine449bd832023-01-11 14:50:10 +01006057 status = psa_key_derivation_input_bytes(derivation,
6058 PSA_KEY_DERIVATION_INPUT_LABEL,
6059 label, label_length);
6060 if (status != PSA_SUCCESS) {
6061 return status;
6062 }
6063 } else {
6064 return PSA_ERROR_NOT_SUPPORTED;
Jerry Yudc7bd172022-02-17 13:44:15 +08006065 }
6066
Gilles Peskine449bd832023-01-11 14:50:10 +01006067 status = psa_key_derivation_set_capacity(derivation, capacity);
6068 if (status != PSA_SUCCESS) {
6069 return status;
6070 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006071
Gilles Peskine449bd832023-01-11 14:50:10 +01006072 return PSA_SUCCESS;
Jerry Yudc7bd172022-02-17 13:44:15 +08006073}
6074
Andrzej Kurek57d10632022-10-24 10:32:01 -04006075#if defined(PSA_WANT_ALG_SHA_384) || \
6076 defined(PSA_WANT_ALG_SHA_256)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006077MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006078static int tls_prf_generic(mbedtls_md_type_t md_type,
6079 const unsigned char *secret, size_t slen,
6080 const char *label,
6081 const unsigned char *random, size_t rlen,
6082 unsigned char *dstbuf, size_t dlen)
Jerry Yudc7bd172022-02-17 13:44:15 +08006083{
6084 psa_status_t status;
6085 psa_algorithm_t alg;
6086 mbedtls_svc_key_id_t master_key = MBEDTLS_SVC_KEY_ID_INIT;
6087 psa_key_derivation_operation_t derivation =
6088 PSA_KEY_DERIVATION_OPERATION_INIT;
6089
Gilles Peskine449bd832023-01-11 14:50:10 +01006090 if (md_type == MBEDTLS_MD_SHA384) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006091 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
Gilles Peskine449bd832023-01-11 14:50:10 +01006092 } else {
Jerry Yudc7bd172022-02-17 13:44:15 +08006093 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
Gilles Peskine449bd832023-01-11 14:50:10 +01006094 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006095
6096 /* Normally a "secret" should be long enough to be impossible to
6097 * find by brute force, and in particular should not be empty. But
6098 * this PRF is also used to derive an IV, in particular in EAP-TLS,
6099 * and for this use case it makes sense to have a 0-length "secret".
6100 * Since the key API doesn't allow importing a key of length 0,
6101 * keep master_key=0, which setup_psa_key_derivation() understands
6102 * to mean a 0-length "secret" input. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006103 if (slen != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006104 psa_key_attributes_t key_attributes = psa_key_attributes_init();
Gilles Peskine449bd832023-01-11 14:50:10 +01006105 psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE);
6106 psa_set_key_algorithm(&key_attributes, alg);
6107 psa_set_key_type(&key_attributes, PSA_KEY_TYPE_DERIVE);
Jerry Yudc7bd172022-02-17 13:44:15 +08006108
Gilles Peskine449bd832023-01-11 14:50:10 +01006109 status = psa_import_key(&key_attributes, secret, slen, &master_key);
6110 if (status != PSA_SUCCESS) {
6111 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
6112 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006113 }
6114
Gilles Peskine449bd832023-01-11 14:50:10 +01006115 status = setup_psa_key_derivation(&derivation,
6116 master_key, alg,
6117 NULL, 0,
6118 random, rlen,
6119 (unsigned char const *) label,
6120 (size_t) strlen(label),
6121 NULL, 0,
6122 dlen);
6123 if (status != PSA_SUCCESS) {
6124 psa_key_derivation_abort(&derivation);
6125 psa_destroy_key(master_key);
6126 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yudc7bd172022-02-17 13:44:15 +08006127 }
6128
Gilles Peskine449bd832023-01-11 14:50:10 +01006129 status = psa_key_derivation_output_bytes(&derivation, dstbuf, dlen);
6130 if (status != PSA_SUCCESS) {
6131 psa_key_derivation_abort(&derivation);
6132 psa_destroy_key(master_key);
6133 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yudc7bd172022-02-17 13:44:15 +08006134 }
6135
Gilles Peskine449bd832023-01-11 14:50:10 +01006136 status = psa_key_derivation_abort(&derivation);
6137 if (status != PSA_SUCCESS) {
6138 psa_destroy_key(master_key);
6139 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yudc7bd172022-02-17 13:44:15 +08006140 }
6141
Gilles Peskine449bd832023-01-11 14:50:10 +01006142 if (!mbedtls_svc_key_id_is_null(master_key)) {
6143 status = psa_destroy_key(master_key);
6144 }
6145 if (status != PSA_SUCCESS) {
6146 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
6147 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006148
Gilles Peskine449bd832023-01-11 14:50:10 +01006149 return 0;
Jerry Yudc7bd172022-02-17 13:44:15 +08006150}
Andrzej Kurek57d10632022-10-24 10:32:01 -04006151#endif /* PSA_WANT_ALG_SHA_256 || PSA_WANT_ALG_SHA_384 */
Jerry Yudc7bd172022-02-17 13:44:15 +08006152#else /* MBEDTLS_USE_PSA_CRYPTO */
6153
Andrzej Kurek57d10632022-10-24 10:32:01 -04006154#if defined(MBEDTLS_MD_C) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01006155 (defined(MBEDTLS_SHA256_C) || \
6156 defined(MBEDTLS_SHA384_C))
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006157MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006158static int tls_prf_generic(mbedtls_md_type_t md_type,
6159 const unsigned char *secret, size_t slen,
6160 const char *label,
6161 const unsigned char *random, size_t rlen,
6162 unsigned char *dstbuf, size_t dlen)
Jerry Yudc7bd172022-02-17 13:44:15 +08006163{
6164 size_t nb;
6165 size_t i, j, k, md_len;
6166 unsigned char *tmp;
6167 size_t tmp_len = 0;
6168 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
6169 const mbedtls_md_info_t *md_info;
6170 mbedtls_md_context_t md_ctx;
6171 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6172
Gilles Peskine449bd832023-01-11 14:50:10 +01006173 mbedtls_md_init(&md_ctx);
Jerry Yudc7bd172022-02-17 13:44:15 +08006174
Gilles Peskine449bd832023-01-11 14:50:10 +01006175 if ((md_info = mbedtls_md_info_from_type(md_type)) == NULL) {
6176 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
6177 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006178
Gilles Peskine449bd832023-01-11 14:50:10 +01006179 md_len = mbedtls_md_get_size(md_info);
Jerry Yudc7bd172022-02-17 13:44:15 +08006180
Gilles Peskine449bd832023-01-11 14:50:10 +01006181 tmp_len = md_len + strlen(label) + rlen;
6182 tmp = mbedtls_calloc(1, tmp_len);
6183 if (tmp == NULL) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006184 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
6185 goto exit;
6186 }
6187
Gilles Peskine449bd832023-01-11 14:50:10 +01006188 nb = strlen(label);
6189 memcpy(tmp + md_len, label, nb);
6190 memcpy(tmp + md_len + nb, random, rlen);
Jerry Yudc7bd172022-02-17 13:44:15 +08006191 nb += rlen;
6192
6193 /*
6194 * Compute P_<hash>(secret, label + random)[0..dlen]
6195 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006196 if ((ret = mbedtls_md_setup(&md_ctx, md_info, 1)) != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006197 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006198 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006199
Gilles Peskine449bd832023-01-11 14:50:10 +01006200 ret = mbedtls_md_hmac_starts(&md_ctx, secret, slen);
6201 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006202 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006203 }
6204 ret = mbedtls_md_hmac_update(&md_ctx, tmp + md_len, nb);
6205 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006206 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006207 }
6208 ret = mbedtls_md_hmac_finish(&md_ctx, tmp);
6209 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006210 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006211 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006212
Gilles Peskine449bd832023-01-11 14:50:10 +01006213 for (i = 0; i < dlen; i += md_len) {
6214 ret = mbedtls_md_hmac_reset(&md_ctx);
6215 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006216 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006217 }
6218 ret = mbedtls_md_hmac_update(&md_ctx, tmp, md_len + nb);
6219 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006220 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006221 }
6222 ret = mbedtls_md_hmac_finish(&md_ctx, h_i);
6223 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006224 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006225 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006226
Gilles Peskine449bd832023-01-11 14:50:10 +01006227 ret = mbedtls_md_hmac_reset(&md_ctx);
6228 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006229 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006230 }
6231 ret = mbedtls_md_hmac_update(&md_ctx, tmp, md_len);
6232 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006233 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006234 }
6235 ret = mbedtls_md_hmac_finish(&md_ctx, tmp);
6236 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006237 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006238 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006239
Gilles Peskine449bd832023-01-11 14:50:10 +01006240 k = (i + md_len > dlen) ? dlen % md_len : md_len;
Jerry Yudc7bd172022-02-17 13:44:15 +08006241
Gilles Peskine449bd832023-01-11 14:50:10 +01006242 for (j = 0; j < k; j++) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006243 dstbuf[i + j] = h_i[j];
Gilles Peskine449bd832023-01-11 14:50:10 +01006244 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006245 }
6246
6247exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006248 mbedtls_md_free(&md_ctx);
Jerry Yudc7bd172022-02-17 13:44:15 +08006249
Gilles Peskine449bd832023-01-11 14:50:10 +01006250 if (tmp != NULL) {
6251 mbedtls_platform_zeroize(tmp, tmp_len);
6252 }
Dave Rodgman29b9b2b2022-11-01 16:08:14 +00006253
Gilles Peskine449bd832023-01-11 14:50:10 +01006254 mbedtls_platform_zeroize(h_i, sizeof(h_i));
Jerry Yudc7bd172022-02-17 13:44:15 +08006255
Gilles Peskine449bd832023-01-11 14:50:10 +01006256 mbedtls_free(tmp);
Jerry Yudc7bd172022-02-17 13:44:15 +08006257
Gilles Peskine449bd832023-01-11 14:50:10 +01006258 return ret;
Jerry Yudc7bd172022-02-17 13:44:15 +08006259}
Andrzej Kurek57d10632022-10-24 10:32:01 -04006260#endif /* MBEDTLS_MD_C && ( MBEDTLS_SHA256_C || MBEDTLS_SHA384_C ) */
Jerry Yudc7bd172022-02-17 13:44:15 +08006261#endif /* MBEDTLS_USE_PSA_CRYPTO */
6262
Andrzej Kurek25f27152022-08-17 16:09:31 -04006263#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006264MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006265static int tls_prf_sha256(const unsigned char *secret, size_t slen,
6266 const char *label,
6267 const unsigned char *random, size_t rlen,
6268 unsigned char *dstbuf, size_t dlen)
Jerry Yudc7bd172022-02-17 13:44:15 +08006269{
Gilles Peskine449bd832023-01-11 14:50:10 +01006270 return tls_prf_generic(MBEDTLS_MD_SHA256, secret, slen,
6271 label, random, rlen, dstbuf, dlen);
Jerry Yudc7bd172022-02-17 13:44:15 +08006272}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04006273#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudc7bd172022-02-17 13:44:15 +08006274
Andrzej Kurek25f27152022-08-17 16:09:31 -04006275#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006276MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006277static int tls_prf_sha384(const unsigned char *secret, size_t slen,
6278 const char *label,
6279 const unsigned char *random, size_t rlen,
6280 unsigned char *dstbuf, size_t dlen)
Jerry Yudc7bd172022-02-17 13:44:15 +08006281{
Gilles Peskine449bd832023-01-11 14:50:10 +01006282 return tls_prf_generic(MBEDTLS_MD_SHA384, secret, slen,
6283 label, random, rlen, dstbuf, dlen);
Jerry Yudc7bd172022-02-17 13:44:15 +08006284}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04006285#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudc7bd172022-02-17 13:44:15 +08006286
Jerry Yuf009d862022-02-17 14:01:37 +08006287/*
6288 * Set appropriate PRF function and other SSL / TLS1.2 functions
6289 *
6290 * Inputs:
Jerry Yuf009d862022-02-17 14:01:37 +08006291 * - hash associated with the ciphersuite (only used by TLS 1.2)
6292 *
6293 * Outputs:
6294 * - the tls_prf, calc_verify and calc_finished members of handshake structure
6295 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006296MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006297static int ssl_set_handshake_prfs(mbedtls_ssl_handshake_params *handshake,
6298 mbedtls_md_type_t hash)
Jerry Yuf009d862022-02-17 14:01:37 +08006299{
Andrzej Kurek25f27152022-08-17 16:09:31 -04006300#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01006301 if (hash == MBEDTLS_MD_SHA384) {
Jerry Yuf009d862022-02-17 14:01:37 +08006302 handshake->tls_prf = tls_prf_sha384;
6303 handshake->calc_verify = ssl_calc_verify_tls_sha384;
6304 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Gilles Peskine449bd832023-01-11 14:50:10 +01006305 } else
Jerry Yuf009d862022-02-17 14:01:37 +08006306#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04006307#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuf009d862022-02-17 14:01:37 +08006308 {
Ronald Cron81591aa2022-03-07 09:05:51 +01006309 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08006310 handshake->tls_prf = tls_prf_sha256;
6311 handshake->calc_verify = ssl_calc_verify_tls_sha256;
6312 handshake->calc_finished = ssl_calc_finished_tls_sha256;
6313 }
Ronald Cron81591aa2022-03-07 09:05:51 +01006314#else
Jerry Yuf009d862022-02-17 14:01:37 +08006315 {
Jerry Yuf009d862022-02-17 14:01:37 +08006316 (void) handshake;
Ronald Cron81591aa2022-03-07 09:05:51 +01006317 (void) hash;
Gilles Peskine449bd832023-01-11 14:50:10 +01006318 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yuf009d862022-02-17 14:01:37 +08006319 }
Ronald Cron81591aa2022-03-07 09:05:51 +01006320#endif
Jerry Yuf009d862022-02-17 14:01:37 +08006321
Gilles Peskine449bd832023-01-11 14:50:10 +01006322 return 0;
Jerry Yuf009d862022-02-17 14:01:37 +08006323}
Jerry Yud6ab2352022-02-17 14:03:43 +08006324
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006325/*
6326 * Compute master secret if needed
6327 *
6328 * Parameters:
6329 * [in/out] handshake
6330 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
6331 * (PSA-PSK) ciphersuite_info, psk_opaque
6332 * [out] premaster (cleared)
6333 * [out] master
6334 * [in] ssl: optionally used for debugging, EMS and PSA-PSK
6335 * debug: conf->f_dbg, conf->p_dbg
6336 * EMS: passed to calc_verify (debug + session_negotiate)
Ronald Crona25cf582022-03-07 11:10:36 +01006337 * PSA-PSA: conf
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006338 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006339MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006340static int ssl_compute_master(mbedtls_ssl_handshake_params *handshake,
6341 unsigned char *master,
6342 const mbedtls_ssl_context *ssl)
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006343{
6344 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6345
6346 /* cf. RFC 5246, Section 8.1:
6347 * "The master secret is always exactly 48 bytes in length." */
6348 size_t const master_secret_len = 48;
6349
6350#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
6351 unsigned char session_hash[48];
6352#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
6353
6354 /* The label for the KDF used for key expansion.
6355 * This is either "master secret" or "extended master secret"
6356 * depending on whether the Extended Master Secret extension
6357 * is used. */
6358 char const *lbl = "master secret";
6359
Przemek Stekielae4ed302022-04-05 17:15:55 +02006360 /* The seed for the KDF used for key expansion.
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006361 * - If the Extended Master Secret extension is not used,
6362 * this is ClientHello.Random + ServerHello.Random
6363 * (see Sect. 8.1 in RFC 5246).
6364 * - If the Extended Master Secret extension is used,
6365 * this is the transcript of the handshake so far.
6366 * (see Sect. 4 in RFC 7627). */
Przemek Stekielae4ed302022-04-05 17:15:55 +02006367 unsigned char const *seed = handshake->randbytes;
6368 size_t seed_len = 64;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006369
6370#if !defined(MBEDTLS_DEBUG_C) && \
6371 !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \
6372 !(defined(MBEDTLS_USE_PSA_CRYPTO) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01006373 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED))
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006374 ssl = NULL; /* make sure we don't use it except for those cases */
6375 (void) ssl;
6376#endif
6377
Gilles Peskine449bd832023-01-11 14:50:10 +01006378 if (handshake->resume != 0) {
6379 MBEDTLS_SSL_DEBUG_MSG(3, ("no premaster (session resumed)"));
6380 return 0;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006381 }
6382
6383#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Gilles Peskine449bd832023-01-11 14:50:10 +01006384 if (handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED) {
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006385 lbl = "extended master secret";
Przemek Stekielae4ed302022-04-05 17:15:55 +02006386 seed = session_hash;
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01006387 ret = handshake->calc_verify(ssl, session_hash, &seed_len);
6388 if (ret != 0) {
6389 MBEDTLS_SSL_DEBUG_RET(1, "calc_verify", ret);
6390 }
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006391
Gilles Peskine449bd832023-01-11 14:50:10 +01006392 MBEDTLS_SSL_DEBUG_BUF(3, "session hash for extended master secret",
6393 session_hash, seed_len);
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006394 }
Przemek Stekiel169bf0b2022-04-29 07:53:29 +02006395#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006396
Przemek Stekiel99114f32022-04-22 11:20:09 +02006397#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Przemek Stekiel8a4b7fd2022-04-28 09:22:22 +02006398 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006399 if (mbedtls_ssl_ciphersuite_uses_psk(handshake->ciphersuite_info) == 1) {
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006400 /* Perform PSK-to-MS expansion in a single step. */
6401 psa_status_t status;
6402 psa_algorithm_t alg;
6403 mbedtls_svc_key_id_t psk;
6404 psa_key_derivation_operation_t derivation =
6405 PSA_KEY_DERIVATION_OPERATION_INIT;
6406 mbedtls_md_type_t hash_alg = handshake->ciphersuite_info->mac;
6407
Gilles Peskine449bd832023-01-11 14:50:10 +01006408 MBEDTLS_SSL_DEBUG_MSG(2, ("perform PSA-based PSK-to-MS expansion"));
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006409
Gilles Peskine449bd832023-01-11 14:50:10 +01006410 psk = mbedtls_ssl_get_opaque_psk(ssl);
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006411
Gilles Peskine449bd832023-01-11 14:50:10 +01006412 if (hash_alg == MBEDTLS_MD_SHA384) {
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006413 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
Gilles Peskine449bd832023-01-11 14:50:10 +01006414 } else {
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006415 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
Gilles Peskine449bd832023-01-11 14:50:10 +01006416 }
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006417
Przemek Stekiel51a1f362022-04-13 08:57:06 +02006418 size_t other_secret_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006419 unsigned char *other_secret = NULL;
Przemek Stekielc2033402022-04-05 17:19:41 +02006420
Gilles Peskine449bd832023-01-11 14:50:10 +01006421 switch (handshake->ciphersuite_info->key_exchange) {
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006422 /* Provide other secret.
Przemek Stekiel51a1f362022-04-13 08:57:06 +02006423 * Other secret is stored in premaster, where first 2 bytes hold the
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006424 * length of the other key.
Przemek Stekielc2033402022-04-05 17:19:41 +02006425 */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006426 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Przemek Stekiel8abcee92022-04-28 09:16:28 +02006427 /* For RSA-PSK other key length is always 48 bytes. */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006428 other_secret_len = 48;
6429 other_secret = handshake->premaster + 2;
6430 break;
6431 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006432 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006433 other_secret_len = MBEDTLS_GET_UINT16_BE(handshake->premaster, 0);
6434 other_secret = handshake->premaster + 2;
6435 break;
6436 default:
6437 break;
Przemek Stekielc2033402022-04-05 17:19:41 +02006438 }
6439
Gilles Peskine449bd832023-01-11 14:50:10 +01006440 status = setup_psa_key_derivation(&derivation, psk, alg,
6441 ssl->conf->psk, ssl->conf->psk_len,
6442 seed, seed_len,
6443 (unsigned char const *) lbl,
6444 (size_t) strlen(lbl),
6445 other_secret, other_secret_len,
6446 master_secret_len);
6447 if (status != PSA_SUCCESS) {
6448 psa_key_derivation_abort(&derivation);
6449 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006450 }
6451
Gilles Peskine449bd832023-01-11 14:50:10 +01006452 status = psa_key_derivation_output_bytes(&derivation,
6453 master,
6454 master_secret_len);
6455 if (status != PSA_SUCCESS) {
6456 psa_key_derivation_abort(&derivation);
6457 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006458 }
6459
Gilles Peskine449bd832023-01-11 14:50:10 +01006460 status = psa_key_derivation_abort(&derivation);
6461 if (status != PSA_SUCCESS) {
6462 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
6463 }
6464 } else
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006465#endif
6466 {
Neil Armstrongca7d5062022-05-31 14:43:23 +02006467#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01006468 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
6469 if (handshake->ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE) {
Neil Armstrongca7d5062022-05-31 14:43:23 +02006470 psa_status_t status;
6471 psa_algorithm_t alg = PSA_ALG_TLS12_ECJPAKE_TO_PMS;
6472 psa_key_derivation_operation_t derivation =
6473 PSA_KEY_DERIVATION_OPERATION_INIT;
6474
Gilles Peskine449bd832023-01-11 14:50:10 +01006475 MBEDTLS_SSL_DEBUG_MSG(2, ("perform PSA-based PMS KDF for ECJPAKE"));
Neil Armstrongca7d5062022-05-31 14:43:23 +02006476
6477 handshake->pmslen = PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE;
6478
Gilles Peskine449bd832023-01-11 14:50:10 +01006479 status = psa_key_derivation_setup(&derivation, alg);
6480 if (status != PSA_SUCCESS) {
6481 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Neil Armstrongca7d5062022-05-31 14:43:23 +02006482 }
6483
Gilles Peskine449bd832023-01-11 14:50:10 +01006484 status = psa_key_derivation_set_capacity(&derivation,
6485 PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE);
6486 if (status != PSA_SUCCESS) {
6487 psa_key_derivation_abort(&derivation);
6488 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Neil Armstrongca7d5062022-05-31 14:43:23 +02006489 }
6490
Gilles Peskine449bd832023-01-11 14:50:10 +01006491 status = psa_pake_get_implicit_key(&handshake->psa_pake_ctx,
6492 &derivation);
6493 if (status != PSA_SUCCESS) {
6494 psa_key_derivation_abort(&derivation);
6495 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Neil Armstrongca7d5062022-05-31 14:43:23 +02006496 }
6497
Gilles Peskine449bd832023-01-11 14:50:10 +01006498 status = psa_key_derivation_output_bytes(&derivation,
6499 handshake->premaster,
6500 handshake->pmslen);
6501 if (status != PSA_SUCCESS) {
6502 psa_key_derivation_abort(&derivation);
6503 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
6504 }
6505
6506 status = psa_key_derivation_abort(&derivation);
6507 if (status != PSA_SUCCESS) {
6508 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Neil Armstrongca7d5062022-05-31 14:43:23 +02006509 }
6510 }
6511#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01006512 ret = handshake->tls_prf(handshake->premaster, handshake->pmslen,
6513 lbl, seed, seed_len,
6514 master,
6515 master_secret_len);
6516 if (ret != 0) {
6517 MBEDTLS_SSL_DEBUG_RET(1, "prf", ret);
6518 return ret;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006519 }
6520
Gilles Peskine449bd832023-01-11 14:50:10 +01006521 MBEDTLS_SSL_DEBUG_BUF(3, "premaster secret",
6522 handshake->premaster,
6523 handshake->pmslen);
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006524
Gilles Peskine449bd832023-01-11 14:50:10 +01006525 mbedtls_platform_zeroize(handshake->premaster,
6526 sizeof(handshake->premaster));
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006527 }
6528
Gilles Peskine449bd832023-01-11 14:50:10 +01006529 return 0;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006530}
6531
Gilles Peskine449bd832023-01-11 14:50:10 +01006532int mbedtls_ssl_derive_keys(mbedtls_ssl_context *ssl)
Jerry Yud62f87e2022-02-17 14:09:02 +08006533{
6534 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6535 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
6536 ssl->handshake->ciphersuite_info;
6537
Gilles Peskine449bd832023-01-11 14:50:10 +01006538 MBEDTLS_SSL_DEBUG_MSG(2, ("=> derive keys"));
Jerry Yud62f87e2022-02-17 14:09:02 +08006539
6540 /* Set PRF, calc_verify and calc_finished function pointers */
Gilles Peskine449bd832023-01-11 14:50:10 +01006541 ret = ssl_set_handshake_prfs(ssl->handshake,
6542 ciphersuite_info->mac);
6543 if (ret != 0) {
6544 MBEDTLS_SSL_DEBUG_RET(1, "ssl_set_handshake_prfs", ret);
6545 return ret;
Jerry Yud62f87e2022-02-17 14:09:02 +08006546 }
6547
6548 /* Compute master secret if needed */
Gilles Peskine449bd832023-01-11 14:50:10 +01006549 ret = ssl_compute_master(ssl->handshake,
6550 ssl->session_negotiate->master,
6551 ssl);
6552 if (ret != 0) {
6553 MBEDTLS_SSL_DEBUG_RET(1, "ssl_compute_master", ret);
6554 return ret;
Jerry Yud62f87e2022-02-17 14:09:02 +08006555 }
6556
6557 /* Swap the client and server random values:
6558 * - MS derivation wanted client+server (RFC 5246 8.1)
6559 * - key derivation wants server+client (RFC 5246 6.3) */
6560 {
6561 unsigned char tmp[64];
Gilles Peskine449bd832023-01-11 14:50:10 +01006562 memcpy(tmp, ssl->handshake->randbytes, 64);
6563 memcpy(ssl->handshake->randbytes, tmp + 32, 32);
6564 memcpy(ssl->handshake->randbytes + 32, tmp, 32);
6565 mbedtls_platform_zeroize(tmp, sizeof(tmp));
Jerry Yud62f87e2022-02-17 14:09:02 +08006566 }
6567
6568 /* Populate transform structure */
Gilles Peskine449bd832023-01-11 14:50:10 +01006569 ret = ssl_tls12_populate_transform(ssl->transform_negotiate,
6570 ssl->session_negotiate->ciphersuite,
6571 ssl->session_negotiate->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02006572#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01006573 ssl->session_negotiate->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02006574#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01006575 ssl->handshake->tls_prf,
6576 ssl->handshake->randbytes,
6577 ssl->tls_version,
6578 ssl->conf->endpoint,
6579 ssl);
6580 if (ret != 0) {
6581 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls12_populate_transform", ret);
6582 return ret;
Jerry Yud62f87e2022-02-17 14:09:02 +08006583 }
6584
6585 /* We no longer need Server/ClientHello.random values */
Gilles Peskine449bd832023-01-11 14:50:10 +01006586 mbedtls_platform_zeroize(ssl->handshake->randbytes,
6587 sizeof(ssl->handshake->randbytes));
Jerry Yud62f87e2022-02-17 14:09:02 +08006588
Gilles Peskine449bd832023-01-11 14:50:10 +01006589 MBEDTLS_SSL_DEBUG_MSG(2, ("<= derive keys"));
Jerry Yud62f87e2022-02-17 14:09:02 +08006590
Gilles Peskine449bd832023-01-11 14:50:10 +01006591 return 0;
Jerry Yud62f87e2022-02-17 14:09:02 +08006592}
Jerry Yu8392e0d2022-02-17 14:10:24 +08006593
Gilles Peskine449bd832023-01-11 14:50:10 +01006594int mbedtls_ssl_set_calc_verify_md(mbedtls_ssl_context *ssl, int md)
Ronald Cron4dcbca92022-03-07 10:21:40 +01006595{
Gilles Peskine449bd832023-01-11 14:50:10 +01006596 switch (md) {
Andrzej Kurek25f27152022-08-17 16:09:31 -04006597#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron4dcbca92022-03-07 10:21:40 +01006598 case MBEDTLS_SSL_HASH_SHA384:
6599 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
6600 break;
6601#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04006602#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron4dcbca92022-03-07 10:21:40 +01006603 case MBEDTLS_SSL_HASH_SHA256:
6604 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
6605 break;
6606#endif
6607 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006608 return -1;
Ronald Cron4dcbca92022-03-07 10:21:40 +01006609 }
Andrzej Kurekeabeb302022-10-17 07:52:51 -04006610#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
6611 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
6612 (void) ssl;
6613#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01006614 return 0;
Ronald Cron4dcbca92022-03-07 10:21:40 +01006615}
6616
Andrzej Kurek25f27152022-08-17 16:09:31 -04006617#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01006618int ssl_calc_verify_tls_sha256(const mbedtls_ssl_context *ssl,
6619 unsigned char *hash,
6620 size_t *hlen)
Jerry Yu8392e0d2022-02-17 14:10:24 +08006621{
6622#if defined(MBEDTLS_USE_PSA_CRYPTO)
6623 size_t hash_size;
6624 psa_status_t status;
6625 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
6626
Gilles Peskine449bd832023-01-11 14:50:10 +01006627 MBEDTLS_SSL_DEBUG_MSG(2, ("=> PSA calc verify sha256"));
6628 status = psa_hash_clone(&ssl->handshake->fin_sha256_psa, &sha256_psa);
6629 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006630 goto exit;
Jerry Yu8392e0d2022-02-17 14:10:24 +08006631 }
6632
Gilles Peskine449bd832023-01-11 14:50:10 +01006633 status = psa_hash_finish(&sha256_psa, hash, 32, &hash_size);
6634 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006635 goto exit;
Jerry Yu8392e0d2022-02-17 14:10:24 +08006636 }
6637
6638 *hlen = 32;
Gilles Peskine449bd832023-01-11 14:50:10 +01006639 MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated verify result", hash, *hlen);
6640 MBEDTLS_SSL_DEBUG_MSG(2, ("<= PSA calc verify"));
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006641
6642exit:
6643 psa_hash_abort(&sha256_psa);
Andrzej Kurekdaf5b562023-03-03 05:52:28 -05006644 return PSA_TO_MD_ERR(status);
Jerry Yu8392e0d2022-02-17 14:10:24 +08006645#else
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006646 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01006647 mbedtls_md_context_t sha256;
Jerry Yu8392e0d2022-02-17 14:10:24 +08006648
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01006649 mbedtls_md_init(&sha256);
Jerry Yu8392e0d2022-02-17 14:10:24 +08006650
Gilles Peskine449bd832023-01-11 14:50:10 +01006651 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc verify sha256"));
Jerry Yu8392e0d2022-02-17 14:10:24 +08006652
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01006653 ret = mbedtls_md_setup(&sha256, mbedtls_md_info_from_type(MBEDTLS_MD_SHA256), 0);
6654 if (ret != 0) {
6655 goto exit;
6656 }
6657 ret = mbedtls_md_clone(&sha256, &ssl->handshake->fin_sha256);
6658 if (ret != 0) {
6659 goto exit;
6660 }
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006661
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01006662 ret = mbedtls_md_finish(&sha256, hash);
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006663 if (ret != 0) {
6664 goto exit;
6665 }
Jerry Yu8392e0d2022-02-17 14:10:24 +08006666
6667 *hlen = 32;
6668
Gilles Peskine449bd832023-01-11 14:50:10 +01006669 MBEDTLS_SSL_DEBUG_BUF(3, "calculated verify result", hash, *hlen);
6670 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc verify"));
Jerry Yu8392e0d2022-02-17 14:10:24 +08006671
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006672exit:
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01006673 mbedtls_md_free(&sha256);
Manuel Pégourié-Gonnard8e176f72023-02-09 10:33:54 +01006674 return ret;
Jerry Yu8392e0d2022-02-17 14:10:24 +08006675#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu8392e0d2022-02-17 14:10:24 +08006676}
Andrzej Kurek25f27152022-08-17 16:09:31 -04006677#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu8392e0d2022-02-17 14:10:24 +08006678
Andrzej Kurek25f27152022-08-17 16:09:31 -04006679#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01006680int ssl_calc_verify_tls_sha384(const mbedtls_ssl_context *ssl,
6681 unsigned char *hash,
6682 size_t *hlen)
Jerry Yuc1cb3842022-02-17 14:13:48 +08006683{
6684#if defined(MBEDTLS_USE_PSA_CRYPTO)
6685 size_t hash_size;
6686 psa_status_t status;
6687 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
6688
Gilles Peskine449bd832023-01-11 14:50:10 +01006689 MBEDTLS_SSL_DEBUG_MSG(2, ("=> PSA calc verify sha384"));
6690 status = psa_hash_clone(&ssl->handshake->fin_sha384_psa, &sha384_psa);
6691 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006692 goto exit;
Jerry Yuc1cb3842022-02-17 14:13:48 +08006693 }
6694
Gilles Peskine449bd832023-01-11 14:50:10 +01006695 status = psa_hash_finish(&sha384_psa, hash, 48, &hash_size);
6696 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006697 goto exit;
Jerry Yuc1cb3842022-02-17 14:13:48 +08006698 }
6699
6700 *hlen = 48;
Gilles Peskine449bd832023-01-11 14:50:10 +01006701 MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated verify result", hash, *hlen);
6702 MBEDTLS_SSL_DEBUG_MSG(2, ("<= PSA calc verify"));
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006703
6704exit:
6705 psa_hash_abort(&sha384_psa);
Andrzej Kurekdaf5b562023-03-03 05:52:28 -05006706 return PSA_TO_MD_ERR(status);
Jerry Yuc1cb3842022-02-17 14:13:48 +08006707#else
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006708 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01006709 mbedtls_md_context_t sha384;
Jerry Yuc1cb3842022-02-17 14:13:48 +08006710
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01006711 mbedtls_md_init(&sha384);
Jerry Yuc1cb3842022-02-17 14:13:48 +08006712
Gilles Peskine449bd832023-01-11 14:50:10 +01006713 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc verify sha384"));
Jerry Yuc1cb3842022-02-17 14:13:48 +08006714
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01006715 ret = mbedtls_md_setup(&sha384, mbedtls_md_info_from_type(MBEDTLS_MD_SHA384), 0);
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01006716 if (ret != 0) {
6717 goto exit;
6718 }
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01006719 ret = mbedtls_md_clone(&sha384, &ssl->handshake->fin_sha384);
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01006720 if (ret != 0) {
6721 goto exit;
6722 }
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006723
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01006724 ret = mbedtls_md_finish(&sha384, hash);
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006725 if (ret != 0) {
6726 goto exit;
6727 }
Jerry Yuc1cb3842022-02-17 14:13:48 +08006728
6729 *hlen = 48;
6730
Gilles Peskine449bd832023-01-11 14:50:10 +01006731 MBEDTLS_SSL_DEBUG_BUF(3, "calculated verify result", hash, *hlen);
6732 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc verify"));
Jerry Yuc1cb3842022-02-17 14:13:48 +08006733
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006734exit:
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01006735 mbedtls_md_free(&sha384);
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006736 return ret;
Jerry Yuc1cb3842022-02-17 14:13:48 +08006737#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yuc1cb3842022-02-17 14:13:48 +08006738}
Andrzej Kurek25f27152022-08-17 16:09:31 -04006739#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yuc1cb3842022-02-17 14:13:48 +08006740
Neil Armstrong80f6f322022-05-03 17:56:38 +02006741#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
6742 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006743int mbedtls_ssl_psk_derive_premaster(mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex)
Jerry Yuce3dca42022-02-17 14:16:37 +08006744{
6745 unsigned char *p = ssl->handshake->premaster;
Gilles Peskine449bd832023-01-11 14:50:10 +01006746 unsigned char *end = p + sizeof(ssl->handshake->premaster);
Jerry Yuce3dca42022-02-17 14:16:37 +08006747 const unsigned char *psk = NULL;
6748 size_t psk_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006749 int psk_ret = mbedtls_ssl_get_psk(ssl, &psk, &psk_len);
Jerry Yuce3dca42022-02-17 14:16:37 +08006750
Gilles Peskine449bd832023-01-11 14:50:10 +01006751 if (psk_ret == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED) {
Jerry Yuce3dca42022-02-17 14:16:37 +08006752 /*
6753 * This should never happen because the existence of a PSK is always
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006754 * checked before calling this function.
6755 *
6756 * The exception is opaque DHE-PSK. For DHE-PSK fill premaster with
Przemek Stekiel8abcee92022-04-28 09:16:28 +02006757 * the shared secret without PSK.
Jerry Yuce3dca42022-02-17 14:16:37 +08006758 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006759 if (key_ex != MBEDTLS_KEY_EXCHANGE_DHE_PSK) {
6760 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
6761 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006762 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006763 }
6764
6765 /*
6766 * PMS = struct {
6767 * opaque other_secret<0..2^16-1>;
6768 * opaque psk<0..2^16-1>;
6769 * };
6770 * with "other_secret" depending on the particular key exchange
6771 */
6772#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006773 if (key_ex == MBEDTLS_KEY_EXCHANGE_PSK) {
6774 if (end - p < 2) {
6775 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6776 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006777
Gilles Peskine449bd832023-01-11 14:50:10 +01006778 MBEDTLS_PUT_UINT16_BE(psk_len, p, 0);
Jerry Yuce3dca42022-02-17 14:16:37 +08006779 p += 2;
6780
Gilles Peskine449bd832023-01-11 14:50:10 +01006781 if (end < p || (size_t) (end - p) < psk_len) {
6782 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6783 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006784
Gilles Peskine449bd832023-01-11 14:50:10 +01006785 memset(p, 0, psk_len);
Jerry Yuce3dca42022-02-17 14:16:37 +08006786 p += psk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01006787 } else
Jerry Yuce3dca42022-02-17 14:16:37 +08006788#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
6789#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006790 if (key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK) {
Jerry Yuce3dca42022-02-17 14:16:37 +08006791 /*
6792 * other_secret already set by the ClientKeyExchange message,
6793 * and is 48 bytes long
6794 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006795 if (end - p < 2) {
6796 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6797 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006798
6799 *p++ = 0;
6800 *p++ = 48;
6801 p += 48;
Gilles Peskine449bd832023-01-11 14:50:10 +01006802 } else
Jerry Yuce3dca42022-02-17 14:16:37 +08006803#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
6804#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006805 if (key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK) {
Jerry Yuce3dca42022-02-17 14:16:37 +08006806 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6807 size_t len;
6808
6809 /* Write length only when we know the actual value */
Gilles Peskine449bd832023-01-11 14:50:10 +01006810 if ((ret = mbedtls_dhm_calc_secret(&ssl->handshake->dhm_ctx,
6811 p + 2, end - (p + 2), &len,
6812 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
6813 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_calc_secret", ret);
6814 return ret;
Jerry Yuce3dca42022-02-17 14:16:37 +08006815 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006816 MBEDTLS_PUT_UINT16_BE(len, p, 0);
Jerry Yuce3dca42022-02-17 14:16:37 +08006817 p += 2 + len;
6818
Gilles Peskine449bd832023-01-11 14:50:10 +01006819 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: K ", &ssl->handshake->dhm_ctx.K);
6820 } else
Jerry Yuce3dca42022-02-17 14:16:37 +08006821#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Neil Armstrong80f6f322022-05-03 17:56:38 +02006822#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006823 if (key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK) {
Jerry Yuce3dca42022-02-17 14:16:37 +08006824 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6825 size_t zlen;
6826
Gilles Peskine449bd832023-01-11 14:50:10 +01006827 if ((ret = mbedtls_ecdh_calc_secret(&ssl->handshake->ecdh_ctx, &zlen,
6828 p + 2, end - (p + 2),
6829 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
6830 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecdh_calc_secret", ret);
6831 return ret;
Jerry Yuce3dca42022-02-17 14:16:37 +08006832 }
6833
Gilles Peskine449bd832023-01-11 14:50:10 +01006834 MBEDTLS_PUT_UINT16_BE(zlen, p, 0);
Jerry Yuce3dca42022-02-17 14:16:37 +08006835 p += 2 + zlen;
6836
Gilles Peskine449bd832023-01-11 14:50:10 +01006837 MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx,
6838 MBEDTLS_DEBUG_ECDH_Z);
6839 } else
Neil Armstrong80f6f322022-05-03 17:56:38 +02006840#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Jerry Yuce3dca42022-02-17 14:16:37 +08006841 {
Gilles Peskine449bd832023-01-11 14:50:10 +01006842 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
6843 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yuce3dca42022-02-17 14:16:37 +08006844 }
6845
6846 /* opaque psk<0..2^16-1>; */
Gilles Peskine449bd832023-01-11 14:50:10 +01006847 if (end - p < 2) {
6848 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6849 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006850
Gilles Peskine449bd832023-01-11 14:50:10 +01006851 MBEDTLS_PUT_UINT16_BE(psk_len, p, 0);
Jerry Yuce3dca42022-02-17 14:16:37 +08006852 p += 2;
6853
Gilles Peskine449bd832023-01-11 14:50:10 +01006854 if (end < p || (size_t) (end - p) < psk_len) {
6855 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6856 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006857
Gilles Peskine449bd832023-01-11 14:50:10 +01006858 memcpy(p, psk, psk_len);
Jerry Yuce3dca42022-02-17 14:16:37 +08006859 p += psk_len;
6860
6861 ssl->handshake->pmslen = p - ssl->handshake->premaster;
6862
Gilles Peskine449bd832023-01-11 14:50:10 +01006863 return 0;
Jerry Yuce3dca42022-02-17 14:16:37 +08006864}
Neil Armstrong80f6f322022-05-03 17:56:38 +02006865#endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Jerry Yuc2c673d2022-02-17 14:20:39 +08006866
6867#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006868MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006869static int ssl_write_hello_request(mbedtls_ssl_context *ssl);
Jerry Yuc2c673d2022-02-17 14:20:39 +08006870
6871#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006872int mbedtls_ssl_resend_hello_request(mbedtls_ssl_context *ssl)
Jerry Yuc2c673d2022-02-17 14:20:39 +08006873{
6874 /* If renegotiation is not enforced, retransmit until we would reach max
6875 * timeout if we were using the usual handshake doubling scheme */
Gilles Peskine449bd832023-01-11 14:50:10 +01006876 if (ssl->conf->renego_max_records < 0) {
Jerry Yuc2c673d2022-02-17 14:20:39 +08006877 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
6878 unsigned char doublings = 1;
6879
Gilles Peskine449bd832023-01-11 14:50:10 +01006880 while (ratio != 0) {
Jerry Yuc2c673d2022-02-17 14:20:39 +08006881 ++doublings;
6882 ratio >>= 1;
6883 }
6884
Gilles Peskine449bd832023-01-11 14:50:10 +01006885 if (++ssl->renego_records_seen > doublings) {
6886 MBEDTLS_SSL_DEBUG_MSG(2, ("no longer retransmitting hello request"));
6887 return 0;
Jerry Yuc2c673d2022-02-17 14:20:39 +08006888 }
6889 }
6890
Gilles Peskine449bd832023-01-11 14:50:10 +01006891 return ssl_write_hello_request(ssl);
Jerry Yuc2c673d2022-02-17 14:20:39 +08006892}
6893#endif
6894#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Jerry Yud9526692022-02-17 14:23:47 +08006895
Jerry Yud9526692022-02-17 14:23:47 +08006896/*
6897 * Handshake functions
6898 */
6899#if !defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
6900/* No certificate support -> dummy functions */
Gilles Peskine449bd832023-01-11 14:50:10 +01006901int mbedtls_ssl_write_certificate(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08006902{
6903 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6904 ssl->handshake->ciphersuite_info;
6905
Gilles Peskine449bd832023-01-11 14:50:10 +01006906 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006907
Gilles Peskine449bd832023-01-11 14:50:10 +01006908 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
6909 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006910 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01006911 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08006912 }
6913
Gilles Peskine449bd832023-01-11 14:50:10 +01006914 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
6915 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08006916}
6917
Gilles Peskine449bd832023-01-11 14:50:10 +01006918int mbedtls_ssl_parse_certificate(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08006919{
6920 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6921 ssl->handshake->ciphersuite_info;
6922
Gilles Peskine449bd832023-01-11 14:50:10 +01006923 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006924
Gilles Peskine449bd832023-01-11 14:50:10 +01006925 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
6926 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006927 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01006928 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08006929 }
6930
Gilles Peskine449bd832023-01-11 14:50:10 +01006931 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
6932 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08006933}
6934
6935#else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
6936/* Some certificate support -> implement write and parse */
6937
Gilles Peskine449bd832023-01-11 14:50:10 +01006938int mbedtls_ssl_write_certificate(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08006939{
6940 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
6941 size_t i, n;
6942 const mbedtls_x509_crt *crt;
6943 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6944 ssl->handshake->ciphersuite_info;
6945
Gilles Peskine449bd832023-01-11 14:50:10 +01006946 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006947
Gilles Peskine449bd832023-01-11 14:50:10 +01006948 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
6949 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006950 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01006951 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08006952 }
6953
6954#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01006955 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
6956 if (ssl->handshake->client_auth == 0) {
6957 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006958 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01006959 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08006960 }
6961 }
6962#endif /* MBEDTLS_SSL_CLI_C */
6963#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01006964 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
6965 if (mbedtls_ssl_own_cert(ssl) == NULL) {
Jerry Yud9526692022-02-17 14:23:47 +08006966 /* Should never happen because we shouldn't have picked the
6967 * ciphersuite if we don't have a certificate. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006968 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08006969 }
6970 }
6971#endif
6972
Gilles Peskine449bd832023-01-11 14:50:10 +01006973 MBEDTLS_SSL_DEBUG_CRT(3, "own certificate", mbedtls_ssl_own_cert(ssl));
Jerry Yud9526692022-02-17 14:23:47 +08006974
6975 /*
6976 * 0 . 0 handshake type
6977 * 1 . 3 handshake length
6978 * 4 . 6 length of all certs
6979 * 7 . 9 length of cert. 1
6980 * 10 . n-1 peer certificate
6981 * n . n+2 length of cert. 2
6982 * n+3 . ... upper level cert, etc.
6983 */
6984 i = 7;
Gilles Peskine449bd832023-01-11 14:50:10 +01006985 crt = mbedtls_ssl_own_cert(ssl);
Jerry Yud9526692022-02-17 14:23:47 +08006986
Gilles Peskine449bd832023-01-11 14:50:10 +01006987 while (crt != NULL) {
Jerry Yud9526692022-02-17 14:23:47 +08006988 n = crt->raw.len;
Gilles Peskine449bd832023-01-11 14:50:10 +01006989 if (n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i) {
6990 MBEDTLS_SSL_DEBUG_MSG(1, ("certificate too large, %" MBEDTLS_PRINTF_SIZET
6991 " > %" MBEDTLS_PRINTF_SIZET,
6992 i + 3 + n, (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN));
6993 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
Jerry Yud9526692022-02-17 14:23:47 +08006994 }
6995
Gilles Peskine449bd832023-01-11 14:50:10 +01006996 ssl->out_msg[i] = MBEDTLS_BYTE_2(n);
6997 ssl->out_msg[i + 1] = MBEDTLS_BYTE_1(n);
6998 ssl->out_msg[i + 2] = MBEDTLS_BYTE_0(n);
Jerry Yud9526692022-02-17 14:23:47 +08006999
Gilles Peskine449bd832023-01-11 14:50:10 +01007000 i += 3; memcpy(ssl->out_msg + i, crt->raw.p, n);
Jerry Yud9526692022-02-17 14:23:47 +08007001 i += n; crt = crt->next;
7002 }
7003
Gilles Peskine449bd832023-01-11 14:50:10 +01007004 ssl->out_msg[4] = MBEDTLS_BYTE_2(i - 7);
7005 ssl->out_msg[5] = MBEDTLS_BYTE_1(i - 7);
7006 ssl->out_msg[6] = MBEDTLS_BYTE_0(i - 7);
Jerry Yud9526692022-02-17 14:23:47 +08007007
7008 ssl->out_msglen = i;
7009 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7010 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
7011
7012 ssl->state++;
7013
Gilles Peskine449bd832023-01-11 14:50:10 +01007014 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
7015 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
7016 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007017 }
7018
Gilles Peskine449bd832023-01-11 14:50:10 +01007019 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007020
Gilles Peskine449bd832023-01-11 14:50:10 +01007021 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007022}
7023
7024#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
7025
7026#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007027MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007028static int ssl_check_peer_crt_unchanged(mbedtls_ssl_context *ssl,
7029 unsigned char *crt_buf,
7030 size_t crt_buf_len)
Jerry Yud9526692022-02-17 14:23:47 +08007031{
7032 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
7033
Gilles Peskine449bd832023-01-11 14:50:10 +01007034 if (peer_crt == NULL) {
7035 return -1;
7036 }
Jerry Yud9526692022-02-17 14:23:47 +08007037
Gilles Peskine449bd832023-01-11 14:50:10 +01007038 if (peer_crt->raw.len != crt_buf_len) {
7039 return -1;
7040 }
Jerry Yud9526692022-02-17 14:23:47 +08007041
Gilles Peskine449bd832023-01-11 14:50:10 +01007042 return memcmp(peer_crt->raw.p, crt_buf, peer_crt->raw.len);
Jerry Yud9526692022-02-17 14:23:47 +08007043}
7044#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007045MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007046static int ssl_check_peer_crt_unchanged(mbedtls_ssl_context *ssl,
7047 unsigned char *crt_buf,
7048 size_t crt_buf_len)
Jerry Yud9526692022-02-17 14:23:47 +08007049{
7050 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7051 unsigned char const * const peer_cert_digest =
7052 ssl->session->peer_cert_digest;
7053 mbedtls_md_type_t const peer_cert_digest_type =
7054 ssl->session->peer_cert_digest_type;
7055 mbedtls_md_info_t const * const digest_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01007056 mbedtls_md_info_from_type(peer_cert_digest_type);
Jerry Yud9526692022-02-17 14:23:47 +08007057 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
7058 size_t digest_len;
7059
Gilles Peskine449bd832023-01-11 14:50:10 +01007060 if (peer_cert_digest == NULL || digest_info == NULL) {
7061 return -1;
7062 }
Jerry Yud9526692022-02-17 14:23:47 +08007063
Gilles Peskine449bd832023-01-11 14:50:10 +01007064 digest_len = mbedtls_md_get_size(digest_info);
7065 if (digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN) {
7066 return -1;
7067 }
Jerry Yud9526692022-02-17 14:23:47 +08007068
Gilles Peskine449bd832023-01-11 14:50:10 +01007069 ret = mbedtls_md(digest_info, crt_buf, crt_buf_len, tmp_digest);
7070 if (ret != 0) {
7071 return -1;
7072 }
Jerry Yud9526692022-02-17 14:23:47 +08007073
Gilles Peskine449bd832023-01-11 14:50:10 +01007074 return memcmp(tmp_digest, peer_cert_digest, digest_len);
Jerry Yud9526692022-02-17 14:23:47 +08007075}
7076#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7077#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
7078
7079/*
7080 * Once the certificate message is read, parse it into a cert chain and
7081 * perform basic checks, but leave actual verification to the caller
7082 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007083MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007084static int ssl_parse_certificate_chain(mbedtls_ssl_context *ssl,
7085 mbedtls_x509_crt *chain)
Jerry Yud9526692022-02-17 14:23:47 +08007086{
7087 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7088#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007089 int crt_cnt = 0;
Jerry Yud9526692022-02-17 14:23:47 +08007090#endif
7091 size_t i, n;
7092 uint8_t alert;
7093
Gilles Peskine449bd832023-01-11 14:50:10 +01007094 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
7095 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7096 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7097 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
7098 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Jerry Yud9526692022-02-17 14:23:47 +08007099 }
7100
Gilles Peskine449bd832023-01-11 14:50:10 +01007101 if (ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE) {
7102 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7103 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
7104 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Jerry Yud9526692022-02-17 14:23:47 +08007105 }
7106
Gilles Peskine449bd832023-01-11 14:50:10 +01007107 if (ssl->in_hslen < mbedtls_ssl_hs_hdr_len(ssl) + 3 + 3) {
7108 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7109 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7110 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
7111 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007112 }
7113
Gilles Peskine449bd832023-01-11 14:50:10 +01007114 i = mbedtls_ssl_hs_hdr_len(ssl);
Jerry Yud9526692022-02-17 14:23:47 +08007115
7116 /*
7117 * Same message structure as in mbedtls_ssl_write_certificate()
7118 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007119 n = (ssl->in_msg[i+1] << 8) | ssl->in_msg[i+2];
Jerry Yud9526692022-02-17 14:23:47 +08007120
Gilles Peskine449bd832023-01-11 14:50:10 +01007121 if (ssl->in_msg[i] != 0 ||
7122 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len(ssl)) {
7123 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7124 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7125 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
7126 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007127 }
7128
7129 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
7130 i += 3;
7131
7132 /* Iterate through and parse the CRTs in the provided chain. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007133 while (i < ssl->in_hslen) {
Jerry Yud9526692022-02-17 14:23:47 +08007134 /* Check that there's room for the next CRT's length fields. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007135 if (i + 3 > ssl->in_hslen) {
7136 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7137 mbedtls_ssl_send_alert_message(ssl,
7138 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7139 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
7140 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007141 }
7142 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
7143 * anything beyond 2**16 ~ 64K. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007144 if (ssl->in_msg[i] != 0) {
7145 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7146 mbedtls_ssl_send_alert_message(ssl,
7147 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7148 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT);
7149 return MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Jerry Yud9526692022-02-17 14:23:47 +08007150 }
7151
7152 /* Read length of the next CRT in the chain. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007153 n = ((unsigned int) ssl->in_msg[i + 1] << 8)
Jerry Yud9526692022-02-17 14:23:47 +08007154 | (unsigned int) ssl->in_msg[i + 2];
7155 i += 3;
7156
Gilles Peskine449bd832023-01-11 14:50:10 +01007157 if (n < 128 || i + n > ssl->in_hslen) {
7158 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7159 mbedtls_ssl_send_alert_message(ssl,
7160 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7161 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
7162 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007163 }
7164
7165 /* Check if we're handling the first CRT in the chain. */
7166#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007167 if (crt_cnt++ == 0 &&
Jerry Yud9526692022-02-17 14:23:47 +08007168 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Gilles Peskine449bd832023-01-11 14:50:10 +01007169 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
Jerry Yud9526692022-02-17 14:23:47 +08007170 /* During client-side renegotiation, check that the server's
7171 * end-CRTs hasn't changed compared to the initial handshake,
7172 * mitigating the triple handshake attack. On success, reuse
7173 * the original end-CRT instead of parsing it again. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007174 MBEDTLS_SSL_DEBUG_MSG(3, ("Check that peer CRT hasn't changed during renegotiation"));
7175 if (ssl_check_peer_crt_unchanged(ssl,
7176 &ssl->in_msg[i],
7177 n) != 0) {
7178 MBEDTLS_SSL_DEBUG_MSG(1, ("new server cert during renegotiation"));
7179 mbedtls_ssl_send_alert_message(ssl,
7180 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7181 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED);
7182 return MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Jerry Yud9526692022-02-17 14:23:47 +08007183 }
7184
7185 /* Now we can safely free the original chain. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007186 ssl_clear_peer_cert(ssl->session);
Jerry Yud9526692022-02-17 14:23:47 +08007187 }
7188#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
7189
7190 /* Parse the next certificate in the chain. */
7191#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007192 ret = mbedtls_x509_crt_parse_der(chain, ssl->in_msg + i, n);
Jerry Yud9526692022-02-17 14:23:47 +08007193#else
7194 /* If we don't need to store the CRT chain permanently, parse
7195 * it in-place from the input buffer instead of making a copy. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007196 ret = mbedtls_x509_crt_parse_der_nocopy(chain, ssl->in_msg + i, n);
Jerry Yud9526692022-02-17 14:23:47 +08007197#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007198 switch (ret) {
Jerry Yud9526692022-02-17 14:23:47 +08007199 case 0: /*ok*/
7200 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
7201 /* Ignore certificate with an unknown algorithm: maybe a
7202 prior certificate was already trusted. */
7203 break;
7204
7205 case MBEDTLS_ERR_X509_ALLOC_FAILED:
7206 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
7207 goto crt_parse_der_failed;
7208
7209 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
7210 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7211 goto crt_parse_der_failed;
7212
7213 default:
7214 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007215crt_parse_der_failed:
7216 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert);
7217 MBEDTLS_SSL_DEBUG_RET(1, " mbedtls_x509_crt_parse_der", ret);
7218 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007219 }
7220
7221 i += n;
7222 }
7223
Gilles Peskine449bd832023-01-11 14:50:10 +01007224 MBEDTLS_SSL_DEBUG_CRT(3, "peer certificate", chain);
7225 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08007226}
7227
7228#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007229MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007230static int ssl_srv_check_client_no_crt_notification(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08007231{
Gilles Peskine449bd832023-01-11 14:50:10 +01007232 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
7233 return -1;
7234 }
Jerry Yud9526692022-02-17 14:23:47 +08007235
Gilles Peskine449bd832023-01-11 14:50:10 +01007236 if (ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len(ssl) &&
Jerry Yud9526692022-02-17 14:23:47 +08007237 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
7238 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
Gilles Peskine449bd832023-01-11 14:50:10 +01007239 memcmp(ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl), "\0\0\0", 3) == 0) {
7240 MBEDTLS_SSL_DEBUG_MSG(1, ("peer has no certificate"));
7241 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08007242 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007243 return -1;
Jerry Yud9526692022-02-17 14:23:47 +08007244}
7245#endif /* MBEDTLS_SSL_SRV_C */
7246
7247/* Check if a certificate message is expected.
7248 * Return either
7249 * - SSL_CERTIFICATE_EXPECTED, or
7250 * - SSL_CERTIFICATE_SKIP
7251 * indicating whether a Certificate message is expected or not.
7252 */
7253#define SSL_CERTIFICATE_EXPECTED 0
7254#define SSL_CERTIFICATE_SKIP 1
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007255MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007256static int ssl_parse_certificate_coordinate(mbedtls_ssl_context *ssl,
7257 int authmode)
Jerry Yud9526692022-02-17 14:23:47 +08007258{
7259 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
7260 ssl->handshake->ciphersuite_info;
7261
Gilles Peskine449bd832023-01-11 14:50:10 +01007262 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
7263 return SSL_CERTIFICATE_SKIP;
7264 }
Jerry Yud9526692022-02-17 14:23:47 +08007265
7266#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007267 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
7268 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK) {
7269 return SSL_CERTIFICATE_SKIP;
7270 }
Jerry Yud9526692022-02-17 14:23:47 +08007271
Gilles Peskine449bd832023-01-11 14:50:10 +01007272 if (authmode == MBEDTLS_SSL_VERIFY_NONE) {
Jerry Yud9526692022-02-17 14:23:47 +08007273 ssl->session_negotiate->verify_result =
7274 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
Gilles Peskine449bd832023-01-11 14:50:10 +01007275 return SSL_CERTIFICATE_SKIP;
Jerry Yud9526692022-02-17 14:23:47 +08007276 }
7277 }
7278#else
7279 ((void) authmode);
7280#endif /* MBEDTLS_SSL_SRV_C */
7281
Gilles Peskine449bd832023-01-11 14:50:10 +01007282 return SSL_CERTIFICATE_EXPECTED;
Jerry Yud9526692022-02-17 14:23:47 +08007283}
7284
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007285MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007286static int ssl_parse_certificate_verify(mbedtls_ssl_context *ssl,
7287 int authmode,
7288 mbedtls_x509_crt *chain,
7289 void *rs_ctx)
Jerry Yud9526692022-02-17 14:23:47 +08007290{
7291 int ret = 0;
7292 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
7293 ssl->handshake->ciphersuite_info;
7294 int have_ca_chain = 0;
7295
7296 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
7297 void *p_vrfy;
7298
Gilles Peskine449bd832023-01-11 14:50:10 +01007299 if (authmode == MBEDTLS_SSL_VERIFY_NONE) {
7300 return 0;
7301 }
Jerry Yud9526692022-02-17 14:23:47 +08007302
Gilles Peskine449bd832023-01-11 14:50:10 +01007303 if (ssl->f_vrfy != NULL) {
7304 MBEDTLS_SSL_DEBUG_MSG(3, ("Use context-specific verification callback"));
Jerry Yud9526692022-02-17 14:23:47 +08007305 f_vrfy = ssl->f_vrfy;
7306 p_vrfy = ssl->p_vrfy;
Gilles Peskine449bd832023-01-11 14:50:10 +01007307 } else {
7308 MBEDTLS_SSL_DEBUG_MSG(3, ("Use configuration-specific verification callback"));
Jerry Yud9526692022-02-17 14:23:47 +08007309 f_vrfy = ssl->conf->f_vrfy;
7310 p_vrfy = ssl->conf->p_vrfy;
7311 }
7312
7313 /*
7314 * Main check: verify certificate
7315 */
7316#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Gilles Peskine449bd832023-01-11 14:50:10 +01007317 if (ssl->conf->f_ca_cb != NULL) {
Jerry Yud9526692022-02-17 14:23:47 +08007318 ((void) rs_ctx);
7319 have_ca_chain = 1;
7320
Gilles Peskine449bd832023-01-11 14:50:10 +01007321 MBEDTLS_SSL_DEBUG_MSG(3, ("use CA callback for X.509 CRT verification"));
Jerry Yud9526692022-02-17 14:23:47 +08007322 ret = mbedtls_x509_crt_verify_with_ca_cb(
7323 chain,
7324 ssl->conf->f_ca_cb,
7325 ssl->conf->p_ca_cb,
7326 ssl->conf->cert_profile,
7327 ssl->hostname,
7328 &ssl->session_negotiate->verify_result,
Gilles Peskine449bd832023-01-11 14:50:10 +01007329 f_vrfy, p_vrfy);
7330 } else
Jerry Yud9526692022-02-17 14:23:47 +08007331#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
7332 {
7333 mbedtls_x509_crt *ca_chain;
7334 mbedtls_x509_crl *ca_crl;
7335
7336#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01007337 if (ssl->handshake->sni_ca_chain != NULL) {
Jerry Yud9526692022-02-17 14:23:47 +08007338 ca_chain = ssl->handshake->sni_ca_chain;
7339 ca_crl = ssl->handshake->sni_ca_crl;
Gilles Peskine449bd832023-01-11 14:50:10 +01007340 } else
Jerry Yud9526692022-02-17 14:23:47 +08007341#endif
7342 {
7343 ca_chain = ssl->conf->ca_chain;
7344 ca_crl = ssl->conf->ca_crl;
7345 }
7346
Gilles Peskine449bd832023-01-11 14:50:10 +01007347 if (ca_chain != NULL) {
Jerry Yud9526692022-02-17 14:23:47 +08007348 have_ca_chain = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01007349 }
Jerry Yud9526692022-02-17 14:23:47 +08007350
7351 ret = mbedtls_x509_crt_verify_restartable(
7352 chain,
7353 ca_chain, ca_crl,
7354 ssl->conf->cert_profile,
7355 ssl->hostname,
7356 &ssl->session_negotiate->verify_result,
Gilles Peskine449bd832023-01-11 14:50:10 +01007357 f_vrfy, p_vrfy, rs_ctx);
Jerry Yud9526692022-02-17 14:23:47 +08007358 }
7359
Gilles Peskine449bd832023-01-11 14:50:10 +01007360 if (ret != 0) {
7361 MBEDTLS_SSL_DEBUG_RET(1, "x509_verify_cert", ret);
Jerry Yud9526692022-02-17 14:23:47 +08007362 }
7363
7364#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007365 if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
7366 return MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
7367 }
Jerry Yud9526692022-02-17 14:23:47 +08007368#endif
7369
7370 /*
7371 * Secondary checks: always done, but change 'ret' only if it was 0
7372 */
7373
7374#if defined(MBEDTLS_ECP_C)
7375 {
7376 const mbedtls_pk_context *pk = &chain->pk;
7377
Manuel Pégourié-Gonnard66b0d612022-06-17 10:49:29 +02007378 /* If certificate uses an EC key, make sure the curve is OK.
7379 * This is a public key, so it can't be opaque, so can_do() is a good
7380 * enough check to ensure pk_ec() is safe to use here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007381 if (mbedtls_pk_can_do(pk, MBEDTLS_PK_ECKEY)) {
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007382 /* and in the unlikely case the above assumption no longer holds
7383 * we are making sure that pk_ec() here does not return a NULL
7384 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007385 const mbedtls_ecp_keypair *ec = mbedtls_pk_ec(*pk);
7386 if (ec == NULL) {
7387 MBEDTLS_SSL_DEBUG_MSG(1, ("mbedtls_pk_ec() returned NULL"));
7388 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007389 }
Jerry Yud9526692022-02-17 14:23:47 +08007390
Gilles Peskine449bd832023-01-11 14:50:10 +01007391 if (mbedtls_ssl_check_curve(ssl, ec->grp.id) != 0) {
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007392 ssl->session_negotiate->verify_result |=
7393 MBEDTLS_X509_BADCERT_BAD_KEY;
7394
Gilles Peskine449bd832023-01-11 14:50:10 +01007395 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate (EC key curve)"));
7396 if (ret == 0) {
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007397 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Gilles Peskine449bd832023-01-11 14:50:10 +01007398 }
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007399 }
Jerry Yud9526692022-02-17 14:23:47 +08007400 }
7401 }
7402#endif /* MBEDTLS_ECP_C */
7403
Gilles Peskine449bd832023-01-11 14:50:10 +01007404 if (mbedtls_ssl_check_cert_usage(chain,
7405 ciphersuite_info,
7406 !ssl->conf->endpoint,
7407 &ssl->session_negotiate->verify_result) != 0) {
7408 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate (usage extensions)"));
7409 if (ret == 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007410 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Gilles Peskine449bd832023-01-11 14:50:10 +01007411 }
Jerry Yud9526692022-02-17 14:23:47 +08007412 }
7413
7414 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
7415 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
7416 * with details encoded in the verification flags. All other kinds
7417 * of error codes, including those from the user provided f_vrfy
7418 * functions, are treated as fatal and lead to a failure of
7419 * ssl_parse_certificate even if verification was optional. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007420 if (authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
7421 (ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
7422 ret == MBEDTLS_ERR_SSL_BAD_CERTIFICATE)) {
Jerry Yud9526692022-02-17 14:23:47 +08007423 ret = 0;
7424 }
7425
Gilles Peskine449bd832023-01-11 14:50:10 +01007426 if (have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED) {
7427 MBEDTLS_SSL_DEBUG_MSG(1, ("got no CA chain"));
Jerry Yud9526692022-02-17 14:23:47 +08007428 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
7429 }
7430
Gilles Peskine449bd832023-01-11 14:50:10 +01007431 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007432 uint8_t alert;
7433
7434 /* The certificate may have been rejected for several reasons.
7435 Pick one and send the corresponding alert. Which alert to send
7436 may be a subject of debate in some cases. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007437 if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER) {
Jerry Yud9526692022-02-17 14:23:47 +08007438 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
Gilles Peskine449bd832023-01-11 14:50:10 +01007439 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH) {
Jerry Yud9526692022-02-17 14:23:47 +08007440 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007441 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE) {
Jerry Yud9526692022-02-17 14:23:47 +08007442 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007443 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE) {
Jerry Yud9526692022-02-17 14:23:47 +08007444 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007445 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE) {
Jerry Yud9526692022-02-17 14:23:47 +08007446 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007447 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK) {
Jerry Yud9526692022-02-17 14:23:47 +08007448 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007449 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY) {
Jerry Yud9526692022-02-17 14:23:47 +08007450 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007451 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED) {
Jerry Yud9526692022-02-17 14:23:47 +08007452 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
Gilles Peskine449bd832023-01-11 14:50:10 +01007453 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED) {
Jerry Yud9526692022-02-17 14:23:47 +08007454 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
Gilles Peskine449bd832023-01-11 14:50:10 +01007455 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED) {
Jerry Yud9526692022-02-17 14:23:47 +08007456 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
Gilles Peskine449bd832023-01-11 14:50:10 +01007457 } else {
Jerry Yud9526692022-02-17 14:23:47 +08007458 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
Gilles Peskine449bd832023-01-11 14:50:10 +01007459 }
7460 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7461 alert);
Jerry Yud9526692022-02-17 14:23:47 +08007462 }
7463
7464#if defined(MBEDTLS_DEBUG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007465 if (ssl->session_negotiate->verify_result != 0) {
7466 MBEDTLS_SSL_DEBUG_MSG(3, ("! Certificate verification flags %08x",
7467 (unsigned int) ssl->session_negotiate->verify_result));
7468 } else {
7469 MBEDTLS_SSL_DEBUG_MSG(3, ("Certificate verification flags clear"));
Jerry Yud9526692022-02-17 14:23:47 +08007470 }
7471#endif /* MBEDTLS_DEBUG_C */
7472
Gilles Peskine449bd832023-01-11 14:50:10 +01007473 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007474}
7475
7476#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007477MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007478static int ssl_remember_peer_crt_digest(mbedtls_ssl_context *ssl,
7479 unsigned char *start, size_t len)
Jerry Yud9526692022-02-17 14:23:47 +08007480{
7481 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7482 /* Remember digest of the peer's end-CRT. */
7483 ssl->session_negotiate->peer_cert_digest =
Gilles Peskine449bd832023-01-11 14:50:10 +01007484 mbedtls_calloc(1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN);
7485 if (ssl->session_negotiate->peer_cert_digest == NULL) {
7486 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%d bytes) failed",
7487 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN));
7488 mbedtls_ssl_send_alert_message(ssl,
7489 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7490 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
Jerry Yud9526692022-02-17 14:23:47 +08007491
Gilles Peskine449bd832023-01-11 14:50:10 +01007492 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Jerry Yud9526692022-02-17 14:23:47 +08007493 }
7494
Gilles Peskine449bd832023-01-11 14:50:10 +01007495 ret = mbedtls_md(mbedtls_md_info_from_type(
7496 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE),
7497 start, len,
7498 ssl->session_negotiate->peer_cert_digest);
Jerry Yud9526692022-02-17 14:23:47 +08007499
7500 ssl->session_negotiate->peer_cert_digest_type =
7501 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
7502 ssl->session_negotiate->peer_cert_digest_len =
7503 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
7504
Gilles Peskine449bd832023-01-11 14:50:10 +01007505 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007506}
7507
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007508MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007509static int ssl_remember_peer_pubkey(mbedtls_ssl_context *ssl,
7510 unsigned char *start, size_t len)
Jerry Yud9526692022-02-17 14:23:47 +08007511{
7512 unsigned char *end = start + len;
7513 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7514
7515 /* Make a copy of the peer's raw public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007516 mbedtls_pk_init(&ssl->handshake->peer_pubkey);
7517 ret = mbedtls_pk_parse_subpubkey(&start, end,
7518 &ssl->handshake->peer_pubkey);
7519 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007520 /* We should have parsed the public key before. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007521 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007522 }
7523
Gilles Peskine449bd832023-01-11 14:50:10 +01007524 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08007525}
7526#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7527
Gilles Peskine449bd832023-01-11 14:50:10 +01007528int mbedtls_ssl_parse_certificate(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08007529{
7530 int ret = 0;
7531 int crt_expected;
7532#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7533 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
7534 ? ssl->handshake->sni_authmode
7535 : ssl->conf->authmode;
7536#else
7537 const int authmode = ssl->conf->authmode;
7538#endif
7539 void *rs_ctx = NULL;
7540 mbedtls_x509_crt *chain = NULL;
7541
Gilles Peskine449bd832023-01-11 14:50:10 +01007542 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007543
Gilles Peskine449bd832023-01-11 14:50:10 +01007544 crt_expected = ssl_parse_certificate_coordinate(ssl, authmode);
7545 if (crt_expected == SSL_CERTIFICATE_SKIP) {
7546 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007547 goto exit;
7548 }
7549
7550#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007551 if (ssl->handshake->ecrs_enabled &&
7552 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify) {
Jerry Yud9526692022-02-17 14:23:47 +08007553 chain = ssl->handshake->ecrs_peer_cert;
7554 ssl->handshake->ecrs_peer_cert = NULL;
7555 goto crt_verify;
7556 }
7557#endif
7558
Gilles Peskine449bd832023-01-11 14:50:10 +01007559 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007560 /* mbedtls_ssl_read_record may have sent an alert already. We
7561 let it decide whether to alert. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007562 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
Jerry Yud9526692022-02-17 14:23:47 +08007563 goto exit;
7564 }
7565
7566#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007567 if (ssl_srv_check_client_no_crt_notification(ssl) == 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007568 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
7569
Gilles Peskine449bd832023-01-11 14:50:10 +01007570 if (authmode != MBEDTLS_SSL_VERIFY_OPTIONAL) {
Jerry Yud9526692022-02-17 14:23:47 +08007571 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Gilles Peskine449bd832023-01-11 14:50:10 +01007572 }
Jerry Yud9526692022-02-17 14:23:47 +08007573
7574 goto exit;
7575 }
7576#endif /* MBEDTLS_SSL_SRV_C */
7577
7578 /* Clear existing peer CRT structure in case we tried to
7579 * reuse a session but it failed, and allocate a new one. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007580 ssl_clear_peer_cert(ssl->session_negotiate);
Jerry Yud9526692022-02-17 14:23:47 +08007581
Gilles Peskine449bd832023-01-11 14:50:10 +01007582 chain = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
7583 if (chain == NULL) {
7584 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed",
7585 sizeof(mbedtls_x509_crt)));
7586 mbedtls_ssl_send_alert_message(ssl,
7587 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7588 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
Jerry Yud9526692022-02-17 14:23:47 +08007589
7590 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
7591 goto exit;
7592 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007593 mbedtls_x509_crt_init(chain);
Jerry Yud9526692022-02-17 14:23:47 +08007594
Gilles Peskine449bd832023-01-11 14:50:10 +01007595 ret = ssl_parse_certificate_chain(ssl, chain);
7596 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007597 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007598 }
Jerry Yud9526692022-02-17 14:23:47 +08007599
7600#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007601 if (ssl->handshake->ecrs_enabled) {
Jerry Yud9526692022-02-17 14:23:47 +08007602 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Gilles Peskine449bd832023-01-11 14:50:10 +01007603 }
Jerry Yud9526692022-02-17 14:23:47 +08007604
7605crt_verify:
Gilles Peskine449bd832023-01-11 14:50:10 +01007606 if (ssl->handshake->ecrs_enabled) {
Jerry Yud9526692022-02-17 14:23:47 +08007607 rs_ctx = &ssl->handshake->ecrs_ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +01007608 }
Jerry Yud9526692022-02-17 14:23:47 +08007609#endif
7610
Gilles Peskine449bd832023-01-11 14:50:10 +01007611 ret = ssl_parse_certificate_verify(ssl, authmode,
7612 chain, rs_ctx);
7613 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007614 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007615 }
Jerry Yud9526692022-02-17 14:23:47 +08007616
7617#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7618 {
7619 unsigned char *crt_start, *pk_start;
7620 size_t crt_len, pk_len;
7621
7622 /* We parse the CRT chain without copying, so
7623 * these pointers point into the input buffer,
7624 * and are hence still valid after freeing the
7625 * CRT chain. */
7626
7627 crt_start = chain->raw.p;
7628 crt_len = chain->raw.len;
7629
7630 pk_start = chain->pk_raw.p;
7631 pk_len = chain->pk_raw.len;
7632
7633 /* Free the CRT structures before computing
7634 * digest and copying the peer's public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007635 mbedtls_x509_crt_free(chain);
7636 mbedtls_free(chain);
Jerry Yud9526692022-02-17 14:23:47 +08007637 chain = NULL;
7638
Gilles Peskine449bd832023-01-11 14:50:10 +01007639 ret = ssl_remember_peer_crt_digest(ssl, crt_start, crt_len);
7640 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007641 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007642 }
Jerry Yud9526692022-02-17 14:23:47 +08007643
Gilles Peskine449bd832023-01-11 14:50:10 +01007644 ret = ssl_remember_peer_pubkey(ssl, pk_start, pk_len);
7645 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007646 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007647 }
Jerry Yud9526692022-02-17 14:23:47 +08007648 }
7649#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7650 /* Pass ownership to session structure. */
7651 ssl->session_negotiate->peer_cert = chain;
7652 chain = NULL;
7653#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7654
Gilles Peskine449bd832023-01-11 14:50:10 +01007655 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007656
7657exit:
7658
Gilles Peskine449bd832023-01-11 14:50:10 +01007659 if (ret == 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007660 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01007661 }
Jerry Yud9526692022-02-17 14:23:47 +08007662
7663#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007664 if (ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS) {
Jerry Yud9526692022-02-17 14:23:47 +08007665 ssl->handshake->ecrs_peer_cert = chain;
7666 chain = NULL;
7667 }
7668#endif
7669
Gilles Peskine449bd832023-01-11 14:50:10 +01007670 if (chain != NULL) {
7671 mbedtls_x509_crt_free(chain);
7672 mbedtls_free(chain);
Jerry Yud9526692022-02-17 14:23:47 +08007673 }
7674
Gilles Peskine449bd832023-01-11 14:50:10 +01007675 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007676}
7677#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
7678
Andrzej Kurek25f27152022-08-17 16:09:31 -04007679#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01007680static int ssl_calc_finished_tls_sha256(
Gilles Peskine449bd832023-01-11 14:50:10 +01007681 mbedtls_ssl_context *ssl, unsigned char *buf, int from)
Jerry Yu615bd6f2022-02-17 14:25:15 +08007682{
7683 int len = 12;
7684 const char *sender;
7685 unsigned char padbuf[32];
7686#if defined(MBEDTLS_USE_PSA_CRYPTO)
7687 size_t hash_size;
7688 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
7689 psa_status_t status;
7690#else
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007691 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01007692 mbedtls_md_context_t sha256;
Jerry Yu615bd6f2022-02-17 14:25:15 +08007693#endif
7694
7695 mbedtls_ssl_session *session = ssl->session_negotiate;
Gilles Peskine449bd832023-01-11 14:50:10 +01007696 if (!session) {
Jerry Yu615bd6f2022-02-17 14:25:15 +08007697 session = ssl->session;
Gilles Peskine449bd832023-01-11 14:50:10 +01007698 }
Jerry Yu615bd6f2022-02-17 14:25:15 +08007699
Gilles Peskine449bd832023-01-11 14:50:10 +01007700 sender = (from == MBEDTLS_SSL_IS_CLIENT)
Jerry Yu615bd6f2022-02-17 14:25:15 +08007701 ? "client finished"
7702 : "server finished";
7703
7704#if defined(MBEDTLS_USE_PSA_CRYPTO)
7705 sha256_psa = psa_hash_operation_init();
7706
Gilles Peskine449bd832023-01-11 14:50:10 +01007707 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc PSA finished tls sha256"));
Jerry Yu615bd6f2022-02-17 14:25:15 +08007708
Gilles Peskine449bd832023-01-11 14:50:10 +01007709 status = psa_hash_clone(&ssl->handshake->fin_sha256_psa, &sha256_psa);
7710 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007711 goto exit;
Jerry Yu615bd6f2022-02-17 14:25:15 +08007712 }
7713
Gilles Peskine449bd832023-01-11 14:50:10 +01007714 status = psa_hash_finish(&sha256_psa, padbuf, sizeof(padbuf), &hash_size);
7715 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007716 goto exit;
Jerry Yu615bd6f2022-02-17 14:25:15 +08007717 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007718 MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated padbuf", padbuf, 32);
Jerry Yu615bd6f2022-02-17 14:25:15 +08007719#else
7720
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01007721 mbedtls_md_init(&sha256);
Jerry Yu615bd6f2022-02-17 14:25:15 +08007722
Gilles Peskine449bd832023-01-11 14:50:10 +01007723 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc finished tls sha256"));
Jerry Yu615bd6f2022-02-17 14:25:15 +08007724
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01007725 ret = mbedtls_md_setup(&sha256, mbedtls_md_info_from_type(MBEDTLS_MD_SHA256), 0);
7726 if (ret != 0) {
7727 goto exit;
7728 }
7729 ret = mbedtls_md_clone(&sha256, &ssl->handshake->fin_sha256);
7730 if (ret != 0) {
7731 goto exit;
7732 }
Jerry Yu615bd6f2022-02-17 14:25:15 +08007733
7734 /*
7735 * TLSv1.2:
7736 * hash = PRF( master, finished_label,
7737 * Hash( handshake ) )[0.11]
7738 */
7739
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01007740 ret = mbedtls_md_finish(&sha256, padbuf);
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007741 if (ret != 0) {
7742 goto exit;
7743 }
Jerry Yu615bd6f2022-02-17 14:25:15 +08007744#endif /* MBEDTLS_USE_PSA_CRYPTO */
7745
Manuel Pégourié-Gonnard0ac71c02023-02-24 12:13:55 +01007746 MBEDTLS_SSL_DEBUG_BUF(4, "finished sha256 output", padbuf, 32);
7747
Gilles Peskine449bd832023-01-11 14:50:10 +01007748 ssl->handshake->tls_prf(session->master, 48, sender,
7749 padbuf, 32, buf, len);
Jerry Yu615bd6f2022-02-17 14:25:15 +08007750
Gilles Peskine449bd832023-01-11 14:50:10 +01007751 MBEDTLS_SSL_DEBUG_BUF(3, "calc finished result", buf, len);
Jerry Yu615bd6f2022-02-17 14:25:15 +08007752
Gilles Peskine449bd832023-01-11 14:50:10 +01007753 mbedtls_platform_zeroize(padbuf, sizeof(padbuf));
Jerry Yu615bd6f2022-02-17 14:25:15 +08007754
Gilles Peskine449bd832023-01-11 14:50:10 +01007755 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc finished"));
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007756
7757exit:
7758#if defined(MBEDTLS_USE_PSA_CRYPTO)
7759 psa_hash_abort(&sha256_psa);
Andrzej Kurekdaf5b562023-03-03 05:52:28 -05007760 return PSA_TO_MD_ERR(status);
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007761#else
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01007762 mbedtls_md_free(&sha256);
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007763 return ret;
7764#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu615bd6f2022-02-17 14:25:15 +08007765}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04007766#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yu615bd6f2022-02-17 14:25:15 +08007767
Jerry Yub7ba49e2022-02-17 14:25:53 +08007768
Andrzej Kurek25f27152022-08-17 16:09:31 -04007769#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01007770static int ssl_calc_finished_tls_sha384(
Gilles Peskine449bd832023-01-11 14:50:10 +01007771 mbedtls_ssl_context *ssl, unsigned char *buf, int from)
Jerry Yub7ba49e2022-02-17 14:25:53 +08007772{
7773 int len = 12;
7774 const char *sender;
7775 unsigned char padbuf[48];
7776#if defined(MBEDTLS_USE_PSA_CRYPTO)
7777 size_t hash_size;
7778 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
7779 psa_status_t status;
7780#else
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007781 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01007782 mbedtls_md_context_t sha384;
Jerry Yub7ba49e2022-02-17 14:25:53 +08007783#endif
7784
7785 mbedtls_ssl_session *session = ssl->session_negotiate;
Gilles Peskine449bd832023-01-11 14:50:10 +01007786 if (!session) {
Jerry Yub7ba49e2022-02-17 14:25:53 +08007787 session = ssl->session;
Gilles Peskine449bd832023-01-11 14:50:10 +01007788 }
Jerry Yub7ba49e2022-02-17 14:25:53 +08007789
Gilles Peskine449bd832023-01-11 14:50:10 +01007790 sender = (from == MBEDTLS_SSL_IS_CLIENT)
Jerry Yub7ba49e2022-02-17 14:25:53 +08007791 ? "client finished"
7792 : "server finished";
7793
7794#if defined(MBEDTLS_USE_PSA_CRYPTO)
7795 sha384_psa = psa_hash_operation_init();
7796
Gilles Peskine449bd832023-01-11 14:50:10 +01007797 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc PSA finished tls sha384"));
Jerry Yub7ba49e2022-02-17 14:25:53 +08007798
Gilles Peskine449bd832023-01-11 14:50:10 +01007799 status = psa_hash_clone(&ssl->handshake->fin_sha384_psa, &sha384_psa);
7800 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007801 goto exit;
Jerry Yub7ba49e2022-02-17 14:25:53 +08007802 }
7803
Gilles Peskine449bd832023-01-11 14:50:10 +01007804 status = psa_hash_finish(&sha384_psa, padbuf, sizeof(padbuf), &hash_size);
7805 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007806 goto exit;
Jerry Yub7ba49e2022-02-17 14:25:53 +08007807 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007808 MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated padbuf", padbuf, 48);
Jerry Yub7ba49e2022-02-17 14:25:53 +08007809#else
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01007810 mbedtls_md_init(&sha384);
Jerry Yub7ba49e2022-02-17 14:25:53 +08007811
Gilles Peskine449bd832023-01-11 14:50:10 +01007812 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc finished tls sha384"));
Jerry Yub7ba49e2022-02-17 14:25:53 +08007813
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01007814 ret = mbedtls_md_setup(&sha384, mbedtls_md_info_from_type(MBEDTLS_MD_SHA384), 0);
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01007815 if (ret != 0) {
7816 goto exit;
7817 }
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01007818 ret = mbedtls_md_clone(&sha384, &ssl->handshake->fin_sha384);
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01007819 if (ret != 0) {
7820 goto exit;
7821 }
Jerry Yub7ba49e2022-02-17 14:25:53 +08007822
7823 /*
7824 * TLSv1.2:
7825 * hash = PRF( master, finished_label,
7826 * Hash( handshake ) )[0.11]
7827 */
7828
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01007829 ret = mbedtls_md_finish(&sha384, padbuf);
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007830 if (ret != 0) {
7831 goto exit;
7832 }
Jerry Yub7ba49e2022-02-17 14:25:53 +08007833#endif
7834
Manuel Pégourié-Gonnard0ac71c02023-02-24 12:13:55 +01007835 MBEDTLS_SSL_DEBUG_BUF(4, "finished sha384 output", padbuf, 48);
7836
Gilles Peskine449bd832023-01-11 14:50:10 +01007837 ssl->handshake->tls_prf(session->master, 48, sender,
7838 padbuf, 48, buf, len);
Jerry Yub7ba49e2022-02-17 14:25:53 +08007839
Gilles Peskine449bd832023-01-11 14:50:10 +01007840 MBEDTLS_SSL_DEBUG_BUF(3, "calc finished result", buf, len);
Jerry Yub7ba49e2022-02-17 14:25:53 +08007841
Gilles Peskine449bd832023-01-11 14:50:10 +01007842 mbedtls_platform_zeroize(padbuf, sizeof(padbuf));
Jerry Yub7ba49e2022-02-17 14:25:53 +08007843
Gilles Peskine449bd832023-01-11 14:50:10 +01007844 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc finished"));
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007845
7846exit:
7847#if defined(MBEDTLS_USE_PSA_CRYPTO)
7848 psa_hash_abort(&sha384_psa);
Andrzej Kurekdaf5b562023-03-03 05:52:28 -05007849 return PSA_TO_MD_ERR(status);
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007850#else
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01007851 mbedtls_md_free(&sha384);
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007852 return ret;
7853#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yub7ba49e2022-02-17 14:25:53 +08007854}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04007855#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yub7ba49e2022-02-17 14:25:53 +08007856
Gilles Peskine449bd832023-01-11 14:50:10 +01007857void mbedtls_ssl_handshake_wrapup_free_hs_transform(mbedtls_ssl_context *ssl)
Jerry Yuaef00152022-02-17 14:27:31 +08007858{
Gilles Peskine449bd832023-01-11 14:50:10 +01007859 MBEDTLS_SSL_DEBUG_MSG(3, ("=> handshake wrapup: final free"));
Jerry Yuaef00152022-02-17 14:27:31 +08007860
7861 /*
7862 * Free our handshake params
7863 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007864 mbedtls_ssl_handshake_free(ssl);
7865 mbedtls_free(ssl->handshake);
Jerry Yuaef00152022-02-17 14:27:31 +08007866 ssl->handshake = NULL;
7867
7868 /*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08007869 * Free the previous transform and switch in the current one
Jerry Yuaef00152022-02-17 14:27:31 +08007870 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007871 if (ssl->transform) {
7872 mbedtls_ssl_transform_free(ssl->transform);
7873 mbedtls_free(ssl->transform);
Jerry Yuaef00152022-02-17 14:27:31 +08007874 }
7875 ssl->transform = ssl->transform_negotiate;
7876 ssl->transform_negotiate = NULL;
7877
Gilles Peskine449bd832023-01-11 14:50:10 +01007878 MBEDTLS_SSL_DEBUG_MSG(3, ("<= handshake wrapup: final free"));
Jerry Yuaef00152022-02-17 14:27:31 +08007879}
7880
Gilles Peskine449bd832023-01-11 14:50:10 +01007881void mbedtls_ssl_handshake_wrapup(mbedtls_ssl_context *ssl)
Jerry Yu2a9fff52022-02-17 14:28:51 +08007882{
7883 int resume = ssl->handshake->resume;
7884
Gilles Peskine449bd832023-01-11 14:50:10 +01007885 MBEDTLS_SSL_DEBUG_MSG(3, ("=> handshake wrapup"));
Jerry Yu2a9fff52022-02-17 14:28:51 +08007886
7887#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01007888 if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
Jerry Yu2a9fff52022-02-17 14:28:51 +08007889 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
7890 ssl->renego_records_seen = 0;
7891 }
7892#endif
7893
7894 /*
7895 * Free the previous session and switch in the current one
7896 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007897 if (ssl->session) {
Jerry Yu2a9fff52022-02-17 14:28:51 +08007898#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
7899 /* RFC 7366 3.1: keep the EtM state */
7900 ssl->session_negotiate->encrypt_then_mac =
Gilles Peskine449bd832023-01-11 14:50:10 +01007901 ssl->session->encrypt_then_mac;
Jerry Yu2a9fff52022-02-17 14:28:51 +08007902#endif
7903
Gilles Peskine449bd832023-01-11 14:50:10 +01007904 mbedtls_ssl_session_free(ssl->session);
7905 mbedtls_free(ssl->session);
Jerry Yu2a9fff52022-02-17 14:28:51 +08007906 }
7907 ssl->session = ssl->session_negotiate;
7908 ssl->session_negotiate = NULL;
7909
7910 /*
7911 * Add cache entry
7912 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007913 if (ssl->conf->f_set_cache != NULL &&
Jerry Yu2a9fff52022-02-17 14:28:51 +08007914 ssl->session->id_len != 0 &&
Gilles Peskine449bd832023-01-11 14:50:10 +01007915 resume == 0) {
7916 if (ssl->conf->f_set_cache(ssl->conf->p_cache,
7917 ssl->session->id,
7918 ssl->session->id_len,
7919 ssl->session) != 0) {
7920 MBEDTLS_SSL_DEBUG_MSG(1, ("cache did not store session"));
7921 }
Jerry Yu2a9fff52022-02-17 14:28:51 +08007922 }
7923
7924#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007925 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7926 ssl->handshake->flight != NULL) {
Jerry Yu2a9fff52022-02-17 14:28:51 +08007927 /* Cancel handshake timer */
Gilles Peskine449bd832023-01-11 14:50:10 +01007928 mbedtls_ssl_set_timer(ssl, 0);
Jerry Yu2a9fff52022-02-17 14:28:51 +08007929
7930 /* Keep last flight around in case we need to resend it:
7931 * we need the handshake and transform structures for that */
Gilles Peskine449bd832023-01-11 14:50:10 +01007932 MBEDTLS_SSL_DEBUG_MSG(3, ("skip freeing handshake and transform"));
7933 } else
Jerry Yu2a9fff52022-02-17 14:28:51 +08007934#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01007935 mbedtls_ssl_handshake_wrapup_free_hs_transform(ssl);
Jerry Yu2a9fff52022-02-17 14:28:51 +08007936
Jerry Yu5ed73ff2022-10-27 13:08:42 +08007937 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
Jerry Yu2a9fff52022-02-17 14:28:51 +08007938
Gilles Peskine449bd832023-01-11 14:50:10 +01007939 MBEDTLS_SSL_DEBUG_MSG(3, ("<= handshake wrapup"));
Jerry Yu2a9fff52022-02-17 14:28:51 +08007940}
7941
Gilles Peskine449bd832023-01-11 14:50:10 +01007942int mbedtls_ssl_write_finished(mbedtls_ssl_context *ssl)
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007943{
7944 int ret, hash_len;
7945
Gilles Peskine449bd832023-01-11 14:50:10 +01007946 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write finished"));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007947
Gilles Peskine449bd832023-01-11 14:50:10 +01007948 mbedtls_ssl_update_out_pointers(ssl, ssl->transform_negotiate);
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007949
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01007950 ret = ssl->handshake->calc_finished(ssl, ssl->out_msg + 4, ssl->conf->endpoint);
7951 if (ret != 0) {
7952 MBEDTLS_SSL_DEBUG_RET(1, "calc_finished", ret);
7953 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007954
7955 /*
7956 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7957 * may define some other value. Currently (early 2016), no defined
7958 * ciphersuite does this (and this is unlikely to change as activity has
7959 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7960 */
7961 hash_len = 12;
7962
7963#if defined(MBEDTLS_SSL_RENEGOTIATION)
7964 ssl->verify_data_len = hash_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01007965 memcpy(ssl->own_verify_data, ssl->out_msg + 4, hash_len);
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007966#endif
7967
7968 ssl->out_msglen = 4 + hash_len;
7969 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7970 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
7971
7972 /*
7973 * In case of session resuming, invert the client and server
7974 * ChangeCipherSpec messages order.
7975 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007976 if (ssl->handshake->resume != 0) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007977#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007978 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007979 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Gilles Peskine449bd832023-01-11 14:50:10 +01007980 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007981#endif
7982#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007983 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007984 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Gilles Peskine449bd832023-01-11 14:50:10 +01007985 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007986#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01007987 } else {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007988 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01007989 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007990
7991 /*
7992 * Switch to our negotiated transform and session parameters for outbound
7993 * data.
7994 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007995 MBEDTLS_SSL_DEBUG_MSG(3, ("switching to new transform spec for outbound data"));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007996
7997#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007998 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007999 unsigned char i;
8000
8001 /* Remember current epoch settings for resending */
8002 ssl->handshake->alt_transform_out = ssl->transform_out;
Gilles Peskine449bd832023-01-11 14:50:10 +01008003 memcpy(ssl->handshake->alt_out_ctr, ssl->cur_out_ctr,
8004 sizeof(ssl->handshake->alt_out_ctr));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008005
8006 /* Set sequence_number to zero */
Gilles Peskine449bd832023-01-11 14:50:10 +01008007 memset(&ssl->cur_out_ctr[2], 0, sizeof(ssl->cur_out_ctr) - 2);
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008008
8009
8010 /* Increment epoch */
Gilles Peskine449bd832023-01-11 14:50:10 +01008011 for (i = 2; i > 0; i--) {
8012 if (++ssl->cur_out_ctr[i - 1] != 0) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008013 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01008014 }
8015 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008016
8017 /* The loop goes to its end iff the counter is wrapping */
Gilles Peskine449bd832023-01-11 14:50:10 +01008018 if (i == 0) {
8019 MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS epoch would wrap"));
8020 return MBEDTLS_ERR_SSL_COUNTER_WRAPPING;
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008021 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008022 } else
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008023#endif /* MBEDTLS_SSL_PROTO_DTLS */
Gilles Peskine449bd832023-01-11 14:50:10 +01008024 memset(ssl->cur_out_ctr, 0, sizeof(ssl->cur_out_ctr));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008025
8026 ssl->transform_out = ssl->transform_negotiate;
8027 ssl->session_out = ssl->session_negotiate;
8028
8029#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01008030 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
8031 mbedtls_ssl_send_flight_completed(ssl);
8032 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008033#endif
8034
Gilles Peskine449bd832023-01-11 14:50:10 +01008035 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
8036 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
8037 return ret;
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008038 }
8039
8040#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01008041 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
8042 (ret = mbedtls_ssl_flight_transmit(ssl)) != 0) {
8043 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_flight_transmit", ret);
8044 return ret;
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008045 }
8046#endif
8047
Gilles Peskine449bd832023-01-11 14:50:10 +01008048 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write finished"));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008049
Gilles Peskine449bd832023-01-11 14:50:10 +01008050 return 0;
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008051}
8052
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008053#define SSL_MAX_HASH_LEN 12
8054
Gilles Peskine449bd832023-01-11 14:50:10 +01008055int mbedtls_ssl_parse_finished(mbedtls_ssl_context *ssl)
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008056{
8057 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
8058 unsigned int hash_len = 12;
8059 unsigned char buf[SSL_MAX_HASH_LEN];
8060
Gilles Peskine449bd832023-01-11 14:50:10 +01008061 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse finished"));
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008062
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01008063 ret = ssl->handshake->calc_finished(ssl, buf, ssl->conf->endpoint ^ 1);
8064 if (ret != 0) {
8065 MBEDTLS_SSL_DEBUG_RET(1, "calc_finished", ret);
8066 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008067
Gilles Peskine449bd832023-01-11 14:50:10 +01008068 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
8069 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008070 goto exit;
8071 }
8072
Gilles Peskine449bd832023-01-11 14:50:10 +01008073 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
8074 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
8075 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8076 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008077 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
8078 goto exit;
8079 }
8080
Gilles Peskine449bd832023-01-11 14:50:10 +01008081 if (ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED) {
8082 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8083 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008084 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
8085 goto exit;
8086 }
8087
Gilles Peskine449bd832023-01-11 14:50:10 +01008088 if (ssl->in_hslen != mbedtls_ssl_hs_hdr_len(ssl) + hash_len) {
8089 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
8090 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8091 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008092 ret = MBEDTLS_ERR_SSL_DECODE_ERROR;
8093 goto exit;
8094 }
8095
Gilles Peskine449bd832023-01-11 14:50:10 +01008096 if (mbedtls_ct_memcmp(ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl),
8097 buf, hash_len) != 0) {
8098 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
8099 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8100 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008101 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
8102 goto exit;
8103 }
8104
8105#if defined(MBEDTLS_SSL_RENEGOTIATION)
8106 ssl->verify_data_len = hash_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008107 memcpy(ssl->peer_verify_data, buf, hash_len);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008108#endif
8109
Gilles Peskine449bd832023-01-11 14:50:10 +01008110 if (ssl->handshake->resume != 0) {
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008111#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008112 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008113 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Gilles Peskine449bd832023-01-11 14:50:10 +01008114 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008115#endif
8116#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008117 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008118 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Gilles Peskine449bd832023-01-11 14:50:10 +01008119 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008120#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01008121 } else {
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008122 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01008123 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008124
8125#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01008126 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
8127 mbedtls_ssl_recv_flight_completed(ssl);
8128 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008129#endif
8130
Gilles Peskine449bd832023-01-11 14:50:10 +01008131 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse finished"));
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008132
8133exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008134 mbedtls_platform_zeroize(buf, hash_len);
8135 return ret;
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008136}
8137
Jerry Yu392112c2022-02-17 14:34:10 +08008138#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
8139/*
8140 * Helper to get TLS 1.2 PRF from ciphersuite
8141 * (Duplicates bits of logic from ssl_set_handshake_prfs().)
8142 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008143static tls_prf_fn ssl_tls12prf_from_cs(int ciphersuite_id)
Jerry Yu392112c2022-02-17 14:34:10 +08008144{
Jerry Yu392112c2022-02-17 14:34:10 +08008145 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01008146 mbedtls_ssl_ciphersuite_from_id(ciphersuite_id);
Andrzej Kurek68327742022-10-03 06:18:18 -04008147#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01008148 if (ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA384) {
8149 return tls_prf_sha384;
8150 } else
Jerry Yu392112c2022-02-17 14:34:10 +08008151#endif
Andrzej Kurek894edde2022-09-29 06:31:14 -04008152#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
8153 {
Gilles Peskine449bd832023-01-11 14:50:10 +01008154 if (ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA256) {
8155 return tls_prf_sha256;
8156 }
Andrzej Kurek894edde2022-09-29 06:31:14 -04008157 }
8158#endif
8159#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
8160 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
8161 (void) ciphersuite_info;
8162#endif
Andrzej Kurekeabeb302022-10-17 07:52:51 -04008163
Gilles Peskine449bd832023-01-11 14:50:10 +01008164 return NULL;
Jerry Yu392112c2022-02-17 14:34:10 +08008165}
8166#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Jerry Yue93ffcd2022-02-17 14:37:06 +08008167
Gilles Peskine449bd832023-01-11 14:50:10 +01008168static mbedtls_tls_prf_types tls_prf_get_type(mbedtls_ssl_tls_prf_cb *tls_prf)
Jerry Yue93ffcd2022-02-17 14:37:06 +08008169{
8170 ((void) tls_prf);
Andrzej Kurek25f27152022-08-17 16:09:31 -04008171#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01008172 if (tls_prf == tls_prf_sha384) {
8173 return MBEDTLS_SSL_TLS_PRF_SHA384;
8174 } else
Jerry Yue93ffcd2022-02-17 14:37:06 +08008175#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04008176#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01008177 if (tls_prf == tls_prf_sha256) {
8178 return MBEDTLS_SSL_TLS_PRF_SHA256;
8179 } else
Jerry Yue93ffcd2022-02-17 14:37:06 +08008180#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01008181 return MBEDTLS_SSL_TLS_PRF_NONE;
Jerry Yue93ffcd2022-02-17 14:37:06 +08008182}
8183
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008184/*
8185 * Populate a transform structure with session keys and all the other
8186 * necessary information.
8187 *
8188 * Parameters:
8189 * - [in/out]: transform: structure to populate
8190 * [in] must be just initialised with mbedtls_ssl_transform_init()
8191 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
8192 * - [in] ciphersuite
8193 * - [in] master
8194 * - [in] encrypt_then_mac
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008195 * - [in] tls_prf: pointer to PRF to use for key derivation
8196 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
Glenn Strauss07c64162022-03-14 12:34:51 -04008197 * - [in] tls_version: TLS version
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008198 * - [in] endpoint: client or server
8199 * - [in] ssl: used for:
8200 * - ssl->conf->{f,p}_export_keys
8201 * [in] optionally used for:
8202 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
8203 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02008204MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01008205static int ssl_tls12_populate_transform(mbedtls_ssl_transform *transform,
8206 int ciphersuite,
8207 const unsigned char master[48],
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008208#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01008209 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008210#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01008211 ssl_tls_prf_t tls_prf,
8212 const unsigned char randbytes[64],
8213 mbedtls_ssl_protocol_version tls_version,
8214 unsigned endpoint,
8215 const mbedtls_ssl_context *ssl)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008216{
8217 int ret = 0;
8218 unsigned char keyblk[256];
8219 unsigned char *key1;
8220 unsigned char *key2;
8221 unsigned char *mac_enc;
8222 unsigned char *mac_dec;
8223 size_t mac_key_len = 0;
8224 size_t iv_copy_len;
8225 size_t keylen;
8226 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008227 mbedtls_ssl_mode_t ssl_mode;
Neil Armstronge4512952022-03-08 09:08:22 +01008228#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008229 const mbedtls_cipher_info_t *cipher_info;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008230 const mbedtls_md_info_t *md_info;
Neil Armstronge4512952022-03-08 09:08:22 +01008231#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008232
8233#if defined(MBEDTLS_USE_PSA_CRYPTO)
8234 psa_key_type_t key_type;
8235 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8236 psa_algorithm_t alg;
Neil Armstronge4512952022-03-08 09:08:22 +01008237 psa_algorithm_t mac_alg = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008238 size_t key_bits;
8239 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
8240#endif
8241
8242#if !defined(MBEDTLS_DEBUG_C) && \
8243 !defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Gilles Peskine449bd832023-01-11 14:50:10 +01008244 if (ssl->f_export_keys == NULL) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008245 ssl = NULL; /* make sure we don't use it except for these cases */
8246 (void) ssl;
8247 }
8248#endif
8249
8250 /*
8251 * Some data just needs copying into the structure
8252 */
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008253#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008254 transform->encrypt_then_mac = encrypt_then_mac;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008255#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Glenn Strauss07c64162022-03-14 12:34:51 -04008256 transform->tls_version = tls_version;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008257
8258#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01008259 memcpy(transform->randbytes, randbytes, sizeof(transform->randbytes));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008260#endif
8261
8262#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01008263 if (tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008264 /* At the moment, we keep TLS <= 1.2 and TLS 1.3 transform
8265 * generation separate. This should never happen. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008266 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008267 }
8268#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
8269
8270 /*
8271 * Get various info structures
8272 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008273 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(ciphersuite);
8274 if (ciphersuite_info == NULL) {
8275 MBEDTLS_SSL_DEBUG_MSG(1, ("ciphersuite info for %d not found",
8276 ciphersuite));
8277 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008278 }
8279
Neil Armstrongab555e02022-04-04 11:07:59 +02008280 ssl_mode = mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008281#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01008282 encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008283#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01008284 ciphersuite_info);
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008285
Gilles Peskine449bd832023-01-11 14:50:10 +01008286 if (ssl_mode == MBEDTLS_SSL_MODE_AEAD) {
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008287 transform->taglen =
8288 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Gilles Peskine449bd832023-01-11 14:50:10 +01008289 }
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008290
8291#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008292 if ((status = mbedtls_ssl_cipher_to_psa(ciphersuite_info->cipher,
8293 transform->taglen,
8294 &alg,
8295 &key_type,
8296 &key_bits)) != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05008297 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01008298 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_cipher_to_psa", ret);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008299 goto end;
8300 }
8301#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008302 cipher_info = mbedtls_cipher_info_from_type(ciphersuite_info->cipher);
8303 if (cipher_info == NULL) {
8304 MBEDTLS_SSL_DEBUG_MSG(1, ("cipher info for %u not found",
8305 ciphersuite_info->cipher));
8306 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008307 }
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008308#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008309
Neil Armstronge4512952022-03-08 09:08:22 +01008310#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008311 mac_alg = mbedtls_hash_info_psa_from_md(ciphersuite_info->mac);
8312 if (mac_alg == 0) {
8313 MBEDTLS_SSL_DEBUG_MSG(1, ("mbedtls_hash_info_psa_from_md for %u not found",
8314 (unsigned) ciphersuite_info->mac));
8315 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Neil Armstronge4512952022-03-08 09:08:22 +01008316 }
8317#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008318 md_info = mbedtls_md_info_from_type(ciphersuite_info->mac);
8319 if (md_info == NULL) {
8320 MBEDTLS_SSL_DEBUG_MSG(1, ("mbedtls_md info for %u not found",
8321 (unsigned) ciphersuite_info->mac));
8322 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008323 }
Neil Armstronge4512952022-03-08 09:08:22 +01008324#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008325
8326#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
8327 /* Copy own and peer's CID if the use of the CID
8328 * extension has been negotiated. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008329 if (ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED) {
8330 MBEDTLS_SSL_DEBUG_MSG(3, ("Copy CIDs into SSL transform"));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008331
8332 transform->in_cid_len = ssl->own_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008333 memcpy(transform->in_cid, ssl->own_cid, ssl->own_cid_len);
8334 MBEDTLS_SSL_DEBUG_BUF(3, "Incoming CID", transform->in_cid,
8335 transform->in_cid_len);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008336
8337 transform->out_cid_len = ssl->handshake->peer_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008338 memcpy(transform->out_cid, ssl->handshake->peer_cid,
8339 ssl->handshake->peer_cid_len);
8340 MBEDTLS_SSL_DEBUG_BUF(3, "Outgoing CID", transform->out_cid,
8341 transform->out_cid_len);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008342 }
8343#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
8344
8345 /*
8346 * Compute key block using the PRF
8347 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008348 ret = tls_prf(master, 48, "key expansion", randbytes, 64, keyblk, 256);
8349 if (ret != 0) {
8350 MBEDTLS_SSL_DEBUG_RET(1, "prf", ret);
8351 return ret;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008352 }
8353
Gilles Peskine449bd832023-01-11 14:50:10 +01008354 MBEDTLS_SSL_DEBUG_MSG(3, ("ciphersuite = %s",
8355 mbedtls_ssl_get_ciphersuite_name(ciphersuite)));
8356 MBEDTLS_SSL_DEBUG_BUF(3, "master secret", master, 48);
8357 MBEDTLS_SSL_DEBUG_BUF(4, "random bytes", randbytes, 64);
8358 MBEDTLS_SSL_DEBUG_BUF(4, "key block", keyblk, 256);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008359
8360 /*
8361 * Determine the appropriate key, IV and MAC length.
8362 */
8363
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008364#if defined(MBEDTLS_USE_PSA_CRYPTO)
8365 keylen = PSA_BITS_TO_BYTES(key_bits);
8366#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008367 keylen = mbedtls_cipher_info_get_key_bitlen(cipher_info) / 8;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008368#endif
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008369
8370#if defined(MBEDTLS_GCM_C) || \
8371 defined(MBEDTLS_CCM_C) || \
8372 defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008373 if (ssl_mode == MBEDTLS_SSL_MODE_AEAD) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008374 size_t explicit_ivlen;
8375
8376 transform->maclen = 0;
8377 mac_key_len = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008378
8379 /* All modes haves 96-bit IVs, but the length of the static parts vary
8380 * with mode and version:
8381 * - For GCM and CCM in TLS 1.2, there's a static IV of 4 Bytes
8382 * (to be concatenated with a dynamically chosen IV of 8 Bytes)
8383 * - For ChaChaPoly in TLS 1.2, and all modes in TLS 1.3, there's
8384 * a static IV of 12 Bytes (to be XOR'ed with the 8 Byte record
8385 * sequence number).
8386 */
8387 transform->ivlen = 12;
David Horstmann3b2276a2022-10-06 14:49:08 +01008388
8389 int is_chachapoly = 0;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008390#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008391 is_chachapoly = (key_type == PSA_KEY_TYPE_CHACHA20);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008392#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008393 is_chachapoly = (mbedtls_cipher_info_get_mode(cipher_info)
8394 == MBEDTLS_MODE_CHACHAPOLY);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008395#endif /* MBEDTLS_USE_PSA_CRYPTO */
David Horstmann3b2276a2022-10-06 14:49:08 +01008396
Gilles Peskine449bd832023-01-11 14:50:10 +01008397 if (is_chachapoly) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008398 transform->fixed_ivlen = 12;
Gilles Peskine449bd832023-01-11 14:50:10 +01008399 } else {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008400 transform->fixed_ivlen = 4;
Gilles Peskine449bd832023-01-11 14:50:10 +01008401 }
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008402
8403 /* Minimum length of encrypted record */
8404 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
8405 transform->minlen = explicit_ivlen + transform->taglen;
Gilles Peskine449bd832023-01-11 14:50:10 +01008406 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008407#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
8408#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01008409 if (ssl_mode == MBEDTLS_SSL_MODE_STREAM ||
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008410 ssl_mode == MBEDTLS_SSL_MODE_CBC ||
Gilles Peskine449bd832023-01-11 14:50:10 +01008411 ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM) {
Neil Armstronge4512952022-03-08 09:08:22 +01008412#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008413 size_t block_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008414#else
8415 size_t block_size = cipher_info->block_size;
8416#endif /* MBEDTLS_USE_PSA_CRYPTO */
8417
8418#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronge4512952022-03-08 09:08:22 +01008419 /* Get MAC length */
8420 mac_key_len = PSA_HASH_LENGTH(mac_alg);
8421#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008422 /* Initialize HMAC contexts */
Gilles Peskine449bd832023-01-11 14:50:10 +01008423 if ((ret = mbedtls_md_setup(&transform->md_ctx_enc, md_info, 1)) != 0 ||
8424 (ret = mbedtls_md_setup(&transform->md_ctx_dec, md_info, 1)) != 0) {
8425 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_setup", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008426 goto end;
8427 }
8428
8429 /* Get MAC length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008430 mac_key_len = mbedtls_md_get_size(md_info);
Neil Armstronge4512952022-03-08 09:08:22 +01008431#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008432 transform->maclen = mac_key_len;
8433
8434 /* IV length */
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008435#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008436 transform->ivlen = PSA_CIPHER_IV_LENGTH(key_type, alg);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008437#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008438 transform->ivlen = cipher_info->iv_size;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008439#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008440
8441 /* Minimum length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008442 if (ssl_mode == MBEDTLS_SSL_MODE_STREAM) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008443 transform->minlen = transform->maclen;
Gilles Peskine449bd832023-01-11 14:50:10 +01008444 } else {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008445 /*
8446 * GenericBlockCipher:
8447 * 1. if EtM is in use: one block plus MAC
8448 * otherwise: * first multiple of blocklen greater than maclen
8449 * 2. IV
8450 */
8451#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01008452 if (ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008453 transform->minlen = transform->maclen
Gilles Peskine449bd832023-01-11 14:50:10 +01008454 + block_size;
8455 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008456#endif
8457 {
8458 transform->minlen = transform->maclen
Gilles Peskine449bd832023-01-11 14:50:10 +01008459 + block_size
8460 - transform->maclen % block_size;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008461 }
8462
Gilles Peskine449bd832023-01-11 14:50:10 +01008463 if (tls_version == MBEDTLS_SSL_VERSION_TLS1_2) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008464 transform->minlen += transform->ivlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01008465 } else {
8466 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008467 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
8468 goto end;
8469 }
8470 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008471 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008472#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
8473 {
Gilles Peskine449bd832023-01-11 14:50:10 +01008474 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
8475 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008476 }
8477
Gilles Peskine449bd832023-01-11 14:50:10 +01008478 MBEDTLS_SSL_DEBUG_MSG(3, ("keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
8479 (unsigned) keylen,
8480 (unsigned) transform->minlen,
8481 (unsigned) transform->ivlen,
8482 (unsigned) transform->maclen));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008483
8484 /*
8485 * Finally setup the cipher contexts, IVs and MAC secrets.
8486 */
8487#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008488 if (endpoint == MBEDTLS_SSL_IS_CLIENT) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008489 key1 = keyblk + mac_key_len * 2;
8490 key2 = keyblk + mac_key_len * 2 + keylen;
8491
8492 mac_enc = keyblk;
8493 mac_dec = keyblk + mac_key_len;
8494
Gilles Peskine449bd832023-01-11 14:50:10 +01008495 iv_copy_len = (transform->fixed_ivlen) ?
8496 transform->fixed_ivlen : transform->ivlen;
8497 memcpy(transform->iv_enc, key2 + keylen, iv_copy_len);
8498 memcpy(transform->iv_dec, key2 + keylen + iv_copy_len,
8499 iv_copy_len);
8500 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008501#endif /* MBEDTLS_SSL_CLI_C */
8502#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008503 if (endpoint == MBEDTLS_SSL_IS_SERVER) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008504 key1 = keyblk + mac_key_len * 2 + keylen;
8505 key2 = keyblk + mac_key_len * 2;
8506
8507 mac_enc = keyblk + mac_key_len;
8508 mac_dec = keyblk;
8509
Gilles Peskine449bd832023-01-11 14:50:10 +01008510 iv_copy_len = (transform->fixed_ivlen) ?
8511 transform->fixed_ivlen : transform->ivlen;
8512 memcpy(transform->iv_dec, key1 + keylen, iv_copy_len);
8513 memcpy(transform->iv_enc, key1 + keylen + iv_copy_len,
8514 iv_copy_len);
8515 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008516#endif /* MBEDTLS_SSL_SRV_C */
8517 {
Gilles Peskine449bd832023-01-11 14:50:10 +01008518 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008519 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
8520 goto end;
8521 }
8522
Gilles Peskine449bd832023-01-11 14:50:10 +01008523 if (ssl != NULL && ssl->f_export_keys != NULL) {
8524 ssl->f_export_keys(ssl->p_export_keys,
8525 MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET,
8526 master, 48,
8527 randbytes + 32,
8528 randbytes,
8529 tls_prf_get_type(tls_prf));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008530 }
8531
8532#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008533 transform->psa_alg = alg;
8534
Gilles Peskine449bd832023-01-11 14:50:10 +01008535 if (alg != MBEDTLS_SSL_NULL_CIPHER) {
8536 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
8537 psa_set_key_algorithm(&attributes, alg);
8538 psa_set_key_type(&attributes, key_type);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008539
Gilles Peskine449bd832023-01-11 14:50:10 +01008540 if ((status = psa_import_key(&attributes,
8541 key1,
8542 PSA_BITS_TO_BYTES(key_bits),
8543 &transform->psa_key_enc)) != PSA_SUCCESS) {
8544 MBEDTLS_SSL_DEBUG_RET(3, "psa_import_key", (int) status);
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05008545 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01008546 MBEDTLS_SSL_DEBUG_RET(1, "psa_import_key", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008547 goto end;
8548 }
8549
Gilles Peskine449bd832023-01-11 14:50:10 +01008550 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008551
Gilles Peskine449bd832023-01-11 14:50:10 +01008552 if ((status = psa_import_key(&attributes,
8553 key2,
8554 PSA_BITS_TO_BYTES(key_bits),
8555 &transform->psa_key_dec)) != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05008556 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01008557 MBEDTLS_SSL_DEBUG_RET(1, "psa_import_key", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008558 goto end;
8559 }
8560 }
8561#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008562 if ((ret = mbedtls_cipher_setup(&transform->cipher_ctx_enc,
8563 cipher_info)) != 0) {
8564 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008565 goto end;
8566 }
8567
Gilles Peskine449bd832023-01-11 14:50:10 +01008568 if ((ret = mbedtls_cipher_setup(&transform->cipher_ctx_dec,
8569 cipher_info)) != 0) {
8570 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008571 goto end;
8572 }
8573
Gilles Peskine449bd832023-01-11 14:50:10 +01008574 if ((ret = mbedtls_cipher_setkey(&transform->cipher_ctx_enc, key1,
8575 (int) mbedtls_cipher_info_get_key_bitlen(cipher_info),
8576 MBEDTLS_ENCRYPT)) != 0) {
8577 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setkey", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008578 goto end;
8579 }
8580
Gilles Peskine449bd832023-01-11 14:50:10 +01008581 if ((ret = mbedtls_cipher_setkey(&transform->cipher_ctx_dec, key2,
8582 (int) mbedtls_cipher_info_get_key_bitlen(cipher_info),
8583 MBEDTLS_DECRYPT)) != 0) {
8584 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setkey", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008585 goto end;
8586 }
8587
8588#if defined(MBEDTLS_CIPHER_MODE_CBC)
Gilles Peskine449bd832023-01-11 14:50:10 +01008589 if (mbedtls_cipher_info_get_mode(cipher_info) == MBEDTLS_MODE_CBC) {
8590 if ((ret = mbedtls_cipher_set_padding_mode(&transform->cipher_ctx_enc,
8591 MBEDTLS_PADDING_NONE)) != 0) {
8592 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_set_padding_mode", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008593 goto end;
8594 }
8595
Gilles Peskine449bd832023-01-11 14:50:10 +01008596 if ((ret = mbedtls_cipher_set_padding_mode(&transform->cipher_ctx_dec,
8597 MBEDTLS_PADDING_NONE)) != 0) {
8598 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_set_padding_mode", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008599 goto end;
8600 }
8601 }
8602#endif /* MBEDTLS_CIPHER_MODE_CBC */
8603#endif /* MBEDTLS_USE_PSA_CRYPTO */
8604
Neil Armstrong29c0c042022-03-17 17:47:28 +01008605#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
8606 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
8607 For AEAD-based ciphersuites, there is nothing to do here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008608 if (mac_key_len != 0) {
Neil Armstrong29c0c042022-03-17 17:47:28 +01008609#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008610 transform->psa_mac_alg = PSA_ALG_HMAC(mac_alg);
Neil Armstrong29c0c042022-03-17 17:47:28 +01008611
Gilles Peskine449bd832023-01-11 14:50:10 +01008612 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
8613 psa_set_key_algorithm(&attributes, PSA_ALG_HMAC(mac_alg));
8614 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
Neil Armstrong29c0c042022-03-17 17:47:28 +01008615
Gilles Peskine449bd832023-01-11 14:50:10 +01008616 if ((status = psa_import_key(&attributes,
8617 mac_enc, mac_key_len,
8618 &transform->psa_mac_enc)) != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05008619 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01008620 MBEDTLS_SSL_DEBUG_RET(1, "psa_import_mac_key", ret);
Neil Armstrong29c0c042022-03-17 17:47:28 +01008621 goto end;
8622 }
8623
Gilles Peskine449bd832023-01-11 14:50:10 +01008624 if ((transform->psa_alg == MBEDTLS_SSL_NULL_CIPHER) ||
8625 ((transform->psa_alg == PSA_ALG_CBC_NO_PADDING)
Andrzej Kurek8c95ac42022-08-17 16:17:00 -04008626#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01008627 && (transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED)
Andrzej Kurek8c95ac42022-08-17 16:17:00 -04008628#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01008629 )) {
Neil Armstrong29c0c042022-03-17 17:47:28 +01008630 /* mbedtls_ct_hmac() requires the key to be exportable */
Gilles Peskine449bd832023-01-11 14:50:10 +01008631 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT |
8632 PSA_KEY_USAGE_VERIFY_HASH);
8633 } else {
8634 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
8635 }
Neil Armstrong29c0c042022-03-17 17:47:28 +01008636
Gilles Peskine449bd832023-01-11 14:50:10 +01008637 if ((status = psa_import_key(&attributes,
8638 mac_dec, mac_key_len,
8639 &transform->psa_mac_dec)) != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05008640 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01008641 MBEDTLS_SSL_DEBUG_RET(1, "psa_import_mac_key", ret);
Neil Armstrong29c0c042022-03-17 17:47:28 +01008642 goto end;
8643 }
8644#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008645 ret = mbedtls_md_hmac_starts(&transform->md_ctx_enc, mac_enc, mac_key_len);
8646 if (ret != 0) {
Neil Armstrong29c0c042022-03-17 17:47:28 +01008647 goto end;
Gilles Peskine449bd832023-01-11 14:50:10 +01008648 }
8649 ret = mbedtls_md_hmac_starts(&transform->md_ctx_dec, mac_dec, mac_key_len);
8650 if (ret != 0) {
Neil Armstrong29c0c042022-03-17 17:47:28 +01008651 goto end;
Gilles Peskine449bd832023-01-11 14:50:10 +01008652 }
Neil Armstrong29c0c042022-03-17 17:47:28 +01008653#endif /* MBEDTLS_USE_PSA_CRYPTO */
8654 }
8655#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
8656
8657 ((void) mac_dec);
8658 ((void) mac_enc);
8659
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008660end:
Gilles Peskine449bd832023-01-11 14:50:10 +01008661 mbedtls_platform_zeroize(keyblk, sizeof(keyblk));
8662 return ret;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008663}
8664
Valerio Settia08b1a42022-11-17 15:10:02 +01008665#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) && \
8666 defined(MBEDTLS_USE_PSA_CRYPTO)
Valerio Setti6b3dab02022-11-17 17:14:54 +01008667int mbedtls_psa_ecjpake_read_round(
Gilles Peskine449bd832023-01-11 14:50:10 +01008668 psa_pake_operation_t *pake_ctx,
8669 const unsigned char *buf,
8670 size_t len, mbedtls_ecjpake_rounds_t round)
Valerio Settia08b1a42022-11-17 15:10:02 +01008671{
8672 psa_status_t status;
8673 size_t input_offset = 0;
Valerio Setti819de862022-11-17 18:05:19 +01008674 /*
Valerio Setti6b3dab02022-11-17 17:14:54 +01008675 * At round one repeat the KEY_SHARE, ZK_PUBLIC & ZF_PROOF twice
8676 * At round two perform a single cycle
8677 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008678 unsigned int remaining_steps = (round == MBEDTLS_ECJPAKE_ROUND_ONE) ? 2 : 1;
Valerio Settia08b1a42022-11-17 15:10:02 +01008679
Gilles Peskine449bd832023-01-11 14:50:10 +01008680 for (; remaining_steps > 0; remaining_steps--) {
8681 for (psa_pake_step_t step = PSA_PAKE_STEP_KEY_SHARE;
Valerio Settia08b1a42022-11-17 15:10:02 +01008682 step <= PSA_PAKE_STEP_ZK_PROOF;
Gilles Peskine449bd832023-01-11 14:50:10 +01008683 ++step) {
Valerio Settia08b1a42022-11-17 15:10:02 +01008684 /* Length is stored at the first byte */
8685 size_t length = buf[input_offset];
8686 input_offset += 1;
8687
Gilles Peskine449bd832023-01-11 14:50:10 +01008688 if (input_offset + length > len) {
Valerio Settia08b1a42022-11-17 15:10:02 +01008689 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
8690 }
8691
Gilles Peskine449bd832023-01-11 14:50:10 +01008692 status = psa_pake_input(pake_ctx, step,
8693 buf + input_offset, length);
8694 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05008695 return PSA_TO_MBEDTLS_ERR(status);
Valerio Settia08b1a42022-11-17 15:10:02 +01008696 }
8697
8698 input_offset += length;
8699 }
8700 }
8701
Gilles Peskine449bd832023-01-11 14:50:10 +01008702 if (input_offset != len) {
Valerio Setti61ea17d2022-11-18 12:11:00 +01008703 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01008704 }
Valerio Setti30ebe112022-11-17 16:23:34 +01008705
Gilles Peskine449bd832023-01-11 14:50:10 +01008706 return 0;
Valerio Settia08b1a42022-11-17 15:10:02 +01008707}
8708
Valerio Setti6b3dab02022-11-17 17:14:54 +01008709int mbedtls_psa_ecjpake_write_round(
Gilles Peskine449bd832023-01-11 14:50:10 +01008710 psa_pake_operation_t *pake_ctx,
8711 unsigned char *buf,
8712 size_t len, size_t *olen,
8713 mbedtls_ecjpake_rounds_t round)
Valerio Settia08b1a42022-11-17 15:10:02 +01008714{
8715 psa_status_t status;
8716 size_t output_offset = 0;
8717 size_t output_len;
Valerio Setti819de862022-11-17 18:05:19 +01008718 /*
Valerio Setti6b3dab02022-11-17 17:14:54 +01008719 * At round one repeat the KEY_SHARE, ZK_PUBLIC & ZF_PROOF twice
8720 * At round two perform a single cycle
8721 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008722 unsigned int remaining_steps = (round == MBEDTLS_ECJPAKE_ROUND_ONE) ? 2 : 1;
Valerio Settia08b1a42022-11-17 15:10:02 +01008723
Gilles Peskine449bd832023-01-11 14:50:10 +01008724 for (; remaining_steps > 0; remaining_steps--) {
8725 for (psa_pake_step_t step = PSA_PAKE_STEP_KEY_SHARE;
8726 step <= PSA_PAKE_STEP_ZK_PROOF;
8727 ++step) {
Valerio Setti79f6b6b2022-11-21 14:17:03 +01008728 /*
Valerio Settid4a9b1a2022-11-22 11:11:10 +01008729 * For each step, prepend 1 byte with the length of the data as
8730 * given by psa_pake_output().
Valerio Setti79f6b6b2022-11-21 14:17:03 +01008731 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008732 status = psa_pake_output(pake_ctx, step,
8733 buf + output_offset + 1,
8734 len - output_offset - 1,
8735 &output_len);
8736 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05008737 return PSA_TO_MBEDTLS_ERR(status);
Valerio Settia08b1a42022-11-17 15:10:02 +01008738 }
8739
Valerio Setti99d88c12022-11-22 16:03:43 +01008740 *(buf + output_offset) = (uint8_t) output_len;
Valerio Setti79f6b6b2022-11-21 14:17:03 +01008741
8742 output_offset += output_len + 1;
Valerio Settia08b1a42022-11-17 15:10:02 +01008743 }
8744 }
8745
8746 *olen = output_offset;
8747
Gilles Peskine449bd832023-01-11 14:50:10 +01008748 return 0;
Valerio Settia08b1a42022-11-17 15:10:02 +01008749}
Valerio Settia08b1a42022-11-17 15:10:02 +01008750#endif //MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED && MBEDTLS_USE_PSA_CRYPTO
8751
Jerry Yuee40f9d2022-02-17 14:55:16 +08008752#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008753int mbedtls_ssl_get_key_exchange_md_tls1_2(mbedtls_ssl_context *ssl,
8754 unsigned char *hash, size_t *hashlen,
8755 unsigned char *data, size_t data_len,
8756 mbedtls_md_type_t md_alg)
Jerry Yuee40f9d2022-02-17 14:55:16 +08008757{
8758 psa_status_t status;
8759 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01008760 psa_algorithm_t hash_alg = mbedtls_hash_info_psa_from_md(md_alg);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008761
Gilles Peskine449bd832023-01-11 14:50:10 +01008762 MBEDTLS_SSL_DEBUG_MSG(3, ("Perform PSA-based computation of digest of ServerKeyExchange"));
Jerry Yuee40f9d2022-02-17 14:55:16 +08008763
Gilles Peskine449bd832023-01-11 14:50:10 +01008764 if ((status = psa_hash_setup(&hash_operation,
8765 hash_alg)) != PSA_SUCCESS) {
8766 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_setup", status);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008767 goto exit;
8768 }
8769
Gilles Peskine449bd832023-01-11 14:50:10 +01008770 if ((status = psa_hash_update(&hash_operation, ssl->handshake->randbytes,
8771 64)) != PSA_SUCCESS) {
8772 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_update", status);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008773 goto exit;
8774 }
8775
Gilles Peskine449bd832023-01-11 14:50:10 +01008776 if ((status = psa_hash_update(&hash_operation,
8777 data, data_len)) != PSA_SUCCESS) {
8778 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_update", status);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008779 goto exit;
8780 }
8781
Gilles Peskine449bd832023-01-11 14:50:10 +01008782 if ((status = psa_hash_finish(&hash_operation, hash, PSA_HASH_MAX_SIZE,
8783 hashlen)) != PSA_SUCCESS) {
8784 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_finish", status);
8785 goto exit;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008786 }
8787
8788exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008789 if (status != PSA_SUCCESS) {
8790 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8791 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
8792 switch (status) {
Jerry Yuee40f9d2022-02-17 14:55:16 +08008793 case PSA_ERROR_NOT_SUPPORTED:
Gilles Peskine449bd832023-01-11 14:50:10 +01008794 return MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008795 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
8796 case PSA_ERROR_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +01008797 return MBEDTLS_ERR_MD_BAD_INPUT_DATA;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008798 case PSA_ERROR_INSUFFICIENT_MEMORY:
Gilles Peskine449bd832023-01-11 14:50:10 +01008799 return MBEDTLS_ERR_MD_ALLOC_FAILED;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008800 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01008801 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008802 }
8803 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008804 return 0;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008805}
8806
8807#else
8808
Gilles Peskine449bd832023-01-11 14:50:10 +01008809int mbedtls_ssl_get_key_exchange_md_tls1_2(mbedtls_ssl_context *ssl,
8810 unsigned char *hash, size_t *hashlen,
8811 unsigned char *data, size_t data_len,
8812 mbedtls_md_type_t md_alg)
Jerry Yuee40f9d2022-02-17 14:55:16 +08008813{
8814 int ret = 0;
8815 mbedtls_md_context_t ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +01008816 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type(md_alg);
8817 *hashlen = mbedtls_md_get_size(md_info);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008818
Gilles Peskine449bd832023-01-11 14:50:10 +01008819 MBEDTLS_SSL_DEBUG_MSG(3, ("Perform mbedtls-based computation of digest of ServerKeyExchange"));
Jerry Yuee40f9d2022-02-17 14:55:16 +08008820
Gilles Peskine449bd832023-01-11 14:50:10 +01008821 mbedtls_md_init(&ctx);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008822
8823 /*
8824 * digitally-signed struct {
8825 * opaque client_random[32];
8826 * opaque server_random[32];
8827 * ServerDHParams params;
8828 * };
8829 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008830 if ((ret = mbedtls_md_setup(&ctx, md_info, 0)) != 0) {
8831 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_setup", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008832 goto exit;
8833 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008834 if ((ret = mbedtls_md_starts(&ctx)) != 0) {
8835 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_starts", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008836 goto exit;
8837 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008838 if ((ret = mbedtls_md_update(&ctx, ssl->handshake->randbytes, 64)) != 0) {
8839 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_update", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008840 goto exit;
8841 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008842 if ((ret = mbedtls_md_update(&ctx, data, data_len)) != 0) {
8843 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_update", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008844 goto exit;
8845 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008846 if ((ret = mbedtls_md_finish(&ctx, hash)) != 0) {
8847 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_finish", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008848 goto exit;
8849 }
8850
8851exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008852 mbedtls_md_free(&ctx);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008853
Gilles Peskine449bd832023-01-11 14:50:10 +01008854 if (ret != 0) {
8855 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8856 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
8857 }
Jerry Yuee40f9d2022-02-17 14:55:16 +08008858
Gilles Peskine449bd832023-01-11 14:50:10 +01008859 return ret;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008860}
8861#endif /* MBEDTLS_USE_PSA_CRYPTO */
8862
Jerry Yud9d91da2022-02-17 14:57:06 +08008863#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
8864
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008865/* Find the preferred hash for a given signature algorithm. */
8866unsigned int mbedtls_ssl_tls12_get_preferred_hash_for_sig_alg(
Gilles Peskine449bd832023-01-11 14:50:10 +01008867 mbedtls_ssl_context *ssl,
8868 unsigned int sig_alg)
Jerry Yud9d91da2022-02-17 14:57:06 +08008869{
Gabor Mezei078e8032022-04-27 21:17:56 +02008870 unsigned int i;
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008871 uint16_t *received_sig_algs = ssl->handshake->received_sig_algs;
Gabor Mezei078e8032022-04-27 21:17:56 +02008872
Gilles Peskine449bd832023-01-11 14:50:10 +01008873 if (sig_alg == MBEDTLS_SSL_SIG_ANON) {
8874 return MBEDTLS_SSL_HASH_NONE;
8875 }
Gabor Mezei078e8032022-04-27 21:17:56 +02008876
Gilles Peskine449bd832023-01-11 14:50:10 +01008877 for (i = 0; received_sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++) {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008878 unsigned int hash_alg_received =
Gilles Peskine449bd832023-01-11 14:50:10 +01008879 MBEDTLS_SSL_TLS12_HASH_ALG_FROM_SIG_AND_HASH_ALG(
8880 received_sig_algs[i]);
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008881 unsigned int sig_alg_received =
Gilles Peskine449bd832023-01-11 14:50:10 +01008882 MBEDTLS_SSL_TLS12_SIG_ALG_FROM_SIG_AND_HASH_ALG(
8883 received_sig_algs[i]);
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008884
Gilles Peskine449bd832023-01-11 14:50:10 +01008885 if (sig_alg == sig_alg_received) {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008886#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008887 if (ssl->handshake->key_cert && ssl->handshake->key_cert->key) {
Neil Armstrong96eceb82022-06-30 18:05:05 +02008888 psa_algorithm_t psa_hash_alg =
Gilles Peskine449bd832023-01-11 14:50:10 +01008889 mbedtls_hash_info_psa_from_md(hash_alg_received);
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008890
Gilles Peskine449bd832023-01-11 14:50:10 +01008891 if (sig_alg_received == MBEDTLS_SSL_SIG_ECDSA &&
8892 !mbedtls_pk_can_do_ext(ssl->handshake->key_cert->key,
8893 PSA_ALG_ECDSA(psa_hash_alg),
8894 PSA_KEY_USAGE_SIGN_HASH)) {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008895 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01008896 }
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008897
Gilles Peskine449bd832023-01-11 14:50:10 +01008898 if (sig_alg_received == MBEDTLS_SSL_SIG_RSA &&
8899 !mbedtls_pk_can_do_ext(ssl->handshake->key_cert->key,
8900 PSA_ALG_RSA_PKCS1V15_SIGN(
8901 psa_hash_alg),
8902 PSA_KEY_USAGE_SIGN_HASH)) {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008903 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01008904 }
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008905 }
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008906#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong96eceb82022-06-30 18:05:05 +02008907
Gilles Peskine449bd832023-01-11 14:50:10 +01008908 return hash_alg_received;
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008909 }
Jerry Yud9d91da2022-02-17 14:57:06 +08008910 }
Jerry Yud9d91da2022-02-17 14:57:06 +08008911
Gilles Peskine449bd832023-01-11 14:50:10 +01008912 return MBEDTLS_SSL_HASH_NONE;
Jerry Yud9d91da2022-02-17 14:57:06 +08008913}
8914
8915#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
8916
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008917/* Serialization of TLS 1.2 sessions:
8918 *
8919 * struct {
8920 * uint64 start_time;
8921 * uint8 ciphersuite[2]; // defined by the standard
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008922 * uint8 session_id_len; // at most 32
8923 * opaque session_id[32];
8924 * opaque master[48]; // fixed length in the standard
8925 * uint32 verify_result;
8926 * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert
8927 * opaque ticket<0..2^24-1>; // length 0 means no ticket
8928 * uint32 ticket_lifetime;
8929 * uint8 mfl_code; // up to 255 according to standard
8930 * uint8 encrypt_then_mac; // 0 or 1
8931 * } serialized_session_tls12;
8932 *
8933 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008934static size_t ssl_tls12_session_save(const mbedtls_ssl_session *session,
8935 unsigned char *buf,
8936 size_t buf_len)
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008937{
8938 unsigned char *p = buf;
8939 size_t used = 0;
8940
8941#if defined(MBEDTLS_HAVE_TIME)
8942 uint64_t start;
8943#endif
8944#if defined(MBEDTLS_X509_CRT_PARSE_C)
8945#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8946 size_t cert_len;
8947#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8948#endif /* MBEDTLS_X509_CRT_PARSE_C */
8949
8950 /*
8951 * Time
8952 */
8953#if defined(MBEDTLS_HAVE_TIME)
8954 used += 8;
8955
Gilles Peskine449bd832023-01-11 14:50:10 +01008956 if (used <= buf_len) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008957 start = (uint64_t) session->start;
8958
Gilles Peskine449bd832023-01-11 14:50:10 +01008959 MBEDTLS_PUT_UINT64_BE(start, p, 0);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008960 p += 8;
8961 }
8962#endif /* MBEDTLS_HAVE_TIME */
8963
8964 /*
8965 * Basic mandatory fields
8966 */
8967 used += 2 /* ciphersuite */
Gilles Peskine449bd832023-01-11 14:50:10 +01008968 + 1 /* id_len */
8969 + sizeof(session->id)
8970 + sizeof(session->master)
8971 + 4; /* verify_result */
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008972
Gilles Peskine449bd832023-01-11 14:50:10 +01008973 if (used <= buf_len) {
8974 MBEDTLS_PUT_UINT16_BE(session->ciphersuite, p, 0);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008975 p += 2;
8976
Gilles Peskine449bd832023-01-11 14:50:10 +01008977 *p++ = MBEDTLS_BYTE_0(session->id_len);
8978 memcpy(p, session->id, 32);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008979 p += 32;
8980
Gilles Peskine449bd832023-01-11 14:50:10 +01008981 memcpy(p, session->master, 48);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008982 p += 48;
8983
Gilles Peskine449bd832023-01-11 14:50:10 +01008984 MBEDTLS_PUT_UINT32_BE(session->verify_result, p, 0);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008985 p += 4;
8986 }
8987
8988 /*
8989 * Peer's end-entity certificate
8990 */
8991#if defined(MBEDTLS_X509_CRT_PARSE_C)
8992#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01008993 if (session->peer_cert == NULL) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008994 cert_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01008995 } else {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008996 cert_len = session->peer_cert->raw.len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008997 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008998
8999 used += 3 + cert_len;
9000
Gilles Peskine449bd832023-01-11 14:50:10 +01009001 if (used <= buf_len) {
9002 *p++ = MBEDTLS_BYTE_2(cert_len);
9003 *p++ = MBEDTLS_BYTE_1(cert_len);
9004 *p++ = MBEDTLS_BYTE_0(cert_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009005
Gilles Peskine449bd832023-01-11 14:50:10 +01009006 if (session->peer_cert != NULL) {
9007 memcpy(p, session->peer_cert->raw.p, cert_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009008 p += cert_len;
9009 }
9010 }
9011#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009012 if (session->peer_cert_digest != NULL) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009013 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01009014 if (used <= buf_len) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009015 *p++ = (unsigned char) session->peer_cert_digest_type;
9016 *p++ = (unsigned char) session->peer_cert_digest_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01009017 memcpy(p, session->peer_cert_digest,
9018 session->peer_cert_digest_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009019 p += session->peer_cert_digest_len;
9020 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009021 } else {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009022 used += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01009023 if (used <= buf_len) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009024 *p++ = (unsigned char) MBEDTLS_MD_NONE;
9025 *p++ = 0;
9026 }
9027 }
9028#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9029#endif /* MBEDTLS_X509_CRT_PARSE_C */
9030
9031 /*
9032 * Session ticket if any, plus associated data
9033 */
9034#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
9035 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
9036
Gilles Peskine449bd832023-01-11 14:50:10 +01009037 if (used <= buf_len) {
9038 *p++ = MBEDTLS_BYTE_2(session->ticket_len);
9039 *p++ = MBEDTLS_BYTE_1(session->ticket_len);
9040 *p++ = MBEDTLS_BYTE_0(session->ticket_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009041
Gilles Peskine449bd832023-01-11 14:50:10 +01009042 if (session->ticket != NULL) {
9043 memcpy(p, session->ticket, session->ticket_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009044 p += session->ticket_len;
9045 }
9046
Gilles Peskine449bd832023-01-11 14:50:10 +01009047 MBEDTLS_PUT_UINT32_BE(session->ticket_lifetime, p, 0);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009048 p += 4;
9049 }
9050#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
9051
9052 /*
9053 * Misc extension-related info
9054 */
9055#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9056 used += 1;
9057
Gilles Peskine449bd832023-01-11 14:50:10 +01009058 if (used <= buf_len) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009059 *p++ = session->mfl_code;
Gilles Peskine449bd832023-01-11 14:50:10 +01009060 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009061#endif
9062
9063#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
9064 used += 1;
9065
Gilles Peskine449bd832023-01-11 14:50:10 +01009066 if (used <= buf_len) {
9067 *p++ = MBEDTLS_BYTE_0(session->encrypt_then_mac);
9068 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009069#endif
9070
Gilles Peskine449bd832023-01-11 14:50:10 +01009071 return used;
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009072}
9073
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02009074MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01009075static int ssl_tls12_session_load(mbedtls_ssl_session *session,
9076 const unsigned char *buf,
9077 size_t len)
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009078{
9079#if defined(MBEDTLS_HAVE_TIME)
9080 uint64_t start;
9081#endif
9082#if defined(MBEDTLS_X509_CRT_PARSE_C)
9083#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
9084 size_t cert_len;
9085#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9086#endif /* MBEDTLS_X509_CRT_PARSE_C */
9087
9088 const unsigned char *p = buf;
9089 const unsigned char * const end = buf + len;
9090
9091 /*
9092 * Time
9093 */
9094#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01009095 if (8 > (size_t) (end - p)) {
9096 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9097 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009098
Gilles Peskine449bd832023-01-11 14:50:10 +01009099 start = ((uint64_t) p[0] << 56) |
9100 ((uint64_t) p[1] << 48) |
9101 ((uint64_t) p[2] << 40) |
9102 ((uint64_t) p[3] << 32) |
9103 ((uint64_t) p[4] << 24) |
9104 ((uint64_t) p[5] << 16) |
9105 ((uint64_t) p[6] << 8) |
9106 ((uint64_t) p[7]);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009107 p += 8;
9108
9109 session->start = (time_t) start;
9110#endif /* MBEDTLS_HAVE_TIME */
9111
9112 /*
9113 * Basic mandatory fields
9114 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009115 if (2 + 1 + 32 + 48 + 4 > (size_t) (end - p)) {
9116 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9117 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009118
Gilles Peskine449bd832023-01-11 14:50:10 +01009119 session->ciphersuite = (p[0] << 8) | p[1];
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009120 p += 2;
9121
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009122 session->id_len = *p++;
Gilles Peskine449bd832023-01-11 14:50:10 +01009123 memcpy(session->id, p, 32);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009124 p += 32;
9125
Gilles Peskine449bd832023-01-11 14:50:10 +01009126 memcpy(session->master, p, 48);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009127 p += 48;
9128
Gilles Peskine449bd832023-01-11 14:50:10 +01009129 session->verify_result = ((uint32_t) p[0] << 24) |
9130 ((uint32_t) p[1] << 16) |
9131 ((uint32_t) p[2] << 8) |
9132 ((uint32_t) p[3]);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009133 p += 4;
9134
9135 /* Immediately clear invalid pointer values that have been read, in case
9136 * we exit early before we replaced them with valid ones. */
9137#if defined(MBEDTLS_X509_CRT_PARSE_C)
9138#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
9139 session->peer_cert = NULL;
9140#else
9141 session->peer_cert_digest = NULL;
9142#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9143#endif /* MBEDTLS_X509_CRT_PARSE_C */
9144#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
9145 session->ticket = NULL;
9146#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
9147
9148 /*
9149 * Peer certificate
9150 */
9151#if defined(MBEDTLS_X509_CRT_PARSE_C)
9152#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
9153 /* Deserialize CRT from the end of the ticket. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009154 if (3 > (size_t) (end - p)) {
9155 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9156 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009157
Gilles Peskine449bd832023-01-11 14:50:10 +01009158 cert_len = (p[0] << 16) | (p[1] << 8) | p[2];
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009159 p += 3;
9160
Gilles Peskine449bd832023-01-11 14:50:10 +01009161 if (cert_len != 0) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009162 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
9163
Gilles Peskine449bd832023-01-11 14:50:10 +01009164 if (cert_len > (size_t) (end - p)) {
9165 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9166 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009167
Gilles Peskine449bd832023-01-11 14:50:10 +01009168 session->peer_cert = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009169
Gilles Peskine449bd832023-01-11 14:50:10 +01009170 if (session->peer_cert == NULL) {
9171 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
9172 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009173
Gilles Peskine449bd832023-01-11 14:50:10 +01009174 mbedtls_x509_crt_init(session->peer_cert);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009175
Gilles Peskine449bd832023-01-11 14:50:10 +01009176 if ((ret = mbedtls_x509_crt_parse_der(session->peer_cert,
9177 p, cert_len)) != 0) {
9178 mbedtls_x509_crt_free(session->peer_cert);
9179 mbedtls_free(session->peer_cert);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009180 session->peer_cert = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01009181 return ret;
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009182 }
9183
9184 p += cert_len;
9185 }
9186#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9187 /* Deserialize CRT digest from the end of the ticket. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009188 if (2 > (size_t) (end - p)) {
9189 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9190 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009191
9192 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
9193 session->peer_cert_digest_len = (size_t) *p++;
9194
Gilles Peskine449bd832023-01-11 14:50:10 +01009195 if (session->peer_cert_digest_len != 0) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009196 const mbedtls_md_info_t *md_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01009197 mbedtls_md_info_from_type(session->peer_cert_digest_type);
9198 if (md_info == NULL) {
9199 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9200 }
9201 if (session->peer_cert_digest_len != mbedtls_md_get_size(md_info)) {
9202 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9203 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009204
Gilles Peskine449bd832023-01-11 14:50:10 +01009205 if (session->peer_cert_digest_len > (size_t) (end - p)) {
9206 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9207 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009208
9209 session->peer_cert_digest =
Gilles Peskine449bd832023-01-11 14:50:10 +01009210 mbedtls_calloc(1, session->peer_cert_digest_len);
9211 if (session->peer_cert_digest == NULL) {
9212 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
9213 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009214
Gilles Peskine449bd832023-01-11 14:50:10 +01009215 memcpy(session->peer_cert_digest, p,
9216 session->peer_cert_digest_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009217 p += session->peer_cert_digest_len;
9218 }
9219#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9220#endif /* MBEDTLS_X509_CRT_PARSE_C */
9221
9222 /*
9223 * Session ticket and associated data
9224 */
9225#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01009226 if (3 > (size_t) (end - p)) {
9227 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9228 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009229
Gilles Peskine449bd832023-01-11 14:50:10 +01009230 session->ticket_len = (p[0] << 16) | (p[1] << 8) | p[2];
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009231 p += 3;
9232
Gilles Peskine449bd832023-01-11 14:50:10 +01009233 if (session->ticket_len != 0) {
9234 if (session->ticket_len > (size_t) (end - p)) {
9235 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9236 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009237
Gilles Peskine449bd832023-01-11 14:50:10 +01009238 session->ticket = mbedtls_calloc(1, session->ticket_len);
9239 if (session->ticket == NULL) {
9240 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
9241 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009242
Gilles Peskine449bd832023-01-11 14:50:10 +01009243 memcpy(session->ticket, p, session->ticket_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009244 p += session->ticket_len;
9245 }
9246
Gilles Peskine449bd832023-01-11 14:50:10 +01009247 if (4 > (size_t) (end - p)) {
9248 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9249 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009250
Gilles Peskine449bd832023-01-11 14:50:10 +01009251 session->ticket_lifetime = ((uint32_t) p[0] << 24) |
9252 ((uint32_t) p[1] << 16) |
9253 ((uint32_t) p[2] << 8) |
9254 ((uint32_t) p[3]);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009255 p += 4;
9256#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
9257
9258 /*
9259 * Misc extension-related info
9260 */
9261#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01009262 if (1 > (size_t) (end - p)) {
9263 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9264 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009265
9266 session->mfl_code = *p++;
9267#endif
9268
9269#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01009270 if (1 > (size_t) (end - p)) {
9271 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9272 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009273
9274 session->encrypt_then_mac = *p++;
9275#endif
9276
9277 /* Done, should have consumed entire buffer */
Gilles Peskine449bd832023-01-11 14:50:10 +01009278 if (p != end) {
9279 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9280 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009281
Gilles Peskine449bd832023-01-11 14:50:10 +01009282 return 0;
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009283}
Jerry Yudc7bd172022-02-17 13:44:15 +08009284#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9285
XiaokangQian75d40ef2022-04-20 11:05:24 +00009286int mbedtls_ssl_validate_ciphersuite(
9287 const mbedtls_ssl_context *ssl,
9288 const mbedtls_ssl_ciphersuite_t *suite_info,
9289 mbedtls_ssl_protocol_version min_tls_version,
Gilles Peskine449bd832023-01-11 14:50:10 +01009290 mbedtls_ssl_protocol_version max_tls_version)
XiaokangQian75d40ef2022-04-20 11:05:24 +00009291{
9292 (void) ssl;
9293
Gilles Peskine449bd832023-01-11 14:50:10 +01009294 if (suite_info == NULL) {
9295 return -1;
9296 }
XiaokangQian75d40ef2022-04-20 11:05:24 +00009297
Gilles Peskine449bd832023-01-11 14:50:10 +01009298 if ((suite_info->min_tls_version > max_tls_version) ||
9299 (suite_info->max_tls_version < min_tls_version)) {
9300 return -1;
XiaokangQian75d40ef2022-04-20 11:05:24 +00009301 }
9302
XiaokangQian060d8672022-04-21 09:24:56 +00009303#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_CLI_C)
XiaokangQian75d40ef2022-04-20 11:05:24 +00009304#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Neil Armstrongca7d5062022-05-31 14:43:23 +02009305#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01009306 if (suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
9307 ssl->handshake->psa_pake_ctx_is_ok != 1)
Neil Armstrongca7d5062022-05-31 14:43:23 +02009308#else
Gilles Peskine449bd832023-01-11 14:50:10 +01009309 if (suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
9310 mbedtls_ecjpake_check(&ssl->handshake->ecjpake_ctx) != 0)
Neil Armstrongca7d5062022-05-31 14:43:23 +02009311#endif /* MBEDTLS_USE_PSA_CRYPTO */
XiaokangQian75d40ef2022-04-20 11:05:24 +00009312 {
Gilles Peskine449bd832023-01-11 14:50:10 +01009313 return -1;
XiaokangQian75d40ef2022-04-20 11:05:24 +00009314 }
9315#endif
9316
9317 /* Don't suggest PSK-based ciphersuite if no PSK is available. */
9318#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01009319 if (mbedtls_ssl_ciphersuite_uses_psk(suite_info) &&
9320 mbedtls_ssl_conf_has_static_psk(ssl->conf) == 0) {
9321 return -1;
XiaokangQian75d40ef2022-04-20 11:05:24 +00009322 }
9323#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
9324#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9325
Gilles Peskine449bd832023-01-11 14:50:10 +01009326 return 0;
XiaokangQian75d40ef2022-04-20 11:05:24 +00009327}
9328
Ronald Crone68ab4f2022-10-05 12:46:29 +02009329#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
XiaokangQianeaf36512022-04-24 09:07:44 +00009330/*
9331 * Function for writing a signature algorithm extension.
9332 *
9333 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
9334 * value (TLS 1.3 RFC8446):
9335 * enum {
9336 * ....
9337 * ecdsa_secp256r1_sha256( 0x0403 ),
9338 * ecdsa_secp384r1_sha384( 0x0503 ),
9339 * ecdsa_secp521r1_sha512( 0x0603 ),
9340 * ....
9341 * } SignatureScheme;
9342 *
9343 * struct {
9344 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
9345 * } SignatureSchemeList;
9346 *
9347 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
9348 * value (TLS 1.2 RFC5246):
9349 * enum {
9350 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
9351 * sha512(6), (255)
9352 * } HashAlgorithm;
9353 *
9354 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
9355 * SignatureAlgorithm;
9356 *
9357 * struct {
9358 * HashAlgorithm hash;
9359 * SignatureAlgorithm signature;
9360 * } SignatureAndHashAlgorithm;
9361 *
9362 * SignatureAndHashAlgorithm
9363 * supported_signature_algorithms<2..2^16-2>;
9364 *
9365 * The TLS 1.3 signature algorithm extension was defined to be a compatible
9366 * generalization of the TLS 1.2 signature algorithm extension.
9367 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
9368 * `SignatureScheme` field of TLS 1.3
9369 *
9370 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009371int mbedtls_ssl_write_sig_alg_ext(mbedtls_ssl_context *ssl, unsigned char *buf,
9372 const unsigned char *end, size_t *out_len)
XiaokangQianeaf36512022-04-24 09:07:44 +00009373{
9374 unsigned char *p = buf;
9375 unsigned char *supported_sig_alg; /* Start of supported_signature_algorithms */
9376 size_t supported_sig_alg_len = 0; /* Length of supported_signature_algorithms */
9377
9378 *out_len = 0;
9379
Gilles Peskine449bd832023-01-11 14:50:10 +01009380 MBEDTLS_SSL_DEBUG_MSG(3, ("adding signature_algorithms extension"));
XiaokangQianeaf36512022-04-24 09:07:44 +00009381
9382 /* Check if we have space for header and length field:
9383 * - extension_type (2 bytes)
9384 * - extension_data_length (2 bytes)
9385 * - supported_signature_algorithms_length (2 bytes)
9386 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009387 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 6);
XiaokangQianeaf36512022-04-24 09:07:44 +00009388 p += 6;
9389
9390 /*
9391 * Write supported_signature_algorithms
9392 */
9393 supported_sig_alg = p;
Gilles Peskine449bd832023-01-11 14:50:10 +01009394 const uint16_t *sig_alg = mbedtls_ssl_get_sig_algs(ssl);
9395 if (sig_alg == NULL) {
9396 return MBEDTLS_ERR_SSL_BAD_CONFIG;
9397 }
XiaokangQianeaf36512022-04-24 09:07:44 +00009398
Gilles Peskine449bd832023-01-11 14:50:10 +01009399 for (; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++) {
9400 MBEDTLS_SSL_DEBUG_MSG(3, ("got signature scheme [%x] %s",
9401 *sig_alg,
9402 mbedtls_ssl_sig_alg_to_str(*sig_alg)));
9403 if (!mbedtls_ssl_sig_alg_is_supported(ssl, *sig_alg)) {
XiaokangQianeaf36512022-04-24 09:07:44 +00009404 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01009405 }
9406 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
9407 MBEDTLS_PUT_UINT16_BE(*sig_alg, p, 0);
XiaokangQianeaf36512022-04-24 09:07:44 +00009408 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01009409 MBEDTLS_SSL_DEBUG_MSG(3, ("sent signature scheme [%x] %s",
9410 *sig_alg,
9411 mbedtls_ssl_sig_alg_to_str(*sig_alg)));
XiaokangQianeaf36512022-04-24 09:07:44 +00009412 }
9413
9414 /* Length of supported_signature_algorithms */
9415 supported_sig_alg_len = p - supported_sig_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01009416 if (supported_sig_alg_len == 0) {
9417 MBEDTLS_SSL_DEBUG_MSG(1, ("No signature algorithms defined."));
9418 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
XiaokangQianeaf36512022-04-24 09:07:44 +00009419 }
9420
Gilles Peskine449bd832023-01-11 14:50:10 +01009421 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SIG_ALG, buf, 0);
9422 MBEDTLS_PUT_UINT16_BE(supported_sig_alg_len + 2, buf, 2);
9423 MBEDTLS_PUT_UINT16_BE(supported_sig_alg_len, buf, 4);
XiaokangQianeaf36512022-04-24 09:07:44 +00009424
XiaokangQianeaf36512022-04-24 09:07:44 +00009425 *out_len = p - buf;
9426
9427#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01009428 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_SIG_ALG);
XiaokangQianeaf36512022-04-24 09:07:44 +00009429#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu0c354a22022-08-29 15:25:36 +08009430
Gilles Peskine449bd832023-01-11 14:50:10 +01009431 return 0;
XiaokangQianeaf36512022-04-24 09:07:44 +00009432}
Ronald Crone68ab4f2022-10-05 12:46:29 +02009433#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
XiaokangQianeaf36512022-04-24 09:07:44 +00009434
XiaokangQian40a35232022-05-07 09:02:40 +00009435#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
XiaokangQian9b2b7712022-05-17 02:57:00 +00009436/*
9437 * mbedtls_ssl_parse_server_name_ext
9438 *
9439 * Structure of server_name extension:
9440 *
9441 * enum {
9442 * host_name(0), (255)
9443 * } NameType;
9444 * opaque HostName<1..2^16-1>;
9445 *
9446 * struct {
9447 * NameType name_type;
9448 * select (name_type) {
9449 * case host_name: HostName;
9450 * } name;
9451 * } ServerName;
9452 * struct {
9453 * ServerName server_name_list<1..2^16-1>
9454 * } ServerNameList;
9455 */
Ronald Cronce7d76e2022-07-08 18:56:49 +02009456MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01009457int mbedtls_ssl_parse_server_name_ext(mbedtls_ssl_context *ssl,
9458 const unsigned char *buf,
9459 const unsigned char *end)
XiaokangQian40a35232022-05-07 09:02:40 +00009460{
9461 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
9462 const unsigned char *p = buf;
XiaokangQian9b2b7712022-05-17 02:57:00 +00009463 size_t server_name_list_len, hostname_len;
9464 const unsigned char *server_name_list_end;
XiaokangQian40a35232022-05-07 09:02:40 +00009465
Gilles Peskine449bd832023-01-11 14:50:10 +01009466 MBEDTLS_SSL_DEBUG_MSG(3, ("parse ServerName extension"));
XiaokangQian40a35232022-05-07 09:02:40 +00009467
Gilles Peskine449bd832023-01-11 14:50:10 +01009468 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
9469 server_name_list_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian40a35232022-05-07 09:02:40 +00009470 p += 2;
9471
Gilles Peskine449bd832023-01-11 14:50:10 +01009472 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, server_name_list_len);
XiaokangQian9b2b7712022-05-17 02:57:00 +00009473 server_name_list_end = p + server_name_list_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01009474 while (p < server_name_list_end) {
9475 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, server_name_list_end, 3);
9476 hostname_len = MBEDTLS_GET_UINT16_BE(p, 1);
9477 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, server_name_list_end,
9478 hostname_len + 3);
XiaokangQian40a35232022-05-07 09:02:40 +00009479
Gilles Peskine449bd832023-01-11 14:50:10 +01009480 if (p[0] == MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME) {
XiaokangQian75fe8c72022-06-15 09:42:45 +00009481 /* sni_name is intended to be used only during the parsing of the
9482 * ClientHello message (it is reset to NULL before the end of
9483 * the message parsing). Thus it is ok to just point to the
9484 * reception buffer and not make a copy of it.
9485 */
XiaokangQianf2a94202022-05-20 06:44:24 +00009486 ssl->handshake->sni_name = p + 3;
9487 ssl->handshake->sni_name_len = hostname_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01009488 if (ssl->conf->f_sni == NULL) {
9489 return 0;
XiaokangQian40a35232022-05-07 09:02:40 +00009490 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009491 ret = ssl->conf->f_sni(ssl->conf->p_sni,
9492 ssl, p + 3, hostname_len);
9493 if (ret != 0) {
9494 MBEDTLS_SSL_DEBUG_RET(1, "ssl_sni_wrapper", ret);
9495 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_UNRECOGNIZED_NAME,
9496 MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME);
9497 return MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME;
9498 }
9499 return 0;
XiaokangQian40a35232022-05-07 09:02:40 +00009500 }
9501
9502 p += hostname_len + 3;
9503 }
9504
Gilles Peskine449bd832023-01-11 14:50:10 +01009505 return 0;
XiaokangQian40a35232022-05-07 09:02:40 +00009506}
9507#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
9508
XiaokangQianacb39922022-06-17 10:18:48 +00009509#if defined(MBEDTLS_SSL_ALPN)
Ronald Cronce7d76e2022-07-08 18:56:49 +02009510MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01009511int mbedtls_ssl_parse_alpn_ext(mbedtls_ssl_context *ssl,
9512 const unsigned char *buf,
9513 const unsigned char *end)
XiaokangQianacb39922022-06-17 10:18:48 +00009514{
9515 const unsigned char *p = buf;
XiaokangQianc7403452022-06-23 03:24:12 +00009516 size_t protocol_name_list_len;
XiaokangQian95d5f542022-06-24 02:29:26 +00009517 const unsigned char *protocol_name_list;
9518 const unsigned char *protocol_name_list_end;
XiaokangQianc7403452022-06-23 03:24:12 +00009519 size_t protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009520
9521 /* If ALPN not configured, just ignore the extension */
Gilles Peskine449bd832023-01-11 14:50:10 +01009522 if (ssl->conf->alpn_list == NULL) {
9523 return 0;
9524 }
XiaokangQianacb39922022-06-17 10:18:48 +00009525
9526 /*
XiaokangQianc7403452022-06-23 03:24:12 +00009527 * RFC7301, section 3.1
9528 * opaque ProtocolName<1..2^8-1>;
XiaokangQianacb39922022-06-17 10:18:48 +00009529 *
XiaokangQianc7403452022-06-23 03:24:12 +00009530 * struct {
9531 * ProtocolName protocol_name_list<2..2^16-1>
9532 * } ProtocolNameList;
XiaokangQianacb39922022-06-17 10:18:48 +00009533 */
9534
XiaokangQianc7403452022-06-23 03:24:12 +00009535 /*
XiaokangQian0b776e22022-06-24 09:04:59 +00009536 * protocol_name_list_len 2 bytes
9537 * protocol_name_len 1 bytes
9538 * protocol_name >=1 byte
XiaokangQianc7403452022-06-23 03:24:12 +00009539 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009540 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 4);
XiaokangQianacb39922022-06-17 10:18:48 +00009541
Gilles Peskine449bd832023-01-11 14:50:10 +01009542 protocol_name_list_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQianacb39922022-06-17 10:18:48 +00009543 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01009544 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, protocol_name_list_len);
XiaokangQian95d5f542022-06-24 02:29:26 +00009545 protocol_name_list = p;
9546 protocol_name_list_end = p + protocol_name_list_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009547
9548 /* Validate peer's list (lengths) */
Gilles Peskine449bd832023-01-11 14:50:10 +01009549 while (p < protocol_name_list_end) {
XiaokangQian95d5f542022-06-24 02:29:26 +00009550 protocol_name_len = *p++;
Gilles Peskine449bd832023-01-11 14:50:10 +01009551 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, protocol_name_list_end,
9552 protocol_name_len);
9553 if (protocol_name_len == 0) {
XiaokangQian95d5f542022-06-24 02:29:26 +00009554 MBEDTLS_SSL_PEND_FATAL_ALERT(
9555 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
Gilles Peskine449bd832023-01-11 14:50:10 +01009556 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
9557 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
XiaokangQian95d5f542022-06-24 02:29:26 +00009558 }
9559
9560 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009561 }
9562
9563 /* Use our order of preference */
Gilles Peskine449bd832023-01-11 14:50:10 +01009564 for (const char **alpn = ssl->conf->alpn_list; *alpn != NULL; alpn++) {
9565 size_t const alpn_len = strlen(*alpn);
XiaokangQian95d5f542022-06-24 02:29:26 +00009566 p = protocol_name_list;
Gilles Peskine449bd832023-01-11 14:50:10 +01009567 while (p < protocol_name_list_end) {
XiaokangQian95d5f542022-06-24 02:29:26 +00009568 protocol_name_len = *p++;
Gilles Peskine449bd832023-01-11 14:50:10 +01009569 if (protocol_name_len == alpn_len &&
9570 memcmp(p, *alpn, alpn_len) == 0) {
XiaokangQianacb39922022-06-17 10:18:48 +00009571 ssl->alpn_chosen = *alpn;
Gilles Peskine449bd832023-01-11 14:50:10 +01009572 return 0;
XiaokangQianacb39922022-06-17 10:18:48 +00009573 }
XiaokangQian95d5f542022-06-24 02:29:26 +00009574
9575 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009576 }
9577 }
9578
XiaokangQian95d5f542022-06-24 02:29:26 +00009579 /* If we get here, no match was found */
XiaokangQianacb39922022-06-17 10:18:48 +00009580 MBEDTLS_SSL_PEND_FATAL_ALERT(
Gilles Peskine449bd832023-01-11 14:50:10 +01009581 MBEDTLS_SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL,
9582 MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL);
9583 return MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL;
XiaokangQianacb39922022-06-17 10:18:48 +00009584}
9585
Gilles Peskine449bd832023-01-11 14:50:10 +01009586int mbedtls_ssl_write_alpn_ext(mbedtls_ssl_context *ssl,
9587 unsigned char *buf,
9588 unsigned char *end,
9589 size_t *out_len)
XiaokangQianacb39922022-06-17 10:18:48 +00009590{
9591 unsigned char *p = buf;
XiaokangQian95d5f542022-06-24 02:29:26 +00009592 size_t protocol_name_len;
XiaokangQianc7403452022-06-23 03:24:12 +00009593 *out_len = 0;
XiaokangQianacb39922022-06-17 10:18:48 +00009594
Gilles Peskine449bd832023-01-11 14:50:10 +01009595 if (ssl->alpn_chosen == NULL) {
9596 return 0;
XiaokangQianacb39922022-06-17 10:18:48 +00009597 }
9598
Gilles Peskine449bd832023-01-11 14:50:10 +01009599 protocol_name_len = strlen(ssl->alpn_chosen);
9600 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 7 + protocol_name_len);
XiaokangQianacb39922022-06-17 10:18:48 +00009601
Gilles Peskine449bd832023-01-11 14:50:10 +01009602 MBEDTLS_SSL_DEBUG_MSG(3, ("server side, adding alpn extension"));
XiaokangQianacb39922022-06-17 10:18:48 +00009603 /*
9604 * 0 . 1 ext identifier
9605 * 2 . 3 ext length
9606 * 4 . 5 protocol list length
9607 * 6 . 6 protocol name length
9608 * 7 . 7+n protocol name
9609 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009610 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_ALPN, p, 0);
XiaokangQianacb39922022-06-17 10:18:48 +00009611
XiaokangQian95d5f542022-06-24 02:29:26 +00009612 *out_len = 7 + protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009613
Gilles Peskine449bd832023-01-11 14:50:10 +01009614 MBEDTLS_PUT_UINT16_BE(protocol_name_len + 3, p, 2);
9615 MBEDTLS_PUT_UINT16_BE(protocol_name_len + 1, p, 4);
XiaokangQian0b776e22022-06-24 09:04:59 +00009616 /* Note: the length of the chosen protocol has been checked to be less
9617 * than 255 bytes in `mbedtls_ssl_conf_alpn_protocols`.
9618 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009619 p[6] = MBEDTLS_BYTE_0(protocol_name_len);
XiaokangQianacb39922022-06-17 10:18:48 +00009620
Gilles Peskine449bd832023-01-11 14:50:10 +01009621 memcpy(p + 7, ssl->alpn_chosen, protocol_name_len);
Jerry Yub95dd362022-11-08 21:19:34 +08009622
9623#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01009624 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_ALPN);
Jerry Yub95dd362022-11-08 21:19:34 +08009625#endif
9626
Gilles Peskine449bd832023-01-11 14:50:10 +01009627 return 0;
XiaokangQianacb39922022-06-17 10:18:48 +00009628}
9629#endif /* MBEDTLS_SSL_ALPN */
9630
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009631#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
Xiaokang Qian03409292022-10-12 02:49:52 +00009632 defined(MBEDTLS_SSL_SESSION_TICKETS) && \
Xiaokang Qianed0620c2022-10-12 06:58:13 +00009633 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \
Xiaokang Qianed3afcd2022-10-12 08:31:11 +00009634 defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01009635int mbedtls_ssl_session_set_hostname(mbedtls_ssl_session *session,
9636 const char *hostname)
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009637{
9638 /* Initialize to suppress unnecessary compiler warning */
9639 size_t hostname_len = 0;
9640
9641 /* Check if new hostname is valid before
9642 * making any change to current one */
Gilles Peskine449bd832023-01-11 14:50:10 +01009643 if (hostname != NULL) {
9644 hostname_len = strlen(hostname);
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009645
Gilles Peskine449bd832023-01-11 14:50:10 +01009646 if (hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN) {
9647 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9648 }
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009649 }
9650
9651 /* Now it's clear that we will overwrite the old hostname,
9652 * so we can free it safely */
Gilles Peskine449bd832023-01-11 14:50:10 +01009653 if (session->hostname != NULL) {
9654 mbedtls_platform_zeroize(session->hostname,
9655 strlen(session->hostname));
9656 mbedtls_free(session->hostname);
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009657 }
9658
9659 /* Passing NULL as hostname shall clear the old one */
Gilles Peskine449bd832023-01-11 14:50:10 +01009660 if (hostname == NULL) {
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009661 session->hostname = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01009662 } else {
9663 session->hostname = mbedtls_calloc(1, hostname_len + 1);
9664 if (session->hostname == NULL) {
9665 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
9666 }
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009667
Gilles Peskine449bd832023-01-11 14:50:10 +01009668 memcpy(session->hostname, hostname, hostname_len);
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009669 }
9670
Gilles Peskine449bd832023-01-11 14:50:10 +01009671 return 0;
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009672}
Xiaokang Qian03409292022-10-12 02:49:52 +00009673#endif /* MBEDTLS_SSL_PROTO_TLS1_3 &&
9674 MBEDTLS_SSL_SESSION_TICKETS &&
Xiaokang Qianed0620c2022-10-12 06:58:13 +00009675 MBEDTLS_SSL_SERVER_NAME_INDICATION &&
Xiaokang Qianed3afcd2022-10-12 08:31:11 +00009676 MBEDTLS_SSL_CLI_C */
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009677
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009678#endif /* MBEDTLS_SSL_TLS_C */