blob: 4a351f320bac3ba9ab51c4a34717fb97f873830c [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
Przemek Stekiel18cd6c92023-03-06 15:40:35 +010064/* JPAKE user/peer ids. */
65#define JPAKE_SERVER_ID "server"
66#define JPAKE_CLIENT_ID "client"
67
Ronald Cronad8c17b2022-06-10 17:18:09 +020068#if defined(MBEDTLS_TEST_HOOKS)
69static mbedtls_ssl_chk_buf_ptr_args chk_buf_ptr_fail_args;
70
71void mbedtls_ssl_set_chk_buf_ptr_fail_args(
Gilles Peskine449bd832023-01-11 14:50:10 +010072 const uint8_t *cur, const uint8_t *end, size_t need)
Ronald Cronad8c17b2022-06-10 17:18:09 +020073{
74 chk_buf_ptr_fail_args.cur = cur;
75 chk_buf_ptr_fail_args.end = end;
76 chk_buf_ptr_fail_args.need = need;
77}
78
Gilles Peskine449bd832023-01-11 14:50:10 +010079void mbedtls_ssl_reset_chk_buf_ptr_fail_args(void)
Ronald Cronad8c17b2022-06-10 17:18:09 +020080{
Gilles Peskine449bd832023-01-11 14:50:10 +010081 memset(&chk_buf_ptr_fail_args, 0, sizeof(chk_buf_ptr_fail_args));
Ronald Cronad8c17b2022-06-10 17:18:09 +020082}
83
Gilles Peskine449bd832023-01-11 14:50:10 +010084int mbedtls_ssl_cmp_chk_buf_ptr_fail_args(mbedtls_ssl_chk_buf_ptr_args *args)
Ronald Cronad8c17b2022-06-10 17:18:09 +020085{
Gilles Peskine449bd832023-01-11 14:50:10 +010086 return (chk_buf_ptr_fail_args.cur != args->cur) ||
87 (chk_buf_ptr_fail_args.end != args->end) ||
88 (chk_buf_ptr_fail_args.need != args->need);
Ronald Cronad8c17b2022-06-10 17:18:09 +020089}
90#endif /* MBEDTLS_TEST_HOOKS */
91
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020092#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +010093
Hanno Beckera0e20d02019-05-15 14:03:01 +010094#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf8542cf2019-04-09 15:22:03 +010095/* Top-level Connection ID API */
96
Gilles Peskine449bd832023-01-11 14:50:10 +010097int mbedtls_ssl_conf_cid(mbedtls_ssl_config *conf,
98 size_t len,
99 int ignore_other_cid)
Hanno Beckerad4a1372019-05-03 13:06:44 +0100100{
Gilles Peskine449bd832023-01-11 14:50:10 +0100101 if (len > MBEDTLS_SSL_CID_IN_LEN_MAX) {
102 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
103 }
Hanno Beckerad4a1372019-05-03 13:06:44 +0100104
Gilles Peskine449bd832023-01-11 14:50:10 +0100105 if (ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_FAIL &&
106 ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE) {
107 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Becker611ac772019-05-14 11:45:26 +0100108 }
109
110 conf->ignore_unexpected_cid = ignore_other_cid;
Hanno Beckerad4a1372019-05-03 13:06:44 +0100111 conf->cid_len = len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100112 return 0;
Hanno Beckerad4a1372019-05-03 13:06:44 +0100113}
114
Gilles Peskine449bd832023-01-11 14:50:10 +0100115int mbedtls_ssl_set_cid(mbedtls_ssl_context *ssl,
116 int enable,
117 unsigned char const *own_cid,
118 size_t own_cid_len)
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100119{
Gilles Peskine449bd832023-01-11 14:50:10 +0100120 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
121 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
122 }
Hanno Becker76a79ab2019-05-03 14:38:32 +0100123
Hanno Beckerca092242019-04-25 16:01:49 +0100124 ssl->negotiate_cid = enable;
Gilles Peskine449bd832023-01-11 14:50:10 +0100125 if (enable == MBEDTLS_SSL_CID_DISABLED) {
126 MBEDTLS_SSL_DEBUG_MSG(3, ("Disable use of CID extension."));
127 return 0;
Hanno Beckerca092242019-04-25 16:01:49 +0100128 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100129 MBEDTLS_SSL_DEBUG_MSG(3, ("Enable use of CID extension."));
130 MBEDTLS_SSL_DEBUG_BUF(3, "Own CID", own_cid, own_cid_len);
Hanno Beckerca092242019-04-25 16:01:49 +0100131
Gilles Peskine449bd832023-01-11 14:50:10 +0100132 if (own_cid_len != ssl->conf->cid_len) {
133 MBEDTLS_SSL_DEBUG_MSG(3, ("CID length %u does not match CID length %u in config",
134 (unsigned) own_cid_len,
135 (unsigned) ssl->conf->cid_len));
136 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Beckerca092242019-04-25 16:01:49 +0100137 }
138
Gilles Peskine449bd832023-01-11 14:50:10 +0100139 memcpy(ssl->own_cid, own_cid, own_cid_len);
Hanno Beckerb7ee0cf2019-04-30 14:07:31 +0100140 /* Truncation is not an issue here because
141 * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */
142 ssl->own_cid_len = (uint8_t) own_cid_len;
Hanno Beckerca092242019-04-25 16:01:49 +0100143
Gilles Peskine449bd832023-01-11 14:50:10 +0100144 return 0;
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100145}
146
Gilles Peskine449bd832023-01-11 14:50:10 +0100147int mbedtls_ssl_get_own_cid(mbedtls_ssl_context *ssl,
148 int *enabled,
149 unsigned char own_cid[MBEDTLS_SSL_CID_OUT_LEN_MAX],
150 size_t *own_cid_len)
Paul Elliott0113cf12022-03-11 20:26:47 +0000151{
152 *enabled = MBEDTLS_SSL_CID_DISABLED;
153
Gilles Peskine449bd832023-01-11 14:50:10 +0100154 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
155 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
156 }
Paul Elliott0113cf12022-03-11 20:26:47 +0000157
158 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID length is
159 * zero as this is indistinguishable from not requesting to use
160 * the CID extension. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100161 if (ssl->own_cid_len == 0 || ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED) {
162 return 0;
163 }
Paul Elliott0113cf12022-03-11 20:26:47 +0000164
Gilles Peskine449bd832023-01-11 14:50:10 +0100165 if (own_cid_len != NULL) {
Paul Elliott0113cf12022-03-11 20:26:47 +0000166 *own_cid_len = ssl->own_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100167 if (own_cid != NULL) {
168 memcpy(own_cid, ssl->own_cid, ssl->own_cid_len);
169 }
Paul Elliott0113cf12022-03-11 20:26:47 +0000170 }
171
172 *enabled = MBEDTLS_SSL_CID_ENABLED;
173
Gilles Peskine449bd832023-01-11 14:50:10 +0100174 return 0;
Paul Elliott0113cf12022-03-11 20:26:47 +0000175}
176
Gilles Peskine449bd832023-01-11 14:50:10 +0100177int mbedtls_ssl_get_peer_cid(mbedtls_ssl_context *ssl,
178 int *enabled,
179 unsigned char peer_cid[MBEDTLS_SSL_CID_OUT_LEN_MAX],
180 size_t *peer_cid_len)
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100181{
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100182 *enabled = MBEDTLS_SSL_CID_DISABLED;
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100183
Gilles Peskine449bd832023-01-11 14:50:10 +0100184 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
185 mbedtls_ssl_is_handshake_over(ssl) == 0) {
186 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Becker76a79ab2019-05-03 14:38:32 +0100187 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100188
Hanno Beckerc5f24222019-05-03 12:54:52 +0100189 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions
190 * were used, but client and server requested the empty CID.
191 * This is indistinguishable from not using the CID extension
192 * in the first place. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100193 if (ssl->transform_in->in_cid_len == 0 &&
194 ssl->transform_in->out_cid_len == 0) {
195 return 0;
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100196 }
197
Gilles Peskine449bd832023-01-11 14:50:10 +0100198 if (peer_cid_len != NULL) {
Hanno Becker615ef172019-05-22 16:50:35 +0100199 *peer_cid_len = ssl->transform_in->out_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100200 if (peer_cid != NULL) {
201 memcpy(peer_cid, ssl->transform_in->out_cid,
202 ssl->transform_in->out_cid_len);
Hanno Becker615ef172019-05-22 16:50:35 +0100203 }
204 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100205
206 *enabled = MBEDTLS_SSL_CID_ENABLED;
207
Gilles Peskine449bd832023-01-11 14:50:10 +0100208 return 0;
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100209}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100210#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200212#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200213
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200214#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200215/*
216 * Convert max_fragment_length codes to length.
217 * RFC 6066 says:
218 * enum{
219 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
220 * } MaxFragmentLength;
221 * and we add 0 -> extension unused
222 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100223static unsigned int ssl_mfl_code_to_length(int mfl)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200224{
Gilles Peskine449bd832023-01-11 14:50:10 +0100225 switch (mfl) {
226 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
227 return MBEDTLS_TLS_EXT_ADV_CONTENT_LEN;
228 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
229 return 512;
230 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
231 return 1024;
232 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
233 return 2048;
234 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
235 return 4096;
236 default:
237 return MBEDTLS_TLS_EXT_ADV_CONTENT_LEN;
Angus Grattond8213d02016-05-25 20:56:48 +1000238 }
239}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200240#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200241
Gilles Peskine449bd832023-01-11 14:50:10 +0100242int mbedtls_ssl_session_copy(mbedtls_ssl_session *dst,
243 const mbedtls_ssl_session *src)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200244{
Gilles Peskine449bd832023-01-11 14:50:10 +0100245 mbedtls_ssl_session_free(dst);
246 memcpy(dst, src, sizeof(mbedtls_ssl_session));
吴敬辉0b716112021-11-29 10:46:35 +0800247#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
248 dst->ticket = NULL;
Xiaokang Qian87306442022-10-12 09:47:38 +0000249#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
250 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaokang Qian126bf8e2022-10-13 02:22:40 +0000251 dst->hostname = NULL;
吴敬辉0b716112021-11-29 10:46:35 +0800252#endif
Xiaokang Qian87306442022-10-12 09:47:38 +0000253#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
吴敬辉0b716112021-11-29 10:46:35 +0800254
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200255#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker6d1986e2019-02-07 12:27:42 +0000256
257#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100258 if (src->peer_cert != NULL) {
Janos Follath865b3eb2019-12-16 11:46:15 +0000259 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker2292d1f2013-09-15 17:06:49 +0200260
Gilles Peskine449bd832023-01-11 14:50:10 +0100261 dst->peer_cert = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
262 if (dst->peer_cert == NULL) {
263 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
264 }
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200265
Gilles Peskine449bd832023-01-11 14:50:10 +0100266 mbedtls_x509_crt_init(dst->peer_cert);
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200267
Gilles Peskine449bd832023-01-11 14:50:10 +0100268 if ((ret = mbedtls_x509_crt_parse_der(dst->peer_cert, src->peer_cert->raw.p,
269 src->peer_cert->raw.len)) != 0) {
270 mbedtls_free(dst->peer_cert);
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200271 dst->peer_cert = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100272 return ret;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200273 }
274 }
Hanno Becker6d1986e2019-02-07 12:27:42 +0000275#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100276 if (src->peer_cert_digest != NULL) {
Hanno Becker9198ad12019-02-05 17:00:50 +0000277 dst->peer_cert_digest =
Gilles Peskine449bd832023-01-11 14:50:10 +0100278 mbedtls_calloc(1, src->peer_cert_digest_len);
279 if (dst->peer_cert_digest == NULL) {
280 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
281 }
Hanno Becker9198ad12019-02-05 17:00:50 +0000282
Gilles Peskine449bd832023-01-11 14:50:10 +0100283 memcpy(dst->peer_cert_digest, src->peer_cert_digest,
284 src->peer_cert_digest_len);
Hanno Becker9198ad12019-02-05 17:00:50 +0000285 dst->peer_cert_digest_type = src->peer_cert_digest_type;
Hanno Beckeraccc5992019-02-25 10:06:59 +0000286 dst->peer_cert_digest_len = src->peer_cert_digest_len;
Hanno Becker9198ad12019-02-05 17:00:50 +0000287 }
288#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
289
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200290#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200291
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200292#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100293 if (src->ticket != NULL) {
294 dst->ticket = mbedtls_calloc(1, src->ticket_len);
295 if (dst->ticket == NULL) {
296 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
297 }
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200298
Gilles Peskine449bd832023-01-11 14:50:10 +0100299 memcpy(dst->ticket, src->ticket, src->ticket_len);
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200300 }
Xiaokang Qian126bf8e2022-10-13 02:22:40 +0000301
302#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
303 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +0100304 if (src->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Xiaokang Qian126bf8e2022-10-13 02:22:40 +0000305 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100306 ret = mbedtls_ssl_session_set_hostname(dst, src->hostname);
307 if (ret != 0) {
308 return ret;
309 }
Xiaokang Qian126bf8e2022-10-13 02:22:40 +0000310 }
311#endif /* MBEDTLS_SSL_PROTO_TLS1_3 &&
312 MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200313#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200314
Gilles Peskine449bd832023-01-11 14:50:10 +0100315 return 0;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200316}
317
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500318#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200319MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100320static int resize_buffer(unsigned char **buffer, size_t len_new, size_t *len_old)
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500321{
Gilles Peskine449bd832023-01-11 14:50:10 +0100322 unsigned char *resized_buffer = mbedtls_calloc(1, len_new);
323 if (resized_buffer == NULL) {
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500324 return -1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100325 }
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500326
327 /* We want to copy len_new bytes when downsizing the buffer, and
328 * len_old bytes when upsizing, so we choose the smaller of two sizes,
329 * to fit one buffer into another. Size checks, ensuring that no data is
330 * lost, are done outside of this function. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100331 memcpy(resized_buffer, *buffer,
332 (len_new < *len_old) ? len_new : *len_old);
333 mbedtls_platform_zeroize(*buffer, *len_old);
334 mbedtls_free(*buffer);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500335
336 *buffer = resized_buffer;
337 *len_old = len_new;
338
339 return 0;
340}
Andrzej Kurek4a063792020-10-21 15:08:44 +0200341
Gilles Peskine449bd832023-01-11 14:50:10 +0100342static void handle_buffer_resizing(mbedtls_ssl_context *ssl, int downsizing,
343 size_t in_buf_new_len,
344 size_t out_buf_new_len)
Andrzej Kurek4a063792020-10-21 15:08:44 +0200345{
346 int modified = 0;
347 size_t written_in = 0, iv_offset_in = 0, len_offset_in = 0;
348 size_t written_out = 0, iv_offset_out = 0, len_offset_out = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100349 if (ssl->in_buf != NULL) {
Andrzej Kurek4a063792020-10-21 15:08:44 +0200350 written_in = ssl->in_msg - ssl->in_buf;
351 iv_offset_in = ssl->in_iv - ssl->in_buf;
352 len_offset_in = ssl->in_len - ssl->in_buf;
Gilles Peskine449bd832023-01-11 14:50:10 +0100353 if (downsizing ?
Andrzej Kurek4a063792020-10-21 15:08:44 +0200354 ssl->in_buf_len > in_buf_new_len && ssl->in_left < in_buf_new_len :
Gilles Peskine449bd832023-01-11 14:50:10 +0100355 ssl->in_buf_len < in_buf_new_len) {
356 if (resize_buffer(&ssl->in_buf, in_buf_new_len, &ssl->in_buf_len) != 0) {
357 MBEDTLS_SSL_DEBUG_MSG(1, ("input buffer resizing failed - out of memory"));
358 } else {
359 MBEDTLS_SSL_DEBUG_MSG(2, ("Reallocating in_buf to %" MBEDTLS_PRINTF_SIZET,
360 in_buf_new_len));
Andrzej Kurek4a063792020-10-21 15:08:44 +0200361 modified = 1;
362 }
363 }
364 }
365
Gilles Peskine449bd832023-01-11 14:50:10 +0100366 if (ssl->out_buf != NULL) {
Andrzej Kurek4a063792020-10-21 15:08:44 +0200367 written_out = ssl->out_msg - ssl->out_buf;
368 iv_offset_out = ssl->out_iv - ssl->out_buf;
369 len_offset_out = ssl->out_len - ssl->out_buf;
Gilles Peskine449bd832023-01-11 14:50:10 +0100370 if (downsizing ?
Andrzej Kurek4a063792020-10-21 15:08:44 +0200371 ssl->out_buf_len > out_buf_new_len && ssl->out_left < out_buf_new_len :
Gilles Peskine449bd832023-01-11 14:50:10 +0100372 ssl->out_buf_len < out_buf_new_len) {
373 if (resize_buffer(&ssl->out_buf, out_buf_new_len, &ssl->out_buf_len) != 0) {
374 MBEDTLS_SSL_DEBUG_MSG(1, ("output buffer resizing failed - out of memory"));
375 } else {
376 MBEDTLS_SSL_DEBUG_MSG(2, ("Reallocating out_buf to %" MBEDTLS_PRINTF_SIZET,
377 out_buf_new_len));
Andrzej Kurek4a063792020-10-21 15:08:44 +0200378 modified = 1;
379 }
380 }
381 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100382 if (modified) {
Andrzej Kurek4a063792020-10-21 15:08:44 +0200383 /* Update pointers here to avoid doing it twice. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100384 mbedtls_ssl_reset_in_out_pointers(ssl);
Andrzej Kurek4a063792020-10-21 15:08:44 +0200385 /* Fields below might not be properly updated with record
386 * splitting or with CID, so they are manually updated here. */
387 ssl->out_msg = ssl->out_buf + written_out;
388 ssl->out_len = ssl->out_buf + len_offset_out;
389 ssl->out_iv = ssl->out_buf + iv_offset_out;
390
391 ssl->in_msg = ssl->in_buf + written_in;
392 ssl->in_len = ssl->in_buf + len_offset_in;
393 ssl->in_iv = ssl->in_buf + iv_offset_in;
394 }
395}
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500396#endif /* MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH */
397
Jerry Yudb8c48a2022-01-27 14:54:54 +0800398#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yued14c932022-02-17 13:40:45 +0800399
400#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Gilles Peskine449bd832023-01-11 14:50:10 +0100401typedef int (*tls_prf_fn)(const unsigned char *secret, size_t slen,
402 const char *label,
403 const unsigned char *random, size_t rlen,
404 unsigned char *dstbuf, size_t dlen);
Jerry Yued14c932022-02-17 13:40:45 +0800405
Gilles Peskine449bd832023-01-11 14:50:10 +0100406static tls_prf_fn ssl_tls12prf_from_cs(int ciphersuite_id);
Jerry Yued14c932022-02-17 13:40:45 +0800407
408#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
409
410/* Type for the TLS PRF */
411typedef int ssl_tls_prf_t(const unsigned char *, size_t, const char *,
412 const unsigned char *, size_t,
413 unsigned char *, size_t);
414
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200415MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100416static int ssl_tls12_populate_transform(mbedtls_ssl_transform *transform,
417 int ciphersuite,
418 const unsigned char master[48],
Neil Armstrongf2c82f02022-04-05 11:16:53 +0200419#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +0100420 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +0200421#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +0100422 ssl_tls_prf_t tls_prf,
423 const unsigned char randbytes[64],
424 mbedtls_ssl_protocol_version tls_version,
425 unsigned endpoint,
426 const mbedtls_ssl_context *ssl);
Jerry Yued14c932022-02-17 13:40:45 +0800427
Andrzej Kurek25f27152022-08-17 16:09:31 -0400428#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200429MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100430static int tls_prf_sha256(const unsigned char *secret, size_t slen,
Ron Eldor51d3ab52019-05-12 14:54:30 +0300431 const char *label,
432 const unsigned char *random, size_t rlen,
Gilles Peskine449bd832023-01-11 14:50:10 +0100433 unsigned char *dstbuf, size_t dlen);
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100434static int ssl_calc_verify_tls_sha256(const mbedtls_ssl_context *, unsigned char *, size_t *);
435static int ssl_calc_finished_tls_sha256(mbedtls_ssl_context *, unsigned char *, int);
Gilles Peskine449bd832023-01-11 14:50:10 +0100436
437#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
438
439#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
440MBEDTLS_CHECK_RETURN_CRITICAL
441static int tls_prf_sha384(const unsigned char *secret, size_t slen,
442 const char *label,
443 const unsigned char *random, size_t rlen,
444 unsigned char *dstbuf, size_t dlen);
445
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100446static int ssl_calc_verify_tls_sha384(const mbedtls_ssl_context *, unsigned char *, size_t *);
447static int ssl_calc_finished_tls_sha384(mbedtls_ssl_context *, unsigned char *, int);
Gilles Peskine449bd832023-01-11 14:50:10 +0100448#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
449
450static size_t ssl_tls12_session_save(const mbedtls_ssl_session *session,
451 unsigned char *buf,
452 size_t buf_len);
453
454MBEDTLS_CHECK_RETURN_CRITICAL
455static int ssl_tls12_session_load(mbedtls_ssl_session *session,
456 const unsigned char *buf,
457 size_t len);
458#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
459
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100460static int ssl_update_checksum_start(mbedtls_ssl_context *, const unsigned char *, size_t);
Gilles Peskine449bd832023-01-11 14:50:10 +0100461
462#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100463static int ssl_update_checksum_sha256(mbedtls_ssl_context *, const unsigned char *, size_t);
Gilles Peskine449bd832023-01-11 14:50:10 +0100464#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
465
466#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100467static int ssl_update_checksum_sha384(mbedtls_ssl_context *, const unsigned char *, size_t);
Gilles Peskine449bd832023-01-11 14:50:10 +0100468#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
469
470int mbedtls_ssl_tls_prf(const mbedtls_tls_prf_types prf,
471 const unsigned char *secret, size_t slen,
472 const char *label,
473 const unsigned char *random, size_t rlen,
474 unsigned char *dstbuf, size_t dlen)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300475{
476 mbedtls_ssl_tls_prf_cb *tls_prf = NULL;
477
Gilles Peskine449bd832023-01-11 14:50:10 +0100478 switch (prf) {
Ron Eldord2f25f72019-05-15 14:54:22 +0300479#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek25f27152022-08-17 16:09:31 -0400480#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300481 case MBEDTLS_SSL_TLS_PRF_SHA384:
482 tls_prf = tls_prf_sha384;
Gilles Peskine449bd832023-01-11 14:50:10 +0100483 break;
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400484#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -0400485#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300486 case MBEDTLS_SSL_TLS_PRF_SHA256:
487 tls_prf = tls_prf_sha256;
Gilles Peskine449bd832023-01-11 14:50:10 +0100488 break;
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400489#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Ron Eldord2f25f72019-05-15 14:54:22 +0300490#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100491 default:
492 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Ron Eldor51d3ab52019-05-12 14:54:30 +0300493 }
494
Gilles Peskine449bd832023-01-11 14:50:10 +0100495 return tls_prf(secret, slen, label, random, rlen, dstbuf, dlen);
Ron Eldor51d3ab52019-05-12 14:54:30 +0300496}
497
Jerry Yuc73c6182022-02-08 20:29:25 +0800498#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100499static void ssl_clear_peer_cert(mbedtls_ssl_session *session)
Jerry Yuc73c6182022-02-08 20:29:25 +0800500{
501#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100502 if (session->peer_cert != NULL) {
503 mbedtls_x509_crt_free(session->peer_cert);
504 mbedtls_free(session->peer_cert);
Jerry Yuc73c6182022-02-08 20:29:25 +0800505 session->peer_cert = NULL;
506 }
507#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100508 if (session->peer_cert_digest != NULL) {
Jerry Yuc73c6182022-02-08 20:29:25 +0800509 /* Zeroization is not necessary. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100510 mbedtls_free(session->peer_cert_digest);
Jerry Yuc73c6182022-02-08 20:29:25 +0800511 session->peer_cert_digest = NULL;
512 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
513 session->peer_cert_digest_len = 0;
514 }
515#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
516}
517#endif /* MBEDTLS_X509_CRT_PARSE_C */
518
Gilles Peskine449bd832023-01-11 14:50:10 +0100519uint32_t mbedtls_ssl_get_extension_id(unsigned int extension_type)
Jerry Yu7a485c12022-10-31 13:08:18 +0800520{
Gilles Peskine449bd832023-01-11 14:50:10 +0100521 switch (extension_type) {
Jerry Yu7a485c12022-10-31 13:08:18 +0800522 case MBEDTLS_TLS_EXT_SERVERNAME:
Gilles Peskine449bd832023-01-11 14:50:10 +0100523 return MBEDTLS_SSL_EXT_ID_SERVERNAME;
Jerry Yu7a485c12022-10-31 13:08:18 +0800524
525 case MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100526 return MBEDTLS_SSL_EXT_ID_MAX_FRAGMENT_LENGTH;
Jerry Yu7a485c12022-10-31 13:08:18 +0800527
528 case MBEDTLS_TLS_EXT_STATUS_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +0100529 return MBEDTLS_SSL_EXT_ID_STATUS_REQUEST;
Jerry Yu7a485c12022-10-31 13:08:18 +0800530
531 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
Gilles Peskine449bd832023-01-11 14:50:10 +0100532 return MBEDTLS_SSL_EXT_ID_SUPPORTED_GROUPS;
Jerry Yu7a485c12022-10-31 13:08:18 +0800533
534 case MBEDTLS_TLS_EXT_SIG_ALG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100535 return MBEDTLS_SSL_EXT_ID_SIG_ALG;
Jerry Yu7a485c12022-10-31 13:08:18 +0800536
537 case MBEDTLS_TLS_EXT_USE_SRTP:
Gilles Peskine449bd832023-01-11 14:50:10 +0100538 return MBEDTLS_SSL_EXT_ID_USE_SRTP;
Jerry Yu7a485c12022-10-31 13:08:18 +0800539
540 case MBEDTLS_TLS_EXT_HEARTBEAT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100541 return MBEDTLS_SSL_EXT_ID_HEARTBEAT;
Jerry Yu7a485c12022-10-31 13:08:18 +0800542
543 case MBEDTLS_TLS_EXT_ALPN:
Gilles Peskine449bd832023-01-11 14:50:10 +0100544 return MBEDTLS_SSL_EXT_ID_ALPN;
Jerry Yu7a485c12022-10-31 13:08:18 +0800545
546 case MBEDTLS_TLS_EXT_SCT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100547 return MBEDTLS_SSL_EXT_ID_SCT;
Jerry Yu7a485c12022-10-31 13:08:18 +0800548
549 case MBEDTLS_TLS_EXT_CLI_CERT_TYPE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100550 return MBEDTLS_SSL_EXT_ID_CLI_CERT_TYPE;
Jerry Yu7a485c12022-10-31 13:08:18 +0800551
552 case MBEDTLS_TLS_EXT_SERV_CERT_TYPE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100553 return MBEDTLS_SSL_EXT_ID_SERV_CERT_TYPE;
Jerry Yu7a485c12022-10-31 13:08:18 +0800554
555 case MBEDTLS_TLS_EXT_PADDING:
Gilles Peskine449bd832023-01-11 14:50:10 +0100556 return MBEDTLS_SSL_EXT_ID_PADDING;
Jerry Yu7a485c12022-10-31 13:08:18 +0800557
558 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100559 return MBEDTLS_SSL_EXT_ID_PRE_SHARED_KEY;
Jerry Yu7a485c12022-10-31 13:08:18 +0800560
561 case MBEDTLS_TLS_EXT_EARLY_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100562 return MBEDTLS_SSL_EXT_ID_EARLY_DATA;
Jerry Yu7a485c12022-10-31 13:08:18 +0800563
564 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
Gilles Peskine449bd832023-01-11 14:50:10 +0100565 return MBEDTLS_SSL_EXT_ID_SUPPORTED_VERSIONS;
Jerry Yu7a485c12022-10-31 13:08:18 +0800566
567 case MBEDTLS_TLS_EXT_COOKIE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100568 return MBEDTLS_SSL_EXT_ID_COOKIE;
Jerry Yu7a485c12022-10-31 13:08:18 +0800569
570 case MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES:
Gilles Peskine449bd832023-01-11 14:50:10 +0100571 return MBEDTLS_SSL_EXT_ID_PSK_KEY_EXCHANGE_MODES;
Jerry Yu7a485c12022-10-31 13:08:18 +0800572
573 case MBEDTLS_TLS_EXT_CERT_AUTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100574 return MBEDTLS_SSL_EXT_ID_CERT_AUTH;
Jerry Yu7a485c12022-10-31 13:08:18 +0800575
576 case MBEDTLS_TLS_EXT_OID_FILTERS:
Gilles Peskine449bd832023-01-11 14:50:10 +0100577 return MBEDTLS_SSL_EXT_ID_OID_FILTERS;
Jerry Yu7a485c12022-10-31 13:08:18 +0800578
579 case MBEDTLS_TLS_EXT_POST_HANDSHAKE_AUTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100580 return MBEDTLS_SSL_EXT_ID_POST_HANDSHAKE_AUTH;
Jerry Yu7a485c12022-10-31 13:08:18 +0800581
582 case MBEDTLS_TLS_EXT_SIG_ALG_CERT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100583 return MBEDTLS_SSL_EXT_ID_SIG_ALG_CERT;
Jerry Yu7a485c12022-10-31 13:08:18 +0800584
585 case MBEDTLS_TLS_EXT_KEY_SHARE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100586 return MBEDTLS_SSL_EXT_ID_KEY_SHARE;
Jerry Yu7a485c12022-10-31 13:08:18 +0800587
588 case MBEDTLS_TLS_EXT_TRUNCATED_HMAC:
Gilles Peskine449bd832023-01-11 14:50:10 +0100589 return MBEDTLS_SSL_EXT_ID_TRUNCATED_HMAC;
Jerry Yu7a485c12022-10-31 13:08:18 +0800590
591 case MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS:
Gilles Peskine449bd832023-01-11 14:50:10 +0100592 return MBEDTLS_SSL_EXT_ID_SUPPORTED_POINT_FORMATS;
Jerry Yu7a485c12022-10-31 13:08:18 +0800593
594 case MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC:
Gilles Peskine449bd832023-01-11 14:50:10 +0100595 return MBEDTLS_SSL_EXT_ID_ENCRYPT_THEN_MAC;
Jerry Yu7a485c12022-10-31 13:08:18 +0800596
597 case MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +0100598 return MBEDTLS_SSL_EXT_ID_EXTENDED_MASTER_SECRET;
Jerry Yu7a485c12022-10-31 13:08:18 +0800599
600 case MBEDTLS_TLS_EXT_SESSION_TICKET:
Gilles Peskine449bd832023-01-11 14:50:10 +0100601 return MBEDTLS_SSL_EXT_ID_SESSION_TICKET;
Jerry Yu7a485c12022-10-31 13:08:18 +0800602
603 }
604
Gilles Peskine449bd832023-01-11 14:50:10 +0100605 return MBEDTLS_SSL_EXT_ID_UNRECOGNIZED;
Jerry Yu7a485c12022-10-31 13:08:18 +0800606}
607
Gilles Peskine449bd832023-01-11 14:50:10 +0100608uint32_t mbedtls_ssl_get_extension_mask(unsigned int extension_type)
Jerry Yu7a485c12022-10-31 13:08:18 +0800609{
Gilles Peskine449bd832023-01-11 14:50:10 +0100610 return 1 << mbedtls_ssl_get_extension_id(extension_type);
Jerry Yu7a485c12022-10-31 13:08:18 +0800611}
612
Jerry Yud25cab02022-10-31 12:48:30 +0800613#if defined(MBEDTLS_DEBUG_C)
614static const char *extension_name_table[] = {
Jerry Yuea52ed92022-11-08 21:01:17 +0800615 [MBEDTLS_SSL_EXT_ID_UNRECOGNIZED] = "unrecognized",
Jerry Yud25cab02022-10-31 12:48:30 +0800616 [MBEDTLS_SSL_EXT_ID_SERVERNAME] = "server_name",
617 [MBEDTLS_SSL_EXT_ID_MAX_FRAGMENT_LENGTH] = "max_fragment_length",
618 [MBEDTLS_SSL_EXT_ID_STATUS_REQUEST] = "status_request",
619 [MBEDTLS_SSL_EXT_ID_SUPPORTED_GROUPS] = "supported_groups",
620 [MBEDTLS_SSL_EXT_ID_SIG_ALG] = "signature_algorithms",
621 [MBEDTLS_SSL_EXT_ID_USE_SRTP] = "use_srtp",
622 [MBEDTLS_SSL_EXT_ID_HEARTBEAT] = "heartbeat",
623 [MBEDTLS_SSL_EXT_ID_ALPN] = "application_layer_protocol_negotiation",
624 [MBEDTLS_SSL_EXT_ID_SCT] = "signed_certificate_timestamp",
625 [MBEDTLS_SSL_EXT_ID_CLI_CERT_TYPE] = "client_certificate_type",
626 [MBEDTLS_SSL_EXT_ID_SERV_CERT_TYPE] = "server_certificate_type",
627 [MBEDTLS_SSL_EXT_ID_PADDING] = "padding",
628 [MBEDTLS_SSL_EXT_ID_PRE_SHARED_KEY] = "pre_shared_key",
629 [MBEDTLS_SSL_EXT_ID_EARLY_DATA] = "early_data",
630 [MBEDTLS_SSL_EXT_ID_SUPPORTED_VERSIONS] = "supported_versions",
631 [MBEDTLS_SSL_EXT_ID_COOKIE] = "cookie",
632 [MBEDTLS_SSL_EXT_ID_PSK_KEY_EXCHANGE_MODES] = "psk_key_exchange_modes",
633 [MBEDTLS_SSL_EXT_ID_CERT_AUTH] = "certificate_authorities",
634 [MBEDTLS_SSL_EXT_ID_OID_FILTERS] = "oid_filters",
635 [MBEDTLS_SSL_EXT_ID_POST_HANDSHAKE_AUTH] = "post_handshake_auth",
636 [MBEDTLS_SSL_EXT_ID_SIG_ALG_CERT] = "signature_algorithms_cert",
637 [MBEDTLS_SSL_EXT_ID_KEY_SHARE] = "key_share",
638 [MBEDTLS_SSL_EXT_ID_TRUNCATED_HMAC] = "truncated_hmac",
639 [MBEDTLS_SSL_EXT_ID_SUPPORTED_POINT_FORMATS] = "supported_point_formats",
640 [MBEDTLS_SSL_EXT_ID_ENCRYPT_THEN_MAC] = "encrypt_then_mac",
641 [MBEDTLS_SSL_EXT_ID_EXTENDED_MASTER_SECRET] = "extended_master_secret",
642 [MBEDTLS_SSL_EXT_ID_SESSION_TICKET] = "session_ticket"
643};
644
Gilles Peskine449bd832023-01-11 14:50:10 +0100645static unsigned int extension_type_table[] = {
Jerry Yud25cab02022-10-31 12:48:30 +0800646 [MBEDTLS_SSL_EXT_ID_UNRECOGNIZED] = 0xff,
647 [MBEDTLS_SSL_EXT_ID_SERVERNAME] = MBEDTLS_TLS_EXT_SERVERNAME,
648 [MBEDTLS_SSL_EXT_ID_MAX_FRAGMENT_LENGTH] = MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH,
649 [MBEDTLS_SSL_EXT_ID_STATUS_REQUEST] = MBEDTLS_TLS_EXT_STATUS_REQUEST,
650 [MBEDTLS_SSL_EXT_ID_SUPPORTED_GROUPS] = MBEDTLS_TLS_EXT_SUPPORTED_GROUPS,
651 [MBEDTLS_SSL_EXT_ID_SIG_ALG] = MBEDTLS_TLS_EXT_SIG_ALG,
652 [MBEDTLS_SSL_EXT_ID_USE_SRTP] = MBEDTLS_TLS_EXT_USE_SRTP,
653 [MBEDTLS_SSL_EXT_ID_HEARTBEAT] = MBEDTLS_TLS_EXT_HEARTBEAT,
654 [MBEDTLS_SSL_EXT_ID_ALPN] = MBEDTLS_TLS_EXT_ALPN,
655 [MBEDTLS_SSL_EXT_ID_SCT] = MBEDTLS_TLS_EXT_SCT,
656 [MBEDTLS_SSL_EXT_ID_CLI_CERT_TYPE] = MBEDTLS_TLS_EXT_CLI_CERT_TYPE,
657 [MBEDTLS_SSL_EXT_ID_SERV_CERT_TYPE] = MBEDTLS_TLS_EXT_SERV_CERT_TYPE,
658 [MBEDTLS_SSL_EXT_ID_PADDING] = MBEDTLS_TLS_EXT_PADDING,
659 [MBEDTLS_SSL_EXT_ID_PRE_SHARED_KEY] = MBEDTLS_TLS_EXT_PRE_SHARED_KEY,
660 [MBEDTLS_SSL_EXT_ID_EARLY_DATA] = MBEDTLS_TLS_EXT_EARLY_DATA,
661 [MBEDTLS_SSL_EXT_ID_SUPPORTED_VERSIONS] = MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS,
662 [MBEDTLS_SSL_EXT_ID_COOKIE] = MBEDTLS_TLS_EXT_COOKIE,
663 [MBEDTLS_SSL_EXT_ID_PSK_KEY_EXCHANGE_MODES] = MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES,
664 [MBEDTLS_SSL_EXT_ID_CERT_AUTH] = MBEDTLS_TLS_EXT_CERT_AUTH,
665 [MBEDTLS_SSL_EXT_ID_OID_FILTERS] = MBEDTLS_TLS_EXT_OID_FILTERS,
666 [MBEDTLS_SSL_EXT_ID_POST_HANDSHAKE_AUTH] = MBEDTLS_TLS_EXT_POST_HANDSHAKE_AUTH,
667 [MBEDTLS_SSL_EXT_ID_SIG_ALG_CERT] = MBEDTLS_TLS_EXT_SIG_ALG_CERT,
668 [MBEDTLS_SSL_EXT_ID_KEY_SHARE] = MBEDTLS_TLS_EXT_KEY_SHARE,
669 [MBEDTLS_SSL_EXT_ID_TRUNCATED_HMAC] = MBEDTLS_TLS_EXT_TRUNCATED_HMAC,
670 [MBEDTLS_SSL_EXT_ID_SUPPORTED_POINT_FORMATS] = MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS,
671 [MBEDTLS_SSL_EXT_ID_ENCRYPT_THEN_MAC] = MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC,
672 [MBEDTLS_SSL_EXT_ID_EXTENDED_MASTER_SECRET] = MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET,
673 [MBEDTLS_SSL_EXT_ID_SESSION_TICKET] = MBEDTLS_TLS_EXT_SESSION_TICKET
674};
675
Gilles Peskine449bd832023-01-11 14:50:10 +0100676const char *mbedtls_ssl_get_extension_name(unsigned int extension_type)
Jerry Yud25cab02022-10-31 12:48:30 +0800677{
Gilles Peskine449bd832023-01-11 14:50:10 +0100678 return extension_name_table[
679 mbedtls_ssl_get_extension_id(extension_type)];
Jerry Yud25cab02022-10-31 12:48:30 +0800680}
681
Gilles Peskine449bd832023-01-11 14:50:10 +0100682static const char *ssl_tls13_get_hs_msg_name(int hs_msg_type)
Jerry Yud25cab02022-10-31 12:48:30 +0800683{
Gilles Peskine449bd832023-01-11 14:50:10 +0100684 switch (hs_msg_type) {
Jerry Yud25cab02022-10-31 12:48:30 +0800685 case MBEDTLS_SSL_HS_CLIENT_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +0100686 return "ClientHello";
Jerry Yud25cab02022-10-31 12:48:30 +0800687 case MBEDTLS_SSL_HS_SERVER_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +0100688 return "ServerHello";
Jerry Yud25cab02022-10-31 12:48:30 +0800689 case MBEDTLS_SSL_TLS1_3_HS_HELLO_RETRY_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +0100690 return "HelloRetryRequest";
Jerry Yud25cab02022-10-31 12:48:30 +0800691 case MBEDTLS_SSL_HS_NEW_SESSION_TICKET:
Gilles Peskine449bd832023-01-11 14:50:10 +0100692 return "NewSessionTicket";
Jerry Yud25cab02022-10-31 12:48:30 +0800693 case MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS:
Gilles Peskine449bd832023-01-11 14:50:10 +0100694 return "EncryptedExtensions";
Jerry Yud25cab02022-10-31 12:48:30 +0800695 case MBEDTLS_SSL_HS_CERTIFICATE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100696 return "Certificate";
Jerry Yud25cab02022-10-31 12:48:30 +0800697 case MBEDTLS_SSL_HS_CERTIFICATE_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +0100698 return "CertificateRequest";
Jerry Yud25cab02022-10-31 12:48:30 +0800699 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100700 return "Unknown";
Jerry Yud25cab02022-10-31 12:48:30 +0800701}
702
Gilles Peskine449bd832023-01-11 14:50:10 +0100703void mbedtls_ssl_print_extension(const mbedtls_ssl_context *ssl,
704 int level, const char *file, int line,
705 int hs_msg_type, unsigned int extension_type,
706 const char *extra_msg0, const char *extra_msg1)
Jerry Yud25cab02022-10-31 12:48:30 +0800707{
708 const char *extra_msg;
Gilles Peskine449bd832023-01-11 14:50:10 +0100709 if (extra_msg0 && extra_msg1) {
Jerry Yud25cab02022-10-31 12:48:30 +0800710 mbedtls_debug_print_msg(
711 ssl, level, file, line,
712 "%s: %s(%u) extension %s %s.",
Gilles Peskine449bd832023-01-11 14:50:10 +0100713 ssl_tls13_get_hs_msg_name(hs_msg_type),
714 mbedtls_ssl_get_extension_name(extension_type),
Jerry Yud25cab02022-10-31 12:48:30 +0800715 extension_type,
Gilles Peskine449bd832023-01-11 14:50:10 +0100716 extra_msg0, extra_msg1);
Jerry Yud25cab02022-10-31 12:48:30 +0800717 return;
718 }
719
720 extra_msg = extra_msg0 ? extra_msg0 : extra_msg1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100721 if (extra_msg) {
Jerry Yud25cab02022-10-31 12:48:30 +0800722 mbedtls_debug_print_msg(
723 ssl, level, file, line,
Gilles Peskine449bd832023-01-11 14:50:10 +0100724 "%s: %s(%u) extension %s.", ssl_tls13_get_hs_msg_name(hs_msg_type),
725 mbedtls_ssl_get_extension_name(extension_type), extension_type,
726 extra_msg);
Jerry Yud25cab02022-10-31 12:48:30 +0800727 return;
728 }
729
730 mbedtls_debug_print_msg(
731 ssl, level, file, line,
Gilles Peskine449bd832023-01-11 14:50:10 +0100732 "%s: %s(%u) extension.", ssl_tls13_get_hs_msg_name(hs_msg_type),
733 mbedtls_ssl_get_extension_name(extension_type), extension_type);
Jerry Yud25cab02022-10-31 12:48:30 +0800734}
735
Gilles Peskine449bd832023-01-11 14:50:10 +0100736void mbedtls_ssl_print_extensions(const mbedtls_ssl_context *ssl,
737 int level, const char *file, int line,
738 int hs_msg_type, uint32_t extensions_mask,
739 const char *extra)
Jerry Yud25cab02022-10-31 12:48:30 +0800740{
741
Gilles Peskine449bd832023-01-11 14:50:10 +0100742 for (unsigned i = 0;
743 i < sizeof(extension_name_table) / sizeof(extension_name_table[0]);
744 i++) {
Jerry Yu79aa7212022-11-08 21:30:21 +0800745 mbedtls_ssl_print_extension(
Jerry Yuea52ed92022-11-08 21:01:17 +0800746 ssl, level, file, line, hs_msg_type, extension_type_table[i],
Gilles Peskine449bd832023-01-11 14:50:10 +0100747 extensions_mask & (1 << i) ? "exists" : "does not exist", extra);
Jerry Yud25cab02022-10-31 12:48:30 +0800748 }
749}
750
Pengyu Lvee455c02023-01-12 14:37:24 +0800751#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS)
752#define ARRAY_LENGTH(a) (sizeof(a) / sizeof(*(a)))
753
754static const char *ticket_flag_name_table[] =
755{
756 [0] = "ALLOW_PSK_RESUMPTION",
757 [2] = "ALLOW_PSK_EPHEMERAL_RESUMPTION",
758 [3] = "ALLOW_EARLY_DATA",
759};
760
Pengyu Lv4938a562023-01-16 11:28:49 +0800761void mbedtls_ssl_print_ticket_flags(const mbedtls_ssl_context *ssl,
762 int level, const char *file, int line,
763 unsigned int flags)
Pengyu Lvee455c02023-01-12 14:37:24 +0800764{
765 size_t i;
766
767 mbedtls_debug_print_msg(ssl, level, file, line,
Pengyu Lv4938a562023-01-16 11:28:49 +0800768 "print ticket_flags (0x%02x)", flags);
769
770 flags = flags & MBEDTLS_SSL_TLS1_3_TICKET_FLAGS_MASK;
Pengyu Lvee455c02023-01-12 14:37:24 +0800771
772 for (i = 0; i < ARRAY_LENGTH(ticket_flag_name_table); i++) {
Pengyu Lv4938a562023-01-16 11:28:49 +0800773 if ((flags & (1 << i))) {
Pengyu Lvee455c02023-01-12 14:37:24 +0800774 mbedtls_debug_print_msg(ssl, level, file, line, "- %s is set.",
775 ticket_flag_name_table[i]);
776 }
777 }
778}
779#endif /* MBEDTLS_SSL_PROTO_TLS1_3 && MBEDTLS_SSL_SESSION_TICKETS */
780
Jerry Yud25cab02022-10-31 12:48:30 +0800781#endif /* MBEDTLS_DEBUG_C */
782
Gilles Peskine449bd832023-01-11 14:50:10 +0100783void mbedtls_ssl_optimize_checksum(mbedtls_ssl_context *ssl,
784 const mbedtls_ssl_ciphersuite_t *ciphersuite_info)
Jerry Yuc73c6182022-02-08 20:29:25 +0800785{
786 ((void) ciphersuite_info);
787
Andrzej Kurek25f27152022-08-17 16:09:31 -0400788#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +0100789 if (ciphersuite_info->mac == MBEDTLS_MD_SHA384) {
Jerry Yuc73c6182022-02-08 20:29:25 +0800790 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
Gilles Peskine449bd832023-01-11 14:50:10 +0100791 } else
Jerry Yuc73c6182022-02-08 20:29:25 +0800792#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400793#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +0100794 if (ciphersuite_info->mac != MBEDTLS_MD_SHA384) {
Jerry Yuc73c6182022-02-08 20:29:25 +0800795 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Gilles Peskine449bd832023-01-11 14:50:10 +0100796 } else
Jerry Yuc73c6182022-02-08 20:29:25 +0800797#endif
798 {
Gilles Peskine449bd832023-01-11 14:50:10 +0100799 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
Jerry Yuc73c6182022-02-08 20:29:25 +0800800 return;
801 }
802}
803
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100804int mbedtls_ssl_add_hs_hdr_to_checksum(mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100805 unsigned hs_type,
806 size_t total_hs_len)
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100807{
808 unsigned char hs_hdr[4];
809
810 /* Build HS header for checksum update. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100811 hs_hdr[0] = MBEDTLS_BYTE_0(hs_type);
812 hs_hdr[1] = MBEDTLS_BYTE_2(total_hs_len);
813 hs_hdr[2] = MBEDTLS_BYTE_1(total_hs_len);
814 hs_hdr[3] = MBEDTLS_BYTE_0(total_hs_len);
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100815
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100816 return ssl->handshake->update_checksum(ssl, hs_hdr, sizeof(hs_hdr));
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100817}
818
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100819int mbedtls_ssl_add_hs_msg_to_checksum(mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100820 unsigned hs_type,
821 unsigned char const *msg,
822 size_t msg_len)
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100823{
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100824 int ret;
825 ret = mbedtls_ssl_add_hs_hdr_to_checksum(ssl, hs_type, msg_len);
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100826 if (ret != 0) {
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100827 return ret;
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100828 }
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100829 return ssl->handshake->update_checksum(ssl, msg, msg_len);
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100830}
831
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100832int mbedtls_ssl_reset_checksum(mbedtls_ssl_context *ssl)
Jerry Yuc73c6182022-02-08 20:29:25 +0800833{
Manuel Pégourié-Gonnard626aaed2023-02-06 22:03:06 +0100834#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) || \
835 defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100836#if defined(MBEDTLS_USE_PSA_CRYPTO)
837 psa_status_t status;
838#else
839 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
840#endif
Manuel Pégourié-Gonnard626aaed2023-02-06 22:03:06 +0100841#else /* SHA-256 or SHA-384 */
Jerry Yuc73c6182022-02-08 20:29:25 +0800842 ((void) ssl);
Manuel Pégourié-Gonnard626aaed2023-02-06 22:03:06 +0100843#endif /* SHA-256 or SHA-384 */
Andrzej Kurek25f27152022-08-17 16:09:31 -0400844#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800845#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100846 status = psa_hash_abort(&ssl->handshake->fin_sha256_psa);
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 }
850 status = psa_hash_setup(&ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256);
851 if (status != PSA_SUCCESS) {
Andrzej Kurekdaf5b562023-03-03 05:52:28 -0500852 return PSA_TO_MD_ERR(status);
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100853 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800854#else
Manuel Pégourié-Gonnard947cee12023-03-06 11:59:59 +0100855 mbedtls_md_free(&ssl->handshake->fin_sha256);
856 mbedtls_md_init(&ssl->handshake->fin_sha256);
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +0100857 ret = mbedtls_md_setup(&ssl->handshake->fin_sha256,
858 mbedtls_md_info_from_type(MBEDTLS_MD_SHA256),
859 0);
860 if (ret != 0) {
861 return ret;
862 }
863 ret = mbedtls_md_starts(&ssl->handshake->fin_sha256);
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100864 if (ret != 0) {
865 return ret;
866 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800867#endif
868#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400869#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800870#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100871 status = psa_hash_abort(&ssl->handshake->fin_sha384_psa);
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 }
875 status = psa_hash_setup(&ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384);
876 if (status != PSA_SUCCESS) {
Andrzej Kurekdaf5b562023-03-03 05:52:28 -0500877 return PSA_TO_MD_ERR(status);
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100878 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800879#else
Manuel Pégourié-Gonnard947cee12023-03-06 11:59:59 +0100880 mbedtls_md_free(&ssl->handshake->fin_sha384);
881 mbedtls_md_init(&ssl->handshake->fin_sha384);
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +0100882 ret = mbedtls_md_setup(&ssl->handshake->fin_sha384,
883 mbedtls_md_info_from_type(MBEDTLS_MD_SHA384), 0);
884 if (ret != 0) {
885 return ret;
886 }
887 ret = mbedtls_md_starts(&ssl->handshake->fin_sha384);
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100888 if (ret != 0) {
889 return ret;
890 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800891#endif
892#endif
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100893 return 0;
Jerry Yuc73c6182022-02-08 20:29:25 +0800894}
895
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100896static int ssl_update_checksum_start(mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100897 const unsigned char *buf, size_t len)
Jerry Yuc73c6182022-02-08 20:29:25 +0800898{
Manuel Pégourié-Gonnard626aaed2023-02-06 22:03:06 +0100899#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) || \
900 defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100901#if defined(MBEDTLS_USE_PSA_CRYPTO)
902 psa_status_t status;
903#else
904 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
905#endif
Manuel Pégourié-Gonnard626aaed2023-02-06 22:03:06 +0100906#else /* SHA-256 or SHA-384 */
907 ((void) ssl);
908 (void) buf;
909 (void) len;
910#endif /* SHA-256 or SHA-384 */
Andrzej Kurek25f27152022-08-17 16:09:31 -0400911#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800912#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100913 status = psa_hash_update(&ssl->handshake->fin_sha256_psa, buf, len);
914 if (status != PSA_SUCCESS) {
Andrzej Kurekdaf5b562023-03-03 05:52:28 -0500915 return PSA_TO_MD_ERR(status);
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100916 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800917#else
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +0100918 ret = mbedtls_md_update(&ssl->handshake->fin_sha256, buf, len);
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100919 if (ret != 0) {
920 return ret;
921 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800922#endif
923#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400924#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800925#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100926 status = psa_hash_update(&ssl->handshake->fin_sha384_psa, buf, len);
927 if (status != PSA_SUCCESS) {
Andrzej Kurekdaf5b562023-03-03 05:52:28 -0500928 return PSA_TO_MD_ERR(status);
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100929 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800930#else
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +0100931 ret = mbedtls_md_update(&ssl->handshake->fin_sha384, buf, len);
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100932 if (ret != 0) {
933 return ret;
934 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800935#endif
936#endif
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100937 return 0;
Jerry Yuc73c6182022-02-08 20:29:25 +0800938}
939
Andrzej Kurek25f27152022-08-17 16:09:31 -0400940#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100941static int ssl_update_checksum_sha256(mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100942 const unsigned char *buf, size_t len)
Jerry Yuc73c6182022-02-08 20:29:25 +0800943{
944#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurekdaf5b562023-03-03 05:52:28 -0500945 return PSA_TO_MD_ERR(psa_hash_update(
946 &ssl->handshake->fin_sha256_psa, buf, len));
Jerry Yuc73c6182022-02-08 20:29:25 +0800947#else
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +0100948 return mbedtls_md_update(&ssl->handshake->fin_sha256, buf, len);
Jerry Yuc73c6182022-02-08 20:29:25 +0800949#endif
950}
951#endif
952
Andrzej Kurek25f27152022-08-17 16:09:31 -0400953#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100954static int ssl_update_checksum_sha384(mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100955 const unsigned char *buf, size_t len)
Jerry Yuc73c6182022-02-08 20:29:25 +0800956{
957#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurekdaf5b562023-03-03 05:52:28 -0500958 return PSA_TO_MD_ERR(psa_hash_update(
959 &ssl->handshake->fin_sha384_psa, buf, len));
Jerry Yuc73c6182022-02-08 20:29:25 +0800960#else
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +0100961 return mbedtls_md_update(&ssl->handshake->fin_sha384, buf, len);
Jerry Yuc73c6182022-02-08 20:29:25 +0800962#endif
963}
964#endif
965
Gilles Peskine449bd832023-01-11 14:50:10 +0100966static void ssl_handshake_params_init(mbedtls_ssl_handshake_params *handshake)
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200967{
Gilles Peskine449bd832023-01-11 14:50:10 +0100968 memset(handshake, 0, sizeof(mbedtls_ssl_handshake_params));
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200969
Andrzej Kurek25f27152022-08-17 16:09:31 -0400970#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -0500971#if defined(MBEDTLS_USE_PSA_CRYPTO)
972 handshake->fin_sha256_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -0500973#else
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +0100974 mbedtls_md_init(&handshake->fin_sha256);
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200975#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -0500976#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400977#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -0500978#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -0500979 handshake->fin_sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -0500980#else
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +0100981 mbedtls_md_init(&handshake->fin_sha384);
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200982#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -0500983#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200984
985 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +0100986
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200987#if defined(MBEDTLS_DHM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100988 mbedtls_dhm_init(&handshake->dhm_ctx);
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200989#endif
Neil Armstrongf3f46412022-04-12 14:43:39 +0200990#if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100991 mbedtls_ecdh_init(&handshake->ecdh_ctx);
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200992#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +0200993#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Neil Armstrongca7d5062022-05-31 14:43:23 +0200994#if defined(MBEDTLS_USE_PSA_CRYPTO)
995 handshake->psa_pake_ctx = psa_pake_operation_init();
996 handshake->psa_pake_password = MBEDTLS_SVC_KEY_ID_INIT;
997#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100998 mbedtls_ecjpake_init(&handshake->ecjpake_ctx);
Neil Armstrongca7d5062022-05-31 14:43:23 +0200999#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02001000#if defined(MBEDTLS_SSL_CLI_C)
1001 handshake->ecjpake_cache = NULL;
1002 handshake->ecjpake_cache_len = 0;
1003#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02001004#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001005
Gilles Peskineeccd8882020-03-10 12:19:08 +01001006#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001007 mbedtls_x509_crt_restart_init(&handshake->ecrs_ctx);
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02001008#endif
1009
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001010#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1011 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
1012#endif
Hanno Becker75173122019-02-06 16:18:31 +00001013
1014#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
1015 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01001016 mbedtls_pk_init(&handshake->peer_pubkey);
Hanno Becker75173122019-02-06 16:18:31 +00001017#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001018}
1019
Gilles Peskine449bd832023-01-11 14:50:10 +01001020void mbedtls_ssl_transform_init(mbedtls_ssl_transform *transform)
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001021{
Gilles Peskine449bd832023-01-11 14:50:10 +01001022 memset(transform, 0, sizeof(mbedtls_ssl_transform));
Paul Bakker84bbeb52014-07-01 14:53:22 +02001023
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +01001024#if defined(MBEDTLS_USE_PSA_CRYPTO)
1025 transform->psa_key_enc = MBEDTLS_SVC_KEY_ID_INIT;
1026 transform->psa_key_dec = MBEDTLS_SVC_KEY_ID_INIT;
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01001027#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001028 mbedtls_cipher_init(&transform->cipher_ctx_enc);
1029 mbedtls_cipher_init(&transform->cipher_ctx_dec);
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +01001030#endif
1031
Hanno Beckerfd86ca82020-11-30 08:54:23 +00001032#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong39b8e7d2022-02-23 09:24:45 +01001033#if defined(MBEDTLS_USE_PSA_CRYPTO)
1034 transform->psa_mac_enc = MBEDTLS_SVC_KEY_ID_INIT;
1035 transform->psa_mac_dec = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrongcf8841a2022-02-24 11:17:45 +01001036#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001037 mbedtls_md_init(&transform->md_ctx_enc);
1038 mbedtls_md_init(&transform->md_ctx_dec);
Hanno Beckerd56ed242018-01-03 15:32:51 +00001039#endif
Neil Armstrongcf8841a2022-02-24 11:17:45 +01001040#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001041}
1042
Gilles Peskine449bd832023-01-11 14:50:10 +01001043void mbedtls_ssl_session_init(mbedtls_ssl_session *session)
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001044{
Gilles Peskine449bd832023-01-11 14:50:10 +01001045 memset(session, 0, sizeof(mbedtls_ssl_session));
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001046}
1047
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001048MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001049static int ssl_handshake_init(mbedtls_ssl_context *ssl)
Paul Bakker48916f92012-09-16 19:57:18 +00001050{
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001051 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1052
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001053 /* Clear old handshake information if present */
Jerry Yu2e199812022-12-01 18:57:19 +08001054#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01001055 if (ssl->transform_negotiate) {
1056 mbedtls_ssl_transform_free(ssl->transform_negotiate);
1057 }
Jerry Yu2e199812022-12-01 18:57:19 +08001058#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001059 if (ssl->session_negotiate) {
1060 mbedtls_ssl_session_free(ssl->session_negotiate);
1061 }
1062 if (ssl->handshake) {
1063 mbedtls_ssl_handshake_free(ssl);
1064 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001065
Jerry Yu2e199812022-12-01 18:57:19 +08001066#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001067 /*
1068 * Either the pointers are now NULL or cleared properly and can be freed.
1069 * Now allocate missing structures.
1070 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001071 if (ssl->transform_negotiate == NULL) {
1072 ssl->transform_negotiate = mbedtls_calloc(1, sizeof(mbedtls_ssl_transform));
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001073 }
Jerry Yu2e199812022-12-01 18:57:19 +08001074#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker48916f92012-09-16 19:57:18 +00001075
Gilles Peskine449bd832023-01-11 14:50:10 +01001076 if (ssl->session_negotiate == NULL) {
1077 ssl->session_negotiate = mbedtls_calloc(1, sizeof(mbedtls_ssl_session));
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001078 }
Paul Bakker48916f92012-09-16 19:57:18 +00001079
Gilles Peskine449bd832023-01-11 14:50:10 +01001080 if (ssl->handshake == NULL) {
1081 ssl->handshake = mbedtls_calloc(1, sizeof(mbedtls_ssl_handshake_params));
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001082 }
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05001083#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1084 /* If the buffers are too small - reallocate */
Andrzej Kurek8ea68722020-04-03 06:40:47 -04001085
Gilles Peskine449bd832023-01-11 14:50:10 +01001086 handle_buffer_resizing(ssl, 0, MBEDTLS_SSL_IN_BUFFER_LEN,
1087 MBEDTLS_SSL_OUT_BUFFER_LEN);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05001088#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001089
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001090 /* All pointers should exist and can be directly freed without issue */
Gilles Peskine449bd832023-01-11 14:50:10 +01001091 if (ssl->handshake == NULL ||
Jerry Yu2e199812022-12-01 18:57:19 +08001092#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker48916f92012-09-16 19:57:18 +00001093 ssl->transform_negotiate == NULL ||
Jerry Yu2e199812022-12-01 18:57:19 +08001094#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001095 ssl->session_negotiate == NULL) {
1096 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc() of ssl sub-contexts failed"));
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001097
Gilles Peskine449bd832023-01-11 14:50:10 +01001098 mbedtls_free(ssl->handshake);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001099 ssl->handshake = NULL;
Jerry Yu2e199812022-12-01 18:57:19 +08001100
1101#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01001102 mbedtls_free(ssl->transform_negotiate);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001103 ssl->transform_negotiate = NULL;
Jerry Yu2e199812022-12-01 18:57:19 +08001104#endif
1105
Gilles Peskine449bd832023-01-11 14:50:10 +01001106 mbedtls_free(ssl->session_negotiate);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001107 ssl->session_negotiate = NULL;
1108
Gilles Peskine449bd832023-01-11 14:50:10 +01001109 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Paul Bakker48916f92012-09-16 19:57:18 +00001110 }
1111
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001112 /* Initialize structures */
Gilles Peskine449bd832023-01-11 14:50:10 +01001113 mbedtls_ssl_session_init(ssl->session_negotiate);
1114 ssl_handshake_params_init(ssl->handshake);
Paul Bakker968afaa2014-07-09 11:09:24 +02001115
Jerry Yu2e199812022-12-01 18:57:19 +08001116#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01001117 mbedtls_ssl_transform_init(ssl->transform_negotiate);
Jerry Yu2e199812022-12-01 18:57:19 +08001118#endif
1119
Manuel Pégourié-Gonnard537f2312023-02-05 10:17:45 +01001120 /* Setup handshake checksums */
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001121 ret = mbedtls_ssl_reset_checksum(ssl);
1122 if (ret != 0) {
1123 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_reset_checksum", ret);
1124 return ret;
1125 }
Manuel Pégourié-Gonnard537f2312023-02-05 10:17:45 +01001126
Jerry Yud0766ec2022-09-22 10:46:57 +08001127#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
1128 defined(MBEDTLS_SSL_SRV_C) && \
1129 defined(MBEDTLS_SSL_SESSION_TICKETS)
1130 ssl->handshake->new_session_tickets_count =
Gilles Peskine449bd832023-01-11 14:50:10 +01001131 ssl->conf->new_session_tickets_count;
Jerry Yud0766ec2022-09-22 10:46:57 +08001132#endif
1133
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001134#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001135 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001136 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001137
Gilles Peskine449bd832023-01-11 14:50:10 +01001138 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001139 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Gilles Peskine449bd832023-01-11 14:50:10 +01001140 } else {
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001141 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Gilles Peskine449bd832023-01-11 14:50:10 +01001142 }
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001143
Gilles Peskine449bd832023-01-11 14:50:10 +01001144 mbedtls_ssl_set_timer(ssl, 0);
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001145 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001146#endif
1147
Brett Warrene0edc842021-08-17 09:53:13 +01001148/*
1149 * curve_list is translated to IANA TLS group identifiers here because
1150 * mbedtls_ssl_conf_curves returns void and so can't return
1151 * any error codes.
1152 */
1153#if defined(MBEDTLS_ECP_C)
1154#if !defined(MBEDTLS_DEPRECATED_REMOVED)
1155 /* Heap allocate and translate curve_list from internal to IANA group ids */
Gilles Peskine449bd832023-01-11 14:50:10 +01001156 if (ssl->conf->curve_list != NULL) {
Brett Warrene0edc842021-08-17 09:53:13 +01001157 size_t length;
1158 const mbedtls_ecp_group_id *curve_list = ssl->conf->curve_list;
1159
Gilles Peskine449bd832023-01-11 14:50:10 +01001160 for (length = 0; (curve_list[length] != MBEDTLS_ECP_DP_NONE) &&
1161 (length < MBEDTLS_ECP_DP_MAX); length++) {
1162 }
Brett Warrene0edc842021-08-17 09:53:13 +01001163
1164 /* Leave room for zero termination */
Gilles Peskine449bd832023-01-11 14:50:10 +01001165 uint16_t *group_list = mbedtls_calloc(length + 1, sizeof(uint16_t));
1166 if (group_list == NULL) {
1167 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
1168 }
Brett Warrene0edc842021-08-17 09:53:13 +01001169
Gilles Peskine449bd832023-01-11 14:50:10 +01001170 for (size_t i = 0; i < length; i++) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01001171 uint16_t tls_id = mbedtls_ssl_get_tls_id_from_ecp_group_id(
Gilles Peskine449bd832023-01-11 14:50:10 +01001172 curve_list[i]);
1173 if (tls_id == 0) {
1174 mbedtls_free(group_list);
1175 return MBEDTLS_ERR_SSL_BAD_CONFIG;
Brett Warrene0edc842021-08-17 09:53:13 +01001176 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01001177 group_list[i] = tls_id;
Brett Warrene0edc842021-08-17 09:53:13 +01001178 }
1179
1180 group_list[length] = 0;
1181
1182 ssl->handshake->group_list = group_list;
1183 ssl->handshake->group_list_heap_allocated = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001184 } else {
Brett Warrene0edc842021-08-17 09:53:13 +01001185 ssl->handshake->group_list = ssl->conf->group_list;
1186 ssl->handshake->group_list_heap_allocated = 0;
1187 }
1188#endif /* MBEDTLS_DEPRECATED_REMOVED */
1189#endif /* MBEDTLS_ECP_C */
1190
Ronald Crone68ab4f2022-10-05 12:46:29 +02001191#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +08001192#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Jerry Yua69269a2022-01-17 21:06:01 +08001193#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yu713013f2022-01-17 18:16:35 +08001194 /* Heap allocate and translate sig_hashes from internal hash identifiers to
1195 signature algorithms IANA identifiers. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001196 if (mbedtls_ssl_conf_is_tls12_only(ssl->conf) &&
1197 ssl->conf->sig_hashes != NULL) {
Jerry Yuf017ee42022-01-12 15:49:48 +08001198 const int *md;
1199 const int *sig_hashes = ssl->conf->sig_hashes;
Jerry Yub476a442022-01-21 18:14:45 +08001200 size_t sig_algs_len = 0;
Jerry Yuf017ee42022-01-12 15:49:48 +08001201 uint16_t *p;
1202
Jerry Yub476a442022-01-21 18:14:45 +08001203#if defined(static_assert)
Gilles Peskine449bd832023-01-11 14:50:10 +01001204 static_assert(MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN
1205 <= (SIZE_MAX - (2 * sizeof(uint16_t))),
1206 "MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN too big");
Jerry Yua68dca22022-01-20 16:28:27 +08001207#endif
1208
Gilles Peskine449bd832023-01-11 14:50:10 +01001209 for (md = sig_hashes; *md != MBEDTLS_MD_NONE; md++) {
1210 if (mbedtls_ssl_hash_from_md_alg(*md) == MBEDTLS_SSL_HASH_NONE) {
Jerry Yuf017ee42022-01-12 15:49:48 +08001211 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001212 }
Valerio Setti75fba322023-02-23 17:35:09 +01001213#if defined(MBEDTLS_PK_CAN_ECDSA_SOME)
Gilles Peskine449bd832023-01-11 14:50:10 +01001214 sig_algs_len += sizeof(uint16_t);
Jerry Yub476a442022-01-21 18:14:45 +08001215#endif
Jerry Yua68dca22022-01-20 16:28:27 +08001216
Jerry Yub476a442022-01-21 18:14:45 +08001217#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001218 sig_algs_len += sizeof(uint16_t);
Jerry Yub476a442022-01-21 18:14:45 +08001219#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001220 if (sig_algs_len > MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN) {
1221 return MBEDTLS_ERR_SSL_BAD_CONFIG;
1222 }
Jerry Yuf017ee42022-01-12 15:49:48 +08001223 }
1224
Gilles Peskine449bd832023-01-11 14:50:10 +01001225 if (sig_algs_len < MBEDTLS_SSL_MIN_SIG_ALG_LIST_LEN) {
1226 return MBEDTLS_ERR_SSL_BAD_CONFIG;
1227 }
Jerry Yuf017ee42022-01-12 15:49:48 +08001228
Gilles Peskine449bd832023-01-11 14:50:10 +01001229 ssl->handshake->sig_algs = mbedtls_calloc(1, sig_algs_len +
1230 sizeof(uint16_t));
1231 if (ssl->handshake->sig_algs == NULL) {
1232 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
1233 }
Jerry Yuf017ee42022-01-12 15:49:48 +08001234
Gilles Peskine449bd832023-01-11 14:50:10 +01001235 p = (uint16_t *) ssl->handshake->sig_algs;
1236 for (md = sig_hashes; *md != MBEDTLS_MD_NONE; md++) {
1237 unsigned char hash = mbedtls_ssl_hash_from_md_alg(*md);
1238 if (hash == MBEDTLS_SSL_HASH_NONE) {
Jerry Yuf017ee42022-01-12 15:49:48 +08001239 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001240 }
Valerio Setti75fba322023-02-23 17:35:09 +01001241#if defined(MBEDTLS_PK_CAN_ECDSA_SOME)
Gilles Peskine449bd832023-01-11 14:50:10 +01001242 *p = ((hash << 8) | MBEDTLS_SSL_SIG_ECDSA);
Jerry Yuf017ee42022-01-12 15:49:48 +08001243 p++;
Jerry Yu6106fdc2022-01-12 16:36:14 +08001244#endif
1245#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001246 *p = ((hash << 8) | MBEDTLS_SSL_SIG_RSA);
Jerry Yuf017ee42022-01-12 15:49:48 +08001247 p++;
Jerry Yu6106fdc2022-01-12 16:36:14 +08001248#endif
Jerry Yuf017ee42022-01-12 15:49:48 +08001249 }
Gabor Mezei15b95a62022-05-09 16:37:58 +02001250 *p = MBEDTLS_TLS_SIG_NONE;
Jerry Yuf017ee42022-01-12 15:49:48 +08001251 ssl->handshake->sig_algs_heap_allocated = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001252 } else
Jerry Yua69269a2022-01-17 21:06:01 +08001253#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Jerry Yuf017ee42022-01-12 15:49:48 +08001254 {
Jerry Yuf017ee42022-01-12 15:49:48 +08001255 ssl->handshake->sig_algs_heap_allocated = 0;
1256 }
Jerry Yucc539102022-06-27 16:27:35 +08001257#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Ronald Crone68ab4f2022-10-05 12:46:29 +02001258#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Gilles Peskine449bd832023-01-11 14:50:10 +01001259 return 0;
Paul Bakker48916f92012-09-16 19:57:18 +00001260}
1261
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001262#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001263/* Dummy cookie callbacks for defaults */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001264MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001265static int ssl_cookie_write_dummy(void *ctx,
1266 unsigned char **p, unsigned char *end,
1267 const unsigned char *cli_id, size_t cli_id_len)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001268{
1269 ((void) ctx);
1270 ((void) p);
1271 ((void) end);
1272 ((void) cli_id);
1273 ((void) cli_id_len);
1274
Gilles Peskine449bd832023-01-11 14:50:10 +01001275 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001276}
1277
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001278MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001279static int ssl_cookie_check_dummy(void *ctx,
1280 const unsigned char *cookie, size_t cookie_len,
1281 const unsigned char *cli_id, size_t cli_id_len)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001282{
1283 ((void) ctx);
1284 ((void) cookie);
1285 ((void) cookie_len);
1286 ((void) cli_id);
1287 ((void) cli_id_len);
1288
Gilles Peskine449bd832023-01-11 14:50:10 +01001289 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001290}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001291#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001292
Paul Bakker5121ce52009-01-03 21:22:43 +00001293/*
1294 * Initialize an SSL context
1295 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001296void mbedtls_ssl_init(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001297{
Gilles Peskine449bd832023-01-11 14:50:10 +01001298 memset(ssl, 0, sizeof(mbedtls_ssl_context));
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001299}
1300
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001301MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001302static int ssl_conf_version_check(const mbedtls_ssl_context *ssl)
Jerry Yu60835a82021-08-04 10:13:52 +08001303{
Ronald Cron086ee0b2022-03-15 15:18:51 +01001304 const mbedtls_ssl_config *conf = ssl->conf;
1305
Ronald Cron6f135e12021-12-08 16:57:54 +01001306#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001307 if (mbedtls_ssl_conf_is_tls13_only(conf)) {
1308 if (conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
1309 MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS 1.3 is not yet supported."));
1310 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQianed582dd2022-04-13 08:21:05 +00001311 }
1312
Gilles Peskine449bd832023-01-11 14:50:10 +01001313 MBEDTLS_SSL_DEBUG_MSG(4, ("The SSL configuration is tls13 only."));
1314 return 0;
Jerry Yu60835a82021-08-04 10:13:52 +08001315 }
1316#endif
1317
1318#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01001319 if (mbedtls_ssl_conf_is_tls12_only(conf)) {
1320 MBEDTLS_SSL_DEBUG_MSG(4, ("The SSL configuration is tls12 only."));
1321 return 0;
Jerry Yu60835a82021-08-04 10:13:52 +08001322 }
1323#endif
1324
Ronald Cron6f135e12021-12-08 16:57:54 +01001325#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001326 if (mbedtls_ssl_conf_is_hybrid_tls12_tls13(conf)) {
1327 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
1328 MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS not yet supported in Hybrid TLS 1.3 + TLS 1.2"));
1329 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQianed582dd2022-04-13 08:21:05 +00001330 }
1331
Gilles Peskine449bd832023-01-11 14:50:10 +01001332 if (conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
1333 MBEDTLS_SSL_DEBUG_MSG(1, ("TLS 1.3 server is not supported yet."));
1334 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQian8f9dfe42022-04-15 02:52:39 +00001335 }
1336
1337
Gilles Peskine449bd832023-01-11 14:50:10 +01001338 MBEDTLS_SSL_DEBUG_MSG(4, ("The SSL configuration is TLS 1.3 or TLS 1.2."));
1339 return 0;
Jerry Yu60835a82021-08-04 10:13:52 +08001340 }
1341#endif
1342
Gilles Peskine449bd832023-01-11 14:50:10 +01001343 MBEDTLS_SSL_DEBUG_MSG(1, ("The SSL configuration is invalid."));
1344 return MBEDTLS_ERR_SSL_BAD_CONFIG;
Jerry Yu60835a82021-08-04 10:13:52 +08001345}
1346
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001347MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu60835a82021-08-04 10:13:52 +08001348static int ssl_conf_check(const mbedtls_ssl_context *ssl)
1349{
1350 int ret;
Gilles Peskine449bd832023-01-11 14:50:10 +01001351 ret = ssl_conf_version_check(ssl);
1352 if (ret != 0) {
1353 return ret;
1354 }
Jerry Yu60835a82021-08-04 10:13:52 +08001355
Jerry Yudef7ae42022-10-30 14:13:19 +08001356#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1357 /* RFC 8446 section 4.4.3
1358 *
1359 * If the verification fails, the receiver MUST terminate the handshake with
1360 * a "decrypt_error" alert.
1361 *
1362 * If the client is configured as TLS 1.3 only with optional verify, return
1363 * bad config.
1364 *
1365 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001366 if (mbedtls_ssl_conf_tls13_ephemeral_enabled(
1367 (mbedtls_ssl_context *) ssl) &&
Jerry Yudef7ae42022-10-30 14:13:19 +08001368 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
1369 ssl->conf->max_tls_version == MBEDTLS_SSL_VERSION_TLS1_3 &&
1370 ssl->conf->min_tls_version == MBEDTLS_SSL_VERSION_TLS1_3 &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001371 ssl->conf->authmode == MBEDTLS_SSL_VERIFY_OPTIONAL) {
Jerry Yudef7ae42022-10-30 14:13:19 +08001372 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +01001373 1, ("Optional verify auth mode "
1374 "is not available for TLS 1.3 client"));
1375 return MBEDTLS_ERR_SSL_BAD_CONFIG;
Jerry Yudef7ae42022-10-30 14:13:19 +08001376 }
1377#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
1378
Jerry Yu60835a82021-08-04 10:13:52 +08001379 /* Space for further checks */
1380
Gilles Peskine449bd832023-01-11 14:50:10 +01001381 return 0;
Jerry Yu60835a82021-08-04 10:13:52 +08001382}
1383
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001384/*
1385 * Setup an SSL context
1386 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01001387
Gilles Peskine449bd832023-01-11 14:50:10 +01001388int mbedtls_ssl_setup(mbedtls_ssl_context *ssl,
1389 const mbedtls_ssl_config *conf)
Paul Bakker5121ce52009-01-03 21:22:43 +00001390{
Janos Follath865b3eb2019-12-16 11:46:15 +00001391 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Darryl Greenb33cc762019-11-28 14:29:44 +00001392 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1393 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00001394
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001395 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00001396
Gilles Peskine449bd832023-01-11 14:50:10 +01001397 if ((ret = ssl_conf_check(ssl)) != 0) {
1398 return ret;
1399 }
Jerry Yu60835a82021-08-04 10:13:52 +08001400
Paul Bakker62f2dee2012-09-28 07:31:51 +00001401 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001402 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00001403 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02001404
1405 /* Set to NULL in case of an error condition */
1406 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02001407
Darryl Greenb33cc762019-11-28 14:29:44 +00001408#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1409 ssl->in_buf_len = in_buf_len;
1410#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001411 ssl->in_buf = mbedtls_calloc(1, in_buf_len);
1412 if (ssl->in_buf == NULL) {
1413 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", in_buf_len));
k-stachowiak9f7798e2018-07-31 16:52:32 +02001414 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02001415 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10001416 }
1417
Darryl Greenb33cc762019-11-28 14:29:44 +00001418#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1419 ssl->out_buf_len = out_buf_len;
1420#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001421 ssl->out_buf = mbedtls_calloc(1, out_buf_len);
1422 if (ssl->out_buf == NULL) {
1423 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", out_buf_len));
k-stachowiak9f7798e2018-07-31 16:52:32 +02001424 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02001425 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00001426 }
1427
Gilles Peskine449bd832023-01-11 14:50:10 +01001428 mbedtls_ssl_reset_in_out_pointers(ssl);
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02001429
Johan Pascalb62bb512015-12-03 21:56:45 +01001430#if defined(MBEDTLS_SSL_DTLS_SRTP)
Gilles Peskine449bd832023-01-11 14:50:10 +01001431 memset(&ssl->dtls_srtp_info, 0, sizeof(ssl->dtls_srtp_info));
Johan Pascalb62bb512015-12-03 21:56:45 +01001432#endif
1433
Gilles Peskine449bd832023-01-11 14:50:10 +01001434 if ((ret = ssl_handshake_init(ssl)) != 0) {
k-stachowiaka47911c2018-07-04 17:41:58 +02001435 goto error;
Gilles Peskine449bd832023-01-11 14:50:10 +01001436 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001437
Gilles Peskine449bd832023-01-11 14:50:10 +01001438 return 0;
k-stachowiaka47911c2018-07-04 17:41:58 +02001439
1440error:
Gilles Peskine449bd832023-01-11 14:50:10 +01001441 mbedtls_free(ssl->in_buf);
1442 mbedtls_free(ssl->out_buf);
k-stachowiaka47911c2018-07-04 17:41:58 +02001443
1444 ssl->conf = NULL;
1445
Darryl Greenb33cc762019-11-28 14:29:44 +00001446#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1447 ssl->in_buf_len = 0;
1448 ssl->out_buf_len = 0;
1449#endif
k-stachowiaka47911c2018-07-04 17:41:58 +02001450 ssl->in_buf = NULL;
1451 ssl->out_buf = NULL;
1452
1453 ssl->in_hdr = NULL;
1454 ssl->in_ctr = NULL;
1455 ssl->in_len = NULL;
1456 ssl->in_iv = NULL;
1457 ssl->in_msg = NULL;
1458
1459 ssl->out_hdr = NULL;
1460 ssl->out_ctr = NULL;
1461 ssl->out_len = NULL;
1462 ssl->out_iv = NULL;
1463 ssl->out_msg = NULL;
1464
Gilles Peskine449bd832023-01-11 14:50:10 +01001465 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001466}
1467
1468/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00001469 * Reset an initialized and used SSL context for re-use while retaining
1470 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001471 *
1472 * If partial is non-zero, keep data in the input buffer and client ID.
1473 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00001474 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001475void mbedtls_ssl_session_reset_msg_layer(mbedtls_ssl_context *ssl,
1476 int partial)
Paul Bakker7eb013f2011-10-06 12:37:39 +00001477{
Darryl Greenb33cc762019-11-28 14:29:44 +00001478#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1479 size_t in_buf_len = ssl->in_buf_len;
1480 size_t out_buf_len = ssl->out_buf_len;
1481#else
1482 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1483 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
1484#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001485
Hanno Beckerb0302c42021-08-03 09:39:42 +01001486#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || !defined(MBEDTLS_SSL_SRV_C)
1487 partial = 0;
Hanno Becker7e772132018-08-10 12:38:21 +01001488#endif
1489
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001490 /* Cancel any possibly running timer */
Gilles Peskine449bd832023-01-11 14:50:10 +01001491 mbedtls_ssl_set_timer(ssl, 0);
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001492
Gilles Peskine449bd832023-01-11 14:50:10 +01001493 mbedtls_ssl_reset_in_out_pointers(ssl);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001494
1495 /* Reset incoming message parsing */
1496 ssl->in_offt = NULL;
1497 ssl->nb_zero = 0;
1498 ssl->in_msgtype = 0;
1499 ssl->in_msglen = 0;
1500 ssl->in_hslen = 0;
1501 ssl->keep_current_message = 0;
1502 ssl->transform_in = NULL;
1503
1504#if defined(MBEDTLS_SSL_PROTO_DTLS)
1505 ssl->next_record_offset = 0;
1506 ssl->in_epoch = 0;
1507#endif
1508
1509 /* Keep current datagram if partial == 1 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001510 if (partial == 0) {
Hanno Beckerb0302c42021-08-03 09:39:42 +01001511 ssl->in_left = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001512 memset(ssl->in_buf, 0, in_buf_len);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001513 }
1514
Ronald Cronad8c17b2022-06-10 17:18:09 +02001515 ssl->send_alert = 0;
1516
Hanno Beckerb0302c42021-08-03 09:39:42 +01001517 /* Reset outgoing message writing */
1518 ssl->out_msgtype = 0;
1519 ssl->out_msglen = 0;
1520 ssl->out_left = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001521 memset(ssl->out_buf, 0, out_buf_len);
1522 memset(ssl->cur_out_ctr, 0, sizeof(ssl->cur_out_ctr));
Hanno Beckerb0302c42021-08-03 09:39:42 +01001523 ssl->transform_out = NULL;
1524
1525#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine449bd832023-01-11 14:50:10 +01001526 mbedtls_ssl_dtls_replay_reset(ssl);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001527#endif
1528
XiaokangQian2b01dc32022-01-21 02:53:13 +00001529#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01001530 if (ssl->transform) {
1531 mbedtls_ssl_transform_free(ssl->transform);
1532 mbedtls_free(ssl->transform);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001533 ssl->transform = NULL;
1534 }
XiaokangQian2b01dc32022-01-21 02:53:13 +00001535#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1536
XiaokangQian2b01dc32022-01-21 02:53:13 +00001537#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001538 mbedtls_ssl_transform_free(ssl->transform_application);
1539 mbedtls_free(ssl->transform_application);
XiaokangQian2b01dc32022-01-21 02:53:13 +00001540 ssl->transform_application = NULL;
1541
Gilles Peskine449bd832023-01-11 14:50:10 +01001542 if (ssl->handshake != NULL) {
Jerry Yu3d9b5902022-11-04 14:07:25 +08001543#if defined(MBEDTLS_SSL_EARLY_DATA)
Gilles Peskine449bd832023-01-11 14:50:10 +01001544 mbedtls_ssl_transform_free(ssl->handshake->transform_earlydata);
1545 mbedtls_free(ssl->handshake->transform_earlydata);
XiaokangQian2b01dc32022-01-21 02:53:13 +00001546 ssl->handshake->transform_earlydata = NULL;
Jerry Yu3d9b5902022-11-04 14:07:25 +08001547#endif
XiaokangQian2b01dc32022-01-21 02:53:13 +00001548
Gilles Peskine449bd832023-01-11 14:50:10 +01001549 mbedtls_ssl_transform_free(ssl->handshake->transform_handshake);
1550 mbedtls_free(ssl->handshake->transform_handshake);
XiaokangQian2b01dc32022-01-21 02:53:13 +00001551 ssl->handshake->transform_handshake = NULL;
1552 }
1553
XiaokangQian2b01dc32022-01-21 02:53:13 +00001554#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerb0302c42021-08-03 09:39:42 +01001555}
1556
Gilles Peskine449bd832023-01-11 14:50:10 +01001557int mbedtls_ssl_session_reset_int(mbedtls_ssl_context *ssl, int partial)
Hanno Beckerb0302c42021-08-03 09:39:42 +01001558{
1559 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1560
1561 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
1562
Gilles Peskine449bd832023-01-11 14:50:10 +01001563 mbedtls_ssl_session_reset_msg_layer(ssl, partial);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001564
1565 /* Reset renegotiation state */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001566#if defined(MBEDTLS_SSL_RENEGOTIATION)
1567 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001568 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00001569
1570 ssl->verify_data_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001571 memset(ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN);
1572 memset(ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN);
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001573#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001574 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00001575
Hanno Beckerb0302c42021-08-03 09:39:42 +01001576 ssl->session_in = NULL;
Hanno Becker78640902018-08-13 16:35:15 +01001577 ssl->session_out = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001578 if (ssl->session) {
1579 mbedtls_ssl_session_free(ssl->session);
1580 mbedtls_free(ssl->session);
Paul Bakkerc0463502013-02-14 11:19:38 +01001581 ssl->session = NULL;
1582 }
1583
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001584#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02001585 ssl->alpn_chosen = NULL;
1586#endif
1587
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001588#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
David Horstmann3b2276a2022-10-06 14:49:08 +01001589 int free_cli_id = 1;
Hanno Becker4ccbf062018-08-10 11:20:38 +01001590#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Gilles Peskine449bd832023-01-11 14:50:10 +01001591 free_cli_id = (partial == 0);
Hanno Becker4ccbf062018-08-10 11:20:38 +01001592#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001593 if (free_cli_id) {
1594 mbedtls_free(ssl->cli_id);
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001595 ssl->cli_id = NULL;
1596 ssl->cli_id_len = 0;
1597 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02001598#endif
1599
Gilles Peskine449bd832023-01-11 14:50:10 +01001600 if ((ret = ssl_handshake_init(ssl)) != 0) {
1601 return ret;
1602 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00001603
Gilles Peskine449bd832023-01-11 14:50:10 +01001604 return 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00001605}
1606
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02001607/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001608 * Reset an initialized and used SSL context for re-use while retaining
1609 * all application-set variables, function pointers and data.
1610 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001611int mbedtls_ssl_session_reset(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001612{
Gilles Peskine449bd832023-01-11 14:50:10 +01001613 return mbedtls_ssl_session_reset_int(ssl, 0);
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001614}
1615
1616/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001617 * SSL set accessors
1618 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001619void mbedtls_ssl_conf_endpoint(mbedtls_ssl_config *conf, int endpoint)
Paul Bakker5121ce52009-01-03 21:22:43 +00001620{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001621 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00001622}
1623
Gilles Peskine449bd832023-01-11 14:50:10 +01001624void mbedtls_ssl_conf_transport(mbedtls_ssl_config *conf, int transport)
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001625{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001626 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001627}
1628
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001629#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine449bd832023-01-11 14:50:10 +01001630void mbedtls_ssl_conf_dtls_anti_replay(mbedtls_ssl_config *conf, char mode)
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001631{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001632 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001633}
1634#endif
1635
Gilles Peskine449bd832023-01-11 14:50:10 +01001636void mbedtls_ssl_conf_dtls_badmac_limit(mbedtls_ssl_config *conf, unsigned limit)
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001637{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001638 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001639}
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001640
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001641#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01001642
Gilles Peskine449bd832023-01-11 14:50:10 +01001643void mbedtls_ssl_set_datagram_packing(mbedtls_ssl_context *ssl,
1644 unsigned allow_packing)
Hanno Becker04da1892018-08-14 13:22:10 +01001645{
1646 ssl->disable_datagram_packing = !allow_packing;
1647}
1648
Gilles Peskine449bd832023-01-11 14:50:10 +01001649void mbedtls_ssl_conf_handshake_timeout(mbedtls_ssl_config *conf,
1650 uint32_t min, uint32_t max)
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001651{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001652 conf->hs_timeout_min = min;
1653 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001654}
1655#endif
1656
Gilles Peskine449bd832023-01-11 14:50:10 +01001657void mbedtls_ssl_conf_authmode(mbedtls_ssl_config *conf, int authmode)
Paul Bakker5121ce52009-01-03 21:22:43 +00001658{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001659 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00001660}
1661
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001662#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001663void mbedtls_ssl_conf_verify(mbedtls_ssl_config *conf,
1664 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
1665 void *p_vrfy)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001666{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001667 conf->f_vrfy = f_vrfy;
1668 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001669}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001670#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001671
Gilles Peskine449bd832023-01-11 14:50:10 +01001672void mbedtls_ssl_conf_rng(mbedtls_ssl_config *conf,
1673 int (*f_rng)(void *, unsigned char *, size_t),
1674 void *p_rng)
Paul Bakker5121ce52009-01-03 21:22:43 +00001675{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001676 conf->f_rng = f_rng;
1677 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00001678}
1679
Gilles Peskine449bd832023-01-11 14:50:10 +01001680void mbedtls_ssl_conf_dbg(mbedtls_ssl_config *conf,
1681 void (*f_dbg)(void *, int, const char *, int, const char *),
1682 void *p_dbg)
Paul Bakker5121ce52009-01-03 21:22:43 +00001683{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001684 conf->f_dbg = f_dbg;
1685 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00001686}
1687
Gilles Peskine449bd832023-01-11 14:50:10 +01001688void mbedtls_ssl_set_bio(mbedtls_ssl_context *ssl,
1689 void *p_bio,
1690 mbedtls_ssl_send_t *f_send,
1691 mbedtls_ssl_recv_t *f_recv,
1692 mbedtls_ssl_recv_timeout_t *f_recv_timeout)
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001693{
1694 ssl->p_bio = p_bio;
1695 ssl->f_send = f_send;
1696 ssl->f_recv = f_recv;
1697 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001698}
1699
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02001700#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001701void mbedtls_ssl_set_mtu(mbedtls_ssl_context *ssl, uint16_t mtu)
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02001702{
1703 ssl->mtu = mtu;
1704}
1705#endif
1706
Gilles Peskine449bd832023-01-11 14:50:10 +01001707void mbedtls_ssl_conf_read_timeout(mbedtls_ssl_config *conf, uint32_t timeout)
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001708{
1709 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001710}
1711
Gilles Peskine449bd832023-01-11 14:50:10 +01001712void mbedtls_ssl_set_timer_cb(mbedtls_ssl_context *ssl,
1713 void *p_timer,
1714 mbedtls_ssl_set_timer_t *f_set_timer,
1715 mbedtls_ssl_get_timer_t *f_get_timer)
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001716{
1717 ssl->p_timer = p_timer;
1718 ssl->f_set_timer = f_set_timer;
1719 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001720
1721 /* Make sure we start with no timer running */
Gilles Peskine449bd832023-01-11 14:50:10 +01001722 mbedtls_ssl_set_timer(ssl, 0);
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001723}
1724
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001725#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001726void mbedtls_ssl_conf_session_cache(mbedtls_ssl_config *conf,
1727 void *p_cache,
1728 mbedtls_ssl_cache_get_t *f_get_cache,
1729 mbedtls_ssl_cache_set_t *f_set_cache)
Paul Bakker5121ce52009-01-03 21:22:43 +00001730{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01001731 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001732 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001733 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00001734}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001735#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001736
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001737#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001738int mbedtls_ssl_set_session(mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session)
Paul Bakker5121ce52009-01-03 21:22:43 +00001739{
Janos Follath865b3eb2019-12-16 11:46:15 +00001740 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001741
Gilles Peskine449bd832023-01-11 14:50:10 +01001742 if (ssl == NULL ||
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001743 session == NULL ||
1744 ssl->session_negotiate == NULL ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001745 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT) {
1746 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001747 }
1748
Gilles Peskine449bd832023-01-11 14:50:10 +01001749 if (ssl->handshake->resume == 1) {
1750 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
1751 }
Hanno Beckere810bbc2021-05-14 16:01:05 +01001752
Jerry Yu21092062022-10-10 21:21:31 +08001753#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001754 if (session->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
Jerry Yu21092062022-10-10 21:21:31 +08001755 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01001756 mbedtls_ssl_ciphersuite_from_id(session->ciphersuite);
Jerry Yu21092062022-10-10 21:21:31 +08001757
Gilles Peskine449bd832023-01-11 14:50:10 +01001758 if (mbedtls_ssl_validate_ciphersuite(
Jerry Yu21092062022-10-10 21:21:31 +08001759 ssl, ciphersuite_info, MBEDTLS_SSL_VERSION_TLS1_3,
Gilles Peskine449bd832023-01-11 14:50:10 +01001760 MBEDTLS_SSL_VERSION_TLS1_3) != 0) {
1761 MBEDTLS_SSL_DEBUG_MSG(4, ("%d is not a valid TLS 1.3 ciphersuite.",
1762 session->ciphersuite));
1763 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jerry Yu21092062022-10-10 21:21:31 +08001764 }
Jerry Yu40afab62022-10-08 10:42:13 +08001765 }
Jerry Yu21092062022-10-10 21:21:31 +08001766#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu40afab62022-10-08 10:42:13 +08001767
Gilles Peskine449bd832023-01-11 14:50:10 +01001768 if ((ret = mbedtls_ssl_session_copy(ssl->session_negotiate,
1769 session)) != 0) {
1770 return ret;
1771 }
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001772
Paul Bakker0a597072012-09-25 21:55:46 +00001773 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001774
Gilles Peskine449bd832023-01-11 14:50:10 +01001775 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001776}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001777#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001778
Gilles Peskine449bd832023-01-11 14:50:10 +01001779void mbedtls_ssl_conf_ciphersuites(mbedtls_ssl_config *conf,
1780 const int *ciphersuites)
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001781{
Hanno Beckerd60b6c62021-04-29 12:04:11 +01001782 conf->ciphersuite_list = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00001783}
1784
Ronald Cron6f135e12021-12-08 16:57:54 +01001785#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001786void mbedtls_ssl_conf_tls13_key_exchange_modes(mbedtls_ssl_config *conf,
1787 const int kex_modes)
Hanno Becker71f1ed62021-07-24 06:01:47 +01001788{
Xiaofei Bai746f9482021-11-12 08:53:56 +00001789 conf->tls13_kex_modes = kex_modes & MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Hanno Becker71f1ed62021-07-24 06:01:47 +01001790}
Xiaokang Qian72de95d2022-10-25 02:54:33 +00001791
1792#if defined(MBEDTLS_SSL_EARLY_DATA)
Gilles Peskine449bd832023-01-11 14:50:10 +01001793void mbedtls_ssl_tls13_conf_early_data(mbedtls_ssl_config *conf,
1794 int early_data_enabled)
Xiaokang Qian72de95d2022-10-25 02:54:33 +00001795{
1796 conf->early_data_enabled = early_data_enabled;
1797}
Jerry Yucc4e0072022-11-22 17:22:22 +08001798
1799#if defined(MBEDTLS_SSL_SRV_C)
1800void mbedtls_ssl_tls13_conf_max_early_data_size(
Gilles Peskine449bd832023-01-11 14:50:10 +01001801 mbedtls_ssl_config *conf, uint32_t max_early_data_size)
Jerry Yucc4e0072022-11-22 17:22:22 +08001802{
Jerry Yu39da9852022-12-06 16:58:36 +08001803 conf->max_early_data_size = max_early_data_size;
Jerry Yucc4e0072022-11-22 17:22:22 +08001804}
1805#endif /* MBEDTLS_SSL_SRV_C */
1806
Xiaokang Qian72de95d2022-10-25 02:54:33 +00001807#endif /* MBEDTLS_SSL_EARLY_DATA */
Ronald Cron6f135e12021-12-08 16:57:54 +01001808#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01001809
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001810#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001811void mbedtls_ssl_conf_cert_profile(mbedtls_ssl_config *conf,
1812 const mbedtls_x509_crt_profile *profile)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02001813{
1814 conf->cert_profile = profile;
1815}
1816
Gilles Peskine449bd832023-01-11 14:50:10 +01001817static void ssl_key_cert_free(mbedtls_ssl_key_cert *key_cert)
Glenn Strauss36872db2022-01-22 05:06:31 -05001818{
1819 mbedtls_ssl_key_cert *cur = key_cert, *next;
1820
Gilles Peskine449bd832023-01-11 14:50:10 +01001821 while (cur != NULL) {
Glenn Strauss36872db2022-01-22 05:06:31 -05001822 next = cur->next;
Gilles Peskine449bd832023-01-11 14:50:10 +01001823 mbedtls_free(cur);
Glenn Strauss36872db2022-01-22 05:06:31 -05001824 cur = next;
1825 }
1826}
1827
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001828/* Append a new keycert entry to a (possibly empty) list */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001829MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001830static int ssl_append_key_cert(mbedtls_ssl_key_cert **head,
1831 mbedtls_x509_crt *cert,
1832 mbedtls_pk_context *key)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001833{
niisato8ee24222018-06-25 19:05:48 +09001834 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001835
Gilles Peskine449bd832023-01-11 14:50:10 +01001836 if (cert == NULL) {
Glenn Strauss36872db2022-01-22 05:06:31 -05001837 /* Free list if cert is null */
Gilles Peskine449bd832023-01-11 14:50:10 +01001838 ssl_key_cert_free(*head);
Glenn Strauss36872db2022-01-22 05:06:31 -05001839 *head = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001840 return 0;
Glenn Strauss36872db2022-01-22 05:06:31 -05001841 }
1842
Gilles Peskine449bd832023-01-11 14:50:10 +01001843 new_cert = mbedtls_calloc(1, sizeof(mbedtls_ssl_key_cert));
1844 if (new_cert == NULL) {
1845 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
1846 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001847
niisato8ee24222018-06-25 19:05:48 +09001848 new_cert->cert = cert;
1849 new_cert->key = key;
1850 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001851
Glenn Strauss36872db2022-01-22 05:06:31 -05001852 /* Update head if the list was null, else add to the end */
Gilles Peskine449bd832023-01-11 14:50:10 +01001853 if (*head == NULL) {
niisato8ee24222018-06-25 19:05:48 +09001854 *head = new_cert;
Gilles Peskine449bd832023-01-11 14:50:10 +01001855 } else {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001856 mbedtls_ssl_key_cert *cur = *head;
Gilles Peskine449bd832023-01-11 14:50:10 +01001857 while (cur->next != NULL) {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001858 cur = cur->next;
Gilles Peskine449bd832023-01-11 14:50:10 +01001859 }
niisato8ee24222018-06-25 19:05:48 +09001860 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001861 }
1862
Gilles Peskine449bd832023-01-11 14:50:10 +01001863 return 0;
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001864}
1865
Gilles Peskine449bd832023-01-11 14:50:10 +01001866int mbedtls_ssl_conf_own_cert(mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001867 mbedtls_x509_crt *own_cert,
Gilles Peskine449bd832023-01-11 14:50:10 +01001868 mbedtls_pk_context *pk_key)
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001869{
Gilles Peskine449bd832023-01-11 14:50:10 +01001870 return ssl_append_key_cert(&conf->key_cert, own_cert, pk_key);
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001871}
1872
Gilles Peskine449bd832023-01-11 14:50:10 +01001873void mbedtls_ssl_conf_ca_chain(mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001874 mbedtls_x509_crt *ca_chain,
Gilles Peskine449bd832023-01-11 14:50:10 +01001875 mbedtls_x509_crl *ca_crl)
Paul Bakker5121ce52009-01-03 21:22:43 +00001876{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001877 conf->ca_chain = ca_chain;
1878 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00001879
1880#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1881 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1882 * cannot be used together. */
1883 conf->f_ca_cb = NULL;
1884 conf->p_ca_cb = NULL;
1885#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00001886}
Hanno Becker5adaad92019-03-27 16:54:37 +00001887
1888#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Gilles Peskine449bd832023-01-11 14:50:10 +01001889void mbedtls_ssl_conf_ca_cb(mbedtls_ssl_config *conf,
1890 mbedtls_x509_crt_ca_cb_t f_ca_cb,
1891 void *p_ca_cb)
Hanno Becker5adaad92019-03-27 16:54:37 +00001892{
1893 conf->f_ca_cb = f_ca_cb;
1894 conf->p_ca_cb = p_ca_cb;
1895
1896 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1897 * cannot be used together. */
1898 conf->ca_chain = NULL;
1899 conf->ca_crl = NULL;
1900}
1901#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001902#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00001903
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001904#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01001905const unsigned char *mbedtls_ssl_get_hs_sni(mbedtls_ssl_context *ssl,
1906 size_t *name_len)
Glenn Strauss69894072022-01-24 12:58:00 -05001907{
1908 *name_len = ssl->handshake->sni_name_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001909 return ssl->handshake->sni_name;
Glenn Strauss69894072022-01-24 12:58:00 -05001910}
1911
Gilles Peskine449bd832023-01-11 14:50:10 +01001912int mbedtls_ssl_set_hs_own_cert(mbedtls_ssl_context *ssl,
1913 mbedtls_x509_crt *own_cert,
1914 mbedtls_pk_context *pk_key)
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001915{
Gilles Peskine449bd832023-01-11 14:50:10 +01001916 return ssl_append_key_cert(&ssl->handshake->sni_key_cert,
1917 own_cert, pk_key);
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001918}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02001919
Gilles Peskine449bd832023-01-11 14:50:10 +01001920void mbedtls_ssl_set_hs_ca_chain(mbedtls_ssl_context *ssl,
1921 mbedtls_x509_crt *ca_chain,
1922 mbedtls_x509_crl *ca_crl)
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02001923{
1924 ssl->handshake->sni_ca_chain = ca_chain;
1925 ssl->handshake->sni_ca_crl = ca_crl;
1926}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001927
Glenn Strauss999ef702022-03-11 01:37:23 -05001928#if defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001929void mbedtls_ssl_set_hs_dn_hints(mbedtls_ssl_context *ssl,
1930 const mbedtls_x509_crt *crt)
Glenn Strauss999ef702022-03-11 01:37:23 -05001931{
1932 ssl->handshake->dn_hints = crt;
1933}
1934#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
1935
Gilles Peskine449bd832023-01-11 14:50:10 +01001936void mbedtls_ssl_set_hs_authmode(mbedtls_ssl_context *ssl,
1937 int authmode)
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001938{
1939 ssl->handshake->sni_authmode = authmode;
1940}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001941#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
1942
Hanno Becker8927c832019-04-03 12:52:50 +01001943#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001944void mbedtls_ssl_set_verify(mbedtls_ssl_context *ssl,
1945 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
1946 void *p_vrfy)
Hanno Becker8927c832019-04-03 12:52:50 +01001947{
1948 ssl->f_vrfy = f_vrfy;
1949 ssl->p_vrfy = p_vrfy;
1950}
1951#endif
1952
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02001953#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001954
Valerio Setti016f6822022-12-09 14:17:50 +01001955#if defined(MBEDTLS_USE_PSA_CRYPTO)
1956static psa_status_t mbedtls_ssl_set_hs_ecjpake_password_common(
Gilles Peskine449bd832023-01-11 14:50:10 +01001957 mbedtls_ssl_context *ssl,
1958 mbedtls_svc_key_id_t pwd)
Valerio Setti016f6822022-12-09 14:17:50 +01001959{
1960 psa_status_t status;
Valerio Setti016f6822022-12-09 14:17:50 +01001961 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
Przemek Stekiel4cd20312023-03-01 11:11:28 +01001962 uint8_t *user = NULL;
1963 size_t user_len = 0;
1964 uint8_t *peer = NULL;
1965 size_t peer_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001966 psa_pake_cs_set_algorithm(&cipher_suite, PSA_ALG_JPAKE);
1967 psa_pake_cs_set_primitive(&cipher_suite,
1968 PSA_PAKE_PRIMITIVE(PSA_PAKE_PRIMITIVE_TYPE_ECC,
1969 PSA_ECC_FAMILY_SECP_R1,
1970 256));
1971 psa_pake_cs_set_hash(&cipher_suite, PSA_ALG_SHA_256);
Valerio Setti016f6822022-12-09 14:17:50 +01001972
Gilles Peskine449bd832023-01-11 14:50:10 +01001973 status = psa_pake_setup(&ssl->handshake->psa_pake_ctx, &cipher_suite);
1974 if (status != PSA_SUCCESS) {
Valerio Setti016f6822022-12-09 14:17:50 +01001975 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001976 }
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001977
Gilles Peskine449bd832023-01-11 14:50:10 +01001978 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Przemek Stekiel18cd6c92023-03-06 15:40:35 +01001979 user = (uint8_t *) JPAKE_SERVER_ID;
1980 user_len = strlen(JPAKE_SERVER_ID);
1981 peer = (uint8_t *) JPAKE_CLIENT_ID;
1982 peer_len = strlen(JPAKE_CLIENT_ID);
Gilles Peskine449bd832023-01-11 14:50:10 +01001983 } else {
Przemek Stekiel18cd6c92023-03-06 15:40:35 +01001984 user = (uint8_t *) JPAKE_CLIENT_ID;
1985 user_len = strlen(JPAKE_CLIENT_ID);
1986 peer = (uint8_t *) JPAKE_SERVER_ID;
1987 peer_len = strlen(JPAKE_SERVER_ID);
Gilles Peskine449bd832023-01-11 14:50:10 +01001988 }
Neil Armstrongca7d5062022-05-31 14:43:23 +02001989
Przemek Stekiel4cd20312023-03-01 11:11:28 +01001990 status = psa_pake_set_user(&ssl->handshake->psa_pake_ctx, user, user_len);
1991 if (status != PSA_SUCCESS) {
1992 return status;
1993 }
1994
1995 status = psa_pake_set_peer(&ssl->handshake->psa_pake_ctx, peer, peer_len);
Gilles Peskine449bd832023-01-11 14:50:10 +01001996 if (status != PSA_SUCCESS) {
Valerio Setti016f6822022-12-09 14:17:50 +01001997 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001998 }
Valerio Setti016f6822022-12-09 14:17:50 +01001999
Gilles Peskine449bd832023-01-11 14:50:10 +01002000 status = psa_pake_set_password_key(&ssl->handshake->psa_pake_ctx, pwd);
2001 if (status != PSA_SUCCESS) {
Valerio Setti016f6822022-12-09 14:17:50 +01002002 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002003 }
Valerio Setti016f6822022-12-09 14:17:50 +01002004
2005 ssl->handshake->psa_pake_ctx_is_ok = 1;
2006
Gilles Peskine449bd832023-01-11 14:50:10 +01002007 return PSA_SUCCESS;
Valerio Setti016f6822022-12-09 14:17:50 +01002008}
2009
Gilles Peskine449bd832023-01-11 14:50:10 +01002010int mbedtls_ssl_set_hs_ecjpake_password(mbedtls_ssl_context *ssl,
2011 const unsigned char *pw,
2012 size_t pw_len)
Valerio Setti016f6822022-12-09 14:17:50 +01002013{
2014 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2015 psa_status_t status;
2016
Gilles Peskine449bd832023-01-11 14:50:10 +01002017 if (ssl->handshake == NULL || ssl->conf == NULL) {
2018 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Neil Armstrongca7d5062022-05-31 14:43:23 +02002019 }
2020
Gilles Peskine449bd832023-01-11 14:50:10 +01002021 /* Empty password is not valid */
2022 if ((pw == NULL) || (pw_len == 0)) {
2023 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2024 }
2025
2026 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
2027 psa_set_key_algorithm(&attributes, PSA_ALG_JPAKE);
2028 psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD);
2029
2030 status = psa_import_key(&attributes, pw, pw_len,
2031 &ssl->handshake->psa_pake_password);
2032 if (status != PSA_SUCCESS) {
2033 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2034 }
2035
2036 status = mbedtls_ssl_set_hs_ecjpake_password_common(ssl,
2037 ssl->handshake->psa_pake_password);
2038 if (status != PSA_SUCCESS) {
2039 psa_destroy_key(ssl->handshake->psa_pake_password);
2040 psa_pake_abort(&ssl->handshake->psa_pake_ctx);
2041 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2042 }
2043
2044 return 0;
Valerio Setti02c25b52022-11-15 14:08:42 +01002045}
Valerio Settia9a97dc2022-11-28 18:26:16 +01002046
Gilles Peskine449bd832023-01-11 14:50:10 +01002047int mbedtls_ssl_set_hs_ecjpake_password_opaque(mbedtls_ssl_context *ssl,
2048 mbedtls_svc_key_id_t pwd)
Valerio Settia9a97dc2022-11-28 18:26:16 +01002049{
Valerio Settia9a97dc2022-11-28 18:26:16 +01002050 psa_status_t status;
2051
Gilles Peskine449bd832023-01-11 14:50:10 +01002052 if (ssl->handshake == NULL || ssl->conf == NULL) {
2053 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Neil Armstrongca7d5062022-05-31 14:43:23 +02002054 }
2055
Gilles Peskine449bd832023-01-11 14:50:10 +01002056 if (mbedtls_svc_key_id_is_null(pwd)) {
2057 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2058 }
2059
2060 status = mbedtls_ssl_set_hs_ecjpake_password_common(ssl, pwd);
2061 if (status != PSA_SUCCESS) {
2062 psa_pake_abort(&ssl->handshake->psa_pake_ctx);
2063 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2064 }
2065
2066 return 0;
Valerio Setti02c25b52022-11-15 14:08:42 +01002067}
2068#else /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01002069int mbedtls_ssl_set_hs_ecjpake_password(mbedtls_ssl_context *ssl,
2070 const unsigned char *pw,
2071 size_t pw_len)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002072{
2073 mbedtls_ecjpake_role role;
2074
Gilles Peskine449bd832023-01-11 14:50:10 +01002075 if (ssl->handshake == NULL || ssl->conf == NULL) {
2076 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2077 }
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002078
Valerio Settic689ed82022-12-07 14:40:38 +01002079 /* Empty password is not valid */
Gilles Peskine449bd832023-01-11 14:50:10 +01002080 if ((pw == NULL) || (pw_len == 0)) {
2081 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2082 }
Valerio Settic689ed82022-12-07 14:40:38 +01002083
Gilles Peskine449bd832023-01-11 14:50:10 +01002084 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002085 role = MBEDTLS_ECJPAKE_SERVER;
Gilles Peskine449bd832023-01-11 14:50:10 +01002086 } else {
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002087 role = MBEDTLS_ECJPAKE_CLIENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01002088 }
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002089
Gilles Peskine449bd832023-01-11 14:50:10 +01002090 return mbedtls_ecjpake_setup(&ssl->handshake->ecjpake_ctx,
2091 role,
2092 MBEDTLS_MD_SHA256,
2093 MBEDTLS_ECP_DP_SECP256R1,
2094 pw, pw_len);
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002095}
Valerio Setti02c25b52022-11-15 14:08:42 +01002096#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02002097#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002098
Ronald Cron73fe8df2022-10-05 14:31:43 +02002099#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002100int mbedtls_ssl_conf_has_static_psk(mbedtls_ssl_config const *conf)
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002101{
Gilles Peskine449bd832023-01-11 14:50:10 +01002102 if (conf->psk_identity == NULL ||
2103 conf->psk_identity_len == 0) {
2104 return 0;
Ronald Crond29e13e2022-10-19 10:33:48 +02002105 }
2106
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002107#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002108 if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) {
2109 return 1;
2110 }
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002111#endif /* MBEDTLS_USE_PSA_CRYPTO */
Ronald Crond29e13e2022-10-19 10:33:48 +02002112
Gilles Peskine449bd832023-01-11 14:50:10 +01002113 if (conf->psk != NULL && conf->psk_len != 0) {
2114 return 1;
2115 }
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002116
Gilles Peskine449bd832023-01-11 14:50:10 +01002117 return 0;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002118}
2119
Gilles Peskine449bd832023-01-11 14:50:10 +01002120static void ssl_conf_remove_psk(mbedtls_ssl_config *conf)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002121{
2122 /* Remove reference to existing PSK, if any. */
2123#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002124 if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) {
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002125 /* The maintenance of the PSK key slot is the
2126 * user's responsibility. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002127 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002128 }
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002129#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01002130 if (conf->psk != NULL) {
2131 mbedtls_platform_zeroize(conf->psk, conf->psk_len);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002132
Gilles Peskine449bd832023-01-11 14:50:10 +01002133 mbedtls_free(conf->psk);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002134 conf->psk = NULL;
2135 conf->psk_len = 0;
2136 }
2137
2138 /* Remove reference to PSK identity, if any. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002139 if (conf->psk_identity != NULL) {
2140 mbedtls_free(conf->psk_identity);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002141 conf->psk_identity = NULL;
2142 conf->psk_identity_len = 0;
2143 }
2144}
2145
Hanno Becker7390c712018-11-15 13:33:04 +00002146/* This function assumes that PSK identity in the SSL config is unset.
2147 * It checks that the provided identity is well-formed and attempts
2148 * to make a copy of it in the SSL config.
2149 * On failure, the PSK identity in the config remains unset. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002150MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002151static int ssl_conf_set_psk_identity(mbedtls_ssl_config *conf,
2152 unsigned char const *psk_identity,
2153 size_t psk_identity_len)
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002154{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02002155 /* Identity len will be encoded on two bytes */
Gilles Peskine449bd832023-01-11 14:50:10 +01002156 if (psk_identity == NULL ||
Ronald Cron2a87e9b2022-10-19 10:55:26 +02002157 psk_identity_len == 0 ||
Gilles Peskine449bd832023-01-11 14:50:10 +01002158 (psk_identity_len >> 16) != 0 ||
2159 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN) {
2160 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02002161 }
2162
Gilles Peskine449bd832023-01-11 14:50:10 +01002163 conf->psk_identity = mbedtls_calloc(1, psk_identity_len);
2164 if (conf->psk_identity == NULL) {
2165 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2166 }
Paul Bakker6db455e2013-09-18 17:29:31 +02002167
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01002168 conf->psk_identity_len = psk_identity_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002169 memcpy(conf->psk_identity, psk_identity, conf->psk_identity_len);
Paul Bakker5ad403f2013-09-18 21:21:30 +02002170
Gilles Peskine449bd832023-01-11 14:50:10 +01002171 return 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02002172}
2173
Gilles Peskine449bd832023-01-11 14:50:10 +01002174int mbedtls_ssl_conf_psk(mbedtls_ssl_config *conf,
2175 const unsigned char *psk, size_t psk_len,
2176 const unsigned char *psk_identity, size_t psk_identity_len)
Hanno Becker7390c712018-11-15 13:33:04 +00002177{
Janos Follath865b3eb2019-12-16 11:46:15 +00002178 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002179
2180 /* We currently only support one PSK, raw or opaque. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002181 if (mbedtls_ssl_conf_has_static_psk(conf)) {
2182 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
2183 }
Hanno Becker7390c712018-11-15 13:33:04 +00002184
2185 /* Check and set raw PSK */
Gilles Peskine449bd832023-01-11 14:50:10 +01002186 if (psk == NULL) {
2187 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2188 }
2189 if (psk_len == 0) {
2190 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2191 }
2192 if (psk_len > MBEDTLS_PSK_MAX_LEN) {
2193 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2194 }
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01002195
Gilles Peskine449bd832023-01-11 14:50:10 +01002196 if ((conf->psk = mbedtls_calloc(1, psk_len)) == NULL) {
2197 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2198 }
Hanno Becker7390c712018-11-15 13:33:04 +00002199 conf->psk_len = psk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002200 memcpy(conf->psk, psk, conf->psk_len);
Hanno Becker7390c712018-11-15 13:33:04 +00002201
2202 /* Check and set PSK Identity */
Gilles Peskine449bd832023-01-11 14:50:10 +01002203 ret = ssl_conf_set_psk_identity(conf, psk_identity, psk_identity_len);
2204 if (ret != 0) {
2205 ssl_conf_remove_psk(conf);
2206 }
Hanno Becker7390c712018-11-15 13:33:04 +00002207
Gilles Peskine449bd832023-01-11 14:50:10 +01002208 return ret;
Hanno Becker7390c712018-11-15 13:33:04 +00002209}
2210
Gilles Peskine449bd832023-01-11 14:50:10 +01002211static void ssl_remove_psk(mbedtls_ssl_context *ssl)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002212{
2213#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002214 if (!mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque)) {
Neil Armstrong501c9322022-05-03 09:35:09 +02002215 /* The maintenance of the external PSK key slot is the
2216 * user's responsibility. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002217 if (ssl->handshake->psk_opaque_is_internal) {
2218 psa_destroy_key(ssl->handshake->psk_opaque);
Neil Armstrong501c9322022-05-03 09:35:09 +02002219 ssl->handshake->psk_opaque_is_internal = 0;
2220 }
Ronald Croncf56a0a2020-08-04 09:51:30 +02002221 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002222 }
Neil Armstronge952a302022-05-03 10:22:14 +02002223#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002224 if (ssl->handshake->psk != NULL) {
2225 mbedtls_platform_zeroize(ssl->handshake->psk,
2226 ssl->handshake->psk_len);
2227 mbedtls_free(ssl->handshake->psk);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002228 ssl->handshake->psk_len = 0;
2229 }
Neil Armstronge952a302022-05-03 10:22:14 +02002230#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002231}
2232
Gilles Peskine449bd832023-01-11 14:50:10 +01002233int mbedtls_ssl_set_hs_psk(mbedtls_ssl_context *ssl,
2234 const unsigned char *psk, size_t psk_len)
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002235{
Neil Armstrong501c9322022-05-03 09:35:09 +02002236#if defined(MBEDTLS_USE_PSA_CRYPTO)
2237 psa_key_attributes_t key_attributes = psa_key_attributes_init();
Jerry Yu5d01c052022-08-17 10:18:10 +08002238 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jerry Yu24b8c812022-08-20 19:06:56 +08002239 psa_algorithm_t alg = PSA_ALG_NONE;
Jerry Yu5d01c052022-08-17 10:18:10 +08002240 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrong501c9322022-05-03 09:35:09 +02002241#endif /* MBEDTLS_USE_PSA_CRYPTO */
2242
Gilles Peskine449bd832023-01-11 14:50:10 +01002243 if (psk == NULL || ssl->handshake == NULL) {
2244 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2245 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002246
Gilles Peskine449bd832023-01-11 14:50:10 +01002247 if (psk_len > MBEDTLS_PSK_MAX_LEN) {
2248 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2249 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002250
Gilles Peskine449bd832023-01-11 14:50:10 +01002251 ssl_remove_psk(ssl);
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002252
Neil Armstrong501c9322022-05-03 09:35:09 +02002253#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yuccc68a42022-07-26 16:39:20 +08002254#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01002255 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2) {
2256 if (ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384) {
2257 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
2258 } else {
2259 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
2260 }
2261 psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE);
Jerry Yuccc68a42022-07-26 16:39:20 +08002262 }
2263#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Neil Armstrong501c9322022-05-03 09:35:09 +02002264
Jerry Yu568ec252022-07-22 21:27:34 +08002265#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01002266 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
2267 alg = PSA_ALG_HKDF_EXTRACT(PSA_ALG_ANY_HASH);
2268 psa_set_key_usage_flags(&key_attributes,
2269 PSA_KEY_USAGE_DERIVE | PSA_KEY_USAGE_EXPORT);
Jerry Yuccc68a42022-07-26 16:39:20 +08002270 }
2271#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
2272
Gilles Peskine449bd832023-01-11 14:50:10 +01002273 psa_set_key_algorithm(&key_attributes, alg);
2274 psa_set_key_type(&key_attributes, PSA_KEY_TYPE_DERIVE);
Neil Armstrong501c9322022-05-03 09:35:09 +02002275
Gilles Peskine449bd832023-01-11 14:50:10 +01002276 status = psa_import_key(&key_attributes, psk, psk_len, &key);
2277 if (status != PSA_SUCCESS) {
2278 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2279 }
Neil Armstrong501c9322022-05-03 09:35:09 +02002280
2281 /* Allow calling psa_destroy_key() on psk remove */
2282 ssl->handshake->psk_opaque_is_internal = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01002283 return mbedtls_ssl_set_hs_psk_opaque(ssl, key);
Neil Armstrong501c9322022-05-03 09:35:09 +02002284#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002285 if ((ssl->handshake->psk = mbedtls_calloc(1, psk_len)) == NULL) {
2286 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2287 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002288
2289 ssl->handshake->psk_len = psk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002290 memcpy(ssl->handshake->psk, psk, ssl->handshake->psk_len);
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002291
Gilles Peskine449bd832023-01-11 14:50:10 +01002292 return 0;
Neil Armstrong501c9322022-05-03 09:35:09 +02002293#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002294}
2295
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002296#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002297int mbedtls_ssl_conf_psk_opaque(mbedtls_ssl_config *conf,
2298 mbedtls_svc_key_id_t psk,
2299 const unsigned char *psk_identity,
2300 size_t psk_identity_len)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002301{
Janos Follath865b3eb2019-12-16 11:46:15 +00002302 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002303
2304 /* We currently only support one PSK, raw or opaque. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002305 if (mbedtls_ssl_conf_has_static_psk(conf)) {
2306 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
2307 }
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002308
Hanno Becker7390c712018-11-15 13:33:04 +00002309 /* Check and set opaque PSK */
Gilles Peskine449bd832023-01-11 14:50:10 +01002310 if (mbedtls_svc_key_id_is_null(psk)) {
2311 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2312 }
Ronald Croncf56a0a2020-08-04 09:51:30 +02002313 conf->psk_opaque = psk;
Hanno Becker7390c712018-11-15 13:33:04 +00002314
2315 /* Check and set PSK Identity */
Gilles Peskine449bd832023-01-11 14:50:10 +01002316 ret = ssl_conf_set_psk_identity(conf, psk_identity,
2317 psk_identity_len);
2318 if (ret != 0) {
2319 ssl_conf_remove_psk(conf);
2320 }
Hanno Becker7390c712018-11-15 13:33:04 +00002321
Gilles Peskine449bd832023-01-11 14:50:10 +01002322 return ret;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002323}
2324
Gilles Peskine449bd832023-01-11 14:50:10 +01002325int mbedtls_ssl_set_hs_psk_opaque(mbedtls_ssl_context *ssl,
2326 mbedtls_svc_key_id_t psk)
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002327{
Gilles Peskine449bd832023-01-11 14:50:10 +01002328 if ((mbedtls_svc_key_id_is_null(psk)) ||
2329 (ssl->handshake == NULL)) {
2330 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2331 }
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002332
Gilles Peskine449bd832023-01-11 14:50:10 +01002333 ssl_remove_psk(ssl);
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002334 ssl->handshake->psk_opaque = psk;
Gilles Peskine449bd832023-01-11 14:50:10 +01002335 return 0;
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002336}
2337#endif /* MBEDTLS_USE_PSA_CRYPTO */
2338
Jerry Yu8897c072022-08-12 13:56:53 +08002339#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002340void mbedtls_ssl_conf_psk_cb(mbedtls_ssl_config *conf,
2341 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
2342 size_t),
2343 void *p_psk)
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002344{
2345 conf->f_psk = f_psk;
2346 conf->p_psk = p_psk;
2347}
Jerry Yu8897c072022-08-12 13:56:53 +08002348#endif /* MBEDTLS_SSL_SRV_C */
2349
Ronald Cron73fe8df2022-10-05 14:31:43 +02002350#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002351
2352#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine301711e2022-04-26 16:57:05 +02002353static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
Gilles Peskine449bd832023-01-11 14:50:10 +01002354 psa_algorithm_t alg)
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002355{
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002356#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01002357 if (alg == PSA_ALG_CBC_NO_PADDING) {
2358 return MBEDTLS_SSL_MODE_CBC;
2359 }
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002360#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
Gilles Peskine449bd832023-01-11 14:50:10 +01002361 if (PSA_ALG_IS_AEAD(alg)) {
2362 return MBEDTLS_SSL_MODE_AEAD;
2363 }
2364 return MBEDTLS_SSL_MODE_STREAM;
Gilles Peskine301711e2022-04-26 16:57:05 +02002365}
2366
2367#else /* MBEDTLS_USE_PSA_CRYPTO */
2368
2369static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
Gilles Peskine449bd832023-01-11 14:50:10 +01002370 mbedtls_cipher_mode_t mode)
Gilles Peskine301711e2022-04-26 16:57:05 +02002371{
2372#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01002373 if (mode == MBEDTLS_MODE_CBC) {
2374 return MBEDTLS_SSL_MODE_CBC;
2375 }
Gilles Peskine301711e2022-04-26 16:57:05 +02002376#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
2377
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002378#if defined(MBEDTLS_GCM_C) || \
2379 defined(MBEDTLS_CCM_C) || \
2380 defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002381 if (mode == MBEDTLS_MODE_GCM ||
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002382 mode == MBEDTLS_MODE_CCM ||
Gilles Peskine449bd832023-01-11 14:50:10 +01002383 mode == MBEDTLS_MODE_CHACHAPOLY) {
2384 return MBEDTLS_SSL_MODE_AEAD;
2385 }
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002386#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002387
Gilles Peskine449bd832023-01-11 14:50:10 +01002388 return MBEDTLS_SSL_MODE_STREAM;
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002389}
Gilles Peskine301711e2022-04-26 16:57:05 +02002390#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002391
Gilles Peskinee108d982022-04-26 16:50:40 +02002392static mbedtls_ssl_mode_t mbedtls_ssl_get_actual_mode(
2393 mbedtls_ssl_mode_t base_mode,
Gilles Peskine449bd832023-01-11 14:50:10 +01002394 int encrypt_then_mac)
Gilles Peskinee108d982022-04-26 16:50:40 +02002395{
2396#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01002397 if (encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED &&
2398 base_mode == MBEDTLS_SSL_MODE_CBC) {
2399 return MBEDTLS_SSL_MODE_CBC_ETM;
Gilles Peskinee108d982022-04-26 16:50:40 +02002400 }
2401#else
2402 (void) encrypt_then_mac;
2403#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002404 return base_mode;
Gilles Peskinee108d982022-04-26 16:50:40 +02002405}
2406
Neil Armstrongab555e02022-04-04 11:07:59 +02002407mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_transform(
Gilles Peskine449bd832023-01-11 14:50:10 +01002408 const mbedtls_ssl_transform *transform)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002409{
Gilles Peskinee108d982022-04-26 16:50:40 +02002410 mbedtls_ssl_mode_t base_mode = mbedtls_ssl_get_base_mode(
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002411#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002412 transform->psa_alg
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002413#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002414 mbedtls_cipher_get_cipher_mode(&transform->cipher_ctx_enc)
Gilles Peskinee108d982022-04-26 16:50:40 +02002415#endif
2416 );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002417
Gilles Peskinee108d982022-04-26 16:50:40 +02002418 int encrypt_then_mac = 0;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002419#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskinee108d982022-04-26 16:50:40 +02002420 encrypt_then_mac = transform->encrypt_then_mac;
2421#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002422 return mbedtls_ssl_get_actual_mode(base_mode, encrypt_then_mac);
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002423}
2424
Neil Armstrongab555e02022-04-04 11:07:59 +02002425mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002426#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01002427 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002428#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01002429 const mbedtls_ssl_ciphersuite_t *suite)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002430{
Gilles Peskinee108d982022-04-26 16:50:40 +02002431 mbedtls_ssl_mode_t base_mode = MBEDTLS_SSL_MODE_STREAM;
2432
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002433#if defined(MBEDTLS_USE_PSA_CRYPTO)
2434 psa_status_t status;
2435 psa_algorithm_t alg;
2436 psa_key_type_t type;
2437 size_t size;
Gilles Peskine449bd832023-01-11 14:50:10 +01002438 status = mbedtls_ssl_cipher_to_psa(suite->cipher, 0, &alg, &type, &size);
2439 if (status == PSA_SUCCESS) {
2440 base_mode = mbedtls_ssl_get_base_mode(alg);
2441 }
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002442#else
2443 const mbedtls_cipher_info_t *cipher =
Gilles Peskine449bd832023-01-11 14:50:10 +01002444 mbedtls_cipher_info_from_type(suite->cipher);
2445 if (cipher != NULL) {
Gilles Peskinee108d982022-04-26 16:50:40 +02002446 base_mode =
2447 mbedtls_ssl_get_base_mode(
Gilles Peskine449bd832023-01-11 14:50:10 +01002448 mbedtls_cipher_info_get_mode(cipher));
Gilles Peskinee108d982022-04-26 16:50:40 +02002449 }
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002450#endif /* MBEDTLS_USE_PSA_CRYPTO */
2451
Gilles Peskinee108d982022-04-26 16:50:40 +02002452#if !defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
2453 int encrypt_then_mac = 0;
2454#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002455 return mbedtls_ssl_get_actual_mode(base_mode, encrypt_then_mac);
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002456}
2457
Neil Armstrong8395d7a2022-05-18 11:44:56 +02002458#if defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu251a12e2022-07-13 15:15:48 +08002459
2460#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu438ddd82022-07-07 06:55:50 +00002461/* Serialization of TLS 1.3 sessions:
2462 *
2463 * struct {
Xiaokang Qian126bf8e2022-10-13 02:22:40 +00002464 * opaque hostname<0..2^16-1>;
Jerry Yu438ddd82022-07-07 06:55:50 +00002465 * uint64 ticket_received;
2466 * uint32 ticket_lifetime;
Jerry Yu34191072022-08-18 10:32:09 +08002467 * opaque ticket<1..2^16-1>;
Jerry Yu438ddd82022-07-07 06:55:50 +00002468 * } ClientOnlyData;
2469 *
2470 * struct {
2471 * uint8 endpoint;
2472 * uint8 ciphersuite[2];
2473 * uint32 ticket_age_add;
2474 * uint8 ticket_flags;
2475 * opaque resumption_key<0..255>;
2476 * select ( endpoint ) {
2477 * case client: ClientOnlyData;
2478 * case server: uint64 start_time;
2479 * };
2480 * } serialized_session_tls13;
2481 *
2482 */
2483#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yue36fdd62022-08-17 21:31:36 +08002484MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002485static int ssl_tls13_session_save(const mbedtls_ssl_session *session,
2486 unsigned char *buf,
2487 size_t buf_len,
2488 size_t *olen)
Jerry Yu438ddd82022-07-07 06:55:50 +00002489{
2490 unsigned char *p = buf;
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00002491#if defined(MBEDTLS_SSL_CLI_C) && \
2492 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01002493 size_t hostname_len = (session->hostname == NULL) ?
2494 0 : strlen(session->hostname) + 1;
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00002495#endif
Jerry Yubc7c1a42022-07-21 22:57:37 +08002496 size_t needed = 1 /* endpoint */
2497 + 2 /* ciphersuite */
2498 + 4 /* ticket_age_add */
Jerry Yu34191072022-08-18 10:32:09 +08002499 + 1 /* ticket_flags */
2500 + 1; /* resumption_key length */
Jerry Yue36fdd62022-08-17 21:31:36 +08002501 *olen = 0;
Jerry Yu34191072022-08-18 10:32:09 +08002502
Gilles Peskine449bd832023-01-11 14:50:10 +01002503 if (session->resumption_key_len > MBEDTLS_SSL_TLS1_3_TICKET_RESUMPTION_KEY_LEN) {
2504 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2505 }
Jerry Yu34191072022-08-18 10:32:09 +08002506 needed += session->resumption_key_len; /* resumption_key */
2507
Jerry Yu438ddd82022-07-07 06:55:50 +00002508#if defined(MBEDTLS_HAVE_TIME)
2509 needed += 8; /* start_time or ticket_received */
2510#endif
2511
2512#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002513 if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Xiaokang Qian126bf8e2022-10-13 02:22:40 +00002514#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaokang Qianbc663a02022-10-09 11:14:39 +00002515 needed += 2 /* hostname_len */
Gilles Peskine449bd832023-01-11 14:50:10 +01002516 + hostname_len; /* hostname */
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00002517#endif
2518
Jerry Yu438ddd82022-07-07 06:55:50 +00002519 needed += 4 /* ticket_lifetime */
Jerry Yue36fdd62022-08-17 21:31:36 +08002520 + 2; /* ticket_len */
Jerry Yu34191072022-08-18 10:32:09 +08002521
2522 /* Check size_t overflow */
Gilles Peskine449bd832023-01-11 14:50:10 +01002523 if (session->ticket_len > SIZE_MAX - needed) {
2524 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2525 }
Jerry Yu34191072022-08-18 10:32:09 +08002526
Jerry Yue28d9742022-08-18 15:44:03 +08002527 needed += session->ticket_len; /* ticket */
Jerry Yu438ddd82022-07-07 06:55:50 +00002528 }
2529#endif /* MBEDTLS_SSL_CLI_C */
2530
Jerry Yue36fdd62022-08-17 21:31:36 +08002531 *olen = needed;
Gilles Peskine449bd832023-01-11 14:50:10 +01002532 if (needed > buf_len) {
2533 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
2534 }
Jerry Yu438ddd82022-07-07 06:55:50 +00002535
2536 p[0] = session->endpoint;
Gilles Peskine449bd832023-01-11 14:50:10 +01002537 MBEDTLS_PUT_UINT16_BE(session->ciphersuite, p, 1);
2538 MBEDTLS_PUT_UINT32_BE(session->ticket_age_add, p, 3);
Jerry Yu438ddd82022-07-07 06:55:50 +00002539 p[7] = session->ticket_flags;
2540
2541 /* save resumption_key */
Jerry Yubc7c1a42022-07-21 22:57:37 +08002542 p[8] = session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002543 p += 9;
Gilles Peskine449bd832023-01-11 14:50:10 +01002544 memcpy(p, session->resumption_key, session->resumption_key_len);
Jerry Yubc7c1a42022-07-21 22:57:37 +08002545 p += session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002546
2547#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002548 if (session->endpoint == MBEDTLS_SSL_IS_SERVER) {
2549 MBEDTLS_PUT_UINT64_BE((uint64_t) session->start, p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002550 p += 8;
2551 }
2552#endif /* MBEDTLS_HAVE_TIME */
2553
2554#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002555 if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002556#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01002557 MBEDTLS_PUT_UINT16_BE(hostname_len, p, 0);
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002558 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002559 if (hostname_len > 0) {
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002560 /* save host name */
Gilles Peskine449bd832023-01-11 14:50:10 +01002561 memcpy(p, session->hostname, hostname_len);
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002562 p += hostname_len;
2563 }
2564#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
2565
Jerry Yu438ddd82022-07-07 06:55:50 +00002566#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01002567 MBEDTLS_PUT_UINT64_BE((uint64_t) session->ticket_received, p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002568 p += 8;
2569#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002570 MBEDTLS_PUT_UINT32_BE(session->ticket_lifetime, p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002571 p += 4;
2572
Gilles Peskine449bd832023-01-11 14:50:10 +01002573 MBEDTLS_PUT_UINT16_BE(session->ticket_len, p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002574 p += 2;
Jerry Yu34191072022-08-18 10:32:09 +08002575
Gilles Peskine449bd832023-01-11 14:50:10 +01002576 if (session->ticket != NULL && session->ticket_len > 0) {
2577 memcpy(p, session->ticket, session->ticket_len);
Jerry Yu438ddd82022-07-07 06:55:50 +00002578 p += session->ticket_len;
2579 }
2580 }
2581#endif /* MBEDTLS_SSL_CLI_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01002582 return 0;
Jerry Yu438ddd82022-07-07 06:55:50 +00002583}
2584
2585MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002586static int ssl_tls13_session_load(mbedtls_ssl_session *session,
2587 const unsigned char *buf,
2588 size_t len)
Jerry Yu438ddd82022-07-07 06:55:50 +00002589{
2590 const unsigned char *p = buf;
2591 const unsigned char *end = buf + len;
2592
Gilles Peskine449bd832023-01-11 14:50:10 +01002593 if (end - p < 9) {
2594 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2595 }
Jerry Yu438ddd82022-07-07 06:55:50 +00002596 session->endpoint = p[0];
Gilles Peskine449bd832023-01-11 14:50:10 +01002597 session->ciphersuite = MBEDTLS_GET_UINT16_BE(p, 1);
2598 session->ticket_age_add = MBEDTLS_GET_UINT32_BE(p, 3);
Jerry Yu438ddd82022-07-07 06:55:50 +00002599 session->ticket_flags = p[7];
2600
2601 /* load resumption_key */
Jerry Yubc7c1a42022-07-21 22:57:37 +08002602 session->resumption_key_len = p[8];
Jerry Yu438ddd82022-07-07 06:55:50 +00002603 p += 9;
2604
Gilles Peskine449bd832023-01-11 14:50:10 +01002605 if (end - p < session->resumption_key_len) {
2606 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2607 }
Jerry Yu438ddd82022-07-07 06:55:50 +00002608
Gilles Peskine449bd832023-01-11 14:50:10 +01002609 if (sizeof(session->resumption_key) < session->resumption_key_len) {
2610 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2611 }
2612 memcpy(session->resumption_key, p, session->resumption_key_len);
Jerry Yubc7c1a42022-07-21 22:57:37 +08002613 p += session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002614
2615#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002616 if (session->endpoint == MBEDTLS_SSL_IS_SERVER) {
2617 if (end - p < 8) {
2618 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2619 }
2620 session->start = MBEDTLS_GET_UINT64_BE(p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002621 p += 8;
2622 }
2623#endif /* MBEDTLS_HAVE_TIME */
2624
2625#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002626 if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002627#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01002628 defined(MBEDTLS_SSL_SESSION_TICKETS)
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002629 size_t hostname_len;
2630 /* load host name */
Gilles Peskine449bd832023-01-11 14:50:10 +01002631 if (end - p < 2) {
2632 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2633 }
2634 hostname_len = MBEDTLS_GET_UINT16_BE(p, 0);
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002635 p += 2;
2636
Gilles Peskine449bd832023-01-11 14:50:10 +01002637 if (end - p < (long int) hostname_len) {
2638 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2639 }
2640 if (hostname_len > 0) {
2641 session->hostname = mbedtls_calloc(1, hostname_len);
2642 if (session->hostname == NULL) {
2643 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2644 }
2645 memcpy(session->hostname, p, hostname_len);
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002646 p += hostname_len;
2647 }
2648#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION &&
2649 MBEDTLS_SSL_SESSION_TICKETS */
2650
Jerry Yu438ddd82022-07-07 06:55:50 +00002651#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01002652 if (end - p < 8) {
2653 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2654 }
2655 session->ticket_received = MBEDTLS_GET_UINT64_BE(p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002656 p += 8;
2657#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002658 if (end - p < 4) {
2659 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2660 }
2661 session->ticket_lifetime = MBEDTLS_GET_UINT32_BE(p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002662 p += 4;
2663
Gilles Peskine449bd832023-01-11 14:50:10 +01002664 if (end - p < 2) {
2665 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2666 }
2667 session->ticket_len = MBEDTLS_GET_UINT16_BE(p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002668 p += 2;
2669
Gilles Peskine449bd832023-01-11 14:50:10 +01002670 if (end - p < (long int) session->ticket_len) {
2671 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2672 }
2673 if (session->ticket_len > 0) {
2674 session->ticket = mbedtls_calloc(1, session->ticket_len);
2675 if (session->ticket == NULL) {
2676 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2677 }
2678 memcpy(session->ticket, p, session->ticket_len);
Jerry Yu438ddd82022-07-07 06:55:50 +00002679 p += session->ticket_len;
2680 }
2681 }
2682#endif /* MBEDTLS_SSL_CLI_C */
2683
Gilles Peskine449bd832023-01-11 14:50:10 +01002684 return 0;
Jerry Yu438ddd82022-07-07 06:55:50 +00002685
2686}
2687#else /* MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yue36fdd62022-08-17 21:31:36 +08002688MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002689static int ssl_tls13_session_save(const mbedtls_ssl_session *session,
2690 unsigned char *buf,
2691 size_t buf_len,
2692 size_t *olen)
Jerry Yu251a12e2022-07-13 15:15:48 +08002693{
2694 ((void) session);
2695 ((void) buf);
2696 ((void) buf_len);
Jerry Yue36fdd62022-08-17 21:31:36 +08002697 *olen = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002698 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Jerry Yu251a12e2022-07-13 15:15:48 +08002699}
Jerry Yu438ddd82022-07-07 06:55:50 +00002700
Gilles Peskine449bd832023-01-11 14:50:10 +01002701static int ssl_tls13_session_load(const mbedtls_ssl_session *session,
2702 unsigned char *buf,
2703 size_t buf_len)
Jerry Yu438ddd82022-07-07 06:55:50 +00002704{
2705 ((void) session);
2706 ((void) buf);
2707 ((void) buf_len);
Gilles Peskine449bd832023-01-11 14:50:10 +01002708 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Jerry Yu438ddd82022-07-07 06:55:50 +00002709}
2710#endif /* !MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yu251a12e2022-07-13 15:15:48 +08002711#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
2712
Gilles Peskine449bd832023-01-11 14:50:10 +01002713psa_status_t mbedtls_ssl_cipher_to_psa(mbedtls_cipher_type_t mbedtls_cipher_type,
2714 size_t taglen,
2715 psa_algorithm_t *alg,
2716 psa_key_type_t *key_type,
2717 size_t *key_size)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002718{
Gilles Peskine449bd832023-01-11 14:50:10 +01002719 switch (mbedtls_cipher_type) {
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002720 case MBEDTLS_CIPHER_AES_128_CBC:
2721 *alg = PSA_ALG_CBC_NO_PADDING;
2722 *key_type = PSA_KEY_TYPE_AES;
2723 *key_size = 128;
2724 break;
2725 case MBEDTLS_CIPHER_AES_128_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002726 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002727 *key_type = PSA_KEY_TYPE_AES;
2728 *key_size = 128;
2729 break;
2730 case MBEDTLS_CIPHER_AES_128_GCM:
2731 *alg = PSA_ALG_GCM;
2732 *key_type = PSA_KEY_TYPE_AES;
2733 *key_size = 128;
2734 break;
2735 case MBEDTLS_CIPHER_AES_192_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002736 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002737 *key_type = PSA_KEY_TYPE_AES;
2738 *key_size = 192;
2739 break;
2740 case MBEDTLS_CIPHER_AES_192_GCM:
2741 *alg = PSA_ALG_GCM;
2742 *key_type = PSA_KEY_TYPE_AES;
2743 *key_size = 192;
2744 break;
2745 case MBEDTLS_CIPHER_AES_256_CBC:
2746 *alg = PSA_ALG_CBC_NO_PADDING;
2747 *key_type = PSA_KEY_TYPE_AES;
2748 *key_size = 256;
2749 break;
2750 case MBEDTLS_CIPHER_AES_256_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002751 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002752 *key_type = PSA_KEY_TYPE_AES;
2753 *key_size = 256;
2754 break;
2755 case MBEDTLS_CIPHER_AES_256_GCM:
2756 *alg = PSA_ALG_GCM;
2757 *key_type = PSA_KEY_TYPE_AES;
2758 *key_size = 256;
2759 break;
2760 case MBEDTLS_CIPHER_ARIA_128_CBC:
2761 *alg = PSA_ALG_CBC_NO_PADDING;
2762 *key_type = PSA_KEY_TYPE_ARIA;
2763 *key_size = 128;
2764 break;
2765 case MBEDTLS_CIPHER_ARIA_128_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002766 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002767 *key_type = PSA_KEY_TYPE_ARIA;
2768 *key_size = 128;
2769 break;
2770 case MBEDTLS_CIPHER_ARIA_128_GCM:
2771 *alg = PSA_ALG_GCM;
2772 *key_type = PSA_KEY_TYPE_ARIA;
2773 *key_size = 128;
2774 break;
2775 case MBEDTLS_CIPHER_ARIA_192_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002776 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002777 *key_type = PSA_KEY_TYPE_ARIA;
2778 *key_size = 192;
2779 break;
2780 case MBEDTLS_CIPHER_ARIA_192_GCM:
2781 *alg = PSA_ALG_GCM;
2782 *key_type = PSA_KEY_TYPE_ARIA;
2783 *key_size = 192;
2784 break;
2785 case MBEDTLS_CIPHER_ARIA_256_CBC:
2786 *alg = PSA_ALG_CBC_NO_PADDING;
2787 *key_type = PSA_KEY_TYPE_ARIA;
2788 *key_size = 256;
2789 break;
2790 case MBEDTLS_CIPHER_ARIA_256_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002791 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002792 *key_type = PSA_KEY_TYPE_ARIA;
2793 *key_size = 256;
2794 break;
2795 case MBEDTLS_CIPHER_ARIA_256_GCM:
2796 *alg = PSA_ALG_GCM;
2797 *key_type = PSA_KEY_TYPE_ARIA;
2798 *key_size = 256;
2799 break;
2800 case MBEDTLS_CIPHER_CAMELLIA_128_CBC:
2801 *alg = PSA_ALG_CBC_NO_PADDING;
2802 *key_type = PSA_KEY_TYPE_CAMELLIA;
2803 *key_size = 128;
2804 break;
2805 case MBEDTLS_CIPHER_CAMELLIA_128_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002806 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002807 *key_type = PSA_KEY_TYPE_CAMELLIA;
2808 *key_size = 128;
2809 break;
2810 case MBEDTLS_CIPHER_CAMELLIA_128_GCM:
2811 *alg = PSA_ALG_GCM;
2812 *key_type = PSA_KEY_TYPE_CAMELLIA;
2813 *key_size = 128;
2814 break;
2815 case MBEDTLS_CIPHER_CAMELLIA_192_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002816 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002817 *key_type = PSA_KEY_TYPE_CAMELLIA;
2818 *key_size = 192;
2819 break;
2820 case MBEDTLS_CIPHER_CAMELLIA_192_GCM:
2821 *alg = PSA_ALG_GCM;
2822 *key_type = PSA_KEY_TYPE_CAMELLIA;
2823 *key_size = 192;
2824 break;
2825 case MBEDTLS_CIPHER_CAMELLIA_256_CBC:
2826 *alg = PSA_ALG_CBC_NO_PADDING;
2827 *key_type = PSA_KEY_TYPE_CAMELLIA;
2828 *key_size = 256;
2829 break;
2830 case MBEDTLS_CIPHER_CAMELLIA_256_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002831 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002832 *key_type = PSA_KEY_TYPE_CAMELLIA;
2833 *key_size = 256;
2834 break;
2835 case MBEDTLS_CIPHER_CAMELLIA_256_GCM:
2836 *alg = PSA_ALG_GCM;
2837 *key_type = PSA_KEY_TYPE_CAMELLIA;
2838 *key_size = 256;
2839 break;
2840 case MBEDTLS_CIPHER_CHACHA20_POLY1305:
2841 *alg = PSA_ALG_CHACHA20_POLY1305;
2842 *key_type = PSA_KEY_TYPE_CHACHA20;
2843 *key_size = 256;
2844 break;
2845 case MBEDTLS_CIPHER_NULL:
2846 *alg = MBEDTLS_SSL_NULL_CIPHER;
2847 *key_type = 0;
2848 *key_size = 0;
2849 break;
2850 default:
2851 return PSA_ERROR_NOT_SUPPORTED;
2852 }
2853
2854 return PSA_SUCCESS;
2855}
Neil Armstrong8395d7a2022-05-18 11:44:56 +02002856#endif /* MBEDTLS_USE_PSA_CRYPTO || MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002857
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002858#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002859int mbedtls_ssl_conf_dh_param_bin(mbedtls_ssl_config *conf,
2860 const unsigned char *dhm_P, size_t P_len,
2861 const unsigned char *dhm_G, size_t G_len)
Hanno Beckera90658f2017-10-04 15:29:08 +01002862{
Janos Follath865b3eb2019-12-16 11:46:15 +00002863 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckera90658f2017-10-04 15:29:08 +01002864
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_mpi_read_binary(&conf->dhm_P, dhm_P, P_len)) != 0 ||
2869 (ret = mbedtls_mpi_read_binary(&conf->dhm_G, dhm_G, G_len)) != 0) {
2870 mbedtls_mpi_free(&conf->dhm_P);
2871 mbedtls_mpi_free(&conf->dhm_G);
2872 return ret;
Hanno Beckera90658f2017-10-04 15:29:08 +01002873 }
2874
Gilles Peskine449bd832023-01-11 14:50:10 +01002875 return 0;
Hanno Beckera90658f2017-10-04 15:29:08 +01002876}
Paul Bakker5121ce52009-01-03 21:22:43 +00002877
Gilles Peskine449bd832023-01-11 14:50:10 +01002878int mbedtls_ssl_conf_dh_param_ctx(mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx)
Paul Bakker1b57b062011-01-06 15:48:19 +00002879{
Janos Follath865b3eb2019-12-16 11:46:15 +00002880 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1b57b062011-01-06 15:48:19 +00002881
Gilles Peskine449bd832023-01-11 14:50:10 +01002882 mbedtls_mpi_free(&conf->dhm_P);
2883 mbedtls_mpi_free(&conf->dhm_G);
Glenn Strausscee11292021-12-20 01:43:17 -05002884
Gilles Peskine449bd832023-01-11 14:50:10 +01002885 if ((ret = mbedtls_dhm_get_value(dhm_ctx, MBEDTLS_DHM_PARAM_P,
2886 &conf->dhm_P)) != 0 ||
2887 (ret = mbedtls_dhm_get_value(dhm_ctx, MBEDTLS_DHM_PARAM_G,
2888 &conf->dhm_G)) != 0) {
2889 mbedtls_mpi_free(&conf->dhm_P);
2890 mbedtls_mpi_free(&conf->dhm_G);
2891 return ret;
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01002892 }
Paul Bakker1b57b062011-01-06 15:48:19 +00002893
Gilles Peskine449bd832023-01-11 14:50:10 +01002894 return 0;
Paul Bakker1b57b062011-01-06 15:48:19 +00002895}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002896#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00002897
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02002898#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
2899/*
2900 * Set the minimum length for Diffie-Hellman parameters
2901 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002902void mbedtls_ssl_conf_dhm_min_bitlen(mbedtls_ssl_config *conf,
2903 unsigned int bitlen)
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02002904{
2905 conf->dhm_min_bitlen = bitlen;
2906}
2907#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
2908
Ronald Crone68ab4f2022-10-05 12:46:29 +02002909#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002910#if !defined(MBEDTLS_DEPRECATED_REMOVED) && defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002911/*
2912 * Set allowed/preferred hashes for handshake signatures
2913 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002914void mbedtls_ssl_conf_sig_hashes(mbedtls_ssl_config *conf,
2915 const int *hashes)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002916{
2917 conf->sig_hashes = hashes;
2918}
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002919#endif /* !MBEDTLS_DEPRECATED_REMOVED && MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker1cd6e002021-08-10 13:27:10 +01002920
Jerry Yuf017ee42022-01-12 15:49:48 +08002921/* Configure allowed signature algorithms for handshake */
Gilles Peskine449bd832023-01-11 14:50:10 +01002922void mbedtls_ssl_conf_sig_algs(mbedtls_ssl_config *conf,
2923 const uint16_t *sig_algs)
Hanno Becker1cd6e002021-08-10 13:27:10 +01002924{
Jerry Yuf017ee42022-01-12 15:49:48 +08002925#if !defined(MBEDTLS_DEPRECATED_REMOVED)
2926 conf->sig_hashes = NULL;
2927#endif /* !MBEDTLS_DEPRECATED_REMOVED */
2928 conf->sig_algs = sig_algs;
Hanno Becker1cd6e002021-08-10 13:27:10 +01002929}
Ronald Crone68ab4f2022-10-05 12:46:29 +02002930#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002931
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02002932#if defined(MBEDTLS_ECP_C)
Brett Warrene0edc842021-08-17 09:53:13 +01002933#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002934/*
2935 * Set the allowed elliptic curves
Brett Warrene0edc842021-08-17 09:53:13 +01002936 *
2937 * mbedtls_ssl_setup() takes the provided list
2938 * and translates it to a list of IANA TLS group identifiers,
2939 * stored in ssl->handshake->group_list.
2940 *
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002941 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002942void mbedtls_ssl_conf_curves(mbedtls_ssl_config *conf,
2943 const mbedtls_ecp_group_id *curve_list)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002944{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002945 conf->curve_list = curve_list;
Brett Warrene0edc842021-08-17 09:53:13 +01002946 conf->group_list = NULL;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002947}
Brett Warrene0edc842021-08-17 09:53:13 +01002948#endif /* MBEDTLS_DEPRECATED_REMOVED */
Hanno Becker947194e2017-04-07 13:25:49 +01002949#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002950
Brett Warrene0edc842021-08-17 09:53:13 +01002951/*
2952 * Set the allowed groups
2953 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002954void mbedtls_ssl_conf_groups(mbedtls_ssl_config *conf,
2955 const uint16_t *group_list)
Brett Warrene0edc842021-08-17 09:53:13 +01002956{
2957#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
2958 conf->curve_list = NULL;
2959#endif
2960 conf->group_list = group_list;
2961}
2962
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002963#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002964int mbedtls_ssl_set_hostname(mbedtls_ssl_context *ssl, const char *hostname)
Paul Bakker5121ce52009-01-03 21:22:43 +00002965{
Hanno Becker947194e2017-04-07 13:25:49 +01002966 /* Initialize to suppress unnecessary compiler warning */
2967 size_t hostname_len = 0;
2968
2969 /* Check if new hostname is valid before
2970 * making any change to current one */
Gilles Peskine449bd832023-01-11 14:50:10 +01002971 if (hostname != NULL) {
2972 hostname_len = strlen(hostname);
Hanno Becker947194e2017-04-07 13:25:49 +01002973
Gilles Peskine449bd832023-01-11 14:50:10 +01002974 if (hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN) {
2975 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2976 }
Hanno Becker947194e2017-04-07 13:25:49 +01002977 }
2978
2979 /* Now it's clear that we will overwrite the old hostname,
2980 * so we can free it safely */
2981
Gilles Peskine449bd832023-01-11 14:50:10 +01002982 if (ssl->hostname != NULL) {
2983 mbedtls_platform_zeroize(ssl->hostname, strlen(ssl->hostname));
2984 mbedtls_free(ssl->hostname);
Hanno Becker947194e2017-04-07 13:25:49 +01002985 }
2986
2987 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01002988
Gilles Peskine449bd832023-01-11 14:50:10 +01002989 if (hostname == NULL) {
Hanno Becker947194e2017-04-07 13:25:49 +01002990 ssl->hostname = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01002991 } else {
2992 ssl->hostname = mbedtls_calloc(1, hostname_len + 1);
2993 if (ssl->hostname == NULL) {
2994 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2995 }
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002996
Gilles Peskine449bd832023-01-11 14:50:10 +01002997 memcpy(ssl->hostname, hostname, hostname_len);
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002998
Hanno Becker947194e2017-04-07 13:25:49 +01002999 ssl->hostname[hostname_len] = '\0';
3000 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003001
Gilles Peskine449bd832023-01-11 14:50:10 +01003002 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00003003}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01003004#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00003005
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01003006#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01003007void mbedtls_ssl_conf_sni(mbedtls_ssl_config *conf,
3008 int (*f_sni)(void *, mbedtls_ssl_context *,
3009 const unsigned char *, size_t),
3010 void *p_sni)
Paul Bakker5701cdc2012-09-27 21:49:42 +00003011{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003012 conf->f_sni = f_sni;
3013 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00003014}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003015#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00003016
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003017#if defined(MBEDTLS_SSL_ALPN)
Gilles Peskine449bd832023-01-11 14:50:10 +01003018int mbedtls_ssl_conf_alpn_protocols(mbedtls_ssl_config *conf, const char **protos)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003019{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02003020 size_t cur_len, tot_len;
3021 const char **p;
3022
3023 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08003024 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
3025 * MUST NOT be truncated."
3026 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02003027 */
3028 tot_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003029 for (p = protos; *p != NULL; p++) {
3030 cur_len = strlen(*p);
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02003031 tot_len += cur_len;
3032
Gilles Peskine449bd832023-01-11 14:50:10 +01003033 if ((cur_len == 0) ||
3034 (cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN) ||
3035 (tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN)) {
3036 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3037 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02003038 }
3039
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003040 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02003041
Gilles Peskine449bd832023-01-11 14:50:10 +01003042 return 0;
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003043}
3044
Gilles Peskine449bd832023-01-11 14:50:10 +01003045const char *mbedtls_ssl_get_alpn_protocol(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003046{
Gilles Peskine449bd832023-01-11 14:50:10 +01003047 return ssl->alpn_chosen;
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003048}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003049#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003050
Johan Pascalb62bb512015-12-03 21:56:45 +01003051#if defined(MBEDTLS_SSL_DTLS_SRTP)
Gilles Peskine449bd832023-01-11 14:50:10 +01003052void mbedtls_ssl_conf_srtp_mki_value_supported(mbedtls_ssl_config *conf,
3053 int support_mki_value)
Ron Eldor591f1622018-01-22 12:30:04 +02003054{
3055 conf->dtls_srtp_mki_support = support_mki_value;
3056}
3057
Gilles Peskine449bd832023-01-11 14:50:10 +01003058int mbedtls_ssl_dtls_srtp_set_mki_value(mbedtls_ssl_context *ssl,
3059 unsigned char *mki_value,
3060 uint16_t mki_len)
Ron Eldor591f1622018-01-22 12:30:04 +02003061{
Gilles Peskine449bd832023-01-11 14:50:10 +01003062 if (mki_len > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH) {
3063 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Ron Eldora9788042018-12-05 11:04:31 +02003064 }
Ron Eldor591f1622018-01-22 12:30:04 +02003065
Gilles Peskine449bd832023-01-11 14:50:10 +01003066 if (ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_UNSUPPORTED) {
3067 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Ron Eldora9788042018-12-05 11:04:31 +02003068 }
Ron Eldor591f1622018-01-22 12:30:04 +02003069
Gilles Peskine449bd832023-01-11 14:50:10 +01003070 memcpy(ssl->dtls_srtp_info.mki_value, mki_value, mki_len);
Ron Eldor591f1622018-01-22 12:30:04 +02003071 ssl->dtls_srtp_info.mki_len = mki_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01003072 return 0;
Ron Eldor591f1622018-01-22 12:30:04 +02003073}
3074
Gilles Peskine449bd832023-01-11 14:50:10 +01003075int mbedtls_ssl_conf_dtls_srtp_protection_profiles(mbedtls_ssl_config *conf,
3076 const mbedtls_ssl_srtp_profile *profiles)
Johan Pascalb62bb512015-12-03 21:56:45 +01003077{
Johan Pascal253d0262020-09-22 13:04:45 +02003078 const mbedtls_ssl_srtp_profile *p;
3079 size_t list_size = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01003080
Johan Pascal253d0262020-09-22 13:04:45 +02003081 /* check the profiles list: all entry must be valid,
3082 * its size cannot be more than the total number of supported profiles, currently 4 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003083 for (p = profiles; *p != MBEDTLS_TLS_SRTP_UNSET &&
3084 list_size <= MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH;
3085 p++) {
3086 if (mbedtls_ssl_check_srtp_profile_value(*p) != MBEDTLS_TLS_SRTP_UNSET) {
Johan Pascal76fdf1d2020-10-22 23:31:00 +02003087 list_size++;
Gilles Peskine449bd832023-01-11 14:50:10 +01003088 } else {
Johan Pascal76fdf1d2020-10-22 23:31:00 +02003089 /* unsupported value, stop parsing and set the size to an error value */
3090 list_size = MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH + 1;
Johan Pascalb62bb512015-12-03 21:56:45 +01003091 }
3092 }
3093
Gilles Peskine449bd832023-01-11 14:50:10 +01003094 if (list_size > MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH) {
3095 conf->dtls_srtp_profile_list = NULL;
3096 conf->dtls_srtp_profile_list_len = 0;
3097 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Johan Pascal253d0262020-09-22 13:04:45 +02003098 }
3099
Johan Pascal9bc97ca2020-09-21 23:44:45 +02003100 conf->dtls_srtp_profile_list = profiles;
Johan Pascal253d0262020-09-22 13:04:45 +02003101 conf->dtls_srtp_profile_list_len = list_size;
Johan Pascalb62bb512015-12-03 21:56:45 +01003102
Gilles Peskine449bd832023-01-11 14:50:10 +01003103 return 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01003104}
3105
Gilles Peskine449bd832023-01-11 14:50:10 +01003106void mbedtls_ssl_get_dtls_srtp_negotiation_result(const mbedtls_ssl_context *ssl,
3107 mbedtls_dtls_srtp_info *dtls_srtp_info)
Johan Pascalb62bb512015-12-03 21:56:45 +01003108{
Johan Pascal2258a4f2020-10-28 13:53:09 +01003109 dtls_srtp_info->chosen_dtls_srtp_profile = ssl->dtls_srtp_info.chosen_dtls_srtp_profile;
3110 /* do not copy the mki value if there is no chosen profile */
Gilles Peskine449bd832023-01-11 14:50:10 +01003111 if (dtls_srtp_info->chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET) {
Johan Pascal2258a4f2020-10-28 13:53:09 +01003112 dtls_srtp_info->mki_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003113 } else {
Johan Pascal2258a4f2020-10-28 13:53:09 +01003114 dtls_srtp_info->mki_len = ssl->dtls_srtp_info.mki_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01003115 memcpy(dtls_srtp_info->mki_value, ssl->dtls_srtp_info.mki_value,
3116 ssl->dtls_srtp_info.mki_len);
Johan Pascal2258a4f2020-10-28 13:53:09 +01003117 }
Johan Pascalb62bb512015-12-03 21:56:45 +01003118}
Johan Pascalb62bb512015-12-03 21:56:45 +01003119#endif /* MBEDTLS_SSL_DTLS_SRTP */
3120
Aditya Patwardhan3096f332022-07-26 14:31:46 +05303121#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003122void mbedtls_ssl_conf_max_version(mbedtls_ssl_config *conf, int major, int minor)
Paul Bakker490ecc82011-10-06 13:04:09 +00003123{
Glenn Strauss2dfcea22022-03-14 17:26:42 -04003124 conf->max_tls_version = (major << 8) | minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00003125}
3126
Gilles Peskine449bd832023-01-11 14:50:10 +01003127void mbedtls_ssl_conf_min_version(mbedtls_ssl_config *conf, int major, int minor)
Paul Bakker1d29fb52012-09-28 13:28:45 +00003128{
Glenn Strauss2dfcea22022-03-14 17:26:42 -04003129 conf->min_tls_version = (major << 8) | minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00003130}
Aditya Patwardhan3096f332022-07-26 14:31:46 +05303131#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker1d29fb52012-09-28 13:28:45 +00003132
Janos Follath088ce432017-04-10 12:42:31 +01003133#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003134void mbedtls_ssl_conf_cert_req_ca_list(mbedtls_ssl_config *conf,
3135 char cert_req_ca_list)
Janos Follath088ce432017-04-10 12:42:31 +01003136{
3137 conf->cert_req_ca_list = cert_req_ca_list;
3138}
3139#endif
3140
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003141#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01003142void mbedtls_ssl_conf_encrypt_then_mac(mbedtls_ssl_config *conf, char etm)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01003143{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003144 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01003145}
3146#endif
3147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003148#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Gilles Peskine449bd832023-01-11 14:50:10 +01003149void mbedtls_ssl_conf_extended_master_secret(mbedtls_ssl_config *conf, char ems)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02003150{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003151 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02003152}
3153#endif
3154
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003155#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01003156int mbedtls_ssl_conf_max_frag_len(mbedtls_ssl_config *conf, unsigned char mfl_code)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003157{
Gilles Peskine449bd832023-01-11 14:50:10 +01003158 if (mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
3159 ssl_mfl_code_to_length(mfl_code) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN) {
3160 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003161 }
3162
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01003163 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003164
Gilles Peskine449bd832023-01-11 14:50:10 +01003165 return 0;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003166}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003167#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003168
Gilles Peskine449bd832023-01-11 14:50:10 +01003169void mbedtls_ssl_conf_legacy_renegotiation(mbedtls_ssl_config *conf, int allow_legacy)
Paul Bakker48916f92012-09-16 19:57:18 +00003170{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003171 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00003172}
3173
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003174#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01003175void mbedtls_ssl_conf_renegotiation(mbedtls_ssl_config *conf, int renegotiation)
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003176{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003177 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003178}
3179
Gilles Peskine449bd832023-01-11 14:50:10 +01003180void mbedtls_ssl_conf_renegotiation_enforced(mbedtls_ssl_config *conf, int max_records)
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003181{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003182 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003183}
3184
Gilles Peskine449bd832023-01-11 14:50:10 +01003185void mbedtls_ssl_conf_renegotiation_period(mbedtls_ssl_config *conf,
3186 const unsigned char period[8])
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01003187{
Gilles Peskine449bd832023-01-11 14:50:10 +01003188 memcpy(conf->renego_period, period, 8);
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01003189}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003190#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00003191
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003192#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003193#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003194void mbedtls_ssl_conf_session_tickets(mbedtls_ssl_config *conf, int use_tickets)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003195{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01003196 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003197}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003198#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02003199
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003200#if defined(MBEDTLS_SSL_SRV_C)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08003201
Jerry Yud0766ec2022-09-22 10:46:57 +08003202#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003203void mbedtls_ssl_conf_new_session_tickets(mbedtls_ssl_config *conf,
3204 uint16_t num_tickets)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08003205{
Jerry Yud0766ec2022-09-22 10:46:57 +08003206 conf->new_session_tickets_count = num_tickets;
Jerry Yu1ad7ace2022-08-09 13:28:39 +08003207}
3208#endif
3209
Gilles Peskine449bd832023-01-11 14:50:10 +01003210void mbedtls_ssl_conf_session_tickets_cb(mbedtls_ssl_config *conf,
3211 mbedtls_ssl_ticket_write_t *f_ticket_write,
3212 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
3213 void *p_ticket)
Paul Bakker606b4ba2013-08-14 16:52:14 +02003214{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02003215 conf->f_ticket_write = f_ticket_write;
3216 conf->f_ticket_parse = f_ticket_parse;
3217 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02003218}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003219#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003220#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003221
Gilles Peskine449bd832023-01-11 14:50:10 +01003222void mbedtls_ssl_set_export_keys_cb(mbedtls_ssl_context *ssl,
3223 mbedtls_ssl_export_keys_t *f_export_keys,
3224 void *p_export_keys)
Robert Cragie4feb7ae2015-10-02 13:33:37 +01003225{
Hanno Becker7e6c1782021-06-08 09:24:55 +01003226 ssl->f_export_keys = f_export_keys;
3227 ssl->p_export_keys = p_export_keys;
Ron Eldorf5cc10d2019-05-07 18:33:40 +03003228}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01003229
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003230#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003231void mbedtls_ssl_conf_async_private_cb(
3232 mbedtls_ssl_config *conf,
3233 mbedtls_ssl_async_sign_t *f_async_sign,
3234 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
3235 mbedtls_ssl_async_resume_t *f_async_resume,
3236 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskine449bd832023-01-11 14:50:10 +01003237 void *async_config_data)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003238{
3239 conf->f_async_sign_start = f_async_sign;
3240 conf->f_async_decrypt_start = f_async_decrypt;
3241 conf->f_async_resume = f_async_resume;
3242 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003243 conf->p_async_config_data = async_config_data;
3244}
3245
Gilles Peskine449bd832023-01-11 14:50:10 +01003246void *mbedtls_ssl_conf_get_async_config_data(const mbedtls_ssl_config *conf)
Gilles Peskine8f97af72018-04-26 11:46:10 +02003247{
Gilles Peskine449bd832023-01-11 14:50:10 +01003248 return conf->p_async_config_data;
Gilles Peskine8f97af72018-04-26 11:46:10 +02003249}
3250
Gilles Peskine449bd832023-01-11 14:50:10 +01003251void *mbedtls_ssl_get_async_operation_data(const mbedtls_ssl_context *ssl)
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003252{
Gilles Peskine449bd832023-01-11 14:50:10 +01003253 if (ssl->handshake == NULL) {
3254 return NULL;
3255 } else {
3256 return ssl->handshake->user_async_ctx;
3257 }
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003258}
3259
Gilles Peskine449bd832023-01-11 14:50:10 +01003260void mbedtls_ssl_set_async_operation_data(mbedtls_ssl_context *ssl,
3261 void *ctx)
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003262{
Gilles Peskine449bd832023-01-11 14:50:10 +01003263 if (ssl->handshake != NULL) {
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003264 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +01003265 }
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003266}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003267#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003268
Paul Bakker5121ce52009-01-03 21:22:43 +00003269/*
3270 * SSL get accessors
3271 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003272uint32_t mbedtls_ssl_get_verify_result(const mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00003273{
Gilles Peskine449bd832023-01-11 14:50:10 +01003274 if (ssl->session != NULL) {
3275 return ssl->session->verify_result;
3276 }
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00003277
Gilles Peskine449bd832023-01-11 14:50:10 +01003278 if (ssl->session_negotiate != NULL) {
3279 return ssl->session_negotiate->verify_result;
3280 }
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00003281
Gilles Peskine449bd832023-01-11 14:50:10 +01003282 return 0xFFFFFFFF;
Paul Bakker5121ce52009-01-03 21:22:43 +00003283}
3284
Gilles Peskine449bd832023-01-11 14:50:10 +01003285int mbedtls_ssl_get_ciphersuite_id_from_ssl(const mbedtls_ssl_context *ssl)
Glenn Strauss8f526902022-01-13 00:04:49 -05003286{
Gilles Peskine449bd832023-01-11 14:50:10 +01003287 if (ssl == NULL || ssl->session == NULL) {
3288 return 0;
3289 }
Glenn Strauss8f526902022-01-13 00:04:49 -05003290
Gilles Peskine449bd832023-01-11 14:50:10 +01003291 return ssl->session->ciphersuite;
Glenn Strauss8f526902022-01-13 00:04:49 -05003292}
3293
Gilles Peskine449bd832023-01-11 14:50:10 +01003294const char *mbedtls_ssl_get_ciphersuite(const mbedtls_ssl_context *ssl)
Paul Bakker72f62662011-01-16 21:27:44 +00003295{
Gilles Peskine449bd832023-01-11 14:50:10 +01003296 if (ssl == NULL || ssl->session == NULL) {
3297 return NULL;
3298 }
Paul Bakker926c8e42013-03-06 10:23:34 +01003299
Gilles Peskine449bd832023-01-11 14:50:10 +01003300 return mbedtls_ssl_get_ciphersuite_name(ssl->session->ciphersuite);
Paul Bakker72f62662011-01-16 21:27:44 +00003301}
3302
Gilles Peskine449bd832023-01-11 14:50:10 +01003303const char *mbedtls_ssl_get_version(const mbedtls_ssl_context *ssl)
Paul Bakker43ca69c2011-01-15 17:35:19 +00003304{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003305#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003306 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
3307 switch (ssl->tls_version) {
Glenn Strauss60bfe602022-03-14 19:04:24 -04003308 case MBEDTLS_SSL_VERSION_TLS1_2:
Gilles Peskine449bd832023-01-11 14:50:10 +01003309 return "DTLSv1.2";
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003310 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01003311 return "unknown (DTLS)";
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003312 }
3313 }
3314#endif
3315
Gilles Peskine449bd832023-01-11 14:50:10 +01003316 switch (ssl->tls_version) {
Glenn Strauss60bfe602022-03-14 19:04:24 -04003317 case MBEDTLS_SSL_VERSION_TLS1_2:
Gilles Peskine449bd832023-01-11 14:50:10 +01003318 return "TLSv1.2";
Glenn Strauss60bfe602022-03-14 19:04:24 -04003319 case MBEDTLS_SSL_VERSION_TLS1_3:
Gilles Peskine449bd832023-01-11 14:50:10 +01003320 return "TLSv1.3";
Paul Bakker43ca69c2011-01-15 17:35:19 +00003321 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01003322 return "unknown";
Paul Bakker43ca69c2011-01-15 17:35:19 +00003323 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00003324}
3325
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003326#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01003327size_t mbedtls_ssl_get_input_max_frag_len(const mbedtls_ssl_context *ssl)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003328{
David Horstmann95d516f2021-05-04 18:36:56 +01003329 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003330 size_t read_mfl;
3331
Jerry Yuddda0502022-12-01 19:43:12 +08003332#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003333 /* Use the configured MFL for the client if we're past SERVER_HELLO_DONE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003334 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
3335 ssl->state >= MBEDTLS_SSL_SERVER_HELLO_DONE) {
3336 return ssl_mfl_code_to_length(ssl->conf->mfl_code);
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003337 }
Jerry Yuddda0502022-12-01 19:43:12 +08003338#endif
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003339
3340 /* Check if a smaller max length was negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01003341 if (ssl->session_out != NULL) {
3342 read_mfl = ssl_mfl_code_to_length(ssl->session_out->mfl_code);
3343 if (read_mfl < max_len) {
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003344 max_len = read_mfl;
3345 }
3346 }
3347
Jerry Yuddda0502022-12-01 19:43:12 +08003348 /* During a handshake, use the value being negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01003349 if (ssl->session_negotiate != NULL) {
3350 read_mfl = ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code);
3351 if (read_mfl < max_len) {
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003352 max_len = read_mfl;
3353 }
3354 }
3355
Gilles Peskine449bd832023-01-11 14:50:10 +01003356 return max_len;
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003357}
3358
Gilles Peskine449bd832023-01-11 14:50:10 +01003359size_t mbedtls_ssl_get_output_max_frag_len(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003360{
3361 size_t max_len;
3362
3363 /*
3364 * Assume mfl_code is correct since it was checked when set
3365 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003366 max_len = ssl_mfl_code_to_length(ssl->conf->mfl_code);
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003367
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02003368 /* Check if a smaller max length was negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01003369 if (ssl->session_out != NULL &&
3370 ssl_mfl_code_to_length(ssl->session_out->mfl_code) < max_len) {
3371 max_len = ssl_mfl_code_to_length(ssl->session_out->mfl_code);
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003372 }
3373
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02003374 /* During a handshake, use the value being negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01003375 if (ssl->session_negotiate != NULL &&
3376 ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code) < max_len) {
3377 max_len = ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code);
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02003378 }
3379
Gilles Peskine449bd832023-01-11 14:50:10 +01003380 return max_len;
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003381}
3382#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
3383
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003384#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003385size_t mbedtls_ssl_get_current_mtu(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003386{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04003387 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003388 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
3389 (ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
3390 ssl->state == MBEDTLS_SSL_SERVER_HELLO)) {
3391 return 0;
3392 }
Andrzej Kurekef43ce62018-10-09 08:24:12 -04003393
Gilles Peskine449bd832023-01-11 14:50:10 +01003394 if (ssl->handshake == NULL || ssl->handshake->mtu == 0) {
3395 return ssl->mtu;
3396 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003397
Gilles Peskine449bd832023-01-11 14:50:10 +01003398 if (ssl->mtu == 0) {
3399 return ssl->handshake->mtu;
3400 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003401
Gilles Peskine449bd832023-01-11 14:50:10 +01003402 return ssl->mtu < ssl->handshake->mtu ?
3403 ssl->mtu : ssl->handshake->mtu;
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003404}
3405#endif /* MBEDTLS_SSL_PROTO_DTLS */
3406
Gilles Peskine449bd832023-01-11 14:50:10 +01003407int mbedtls_ssl_get_max_out_record_payload(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003408{
3409 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
3410
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02003411#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
3412 !defined(MBEDTLS_SSL_PROTO_DTLS)
3413 (void) ssl;
3414#endif
3415
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003416#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01003417 const size_t mfl = mbedtls_ssl_get_output_max_frag_len(ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003418
Gilles Peskine449bd832023-01-11 14:50:10 +01003419 if (max_len > mfl) {
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003420 max_len = mfl;
Gilles Peskine449bd832023-01-11 14:50:10 +01003421 }
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003422#endif
3423
3424#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003425 if (mbedtls_ssl_get_current_mtu(ssl) != 0) {
3426 const size_t mtu = mbedtls_ssl_get_current_mtu(ssl);
3427 const int ret = mbedtls_ssl_get_record_expansion(ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003428 const size_t overhead = (size_t) ret;
3429
Gilles Peskine449bd832023-01-11 14:50:10 +01003430 if (ret < 0) {
3431 return ret;
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003432 }
3433
Gilles Peskine449bd832023-01-11 14:50:10 +01003434 if (mtu <= overhead) {
3435 MBEDTLS_SSL_DEBUG_MSG(1, ("MTU too low for record expansion"));
3436 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
3437 }
3438
3439 if (max_len > mtu - overhead) {
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003440 max_len = mtu - overhead;
Gilles Peskine449bd832023-01-11 14:50:10 +01003441 }
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003442 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003443#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003444
Hanno Becker0defedb2018-08-10 12:35:02 +01003445#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
3446 !defined(MBEDTLS_SSL_PROTO_DTLS)
3447 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003448#endif
3449
Gilles Peskine449bd832023-01-11 14:50:10 +01003450 return (int) max_len;
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003451}
3452
Gilles Peskine449bd832023-01-11 14:50:10 +01003453int mbedtls_ssl_get_max_in_record_payload(const mbedtls_ssl_context *ssl)
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003454{
3455 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
3456
3457#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
3458 (void) ssl;
3459#endif
3460
3461#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01003462 const size_t mfl = mbedtls_ssl_get_input_max_frag_len(ssl);
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003463
Gilles Peskine449bd832023-01-11 14:50:10 +01003464 if (max_len > mfl) {
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003465 max_len = mfl;
Gilles Peskine449bd832023-01-11 14:50:10 +01003466 }
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003467#endif
3468
Gilles Peskine449bd832023-01-11 14:50:10 +01003469 return (int) max_len;
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003470}
3471
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003472#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003473const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert(const mbedtls_ssl_context *ssl)
Paul Bakkerb0550d92012-10-30 07:51:03 +00003474{
Gilles Peskine449bd832023-01-11 14:50:10 +01003475 if (ssl == NULL || ssl->session == NULL) {
3476 return NULL;
3477 }
Paul Bakkerb0550d92012-10-30 07:51:03 +00003478
Hanno Beckere6824572019-02-07 13:18:46 +00003479#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01003480 return ssl->session->peer_cert;
Hanno Beckere6824572019-02-07 13:18:46 +00003481#else
Gilles Peskine449bd832023-01-11 14:50:10 +01003482 return NULL;
Hanno Beckere6824572019-02-07 13:18:46 +00003483#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00003484}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003485#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00003486
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003487#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003488int mbedtls_ssl_get_session(const mbedtls_ssl_context *ssl,
3489 mbedtls_ssl_session *dst)
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003490{
Hanno Beckere810bbc2021-05-14 16:01:05 +01003491 int ret;
3492
Gilles Peskine449bd832023-01-11 14:50:10 +01003493 if (ssl == NULL ||
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003494 dst == NULL ||
3495 ssl->session == NULL ||
Gilles Peskine449bd832023-01-11 14:50:10 +01003496 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT) {
3497 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003498 }
3499
Hanno Beckere810bbc2021-05-14 16:01:05 +01003500 /* Since Mbed TLS 3.0, mbedtls_ssl_get_session() is no longer
3501 * idempotent: Each session can only be exported once.
3502 *
3503 * (This is in preparation for TLS 1.3 support where we will
3504 * need the ability to export multiple sessions (aka tickets),
3505 * which will be achieved by calling mbedtls_ssl_get_session()
3506 * multiple times until it fails.)
3507 *
3508 * Check whether we have already exported the current session,
3509 * and fail if so.
3510 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003511 if (ssl->session->exported == 1) {
3512 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
3513 }
Hanno Beckere810bbc2021-05-14 16:01:05 +01003514
Gilles Peskine449bd832023-01-11 14:50:10 +01003515 ret = mbedtls_ssl_session_copy(dst, ssl->session);
3516 if (ret != 0) {
3517 return ret;
3518 }
Hanno Beckere810bbc2021-05-14 16:01:05 +01003519
3520 /* Remember that we've exported the session. */
3521 ssl->session->exported = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003522 return 0;
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003523}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003524#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003525
Paul Bakker5121ce52009-01-03 21:22:43 +00003526/*
Hanno Beckera835da52019-05-16 12:39:07 +01003527 * Define ticket header determining Mbed TLS version
3528 * and structure of the ticket.
3529 */
3530
Hanno Becker94ef3b32019-05-16 12:50:45 +01003531/*
Hanno Becker50b59662019-05-28 14:30:45 +01003532 * Define bitflag determining compile-time settings influencing
3533 * structure of serialized SSL sessions.
Hanno Becker94ef3b32019-05-16 12:50:45 +01003534 */
3535
Hanno Becker50b59662019-05-28 14:30:45 +01003536#if defined(MBEDTLS_HAVE_TIME)
Hanno Becker3e088662019-05-29 11:10:18 +01003537#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker50b59662019-05-28 14:30:45 +01003538#else
Hanno Becker3e088662019-05-29 11:10:18 +01003539#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003540#endif /* MBEDTLS_HAVE_TIME */
3541
3542#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker3e088662019-05-29 11:10:18 +01003543#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003544#else
Hanno Becker3e088662019-05-29 11:10:18 +01003545#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003546#endif /* MBEDTLS_X509_CRT_PARSE_C */
3547
3548#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Becker3e088662019-05-29 11:10:18 +01003549#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003550#else
Hanno Becker3e088662019-05-29 11:10:18 +01003551#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003552#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
3553
3554#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Becker3e088662019-05-29 11:10:18 +01003555#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003556#else
Hanno Becker3e088662019-05-29 11:10:18 +01003557#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003558#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
3559
Hanno Becker94ef3b32019-05-16 12:50:45 +01003560#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3e088662019-05-29 11:10:18 +01003561#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003562#else
Hanno Becker3e088662019-05-29 11:10:18 +01003563#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003564#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
3565
Hanno Becker94ef3b32019-05-16 12:50:45 +01003566#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3567#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
3568#else
3569#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
3570#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3571
Hanno Becker3e088662019-05-29 11:10:18 +01003572#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
3573#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
3574#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
3575#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
Hanno Becker37bdbe62021-08-01 05:38:58 +01003576#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 4
3577#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 5
Hanno Becker3e088662019-05-29 11:10:18 +01003578
Hanno Becker50b59662019-05-28 14:30:45 +01003579#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Gilles Peskine449bd832023-01-11 14:50:10 +01003580 ((uint16_t) ( \
3581 (SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT) | \
3582 (SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT) | \
3583 (SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << \
3584 SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT) | \
3585 (SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT) | \
3586 (SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT) | \
3587 (SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT)))
Hanno Becker94ef3b32019-05-16 12:50:45 +01003588
Hanno Beckerf8787072019-05-16 12:41:07 +01003589static unsigned char ssl_serialized_session_header[] = {
Hanno Becker94ef3b32019-05-16 12:50:45 +01003590 MBEDTLS_VERSION_MAJOR,
3591 MBEDTLS_VERSION_MINOR,
3592 MBEDTLS_VERSION_PATCH,
Gilles Peskine449bd832023-01-11 14:50:10 +01003593 MBEDTLS_BYTE_1(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
3594 MBEDTLS_BYTE_0(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
Hanno Beckerf8787072019-05-16 12:41:07 +01003595};
Hanno Beckera835da52019-05-16 12:39:07 +01003596
3597/*
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003598 * Serialize a session in the following format:
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02003599 * (in the presentation language of TLS, RFC 8446 section 3)
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003600 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003601 * struct {
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01003602 *
Hanno Beckerdce50972021-08-01 05:39:23 +01003603 * opaque mbedtls_version[3]; // library version: major, minor, patch
3604 * opaque session_format[2]; // library-version specific 16-bit field
3605 * // determining the format of the remaining
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003606 * // serialized data.
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01003607 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003608 * Note: When updating the format, remember to keep
3609 * these version+format bytes.
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003610 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003611 * // In this version, `session_format` determines
3612 * // the setting of those compile-time
3613 * // configuration options which influence
3614 * // the structure of mbedtls_ssl_session.
3615 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003616 * uint8_t minor_ver; // Protocol minor version. Possible values:
Jerry Yu0c2a7382022-12-06 13:27:25 +08003617 * // - TLS 1.2 (0x0303)
3618 * // - TLS 1.3 (0x0304)
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003619 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003620 * select (serialized_session.tls_version) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003621 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003622 * case MBEDTLS_SSL_VERSION_TLS1_2:
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003623 * serialized_session_tls12 data;
Jerry Yu0c2a7382022-12-06 13:27:25 +08003624 * case MBEDTLS_SSL_VERSION_TLS1_3:
Jerry Yuddda0502022-12-01 19:43:12 +08003625 * serialized_session_tls13 data;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003626 *
3627 * };
3628 *
3629 * } serialized_session;
3630 *
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003631 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003632
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003633MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003634static int ssl_session_save(const mbedtls_ssl_session *session,
3635 unsigned char omit_header,
3636 unsigned char *buf,
3637 size_t buf_len,
3638 size_t *olen)
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003639{
3640 unsigned char *p = buf;
3641 size_t used = 0;
Jerry Yu251a12e2022-07-13 15:15:48 +08003642 size_t remaining_len;
Jerry Yue36fdd62022-08-17 21:31:36 +08003643#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3644 size_t out_len;
3645 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3646#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01003647 if (session == NULL) {
3648 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
3649 }
Jerry Yu438ddd82022-07-07 06:55:50 +00003650
Gilles Peskine449bd832023-01-11 14:50:10 +01003651 if (!omit_header) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003652 /*
3653 * Add Mbed TLS version identifier
3654 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003655 used += sizeof(ssl_serialized_session_header);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003656
Gilles Peskine449bd832023-01-11 14:50:10 +01003657 if (used <= buf_len) {
3658 memcpy(p, ssl_serialized_session_header,
3659 sizeof(ssl_serialized_session_header));
3660 p += sizeof(ssl_serialized_session_header);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003661 }
3662 }
3663
3664 /*
3665 * TLS version identifier
3666 */
3667 used += 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003668 if (used <= buf_len) {
3669 *p++ = MBEDTLS_BYTE_0(session->tls_version);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003670 }
3671
3672 /* Forward to version-specific serialization routine. */
Jerry Yufca4d572022-07-21 10:37:48 +08003673 remaining_len = (buf_len >= used) ? buf_len - used : 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003674 switch (session->tls_version) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003675#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01003676 case MBEDTLS_SSL_VERSION_TLS1_2:
3677 used += ssl_tls12_session_save(session, p, remaining_len);
3678 break;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003679#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3680
Jerry Yu251a12e2022-07-13 15:15:48 +08003681#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01003682 case MBEDTLS_SSL_VERSION_TLS1_3:
3683 ret = ssl_tls13_session_save(session, p, remaining_len, &out_len);
3684 if (ret != 0 && ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL) {
3685 return ret;
3686 }
3687 used += out_len;
3688 break;
Jerry Yu251a12e2022-07-13 15:15:48 +08003689#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3690
Gilles Peskine449bd832023-01-11 14:50:10 +01003691 default:
3692 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003693 }
3694
3695 *olen = used;
Gilles Peskine449bd832023-01-11 14:50:10 +01003696 if (used > buf_len) {
3697 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
3698 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003699
Gilles Peskine449bd832023-01-11 14:50:10 +01003700 return 0;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003701}
3702
3703/*
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003704 * Public wrapper for ssl_session_save()
3705 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003706int mbedtls_ssl_session_save(const mbedtls_ssl_session *session,
3707 unsigned char *buf,
3708 size_t buf_len,
3709 size_t *olen)
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003710{
Gilles Peskine449bd832023-01-11 14:50:10 +01003711 return ssl_session_save(session, 0, buf, buf_len, olen);
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003712}
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003713
Jerry Yuf1b23ca2022-02-18 11:48:47 +08003714/*
3715 * Deserialize session, see mbedtls_ssl_session_save() for format.
3716 *
3717 * This internal version is wrapped by a public function that cleans up in
3718 * case of error, and has an extra option omit_header.
3719 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003720MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003721static int ssl_session_load(mbedtls_ssl_session *session,
3722 unsigned char omit_header,
3723 const unsigned char *buf,
3724 size_t len)
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003725{
3726 const unsigned char *p = buf;
3727 const unsigned char * const end = buf + len;
Jerry Yu438ddd82022-07-07 06:55:50 +00003728 size_t remaining_len;
3729
3730
Gilles Peskine449bd832023-01-11 14:50:10 +01003731 if (session == NULL) {
3732 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
3733 }
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003734
Gilles Peskine449bd832023-01-11 14:50:10 +01003735 if (!omit_header) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003736 /*
3737 * Check Mbed TLS version identifier
3738 */
3739
Gilles Peskine449bd832023-01-11 14:50:10 +01003740 if ((size_t) (end - p) < sizeof(ssl_serialized_session_header)) {
3741 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003742 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003743
3744 if (memcmp(p, ssl_serialized_session_header,
3745 sizeof(ssl_serialized_session_header)) != 0) {
3746 return MBEDTLS_ERR_SSL_VERSION_MISMATCH;
3747 }
3748 p += sizeof(ssl_serialized_session_header);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003749 }
3750
3751 /*
3752 * TLS version identifier
3753 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003754 if (1 > (size_t) (end - p)) {
3755 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3756 }
Glenn Straussda7851c2022-03-14 13:29:48 -04003757 session->tls_version = 0x0300 | *p++;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003758
3759 /* Dispatch according to TLS version. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003760 remaining_len = (end - p);
3761 switch (session->tls_version) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003762#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01003763 case MBEDTLS_SSL_VERSION_TLS1_2:
3764 return ssl_tls12_session_load(session, p, remaining_len);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003765#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3766
Jerry Yu438ddd82022-07-07 06:55:50 +00003767#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01003768 case MBEDTLS_SSL_VERSION_TLS1_3:
3769 return ssl_tls13_session_load(session, p, remaining_len);
Jerry Yu438ddd82022-07-07 06:55:50 +00003770#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3771
Gilles Peskine449bd832023-01-11 14:50:10 +01003772 default:
3773 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003774 }
3775}
3776
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003777/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02003778 * Deserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003779 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003780int mbedtls_ssl_session_load(mbedtls_ssl_session *session,
3781 const unsigned char *buf,
3782 size_t len)
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003783{
Gilles Peskine449bd832023-01-11 14:50:10 +01003784 int ret = ssl_session_load(session, 0, buf, len);
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003785
Gilles Peskine449bd832023-01-11 14:50:10 +01003786 if (ret != 0) {
3787 mbedtls_ssl_session_free(session);
3788 }
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003789
Gilles Peskine449bd832023-01-11 14:50:10 +01003790 return ret;
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003791}
3792
3793/*
Paul Bakker1961b702013-01-25 14:49:24 +01003794 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00003795 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003796MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003797static int ssl_prepare_handshake_step(mbedtls_ssl_context *ssl)
Hanno Becker41934dd2021-08-07 19:13:43 +01003798{
3799 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3800
Ronald Cron66dbf912022-02-02 15:33:46 +01003801 /*
3802 * We may have not been able to send to the peer all the handshake data
Ronald Cron3f20b772022-03-08 16:00:02 +01003803 * that were written into the output buffer by the previous handshake step,
3804 * if the write to the network callback returned with the
Ronald Cron66dbf912022-02-02 15:33:46 +01003805 * #MBEDTLS_ERR_SSL_WANT_WRITE error code.
3806 * We proceed to the next handshake step only when all data from the
3807 * previous one have been sent to the peer, thus we make sure that this is
3808 * the case here by calling `mbedtls_ssl_flush_output()`. The function may
3809 * return with the #MBEDTLS_ERR_SSL_WANT_WRITE error code in which case
3810 * we have to wait before to go ahead.
3811 * In the case of TLS 1.3, handshake step handlers do not send data to the
3812 * peer. Data are only sent here and through
3813 * `mbedtls_ssl_handle_pending_alert` in case an error that triggered an
Andrzej Kurek5c65c572022-04-13 14:28:52 -04003814 * alert occurred.
Ronald Cron66dbf912022-02-02 15:33:46 +01003815 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003816 if ((ret = mbedtls_ssl_flush_output(ssl)) != 0) {
3817 return ret;
3818 }
Hanno Becker41934dd2021-08-07 19:13:43 +01003819
3820#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003821 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3822 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING) {
3823 if ((ret = mbedtls_ssl_flight_transmit(ssl)) != 0) {
3824 return ret;
3825 }
Hanno Becker41934dd2021-08-07 19:13:43 +01003826 }
3827#endif /* MBEDTLS_SSL_PROTO_DTLS */
3828
Gilles Peskine449bd832023-01-11 14:50:10 +01003829 return ret;
Hanno Becker41934dd2021-08-07 19:13:43 +01003830}
3831
Gilles Peskine449bd832023-01-11 14:50:10 +01003832int mbedtls_ssl_handshake_step(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00003833{
Hanno Becker41934dd2021-08-07 19:13:43 +01003834 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003835
Gilles Peskine449bd832023-01-11 14:50:10 +01003836 if (ssl == NULL ||
Hanno Becker41934dd2021-08-07 19:13:43 +01003837 ssl->conf == NULL ||
3838 ssl->handshake == NULL ||
Gilles Peskine449bd832023-01-11 14:50:10 +01003839 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER) {
3840 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Becker41934dd2021-08-07 19:13:43 +01003841 }
3842
Gilles Peskine449bd832023-01-11 14:50:10 +01003843 ret = ssl_prepare_handshake_step(ssl);
3844 if (ret != 0) {
3845 return ret;
3846 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003847
Gilles Peskine449bd832023-01-11 14:50:10 +01003848 ret = mbedtls_ssl_handle_pending_alert(ssl);
3849 if (ret != 0) {
Jerry Yue7047812021-09-13 19:26:39 +08003850 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01003851 }
Jerry Yue7047812021-09-13 19:26:39 +08003852
Tom Cosgrove2fdc7b32022-09-21 12:33:17 +01003853 /* If ssl->conf->endpoint is not one of MBEDTLS_SSL_IS_CLIENT or
3854 * MBEDTLS_SSL_IS_SERVER, this is the return code we give */
3855 ret = MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3856
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003857#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003858 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
3859 MBEDTLS_SSL_DEBUG_MSG(2, ("client state: %s",
3860 mbedtls_ssl_states_str(ssl->state)));
Jerry Yub9930e72021-08-06 17:11:51 +08003861
Gilles Peskine449bd832023-01-11 14:50:10 +01003862 switch (ssl->state) {
Ronald Cron9f0fba32022-02-10 16:45:15 +01003863 case MBEDTLS_SSL_HELLO_REQUEST:
3864 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
Tom Cosgrove87d9c6c2022-09-22 09:27:56 +01003865 ret = 0;
Ronald Cron9f0fba32022-02-10 16:45:15 +01003866 break;
Jerry Yub9930e72021-08-06 17:11:51 +08003867
Ronald Cron9f0fba32022-02-10 16:45:15 +01003868 case MBEDTLS_SSL_CLIENT_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01003869 ret = mbedtls_ssl_write_client_hello(ssl);
Ronald Cron9f0fba32022-02-10 16:45:15 +01003870 break;
3871
3872 default:
3873#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01003874 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
3875 ret = mbedtls_ssl_tls13_handshake_client_step(ssl);
3876 } else {
3877 ret = mbedtls_ssl_handshake_client_step(ssl);
3878 }
Ronald Cron9f0fba32022-02-10 16:45:15 +01003879#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01003880 ret = mbedtls_ssl_handshake_client_step(ssl);
Ronald Cron9f0fba32022-02-10 16:45:15 +01003881#else
Gilles Peskine449bd832023-01-11 14:50:10 +01003882 ret = mbedtls_ssl_tls13_handshake_client_step(ssl);
Ronald Cron9f0fba32022-02-10 16:45:15 +01003883#endif
3884 }
Jerry Yub9930e72021-08-06 17:11:51 +08003885 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003886#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003887#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003888 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Ronald Cron6f135e12021-12-08 16:57:54 +01003889#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01003890 if (mbedtls_ssl_conf_is_tls13_only(ssl->conf)) {
3891 ret = mbedtls_ssl_tls13_handshake_server_step(ssl);
3892 }
Ronald Cron6f135e12021-12-08 16:57:54 +01003893#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yub9930e72021-08-06 17:11:51 +08003894
3895#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01003896 if (mbedtls_ssl_conf_is_tls12_only(ssl->conf)) {
3897 ret = mbedtls_ssl_handshake_server_step(ssl);
3898 }
Jerry Yub9930e72021-08-06 17:11:51 +08003899#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3900 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003901#endif
3902
Gilles Peskine449bd832023-01-11 14:50:10 +01003903 if (ret != 0) {
Jerry Yubbd5a3f2021-09-18 20:50:22 +08003904 /* handshake_step return error. And it is same
3905 * with alert_reason.
3906 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003907 if (ssl->send_alert) {
3908 ret = mbedtls_ssl_handle_pending_alert(ssl);
Jerry Yue7047812021-09-13 19:26:39 +08003909 goto cleanup;
3910 }
3911 }
3912
3913cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01003914 return ret;
Paul Bakker1961b702013-01-25 14:49:24 +01003915}
3916
3917/*
3918 * Perform the SSL handshake
3919 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003920int mbedtls_ssl_handshake(mbedtls_ssl_context *ssl)
Paul Bakker1961b702013-01-25 14:49:24 +01003921{
3922 int ret = 0;
3923
Hanno Beckera817ea42020-10-20 15:20:23 +01003924 /* Sanity checks */
3925
Gilles Peskine449bd832023-01-11 14:50:10 +01003926 if (ssl == NULL || ssl->conf == NULL) {
3927 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3928 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003929
Hanno Beckera817ea42020-10-20 15:20:23 +01003930#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003931 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3932 (ssl->f_set_timer == NULL || ssl->f_get_timer == NULL)) {
3933 MBEDTLS_SSL_DEBUG_MSG(1, ("You must use "
3934 "mbedtls_ssl_set_timer_cb() for DTLS"));
3935 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Beckera817ea42020-10-20 15:20:23 +01003936 }
3937#endif /* MBEDTLS_SSL_PROTO_DTLS */
3938
Gilles Peskine449bd832023-01-11 14:50:10 +01003939 MBEDTLS_SSL_DEBUG_MSG(2, ("=> handshake"));
Paul Bakker1961b702013-01-25 14:49:24 +01003940
Hanno Beckera817ea42020-10-20 15:20:23 +01003941 /* Main handshake loop */
Gilles Peskine449bd832023-01-11 14:50:10 +01003942 while (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER) {
3943 ret = mbedtls_ssl_handshake_step(ssl);
Paul Bakker1961b702013-01-25 14:49:24 +01003944
Gilles Peskine449bd832023-01-11 14:50:10 +01003945 if (ret != 0) {
Paul Bakker1961b702013-01-25 14:49:24 +01003946 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01003947 }
Paul Bakker1961b702013-01-25 14:49:24 +01003948 }
3949
Gilles Peskine449bd832023-01-11 14:50:10 +01003950 MBEDTLS_SSL_DEBUG_MSG(2, ("<= handshake"));
Paul Bakker5121ce52009-01-03 21:22:43 +00003951
Gilles Peskine449bd832023-01-11 14:50:10 +01003952 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003953}
3954
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003955#if defined(MBEDTLS_SSL_RENEGOTIATION)
3956#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003957/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003958 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00003959 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003960MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003961static int ssl_write_hello_request(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003962{
Janos Follath865b3eb2019-12-16 11:46:15 +00003963 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003964
Gilles Peskine449bd832023-01-11 14:50:10 +01003965 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write hello request"));
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003966
3967 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003968 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3969 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003970
Gilles Peskine449bd832023-01-11 14:50:10 +01003971 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
3972 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
3973 return ret;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003974 }
3975
Gilles Peskine449bd832023-01-11 14:50:10 +01003976 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write hello request"));
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003977
Gilles Peskine449bd832023-01-11 14:50:10 +01003978 return 0;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003979}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003980#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003981
3982/*
3983 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003984 * - any side: calling mbedtls_ssl_renegotiate(),
3985 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
3986 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02003987 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003988 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003989 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003990 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003991int mbedtls_ssl_start_renegotiation(mbedtls_ssl_context *ssl)
Paul Bakker48916f92012-09-16 19:57:18 +00003992{
Janos Follath865b3eb2019-12-16 11:46:15 +00003993 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker48916f92012-09-16 19:57:18 +00003994
Gilles Peskine449bd832023-01-11 14:50:10 +01003995 MBEDTLS_SSL_DEBUG_MSG(2, ("=> renegotiate"));
Paul Bakker48916f92012-09-16 19:57:18 +00003996
Gilles Peskine449bd832023-01-11 14:50:10 +01003997 if ((ret = ssl_handshake_init(ssl)) != 0) {
3998 return ret;
3999 }
Paul Bakker48916f92012-09-16 19:57:18 +00004000
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02004001 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
4002 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004003#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004004 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4005 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING) {
4006 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004007 ssl->handshake->out_msg_seq = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004008 } else {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004009 ssl->handshake->in_msg_seq = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004010 }
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02004011 }
4012#endif
4013
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004014 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
4015 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00004016
Gilles Peskine449bd832023-01-11 14:50:10 +01004017 if ((ret = mbedtls_ssl_handshake(ssl)) != 0) {
4018 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_handshake", ret);
4019 return ret;
Paul Bakker48916f92012-09-16 19:57:18 +00004020 }
4021
Gilles Peskine449bd832023-01-11 14:50:10 +01004022 MBEDTLS_SSL_DEBUG_MSG(2, ("<= renegotiate"));
Paul Bakker48916f92012-09-16 19:57:18 +00004023
Gilles Peskine449bd832023-01-11 14:50:10 +01004024 return 0;
Paul Bakker48916f92012-09-16 19:57:18 +00004025}
4026
4027/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004028 * Renegotiate current connection on client,
4029 * or request renegotiation on server
4030 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004031int mbedtls_ssl_renegotiate(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004032{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004033 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004034
Gilles Peskine449bd832023-01-11 14:50:10 +01004035 if (ssl == NULL || ssl->conf == NULL) {
4036 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4037 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02004038
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004039#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004040 /* On server, just send the request */
Gilles Peskine449bd832023-01-11 14:50:10 +01004041 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
4042 if (mbedtls_ssl_is_handshake_over(ssl) == 0) {
4043 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4044 }
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004045
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004046 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02004047
4048 /* Did we already try/start sending HelloRequest? */
Gilles Peskine449bd832023-01-11 14:50:10 +01004049 if (ssl->out_left != 0) {
4050 return mbedtls_ssl_flush_output(ssl);
4051 }
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02004052
Gilles Peskine449bd832023-01-11 14:50:10 +01004053 return ssl_write_hello_request(ssl);
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004054 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004055#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004056
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004057#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004058 /*
4059 * On client, either start the renegotiation process or,
4060 * if already in progress, continue the handshake
4061 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004062 if (ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
4063 if (mbedtls_ssl_is_handshake_over(ssl) == 0) {
4064 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004065 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004066
4067 if ((ret = mbedtls_ssl_start_renegotiation(ssl)) != 0) {
4068 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_start_renegotiation", ret);
4069 return ret;
4070 }
4071 } else {
4072 if ((ret = mbedtls_ssl_handshake(ssl)) != 0) {
4073 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_handshake", ret);
4074 return ret;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004075 }
4076 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004077#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004078
Gilles Peskine449bd832023-01-11 14:50:10 +01004079 return ret;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004080}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004081#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004082
Gilles Peskine449bd832023-01-11 14:50:10 +01004083void mbedtls_ssl_handshake_free(mbedtls_ssl_context *ssl)
Paul Bakker48916f92012-09-16 19:57:18 +00004084{
Gilles Peskine9b562d52018-04-25 20:32:43 +02004085 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
4086
Gilles Peskine449bd832023-01-11 14:50:10 +01004087 if (handshake == NULL) {
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004088 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01004089 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004090
Brett Warrene0edc842021-08-17 09:53:13 +01004091#if defined(MBEDTLS_ECP_C)
4092#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01004093 if (ssl->handshake->group_list_heap_allocated) {
4094 mbedtls_free((void *) handshake->group_list);
4095 }
Brett Warrene0edc842021-08-17 09:53:13 +01004096 handshake->group_list = NULL;
4097#endif /* MBEDTLS_DEPRECATED_REMOVED */
4098#endif /* MBEDTLS_ECP_C */
4099
Ronald Crone68ab4f2022-10-05 12:46:29 +02004100#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +08004101#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01004102 if (ssl->handshake->sig_algs_heap_allocated) {
4103 mbedtls_free((void *) handshake->sig_algs);
4104 }
Jerry Yuf017ee42022-01-12 15:49:48 +08004105 handshake->sig_algs = NULL;
4106#endif /* MBEDTLS_DEPRECATED_REMOVED */
Xiaofei Baic234ecf2022-02-08 09:59:23 +00004107#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01004108 if (ssl->handshake->certificate_request_context) {
4109 mbedtls_free((void *) handshake->certificate_request_context);
Xiaofei Baic234ecf2022-02-08 09:59:23 +00004110 }
4111#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Ronald Crone68ab4f2022-10-05 12:46:29 +02004112#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yuf017ee42022-01-12 15:49:48 +08004113
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02004114#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01004115 if (ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0) {
4116 ssl->conf->f_async_cancel(ssl);
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02004117 handshake->async_in_progress = 0;
4118 }
4119#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
4120
Andrzej Kurek25f27152022-08-17 16:09:31 -04004121#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -05004122#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01004123 psa_hash_abort(&handshake->fin_sha256_psa);
Andrzej Kurekeb342242019-01-29 09:14:33 -05004124#else
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01004125 mbedtls_md_free(&handshake->fin_sha256);
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02004126#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05004127#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004128#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -05004129#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01004130 psa_hash_abort(&handshake->fin_sha384_psa);
Andrzej Kurekeb342242019-01-29 09:14:33 -05004131#else
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01004132 mbedtls_md_free(&handshake->fin_sha384);
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02004133#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05004134#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02004135
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004136#if defined(MBEDTLS_DHM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004137 mbedtls_dhm_free(&handshake->dhm_ctx);
Paul Bakker48916f92012-09-16 19:57:18 +00004138#endif
Neil Armstrongf3f46412022-04-12 14:43:39 +02004139#if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004140 mbedtls_ecdh_free(&handshake->ecdh_ctx);
Paul Bakker61d113b2013-07-04 11:51:43 +02004141#endif
Valerio Setti02c25b52022-11-15 14:08:42 +01004142
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02004143#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Neil Armstrongca7d5062022-05-31 14:43:23 +02004144#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01004145 psa_pake_abort(&handshake->psa_pake_ctx);
Valerio Settieb3f7882022-12-08 18:42:58 +01004146 /*
4147 * Opaque keys are not stored in the handshake's data and it's the user
4148 * responsibility to destroy them. Clear ones, instead, are created by
4149 * the TLS library and should be destroyed at the same level
4150 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004151 if (!mbedtls_svc_key_id_is_null(handshake->psa_pake_password)) {
4152 psa_destroy_key(handshake->psa_pake_password);
Valerio Settieb3f7882022-12-08 18:42:58 +01004153 }
Neil Armstrongca7d5062022-05-31 14:43:23 +02004154 handshake->psa_pake_password = MBEDTLS_SVC_KEY_ID_INIT;
4155#else
Gilles Peskine449bd832023-01-11 14:50:10 +01004156 mbedtls_ecjpake_free(&handshake->ecjpake_ctx);
Neil Armstrongca7d5062022-05-31 14:43:23 +02004157#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02004158#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004159 mbedtls_free(handshake->ecjpake_cache);
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02004160 handshake->ecjpake_cache = NULL;
4161 handshake->ecjpake_cache_len = 0;
4162#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02004163#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02004164
Janos Follath4ae5c292016-02-10 11:27:43 +00004165#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
4166 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02004167 /* explicit void pointer cast for buggy MS compiler */
Gilles Peskine449bd832023-01-11 14:50:10 +01004168 mbedtls_free((void *) handshake->curves_tls_id);
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004169#endif
4170
Ronald Cron73fe8df2022-10-05 14:31:43 +02004171#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02004172#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01004173 if (!mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque)) {
Neil Armstrong501c9322022-05-03 09:35:09 +02004174 /* The maintenance of the external PSK key slot is the
4175 * user's responsibility. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004176 if (ssl->handshake->psk_opaque_is_internal) {
4177 psa_destroy_key(ssl->handshake->psk_opaque);
Neil Armstrong501c9322022-05-03 09:35:09 +02004178 ssl->handshake->psk_opaque_is_internal = 0;
4179 }
4180 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
4181 }
Neil Armstronge952a302022-05-03 10:22:14 +02004182#else
Gilles Peskine449bd832023-01-11 14:50:10 +01004183 if (handshake->psk != NULL) {
4184 mbedtls_platform_zeroize(handshake->psk, handshake->psk_len);
4185 mbedtls_free(handshake->psk);
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004186 }
Neil Armstronge952a302022-05-03 10:22:14 +02004187#endif /* MBEDTLS_USE_PSA_CRYPTO */
Ronald Cron73fe8df2022-10-05 14:31:43 +02004188#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004189
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004190#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
4191 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02004192 /*
4193 * Free only the linked list wrapper, not the keys themselves
4194 * since the belong to the SNI callback
4195 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004196 ssl_key_cert_free(handshake->sni_key_cert);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004197#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004198
Gilles Peskineeccd8882020-03-10 12:19:08 +01004199#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01004200 mbedtls_x509_crt_restart_free(&handshake->ecrs_ctx);
4201 if (handshake->ecrs_peer_cert != NULL) {
4202 mbedtls_x509_crt_free(handshake->ecrs_peer_cert);
4203 mbedtls_free(handshake->ecrs_peer_cert);
Hanno Becker3dad3112019-02-05 17:19:52 +00004204 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02004205#endif
4206
Hanno Becker75173122019-02-06 16:18:31 +00004207#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
4208 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01004209 mbedtls_pk_free(&handshake->peer_pubkey);
Hanno Becker75173122019-02-06 16:18:31 +00004210#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
4211
XiaokangQian9b93c0d2022-02-09 06:02:25 +00004212#if defined(MBEDTLS_SSL_CLI_C) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01004213 (defined(MBEDTLS_SSL_PROTO_DTLS) || defined(MBEDTLS_SSL_PROTO_TLS1_3))
4214 mbedtls_free(handshake->cookie);
XiaokangQian9b93c0d2022-02-09 06:02:25 +00004215#endif /* MBEDTLS_SSL_CLI_C &&
4216 ( MBEDTLS_SSL_PROTO_DTLS || MBEDTLS_SSL_PROTO_TLS1_3 ) */
XiaokangQian8499b6c2022-01-27 09:00:11 +00004217
4218#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004219 mbedtls_ssl_flight_free(handshake->flight);
4220 mbedtls_ssl_buffering_free(ssl);
XiaokangQian8499b6c2022-01-27 09:00:11 +00004221#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02004222
Ronald Cronf12b81d2022-03-15 10:42:41 +01004223#if defined(MBEDTLS_ECDH_C) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01004224 (defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3))
4225 if (handshake->ecdh_psa_privkey_is_external == 0) {
4226 psa_destroy_key(handshake->ecdh_psa_privkey);
4227 }
Hanno Becker4a63ed42019-01-08 11:39:35 +00004228#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
4229
Ronald Cron6f135e12021-12-08 16:57:54 +01004230#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01004231 mbedtls_ssl_transform_free(handshake->transform_handshake);
4232 mbedtls_free(handshake->transform_handshake);
Jerry Yu3d9b5902022-11-04 14:07:25 +08004233#if defined(MBEDTLS_SSL_EARLY_DATA)
Gilles Peskine449bd832023-01-11 14:50:10 +01004234 mbedtls_ssl_transform_free(handshake->transform_earlydata);
4235 mbedtls_free(handshake->transform_earlydata);
Jerry Yu3d9b5902022-11-04 14:07:25 +08004236#endif
Ronald Cron6f135e12021-12-08 16:57:54 +01004237#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yuba9c7272021-10-30 11:54:10 +08004238
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004239
4240#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4241 /* If the buffers are too big - reallocate. Because of the way Mbed TLS
4242 * processes datagrams and the fact that a datagram is allowed to have
4243 * several records in it, it is possible that the I/O buffers are not
4244 * empty at this stage */
Gilles Peskine449bd832023-01-11 14:50:10 +01004245 handle_buffer_resizing(ssl, 1, mbedtls_ssl_get_input_buflen(ssl),
4246 mbedtls_ssl_get_output_buflen(ssl));
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004247#endif
Hanno Becker3aa186f2021-08-10 09:24:19 +01004248
Jerry Yuba9c7272021-10-30 11:54:10 +08004249 /* mbedtls_platform_zeroize MUST be last one in this function */
Gilles Peskine449bd832023-01-11 14:50:10 +01004250 mbedtls_platform_zeroize(handshake,
4251 sizeof(mbedtls_ssl_handshake_params));
Paul Bakker48916f92012-09-16 19:57:18 +00004252}
4253
Gilles Peskine449bd832023-01-11 14:50:10 +01004254void mbedtls_ssl_session_free(mbedtls_ssl_session *session)
Paul Bakker48916f92012-09-16 19:57:18 +00004255{
Gilles Peskine449bd832023-01-11 14:50:10 +01004256 if (session == NULL) {
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004257 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01004258 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004259
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004260#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004261 ssl_clear_peer_cert(session);
Paul Bakkered27a042013-04-18 22:46:23 +02004262#endif
Paul Bakker0a597072012-09-25 21:55:46 +00004263
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02004264#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Xiaokang Qianbc663a02022-10-09 11:14:39 +00004265#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
4266 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01004267 mbedtls_free(session->hostname);
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00004268#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01004269 mbedtls_free(session->ticket);
Paul Bakkera503a632013-08-14 13:48:06 +02004270#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004271
Gilles Peskine449bd832023-01-11 14:50:10 +01004272 mbedtls_platform_zeroize(session, sizeof(mbedtls_ssl_session));
Paul Bakker48916f92012-09-16 19:57:18 +00004273}
4274
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02004275#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004276
4277#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4278#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u
4279#else
4280#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u
4281#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4282
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004283#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004284
4285#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4286#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u
4287#else
4288#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u
4289#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4290
4291#if defined(MBEDTLS_SSL_ALPN)
4292#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u
4293#else
4294#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u
4295#endif /* MBEDTLS_SSL_ALPN */
4296
4297#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0
4298#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1
4299#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2
4300#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3
4301
4302#define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \
Gilles Peskine449bd832023-01-11 14:50:10 +01004303 ((uint32_t) ( \
4304 (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << \
4305 SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT) | \
4306 (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << \
4307 SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT) | \
4308 (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << \
4309 SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT) | \
4310 (SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT) | \
4311 0u))
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004312
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004313static unsigned char ssl_serialized_context_header[] = {
4314 MBEDTLS_VERSION_MAJOR,
4315 MBEDTLS_VERSION_MINOR,
4316 MBEDTLS_VERSION_PATCH,
Gilles Peskine449bd832023-01-11 14:50:10 +01004317 MBEDTLS_BYTE_1(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
4318 MBEDTLS_BYTE_0(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
4319 MBEDTLS_BYTE_2(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG),
4320 MBEDTLS_BYTE_1(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG),
4321 MBEDTLS_BYTE_0(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG),
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004322};
4323
Paul Bakker5121ce52009-01-03 21:22:43 +00004324/*
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004325 * Serialize a full SSL context
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02004326 *
4327 * The format of the serialized data is:
4328 * (in the presentation language of TLS, RFC 8446 section 3)
4329 *
4330 * // header
4331 * opaque mbedtls_version[3]; // major, minor, patch
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004332 * opaque context_format[5]; // version-specific field determining
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02004333 * // the format of the remaining
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004334 * // serialized data.
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004335 * Note: When updating the format, remember to keep these
4336 * version+format bytes. (We may make their size part of the API.)
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02004337 *
4338 * // session sub-structure
4339 * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save()
4340 * // transform sub-structure
4341 * uint8 random[64]; // ServerHello.random+ClientHello.random
4342 * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value
4343 * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use
4344 * // fields from ssl_context
4345 * uint32 badmac_seen; // DTLS: number of records with failing MAC
4346 * uint64 in_window_top; // DTLS: last validated record seq_num
4347 * uint64 in_window; // DTLS: bitmask for replay protection
4348 * uint8 disable_datagram_packing; // DTLS: only one record per datagram
4349 * uint64 cur_out_ctr; // Record layer: outgoing sequence number
4350 * uint16 mtu; // DTLS: path mtu (max outgoing fragment size)
4351 * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol
4352 *
4353 * Note that many fields of the ssl_context or sub-structures are not
4354 * serialized, as they fall in one of the following categories:
4355 *
4356 * 1. forced value (eg in_left must be 0)
4357 * 2. pointer to dynamically-allocated memory (eg session, transform)
4358 * 3. value can be re-derived from other data (eg session keys from MS)
4359 * 4. value was temporary (eg content of input buffer)
4360 * 5. value will be provided by the user again (eg I/O callbacks and context)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004361 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004362int mbedtls_ssl_context_save(mbedtls_ssl_context *ssl,
4363 unsigned char *buf,
4364 size_t buf_len,
4365 size_t *olen)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004366{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004367 unsigned char *p = buf;
4368 size_t used = 0;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004369 size_t session_len;
4370 int ret = 0;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004371
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02004372 /*
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004373 * Enforce usage restrictions, see "return BAD_INPUT_DATA" in
4374 * this function's documentation.
4375 *
4376 * These are due to assumptions/limitations in the implementation. Some of
4377 * them are likely to stay (no handshake in progress) some might go away
4378 * (only DTLS) but are currently used to simplify the implementation.
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02004379 */
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004380 /* The initial handshake must be over */
Gilles Peskine449bd832023-01-11 14:50:10 +01004381 if (mbedtls_ssl_is_handshake_over(ssl) == 0) {
4382 MBEDTLS_SSL_DEBUG_MSG(1, ("Initial handshake isn't over"));
4383 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004384 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004385 if (ssl->handshake != NULL) {
4386 MBEDTLS_SSL_DEBUG_MSG(1, ("Handshake isn't completed"));
4387 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004388 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004389 /* Double-check that sub-structures are indeed ready */
Gilles Peskine449bd832023-01-11 14:50:10 +01004390 if (ssl->transform == NULL || ssl->session == NULL) {
4391 MBEDTLS_SSL_DEBUG_MSG(1, ("Serialised structures aren't ready"));
4392 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004393 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004394 /* There must be no pending incoming or outgoing data */
Gilles Peskine449bd832023-01-11 14:50:10 +01004395 if (mbedtls_ssl_check_pending(ssl) != 0) {
4396 MBEDTLS_SSL_DEBUG_MSG(1, ("There is pending incoming data"));
4397 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004398 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004399 if (ssl->out_left != 0) {
4400 MBEDTLS_SSL_DEBUG_MSG(1, ("There is pending outgoing data"));
4401 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004402 }
Dave Rodgman556e8a32022-12-06 16:31:25 +00004403 /* Protocol must be DTLS, not TLS */
Gilles Peskine449bd832023-01-11 14:50:10 +01004404 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
4405 MBEDTLS_SSL_DEBUG_MSG(1, ("Only DTLS is supported"));
4406 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004407 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004408 /* Version must be 1.2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004409 if (ssl->tls_version != MBEDTLS_SSL_VERSION_TLS1_2) {
4410 MBEDTLS_SSL_DEBUG_MSG(1, ("Only version 1.2 supported"));
4411 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004412 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004413 /* We must be using an AEAD ciphersuite */
Gilles Peskine449bd832023-01-11 14:50:10 +01004414 if (mbedtls_ssl_transform_uses_aead(ssl->transform) != 1) {
4415 MBEDTLS_SSL_DEBUG_MSG(1, ("Only AEAD ciphersuites supported"));
4416 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004417 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004418 /* Renegotiation must not be enabled */
4419#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01004420 if (ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED) {
4421 MBEDTLS_SSL_DEBUG_MSG(1, ("Renegotiation must not be enabled"));
4422 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004423 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004424#endif
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004425
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004426 /*
4427 * Version and format identifier
4428 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004429 used += sizeof(ssl_serialized_context_header);
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004430
Gilles Peskine449bd832023-01-11 14:50:10 +01004431 if (used <= buf_len) {
4432 memcpy(p, ssl_serialized_context_header,
4433 sizeof(ssl_serialized_context_header));
4434 p += sizeof(ssl_serialized_context_header);
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004435 }
4436
4437 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004438 * Session (length + data)
4439 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004440 ret = ssl_session_save(ssl->session, 1, NULL, 0, &session_len);
4441 if (ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL) {
4442 return ret;
4443 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004444
4445 used += 4 + session_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004446 if (used <= buf_len) {
4447 MBEDTLS_PUT_UINT32_BE(session_len, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004448 p += 4;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004449
Gilles Peskine449bd832023-01-11 14:50:10 +01004450 ret = ssl_session_save(ssl->session, 1,
4451 p, session_len, &session_len);
4452 if (ret != 0) {
4453 return ret;
4454 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004455
4456 p += session_len;
4457 }
4458
4459 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004460 * Transform
4461 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004462 used += sizeof(ssl->transform->randbytes);
4463 if (used <= buf_len) {
4464 memcpy(p, ssl->transform->randbytes,
4465 sizeof(ssl->transform->randbytes));
4466 p += sizeof(ssl->transform->randbytes);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004467 }
4468
4469#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4470 used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004471 if (used <= buf_len) {
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004472 *p++ = ssl->transform->in_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004473 memcpy(p, ssl->transform->in_cid, ssl->transform->in_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004474 p += ssl->transform->in_cid_len;
4475
4476 *p++ = ssl->transform->out_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004477 memcpy(p, ssl->transform->out_cid, ssl->transform->out_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004478 p += ssl->transform->out_cid_len;
4479 }
4480#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4481
4482 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004483 * Saved fields from top-level ssl_context structure
4484 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004485 used += 4;
Gilles Peskine449bd832023-01-11 14:50:10 +01004486 if (used <= buf_len) {
4487 MBEDTLS_PUT_UINT32_BE(ssl->badmac_seen, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004488 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004489 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004490
4491#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4492 used += 16;
Gilles Peskine449bd832023-01-11 14:50:10 +01004493 if (used <= buf_len) {
4494 MBEDTLS_PUT_UINT64_BE(ssl->in_window_top, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004495 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004496
Gilles Peskine449bd832023-01-11 14:50:10 +01004497 MBEDTLS_PUT_UINT64_BE(ssl->in_window, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004498 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004499 }
4500#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4501
4502#if defined(MBEDTLS_SSL_PROTO_DTLS)
4503 used += 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004504 if (used <= buf_len) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004505 *p++ = ssl->disable_datagram_packing;
4506 }
4507#endif /* MBEDTLS_SSL_PROTO_DTLS */
4508
Jerry Yuae0b2e22021-10-08 15:21:19 +08004509 used += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Gilles Peskine449bd832023-01-11 14:50:10 +01004510 if (used <= buf_len) {
4511 memcpy(p, ssl->cur_out_ctr, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN);
Jerry Yuae0b2e22021-10-08 15:21:19 +08004512 p += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004513 }
4514
4515#if defined(MBEDTLS_SSL_PROTO_DTLS)
4516 used += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01004517 if (used <= buf_len) {
4518 MBEDTLS_PUT_UINT16_BE(ssl->mtu, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004519 p += 2;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004520 }
4521#endif /* MBEDTLS_SSL_PROTO_DTLS */
4522
4523#if defined(MBEDTLS_SSL_ALPN)
4524 {
4525 const uint8_t alpn_len = ssl->alpn_chosen
Gilles Peskine449bd832023-01-11 14:50:10 +01004526 ? (uint8_t) strlen(ssl->alpn_chosen)
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004527 : 0;
4528
4529 used += 1 + alpn_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004530 if (used <= buf_len) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004531 *p++ = alpn_len;
4532
Gilles Peskine449bd832023-01-11 14:50:10 +01004533 if (ssl->alpn_chosen != NULL) {
4534 memcpy(p, ssl->alpn_chosen, alpn_len);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004535 p += alpn_len;
4536 }
4537 }
4538 }
4539#endif /* MBEDTLS_SSL_ALPN */
4540
4541 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004542 * Done
4543 */
4544 *olen = used;
4545
Gilles Peskine449bd832023-01-11 14:50:10 +01004546 if (used > buf_len) {
4547 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
4548 }
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004549
Gilles Peskine449bd832023-01-11 14:50:10 +01004550 MBEDTLS_SSL_DEBUG_BUF(4, "saved context", buf, used);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004551
Gilles Peskine449bd832023-01-11 14:50:10 +01004552 return mbedtls_ssl_session_reset_int(ssl, 0);
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004553}
4554
4555/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02004556 * Deserialize context, see mbedtls_ssl_context_save() for format.
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004557 *
4558 * This internal version is wrapped by a public function that cleans up in
4559 * case of error.
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004560 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004561MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01004562static int ssl_context_load(mbedtls_ssl_context *ssl,
4563 const unsigned char *buf,
4564 size_t len)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004565{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004566 const unsigned char *p = buf;
4567 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004568 size_t session_len;
Janos Follath865b3eb2019-12-16 11:46:15 +00004569 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004570#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek894edde2022-09-29 06:31:14 -04004571 tls_prf_fn prf_func = NULL;
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004572#endif
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004573
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004574 /*
4575 * The context should have been freshly setup or reset.
4576 * Give the user an error in case of obvious misuse.
Manuel Pégourié-Gonnard4ca930f2019-07-26 16:31:53 +02004577 * (Checking session is useful because it won't be NULL if we're
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004578 * renegotiating, or if the user mistakenly loaded a session first.)
4579 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004580 if (ssl->state != MBEDTLS_SSL_HELLO_REQUEST ||
4581 ssl->session != NULL) {
4582 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004583 }
4584
4585 /*
4586 * We can't check that the config matches the initial one, but we can at
4587 * least check it matches the requirements for serializing.
4588 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004589 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
Glenn Strauss2dfcea22022-03-14 17:26:42 -04004590 ssl->conf->max_tls_version < MBEDTLS_SSL_VERSION_TLS1_2 ||
4591 ssl->conf->min_tls_version > MBEDTLS_SSL_VERSION_TLS1_2 ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004592#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02004593 ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004594#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01004595 0) {
4596 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004597 }
4598
Gilles Peskine449bd832023-01-11 14:50:10 +01004599 MBEDTLS_SSL_DEBUG_BUF(4, "context to load", buf, len);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004600
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004601 /*
4602 * Check version identifier
4603 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004604 if ((size_t) (end - p) < sizeof(ssl_serialized_context_header)) {
4605 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004606 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004607
4608 if (memcmp(p, ssl_serialized_context_header,
4609 sizeof(ssl_serialized_context_header)) != 0) {
4610 return MBEDTLS_ERR_SSL_VERSION_MISMATCH;
4611 }
4612 p += sizeof(ssl_serialized_context_header);
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004613
4614 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004615 * Session
4616 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004617 if ((size_t) (end - p) < 4) {
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 session_len = ((size_t) p[0] << 24) |
4622 ((size_t) p[1] << 16) |
4623 ((size_t) p[2] << 8) |
4624 ((size_t) p[3]);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004625 p += 4;
4626
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004627 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00004628 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004629 ssl->session = ssl->session_negotiate;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004630 ssl->session_in = ssl->session;
4631 ssl->session_out = ssl->session;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004632 ssl->session_negotiate = NULL;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004633
Gilles Peskine449bd832023-01-11 14:50:10 +01004634 if ((size_t) (end - p) < session_len) {
4635 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4636 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004637
Gilles Peskine449bd832023-01-11 14:50:10 +01004638 ret = ssl_session_load(ssl->session, 1, p, session_len);
4639 if (ret != 0) {
4640 mbedtls_ssl_session_free(ssl->session);
4641 return ret;
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004642 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004643
4644 p += session_len;
4645
4646 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004647 * Transform
4648 */
4649
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004650 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00004651 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Jerry Yu2e199812022-12-01 18:57:19 +08004652#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004653 ssl->transform = ssl->transform_negotiate;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004654 ssl->transform_in = ssl->transform;
4655 ssl->transform_out = ssl->transform;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004656 ssl->transform_negotiate = NULL;
Jerry Yu2e199812022-12-01 18:57:19 +08004657#endif
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004658
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004659#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01004660 prf_func = ssl_tls12prf_from_cs(ssl->session->ciphersuite);
4661 if (prf_func == NULL) {
4662 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4663 }
Andrzej Kurek894edde2022-09-29 06:31:14 -04004664
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004665 /* Read random bytes and populate structure */
Gilles Peskine449bd832023-01-11 14:50:10 +01004666 if ((size_t) (end - p) < sizeof(ssl->transform->randbytes)) {
4667 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4668 }
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004669
Gilles Peskine449bd832023-01-11 14:50:10 +01004670 ret = ssl_tls12_populate_transform(ssl->transform,
4671 ssl->session->ciphersuite,
4672 ssl->session->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02004673#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004674 ssl->session->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02004675#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01004676 prf_func,
4677 p, /* currently pointing to randbytes */
4678 MBEDTLS_SSL_VERSION_TLS1_2, /* (D)TLS 1.2 is forced */
4679 ssl->conf->endpoint,
4680 ssl);
4681 if (ret != 0) {
4682 return ret;
4683 }
Jerry Yu840fbb22022-02-17 14:59:29 +08004684#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004685 p += sizeof(ssl->transform->randbytes);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004686
4687#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4688 /* Read connection IDs and store them */
Gilles Peskine449bd832023-01-11 14:50:10 +01004689 if ((size_t) (end - p) < 1) {
4690 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4691 }
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004692
4693 ssl->transform->in_cid_len = *p++;
4694
Gilles Peskine449bd832023-01-11 14:50:10 +01004695 if ((size_t) (end - p) < ssl->transform->in_cid_len + 1u) {
4696 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4697 }
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004698
Gilles Peskine449bd832023-01-11 14:50:10 +01004699 memcpy(ssl->transform->in_cid, p, ssl->transform->in_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004700 p += ssl->transform->in_cid_len;
4701
4702 ssl->transform->out_cid_len = *p++;
4703
Gilles Peskine449bd832023-01-11 14:50:10 +01004704 if ((size_t) (end - p) < ssl->transform->out_cid_len) {
4705 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4706 }
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004707
Gilles Peskine449bd832023-01-11 14:50:10 +01004708 memcpy(ssl->transform->out_cid, p, ssl->transform->out_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004709 p += ssl->transform->out_cid_len;
4710#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4711
4712 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004713 * Saved fields from top-level ssl_context structure
4714 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004715 if ((size_t) (end - p) < 4) {
4716 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4717 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004718
Gilles Peskine449bd832023-01-11 14:50:10 +01004719 ssl->badmac_seen = ((uint32_t) p[0] << 24) |
4720 ((uint32_t) p[1] << 16) |
4721 ((uint32_t) p[2] << 8) |
4722 ((uint32_t) p[3]);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004723 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004724
4725#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine449bd832023-01-11 14:50:10 +01004726 if ((size_t) (end - p) < 16) {
4727 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4728 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004729
Gilles Peskine449bd832023-01-11 14:50:10 +01004730 ssl->in_window_top = ((uint64_t) p[0] << 56) |
4731 ((uint64_t) p[1] << 48) |
4732 ((uint64_t) p[2] << 40) |
4733 ((uint64_t) p[3] << 32) |
4734 ((uint64_t) p[4] << 24) |
4735 ((uint64_t) p[5] << 16) |
4736 ((uint64_t) p[6] << 8) |
4737 ((uint64_t) p[7]);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004738 p += 8;
4739
Gilles Peskine449bd832023-01-11 14:50:10 +01004740 ssl->in_window = ((uint64_t) p[0] << 56) |
4741 ((uint64_t) p[1] << 48) |
4742 ((uint64_t) p[2] << 40) |
4743 ((uint64_t) p[3] << 32) |
4744 ((uint64_t) p[4] << 24) |
4745 ((uint64_t) p[5] << 16) |
4746 ((uint64_t) p[6] << 8) |
4747 ((uint64_t) p[7]);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004748 p += 8;
4749#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4750
4751#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004752 if ((size_t) (end - p) < 1) {
4753 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4754 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004755
4756 ssl->disable_datagram_packing = *p++;
4757#endif /* MBEDTLS_SSL_PROTO_DTLS */
4758
Gilles Peskine449bd832023-01-11 14:50:10 +01004759 if ((size_t) (end - p) < sizeof(ssl->cur_out_ctr)) {
4760 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4761 }
4762 memcpy(ssl->cur_out_ctr, p, sizeof(ssl->cur_out_ctr));
4763 p += sizeof(ssl->cur_out_ctr);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004764
4765#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004766 if ((size_t) (end - p) < 2) {
4767 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4768 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004769
Gilles Peskine449bd832023-01-11 14:50:10 +01004770 ssl->mtu = (p[0] << 8) | p[1];
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004771 p += 2;
4772#endif /* MBEDTLS_SSL_PROTO_DTLS */
4773
4774#if defined(MBEDTLS_SSL_ALPN)
4775 {
4776 uint8_t alpn_len;
4777 const char **cur;
4778
Gilles Peskine449bd832023-01-11 14:50:10 +01004779 if ((size_t) (end - p) < 1) {
4780 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4781 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004782
4783 alpn_len = *p++;
4784
Gilles Peskine449bd832023-01-11 14:50:10 +01004785 if (alpn_len != 0 && ssl->conf->alpn_list != NULL) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004786 /* alpn_chosen should point to an item in the configured list */
Gilles Peskine449bd832023-01-11 14:50:10 +01004787 for (cur = ssl->conf->alpn_list; *cur != NULL; cur++) {
4788 if (strlen(*cur) == alpn_len &&
4789 memcmp(p, cur, alpn_len) == 0) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004790 ssl->alpn_chosen = *cur;
4791 break;
4792 }
4793 }
4794 }
4795
4796 /* can only happen on conf mismatch */
Gilles Peskine449bd832023-01-11 14:50:10 +01004797 if (alpn_len != 0 && ssl->alpn_chosen == NULL) {
4798 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4799 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004800
4801 p += alpn_len;
4802 }
4803#endif /* MBEDTLS_SSL_ALPN */
4804
4805 /*
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004806 * Forced fields from top-level ssl_context structure
4807 *
4808 * Most of them already set to the correct value by mbedtls_ssl_init() and
4809 * mbedtls_ssl_reset(), so we only need to set the remaining ones.
4810 */
4811 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
Glenn Strauss60bfe602022-03-14 19:04:24 -04004812 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004813
Hanno Becker361b10d2019-08-30 10:42:49 +01004814 /* Adjust pointers for header fields of outgoing records to
4815 * the given transform, accounting for explicit IV and CID. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004816 mbedtls_ssl_update_out_pointers(ssl, ssl->transform);
Hanno Becker361b10d2019-08-30 10:42:49 +01004817
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004818#if defined(MBEDTLS_SSL_PROTO_DTLS)
4819 ssl->in_epoch = 1;
4820#endif
4821
4822 /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated,
4823 * which we don't want - otherwise we'd end up freeing the wrong transform
Hanno Beckerce5f5fd2020-02-05 10:47:44 +00004824 * by calling mbedtls_ssl_handshake_wrapup_free_hs_transform()
4825 * inappropriately. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004826 if (ssl->handshake != NULL) {
4827 mbedtls_ssl_handshake_free(ssl);
4828 mbedtls_free(ssl->handshake);
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004829 ssl->handshake = NULL;
4830 }
4831
4832 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004833 * Done - should have consumed entire buffer
4834 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004835 if (p != end) {
4836 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4837 }
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004838
Gilles Peskine449bd832023-01-11 14:50:10 +01004839 return 0;
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004840}
4841
4842/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02004843 * Deserialize context: public wrapper for error cleaning
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004844 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004845int mbedtls_ssl_context_load(mbedtls_ssl_context *context,
4846 const unsigned char *buf,
4847 size_t len)
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004848{
Gilles Peskine449bd832023-01-11 14:50:10 +01004849 int ret = ssl_context_load(context, buf, len);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004850
Gilles Peskine449bd832023-01-11 14:50:10 +01004851 if (ret != 0) {
4852 mbedtls_ssl_free(context);
4853 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004854
Gilles Peskine449bd832023-01-11 14:50:10 +01004855 return ret;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004856}
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02004857#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004858
4859/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004860 * Free an SSL context
4861 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004862void mbedtls_ssl_free(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00004863{
Gilles Peskine449bd832023-01-11 14:50:10 +01004864 if (ssl == NULL) {
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004865 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01004866 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004867
Gilles Peskine449bd832023-01-11 14:50:10 +01004868 MBEDTLS_SSL_DEBUG_MSG(2, ("=> free"));
Paul Bakker5121ce52009-01-03 21:22:43 +00004869
Gilles Peskine449bd832023-01-11 14:50:10 +01004870 if (ssl->out_buf != NULL) {
sander-visserb8aa2072020-05-06 22:05:13 +02004871#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4872 size_t out_buf_len = ssl->out_buf_len;
4873#else
4874 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
4875#endif
4876
Gilles Peskine449bd832023-01-11 14:50:10 +01004877 mbedtls_platform_zeroize(ssl->out_buf, out_buf_len);
4878 mbedtls_free(ssl->out_buf);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004879 ssl->out_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004880 }
4881
Gilles Peskine449bd832023-01-11 14:50:10 +01004882 if (ssl->in_buf != NULL) {
sander-visserb8aa2072020-05-06 22:05:13 +02004883#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4884 size_t in_buf_len = ssl->in_buf_len;
4885#else
4886 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
4887#endif
4888
Gilles Peskine449bd832023-01-11 14:50:10 +01004889 mbedtls_platform_zeroize(ssl->in_buf, in_buf_len);
4890 mbedtls_free(ssl->in_buf);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004891 ssl->in_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004892 }
4893
Gilles Peskine449bd832023-01-11 14:50:10 +01004894 if (ssl->transform) {
4895 mbedtls_ssl_transform_free(ssl->transform);
4896 mbedtls_free(ssl->transform);
Paul Bakker48916f92012-09-16 19:57:18 +00004897 }
4898
Gilles Peskine449bd832023-01-11 14:50:10 +01004899 if (ssl->handshake) {
4900 mbedtls_ssl_handshake_free(ssl);
4901 mbedtls_free(ssl->handshake);
Jerry Yu2e199812022-12-01 18:57:19 +08004902
4903#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01004904 mbedtls_ssl_transform_free(ssl->transform_negotiate);
4905 mbedtls_free(ssl->transform_negotiate);
Jerry Yu2e199812022-12-01 18:57:19 +08004906#endif
4907
Gilles Peskine449bd832023-01-11 14:50:10 +01004908 mbedtls_ssl_session_free(ssl->session_negotiate);
4909 mbedtls_free(ssl->session_negotiate);
Paul Bakker48916f92012-09-16 19:57:18 +00004910 }
4911
Ronald Cron6f135e12021-12-08 16:57:54 +01004912#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01004913 mbedtls_ssl_transform_free(ssl->transform_application);
4914 mbedtls_free(ssl->transform_application);
Ronald Cron6f135e12021-12-08 16:57:54 +01004915#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker3aa186f2021-08-10 09:24:19 +01004916
Gilles Peskine449bd832023-01-11 14:50:10 +01004917 if (ssl->session) {
4918 mbedtls_ssl_session_free(ssl->session);
4919 mbedtls_free(ssl->session);
Paul Bakkerc0463502013-02-14 11:19:38 +01004920 }
4921
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02004922#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004923 if (ssl->hostname != NULL) {
4924 mbedtls_platform_zeroize(ssl->hostname, strlen(ssl->hostname));
4925 mbedtls_free(ssl->hostname);
Paul Bakker5121ce52009-01-03 21:22:43 +00004926 }
Paul Bakker0be444a2013-08-27 21:55:01 +02004927#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004928
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02004929#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004930 mbedtls_free(ssl->cli_id);
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02004931#endif
4932
Gilles Peskine449bd832023-01-11 14:50:10 +01004933 MBEDTLS_SSL_DEBUG_MSG(2, ("<= free"));
Paul Bakker2da561c2009-02-05 18:00:28 +00004934
Paul Bakker86f04f42013-02-14 11:20:09 +01004935 /* Actually clear after last debug message */
Gilles Peskine449bd832023-01-11 14:50:10 +01004936 mbedtls_platform_zeroize(ssl, sizeof(mbedtls_ssl_context));
Paul Bakker5121ce52009-01-03 21:22:43 +00004937}
4938
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004939/*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08004940 * Initialize mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004941 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004942void mbedtls_ssl_config_init(mbedtls_ssl_config *conf)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004943{
Gilles Peskine449bd832023-01-11 14:50:10 +01004944 memset(conf, 0, sizeof(mbedtls_ssl_config));
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004945}
4946
Gilles Peskineae270bf2021-06-02 00:05:29 +02004947/* The selection should be the same as mbedtls_x509_crt_profile_default in
4948 * x509_crt.c, plus Montgomery curves for ECDHE. Here, the order matters:
Gilles Peskineb1940a72021-06-02 15:18:12 +02004949 * curves with a lower resource usage come first.
Gilles Peskineae270bf2021-06-02 00:05:29 +02004950 * See the documentation of mbedtls_ssl_conf_curves() for what we promise
Gilles Peskineb1940a72021-06-02 15:18:12 +02004951 * about this list.
4952 */
Brett Warrene0edc842021-08-17 09:53:13 +01004953static uint16_t ssl_preset_default_groups[] = {
Gilles Peskineae270bf2021-06-02 00:05:29 +02004954#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004955 MBEDTLS_SSL_IANA_TLS_GROUP_X25519,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004956#endif
4957#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004958 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004959#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02004960#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004961 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004962#endif
4963#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004964 MBEDTLS_SSL_IANA_TLS_GROUP_X448,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004965#endif
4966#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004967 MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004968#endif
Gilles Peskineae270bf2021-06-02 00:05:29 +02004969#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004970 MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004971#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02004972#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004973 MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004974#endif
4975#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004976 MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004977#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004978 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Gilles Peskineae270bf2021-06-02 00:05:29 +02004979};
Gilles Peskineae270bf2021-06-02 00:05:29 +02004980
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004981static int ssl_preset_suiteb_ciphersuites[] = {
4982 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
4983 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
4984 0
4985};
4986
Ronald Crone68ab4f2022-10-05 12:46:29 +02004987#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004988
Jerry Yu909df7b2022-01-22 11:56:27 +08004989/* NOTICE:
Jerry Yu0b994b82022-01-25 17:22:12 +08004990 * For ssl_preset_*_sig_algs and ssl_tls12_preset_*_sig_algs, the following
Jerry Yu370e1462022-01-25 10:36:53 +08004991 * rules SHOULD be upheld.
4992 * - No duplicate entries.
4993 * - But if there is a good reason, do not change the order of the algorithms.
Jerry Yu09a99fc2022-07-28 14:22:17 +08004994 * - ssl_tls12_preset* is for TLS 1.2 use only.
Jerry Yu370e1462022-01-25 10:36:53 +08004995 * - ssl_preset_* is for TLS 1.3 only or hybrid TLS 1.3/1.2 handshakes.
Jerry Yu1a8b4812022-01-20 17:56:50 +08004996 */
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004997static uint16_t ssl_preset_default_sig_algs[] = {
Jerry Yu1a8b4812022-01-20 17:56:50 +08004998
Valerio Setti75fba322023-02-23 17:35:09 +01004999#if defined(MBEDTLS_PK_CAN_ECDSA_SOME) && \
5000 defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
Jerry Yued5e9f42022-01-26 11:21:34 +08005001 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
5002 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Valerio Setti75fba322023-02-23 17:35:09 +01005003#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 +08005004 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08005005
Valerio Setti75fba322023-02-23 17:35:09 +01005006#if defined(MBEDTLS_PK_CAN_ECDSA_SOME) && \
5007 defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
Jerry Yu909df7b2022-01-22 11:56:27 +08005008 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
5009 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Valerio Setti75fba322023-02-23 17:35:09 +01005010#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 +08005011 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
5012
Valerio Setti75fba322023-02-23 17:35:09 +01005013#if defined(MBEDTLS_PK_CAN_ECDSA_SOME) && \
5014 defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
Jerry Yued5e9f42022-01-26 11:21:34 +08005015 defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
5016 MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512,
Valerio Setti75fba322023-02-23 17:35:09 +01005017#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 +08005018 MBEDTLS_ECP_DP_SECP521R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08005019
Gilles Peskine449bd832023-01-11 14:50:10 +01005020#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && \
5021 defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu9bb3ee42022-06-23 10:16:33 +08005022 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
Gilles Peskine449bd832023-01-11 14:50:10 +01005023#endif \
5024 /* 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 +08005025
Gilles Peskine449bd832023-01-11 14:50:10 +01005026#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && \
5027 defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu9bb3ee42022-06-23 10:16:33 +08005028 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
Gilles Peskine449bd832023-01-11 14:50:10 +01005029#endif \
5030 /* 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 +08005031
Gilles Peskine449bd832023-01-11 14:50:10 +01005032#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && \
5033 defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu9bb3ee42022-06-23 10:16:33 +08005034 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Gilles Peskine449bd832023-01-11 14:50:10 +01005035#endif \
5036 /* 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 +08005037
Andrzej Kurek25f27152022-08-17 16:09:31 -04005038#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 +08005039 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512,
5040#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA512_C */
5041
Andrzej Kurek25f27152022-08-17 16:09:31 -04005042#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 +08005043 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384,
5044#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA384_C */
5045
Andrzej Kurek25f27152022-08-17 16:09:31 -04005046#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 +08005047 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
5048#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA256_C */
5049
Gabor Mezei15b95a62022-05-09 16:37:58 +02005050 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08005051};
5052
5053/* NOTICE: see above */
5054#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5055static uint16_t ssl_tls12_preset_default_sig_algs[] = {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005056#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Valerio Setti75fba322023-02-23 17:35:09 +01005057#if defined(MBEDTLS_PK_CAN_ECDSA_SOME)
Gilles Peskine449bd832023-01-11 14:50:10 +01005058 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA512),
Jerry Yu713013f2022-01-17 18:16:35 +08005059#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08005060#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
5061 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
5062#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02005063#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005064 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA512),
Gabor Mezeic1051b62022-05-10 13:13:58 +02005065#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005066#endif /* MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04005067#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Valerio Setti75fba322023-02-23 17:35:09 +01005068#if defined(MBEDTLS_PK_CAN_ECDSA_SOME)
Gilles Peskine449bd832023-01-11 14:50:10 +01005069 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08005070#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08005071#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
5072 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
5073#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02005074#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005075 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384),
Gabor Mezeic1051b62022-05-10 13:13:58 +02005076#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005077#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04005078#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Valerio Setti75fba322023-02-23 17:35:09 +01005079#if defined(MBEDTLS_PK_CAN_ECDSA_SOME)
Gilles Peskine449bd832023-01-11 14:50:10 +01005080 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08005081#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08005082#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
5083 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
5084#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02005085#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005086 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256),
Gabor Mezeic1051b62022-05-10 13:13:58 +02005087#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005088#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Gabor Mezei15b95a62022-05-09 16:37:58 +02005089 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08005090};
5091#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5092/* NOTICE: see above */
5093static uint16_t ssl_preset_suiteb_sig_algs[] = {
5094
Andrzej Kurek25f27152022-08-17 16:09:31 -04005095#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 +08005096 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
5097 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005098#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu909df7b2022-01-22 11:56:27 +08005099 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
5100
Andrzej Kurek25f27152022-08-17 16:09:31 -04005101#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 +08005102 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
5103 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005104#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu53037892022-01-25 11:02:06 +08005105 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08005106
Gilles Peskine449bd832023-01-11 14:50:10 +01005107#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && \
5108 defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu909df7b2022-01-22 11:56:27 +08005109 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Gilles Peskine449bd832023-01-11 14:50:10 +01005110#endif \
5111 /* 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 +08005112
Andrzej Kurek25f27152022-08-17 16:09:31 -04005113#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 +08005114 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005115#endif /* MBEDTLS_RSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yu53037892022-01-25 11:02:06 +08005116
Gabor Mezei15b95a62022-05-09 16:37:58 +02005117 MBEDTLS_TLS_SIG_NONE
Jerry Yu713013f2022-01-17 18:16:35 +08005118};
Jerry Yu6106fdc2022-01-12 16:36:14 +08005119
Jerry Yu909df7b2022-01-22 11:56:27 +08005120/* NOTICE: see above */
5121#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5122static uint16_t ssl_tls12_preset_suiteb_sig_algs[] = {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005123#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02005124#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005125 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256),
Jerry Yu713013f2022-01-17 18:16:35 +08005126#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02005127#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005128 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256),
Gabor Mezeic1051b62022-05-10 13:13:58 +02005129#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005130#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04005131#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02005132#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005133 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384),
Jerry Yu18c833e2022-01-25 10:55:47 +08005134#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02005135#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005136 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384),
Gabor Mezeic1051b62022-05-10 13:13:58 +02005137#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005138#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Gabor Mezei15b95a62022-05-09 16:37:58 +02005139 MBEDTLS_TLS_SIG_NONE
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01005140};
Jerry Yu909df7b2022-01-22 11:56:27 +08005141#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5142
Ronald Crone68ab4f2022-10-05 12:46:29 +02005143#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005144
Brett Warrene0edc842021-08-17 09:53:13 +01005145static uint16_t ssl_preset_suiteb_groups[] = {
Jaeden Amerod4311042019-06-03 08:27:16 +01005146#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01005147 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01005148#endif
5149#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01005150 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01005151#endif
Brett Warrene0edc842021-08-17 09:53:13 +01005152 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005153};
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005154
Ronald Crone68ab4f2022-10-05 12:46:29 +02005155#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08005156/* Function for checking `ssl_preset_*_sig_algs` and `ssl_tls12_preset_*_sig_algs`
Jerry Yu370e1462022-01-25 10:36:53 +08005157 * to make sure there are no duplicated signature algorithm entries. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005158MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01005159static int ssl_check_no_sig_alg_duplication(uint16_t *sig_algs)
Jerry Yu1a8b4812022-01-20 17:56:50 +08005160{
5161 size_t i, j;
5162 int ret = 0;
Jerry Yu909df7b2022-01-22 11:56:27 +08005163
Gilles Peskine449bd832023-01-11 14:50:10 +01005164 for (i = 0; sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++) {
5165 for (j = 0; j < i; j++) {
5166 if (sig_algs[i] != sig_algs[j]) {
Jerry Yuf377d642022-01-25 10:43:59 +08005167 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01005168 }
5169 mbedtls_printf(" entry(%04x,%" MBEDTLS_PRINTF_SIZET
5170 ") is duplicated at %" MBEDTLS_PRINTF_SIZET "\n",
5171 sig_algs[i], j, i);
Jerry Yuf377d642022-01-25 10:43:59 +08005172 ret = -1;
Jerry Yu1a8b4812022-01-20 17:56:50 +08005173 }
5174 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005175 return ret;
Jerry Yu1a8b4812022-01-20 17:56:50 +08005176}
5177
Ronald Crone68ab4f2022-10-05 12:46:29 +02005178#endif /* MBEDTLS_DEBUG_C && MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yu1a8b4812022-01-20 17:56:50 +08005179
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005180/*
Tillmann Karras588ad502015-09-25 04:27:22 +02005181 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005182 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005183int mbedtls_ssl_config_defaults(mbedtls_ssl_config *conf,
5184 int endpoint, int transport, int preset)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005185{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005186#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Janos Follath865b3eb2019-12-16 11:46:15 +00005187 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005188#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005189
Ronald Crone68ab4f2022-10-05 12:46:29 +02005190#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01005191 if (ssl_check_no_sig_alg_duplication(ssl_preset_suiteb_sig_algs)) {
5192 mbedtls_printf("ssl_preset_suiteb_sig_algs has duplicated entries\n");
5193 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu1a8b4812022-01-20 17:56:50 +08005194 }
5195
Gilles Peskine449bd832023-01-11 14:50:10 +01005196 if (ssl_check_no_sig_alg_duplication(ssl_preset_default_sig_algs)) {
5197 mbedtls_printf("ssl_preset_default_sig_algs has duplicated entries\n");
5198 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu1a8b4812022-01-20 17:56:50 +08005199 }
Jerry Yu909df7b2022-01-22 11:56:27 +08005200
5201#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01005202 if (ssl_check_no_sig_alg_duplication(ssl_tls12_preset_suiteb_sig_algs)) {
5203 mbedtls_printf("ssl_tls12_preset_suiteb_sig_algs has duplicated entries\n");
5204 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu909df7b2022-01-22 11:56:27 +08005205 }
5206
Gilles Peskine449bd832023-01-11 14:50:10 +01005207 if (ssl_check_no_sig_alg_duplication(ssl_tls12_preset_default_sig_algs)) {
5208 mbedtls_printf("ssl_tls12_preset_default_sig_algs has duplicated entries\n");
5209 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu909df7b2022-01-22 11:56:27 +08005210 }
5211#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Ronald Crone68ab4f2022-10-05 12:46:29 +02005212#endif /* MBEDTLS_DEBUG_C && MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yu1a8b4812022-01-20 17:56:50 +08005213
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02005214 /* Use the functions here so that they are covered in tests,
5215 * but otherwise access member directly for efficiency */
Gilles Peskine449bd832023-01-11 14:50:10 +01005216 mbedtls_ssl_conf_endpoint(conf, endpoint);
5217 mbedtls_ssl_conf_transport(conf, transport);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005218
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005219 /*
5220 * Things that are common to all presets
5221 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02005222#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005223 if (endpoint == MBEDTLS_SSL_IS_CLIENT) {
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02005224 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
5225#if defined(MBEDTLS_SSL_SESSION_TICKETS)
5226 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
5227#endif
5228 }
5229#endif
5230
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005231#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
5232 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
5233#endif
5234
5235#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
5236 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
5237#endif
5238
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02005239#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005240 conf->f_cookie_write = ssl_cookie_write_dummy;
5241 conf->f_cookie_check = ssl_cookie_check_dummy;
5242#endif
5243
5244#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
5245 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
5246#endif
5247
Janos Follath088ce432017-04-10 12:42:31 +01005248#if defined(MBEDTLS_SSL_SRV_C)
5249 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
TRodziewicz3946f792021-06-14 12:11:18 +02005250 conf->respect_cli_pref = MBEDTLS_SSL_SRV_CIPHERSUITE_ORDER_SERVER;
Janos Follath088ce432017-04-10 12:42:31 +01005251#endif
5252
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005253#if defined(MBEDTLS_SSL_PROTO_DTLS)
5254 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
5255 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
5256#endif
5257
5258#if defined(MBEDTLS_SSL_RENEGOTIATION)
5259 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005260 memset(conf->renego_period, 0x00, 2);
5261 memset(conf->renego_period + 2, 0xFF, 6);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005262#endif
5263
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005264#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005265 if (endpoint == MBEDTLS_SSL_IS_SERVER) {
Hanno Beckere2defad2021-07-24 05:59:17 +01005266 const unsigned char dhm_p[] =
5267 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
5268 const unsigned char dhm_g[] =
5269 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
Hanno Becker00d0a682017-10-04 13:14:29 +01005270
Gilles Peskine449bd832023-01-11 14:50:10 +01005271 if ((ret = mbedtls_ssl_conf_dh_param_bin(conf,
5272 dhm_p, sizeof(dhm_p),
5273 dhm_g, sizeof(dhm_g))) != 0) {
5274 return ret;
Hanno Beckere2defad2021-07-24 05:59:17 +01005275 }
5276 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02005277#endif
5278
Ronald Cron6f135e12021-12-08 16:57:54 +01005279#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu6ee56aa2022-12-06 17:47:22 +08005280
5281#if defined(MBEDTLS_SSL_EARLY_DATA)
Gilles Peskine449bd832023-01-11 14:50:10 +01005282 mbedtls_ssl_tls13_conf_early_data(conf, MBEDTLS_SSL_EARLY_DATA_DISABLED);
Jerry Yu6ee56aa2022-12-06 17:47:22 +08005283#if defined(MBEDTLS_SSL_SRV_C)
5284 mbedtls_ssl_tls13_conf_max_early_data_size(
Gilles Peskine449bd832023-01-11 14:50:10 +01005285 conf, MBEDTLS_SSL_MAX_EARLY_DATA_SIZE);
Jerry Yu6ee56aa2022-12-06 17:47:22 +08005286#endif
5287#endif /* MBEDTLS_SSL_EARLY_DATA */
5288
Jerry Yud0766ec2022-09-22 10:46:57 +08005289#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08005290 mbedtls_ssl_conf_new_session_tickets(
Gilles Peskine449bd832023-01-11 14:50:10 +01005291 conf, MBEDTLS_SSL_TLS1_3_DEFAULT_NEW_SESSION_TICKETS);
Jerry Yu1ad7ace2022-08-09 13:28:39 +08005292#endif
Hanno Becker71f1ed62021-07-24 06:01:47 +01005293 /*
5294 * Allow all TLS 1.3 key exchange modes by default.
5295 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00005296 conf->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Ronald Cron6f135e12021-12-08 16:57:54 +01005297#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01005298
Gilles Peskine449bd832023-01-11 14:50:10 +01005299 if (transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Glenn Strauss2dfcea22022-03-14 17:26:42 -04005300#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
XiaokangQian4d3a6042022-04-21 13:46:17 +00005301 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian060d8672022-04-21 09:24:56 +00005302 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian4d3a6042022-04-21 13:46:17 +00005303#else
Gilles Peskine449bd832023-01-11 14:50:10 +01005304 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQian060d8672022-04-21 09:24:56 +00005305#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005306 } else {
XiaokangQian4d3a6042022-04-21 13:46:17 +00005307#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01005308 if (endpoint == MBEDTLS_SSL_IS_CLIENT) {
XiaokangQian4d3a6042022-04-21 13:46:17 +00005309 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5310 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
Gilles Peskine449bd832023-01-11 14:50:10 +01005311 } else {
5312 /* Hybrid TLS 1.2 / 1.3 is not supported on server side yet */
XiaokangQian4d3a6042022-04-21 13:46:17 +00005313 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5314 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5315 }
5316#elif defined(MBEDTLS_SSL_PROTO_TLS1_3)
5317 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
5318 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
5319#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
5320 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5321 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5322#else
Gilles Peskine449bd832023-01-11 14:50:10 +01005323 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQian4d3a6042022-04-21 13:46:17 +00005324#endif
5325 }
Glenn Strauss2dfcea22022-03-14 17:26:42 -04005326
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005327 /*
5328 * Preset-specific defaults
5329 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005330 switch (preset) {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005331 /*
5332 * NSA Suite B
5333 */
5334 case MBEDTLS_SSL_PRESET_SUITEB:
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005335
Hanno Beckerd60b6c62021-04-29 12:04:11 +01005336 conf->ciphersuite_list = ssl_preset_suiteb_ciphersuites;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005337
5338#if defined(MBEDTLS_X509_CRT_PARSE_C)
5339 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005340#endif
5341
Ronald Crone68ab4f2022-10-05 12:46:29 +02005342#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08005343#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01005344 if (mbedtls_ssl_conf_is_tls12_only(conf)) {
Jerry Yu909df7b2022-01-22 11:56:27 +08005345 conf->sig_algs = ssl_tls12_preset_suiteb_sig_algs;
Gilles Peskine449bd832023-01-11 14:50:10 +01005346 } else
Jerry Yu909df7b2022-01-22 11:56:27 +08005347#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005348 conf->sig_algs = ssl_preset_suiteb_sig_algs;
Ronald Crone68ab4f2022-10-05 12:46:29 +02005349#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005350
Brett Warrene0edc842021-08-17 09:53:13 +01005351#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
5352 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005353#endif
Brett Warrene0edc842021-08-17 09:53:13 +01005354 conf->group_list = ssl_preset_suiteb_groups;
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02005355 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005356
5357 /*
5358 * Default
5359 */
5360 default:
Ronald Cronf6606552022-03-15 11:23:25 +01005361
Hanno Beckerd60b6c62021-04-29 12:04:11 +01005362 conf->ciphersuite_list = mbedtls_ssl_list_ciphersuites();
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005363
5364#if defined(MBEDTLS_X509_CRT_PARSE_C)
5365 conf->cert_profile = &mbedtls_x509_crt_profile_default;
5366#endif
5367
Ronald Crone68ab4f2022-10-05 12:46:29 +02005368#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08005369#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01005370 if (mbedtls_ssl_conf_is_tls12_only(conf)) {
Jerry Yu909df7b2022-01-22 11:56:27 +08005371 conf->sig_algs = ssl_tls12_preset_default_sig_algs;
Gilles Peskine449bd832023-01-11 14:50:10 +01005372 } else
Jerry Yu909df7b2022-01-22 11:56:27 +08005373#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005374 conf->sig_algs = ssl_preset_default_sig_algs;
Ronald Crone68ab4f2022-10-05 12:46:29 +02005375#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005376
Brett Warrene0edc842021-08-17 09:53:13 +01005377#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
5378 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005379#endif
Brett Warrene0edc842021-08-17 09:53:13 +01005380 conf->group_list = ssl_preset_default_groups;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005381
5382#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
5383 conf->dhm_min_bitlen = 1024;
5384#endif
5385 }
5386
Gilles Peskine449bd832023-01-11 14:50:10 +01005387 return 0;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005388}
5389
5390/*
5391 * Free mbedtls_ssl_config
5392 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005393void mbedtls_ssl_config_free(mbedtls_ssl_config *conf)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005394{
5395#if defined(MBEDTLS_DHM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005396 mbedtls_mpi_free(&conf->dhm_P);
5397 mbedtls_mpi_free(&conf->dhm_G);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005398#endif
5399
Ronald Cron73fe8df2022-10-05 14:31:43 +02005400#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02005401#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01005402 if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) {
Neil Armstrong501c9322022-05-03 09:35:09 +02005403 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
5404 }
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005405#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01005406 if (conf->psk != NULL) {
5407 mbedtls_platform_zeroize(conf->psk, conf->psk_len);
5408 mbedtls_free(conf->psk);
Azim Khan27e8a122018-03-21 14:24:11 +00005409 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005410 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09005411 }
5412
Gilles Peskine449bd832023-01-11 14:50:10 +01005413 if (conf->psk_identity != NULL) {
5414 mbedtls_platform_zeroize(conf->psk_identity, conf->psk_identity_len);
5415 mbedtls_free(conf->psk_identity);
Azim Khan27e8a122018-03-21 14:24:11 +00005416 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005417 conf->psk_identity_len = 0;
5418 }
Ronald Cron73fe8df2022-10-05 14:31:43 +02005419#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005420
5421#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005422 ssl_key_cert_free(conf->key_cert);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005423#endif
5424
Gilles Peskine449bd832023-01-11 14:50:10 +01005425 mbedtls_platform_zeroize(conf, sizeof(mbedtls_ssl_config));
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005426}
5427
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02005428#if defined(MBEDTLS_PK_C) && \
Valerio Setti1ad9ef22023-02-22 12:38:07 +01005429 (defined(MBEDTLS_RSA_C) || defined(MBEDTLS_PK_CAN_ECDSA_SOME))
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005430/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005431 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005432 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005433unsigned char mbedtls_ssl_sig_from_pk(mbedtls_pk_context *pk)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005434{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005435#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005436 if (mbedtls_pk_can_do(pk, MBEDTLS_PK_RSA)) {
5437 return MBEDTLS_SSL_SIG_RSA;
5438 }
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005439#endif
Valerio Setti1ad9ef22023-02-22 12:38:07 +01005440#if defined(MBEDTLS_PK_CAN_ECDSA_SOME)
Gilles Peskine449bd832023-01-11 14:50:10 +01005441 if (mbedtls_pk_can_do(pk, MBEDTLS_PK_ECDSA)) {
5442 return MBEDTLS_SSL_SIG_ECDSA;
5443 }
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005444#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005445 return MBEDTLS_SSL_SIG_ANON;
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005446}
5447
Gilles Peskine449bd832023-01-11 14:50:10 +01005448unsigned char mbedtls_ssl_sig_from_pk_alg(mbedtls_pk_type_t type)
Hanno Becker7e5437a2017-04-28 17:15:26 +01005449{
Gilles Peskine449bd832023-01-11 14:50:10 +01005450 switch (type) {
Hanno Becker7e5437a2017-04-28 17:15:26 +01005451 case MBEDTLS_PK_RSA:
Gilles Peskine449bd832023-01-11 14:50:10 +01005452 return MBEDTLS_SSL_SIG_RSA;
Hanno Becker7e5437a2017-04-28 17:15:26 +01005453 case MBEDTLS_PK_ECDSA:
5454 case MBEDTLS_PK_ECKEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01005455 return MBEDTLS_SSL_SIG_ECDSA;
Hanno Becker7e5437a2017-04-28 17:15:26 +01005456 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005457 return MBEDTLS_SSL_SIG_ANON;
Hanno Becker7e5437a2017-04-28 17:15:26 +01005458 }
5459}
5460
Gilles Peskine449bd832023-01-11 14:50:10 +01005461mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig(unsigned char sig)
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005462{
Gilles Peskine449bd832023-01-11 14:50:10 +01005463 switch (sig) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005464#if defined(MBEDTLS_RSA_C)
5465 case MBEDTLS_SSL_SIG_RSA:
Gilles Peskine449bd832023-01-11 14:50:10 +01005466 return MBEDTLS_PK_RSA;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005467#endif
Valerio Setti1ad9ef22023-02-22 12:38:07 +01005468#if defined(MBEDTLS_PK_CAN_ECDSA_SOME)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005469 case MBEDTLS_SSL_SIG_ECDSA:
Gilles Peskine449bd832023-01-11 14:50:10 +01005470 return MBEDTLS_PK_ECDSA;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005471#endif
5472 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005473 return MBEDTLS_PK_NONE;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005474 }
5475}
Valerio Setti1ad9ef22023-02-22 12:38:07 +01005476#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_PK_CAN_ECDSA_SOME ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005477
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02005478/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005479 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02005480 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005481mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash(unsigned char hash)
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005482{
Gilles Peskine449bd832023-01-11 14:50:10 +01005483 switch (hash) {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005484#if defined(MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005485 case MBEDTLS_SSL_HASH_MD5:
Gilles Peskine449bd832023-01-11 14:50:10 +01005486 return MBEDTLS_MD_MD5;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005487#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005488#if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005489 case MBEDTLS_SSL_HASH_SHA1:
Gilles Peskine449bd832023-01-11 14:50:10 +01005490 return MBEDTLS_MD_SHA1;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005491#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005492#if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005493 case MBEDTLS_SSL_HASH_SHA224:
Gilles Peskine449bd832023-01-11 14:50:10 +01005494 return MBEDTLS_MD_SHA224;
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02005495#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005496#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005497 case MBEDTLS_SSL_HASH_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01005498 return MBEDTLS_MD_SHA256;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005499#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005500#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005501 case MBEDTLS_SSL_HASH_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01005502 return MBEDTLS_MD_SHA384;
Mateusz Starzyk3352a532021-04-06 14:28:22 +02005503#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005504#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005505 case MBEDTLS_SSL_HASH_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01005506 return MBEDTLS_MD_SHA512;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005507#endif
5508 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005509 return MBEDTLS_MD_NONE;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005510 }
5511}
5512
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005513/*
5514 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
5515 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005516unsigned char mbedtls_ssl_hash_from_md_alg(int md)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005517{
Gilles Peskine449bd832023-01-11 14:50:10 +01005518 switch (md) {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005519#if defined(MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005520 case MBEDTLS_MD_MD5:
Gilles Peskine449bd832023-01-11 14:50:10 +01005521 return MBEDTLS_SSL_HASH_MD5;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005522#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005523#if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005524 case MBEDTLS_MD_SHA1:
Gilles Peskine449bd832023-01-11 14:50:10 +01005525 return MBEDTLS_SSL_HASH_SHA1;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005526#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005527#if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005528 case MBEDTLS_MD_SHA224:
Gilles Peskine449bd832023-01-11 14:50:10 +01005529 return MBEDTLS_SSL_HASH_SHA224;
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02005530#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005531#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005532 case MBEDTLS_MD_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01005533 return MBEDTLS_SSL_HASH_SHA256;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005534#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005535#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005536 case MBEDTLS_MD_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01005537 return MBEDTLS_SSL_HASH_SHA384;
Mateusz Starzyk3352a532021-04-06 14:28:22 +02005538#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005539#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005540 case MBEDTLS_MD_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01005541 return MBEDTLS_SSL_HASH_SHA512;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005542#endif
5543 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005544 return MBEDTLS_SSL_HASH_NONE;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005545 }
5546}
5547
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005548/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005549 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005550 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005551 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005552int mbedtls_ssl_check_curve_tls_id(const mbedtls_ssl_context *ssl, uint16_t tls_id)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005553{
Gilles Peskine449bd832023-01-11 14:50:10 +01005554 const uint16_t *group_list = mbedtls_ssl_get_groups(ssl);
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005555
Gilles Peskine449bd832023-01-11 14:50:10 +01005556 if (group_list == NULL) {
5557 return -1;
Brett Warrene0edc842021-08-17 09:53:13 +01005558 }
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005559
Gilles Peskine449bd832023-01-11 14:50:10 +01005560 for (; *group_list != 0; group_list++) {
5561 if (*group_list == tls_id) {
5562 return 0;
5563 }
5564 }
5565
5566 return -1;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005567}
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005568
5569#if defined(MBEDTLS_ECP_C)
5570/*
5571 * Same as mbedtls_ssl_check_curve_tls_id() but with a mbedtls_ecp_group_id.
5572 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005573int mbedtls_ssl_check_curve(const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id)
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005574{
Gilles Peskine449bd832023-01-11 14:50:10 +01005575 uint16_t tls_id = mbedtls_ssl_get_tls_id_from_ecp_group_id(grp_id);
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005576
Gilles Peskine449bd832023-01-11 14:50:10 +01005577 if (tls_id == 0) {
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005578 return -1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005579 }
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005580
Gilles Peskine449bd832023-01-11 14:50:10 +01005581 return mbedtls_ssl_check_curve_tls_id(ssl, tls_id);
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005582}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02005583#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005584
Gilles Peskine449bd832023-01-11 14:50:10 +01005585#if defined(MBEDTLS_DEBUG_C)
Valerio Setti67419f02023-01-04 16:12:42 +01005586#define EC_NAME(_name_) _name_
5587#else
5588#define EC_NAME(_name_) NULL
5589#endif
5590
Valerio Setti18c9fed2022-12-30 17:44:24 +01005591static const struct {
5592 uint16_t tls_id;
5593 mbedtls_ecp_group_id ecp_group_id;
5594 psa_ecc_family_t psa_family;
5595 uint16_t bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01005596 const char *name;
Valerio Setti18c9fed2022-12-30 17:44:24 +01005597} tls_id_match_table[] =
5598{
5599#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_521)
Gilles Peskine449bd832023-01-11 14:50:10 +01005600 { 25, MBEDTLS_ECP_DP_SECP521R1, PSA_ECC_FAMILY_SECP_R1, 521, EC_NAME("secp521r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005601#endif
5602#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED) || defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512)
Gilles Peskine449bd832023-01-11 14:50:10 +01005603 { 28, MBEDTLS_ECP_DP_BP512R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 512, EC_NAME("brainpoolP512r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005604#endif
Valerio Setti67419f02023-01-04 16:12:42 +01005605#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_384)
Gilles Peskine449bd832023-01-11 14:50:10 +01005606 { 24, MBEDTLS_ECP_DP_SECP384R1, PSA_ECC_FAMILY_SECP_R1, 384, EC_NAME("secp384r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005607#endif
Valerio Setti67419f02023-01-04 16:12:42 +01005608#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED) || defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384)
Gilles Peskine449bd832023-01-11 14:50:10 +01005609 { 27, MBEDTLS_ECP_DP_BP384R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 384, EC_NAME("brainpoolP384r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005610#endif
Valerio Setti67419f02023-01-04 16:12:42 +01005611#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_256)
Gilles Peskine449bd832023-01-11 14:50:10 +01005612 { 23, MBEDTLS_ECP_DP_SECP256R1, PSA_ECC_FAMILY_SECP_R1, 256, EC_NAME("secp256r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005613#endif
5614#if defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED) || defined(PSA_WANT_ECC_SECP_K1_256)
Gilles Peskine449bd832023-01-11 14:50:10 +01005615 { 22, MBEDTLS_ECP_DP_SECP256K1, PSA_ECC_FAMILY_SECP_K1, 256, EC_NAME("secp256k1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005616#endif
Valerio Setti67419f02023-01-04 16:12:42 +01005617#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED) || defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256)
Gilles Peskine449bd832023-01-11 14:50:10 +01005618 { 26, MBEDTLS_ECP_DP_BP256R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 256, EC_NAME("brainpoolP256r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005619#endif
5620#if defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_224)
Gilles Peskine449bd832023-01-11 14:50:10 +01005621 { 21, MBEDTLS_ECP_DP_SECP224R1, PSA_ECC_FAMILY_SECP_R1, 224, EC_NAME("secp224r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005622#endif
5623#if defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED) || defined(PSA_WANT_ECC_SECP_K1_224)
Gilles Peskine449bd832023-01-11 14:50:10 +01005624 { 20, MBEDTLS_ECP_DP_SECP224K1, PSA_ECC_FAMILY_SECP_K1, 224, EC_NAME("secp224k1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005625#endif
5626#if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_192)
Gilles Peskine449bd832023-01-11 14:50:10 +01005627 { 19, MBEDTLS_ECP_DP_SECP192R1, PSA_ECC_FAMILY_SECP_R1, 192, EC_NAME("secp192r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005628#endif
5629#if defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED) || defined(PSA_WANT_ECC_SECP_K1_192)
Gilles Peskine449bd832023-01-11 14:50:10 +01005630 { 18, MBEDTLS_ECP_DP_SECP192K1, PSA_ECC_FAMILY_SECP_K1, 192, EC_NAME("secp192k1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005631#endif
5632#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED) || defined(PSA_WANT_ECC_MONTGOMERY_255)
Gilles Peskine449bd832023-01-11 14:50:10 +01005633 { 29, MBEDTLS_ECP_DP_CURVE25519, PSA_ECC_FAMILY_MONTGOMERY, 255, EC_NAME("x25519") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005634#endif
5635#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED) || defined(PSA_WANT_ECC_MONTGOMERY_448)
Gilles Peskine449bd832023-01-11 14:50:10 +01005636 { 30, MBEDTLS_ECP_DP_CURVE448, PSA_ECC_FAMILY_MONTGOMERY, 448, EC_NAME("x448") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005637#endif
5638 { 0, MBEDTLS_ECP_DP_NONE, 0, 0, NULL },
5639};
5640
Gilles Peskine449bd832023-01-11 14:50:10 +01005641int mbedtls_ssl_get_psa_curve_info_from_tls_id(uint16_t tls_id,
5642 psa_ecc_family_t *family,
5643 size_t *bits)
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) {
5647 if (family != NULL) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01005648 *family = tls_id_match_table[i].psa_family;
Gilles Peskine449bd832023-01-11 14:50:10 +01005649 }
5650 if (bits != NULL) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01005651 *bits = tls_id_match_table[i].bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01005652 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01005653 return PSA_SUCCESS;
5654 }
5655 }
5656
5657 return PSA_ERROR_NOT_SUPPORTED;
5658}
5659
Gilles Peskine449bd832023-01-11 14:50:10 +01005660mbedtls_ecp_group_id mbedtls_ssl_get_ecp_group_id_from_tls_id(uint16_t tls_id)
Valerio Setti18c9fed2022-12-30 17:44:24 +01005661{
Gilles Peskine449bd832023-01-11 14:50:10 +01005662 for (int i = 0; tls_id_match_table[i].tls_id != 0; i++) {
5663 if (tls_id_match_table[i].tls_id == tls_id) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01005664 return tls_id_match_table[i].ecp_group_id;
Gilles Peskine449bd832023-01-11 14:50:10 +01005665 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01005666 }
5667
5668 return MBEDTLS_ECP_DP_NONE;
5669}
5670
Gilles Peskine449bd832023-01-11 14:50:10 +01005671uint16_t mbedtls_ssl_get_tls_id_from_ecp_group_id(mbedtls_ecp_group_id grp_id)
Valerio Setti18c9fed2022-12-30 17:44:24 +01005672{
Gilles Peskine449bd832023-01-11 14:50:10 +01005673 for (int i = 0; tls_id_match_table[i].ecp_group_id != MBEDTLS_ECP_DP_NONE;
5674 i++) {
5675 if (tls_id_match_table[i].ecp_group_id == grp_id) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01005676 return tls_id_match_table[i].tls_id;
Gilles Peskine449bd832023-01-11 14:50:10 +01005677 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01005678 }
5679
5680 return 0;
5681}
5682
Valerio Setti67419f02023-01-04 16:12:42 +01005683#if defined(MBEDTLS_DEBUG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005684const char *mbedtls_ssl_get_curve_name_from_tls_id(uint16_t tls_id)
Valerio Setti18c9fed2022-12-30 17:44:24 +01005685{
Gilles Peskine449bd832023-01-11 14:50:10 +01005686 for (int i = 0; tls_id_match_table[i].tls_id != 0; i++) {
5687 if (tls_id_match_table[i].tls_id == tls_id) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01005688 return tls_id_match_table[i].name;
Gilles Peskine449bd832023-01-11 14:50:10 +01005689 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01005690 }
5691
5692 return NULL;
5693}
Valerio Setti67419f02023-01-04 16:12:42 +01005694#endif
Valerio Setti18c9fed2022-12-30 17:44:24 +01005695
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005696#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005697int mbedtls_ssl_check_cert_usage(const mbedtls_x509_crt *cert,
5698 const mbedtls_ssl_ciphersuite_t *ciphersuite,
5699 int cert_endpoint,
5700 uint32_t *flags)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005701{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005702 int ret = 0;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005703 int usage = 0;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005704 const char *ext_oid;
5705 size_t ext_len;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005706
Gilles Peskine449bd832023-01-11 14:50:10 +01005707 if (cert_endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005708 /* Server part of the key exchange */
Gilles Peskine449bd832023-01-11 14:50:10 +01005709 switch (ciphersuite->key_exchange) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005710 case MBEDTLS_KEY_EXCHANGE_RSA:
5711 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005712 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005713 break;
5714
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005715 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
5716 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
5717 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
5718 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005719 break;
5720
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005721 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
5722 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005723 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005724 break;
5725
5726 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005727 case MBEDTLS_KEY_EXCHANGE_NONE:
5728 case MBEDTLS_KEY_EXCHANGE_PSK:
5729 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
5730 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02005731 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005732 usage = 0;
5733 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005734 } else {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005735 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
5736 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005737 }
5738
Gilles Peskine449bd832023-01-11 14:50:10 +01005739 if (mbedtls_x509_crt_check_key_usage(cert, usage) != 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005740 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005741 ret = -1;
5742 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005743
Gilles Peskine449bd832023-01-11 14:50:10 +01005744 if (cert_endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005745 ext_oid = MBEDTLS_OID_SERVER_AUTH;
Gilles Peskine449bd832023-01-11 14:50:10 +01005746 ext_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_SERVER_AUTH);
5747 } else {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005748 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
Gilles Peskine449bd832023-01-11 14:50:10 +01005749 ext_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_CLIENT_AUTH);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005750 }
5751
Gilles Peskine449bd832023-01-11 14:50:10 +01005752 if (mbedtls_x509_crt_check_extended_key_usage(cert, ext_oid, ext_len) != 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005753 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005754 ret = -1;
5755 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005756
Gilles Peskine449bd832023-01-11 14:50:10 +01005757 return ret;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005758}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005759#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02005760
Jerry Yu148165c2021-09-24 23:20:59 +08005761#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01005762int mbedtls_ssl_get_handshake_transcript(mbedtls_ssl_context *ssl,
5763 const mbedtls_md_type_t md,
5764 unsigned char *dst,
5765 size_t dst_len,
5766 size_t *olen)
Jerry Yu148165c2021-09-24 23:20:59 +08005767{
Ronald Cronf6893e12022-01-07 22:09:01 +01005768 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5769 psa_hash_operation_t *hash_operation_to_clone;
5770 psa_hash_operation_t hash_operation = psa_hash_operation_init();
5771
Jerry Yu148165c2021-09-24 23:20:59 +08005772 *olen = 0;
Ronald Cronf6893e12022-01-07 22:09:01 +01005773
Gilles Peskine449bd832023-01-11 14:50:10 +01005774 switch (md) {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005775#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01005776 case MBEDTLS_MD_SHA384:
5777 hash_operation_to_clone = &ssl->handshake->fin_sha384_psa;
5778 break;
Ronald Cronf6893e12022-01-07 22:09:01 +01005779#endif
5780
Andrzej Kurek25f27152022-08-17 16:09:31 -04005781#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01005782 case MBEDTLS_MD_SHA256:
5783 hash_operation_to_clone = &ssl->handshake->fin_sha256_psa;
5784 break;
Ronald Cronf6893e12022-01-07 22:09:01 +01005785#endif
5786
Gilles Peskine449bd832023-01-11 14:50:10 +01005787 default:
5788 goto exit;
5789 }
5790
5791 status = psa_hash_clone(hash_operation_to_clone, &hash_operation);
5792 if (status != PSA_SUCCESS) {
Ronald Cronf6893e12022-01-07 22:09:01 +01005793 goto exit;
5794 }
5795
Gilles Peskine449bd832023-01-11 14:50:10 +01005796 status = psa_hash_finish(&hash_operation, dst, dst_len, olen);
5797 if (status != PSA_SUCCESS) {
Ronald Cronf6893e12022-01-07 22:09:01 +01005798 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005799 }
Ronald Cronf6893e12022-01-07 22:09:01 +01005800
5801exit:
Andrzej Kurekeabeb302022-10-17 07:52:51 -04005802#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
5803 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
5804 (void) ssl;
5805#endif
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05005806 return PSA_TO_MBEDTLS_ERR(status);
Jerry Yu148165c2021-09-24 23:20:59 +08005807}
5808#else /* MBEDTLS_USE_PSA_CRYPTO */
5809
Andrzej Kurek25f27152022-08-17 16:09:31 -04005810#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005811MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01005812static int ssl_get_handshake_transcript_sha384(mbedtls_ssl_context *ssl,
5813 unsigned char *dst,
5814 size_t dst_len,
5815 size_t *olen)
Jerry Yu24c0ec32021-09-09 14:21:07 +08005816{
Jerry Yu24c0ec32021-09-09 14:21:07 +08005817 int ret;
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01005818 mbedtls_md_context_t sha384;
Jerry Yu24c0ec32021-09-09 14:21:07 +08005819
Gilles Peskine449bd832023-01-11 14:50:10 +01005820 if (dst_len < 48) {
5821 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
5822 }
Jerry Yu24c0ec32021-09-09 14:21:07 +08005823
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01005824 mbedtls_md_init(&sha384);
5825 ret = mbedtls_md_setup(&sha384, mbedtls_md_info_from_type(MBEDTLS_MD_SHA384), 0);
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01005826 if (ret != 0) {
5827 goto exit;
5828 }
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01005829 ret = mbedtls_md_clone(&sha384, &ssl->handshake->fin_sha384);
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01005830 if (ret != 0) {
5831 goto exit;
5832 }
Jerry Yu24c0ec32021-09-09 14:21:07 +08005833
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01005834 if ((ret = mbedtls_md_finish(&sha384, dst)) != 0) {
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01005835 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_finish", ret);
Jerry Yu24c0ec32021-09-09 14:21:07 +08005836 goto exit;
5837 }
5838
5839 *olen = 48;
5840
5841exit:
5842
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01005843 mbedtls_md_free(&sha384);
Gilles Peskine449bd832023-01-11 14:50:10 +01005844 return ret;
Jerry Yu24c0ec32021-09-09 14:21:07 +08005845}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005846#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005847
Andrzej Kurek25f27152022-08-17 16:09:31 -04005848#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005849MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01005850static int ssl_get_handshake_transcript_sha256(mbedtls_ssl_context *ssl,
5851 unsigned char *dst,
5852 size_t dst_len,
5853 size_t *olen)
Jerry Yu24c0ec32021-09-09 14:21:07 +08005854{
Jerry Yu24c0ec32021-09-09 14:21:07 +08005855 int ret;
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01005856 mbedtls_md_context_t sha256;
Jerry Yu24c0ec32021-09-09 14:21:07 +08005857
Gilles Peskine449bd832023-01-11 14:50:10 +01005858 if (dst_len < 32) {
5859 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
5860 }
Jerry Yu24c0ec32021-09-09 14:21:07 +08005861
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01005862 mbedtls_md_init(&sha256);
5863 ret = mbedtls_md_setup(&sha256, mbedtls_md_info_from_type(MBEDTLS_MD_SHA256), 0);
5864 if (ret != 0) {
5865 goto exit;
5866 }
5867 ret = mbedtls_md_clone(&sha256, &ssl->handshake->fin_sha256);
5868 if (ret != 0) {
5869 goto exit;
5870 }
Jerry Yuc5aef882021-12-23 20:15:02 +08005871
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01005872 if ((ret = mbedtls_md_finish(&sha256, dst)) != 0) {
5873 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_finish", ret);
Jerry Yu24c0ec32021-09-09 14:21:07 +08005874 goto exit;
5875 }
5876
5877 *olen = 32;
5878
5879exit:
5880
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01005881 mbedtls_md_free(&sha256);
Gilles Peskine449bd832023-01-11 14:50:10 +01005882 return ret;
Jerry Yu24c0ec32021-09-09 14:21:07 +08005883}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005884#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005885
Gilles Peskine449bd832023-01-11 14:50:10 +01005886int mbedtls_ssl_get_handshake_transcript(mbedtls_ssl_context *ssl,
5887 const mbedtls_md_type_t md,
5888 unsigned char *dst,
5889 size_t dst_len,
5890 size_t *olen)
Jerry Yu24c0ec32021-09-09 14:21:07 +08005891{
Gilles Peskine449bd832023-01-11 14:50:10 +01005892 switch (md) {
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005893
Andrzej Kurek25f27152022-08-17 16:09:31 -04005894#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01005895 case MBEDTLS_MD_SHA384:
5896 return ssl_get_handshake_transcript_sha384(ssl, dst, dst_len, olen);
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005897#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005898
Andrzej Kurek25f27152022-08-17 16:09:31 -04005899#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01005900 case MBEDTLS_MD_SHA256:
5901 return ssl_get_handshake_transcript_sha256(ssl, dst, dst_len, olen);
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005902#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005903
Gilles Peskine449bd832023-01-11 14:50:10 +01005904 default:
Andrzej Kurek409248a2022-10-24 10:33:21 -04005905#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01005906 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
5907 (void) ssl;
5908 (void) dst;
5909 (void) dst_len;
5910 (void) olen;
Andrzej Kurek409248a2022-10-24 10:33:21 -04005911#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005912 break;
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005913 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005914 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu24c0ec32021-09-09 14:21:07 +08005915}
XiaokangQian647719a2021-12-07 09:16:29 +00005916
Jerry Yu148165c2021-09-24 23:20:59 +08005917#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005918
Ronald Crone68ab4f2022-10-05 12:46:29 +02005919#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Gabor Mezei078e8032022-04-27 21:17:56 +02005920/* mbedtls_ssl_parse_sig_alg_ext()
5921 *
5922 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
5923 * value (TLS 1.3 RFC8446):
5924 * enum {
5925 * ....
5926 * ecdsa_secp256r1_sha256( 0x0403 ),
5927 * ecdsa_secp384r1_sha384( 0x0503 ),
5928 * ecdsa_secp521r1_sha512( 0x0603 ),
5929 * ....
5930 * } SignatureScheme;
5931 *
5932 * struct {
5933 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
5934 * } SignatureSchemeList;
5935 *
5936 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
5937 * value (TLS 1.2 RFC5246):
5938 * enum {
5939 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
5940 * sha512(6), (255)
5941 * } HashAlgorithm;
5942 *
5943 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
5944 * SignatureAlgorithm;
5945 *
5946 * struct {
5947 * HashAlgorithm hash;
5948 * SignatureAlgorithm signature;
5949 * } SignatureAndHashAlgorithm;
5950 *
5951 * SignatureAndHashAlgorithm
5952 * supported_signature_algorithms<2..2^16-2>;
5953 *
5954 * The TLS 1.3 signature algorithm extension was defined to be a compatible
5955 * generalization of the TLS 1.2 signature algorithm extension.
5956 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
5957 * `SignatureScheme` field of TLS 1.3
5958 *
5959 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005960int mbedtls_ssl_parse_sig_alg_ext(mbedtls_ssl_context *ssl,
5961 const unsigned char *buf,
5962 const unsigned char *end)
Gabor Mezei078e8032022-04-27 21:17:56 +02005963{
5964 const unsigned char *p = buf;
5965 size_t supported_sig_algs_len = 0;
5966 const unsigned char *supported_sig_algs_end;
5967 uint16_t sig_alg;
5968 uint32_t common_idx = 0;
5969
Gilles Peskine449bd832023-01-11 14:50:10 +01005970 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
5971 supported_sig_algs_len = MBEDTLS_GET_UINT16_BE(p, 0);
Gabor Mezei078e8032022-04-27 21:17:56 +02005972 p += 2;
5973
Gilles Peskine449bd832023-01-11 14:50:10 +01005974 memset(ssl->handshake->received_sig_algs, 0,
5975 sizeof(ssl->handshake->received_sig_algs));
Gabor Mezei078e8032022-04-27 21:17:56 +02005976
Gilles Peskine449bd832023-01-11 14:50:10 +01005977 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, supported_sig_algs_len);
Gabor Mezei078e8032022-04-27 21:17:56 +02005978 supported_sig_algs_end = p + supported_sig_algs_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01005979 while (p < supported_sig_algs_end) {
5980 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, supported_sig_algs_end, 2);
5981 sig_alg = MBEDTLS_GET_UINT16_BE(p, 0);
Gabor Mezei078e8032022-04-27 21:17:56 +02005982 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01005983 MBEDTLS_SSL_DEBUG_MSG(4, ("received signature algorithm: 0x%x %s",
5984 sig_alg,
5985 mbedtls_ssl_sig_alg_to_str(sig_alg)));
Jerry Yu2fe6c632022-06-29 10:02:38 +08005986#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01005987 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 &&
5988 (!(mbedtls_ssl_sig_alg_is_supported(ssl, sig_alg) &&
5989 mbedtls_ssl_sig_alg_is_offered(ssl, sig_alg)))) {
Gabor Mezei078e8032022-04-27 21:17:56 +02005990 continue;
Jerry Yu2fe6c632022-06-29 10:02:38 +08005991 }
5992#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gabor Mezei078e8032022-04-27 21:17:56 +02005993
Gilles Peskine449bd832023-01-11 14:50:10 +01005994 MBEDTLS_SSL_DEBUG_MSG(4, ("valid signature algorithm: %s",
5995 mbedtls_ssl_sig_alg_to_str(sig_alg)));
Gabor Mezei078e8032022-04-27 21:17:56 +02005996
Gilles Peskine449bd832023-01-11 14:50:10 +01005997 if (common_idx + 1 < MBEDTLS_RECEIVED_SIG_ALGS_SIZE) {
Gabor Mezei078e8032022-04-27 21:17:56 +02005998 ssl->handshake->received_sig_algs[common_idx] = sig_alg;
5999 common_idx += 1;
6000 }
6001 }
6002 /* Check that we consumed all the message. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006003 if (p != end) {
6004 MBEDTLS_SSL_DEBUG_MSG(1,
6005 ("Signature algorithms extension length misaligned"));
6006 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
6007 MBEDTLS_ERR_SSL_DECODE_ERROR);
6008 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Gabor Mezei078e8032022-04-27 21:17:56 +02006009 }
6010
Gilles Peskine449bd832023-01-11 14:50:10 +01006011 if (common_idx == 0) {
6012 MBEDTLS_SSL_DEBUG_MSG(3, ("no signature algorithm in common"));
6013 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
6014 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
6015 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Gabor Mezei078e8032022-04-27 21:17:56 +02006016 }
6017
Gabor Mezei15b95a62022-05-09 16:37:58 +02006018 ssl->handshake->received_sig_algs[common_idx] = MBEDTLS_TLS_SIG_NONE;
Gilles Peskine449bd832023-01-11 14:50:10 +01006019 return 0;
Gabor Mezei078e8032022-04-27 21:17:56 +02006020}
6021
Ronald Crone68ab4f2022-10-05 12:46:29 +02006022#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Gabor Mezei078e8032022-04-27 21:17:56 +02006023
Jerry Yudc7bd172022-02-17 13:44:15 +08006024#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6025
6026#if defined(MBEDTLS_USE_PSA_CRYPTO)
6027
Gilles Peskine449bd832023-01-11 14:50:10 +01006028static psa_status_t setup_psa_key_derivation(psa_key_derivation_operation_t *derivation,
6029 mbedtls_svc_key_id_t key,
6030 psa_algorithm_t alg,
6031 const unsigned char *raw_psk, size_t raw_psk_length,
6032 const unsigned char *seed, size_t seed_length,
6033 const unsigned char *label, size_t label_length,
6034 const unsigned char *other_secret,
6035 size_t other_secret_length,
6036 size_t capacity)
Jerry Yudc7bd172022-02-17 13:44:15 +08006037{
6038 psa_status_t status;
6039
Gilles Peskine449bd832023-01-11 14:50:10 +01006040 status = psa_key_derivation_setup(derivation, alg);
6041 if (status != PSA_SUCCESS) {
6042 return status;
6043 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006044
Gilles Peskine449bd832023-01-11 14:50:10 +01006045 if (PSA_ALG_IS_TLS12_PRF(alg) || PSA_ALG_IS_TLS12_PSK_TO_MS(alg)) {
6046 status = psa_key_derivation_input_bytes(derivation,
6047 PSA_KEY_DERIVATION_INPUT_SEED,
6048 seed, seed_length);
6049 if (status != PSA_SUCCESS) {
6050 return status;
Przemek Stekiel1f027032022-04-05 17:12:11 +02006051 }
6052
Gilles Peskine449bd832023-01-11 14:50:10 +01006053 if (other_secret != NULL) {
6054 status = psa_key_derivation_input_bytes(derivation,
6055 PSA_KEY_DERIVATION_INPUT_OTHER_SECRET,
6056 other_secret, other_secret_length);
6057 if (status != PSA_SUCCESS) {
6058 return status;
6059 }
6060 }
6061
6062 if (mbedtls_svc_key_id_is_null(key)) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006063 status = psa_key_derivation_input_bytes(
6064 derivation, PSA_KEY_DERIVATION_INPUT_SECRET,
Gilles Peskine449bd832023-01-11 14:50:10 +01006065 raw_psk, raw_psk_length);
6066 } else {
Jerry Yudc7bd172022-02-17 13:44:15 +08006067 status = psa_key_derivation_input_key(
Gilles Peskine449bd832023-01-11 14:50:10 +01006068 derivation, PSA_KEY_DERIVATION_INPUT_SECRET, key);
Jerry Yudc7bd172022-02-17 13:44:15 +08006069 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006070 if (status != PSA_SUCCESS) {
6071 return status;
6072 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006073
Gilles Peskine449bd832023-01-11 14:50:10 +01006074 status = psa_key_derivation_input_bytes(derivation,
6075 PSA_KEY_DERIVATION_INPUT_LABEL,
6076 label, label_length);
6077 if (status != PSA_SUCCESS) {
6078 return status;
6079 }
6080 } else {
6081 return PSA_ERROR_NOT_SUPPORTED;
Jerry Yudc7bd172022-02-17 13:44:15 +08006082 }
6083
Gilles Peskine449bd832023-01-11 14:50:10 +01006084 status = psa_key_derivation_set_capacity(derivation, capacity);
6085 if (status != PSA_SUCCESS) {
6086 return status;
6087 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006088
Gilles Peskine449bd832023-01-11 14:50:10 +01006089 return PSA_SUCCESS;
Jerry Yudc7bd172022-02-17 13:44:15 +08006090}
6091
Andrzej Kurek57d10632022-10-24 10:32:01 -04006092#if defined(PSA_WANT_ALG_SHA_384) || \
6093 defined(PSA_WANT_ALG_SHA_256)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006094MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006095static int tls_prf_generic(mbedtls_md_type_t md_type,
6096 const unsigned char *secret, size_t slen,
6097 const char *label,
6098 const unsigned char *random, size_t rlen,
6099 unsigned char *dstbuf, size_t dlen)
Jerry Yudc7bd172022-02-17 13:44:15 +08006100{
6101 psa_status_t status;
6102 psa_algorithm_t alg;
6103 mbedtls_svc_key_id_t master_key = MBEDTLS_SVC_KEY_ID_INIT;
6104 psa_key_derivation_operation_t derivation =
6105 PSA_KEY_DERIVATION_OPERATION_INIT;
6106
Gilles Peskine449bd832023-01-11 14:50:10 +01006107 if (md_type == MBEDTLS_MD_SHA384) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006108 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
Gilles Peskine449bd832023-01-11 14:50:10 +01006109 } else {
Jerry Yudc7bd172022-02-17 13:44:15 +08006110 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
Gilles Peskine449bd832023-01-11 14:50:10 +01006111 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006112
6113 /* Normally a "secret" should be long enough to be impossible to
6114 * find by brute force, and in particular should not be empty. But
6115 * this PRF is also used to derive an IV, in particular in EAP-TLS,
6116 * and for this use case it makes sense to have a 0-length "secret".
6117 * Since the key API doesn't allow importing a key of length 0,
6118 * keep master_key=0, which setup_psa_key_derivation() understands
6119 * to mean a 0-length "secret" input. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006120 if (slen != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006121 psa_key_attributes_t key_attributes = psa_key_attributes_init();
Gilles Peskine449bd832023-01-11 14:50:10 +01006122 psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE);
6123 psa_set_key_algorithm(&key_attributes, alg);
6124 psa_set_key_type(&key_attributes, PSA_KEY_TYPE_DERIVE);
Jerry Yudc7bd172022-02-17 13:44:15 +08006125
Gilles Peskine449bd832023-01-11 14:50:10 +01006126 status = psa_import_key(&key_attributes, secret, slen, &master_key);
6127 if (status != PSA_SUCCESS) {
6128 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
6129 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006130 }
6131
Gilles Peskine449bd832023-01-11 14:50:10 +01006132 status = setup_psa_key_derivation(&derivation,
6133 master_key, alg,
6134 NULL, 0,
6135 random, rlen,
6136 (unsigned char const *) label,
6137 (size_t) strlen(label),
6138 NULL, 0,
6139 dlen);
6140 if (status != PSA_SUCCESS) {
6141 psa_key_derivation_abort(&derivation);
6142 psa_destroy_key(master_key);
6143 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yudc7bd172022-02-17 13:44:15 +08006144 }
6145
Gilles Peskine449bd832023-01-11 14:50:10 +01006146 status = psa_key_derivation_output_bytes(&derivation, dstbuf, dlen);
6147 if (status != PSA_SUCCESS) {
6148 psa_key_derivation_abort(&derivation);
6149 psa_destroy_key(master_key);
6150 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yudc7bd172022-02-17 13:44:15 +08006151 }
6152
Gilles Peskine449bd832023-01-11 14:50:10 +01006153 status = psa_key_derivation_abort(&derivation);
6154 if (status != PSA_SUCCESS) {
6155 psa_destroy_key(master_key);
6156 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yudc7bd172022-02-17 13:44:15 +08006157 }
6158
Gilles Peskine449bd832023-01-11 14:50:10 +01006159 if (!mbedtls_svc_key_id_is_null(master_key)) {
6160 status = psa_destroy_key(master_key);
6161 }
6162 if (status != PSA_SUCCESS) {
6163 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
6164 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006165
Gilles Peskine449bd832023-01-11 14:50:10 +01006166 return 0;
Jerry Yudc7bd172022-02-17 13:44:15 +08006167}
Andrzej Kurek57d10632022-10-24 10:32:01 -04006168#endif /* PSA_WANT_ALG_SHA_256 || PSA_WANT_ALG_SHA_384 */
Jerry Yudc7bd172022-02-17 13:44:15 +08006169#else /* MBEDTLS_USE_PSA_CRYPTO */
6170
Andrzej Kurek57d10632022-10-24 10:32:01 -04006171#if defined(MBEDTLS_MD_C) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01006172 (defined(MBEDTLS_SHA256_C) || \
6173 defined(MBEDTLS_SHA384_C))
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006174MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006175static int tls_prf_generic(mbedtls_md_type_t md_type,
6176 const unsigned char *secret, size_t slen,
6177 const char *label,
6178 const unsigned char *random, size_t rlen,
6179 unsigned char *dstbuf, size_t dlen)
Jerry Yudc7bd172022-02-17 13:44:15 +08006180{
6181 size_t nb;
6182 size_t i, j, k, md_len;
6183 unsigned char *tmp;
6184 size_t tmp_len = 0;
6185 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
6186 const mbedtls_md_info_t *md_info;
6187 mbedtls_md_context_t md_ctx;
6188 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6189
Gilles Peskine449bd832023-01-11 14:50:10 +01006190 mbedtls_md_init(&md_ctx);
Jerry Yudc7bd172022-02-17 13:44:15 +08006191
Gilles Peskine449bd832023-01-11 14:50:10 +01006192 if ((md_info = mbedtls_md_info_from_type(md_type)) == NULL) {
6193 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
6194 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006195
Gilles Peskine449bd832023-01-11 14:50:10 +01006196 md_len = mbedtls_md_get_size(md_info);
Jerry Yudc7bd172022-02-17 13:44:15 +08006197
Gilles Peskine449bd832023-01-11 14:50:10 +01006198 tmp_len = md_len + strlen(label) + rlen;
6199 tmp = mbedtls_calloc(1, tmp_len);
6200 if (tmp == NULL) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006201 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
6202 goto exit;
6203 }
6204
Gilles Peskine449bd832023-01-11 14:50:10 +01006205 nb = strlen(label);
6206 memcpy(tmp + md_len, label, nb);
6207 memcpy(tmp + md_len + nb, random, rlen);
Jerry Yudc7bd172022-02-17 13:44:15 +08006208 nb += rlen;
6209
6210 /*
6211 * Compute P_<hash>(secret, label + random)[0..dlen]
6212 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006213 if ((ret = mbedtls_md_setup(&md_ctx, md_info, 1)) != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006214 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006215 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006216
Gilles Peskine449bd832023-01-11 14:50:10 +01006217 ret = mbedtls_md_hmac_starts(&md_ctx, secret, slen);
6218 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006219 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006220 }
6221 ret = mbedtls_md_hmac_update(&md_ctx, tmp + md_len, nb);
6222 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006223 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006224 }
6225 ret = mbedtls_md_hmac_finish(&md_ctx, tmp);
6226 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006227 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006228 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006229
Gilles Peskine449bd832023-01-11 14:50:10 +01006230 for (i = 0; i < dlen; i += md_len) {
6231 ret = mbedtls_md_hmac_reset(&md_ctx);
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_update(&md_ctx, tmp, md_len + nb);
6236 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006237 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006238 }
6239 ret = mbedtls_md_hmac_finish(&md_ctx, h_i);
6240 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006241 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006242 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006243
Gilles Peskine449bd832023-01-11 14:50:10 +01006244 ret = mbedtls_md_hmac_reset(&md_ctx);
6245 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006246 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006247 }
6248 ret = mbedtls_md_hmac_update(&md_ctx, tmp, md_len);
6249 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006250 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006251 }
6252 ret = mbedtls_md_hmac_finish(&md_ctx, tmp);
6253 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006254 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006255 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006256
Gilles Peskine449bd832023-01-11 14:50:10 +01006257 k = (i + md_len > dlen) ? dlen % md_len : md_len;
Jerry Yudc7bd172022-02-17 13:44:15 +08006258
Gilles Peskine449bd832023-01-11 14:50:10 +01006259 for (j = 0; j < k; j++) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006260 dstbuf[i + j] = h_i[j];
Gilles Peskine449bd832023-01-11 14:50:10 +01006261 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006262 }
6263
6264exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006265 mbedtls_md_free(&md_ctx);
Jerry Yudc7bd172022-02-17 13:44:15 +08006266
Gilles Peskine449bd832023-01-11 14:50:10 +01006267 if (tmp != NULL) {
6268 mbedtls_platform_zeroize(tmp, tmp_len);
6269 }
Dave Rodgman29b9b2b2022-11-01 16:08:14 +00006270
Gilles Peskine449bd832023-01-11 14:50:10 +01006271 mbedtls_platform_zeroize(h_i, sizeof(h_i));
Jerry Yudc7bd172022-02-17 13:44:15 +08006272
Gilles Peskine449bd832023-01-11 14:50:10 +01006273 mbedtls_free(tmp);
Jerry Yudc7bd172022-02-17 13:44:15 +08006274
Gilles Peskine449bd832023-01-11 14:50:10 +01006275 return ret;
Jerry Yudc7bd172022-02-17 13:44:15 +08006276}
Andrzej Kurek57d10632022-10-24 10:32:01 -04006277#endif /* MBEDTLS_MD_C && ( MBEDTLS_SHA256_C || MBEDTLS_SHA384_C ) */
Jerry Yudc7bd172022-02-17 13:44:15 +08006278#endif /* MBEDTLS_USE_PSA_CRYPTO */
6279
Andrzej Kurek25f27152022-08-17 16:09:31 -04006280#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006281MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006282static int tls_prf_sha256(const unsigned char *secret, size_t slen,
6283 const char *label,
6284 const unsigned char *random, size_t rlen,
6285 unsigned char *dstbuf, size_t dlen)
Jerry Yudc7bd172022-02-17 13:44:15 +08006286{
Gilles Peskine449bd832023-01-11 14:50:10 +01006287 return tls_prf_generic(MBEDTLS_MD_SHA256, secret, slen,
6288 label, random, rlen, dstbuf, dlen);
Jerry Yudc7bd172022-02-17 13:44:15 +08006289}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04006290#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudc7bd172022-02-17 13:44:15 +08006291
Andrzej Kurek25f27152022-08-17 16:09:31 -04006292#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006293MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006294static int tls_prf_sha384(const unsigned char *secret, size_t slen,
6295 const char *label,
6296 const unsigned char *random, size_t rlen,
6297 unsigned char *dstbuf, size_t dlen)
Jerry Yudc7bd172022-02-17 13:44:15 +08006298{
Gilles Peskine449bd832023-01-11 14:50:10 +01006299 return tls_prf_generic(MBEDTLS_MD_SHA384, secret, slen,
6300 label, random, rlen, dstbuf, dlen);
Jerry Yudc7bd172022-02-17 13:44:15 +08006301}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04006302#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudc7bd172022-02-17 13:44:15 +08006303
Jerry Yuf009d862022-02-17 14:01:37 +08006304/*
6305 * Set appropriate PRF function and other SSL / TLS1.2 functions
6306 *
6307 * Inputs:
Jerry Yuf009d862022-02-17 14:01:37 +08006308 * - hash associated with the ciphersuite (only used by TLS 1.2)
6309 *
6310 * Outputs:
6311 * - the tls_prf, calc_verify and calc_finished members of handshake structure
6312 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006313MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006314static int ssl_set_handshake_prfs(mbedtls_ssl_handshake_params *handshake,
6315 mbedtls_md_type_t hash)
Jerry Yuf009d862022-02-17 14:01:37 +08006316{
Andrzej Kurek25f27152022-08-17 16:09:31 -04006317#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01006318 if (hash == MBEDTLS_MD_SHA384) {
Jerry Yuf009d862022-02-17 14:01:37 +08006319 handshake->tls_prf = tls_prf_sha384;
6320 handshake->calc_verify = ssl_calc_verify_tls_sha384;
6321 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Gilles Peskine449bd832023-01-11 14:50:10 +01006322 } else
Jerry Yuf009d862022-02-17 14:01:37 +08006323#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04006324#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuf009d862022-02-17 14:01:37 +08006325 {
Ronald Cron81591aa2022-03-07 09:05:51 +01006326 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08006327 handshake->tls_prf = tls_prf_sha256;
6328 handshake->calc_verify = ssl_calc_verify_tls_sha256;
6329 handshake->calc_finished = ssl_calc_finished_tls_sha256;
6330 }
Ronald Cron81591aa2022-03-07 09:05:51 +01006331#else
Jerry Yuf009d862022-02-17 14:01:37 +08006332 {
Jerry Yuf009d862022-02-17 14:01:37 +08006333 (void) handshake;
Ronald Cron81591aa2022-03-07 09:05:51 +01006334 (void) hash;
Gilles Peskine449bd832023-01-11 14:50:10 +01006335 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yuf009d862022-02-17 14:01:37 +08006336 }
Ronald Cron81591aa2022-03-07 09:05:51 +01006337#endif
Jerry Yuf009d862022-02-17 14:01:37 +08006338
Gilles Peskine449bd832023-01-11 14:50:10 +01006339 return 0;
Jerry Yuf009d862022-02-17 14:01:37 +08006340}
Jerry Yud6ab2352022-02-17 14:03:43 +08006341
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006342/*
6343 * Compute master secret if needed
6344 *
6345 * Parameters:
6346 * [in/out] handshake
6347 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
6348 * (PSA-PSK) ciphersuite_info, psk_opaque
6349 * [out] premaster (cleared)
6350 * [out] master
6351 * [in] ssl: optionally used for debugging, EMS and PSA-PSK
6352 * debug: conf->f_dbg, conf->p_dbg
6353 * EMS: passed to calc_verify (debug + session_negotiate)
Ronald Crona25cf582022-03-07 11:10:36 +01006354 * PSA-PSA: conf
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006355 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006356MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006357static int ssl_compute_master(mbedtls_ssl_handshake_params *handshake,
6358 unsigned char *master,
6359 const mbedtls_ssl_context *ssl)
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006360{
6361 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6362
6363 /* cf. RFC 5246, Section 8.1:
6364 * "The master secret is always exactly 48 bytes in length." */
6365 size_t const master_secret_len = 48;
6366
6367#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
6368 unsigned char session_hash[48];
6369#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
6370
6371 /* The label for the KDF used for key expansion.
6372 * This is either "master secret" or "extended master secret"
6373 * depending on whether the Extended Master Secret extension
6374 * is used. */
6375 char const *lbl = "master secret";
6376
Przemek Stekielae4ed302022-04-05 17:15:55 +02006377 /* The seed for the KDF used for key expansion.
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006378 * - If the Extended Master Secret extension is not used,
6379 * this is ClientHello.Random + ServerHello.Random
6380 * (see Sect. 8.1 in RFC 5246).
6381 * - If the Extended Master Secret extension is used,
6382 * this is the transcript of the handshake so far.
6383 * (see Sect. 4 in RFC 7627). */
Przemek Stekielae4ed302022-04-05 17:15:55 +02006384 unsigned char const *seed = handshake->randbytes;
6385 size_t seed_len = 64;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006386
6387#if !defined(MBEDTLS_DEBUG_C) && \
6388 !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \
6389 !(defined(MBEDTLS_USE_PSA_CRYPTO) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01006390 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED))
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006391 ssl = NULL; /* make sure we don't use it except for those cases */
6392 (void) ssl;
6393#endif
6394
Gilles Peskine449bd832023-01-11 14:50:10 +01006395 if (handshake->resume != 0) {
6396 MBEDTLS_SSL_DEBUG_MSG(3, ("no premaster (session resumed)"));
6397 return 0;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006398 }
6399
6400#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Gilles Peskine449bd832023-01-11 14:50:10 +01006401 if (handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED) {
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006402 lbl = "extended master secret";
Przemek Stekielae4ed302022-04-05 17:15:55 +02006403 seed = session_hash;
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01006404 ret = handshake->calc_verify(ssl, session_hash, &seed_len);
6405 if (ret != 0) {
6406 MBEDTLS_SSL_DEBUG_RET(1, "calc_verify", ret);
6407 }
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006408
Gilles Peskine449bd832023-01-11 14:50:10 +01006409 MBEDTLS_SSL_DEBUG_BUF(3, "session hash for extended master secret",
6410 session_hash, seed_len);
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006411 }
Przemek Stekiel169bf0b2022-04-29 07:53:29 +02006412#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006413
Przemek Stekiel99114f32022-04-22 11:20:09 +02006414#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Przemek Stekiel8a4b7fd2022-04-28 09:22:22 +02006415 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006416 if (mbedtls_ssl_ciphersuite_uses_psk(handshake->ciphersuite_info) == 1) {
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006417 /* Perform PSK-to-MS expansion in a single step. */
6418 psa_status_t status;
6419 psa_algorithm_t alg;
6420 mbedtls_svc_key_id_t psk;
6421 psa_key_derivation_operation_t derivation =
6422 PSA_KEY_DERIVATION_OPERATION_INIT;
6423 mbedtls_md_type_t hash_alg = handshake->ciphersuite_info->mac;
6424
Gilles Peskine449bd832023-01-11 14:50:10 +01006425 MBEDTLS_SSL_DEBUG_MSG(2, ("perform PSA-based PSK-to-MS expansion"));
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006426
Gilles Peskine449bd832023-01-11 14:50:10 +01006427 psk = mbedtls_ssl_get_opaque_psk(ssl);
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006428
Gilles Peskine449bd832023-01-11 14:50:10 +01006429 if (hash_alg == MBEDTLS_MD_SHA384) {
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006430 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
Gilles Peskine449bd832023-01-11 14:50:10 +01006431 } else {
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006432 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
Gilles Peskine449bd832023-01-11 14:50:10 +01006433 }
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006434
Przemek Stekiel51a1f362022-04-13 08:57:06 +02006435 size_t other_secret_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006436 unsigned char *other_secret = NULL;
Przemek Stekielc2033402022-04-05 17:19:41 +02006437
Gilles Peskine449bd832023-01-11 14:50:10 +01006438 switch (handshake->ciphersuite_info->key_exchange) {
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006439 /* Provide other secret.
Przemek Stekiel51a1f362022-04-13 08:57:06 +02006440 * Other secret is stored in premaster, where first 2 bytes hold the
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006441 * length of the other key.
Przemek Stekielc2033402022-04-05 17:19:41 +02006442 */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006443 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Przemek Stekiel8abcee92022-04-28 09:16:28 +02006444 /* For RSA-PSK other key length is always 48 bytes. */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006445 other_secret_len = 48;
6446 other_secret = handshake->premaster + 2;
6447 break;
6448 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006449 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006450 other_secret_len = MBEDTLS_GET_UINT16_BE(handshake->premaster, 0);
6451 other_secret = handshake->premaster + 2;
6452 break;
6453 default:
6454 break;
Przemek Stekielc2033402022-04-05 17:19:41 +02006455 }
6456
Gilles Peskine449bd832023-01-11 14:50:10 +01006457 status = setup_psa_key_derivation(&derivation, psk, alg,
6458 ssl->conf->psk, ssl->conf->psk_len,
6459 seed, seed_len,
6460 (unsigned char const *) lbl,
6461 (size_t) strlen(lbl),
6462 other_secret, other_secret_len,
6463 master_secret_len);
6464 if (status != PSA_SUCCESS) {
6465 psa_key_derivation_abort(&derivation);
6466 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006467 }
6468
Gilles Peskine449bd832023-01-11 14:50:10 +01006469 status = psa_key_derivation_output_bytes(&derivation,
6470 master,
6471 master_secret_len);
6472 if (status != PSA_SUCCESS) {
6473 psa_key_derivation_abort(&derivation);
6474 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006475 }
6476
Gilles Peskine449bd832023-01-11 14:50:10 +01006477 status = psa_key_derivation_abort(&derivation);
6478 if (status != PSA_SUCCESS) {
6479 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
6480 }
6481 } else
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006482#endif
6483 {
Neil Armstrongca7d5062022-05-31 14:43:23 +02006484#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01006485 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
6486 if (handshake->ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE) {
Neil Armstrongca7d5062022-05-31 14:43:23 +02006487 psa_status_t status;
6488 psa_algorithm_t alg = PSA_ALG_TLS12_ECJPAKE_TO_PMS;
6489 psa_key_derivation_operation_t derivation =
6490 PSA_KEY_DERIVATION_OPERATION_INIT;
6491
Gilles Peskine449bd832023-01-11 14:50:10 +01006492 MBEDTLS_SSL_DEBUG_MSG(2, ("perform PSA-based PMS KDF for ECJPAKE"));
Neil Armstrongca7d5062022-05-31 14:43:23 +02006493
6494 handshake->pmslen = PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE;
6495
Gilles Peskine449bd832023-01-11 14:50:10 +01006496 status = psa_key_derivation_setup(&derivation, alg);
6497 if (status != PSA_SUCCESS) {
6498 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Neil Armstrongca7d5062022-05-31 14:43:23 +02006499 }
6500
Gilles Peskine449bd832023-01-11 14:50:10 +01006501 status = psa_key_derivation_set_capacity(&derivation,
6502 PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE);
6503 if (status != PSA_SUCCESS) {
6504 psa_key_derivation_abort(&derivation);
6505 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Neil Armstrongca7d5062022-05-31 14:43:23 +02006506 }
6507
Gilles Peskine449bd832023-01-11 14:50:10 +01006508 status = psa_pake_get_implicit_key(&handshake->psa_pake_ctx,
6509 &derivation);
6510 if (status != PSA_SUCCESS) {
6511 psa_key_derivation_abort(&derivation);
6512 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Neil Armstrongca7d5062022-05-31 14:43:23 +02006513 }
6514
Gilles Peskine449bd832023-01-11 14:50:10 +01006515 status = psa_key_derivation_output_bytes(&derivation,
6516 handshake->premaster,
6517 handshake->pmslen);
6518 if (status != PSA_SUCCESS) {
6519 psa_key_derivation_abort(&derivation);
6520 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
6521 }
6522
6523 status = psa_key_derivation_abort(&derivation);
6524 if (status != PSA_SUCCESS) {
6525 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Neil Armstrongca7d5062022-05-31 14:43:23 +02006526 }
6527 }
6528#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01006529 ret = handshake->tls_prf(handshake->premaster, handshake->pmslen,
6530 lbl, seed, seed_len,
6531 master,
6532 master_secret_len);
6533 if (ret != 0) {
6534 MBEDTLS_SSL_DEBUG_RET(1, "prf", ret);
6535 return ret;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006536 }
6537
Gilles Peskine449bd832023-01-11 14:50:10 +01006538 MBEDTLS_SSL_DEBUG_BUF(3, "premaster secret",
6539 handshake->premaster,
6540 handshake->pmslen);
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006541
Gilles Peskine449bd832023-01-11 14:50:10 +01006542 mbedtls_platform_zeroize(handshake->premaster,
6543 sizeof(handshake->premaster));
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006544 }
6545
Gilles Peskine449bd832023-01-11 14:50:10 +01006546 return 0;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006547}
6548
Gilles Peskine449bd832023-01-11 14:50:10 +01006549int mbedtls_ssl_derive_keys(mbedtls_ssl_context *ssl)
Jerry Yud62f87e2022-02-17 14:09:02 +08006550{
6551 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6552 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
6553 ssl->handshake->ciphersuite_info;
6554
Gilles Peskine449bd832023-01-11 14:50:10 +01006555 MBEDTLS_SSL_DEBUG_MSG(2, ("=> derive keys"));
Jerry Yud62f87e2022-02-17 14:09:02 +08006556
6557 /* Set PRF, calc_verify and calc_finished function pointers */
Gilles Peskine449bd832023-01-11 14:50:10 +01006558 ret = ssl_set_handshake_prfs(ssl->handshake,
6559 ciphersuite_info->mac);
6560 if (ret != 0) {
6561 MBEDTLS_SSL_DEBUG_RET(1, "ssl_set_handshake_prfs", ret);
6562 return ret;
Jerry Yud62f87e2022-02-17 14:09:02 +08006563 }
6564
6565 /* Compute master secret if needed */
Gilles Peskine449bd832023-01-11 14:50:10 +01006566 ret = ssl_compute_master(ssl->handshake,
6567 ssl->session_negotiate->master,
6568 ssl);
6569 if (ret != 0) {
6570 MBEDTLS_SSL_DEBUG_RET(1, "ssl_compute_master", ret);
6571 return ret;
Jerry Yud62f87e2022-02-17 14:09:02 +08006572 }
6573
6574 /* Swap the client and server random values:
6575 * - MS derivation wanted client+server (RFC 5246 8.1)
6576 * - key derivation wants server+client (RFC 5246 6.3) */
6577 {
6578 unsigned char tmp[64];
Gilles Peskine449bd832023-01-11 14:50:10 +01006579 memcpy(tmp, ssl->handshake->randbytes, 64);
6580 memcpy(ssl->handshake->randbytes, tmp + 32, 32);
6581 memcpy(ssl->handshake->randbytes + 32, tmp, 32);
6582 mbedtls_platform_zeroize(tmp, sizeof(tmp));
Jerry Yud62f87e2022-02-17 14:09:02 +08006583 }
6584
6585 /* Populate transform structure */
Gilles Peskine449bd832023-01-11 14:50:10 +01006586 ret = ssl_tls12_populate_transform(ssl->transform_negotiate,
6587 ssl->session_negotiate->ciphersuite,
6588 ssl->session_negotiate->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02006589#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01006590 ssl->session_negotiate->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02006591#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01006592 ssl->handshake->tls_prf,
6593 ssl->handshake->randbytes,
6594 ssl->tls_version,
6595 ssl->conf->endpoint,
6596 ssl);
6597 if (ret != 0) {
6598 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls12_populate_transform", ret);
6599 return ret;
Jerry Yud62f87e2022-02-17 14:09:02 +08006600 }
6601
6602 /* We no longer need Server/ClientHello.random values */
Gilles Peskine449bd832023-01-11 14:50:10 +01006603 mbedtls_platform_zeroize(ssl->handshake->randbytes,
6604 sizeof(ssl->handshake->randbytes));
Jerry Yud62f87e2022-02-17 14:09:02 +08006605
Gilles Peskine449bd832023-01-11 14:50:10 +01006606 MBEDTLS_SSL_DEBUG_MSG(2, ("<= derive keys"));
Jerry Yud62f87e2022-02-17 14:09:02 +08006607
Gilles Peskine449bd832023-01-11 14:50:10 +01006608 return 0;
Jerry Yud62f87e2022-02-17 14:09:02 +08006609}
Jerry Yu8392e0d2022-02-17 14:10:24 +08006610
Gilles Peskine449bd832023-01-11 14:50:10 +01006611int mbedtls_ssl_set_calc_verify_md(mbedtls_ssl_context *ssl, int md)
Ronald Cron4dcbca92022-03-07 10:21:40 +01006612{
Gilles Peskine449bd832023-01-11 14:50:10 +01006613 switch (md) {
Andrzej Kurek25f27152022-08-17 16:09:31 -04006614#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron4dcbca92022-03-07 10:21:40 +01006615 case MBEDTLS_SSL_HASH_SHA384:
6616 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
6617 break;
6618#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04006619#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron4dcbca92022-03-07 10:21:40 +01006620 case MBEDTLS_SSL_HASH_SHA256:
6621 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
6622 break;
6623#endif
6624 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006625 return -1;
Ronald Cron4dcbca92022-03-07 10:21:40 +01006626 }
Andrzej Kurekeabeb302022-10-17 07:52:51 -04006627#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
6628 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
6629 (void) ssl;
6630#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01006631 return 0;
Ronald Cron4dcbca92022-03-07 10:21:40 +01006632}
6633
Andrzej Kurek25f27152022-08-17 16:09:31 -04006634#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01006635int ssl_calc_verify_tls_sha256(const mbedtls_ssl_context *ssl,
6636 unsigned char *hash,
6637 size_t *hlen)
Jerry Yu8392e0d2022-02-17 14:10:24 +08006638{
6639#if defined(MBEDTLS_USE_PSA_CRYPTO)
6640 size_t hash_size;
6641 psa_status_t status;
6642 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
6643
Gilles Peskine449bd832023-01-11 14:50:10 +01006644 MBEDTLS_SSL_DEBUG_MSG(2, ("=> PSA calc verify sha256"));
6645 status = psa_hash_clone(&ssl->handshake->fin_sha256_psa, &sha256_psa);
6646 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006647 goto exit;
Jerry Yu8392e0d2022-02-17 14:10:24 +08006648 }
6649
Gilles Peskine449bd832023-01-11 14:50:10 +01006650 status = psa_hash_finish(&sha256_psa, hash, 32, &hash_size);
6651 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006652 goto exit;
Jerry Yu8392e0d2022-02-17 14:10:24 +08006653 }
6654
6655 *hlen = 32;
Gilles Peskine449bd832023-01-11 14:50:10 +01006656 MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated verify result", hash, *hlen);
6657 MBEDTLS_SSL_DEBUG_MSG(2, ("<= PSA calc verify"));
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006658
6659exit:
6660 psa_hash_abort(&sha256_psa);
Andrzej Kurekdaf5b562023-03-03 05:52:28 -05006661 return PSA_TO_MD_ERR(status);
Jerry Yu8392e0d2022-02-17 14:10:24 +08006662#else
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006663 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01006664 mbedtls_md_context_t sha256;
Jerry Yu8392e0d2022-02-17 14:10:24 +08006665
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01006666 mbedtls_md_init(&sha256);
Jerry Yu8392e0d2022-02-17 14:10:24 +08006667
Gilles Peskine449bd832023-01-11 14:50:10 +01006668 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc verify sha256"));
Jerry Yu8392e0d2022-02-17 14:10:24 +08006669
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01006670 ret = mbedtls_md_setup(&sha256, mbedtls_md_info_from_type(MBEDTLS_MD_SHA256), 0);
6671 if (ret != 0) {
6672 goto exit;
6673 }
6674 ret = mbedtls_md_clone(&sha256, &ssl->handshake->fin_sha256);
6675 if (ret != 0) {
6676 goto exit;
6677 }
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006678
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01006679 ret = mbedtls_md_finish(&sha256, hash);
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006680 if (ret != 0) {
6681 goto exit;
6682 }
Jerry Yu8392e0d2022-02-17 14:10:24 +08006683
6684 *hlen = 32;
6685
Gilles Peskine449bd832023-01-11 14:50:10 +01006686 MBEDTLS_SSL_DEBUG_BUF(3, "calculated verify result", hash, *hlen);
6687 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc verify"));
Jerry Yu8392e0d2022-02-17 14:10:24 +08006688
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006689exit:
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01006690 mbedtls_md_free(&sha256);
Manuel Pégourié-Gonnard8e176f72023-02-09 10:33:54 +01006691 return ret;
Jerry Yu8392e0d2022-02-17 14:10:24 +08006692#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu8392e0d2022-02-17 14:10:24 +08006693}
Andrzej Kurek25f27152022-08-17 16:09:31 -04006694#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu8392e0d2022-02-17 14:10:24 +08006695
Andrzej Kurek25f27152022-08-17 16:09:31 -04006696#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01006697int ssl_calc_verify_tls_sha384(const mbedtls_ssl_context *ssl,
6698 unsigned char *hash,
6699 size_t *hlen)
Jerry Yuc1cb3842022-02-17 14:13:48 +08006700{
6701#if defined(MBEDTLS_USE_PSA_CRYPTO)
6702 size_t hash_size;
6703 psa_status_t status;
6704 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
6705
Gilles Peskine449bd832023-01-11 14:50:10 +01006706 MBEDTLS_SSL_DEBUG_MSG(2, ("=> PSA calc verify sha384"));
6707 status = psa_hash_clone(&ssl->handshake->fin_sha384_psa, &sha384_psa);
6708 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006709 goto exit;
Jerry Yuc1cb3842022-02-17 14:13:48 +08006710 }
6711
Gilles Peskine449bd832023-01-11 14:50:10 +01006712 status = psa_hash_finish(&sha384_psa, hash, 48, &hash_size);
6713 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006714 goto exit;
Jerry Yuc1cb3842022-02-17 14:13:48 +08006715 }
6716
6717 *hlen = 48;
Gilles Peskine449bd832023-01-11 14:50:10 +01006718 MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated verify result", hash, *hlen);
6719 MBEDTLS_SSL_DEBUG_MSG(2, ("<= PSA calc verify"));
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006720
6721exit:
6722 psa_hash_abort(&sha384_psa);
Andrzej Kurekdaf5b562023-03-03 05:52:28 -05006723 return PSA_TO_MD_ERR(status);
Jerry Yuc1cb3842022-02-17 14:13:48 +08006724#else
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006725 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01006726 mbedtls_md_context_t sha384;
Jerry Yuc1cb3842022-02-17 14:13:48 +08006727
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01006728 mbedtls_md_init(&sha384);
Jerry Yuc1cb3842022-02-17 14:13:48 +08006729
Gilles Peskine449bd832023-01-11 14:50:10 +01006730 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc verify sha384"));
Jerry Yuc1cb3842022-02-17 14:13:48 +08006731
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01006732 ret = mbedtls_md_setup(&sha384, mbedtls_md_info_from_type(MBEDTLS_MD_SHA384), 0);
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01006733 if (ret != 0) {
6734 goto exit;
6735 }
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01006736 ret = mbedtls_md_clone(&sha384, &ssl->handshake->fin_sha384);
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01006737 if (ret != 0) {
6738 goto exit;
6739 }
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006740
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01006741 ret = mbedtls_md_finish(&sha384, hash);
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006742 if (ret != 0) {
6743 goto exit;
6744 }
Jerry Yuc1cb3842022-02-17 14:13:48 +08006745
6746 *hlen = 48;
6747
Gilles Peskine449bd832023-01-11 14:50:10 +01006748 MBEDTLS_SSL_DEBUG_BUF(3, "calculated verify result", hash, *hlen);
6749 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc verify"));
Jerry Yuc1cb3842022-02-17 14:13:48 +08006750
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006751exit:
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01006752 mbedtls_md_free(&sha384);
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006753 return ret;
Jerry Yuc1cb3842022-02-17 14:13:48 +08006754#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yuc1cb3842022-02-17 14:13:48 +08006755}
Andrzej Kurek25f27152022-08-17 16:09:31 -04006756#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yuc1cb3842022-02-17 14:13:48 +08006757
Neil Armstrong80f6f322022-05-03 17:56:38 +02006758#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
6759 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006760int mbedtls_ssl_psk_derive_premaster(mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex)
Jerry Yuce3dca42022-02-17 14:16:37 +08006761{
6762 unsigned char *p = ssl->handshake->premaster;
Gilles Peskine449bd832023-01-11 14:50:10 +01006763 unsigned char *end = p + sizeof(ssl->handshake->premaster);
Jerry Yuce3dca42022-02-17 14:16:37 +08006764 const unsigned char *psk = NULL;
6765 size_t psk_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006766 int psk_ret = mbedtls_ssl_get_psk(ssl, &psk, &psk_len);
Jerry Yuce3dca42022-02-17 14:16:37 +08006767
Gilles Peskine449bd832023-01-11 14:50:10 +01006768 if (psk_ret == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED) {
Jerry Yuce3dca42022-02-17 14:16:37 +08006769 /*
6770 * This should never happen because the existence of a PSK is always
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006771 * checked before calling this function.
6772 *
6773 * The exception is opaque DHE-PSK. For DHE-PSK fill premaster with
Przemek Stekiel8abcee92022-04-28 09:16:28 +02006774 * the shared secret without PSK.
Jerry Yuce3dca42022-02-17 14:16:37 +08006775 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006776 if (key_ex != MBEDTLS_KEY_EXCHANGE_DHE_PSK) {
6777 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
6778 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006779 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006780 }
6781
6782 /*
6783 * PMS = struct {
6784 * opaque other_secret<0..2^16-1>;
6785 * opaque psk<0..2^16-1>;
6786 * };
6787 * with "other_secret" depending on the particular key exchange
6788 */
6789#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006790 if (key_ex == MBEDTLS_KEY_EXCHANGE_PSK) {
6791 if (end - p < 2) {
6792 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6793 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006794
Gilles Peskine449bd832023-01-11 14:50:10 +01006795 MBEDTLS_PUT_UINT16_BE(psk_len, p, 0);
Jerry Yuce3dca42022-02-17 14:16:37 +08006796 p += 2;
6797
Gilles Peskine449bd832023-01-11 14:50:10 +01006798 if (end < p || (size_t) (end - p) < psk_len) {
6799 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6800 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006801
Gilles Peskine449bd832023-01-11 14:50:10 +01006802 memset(p, 0, psk_len);
Jerry Yuce3dca42022-02-17 14:16:37 +08006803 p += psk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01006804 } else
Jerry Yuce3dca42022-02-17 14:16:37 +08006805#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
6806#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006807 if (key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK) {
Jerry Yuce3dca42022-02-17 14:16:37 +08006808 /*
6809 * other_secret already set by the ClientKeyExchange message,
6810 * and is 48 bytes long
6811 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006812 if (end - p < 2) {
6813 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6814 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006815
6816 *p++ = 0;
6817 *p++ = 48;
6818 p += 48;
Gilles Peskine449bd832023-01-11 14:50:10 +01006819 } else
Jerry Yuce3dca42022-02-17 14:16:37 +08006820#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
6821#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006822 if (key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK) {
Jerry Yuce3dca42022-02-17 14:16:37 +08006823 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6824 size_t len;
6825
6826 /* Write length only when we know the actual value */
Gilles Peskine449bd832023-01-11 14:50:10 +01006827 if ((ret = mbedtls_dhm_calc_secret(&ssl->handshake->dhm_ctx,
6828 p + 2, end - (p + 2), &len,
6829 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
6830 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_calc_secret", ret);
6831 return ret;
Jerry Yuce3dca42022-02-17 14:16:37 +08006832 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006833 MBEDTLS_PUT_UINT16_BE(len, p, 0);
Jerry Yuce3dca42022-02-17 14:16:37 +08006834 p += 2 + len;
6835
Gilles Peskine449bd832023-01-11 14:50:10 +01006836 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: K ", &ssl->handshake->dhm_ctx.K);
6837 } else
Jerry Yuce3dca42022-02-17 14:16:37 +08006838#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Neil Armstrong80f6f322022-05-03 17:56:38 +02006839#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006840 if (key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK) {
Jerry Yuce3dca42022-02-17 14:16:37 +08006841 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6842 size_t zlen;
6843
Gilles Peskine449bd832023-01-11 14:50:10 +01006844 if ((ret = mbedtls_ecdh_calc_secret(&ssl->handshake->ecdh_ctx, &zlen,
6845 p + 2, end - (p + 2),
6846 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
6847 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecdh_calc_secret", ret);
6848 return ret;
Jerry Yuce3dca42022-02-17 14:16:37 +08006849 }
6850
Gilles Peskine449bd832023-01-11 14:50:10 +01006851 MBEDTLS_PUT_UINT16_BE(zlen, p, 0);
Jerry Yuce3dca42022-02-17 14:16:37 +08006852 p += 2 + zlen;
6853
Gilles Peskine449bd832023-01-11 14:50:10 +01006854 MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx,
6855 MBEDTLS_DEBUG_ECDH_Z);
6856 } else
Neil Armstrong80f6f322022-05-03 17:56:38 +02006857#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Jerry Yuce3dca42022-02-17 14:16:37 +08006858 {
Gilles Peskine449bd832023-01-11 14:50:10 +01006859 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
6860 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yuce3dca42022-02-17 14:16:37 +08006861 }
6862
6863 /* opaque psk<0..2^16-1>; */
Gilles Peskine449bd832023-01-11 14:50:10 +01006864 if (end - p < 2) {
6865 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6866 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006867
Gilles Peskine449bd832023-01-11 14:50:10 +01006868 MBEDTLS_PUT_UINT16_BE(psk_len, p, 0);
Jerry Yuce3dca42022-02-17 14:16:37 +08006869 p += 2;
6870
Gilles Peskine449bd832023-01-11 14:50:10 +01006871 if (end < p || (size_t) (end - p) < psk_len) {
6872 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6873 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006874
Gilles Peskine449bd832023-01-11 14:50:10 +01006875 memcpy(p, psk, psk_len);
Jerry Yuce3dca42022-02-17 14:16:37 +08006876 p += psk_len;
6877
6878 ssl->handshake->pmslen = p - ssl->handshake->premaster;
6879
Gilles Peskine449bd832023-01-11 14:50:10 +01006880 return 0;
Jerry Yuce3dca42022-02-17 14:16:37 +08006881}
Neil Armstrong80f6f322022-05-03 17:56:38 +02006882#endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Jerry Yuc2c673d2022-02-17 14:20:39 +08006883
6884#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006885MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006886static int ssl_write_hello_request(mbedtls_ssl_context *ssl);
Jerry Yuc2c673d2022-02-17 14:20:39 +08006887
6888#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006889int mbedtls_ssl_resend_hello_request(mbedtls_ssl_context *ssl)
Jerry Yuc2c673d2022-02-17 14:20:39 +08006890{
6891 /* If renegotiation is not enforced, retransmit until we would reach max
6892 * timeout if we were using the usual handshake doubling scheme */
Gilles Peskine449bd832023-01-11 14:50:10 +01006893 if (ssl->conf->renego_max_records < 0) {
Jerry Yuc2c673d2022-02-17 14:20:39 +08006894 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
6895 unsigned char doublings = 1;
6896
Gilles Peskine449bd832023-01-11 14:50:10 +01006897 while (ratio != 0) {
Jerry Yuc2c673d2022-02-17 14:20:39 +08006898 ++doublings;
6899 ratio >>= 1;
6900 }
6901
Gilles Peskine449bd832023-01-11 14:50:10 +01006902 if (++ssl->renego_records_seen > doublings) {
6903 MBEDTLS_SSL_DEBUG_MSG(2, ("no longer retransmitting hello request"));
6904 return 0;
Jerry Yuc2c673d2022-02-17 14:20:39 +08006905 }
6906 }
6907
Gilles Peskine449bd832023-01-11 14:50:10 +01006908 return ssl_write_hello_request(ssl);
Jerry Yuc2c673d2022-02-17 14:20:39 +08006909}
6910#endif
6911#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Jerry Yud9526692022-02-17 14:23:47 +08006912
Jerry Yud9526692022-02-17 14:23:47 +08006913/*
6914 * Handshake functions
6915 */
6916#if !defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
6917/* No certificate support -> dummy functions */
Gilles Peskine449bd832023-01-11 14:50:10 +01006918int mbedtls_ssl_write_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, ("=> write 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 write 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
Gilles Peskine449bd832023-01-11 14:50:10 +01006935int mbedtls_ssl_parse_certificate(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08006936{
6937 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6938 ssl->handshake->ciphersuite_info;
6939
Gilles Peskine449bd832023-01-11 14:50:10 +01006940 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006941
Gilles Peskine449bd832023-01-11 14:50:10 +01006942 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
6943 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006944 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01006945 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08006946 }
6947
Gilles Peskine449bd832023-01-11 14:50:10 +01006948 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
6949 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08006950}
6951
6952#else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
6953/* Some certificate support -> implement write and parse */
6954
Gilles Peskine449bd832023-01-11 14:50:10 +01006955int mbedtls_ssl_write_certificate(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08006956{
6957 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
6958 size_t i, n;
6959 const mbedtls_x509_crt *crt;
6960 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6961 ssl->handshake->ciphersuite_info;
6962
Gilles Peskine449bd832023-01-11 14:50:10 +01006963 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006964
Gilles Peskine449bd832023-01-11 14:50:10 +01006965 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
6966 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006967 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01006968 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08006969 }
6970
6971#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01006972 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
6973 if (ssl->handshake->client_auth == 0) {
6974 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006975 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01006976 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08006977 }
6978 }
6979#endif /* MBEDTLS_SSL_CLI_C */
6980#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01006981 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
6982 if (mbedtls_ssl_own_cert(ssl) == NULL) {
Jerry Yud9526692022-02-17 14:23:47 +08006983 /* Should never happen because we shouldn't have picked the
6984 * ciphersuite if we don't have a certificate. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006985 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08006986 }
6987 }
6988#endif
6989
Gilles Peskine449bd832023-01-11 14:50:10 +01006990 MBEDTLS_SSL_DEBUG_CRT(3, "own certificate", mbedtls_ssl_own_cert(ssl));
Jerry Yud9526692022-02-17 14:23:47 +08006991
6992 /*
6993 * 0 . 0 handshake type
6994 * 1 . 3 handshake length
6995 * 4 . 6 length of all certs
6996 * 7 . 9 length of cert. 1
6997 * 10 . n-1 peer certificate
6998 * n . n+2 length of cert. 2
6999 * n+3 . ... upper level cert, etc.
7000 */
7001 i = 7;
Gilles Peskine449bd832023-01-11 14:50:10 +01007002 crt = mbedtls_ssl_own_cert(ssl);
Jerry Yud9526692022-02-17 14:23:47 +08007003
Gilles Peskine449bd832023-01-11 14:50:10 +01007004 while (crt != NULL) {
Jerry Yud9526692022-02-17 14:23:47 +08007005 n = crt->raw.len;
Gilles Peskine449bd832023-01-11 14:50:10 +01007006 if (n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i) {
7007 MBEDTLS_SSL_DEBUG_MSG(1, ("certificate too large, %" MBEDTLS_PRINTF_SIZET
7008 " > %" MBEDTLS_PRINTF_SIZET,
7009 i + 3 + n, (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN));
7010 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
Jerry Yud9526692022-02-17 14:23:47 +08007011 }
7012
Gilles Peskine449bd832023-01-11 14:50:10 +01007013 ssl->out_msg[i] = MBEDTLS_BYTE_2(n);
7014 ssl->out_msg[i + 1] = MBEDTLS_BYTE_1(n);
7015 ssl->out_msg[i + 2] = MBEDTLS_BYTE_0(n);
Jerry Yud9526692022-02-17 14:23:47 +08007016
Gilles Peskine449bd832023-01-11 14:50:10 +01007017 i += 3; memcpy(ssl->out_msg + i, crt->raw.p, n);
Jerry Yud9526692022-02-17 14:23:47 +08007018 i += n; crt = crt->next;
7019 }
7020
Gilles Peskine449bd832023-01-11 14:50:10 +01007021 ssl->out_msg[4] = MBEDTLS_BYTE_2(i - 7);
7022 ssl->out_msg[5] = MBEDTLS_BYTE_1(i - 7);
7023 ssl->out_msg[6] = MBEDTLS_BYTE_0(i - 7);
Jerry Yud9526692022-02-17 14:23:47 +08007024
7025 ssl->out_msglen = i;
7026 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7027 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
7028
7029 ssl->state++;
7030
Gilles Peskine449bd832023-01-11 14:50:10 +01007031 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
7032 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
7033 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007034 }
7035
Gilles Peskine449bd832023-01-11 14:50:10 +01007036 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007037
Gilles Peskine449bd832023-01-11 14:50:10 +01007038 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007039}
7040
7041#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
7042
7043#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007044MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007045static int ssl_check_peer_crt_unchanged(mbedtls_ssl_context *ssl,
7046 unsigned char *crt_buf,
7047 size_t crt_buf_len)
Jerry Yud9526692022-02-17 14:23:47 +08007048{
7049 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
7050
Gilles Peskine449bd832023-01-11 14:50:10 +01007051 if (peer_crt == NULL) {
7052 return -1;
7053 }
Jerry Yud9526692022-02-17 14:23:47 +08007054
Gilles Peskine449bd832023-01-11 14:50:10 +01007055 if (peer_crt->raw.len != crt_buf_len) {
7056 return -1;
7057 }
Jerry Yud9526692022-02-17 14:23:47 +08007058
Gilles Peskine449bd832023-01-11 14:50:10 +01007059 return memcmp(peer_crt->raw.p, crt_buf, peer_crt->raw.len);
Jerry Yud9526692022-02-17 14:23:47 +08007060}
7061#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007062MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007063static int ssl_check_peer_crt_unchanged(mbedtls_ssl_context *ssl,
7064 unsigned char *crt_buf,
7065 size_t crt_buf_len)
Jerry Yud9526692022-02-17 14:23:47 +08007066{
7067 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7068 unsigned char const * const peer_cert_digest =
7069 ssl->session->peer_cert_digest;
7070 mbedtls_md_type_t const peer_cert_digest_type =
7071 ssl->session->peer_cert_digest_type;
7072 mbedtls_md_info_t const * const digest_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01007073 mbedtls_md_info_from_type(peer_cert_digest_type);
Jerry Yud9526692022-02-17 14:23:47 +08007074 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
7075 size_t digest_len;
7076
Gilles Peskine449bd832023-01-11 14:50:10 +01007077 if (peer_cert_digest == NULL || digest_info == NULL) {
7078 return -1;
7079 }
Jerry Yud9526692022-02-17 14:23:47 +08007080
Gilles Peskine449bd832023-01-11 14:50:10 +01007081 digest_len = mbedtls_md_get_size(digest_info);
7082 if (digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN) {
7083 return -1;
7084 }
Jerry Yud9526692022-02-17 14:23:47 +08007085
Gilles Peskine449bd832023-01-11 14:50:10 +01007086 ret = mbedtls_md(digest_info, crt_buf, crt_buf_len, tmp_digest);
7087 if (ret != 0) {
7088 return -1;
7089 }
Jerry Yud9526692022-02-17 14:23:47 +08007090
Gilles Peskine449bd832023-01-11 14:50:10 +01007091 return memcmp(tmp_digest, peer_cert_digest, digest_len);
Jerry Yud9526692022-02-17 14:23:47 +08007092}
7093#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7094#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
7095
7096/*
7097 * Once the certificate message is read, parse it into a cert chain and
7098 * perform basic checks, but leave actual verification to the caller
7099 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007100MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007101static int ssl_parse_certificate_chain(mbedtls_ssl_context *ssl,
7102 mbedtls_x509_crt *chain)
Jerry Yud9526692022-02-17 14:23:47 +08007103{
7104 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7105#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007106 int crt_cnt = 0;
Jerry Yud9526692022-02-17 14:23:47 +08007107#endif
7108 size_t i, n;
7109 uint8_t alert;
7110
Gilles Peskine449bd832023-01-11 14:50:10 +01007111 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
7112 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7113 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7114 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
7115 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Jerry Yud9526692022-02-17 14:23:47 +08007116 }
7117
Gilles Peskine449bd832023-01-11 14:50:10 +01007118 if (ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE) {
7119 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7120 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
7121 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Jerry Yud9526692022-02-17 14:23:47 +08007122 }
7123
Gilles Peskine449bd832023-01-11 14:50:10 +01007124 if (ssl->in_hslen < mbedtls_ssl_hs_hdr_len(ssl) + 3 + 3) {
7125 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7126 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7127 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
7128 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007129 }
7130
Gilles Peskine449bd832023-01-11 14:50:10 +01007131 i = mbedtls_ssl_hs_hdr_len(ssl);
Jerry Yud9526692022-02-17 14:23:47 +08007132
7133 /*
7134 * Same message structure as in mbedtls_ssl_write_certificate()
7135 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007136 n = (ssl->in_msg[i+1] << 8) | ssl->in_msg[i+2];
Jerry Yud9526692022-02-17 14:23:47 +08007137
Gilles Peskine449bd832023-01-11 14:50:10 +01007138 if (ssl->in_msg[i] != 0 ||
7139 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len(ssl)) {
7140 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7141 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7142 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
7143 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007144 }
7145
7146 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
7147 i += 3;
7148
7149 /* Iterate through and parse the CRTs in the provided chain. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007150 while (i < ssl->in_hslen) {
Jerry Yud9526692022-02-17 14:23:47 +08007151 /* Check that there's room for the next CRT's length fields. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007152 if (i + 3 > ssl->in_hslen) {
7153 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7154 mbedtls_ssl_send_alert_message(ssl,
7155 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7156 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
7157 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007158 }
7159 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
7160 * anything beyond 2**16 ~ 64K. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007161 if (ssl->in_msg[i] != 0) {
7162 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7163 mbedtls_ssl_send_alert_message(ssl,
7164 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7165 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT);
7166 return MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Jerry Yud9526692022-02-17 14:23:47 +08007167 }
7168
7169 /* Read length of the next CRT in the chain. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007170 n = ((unsigned int) ssl->in_msg[i + 1] << 8)
Jerry Yud9526692022-02-17 14:23:47 +08007171 | (unsigned int) ssl->in_msg[i + 2];
7172 i += 3;
7173
Gilles Peskine449bd832023-01-11 14:50:10 +01007174 if (n < 128 || i + n > ssl->in_hslen) {
7175 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7176 mbedtls_ssl_send_alert_message(ssl,
7177 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7178 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
7179 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007180 }
7181
7182 /* Check if we're handling the first CRT in the chain. */
7183#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007184 if (crt_cnt++ == 0 &&
Jerry Yud9526692022-02-17 14:23:47 +08007185 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Gilles Peskine449bd832023-01-11 14:50:10 +01007186 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
Jerry Yud9526692022-02-17 14:23:47 +08007187 /* During client-side renegotiation, check that the server's
7188 * end-CRTs hasn't changed compared to the initial handshake,
7189 * mitigating the triple handshake attack. On success, reuse
7190 * the original end-CRT instead of parsing it again. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007191 MBEDTLS_SSL_DEBUG_MSG(3, ("Check that peer CRT hasn't changed during renegotiation"));
7192 if (ssl_check_peer_crt_unchanged(ssl,
7193 &ssl->in_msg[i],
7194 n) != 0) {
7195 MBEDTLS_SSL_DEBUG_MSG(1, ("new server cert during renegotiation"));
7196 mbedtls_ssl_send_alert_message(ssl,
7197 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7198 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED);
7199 return MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Jerry Yud9526692022-02-17 14:23:47 +08007200 }
7201
7202 /* Now we can safely free the original chain. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007203 ssl_clear_peer_cert(ssl->session);
Jerry Yud9526692022-02-17 14:23:47 +08007204 }
7205#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
7206
7207 /* Parse the next certificate in the chain. */
7208#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007209 ret = mbedtls_x509_crt_parse_der(chain, ssl->in_msg + i, n);
Jerry Yud9526692022-02-17 14:23:47 +08007210#else
7211 /* If we don't need to store the CRT chain permanently, parse
7212 * it in-place from the input buffer instead of making a copy. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007213 ret = mbedtls_x509_crt_parse_der_nocopy(chain, ssl->in_msg + i, n);
Jerry Yud9526692022-02-17 14:23:47 +08007214#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007215 switch (ret) {
Jerry Yud9526692022-02-17 14:23:47 +08007216 case 0: /*ok*/
7217 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
7218 /* Ignore certificate with an unknown algorithm: maybe a
7219 prior certificate was already trusted. */
7220 break;
7221
7222 case MBEDTLS_ERR_X509_ALLOC_FAILED:
7223 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
7224 goto crt_parse_der_failed;
7225
7226 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
7227 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7228 goto crt_parse_der_failed;
7229
7230 default:
7231 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007232crt_parse_der_failed:
7233 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert);
7234 MBEDTLS_SSL_DEBUG_RET(1, " mbedtls_x509_crt_parse_der", ret);
7235 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007236 }
7237
7238 i += n;
7239 }
7240
Gilles Peskine449bd832023-01-11 14:50:10 +01007241 MBEDTLS_SSL_DEBUG_CRT(3, "peer certificate", chain);
7242 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08007243}
7244
7245#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007246MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007247static int ssl_srv_check_client_no_crt_notification(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08007248{
Gilles Peskine449bd832023-01-11 14:50:10 +01007249 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
7250 return -1;
7251 }
Jerry Yud9526692022-02-17 14:23:47 +08007252
Gilles Peskine449bd832023-01-11 14:50:10 +01007253 if (ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len(ssl) &&
Jerry Yud9526692022-02-17 14:23:47 +08007254 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
7255 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
Gilles Peskine449bd832023-01-11 14:50:10 +01007256 memcmp(ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl), "\0\0\0", 3) == 0) {
7257 MBEDTLS_SSL_DEBUG_MSG(1, ("peer has no certificate"));
7258 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08007259 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007260 return -1;
Jerry Yud9526692022-02-17 14:23:47 +08007261}
7262#endif /* MBEDTLS_SSL_SRV_C */
7263
7264/* Check if a certificate message is expected.
7265 * Return either
7266 * - SSL_CERTIFICATE_EXPECTED, or
7267 * - SSL_CERTIFICATE_SKIP
7268 * indicating whether a Certificate message is expected or not.
7269 */
7270#define SSL_CERTIFICATE_EXPECTED 0
7271#define SSL_CERTIFICATE_SKIP 1
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007272MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007273static int ssl_parse_certificate_coordinate(mbedtls_ssl_context *ssl,
7274 int authmode)
Jerry Yud9526692022-02-17 14:23:47 +08007275{
7276 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
7277 ssl->handshake->ciphersuite_info;
7278
Gilles Peskine449bd832023-01-11 14:50:10 +01007279 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
7280 return SSL_CERTIFICATE_SKIP;
7281 }
Jerry Yud9526692022-02-17 14:23:47 +08007282
7283#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007284 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
7285 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK) {
7286 return SSL_CERTIFICATE_SKIP;
7287 }
Jerry Yud9526692022-02-17 14:23:47 +08007288
Gilles Peskine449bd832023-01-11 14:50:10 +01007289 if (authmode == MBEDTLS_SSL_VERIFY_NONE) {
Jerry Yud9526692022-02-17 14:23:47 +08007290 ssl->session_negotiate->verify_result =
7291 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
Gilles Peskine449bd832023-01-11 14:50:10 +01007292 return SSL_CERTIFICATE_SKIP;
Jerry Yud9526692022-02-17 14:23:47 +08007293 }
7294 }
7295#else
7296 ((void) authmode);
7297#endif /* MBEDTLS_SSL_SRV_C */
7298
Gilles Peskine449bd832023-01-11 14:50:10 +01007299 return SSL_CERTIFICATE_EXPECTED;
Jerry Yud9526692022-02-17 14:23:47 +08007300}
7301
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007302MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007303static int ssl_parse_certificate_verify(mbedtls_ssl_context *ssl,
7304 int authmode,
7305 mbedtls_x509_crt *chain,
7306 void *rs_ctx)
Jerry Yud9526692022-02-17 14:23:47 +08007307{
7308 int ret = 0;
7309 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
7310 ssl->handshake->ciphersuite_info;
7311 int have_ca_chain = 0;
7312
7313 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
7314 void *p_vrfy;
7315
Gilles Peskine449bd832023-01-11 14:50:10 +01007316 if (authmode == MBEDTLS_SSL_VERIFY_NONE) {
7317 return 0;
7318 }
Jerry Yud9526692022-02-17 14:23:47 +08007319
Gilles Peskine449bd832023-01-11 14:50:10 +01007320 if (ssl->f_vrfy != NULL) {
7321 MBEDTLS_SSL_DEBUG_MSG(3, ("Use context-specific verification callback"));
Jerry Yud9526692022-02-17 14:23:47 +08007322 f_vrfy = ssl->f_vrfy;
7323 p_vrfy = ssl->p_vrfy;
Gilles Peskine449bd832023-01-11 14:50:10 +01007324 } else {
7325 MBEDTLS_SSL_DEBUG_MSG(3, ("Use configuration-specific verification callback"));
Jerry Yud9526692022-02-17 14:23:47 +08007326 f_vrfy = ssl->conf->f_vrfy;
7327 p_vrfy = ssl->conf->p_vrfy;
7328 }
7329
7330 /*
7331 * Main check: verify certificate
7332 */
7333#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Gilles Peskine449bd832023-01-11 14:50:10 +01007334 if (ssl->conf->f_ca_cb != NULL) {
Jerry Yud9526692022-02-17 14:23:47 +08007335 ((void) rs_ctx);
7336 have_ca_chain = 1;
7337
Gilles Peskine449bd832023-01-11 14:50:10 +01007338 MBEDTLS_SSL_DEBUG_MSG(3, ("use CA callback for X.509 CRT verification"));
Jerry Yud9526692022-02-17 14:23:47 +08007339 ret = mbedtls_x509_crt_verify_with_ca_cb(
7340 chain,
7341 ssl->conf->f_ca_cb,
7342 ssl->conf->p_ca_cb,
7343 ssl->conf->cert_profile,
7344 ssl->hostname,
7345 &ssl->session_negotiate->verify_result,
Gilles Peskine449bd832023-01-11 14:50:10 +01007346 f_vrfy, p_vrfy);
7347 } else
Jerry Yud9526692022-02-17 14:23:47 +08007348#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
7349 {
7350 mbedtls_x509_crt *ca_chain;
7351 mbedtls_x509_crl *ca_crl;
7352
7353#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01007354 if (ssl->handshake->sni_ca_chain != NULL) {
Jerry Yud9526692022-02-17 14:23:47 +08007355 ca_chain = ssl->handshake->sni_ca_chain;
7356 ca_crl = ssl->handshake->sni_ca_crl;
Gilles Peskine449bd832023-01-11 14:50:10 +01007357 } else
Jerry Yud9526692022-02-17 14:23:47 +08007358#endif
7359 {
7360 ca_chain = ssl->conf->ca_chain;
7361 ca_crl = ssl->conf->ca_crl;
7362 }
7363
Gilles Peskine449bd832023-01-11 14:50:10 +01007364 if (ca_chain != NULL) {
Jerry Yud9526692022-02-17 14:23:47 +08007365 have_ca_chain = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01007366 }
Jerry Yud9526692022-02-17 14:23:47 +08007367
7368 ret = mbedtls_x509_crt_verify_restartable(
7369 chain,
7370 ca_chain, ca_crl,
7371 ssl->conf->cert_profile,
7372 ssl->hostname,
7373 &ssl->session_negotiate->verify_result,
Gilles Peskine449bd832023-01-11 14:50:10 +01007374 f_vrfy, p_vrfy, rs_ctx);
Jerry Yud9526692022-02-17 14:23:47 +08007375 }
7376
Gilles Peskine449bd832023-01-11 14:50:10 +01007377 if (ret != 0) {
7378 MBEDTLS_SSL_DEBUG_RET(1, "x509_verify_cert", ret);
Jerry Yud9526692022-02-17 14:23:47 +08007379 }
7380
7381#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007382 if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
7383 return MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
7384 }
Jerry Yud9526692022-02-17 14:23:47 +08007385#endif
7386
7387 /*
7388 * Secondary checks: always done, but change 'ret' only if it was 0
7389 */
7390
7391#if defined(MBEDTLS_ECP_C)
7392 {
7393 const mbedtls_pk_context *pk = &chain->pk;
7394
Manuel Pégourié-Gonnard66b0d612022-06-17 10:49:29 +02007395 /* If certificate uses an EC key, make sure the curve is OK.
7396 * This is a public key, so it can't be opaque, so can_do() is a good
7397 * enough check to ensure pk_ec() is safe to use here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007398 if (mbedtls_pk_can_do(pk, MBEDTLS_PK_ECKEY)) {
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007399 /* and in the unlikely case the above assumption no longer holds
7400 * we are making sure that pk_ec() here does not return a NULL
7401 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007402 const mbedtls_ecp_keypair *ec = mbedtls_pk_ec(*pk);
7403 if (ec == NULL) {
7404 MBEDTLS_SSL_DEBUG_MSG(1, ("mbedtls_pk_ec() returned NULL"));
7405 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007406 }
Jerry Yud9526692022-02-17 14:23:47 +08007407
Gilles Peskine449bd832023-01-11 14:50:10 +01007408 if (mbedtls_ssl_check_curve(ssl, ec->grp.id) != 0) {
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007409 ssl->session_negotiate->verify_result |=
7410 MBEDTLS_X509_BADCERT_BAD_KEY;
7411
Gilles Peskine449bd832023-01-11 14:50:10 +01007412 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate (EC key curve)"));
7413 if (ret == 0) {
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007414 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Gilles Peskine449bd832023-01-11 14:50:10 +01007415 }
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007416 }
Jerry Yud9526692022-02-17 14:23:47 +08007417 }
7418 }
7419#endif /* MBEDTLS_ECP_C */
7420
Gilles Peskine449bd832023-01-11 14:50:10 +01007421 if (mbedtls_ssl_check_cert_usage(chain,
7422 ciphersuite_info,
7423 !ssl->conf->endpoint,
7424 &ssl->session_negotiate->verify_result) != 0) {
7425 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate (usage extensions)"));
7426 if (ret == 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007427 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Gilles Peskine449bd832023-01-11 14:50:10 +01007428 }
Jerry Yud9526692022-02-17 14:23:47 +08007429 }
7430
7431 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
7432 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
7433 * with details encoded in the verification flags. All other kinds
7434 * of error codes, including those from the user provided f_vrfy
7435 * functions, are treated as fatal and lead to a failure of
7436 * ssl_parse_certificate even if verification was optional. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007437 if (authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
7438 (ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
7439 ret == MBEDTLS_ERR_SSL_BAD_CERTIFICATE)) {
Jerry Yud9526692022-02-17 14:23:47 +08007440 ret = 0;
7441 }
7442
Gilles Peskine449bd832023-01-11 14:50:10 +01007443 if (have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED) {
7444 MBEDTLS_SSL_DEBUG_MSG(1, ("got no CA chain"));
Jerry Yud9526692022-02-17 14:23:47 +08007445 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
7446 }
7447
Gilles Peskine449bd832023-01-11 14:50:10 +01007448 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007449 uint8_t alert;
7450
7451 /* The certificate may have been rejected for several reasons.
7452 Pick one and send the corresponding alert. Which alert to send
7453 may be a subject of debate in some cases. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007454 if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER) {
Jerry Yud9526692022-02-17 14:23:47 +08007455 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
Gilles Peskine449bd832023-01-11 14:50:10 +01007456 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH) {
Jerry Yud9526692022-02-17 14:23:47 +08007457 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007458 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE) {
Jerry Yud9526692022-02-17 14:23:47 +08007459 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007460 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE) {
Jerry Yud9526692022-02-17 14:23:47 +08007461 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007462 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE) {
Jerry Yud9526692022-02-17 14:23:47 +08007463 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007464 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK) {
Jerry Yud9526692022-02-17 14:23:47 +08007465 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007466 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY) {
Jerry Yud9526692022-02-17 14:23:47 +08007467 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007468 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED) {
Jerry Yud9526692022-02-17 14:23:47 +08007469 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
Gilles Peskine449bd832023-01-11 14:50:10 +01007470 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED) {
Jerry Yud9526692022-02-17 14:23:47 +08007471 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
Gilles Peskine449bd832023-01-11 14:50:10 +01007472 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED) {
Jerry Yud9526692022-02-17 14:23:47 +08007473 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
Gilles Peskine449bd832023-01-11 14:50:10 +01007474 } else {
Jerry Yud9526692022-02-17 14:23:47 +08007475 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
Gilles Peskine449bd832023-01-11 14:50:10 +01007476 }
7477 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7478 alert);
Jerry Yud9526692022-02-17 14:23:47 +08007479 }
7480
7481#if defined(MBEDTLS_DEBUG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007482 if (ssl->session_negotiate->verify_result != 0) {
7483 MBEDTLS_SSL_DEBUG_MSG(3, ("! Certificate verification flags %08x",
7484 (unsigned int) ssl->session_negotiate->verify_result));
7485 } else {
7486 MBEDTLS_SSL_DEBUG_MSG(3, ("Certificate verification flags clear"));
Jerry Yud9526692022-02-17 14:23:47 +08007487 }
7488#endif /* MBEDTLS_DEBUG_C */
7489
Gilles Peskine449bd832023-01-11 14:50:10 +01007490 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007491}
7492
7493#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007494MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007495static int ssl_remember_peer_crt_digest(mbedtls_ssl_context *ssl,
7496 unsigned char *start, size_t len)
Jerry Yud9526692022-02-17 14:23:47 +08007497{
7498 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7499 /* Remember digest of the peer's end-CRT. */
7500 ssl->session_negotiate->peer_cert_digest =
Gilles Peskine449bd832023-01-11 14:50:10 +01007501 mbedtls_calloc(1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN);
7502 if (ssl->session_negotiate->peer_cert_digest == NULL) {
7503 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%d bytes) failed",
7504 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN));
7505 mbedtls_ssl_send_alert_message(ssl,
7506 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7507 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
Jerry Yud9526692022-02-17 14:23:47 +08007508
Gilles Peskine449bd832023-01-11 14:50:10 +01007509 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Jerry Yud9526692022-02-17 14:23:47 +08007510 }
7511
Gilles Peskine449bd832023-01-11 14:50:10 +01007512 ret = mbedtls_md(mbedtls_md_info_from_type(
7513 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE),
7514 start, len,
7515 ssl->session_negotiate->peer_cert_digest);
Jerry Yud9526692022-02-17 14:23:47 +08007516
7517 ssl->session_negotiate->peer_cert_digest_type =
7518 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
7519 ssl->session_negotiate->peer_cert_digest_len =
7520 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
7521
Gilles Peskine449bd832023-01-11 14:50:10 +01007522 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007523}
7524
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007525MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007526static int ssl_remember_peer_pubkey(mbedtls_ssl_context *ssl,
7527 unsigned char *start, size_t len)
Jerry Yud9526692022-02-17 14:23:47 +08007528{
7529 unsigned char *end = start + len;
7530 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7531
7532 /* Make a copy of the peer's raw public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007533 mbedtls_pk_init(&ssl->handshake->peer_pubkey);
7534 ret = mbedtls_pk_parse_subpubkey(&start, end,
7535 &ssl->handshake->peer_pubkey);
7536 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007537 /* We should have parsed the public key before. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007538 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007539 }
7540
Gilles Peskine449bd832023-01-11 14:50:10 +01007541 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08007542}
7543#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7544
Gilles Peskine449bd832023-01-11 14:50:10 +01007545int mbedtls_ssl_parse_certificate(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08007546{
7547 int ret = 0;
7548 int crt_expected;
7549#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7550 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
7551 ? ssl->handshake->sni_authmode
7552 : ssl->conf->authmode;
7553#else
7554 const int authmode = ssl->conf->authmode;
7555#endif
7556 void *rs_ctx = NULL;
7557 mbedtls_x509_crt *chain = NULL;
7558
Gilles Peskine449bd832023-01-11 14:50:10 +01007559 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007560
Gilles Peskine449bd832023-01-11 14:50:10 +01007561 crt_expected = ssl_parse_certificate_coordinate(ssl, authmode);
7562 if (crt_expected == SSL_CERTIFICATE_SKIP) {
7563 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007564 goto exit;
7565 }
7566
7567#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007568 if (ssl->handshake->ecrs_enabled &&
7569 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify) {
Jerry Yud9526692022-02-17 14:23:47 +08007570 chain = ssl->handshake->ecrs_peer_cert;
7571 ssl->handshake->ecrs_peer_cert = NULL;
7572 goto crt_verify;
7573 }
7574#endif
7575
Gilles Peskine449bd832023-01-11 14:50:10 +01007576 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007577 /* mbedtls_ssl_read_record may have sent an alert already. We
7578 let it decide whether to alert. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007579 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
Jerry Yud9526692022-02-17 14:23:47 +08007580 goto exit;
7581 }
7582
7583#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007584 if (ssl_srv_check_client_no_crt_notification(ssl) == 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007585 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
7586
Gilles Peskine449bd832023-01-11 14:50:10 +01007587 if (authmode != MBEDTLS_SSL_VERIFY_OPTIONAL) {
Jerry Yud9526692022-02-17 14:23:47 +08007588 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Gilles Peskine449bd832023-01-11 14:50:10 +01007589 }
Jerry Yud9526692022-02-17 14:23:47 +08007590
7591 goto exit;
7592 }
7593#endif /* MBEDTLS_SSL_SRV_C */
7594
7595 /* Clear existing peer CRT structure in case we tried to
7596 * reuse a session but it failed, and allocate a new one. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007597 ssl_clear_peer_cert(ssl->session_negotiate);
Jerry Yud9526692022-02-17 14:23:47 +08007598
Gilles Peskine449bd832023-01-11 14:50:10 +01007599 chain = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
7600 if (chain == NULL) {
7601 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed",
7602 sizeof(mbedtls_x509_crt)));
7603 mbedtls_ssl_send_alert_message(ssl,
7604 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7605 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
Jerry Yud9526692022-02-17 14:23:47 +08007606
7607 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
7608 goto exit;
7609 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007610 mbedtls_x509_crt_init(chain);
Jerry Yud9526692022-02-17 14:23:47 +08007611
Gilles Peskine449bd832023-01-11 14:50:10 +01007612 ret = ssl_parse_certificate_chain(ssl, chain);
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_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007618 if (ssl->handshake->ecrs_enabled) {
Jerry Yud9526692022-02-17 14:23:47 +08007619 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Gilles Peskine449bd832023-01-11 14:50:10 +01007620 }
Jerry Yud9526692022-02-17 14:23:47 +08007621
7622crt_verify:
Gilles Peskine449bd832023-01-11 14:50:10 +01007623 if (ssl->handshake->ecrs_enabled) {
Jerry Yud9526692022-02-17 14:23:47 +08007624 rs_ctx = &ssl->handshake->ecrs_ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +01007625 }
Jerry Yud9526692022-02-17 14:23:47 +08007626#endif
7627
Gilles Peskine449bd832023-01-11 14:50:10 +01007628 ret = ssl_parse_certificate_verify(ssl, authmode,
7629 chain, rs_ctx);
7630 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007631 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007632 }
Jerry Yud9526692022-02-17 14:23:47 +08007633
7634#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7635 {
7636 unsigned char *crt_start, *pk_start;
7637 size_t crt_len, pk_len;
7638
7639 /* We parse the CRT chain without copying, so
7640 * these pointers point into the input buffer,
7641 * and are hence still valid after freeing the
7642 * CRT chain. */
7643
7644 crt_start = chain->raw.p;
7645 crt_len = chain->raw.len;
7646
7647 pk_start = chain->pk_raw.p;
7648 pk_len = chain->pk_raw.len;
7649
7650 /* Free the CRT structures before computing
7651 * digest and copying the peer's public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007652 mbedtls_x509_crt_free(chain);
7653 mbedtls_free(chain);
Jerry Yud9526692022-02-17 14:23:47 +08007654 chain = NULL;
7655
Gilles Peskine449bd832023-01-11 14:50:10 +01007656 ret = ssl_remember_peer_crt_digest(ssl, crt_start, crt_len);
7657 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007658 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007659 }
Jerry Yud9526692022-02-17 14:23:47 +08007660
Gilles Peskine449bd832023-01-11 14:50:10 +01007661 ret = ssl_remember_peer_pubkey(ssl, pk_start, pk_len);
7662 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007663 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007664 }
Jerry Yud9526692022-02-17 14:23:47 +08007665 }
7666#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7667 /* Pass ownership to session structure. */
7668 ssl->session_negotiate->peer_cert = chain;
7669 chain = NULL;
7670#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7671
Gilles Peskine449bd832023-01-11 14:50:10 +01007672 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007673
7674exit:
7675
Gilles Peskine449bd832023-01-11 14:50:10 +01007676 if (ret == 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007677 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01007678 }
Jerry Yud9526692022-02-17 14:23:47 +08007679
7680#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007681 if (ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS) {
Jerry Yud9526692022-02-17 14:23:47 +08007682 ssl->handshake->ecrs_peer_cert = chain;
7683 chain = NULL;
7684 }
7685#endif
7686
Gilles Peskine449bd832023-01-11 14:50:10 +01007687 if (chain != NULL) {
7688 mbedtls_x509_crt_free(chain);
7689 mbedtls_free(chain);
Jerry Yud9526692022-02-17 14:23:47 +08007690 }
7691
Gilles Peskine449bd832023-01-11 14:50:10 +01007692 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007693}
7694#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
7695
Andrzej Kurek25f27152022-08-17 16:09:31 -04007696#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01007697static int ssl_calc_finished_tls_sha256(
Gilles Peskine449bd832023-01-11 14:50:10 +01007698 mbedtls_ssl_context *ssl, unsigned char *buf, int from)
Jerry Yu615bd6f2022-02-17 14:25:15 +08007699{
7700 int len = 12;
7701 const char *sender;
7702 unsigned char padbuf[32];
7703#if defined(MBEDTLS_USE_PSA_CRYPTO)
7704 size_t hash_size;
7705 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
7706 psa_status_t status;
7707#else
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007708 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01007709 mbedtls_md_context_t sha256;
Jerry Yu615bd6f2022-02-17 14:25:15 +08007710#endif
7711
7712 mbedtls_ssl_session *session = ssl->session_negotiate;
Gilles Peskine449bd832023-01-11 14:50:10 +01007713 if (!session) {
Jerry Yu615bd6f2022-02-17 14:25:15 +08007714 session = ssl->session;
Gilles Peskine449bd832023-01-11 14:50:10 +01007715 }
Jerry Yu615bd6f2022-02-17 14:25:15 +08007716
Gilles Peskine449bd832023-01-11 14:50:10 +01007717 sender = (from == MBEDTLS_SSL_IS_CLIENT)
Jerry Yu615bd6f2022-02-17 14:25:15 +08007718 ? "client finished"
7719 : "server finished";
7720
7721#if defined(MBEDTLS_USE_PSA_CRYPTO)
7722 sha256_psa = psa_hash_operation_init();
7723
Gilles Peskine449bd832023-01-11 14:50:10 +01007724 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc PSA finished tls sha256"));
Jerry Yu615bd6f2022-02-17 14:25:15 +08007725
Gilles Peskine449bd832023-01-11 14:50:10 +01007726 status = psa_hash_clone(&ssl->handshake->fin_sha256_psa, &sha256_psa);
7727 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007728 goto exit;
Jerry Yu615bd6f2022-02-17 14:25:15 +08007729 }
7730
Gilles Peskine449bd832023-01-11 14:50:10 +01007731 status = psa_hash_finish(&sha256_psa, padbuf, sizeof(padbuf), &hash_size);
7732 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007733 goto exit;
Jerry Yu615bd6f2022-02-17 14:25:15 +08007734 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007735 MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated padbuf", padbuf, 32);
Jerry Yu615bd6f2022-02-17 14:25:15 +08007736#else
7737
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01007738 mbedtls_md_init(&sha256);
Jerry Yu615bd6f2022-02-17 14:25:15 +08007739
Gilles Peskine449bd832023-01-11 14:50:10 +01007740 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc finished tls sha256"));
Jerry Yu615bd6f2022-02-17 14:25:15 +08007741
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01007742 ret = mbedtls_md_setup(&sha256, mbedtls_md_info_from_type(MBEDTLS_MD_SHA256), 0);
7743 if (ret != 0) {
7744 goto exit;
7745 }
7746 ret = mbedtls_md_clone(&sha256, &ssl->handshake->fin_sha256);
7747 if (ret != 0) {
7748 goto exit;
7749 }
Jerry Yu615bd6f2022-02-17 14:25:15 +08007750
7751 /*
7752 * TLSv1.2:
7753 * hash = PRF( master, finished_label,
7754 * Hash( handshake ) )[0.11]
7755 */
7756
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01007757 ret = mbedtls_md_finish(&sha256, padbuf);
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007758 if (ret != 0) {
7759 goto exit;
7760 }
Jerry Yu615bd6f2022-02-17 14:25:15 +08007761#endif /* MBEDTLS_USE_PSA_CRYPTO */
7762
Manuel Pégourié-Gonnard0ac71c02023-02-24 12:13:55 +01007763 MBEDTLS_SSL_DEBUG_BUF(4, "finished sha256 output", padbuf, 32);
7764
Gilles Peskine449bd832023-01-11 14:50:10 +01007765 ssl->handshake->tls_prf(session->master, 48, sender,
7766 padbuf, 32, buf, len);
Jerry Yu615bd6f2022-02-17 14:25:15 +08007767
Gilles Peskine449bd832023-01-11 14:50:10 +01007768 MBEDTLS_SSL_DEBUG_BUF(3, "calc finished result", buf, len);
Jerry Yu615bd6f2022-02-17 14:25:15 +08007769
Gilles Peskine449bd832023-01-11 14:50:10 +01007770 mbedtls_platform_zeroize(padbuf, sizeof(padbuf));
Jerry Yu615bd6f2022-02-17 14:25:15 +08007771
Gilles Peskine449bd832023-01-11 14:50:10 +01007772 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc finished"));
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007773
7774exit:
7775#if defined(MBEDTLS_USE_PSA_CRYPTO)
7776 psa_hash_abort(&sha256_psa);
Andrzej Kurekdaf5b562023-03-03 05:52:28 -05007777 return PSA_TO_MD_ERR(status);
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007778#else
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01007779 mbedtls_md_free(&sha256);
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007780 return ret;
7781#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu615bd6f2022-02-17 14:25:15 +08007782}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04007783#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yu615bd6f2022-02-17 14:25:15 +08007784
Jerry Yub7ba49e2022-02-17 14:25:53 +08007785
Andrzej Kurek25f27152022-08-17 16:09:31 -04007786#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01007787static int ssl_calc_finished_tls_sha384(
Gilles Peskine449bd832023-01-11 14:50:10 +01007788 mbedtls_ssl_context *ssl, unsigned char *buf, int from)
Jerry Yub7ba49e2022-02-17 14:25:53 +08007789{
7790 int len = 12;
7791 const char *sender;
7792 unsigned char padbuf[48];
7793#if defined(MBEDTLS_USE_PSA_CRYPTO)
7794 size_t hash_size;
7795 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
7796 psa_status_t status;
7797#else
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007798 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01007799 mbedtls_md_context_t sha384;
Jerry Yub7ba49e2022-02-17 14:25:53 +08007800#endif
7801
7802 mbedtls_ssl_session *session = ssl->session_negotiate;
Gilles Peskine449bd832023-01-11 14:50:10 +01007803 if (!session) {
Jerry Yub7ba49e2022-02-17 14:25:53 +08007804 session = ssl->session;
Gilles Peskine449bd832023-01-11 14:50:10 +01007805 }
Jerry Yub7ba49e2022-02-17 14:25:53 +08007806
Gilles Peskine449bd832023-01-11 14:50:10 +01007807 sender = (from == MBEDTLS_SSL_IS_CLIENT)
Jerry Yub7ba49e2022-02-17 14:25:53 +08007808 ? "client finished"
7809 : "server finished";
7810
7811#if defined(MBEDTLS_USE_PSA_CRYPTO)
7812 sha384_psa = psa_hash_operation_init();
7813
Gilles Peskine449bd832023-01-11 14:50:10 +01007814 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc PSA finished tls sha384"));
Jerry Yub7ba49e2022-02-17 14:25:53 +08007815
Gilles Peskine449bd832023-01-11 14:50:10 +01007816 status = psa_hash_clone(&ssl->handshake->fin_sha384_psa, &sha384_psa);
7817 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007818 goto exit;
Jerry Yub7ba49e2022-02-17 14:25:53 +08007819 }
7820
Gilles Peskine449bd832023-01-11 14:50:10 +01007821 status = psa_hash_finish(&sha384_psa, padbuf, sizeof(padbuf), &hash_size);
7822 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007823 goto exit;
Jerry Yub7ba49e2022-02-17 14:25:53 +08007824 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007825 MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated padbuf", padbuf, 48);
Jerry Yub7ba49e2022-02-17 14:25:53 +08007826#else
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01007827 mbedtls_md_init(&sha384);
Jerry Yub7ba49e2022-02-17 14:25:53 +08007828
Gilles Peskine449bd832023-01-11 14:50:10 +01007829 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc finished tls sha384"));
Jerry Yub7ba49e2022-02-17 14:25:53 +08007830
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01007831 ret = mbedtls_md_setup(&sha384, mbedtls_md_info_from_type(MBEDTLS_MD_SHA384), 0);
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01007832 if (ret != 0) {
7833 goto exit;
7834 }
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01007835 ret = mbedtls_md_clone(&sha384, &ssl->handshake->fin_sha384);
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01007836 if (ret != 0) {
7837 goto exit;
7838 }
Jerry Yub7ba49e2022-02-17 14:25:53 +08007839
7840 /*
7841 * TLSv1.2:
7842 * hash = PRF( master, finished_label,
7843 * Hash( handshake ) )[0.11]
7844 */
7845
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01007846 ret = mbedtls_md_finish(&sha384, padbuf);
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007847 if (ret != 0) {
7848 goto exit;
7849 }
Jerry Yub7ba49e2022-02-17 14:25:53 +08007850#endif
7851
Manuel Pégourié-Gonnard0ac71c02023-02-24 12:13:55 +01007852 MBEDTLS_SSL_DEBUG_BUF(4, "finished sha384 output", padbuf, 48);
7853
Gilles Peskine449bd832023-01-11 14:50:10 +01007854 ssl->handshake->tls_prf(session->master, 48, sender,
7855 padbuf, 48, buf, len);
Jerry Yub7ba49e2022-02-17 14:25:53 +08007856
Gilles Peskine449bd832023-01-11 14:50:10 +01007857 MBEDTLS_SSL_DEBUG_BUF(3, "calc finished result", buf, len);
Jerry Yub7ba49e2022-02-17 14:25:53 +08007858
Gilles Peskine449bd832023-01-11 14:50:10 +01007859 mbedtls_platform_zeroize(padbuf, sizeof(padbuf));
Jerry Yub7ba49e2022-02-17 14:25:53 +08007860
Gilles Peskine449bd832023-01-11 14:50:10 +01007861 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc finished"));
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007862
7863exit:
7864#if defined(MBEDTLS_USE_PSA_CRYPTO)
7865 psa_hash_abort(&sha384_psa);
Andrzej Kurekdaf5b562023-03-03 05:52:28 -05007866 return PSA_TO_MD_ERR(status);
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007867#else
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01007868 mbedtls_md_free(&sha384);
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007869 return ret;
7870#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yub7ba49e2022-02-17 14:25:53 +08007871}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04007872#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yub7ba49e2022-02-17 14:25:53 +08007873
Gilles Peskine449bd832023-01-11 14:50:10 +01007874void mbedtls_ssl_handshake_wrapup_free_hs_transform(mbedtls_ssl_context *ssl)
Jerry Yuaef00152022-02-17 14:27:31 +08007875{
Gilles Peskine449bd832023-01-11 14:50:10 +01007876 MBEDTLS_SSL_DEBUG_MSG(3, ("=> handshake wrapup: final free"));
Jerry Yuaef00152022-02-17 14:27:31 +08007877
7878 /*
7879 * Free our handshake params
7880 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007881 mbedtls_ssl_handshake_free(ssl);
7882 mbedtls_free(ssl->handshake);
Jerry Yuaef00152022-02-17 14:27:31 +08007883 ssl->handshake = NULL;
7884
7885 /*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08007886 * Free the previous transform and switch in the current one
Jerry Yuaef00152022-02-17 14:27:31 +08007887 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007888 if (ssl->transform) {
7889 mbedtls_ssl_transform_free(ssl->transform);
7890 mbedtls_free(ssl->transform);
Jerry Yuaef00152022-02-17 14:27:31 +08007891 }
7892 ssl->transform = ssl->transform_negotiate;
7893 ssl->transform_negotiate = NULL;
7894
Gilles Peskine449bd832023-01-11 14:50:10 +01007895 MBEDTLS_SSL_DEBUG_MSG(3, ("<= handshake wrapup: final free"));
Jerry Yuaef00152022-02-17 14:27:31 +08007896}
7897
Gilles Peskine449bd832023-01-11 14:50:10 +01007898void mbedtls_ssl_handshake_wrapup(mbedtls_ssl_context *ssl)
Jerry Yu2a9fff52022-02-17 14:28:51 +08007899{
7900 int resume = ssl->handshake->resume;
7901
Gilles Peskine449bd832023-01-11 14:50:10 +01007902 MBEDTLS_SSL_DEBUG_MSG(3, ("=> handshake wrapup"));
Jerry Yu2a9fff52022-02-17 14:28:51 +08007903
7904#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01007905 if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
Jerry Yu2a9fff52022-02-17 14:28:51 +08007906 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
7907 ssl->renego_records_seen = 0;
7908 }
7909#endif
7910
7911 /*
7912 * Free the previous session and switch in the current one
7913 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007914 if (ssl->session) {
Jerry Yu2a9fff52022-02-17 14:28:51 +08007915#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
7916 /* RFC 7366 3.1: keep the EtM state */
7917 ssl->session_negotiate->encrypt_then_mac =
Gilles Peskine449bd832023-01-11 14:50:10 +01007918 ssl->session->encrypt_then_mac;
Jerry Yu2a9fff52022-02-17 14:28:51 +08007919#endif
7920
Gilles Peskine449bd832023-01-11 14:50:10 +01007921 mbedtls_ssl_session_free(ssl->session);
7922 mbedtls_free(ssl->session);
Jerry Yu2a9fff52022-02-17 14:28:51 +08007923 }
7924 ssl->session = ssl->session_negotiate;
7925 ssl->session_negotiate = NULL;
7926
7927 /*
7928 * Add cache entry
7929 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007930 if (ssl->conf->f_set_cache != NULL &&
Jerry Yu2a9fff52022-02-17 14:28:51 +08007931 ssl->session->id_len != 0 &&
Gilles Peskine449bd832023-01-11 14:50:10 +01007932 resume == 0) {
7933 if (ssl->conf->f_set_cache(ssl->conf->p_cache,
7934 ssl->session->id,
7935 ssl->session->id_len,
7936 ssl->session) != 0) {
7937 MBEDTLS_SSL_DEBUG_MSG(1, ("cache did not store session"));
7938 }
Jerry Yu2a9fff52022-02-17 14:28:51 +08007939 }
7940
7941#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007942 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7943 ssl->handshake->flight != NULL) {
Jerry Yu2a9fff52022-02-17 14:28:51 +08007944 /* Cancel handshake timer */
Gilles Peskine449bd832023-01-11 14:50:10 +01007945 mbedtls_ssl_set_timer(ssl, 0);
Jerry Yu2a9fff52022-02-17 14:28:51 +08007946
7947 /* Keep last flight around in case we need to resend it:
7948 * we need the handshake and transform structures for that */
Gilles Peskine449bd832023-01-11 14:50:10 +01007949 MBEDTLS_SSL_DEBUG_MSG(3, ("skip freeing handshake and transform"));
7950 } else
Jerry Yu2a9fff52022-02-17 14:28:51 +08007951#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01007952 mbedtls_ssl_handshake_wrapup_free_hs_transform(ssl);
Jerry Yu2a9fff52022-02-17 14:28:51 +08007953
Jerry Yu5ed73ff2022-10-27 13:08:42 +08007954 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
Jerry Yu2a9fff52022-02-17 14:28:51 +08007955
Gilles Peskine449bd832023-01-11 14:50:10 +01007956 MBEDTLS_SSL_DEBUG_MSG(3, ("<= handshake wrapup"));
Jerry Yu2a9fff52022-02-17 14:28:51 +08007957}
7958
Gilles Peskine449bd832023-01-11 14:50:10 +01007959int mbedtls_ssl_write_finished(mbedtls_ssl_context *ssl)
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007960{
7961 int ret, hash_len;
7962
Gilles Peskine449bd832023-01-11 14:50:10 +01007963 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write finished"));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007964
Gilles Peskine449bd832023-01-11 14:50:10 +01007965 mbedtls_ssl_update_out_pointers(ssl, ssl->transform_negotiate);
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007966
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01007967 ret = ssl->handshake->calc_finished(ssl, ssl->out_msg + 4, ssl->conf->endpoint);
7968 if (ret != 0) {
7969 MBEDTLS_SSL_DEBUG_RET(1, "calc_finished", ret);
7970 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007971
7972 /*
7973 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7974 * may define some other value. Currently (early 2016), no defined
7975 * ciphersuite does this (and this is unlikely to change as activity has
7976 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7977 */
7978 hash_len = 12;
7979
7980#if defined(MBEDTLS_SSL_RENEGOTIATION)
7981 ssl->verify_data_len = hash_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01007982 memcpy(ssl->own_verify_data, ssl->out_msg + 4, hash_len);
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007983#endif
7984
7985 ssl->out_msglen = 4 + hash_len;
7986 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7987 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
7988
7989 /*
7990 * In case of session resuming, invert the client and server
7991 * ChangeCipherSpec messages order.
7992 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007993 if (ssl->handshake->resume != 0) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007994#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007995 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007996 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Gilles Peskine449bd832023-01-11 14:50:10 +01007997 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007998#endif
7999#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008000 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008001 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Gilles Peskine449bd832023-01-11 14:50:10 +01008002 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008003#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01008004 } else {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008005 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01008006 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008007
8008 /*
8009 * Switch to our negotiated transform and session parameters for outbound
8010 * data.
8011 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008012 MBEDTLS_SSL_DEBUG_MSG(3, ("switching to new transform spec for outbound data"));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008013
8014#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01008015 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008016 unsigned char i;
8017
8018 /* Remember current epoch settings for resending */
8019 ssl->handshake->alt_transform_out = ssl->transform_out;
Gilles Peskine449bd832023-01-11 14:50:10 +01008020 memcpy(ssl->handshake->alt_out_ctr, ssl->cur_out_ctr,
8021 sizeof(ssl->handshake->alt_out_ctr));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008022
8023 /* Set sequence_number to zero */
Gilles Peskine449bd832023-01-11 14:50:10 +01008024 memset(&ssl->cur_out_ctr[2], 0, sizeof(ssl->cur_out_ctr) - 2);
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008025
8026
8027 /* Increment epoch */
Gilles Peskine449bd832023-01-11 14:50:10 +01008028 for (i = 2; i > 0; i--) {
8029 if (++ssl->cur_out_ctr[i - 1] != 0) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008030 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01008031 }
8032 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008033
8034 /* The loop goes to its end iff the counter is wrapping */
Gilles Peskine449bd832023-01-11 14:50:10 +01008035 if (i == 0) {
8036 MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS epoch would wrap"));
8037 return MBEDTLS_ERR_SSL_COUNTER_WRAPPING;
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008038 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008039 } else
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008040#endif /* MBEDTLS_SSL_PROTO_DTLS */
Gilles Peskine449bd832023-01-11 14:50:10 +01008041 memset(ssl->cur_out_ctr, 0, sizeof(ssl->cur_out_ctr));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008042
8043 ssl->transform_out = ssl->transform_negotiate;
8044 ssl->session_out = ssl->session_negotiate;
8045
8046#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01008047 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
8048 mbedtls_ssl_send_flight_completed(ssl);
8049 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008050#endif
8051
Gilles Peskine449bd832023-01-11 14:50:10 +01008052 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
8053 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
8054 return ret;
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008055 }
8056
8057#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01008058 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
8059 (ret = mbedtls_ssl_flight_transmit(ssl)) != 0) {
8060 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_flight_transmit", ret);
8061 return ret;
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008062 }
8063#endif
8064
Gilles Peskine449bd832023-01-11 14:50:10 +01008065 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write finished"));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008066
Gilles Peskine449bd832023-01-11 14:50:10 +01008067 return 0;
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008068}
8069
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008070#define SSL_MAX_HASH_LEN 12
8071
Gilles Peskine449bd832023-01-11 14:50:10 +01008072int mbedtls_ssl_parse_finished(mbedtls_ssl_context *ssl)
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008073{
8074 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
8075 unsigned int hash_len = 12;
8076 unsigned char buf[SSL_MAX_HASH_LEN];
8077
Gilles Peskine449bd832023-01-11 14:50:10 +01008078 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse finished"));
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008079
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01008080 ret = ssl->handshake->calc_finished(ssl, buf, ssl->conf->endpoint ^ 1);
8081 if (ret != 0) {
8082 MBEDTLS_SSL_DEBUG_RET(1, "calc_finished", ret);
8083 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008084
Gilles Peskine449bd832023-01-11 14:50:10 +01008085 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
8086 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008087 goto exit;
8088 }
8089
Gilles Peskine449bd832023-01-11 14:50:10 +01008090 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
8091 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
8092 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8093 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008094 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
8095 goto exit;
8096 }
8097
Gilles Peskine449bd832023-01-11 14:50:10 +01008098 if (ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED) {
8099 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8100 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008101 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
8102 goto exit;
8103 }
8104
Gilles Peskine449bd832023-01-11 14:50:10 +01008105 if (ssl->in_hslen != mbedtls_ssl_hs_hdr_len(ssl) + hash_len) {
8106 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
8107 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8108 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008109 ret = MBEDTLS_ERR_SSL_DECODE_ERROR;
8110 goto exit;
8111 }
8112
Gilles Peskine449bd832023-01-11 14:50:10 +01008113 if (mbedtls_ct_memcmp(ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl),
8114 buf, hash_len) != 0) {
8115 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
8116 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8117 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008118 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
8119 goto exit;
8120 }
8121
8122#if defined(MBEDTLS_SSL_RENEGOTIATION)
8123 ssl->verify_data_len = hash_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008124 memcpy(ssl->peer_verify_data, buf, hash_len);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008125#endif
8126
Gilles Peskine449bd832023-01-11 14:50:10 +01008127 if (ssl->handshake->resume != 0) {
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008128#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008129 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008130 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Gilles Peskine449bd832023-01-11 14:50:10 +01008131 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008132#endif
8133#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008134 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008135 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Gilles Peskine449bd832023-01-11 14:50:10 +01008136 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008137#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01008138 } else {
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008139 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01008140 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008141
8142#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01008143 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
8144 mbedtls_ssl_recv_flight_completed(ssl);
8145 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008146#endif
8147
Gilles Peskine449bd832023-01-11 14:50:10 +01008148 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse finished"));
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008149
8150exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008151 mbedtls_platform_zeroize(buf, hash_len);
8152 return ret;
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008153}
8154
Jerry Yu392112c2022-02-17 14:34:10 +08008155#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
8156/*
8157 * Helper to get TLS 1.2 PRF from ciphersuite
8158 * (Duplicates bits of logic from ssl_set_handshake_prfs().)
8159 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008160static tls_prf_fn ssl_tls12prf_from_cs(int ciphersuite_id)
Jerry Yu392112c2022-02-17 14:34:10 +08008161{
Jerry Yu392112c2022-02-17 14:34:10 +08008162 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01008163 mbedtls_ssl_ciphersuite_from_id(ciphersuite_id);
Andrzej Kurek68327742022-10-03 06:18:18 -04008164#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01008165 if (ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA384) {
8166 return tls_prf_sha384;
8167 } else
Jerry Yu392112c2022-02-17 14:34:10 +08008168#endif
Andrzej Kurek894edde2022-09-29 06:31:14 -04008169#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
8170 {
Gilles Peskine449bd832023-01-11 14:50:10 +01008171 if (ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA256) {
8172 return tls_prf_sha256;
8173 }
Andrzej Kurek894edde2022-09-29 06:31:14 -04008174 }
8175#endif
8176#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
8177 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
8178 (void) ciphersuite_info;
8179#endif
Andrzej Kurekeabeb302022-10-17 07:52:51 -04008180
Gilles Peskine449bd832023-01-11 14:50:10 +01008181 return NULL;
Jerry Yu392112c2022-02-17 14:34:10 +08008182}
8183#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Jerry Yue93ffcd2022-02-17 14:37:06 +08008184
Gilles Peskine449bd832023-01-11 14:50:10 +01008185static mbedtls_tls_prf_types tls_prf_get_type(mbedtls_ssl_tls_prf_cb *tls_prf)
Jerry Yue93ffcd2022-02-17 14:37:06 +08008186{
8187 ((void) tls_prf);
Andrzej Kurek25f27152022-08-17 16:09:31 -04008188#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01008189 if (tls_prf == tls_prf_sha384) {
8190 return MBEDTLS_SSL_TLS_PRF_SHA384;
8191 } else
Jerry Yue93ffcd2022-02-17 14:37:06 +08008192#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04008193#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01008194 if (tls_prf == tls_prf_sha256) {
8195 return MBEDTLS_SSL_TLS_PRF_SHA256;
8196 } else
Jerry Yue93ffcd2022-02-17 14:37:06 +08008197#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01008198 return MBEDTLS_SSL_TLS_PRF_NONE;
Jerry Yue93ffcd2022-02-17 14:37:06 +08008199}
8200
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008201/*
8202 * Populate a transform structure with session keys and all the other
8203 * necessary information.
8204 *
8205 * Parameters:
8206 * - [in/out]: transform: structure to populate
8207 * [in] must be just initialised with mbedtls_ssl_transform_init()
8208 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
8209 * - [in] ciphersuite
8210 * - [in] master
8211 * - [in] encrypt_then_mac
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008212 * - [in] tls_prf: pointer to PRF to use for key derivation
8213 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
Glenn Strauss07c64162022-03-14 12:34:51 -04008214 * - [in] tls_version: TLS version
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008215 * - [in] endpoint: client or server
8216 * - [in] ssl: used for:
8217 * - ssl->conf->{f,p}_export_keys
8218 * [in] optionally used for:
8219 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
8220 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02008221MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01008222static int ssl_tls12_populate_transform(mbedtls_ssl_transform *transform,
8223 int ciphersuite,
8224 const unsigned char master[48],
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008225#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01008226 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008227#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01008228 ssl_tls_prf_t tls_prf,
8229 const unsigned char randbytes[64],
8230 mbedtls_ssl_protocol_version tls_version,
8231 unsigned endpoint,
8232 const mbedtls_ssl_context *ssl)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008233{
8234 int ret = 0;
8235 unsigned char keyblk[256];
8236 unsigned char *key1;
8237 unsigned char *key2;
8238 unsigned char *mac_enc;
8239 unsigned char *mac_dec;
8240 size_t mac_key_len = 0;
8241 size_t iv_copy_len;
8242 size_t keylen;
8243 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008244 mbedtls_ssl_mode_t ssl_mode;
Neil Armstronge4512952022-03-08 09:08:22 +01008245#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008246 const mbedtls_cipher_info_t *cipher_info;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008247 const mbedtls_md_info_t *md_info;
Neil Armstronge4512952022-03-08 09:08:22 +01008248#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008249
8250#if defined(MBEDTLS_USE_PSA_CRYPTO)
8251 psa_key_type_t key_type;
8252 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8253 psa_algorithm_t alg;
Neil Armstronge4512952022-03-08 09:08:22 +01008254 psa_algorithm_t mac_alg = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008255 size_t key_bits;
8256 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
8257#endif
8258
8259#if !defined(MBEDTLS_DEBUG_C) && \
8260 !defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Gilles Peskine449bd832023-01-11 14:50:10 +01008261 if (ssl->f_export_keys == NULL) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008262 ssl = NULL; /* make sure we don't use it except for these cases */
8263 (void) ssl;
8264 }
8265#endif
8266
8267 /*
8268 * Some data just needs copying into the structure
8269 */
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008270#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008271 transform->encrypt_then_mac = encrypt_then_mac;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008272#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Glenn Strauss07c64162022-03-14 12:34:51 -04008273 transform->tls_version = tls_version;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008274
8275#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01008276 memcpy(transform->randbytes, randbytes, sizeof(transform->randbytes));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008277#endif
8278
8279#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01008280 if (tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008281 /* At the moment, we keep TLS <= 1.2 and TLS 1.3 transform
8282 * generation separate. This should never happen. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008283 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008284 }
8285#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
8286
8287 /*
8288 * Get various info structures
8289 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008290 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(ciphersuite);
8291 if (ciphersuite_info == NULL) {
8292 MBEDTLS_SSL_DEBUG_MSG(1, ("ciphersuite info for %d not found",
8293 ciphersuite));
8294 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008295 }
8296
Neil Armstrongab555e02022-04-04 11:07:59 +02008297 ssl_mode = mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008298#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01008299 encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008300#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01008301 ciphersuite_info);
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008302
Gilles Peskine449bd832023-01-11 14:50:10 +01008303 if (ssl_mode == MBEDTLS_SSL_MODE_AEAD) {
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008304 transform->taglen =
8305 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Gilles Peskine449bd832023-01-11 14:50:10 +01008306 }
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008307
8308#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008309 if ((status = mbedtls_ssl_cipher_to_psa(ciphersuite_info->cipher,
8310 transform->taglen,
8311 &alg,
8312 &key_type,
8313 &key_bits)) != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05008314 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01008315 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_cipher_to_psa", ret);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008316 goto end;
8317 }
8318#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008319 cipher_info = mbedtls_cipher_info_from_type(ciphersuite_info->cipher);
8320 if (cipher_info == NULL) {
8321 MBEDTLS_SSL_DEBUG_MSG(1, ("cipher info for %u not found",
8322 ciphersuite_info->cipher));
8323 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008324 }
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008325#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008326
Neil Armstronge4512952022-03-08 09:08:22 +01008327#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008328 mac_alg = mbedtls_hash_info_psa_from_md(ciphersuite_info->mac);
8329 if (mac_alg == 0) {
8330 MBEDTLS_SSL_DEBUG_MSG(1, ("mbedtls_hash_info_psa_from_md for %u not found",
8331 (unsigned) ciphersuite_info->mac));
8332 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Neil Armstronge4512952022-03-08 09:08:22 +01008333 }
8334#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008335 md_info = mbedtls_md_info_from_type(ciphersuite_info->mac);
8336 if (md_info == NULL) {
8337 MBEDTLS_SSL_DEBUG_MSG(1, ("mbedtls_md info for %u not found",
8338 (unsigned) ciphersuite_info->mac));
8339 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008340 }
Neil Armstronge4512952022-03-08 09:08:22 +01008341#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008342
8343#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
8344 /* Copy own and peer's CID if the use of the CID
8345 * extension has been negotiated. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008346 if (ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED) {
8347 MBEDTLS_SSL_DEBUG_MSG(3, ("Copy CIDs into SSL transform"));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008348
8349 transform->in_cid_len = ssl->own_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008350 memcpy(transform->in_cid, ssl->own_cid, ssl->own_cid_len);
8351 MBEDTLS_SSL_DEBUG_BUF(3, "Incoming CID", transform->in_cid,
8352 transform->in_cid_len);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008353
8354 transform->out_cid_len = ssl->handshake->peer_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008355 memcpy(transform->out_cid, ssl->handshake->peer_cid,
8356 ssl->handshake->peer_cid_len);
8357 MBEDTLS_SSL_DEBUG_BUF(3, "Outgoing CID", transform->out_cid,
8358 transform->out_cid_len);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008359 }
8360#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
8361
8362 /*
8363 * Compute key block using the PRF
8364 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008365 ret = tls_prf(master, 48, "key expansion", randbytes, 64, keyblk, 256);
8366 if (ret != 0) {
8367 MBEDTLS_SSL_DEBUG_RET(1, "prf", ret);
8368 return ret;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008369 }
8370
Gilles Peskine449bd832023-01-11 14:50:10 +01008371 MBEDTLS_SSL_DEBUG_MSG(3, ("ciphersuite = %s",
8372 mbedtls_ssl_get_ciphersuite_name(ciphersuite)));
8373 MBEDTLS_SSL_DEBUG_BUF(3, "master secret", master, 48);
8374 MBEDTLS_SSL_DEBUG_BUF(4, "random bytes", randbytes, 64);
8375 MBEDTLS_SSL_DEBUG_BUF(4, "key block", keyblk, 256);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008376
8377 /*
8378 * Determine the appropriate key, IV and MAC length.
8379 */
8380
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008381#if defined(MBEDTLS_USE_PSA_CRYPTO)
8382 keylen = PSA_BITS_TO_BYTES(key_bits);
8383#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008384 keylen = mbedtls_cipher_info_get_key_bitlen(cipher_info) / 8;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008385#endif
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008386
8387#if defined(MBEDTLS_GCM_C) || \
8388 defined(MBEDTLS_CCM_C) || \
8389 defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008390 if (ssl_mode == MBEDTLS_SSL_MODE_AEAD) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008391 size_t explicit_ivlen;
8392
8393 transform->maclen = 0;
8394 mac_key_len = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008395
8396 /* All modes haves 96-bit IVs, but the length of the static parts vary
8397 * with mode and version:
8398 * - For GCM and CCM in TLS 1.2, there's a static IV of 4 Bytes
8399 * (to be concatenated with a dynamically chosen IV of 8 Bytes)
8400 * - For ChaChaPoly in TLS 1.2, and all modes in TLS 1.3, there's
8401 * a static IV of 12 Bytes (to be XOR'ed with the 8 Byte record
8402 * sequence number).
8403 */
8404 transform->ivlen = 12;
David Horstmann3b2276a2022-10-06 14:49:08 +01008405
8406 int is_chachapoly = 0;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008407#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008408 is_chachapoly = (key_type == PSA_KEY_TYPE_CHACHA20);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008409#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008410 is_chachapoly = (mbedtls_cipher_info_get_mode(cipher_info)
8411 == MBEDTLS_MODE_CHACHAPOLY);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008412#endif /* MBEDTLS_USE_PSA_CRYPTO */
David Horstmann3b2276a2022-10-06 14:49:08 +01008413
Gilles Peskine449bd832023-01-11 14:50:10 +01008414 if (is_chachapoly) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008415 transform->fixed_ivlen = 12;
Gilles Peskine449bd832023-01-11 14:50:10 +01008416 } else {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008417 transform->fixed_ivlen = 4;
Gilles Peskine449bd832023-01-11 14:50:10 +01008418 }
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008419
8420 /* Minimum length of encrypted record */
8421 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
8422 transform->minlen = explicit_ivlen + transform->taglen;
Gilles Peskine449bd832023-01-11 14:50:10 +01008423 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008424#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
8425#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01008426 if (ssl_mode == MBEDTLS_SSL_MODE_STREAM ||
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008427 ssl_mode == MBEDTLS_SSL_MODE_CBC ||
Gilles Peskine449bd832023-01-11 14:50:10 +01008428 ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM) {
Neil Armstronge4512952022-03-08 09:08:22 +01008429#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008430 size_t block_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008431#else
8432 size_t block_size = cipher_info->block_size;
8433#endif /* MBEDTLS_USE_PSA_CRYPTO */
8434
8435#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronge4512952022-03-08 09:08:22 +01008436 /* Get MAC length */
8437 mac_key_len = PSA_HASH_LENGTH(mac_alg);
8438#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008439 /* Initialize HMAC contexts */
Gilles Peskine449bd832023-01-11 14:50:10 +01008440 if ((ret = mbedtls_md_setup(&transform->md_ctx_enc, md_info, 1)) != 0 ||
8441 (ret = mbedtls_md_setup(&transform->md_ctx_dec, md_info, 1)) != 0) {
8442 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_setup", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008443 goto end;
8444 }
8445
8446 /* Get MAC length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008447 mac_key_len = mbedtls_md_get_size(md_info);
Neil Armstronge4512952022-03-08 09:08:22 +01008448#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008449 transform->maclen = mac_key_len;
8450
8451 /* IV length */
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008452#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008453 transform->ivlen = PSA_CIPHER_IV_LENGTH(key_type, alg);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008454#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008455 transform->ivlen = cipher_info->iv_size;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008456#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008457
8458 /* Minimum length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008459 if (ssl_mode == MBEDTLS_SSL_MODE_STREAM) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008460 transform->minlen = transform->maclen;
Gilles Peskine449bd832023-01-11 14:50:10 +01008461 } else {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008462 /*
8463 * GenericBlockCipher:
8464 * 1. if EtM is in use: one block plus MAC
8465 * otherwise: * first multiple of blocklen greater than maclen
8466 * 2. IV
8467 */
8468#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01008469 if (ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008470 transform->minlen = transform->maclen
Gilles Peskine449bd832023-01-11 14:50:10 +01008471 + block_size;
8472 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008473#endif
8474 {
8475 transform->minlen = transform->maclen
Gilles Peskine449bd832023-01-11 14:50:10 +01008476 + block_size
8477 - transform->maclen % block_size;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008478 }
8479
Gilles Peskine449bd832023-01-11 14:50:10 +01008480 if (tls_version == MBEDTLS_SSL_VERSION_TLS1_2) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008481 transform->minlen += transform->ivlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01008482 } else {
8483 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008484 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
8485 goto end;
8486 }
8487 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008488 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008489#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
8490 {
Gilles Peskine449bd832023-01-11 14:50:10 +01008491 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
8492 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008493 }
8494
Gilles Peskine449bd832023-01-11 14:50:10 +01008495 MBEDTLS_SSL_DEBUG_MSG(3, ("keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
8496 (unsigned) keylen,
8497 (unsigned) transform->minlen,
8498 (unsigned) transform->ivlen,
8499 (unsigned) transform->maclen));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008500
8501 /*
8502 * Finally setup the cipher contexts, IVs and MAC secrets.
8503 */
8504#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008505 if (endpoint == MBEDTLS_SSL_IS_CLIENT) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008506 key1 = keyblk + mac_key_len * 2;
8507 key2 = keyblk + mac_key_len * 2 + keylen;
8508
8509 mac_enc = keyblk;
8510 mac_dec = keyblk + mac_key_len;
8511
Gilles Peskine449bd832023-01-11 14:50:10 +01008512 iv_copy_len = (transform->fixed_ivlen) ?
8513 transform->fixed_ivlen : transform->ivlen;
8514 memcpy(transform->iv_enc, key2 + keylen, iv_copy_len);
8515 memcpy(transform->iv_dec, key2 + keylen + iv_copy_len,
8516 iv_copy_len);
8517 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008518#endif /* MBEDTLS_SSL_CLI_C */
8519#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008520 if (endpoint == MBEDTLS_SSL_IS_SERVER) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008521 key1 = keyblk + mac_key_len * 2 + keylen;
8522 key2 = keyblk + mac_key_len * 2;
8523
8524 mac_enc = keyblk + mac_key_len;
8525 mac_dec = keyblk;
8526
Gilles Peskine449bd832023-01-11 14:50:10 +01008527 iv_copy_len = (transform->fixed_ivlen) ?
8528 transform->fixed_ivlen : transform->ivlen;
8529 memcpy(transform->iv_dec, key1 + keylen, iv_copy_len);
8530 memcpy(transform->iv_enc, key1 + keylen + iv_copy_len,
8531 iv_copy_len);
8532 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008533#endif /* MBEDTLS_SSL_SRV_C */
8534 {
Gilles Peskine449bd832023-01-11 14:50:10 +01008535 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008536 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
8537 goto end;
8538 }
8539
Gilles Peskine449bd832023-01-11 14:50:10 +01008540 if (ssl != NULL && ssl->f_export_keys != NULL) {
8541 ssl->f_export_keys(ssl->p_export_keys,
8542 MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET,
8543 master, 48,
8544 randbytes + 32,
8545 randbytes,
8546 tls_prf_get_type(tls_prf));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008547 }
8548
8549#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008550 transform->psa_alg = alg;
8551
Gilles Peskine449bd832023-01-11 14:50:10 +01008552 if (alg != MBEDTLS_SSL_NULL_CIPHER) {
8553 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
8554 psa_set_key_algorithm(&attributes, alg);
8555 psa_set_key_type(&attributes, key_type);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008556
Gilles Peskine449bd832023-01-11 14:50:10 +01008557 if ((status = psa_import_key(&attributes,
8558 key1,
8559 PSA_BITS_TO_BYTES(key_bits),
8560 &transform->psa_key_enc)) != PSA_SUCCESS) {
8561 MBEDTLS_SSL_DEBUG_RET(3, "psa_import_key", (int) status);
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05008562 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01008563 MBEDTLS_SSL_DEBUG_RET(1, "psa_import_key", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008564 goto end;
8565 }
8566
Gilles Peskine449bd832023-01-11 14:50:10 +01008567 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008568
Gilles Peskine449bd832023-01-11 14:50:10 +01008569 if ((status = psa_import_key(&attributes,
8570 key2,
8571 PSA_BITS_TO_BYTES(key_bits),
8572 &transform->psa_key_dec)) != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05008573 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01008574 MBEDTLS_SSL_DEBUG_RET(1, "psa_import_key", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008575 goto end;
8576 }
8577 }
8578#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008579 if ((ret = mbedtls_cipher_setup(&transform->cipher_ctx_enc,
8580 cipher_info)) != 0) {
8581 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008582 goto end;
8583 }
8584
Gilles Peskine449bd832023-01-11 14:50:10 +01008585 if ((ret = mbedtls_cipher_setup(&transform->cipher_ctx_dec,
8586 cipher_info)) != 0) {
8587 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008588 goto end;
8589 }
8590
Gilles Peskine449bd832023-01-11 14:50:10 +01008591 if ((ret = mbedtls_cipher_setkey(&transform->cipher_ctx_enc, key1,
8592 (int) mbedtls_cipher_info_get_key_bitlen(cipher_info),
8593 MBEDTLS_ENCRYPT)) != 0) {
8594 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setkey", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008595 goto end;
8596 }
8597
Gilles Peskine449bd832023-01-11 14:50:10 +01008598 if ((ret = mbedtls_cipher_setkey(&transform->cipher_ctx_dec, key2,
8599 (int) mbedtls_cipher_info_get_key_bitlen(cipher_info),
8600 MBEDTLS_DECRYPT)) != 0) {
8601 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setkey", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008602 goto end;
8603 }
8604
8605#if defined(MBEDTLS_CIPHER_MODE_CBC)
Gilles Peskine449bd832023-01-11 14:50:10 +01008606 if (mbedtls_cipher_info_get_mode(cipher_info) == MBEDTLS_MODE_CBC) {
8607 if ((ret = mbedtls_cipher_set_padding_mode(&transform->cipher_ctx_enc,
8608 MBEDTLS_PADDING_NONE)) != 0) {
8609 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_set_padding_mode", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008610 goto end;
8611 }
8612
Gilles Peskine449bd832023-01-11 14:50:10 +01008613 if ((ret = mbedtls_cipher_set_padding_mode(&transform->cipher_ctx_dec,
8614 MBEDTLS_PADDING_NONE)) != 0) {
8615 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_set_padding_mode", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008616 goto end;
8617 }
8618 }
8619#endif /* MBEDTLS_CIPHER_MODE_CBC */
8620#endif /* MBEDTLS_USE_PSA_CRYPTO */
8621
Neil Armstrong29c0c042022-03-17 17:47:28 +01008622#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
8623 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
8624 For AEAD-based ciphersuites, there is nothing to do here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008625 if (mac_key_len != 0) {
Neil Armstrong29c0c042022-03-17 17:47:28 +01008626#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008627 transform->psa_mac_alg = PSA_ALG_HMAC(mac_alg);
Neil Armstrong29c0c042022-03-17 17:47:28 +01008628
Gilles Peskine449bd832023-01-11 14:50:10 +01008629 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
8630 psa_set_key_algorithm(&attributes, PSA_ALG_HMAC(mac_alg));
8631 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
Neil Armstrong29c0c042022-03-17 17:47:28 +01008632
Gilles Peskine449bd832023-01-11 14:50:10 +01008633 if ((status = psa_import_key(&attributes,
8634 mac_enc, mac_key_len,
8635 &transform->psa_mac_enc)) != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05008636 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01008637 MBEDTLS_SSL_DEBUG_RET(1, "psa_import_mac_key", ret);
Neil Armstrong29c0c042022-03-17 17:47:28 +01008638 goto end;
8639 }
8640
Gilles Peskine449bd832023-01-11 14:50:10 +01008641 if ((transform->psa_alg == MBEDTLS_SSL_NULL_CIPHER) ||
8642 ((transform->psa_alg == PSA_ALG_CBC_NO_PADDING)
Andrzej Kurek8c95ac42022-08-17 16:17:00 -04008643#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01008644 && (transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED)
Andrzej Kurek8c95ac42022-08-17 16:17:00 -04008645#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01008646 )) {
Neil Armstrong29c0c042022-03-17 17:47:28 +01008647 /* mbedtls_ct_hmac() requires the key to be exportable */
Gilles Peskine449bd832023-01-11 14:50:10 +01008648 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT |
8649 PSA_KEY_USAGE_VERIFY_HASH);
8650 } else {
8651 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
8652 }
Neil Armstrong29c0c042022-03-17 17:47:28 +01008653
Gilles Peskine449bd832023-01-11 14:50:10 +01008654 if ((status = psa_import_key(&attributes,
8655 mac_dec, mac_key_len,
8656 &transform->psa_mac_dec)) != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05008657 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01008658 MBEDTLS_SSL_DEBUG_RET(1, "psa_import_mac_key", ret);
Neil Armstrong29c0c042022-03-17 17:47:28 +01008659 goto end;
8660 }
8661#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008662 ret = mbedtls_md_hmac_starts(&transform->md_ctx_enc, mac_enc, mac_key_len);
8663 if (ret != 0) {
Neil Armstrong29c0c042022-03-17 17:47:28 +01008664 goto end;
Gilles Peskine449bd832023-01-11 14:50:10 +01008665 }
8666 ret = mbedtls_md_hmac_starts(&transform->md_ctx_dec, mac_dec, mac_key_len);
8667 if (ret != 0) {
Neil Armstrong29c0c042022-03-17 17:47:28 +01008668 goto end;
Gilles Peskine449bd832023-01-11 14:50:10 +01008669 }
Neil Armstrong29c0c042022-03-17 17:47:28 +01008670#endif /* MBEDTLS_USE_PSA_CRYPTO */
8671 }
8672#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
8673
8674 ((void) mac_dec);
8675 ((void) mac_enc);
8676
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008677end:
Gilles Peskine449bd832023-01-11 14:50:10 +01008678 mbedtls_platform_zeroize(keyblk, sizeof(keyblk));
8679 return ret;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008680}
8681
Valerio Settia08b1a42022-11-17 15:10:02 +01008682#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) && \
8683 defined(MBEDTLS_USE_PSA_CRYPTO)
Valerio Setti6b3dab02022-11-17 17:14:54 +01008684int mbedtls_psa_ecjpake_read_round(
Gilles Peskine449bd832023-01-11 14:50:10 +01008685 psa_pake_operation_t *pake_ctx,
8686 const unsigned char *buf,
8687 size_t len, mbedtls_ecjpake_rounds_t round)
Valerio Settia08b1a42022-11-17 15:10:02 +01008688{
8689 psa_status_t status;
8690 size_t input_offset = 0;
Valerio Setti819de862022-11-17 18:05:19 +01008691 /*
Valerio Setti6b3dab02022-11-17 17:14:54 +01008692 * At round one repeat the KEY_SHARE, ZK_PUBLIC & ZF_PROOF twice
8693 * At round two perform a single cycle
8694 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008695 unsigned int remaining_steps = (round == MBEDTLS_ECJPAKE_ROUND_ONE) ? 2 : 1;
Valerio Settia08b1a42022-11-17 15:10:02 +01008696
Gilles Peskine449bd832023-01-11 14:50:10 +01008697 for (; remaining_steps > 0; remaining_steps--) {
8698 for (psa_pake_step_t step = PSA_PAKE_STEP_KEY_SHARE;
Valerio Settia08b1a42022-11-17 15:10:02 +01008699 step <= PSA_PAKE_STEP_ZK_PROOF;
Gilles Peskine449bd832023-01-11 14:50:10 +01008700 ++step) {
Valerio Settia08b1a42022-11-17 15:10:02 +01008701 /* Length is stored at the first byte */
8702 size_t length = buf[input_offset];
8703 input_offset += 1;
8704
Gilles Peskine449bd832023-01-11 14:50:10 +01008705 if (input_offset + length > len) {
Valerio Settia08b1a42022-11-17 15:10:02 +01008706 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
8707 }
8708
Gilles Peskine449bd832023-01-11 14:50:10 +01008709 status = psa_pake_input(pake_ctx, step,
8710 buf + input_offset, length);
8711 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05008712 return PSA_TO_MBEDTLS_ERR(status);
Valerio Settia08b1a42022-11-17 15:10:02 +01008713 }
8714
8715 input_offset += length;
8716 }
8717 }
8718
Gilles Peskine449bd832023-01-11 14:50:10 +01008719 if (input_offset != len) {
Valerio Setti61ea17d2022-11-18 12:11:00 +01008720 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01008721 }
Valerio Setti30ebe112022-11-17 16:23:34 +01008722
Gilles Peskine449bd832023-01-11 14:50:10 +01008723 return 0;
Valerio Settia08b1a42022-11-17 15:10:02 +01008724}
8725
Valerio Setti6b3dab02022-11-17 17:14:54 +01008726int mbedtls_psa_ecjpake_write_round(
Gilles Peskine449bd832023-01-11 14:50:10 +01008727 psa_pake_operation_t *pake_ctx,
8728 unsigned char *buf,
8729 size_t len, size_t *olen,
8730 mbedtls_ecjpake_rounds_t round)
Valerio Settia08b1a42022-11-17 15:10:02 +01008731{
8732 psa_status_t status;
8733 size_t output_offset = 0;
8734 size_t output_len;
Valerio Setti819de862022-11-17 18:05:19 +01008735 /*
Valerio Setti6b3dab02022-11-17 17:14:54 +01008736 * At round one repeat the KEY_SHARE, ZK_PUBLIC & ZF_PROOF twice
8737 * At round two perform a single cycle
8738 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008739 unsigned int remaining_steps = (round == MBEDTLS_ECJPAKE_ROUND_ONE) ? 2 : 1;
Valerio Settia08b1a42022-11-17 15:10:02 +01008740
Gilles Peskine449bd832023-01-11 14:50:10 +01008741 for (; remaining_steps > 0; remaining_steps--) {
8742 for (psa_pake_step_t step = PSA_PAKE_STEP_KEY_SHARE;
8743 step <= PSA_PAKE_STEP_ZK_PROOF;
8744 ++step) {
Valerio Setti79f6b6b2022-11-21 14:17:03 +01008745 /*
Valerio Settid4a9b1a2022-11-22 11:11:10 +01008746 * For each step, prepend 1 byte with the length of the data as
8747 * given by psa_pake_output().
Valerio Setti79f6b6b2022-11-21 14:17:03 +01008748 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008749 status = psa_pake_output(pake_ctx, step,
8750 buf + output_offset + 1,
8751 len - output_offset - 1,
8752 &output_len);
8753 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05008754 return PSA_TO_MBEDTLS_ERR(status);
Valerio Settia08b1a42022-11-17 15:10:02 +01008755 }
8756
Valerio Setti99d88c12022-11-22 16:03:43 +01008757 *(buf + output_offset) = (uint8_t) output_len;
Valerio Setti79f6b6b2022-11-21 14:17:03 +01008758
8759 output_offset += output_len + 1;
Valerio Settia08b1a42022-11-17 15:10:02 +01008760 }
8761 }
8762
8763 *olen = output_offset;
8764
Gilles Peskine449bd832023-01-11 14:50:10 +01008765 return 0;
Valerio Settia08b1a42022-11-17 15:10:02 +01008766}
Valerio Settia08b1a42022-11-17 15:10:02 +01008767#endif //MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED && MBEDTLS_USE_PSA_CRYPTO
8768
Jerry Yuee40f9d2022-02-17 14:55:16 +08008769#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008770int mbedtls_ssl_get_key_exchange_md_tls1_2(mbedtls_ssl_context *ssl,
8771 unsigned char *hash, size_t *hashlen,
8772 unsigned char *data, size_t data_len,
8773 mbedtls_md_type_t md_alg)
Jerry Yuee40f9d2022-02-17 14:55:16 +08008774{
8775 psa_status_t status;
8776 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01008777 psa_algorithm_t hash_alg = mbedtls_hash_info_psa_from_md(md_alg);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008778
Gilles Peskine449bd832023-01-11 14:50:10 +01008779 MBEDTLS_SSL_DEBUG_MSG(3, ("Perform PSA-based computation of digest of ServerKeyExchange"));
Jerry Yuee40f9d2022-02-17 14:55:16 +08008780
Gilles Peskine449bd832023-01-11 14:50:10 +01008781 if ((status = psa_hash_setup(&hash_operation,
8782 hash_alg)) != PSA_SUCCESS) {
8783 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_setup", status);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008784 goto exit;
8785 }
8786
Gilles Peskine449bd832023-01-11 14:50:10 +01008787 if ((status = psa_hash_update(&hash_operation, ssl->handshake->randbytes,
8788 64)) != PSA_SUCCESS) {
8789 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_update", status);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008790 goto exit;
8791 }
8792
Gilles Peskine449bd832023-01-11 14:50:10 +01008793 if ((status = psa_hash_update(&hash_operation,
8794 data, data_len)) != PSA_SUCCESS) {
8795 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_update", status);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008796 goto exit;
8797 }
8798
Gilles Peskine449bd832023-01-11 14:50:10 +01008799 if ((status = psa_hash_finish(&hash_operation, hash, PSA_HASH_MAX_SIZE,
8800 hashlen)) != PSA_SUCCESS) {
8801 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_finish", status);
8802 goto exit;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008803 }
8804
8805exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008806 if (status != PSA_SUCCESS) {
8807 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8808 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
8809 switch (status) {
Jerry Yuee40f9d2022-02-17 14:55:16 +08008810 case PSA_ERROR_NOT_SUPPORTED:
Gilles Peskine449bd832023-01-11 14:50:10 +01008811 return MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008812 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
8813 case PSA_ERROR_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +01008814 return MBEDTLS_ERR_MD_BAD_INPUT_DATA;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008815 case PSA_ERROR_INSUFFICIENT_MEMORY:
Gilles Peskine449bd832023-01-11 14:50:10 +01008816 return MBEDTLS_ERR_MD_ALLOC_FAILED;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008817 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01008818 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008819 }
8820 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008821 return 0;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008822}
8823
8824#else
8825
Gilles Peskine449bd832023-01-11 14:50:10 +01008826int mbedtls_ssl_get_key_exchange_md_tls1_2(mbedtls_ssl_context *ssl,
8827 unsigned char *hash, size_t *hashlen,
8828 unsigned char *data, size_t data_len,
8829 mbedtls_md_type_t md_alg)
Jerry Yuee40f9d2022-02-17 14:55:16 +08008830{
8831 int ret = 0;
8832 mbedtls_md_context_t ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +01008833 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type(md_alg);
8834 *hashlen = mbedtls_md_get_size(md_info);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008835
Gilles Peskine449bd832023-01-11 14:50:10 +01008836 MBEDTLS_SSL_DEBUG_MSG(3, ("Perform mbedtls-based computation of digest of ServerKeyExchange"));
Jerry Yuee40f9d2022-02-17 14:55:16 +08008837
Gilles Peskine449bd832023-01-11 14:50:10 +01008838 mbedtls_md_init(&ctx);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008839
8840 /*
8841 * digitally-signed struct {
8842 * opaque client_random[32];
8843 * opaque server_random[32];
8844 * ServerDHParams params;
8845 * };
8846 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008847 if ((ret = mbedtls_md_setup(&ctx, md_info, 0)) != 0) {
8848 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_setup", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008849 goto exit;
8850 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008851 if ((ret = mbedtls_md_starts(&ctx)) != 0) {
8852 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_starts", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008853 goto exit;
8854 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008855 if ((ret = mbedtls_md_update(&ctx, ssl->handshake->randbytes, 64)) != 0) {
8856 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_update", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008857 goto exit;
8858 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008859 if ((ret = mbedtls_md_update(&ctx, data, data_len)) != 0) {
8860 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_update", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008861 goto exit;
8862 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008863 if ((ret = mbedtls_md_finish(&ctx, hash)) != 0) {
8864 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_finish", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008865 goto exit;
8866 }
8867
8868exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008869 mbedtls_md_free(&ctx);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008870
Gilles Peskine449bd832023-01-11 14:50:10 +01008871 if (ret != 0) {
8872 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8873 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
8874 }
Jerry Yuee40f9d2022-02-17 14:55:16 +08008875
Gilles Peskine449bd832023-01-11 14:50:10 +01008876 return ret;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008877}
8878#endif /* MBEDTLS_USE_PSA_CRYPTO */
8879
Jerry Yud9d91da2022-02-17 14:57:06 +08008880#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
8881
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008882/* Find the preferred hash for a given signature algorithm. */
8883unsigned int mbedtls_ssl_tls12_get_preferred_hash_for_sig_alg(
Gilles Peskine449bd832023-01-11 14:50:10 +01008884 mbedtls_ssl_context *ssl,
8885 unsigned int sig_alg)
Jerry Yud9d91da2022-02-17 14:57:06 +08008886{
Gabor Mezei078e8032022-04-27 21:17:56 +02008887 unsigned int i;
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008888 uint16_t *received_sig_algs = ssl->handshake->received_sig_algs;
Gabor Mezei078e8032022-04-27 21:17:56 +02008889
Gilles Peskine449bd832023-01-11 14:50:10 +01008890 if (sig_alg == MBEDTLS_SSL_SIG_ANON) {
8891 return MBEDTLS_SSL_HASH_NONE;
8892 }
Gabor Mezei078e8032022-04-27 21:17:56 +02008893
Gilles Peskine449bd832023-01-11 14:50:10 +01008894 for (i = 0; received_sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++) {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008895 unsigned int hash_alg_received =
Gilles Peskine449bd832023-01-11 14:50:10 +01008896 MBEDTLS_SSL_TLS12_HASH_ALG_FROM_SIG_AND_HASH_ALG(
8897 received_sig_algs[i]);
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008898 unsigned int sig_alg_received =
Gilles Peskine449bd832023-01-11 14:50:10 +01008899 MBEDTLS_SSL_TLS12_SIG_ALG_FROM_SIG_AND_HASH_ALG(
8900 received_sig_algs[i]);
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008901
Gilles Peskine449bd832023-01-11 14:50:10 +01008902 if (sig_alg == sig_alg_received) {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008903#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008904 if (ssl->handshake->key_cert && ssl->handshake->key_cert->key) {
Neil Armstrong96eceb82022-06-30 18:05:05 +02008905 psa_algorithm_t psa_hash_alg =
Gilles Peskine449bd832023-01-11 14:50:10 +01008906 mbedtls_hash_info_psa_from_md(hash_alg_received);
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008907
Gilles Peskine449bd832023-01-11 14:50:10 +01008908 if (sig_alg_received == MBEDTLS_SSL_SIG_ECDSA &&
8909 !mbedtls_pk_can_do_ext(ssl->handshake->key_cert->key,
8910 PSA_ALG_ECDSA(psa_hash_alg),
8911 PSA_KEY_USAGE_SIGN_HASH)) {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008912 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01008913 }
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008914
Gilles Peskine449bd832023-01-11 14:50:10 +01008915 if (sig_alg_received == MBEDTLS_SSL_SIG_RSA &&
8916 !mbedtls_pk_can_do_ext(ssl->handshake->key_cert->key,
8917 PSA_ALG_RSA_PKCS1V15_SIGN(
8918 psa_hash_alg),
8919 PSA_KEY_USAGE_SIGN_HASH)) {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008920 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01008921 }
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008922 }
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008923#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong96eceb82022-06-30 18:05:05 +02008924
Gilles Peskine449bd832023-01-11 14:50:10 +01008925 return hash_alg_received;
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008926 }
Jerry Yud9d91da2022-02-17 14:57:06 +08008927 }
Jerry Yud9d91da2022-02-17 14:57:06 +08008928
Gilles Peskine449bd832023-01-11 14:50:10 +01008929 return MBEDTLS_SSL_HASH_NONE;
Jerry Yud9d91da2022-02-17 14:57:06 +08008930}
8931
8932#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
8933
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008934/* Serialization of TLS 1.2 sessions:
8935 *
8936 * struct {
8937 * uint64 start_time;
8938 * uint8 ciphersuite[2]; // defined by the standard
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008939 * uint8 session_id_len; // at most 32
8940 * opaque session_id[32];
8941 * opaque master[48]; // fixed length in the standard
8942 * uint32 verify_result;
8943 * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert
8944 * opaque ticket<0..2^24-1>; // length 0 means no ticket
8945 * uint32 ticket_lifetime;
8946 * uint8 mfl_code; // up to 255 according to standard
8947 * uint8 encrypt_then_mac; // 0 or 1
8948 * } serialized_session_tls12;
8949 *
8950 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008951static size_t ssl_tls12_session_save(const mbedtls_ssl_session *session,
8952 unsigned char *buf,
8953 size_t buf_len)
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008954{
8955 unsigned char *p = buf;
8956 size_t used = 0;
8957
8958#if defined(MBEDTLS_HAVE_TIME)
8959 uint64_t start;
8960#endif
8961#if defined(MBEDTLS_X509_CRT_PARSE_C)
8962#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8963 size_t cert_len;
8964#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8965#endif /* MBEDTLS_X509_CRT_PARSE_C */
8966
8967 /*
8968 * Time
8969 */
8970#if defined(MBEDTLS_HAVE_TIME)
8971 used += 8;
8972
Gilles Peskine449bd832023-01-11 14:50:10 +01008973 if (used <= buf_len) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008974 start = (uint64_t) session->start;
8975
Gilles Peskine449bd832023-01-11 14:50:10 +01008976 MBEDTLS_PUT_UINT64_BE(start, p, 0);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008977 p += 8;
8978 }
8979#endif /* MBEDTLS_HAVE_TIME */
8980
8981 /*
8982 * Basic mandatory fields
8983 */
8984 used += 2 /* ciphersuite */
Gilles Peskine449bd832023-01-11 14:50:10 +01008985 + 1 /* id_len */
8986 + sizeof(session->id)
8987 + sizeof(session->master)
8988 + 4; /* verify_result */
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008989
Gilles Peskine449bd832023-01-11 14:50:10 +01008990 if (used <= buf_len) {
8991 MBEDTLS_PUT_UINT16_BE(session->ciphersuite, p, 0);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008992 p += 2;
8993
Gilles Peskine449bd832023-01-11 14:50:10 +01008994 *p++ = MBEDTLS_BYTE_0(session->id_len);
8995 memcpy(p, session->id, 32);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008996 p += 32;
8997
Gilles Peskine449bd832023-01-11 14:50:10 +01008998 memcpy(p, session->master, 48);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008999 p += 48;
9000
Gilles Peskine449bd832023-01-11 14:50:10 +01009001 MBEDTLS_PUT_UINT32_BE(session->verify_result, p, 0);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009002 p += 4;
9003 }
9004
9005 /*
9006 * Peer's end-entity certificate
9007 */
9008#if defined(MBEDTLS_X509_CRT_PARSE_C)
9009#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01009010 if (session->peer_cert == NULL) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009011 cert_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01009012 } else {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009013 cert_len = session->peer_cert->raw.len;
Gilles Peskine449bd832023-01-11 14:50:10 +01009014 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009015
9016 used += 3 + cert_len;
9017
Gilles Peskine449bd832023-01-11 14:50:10 +01009018 if (used <= buf_len) {
9019 *p++ = MBEDTLS_BYTE_2(cert_len);
9020 *p++ = MBEDTLS_BYTE_1(cert_len);
9021 *p++ = MBEDTLS_BYTE_0(cert_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009022
Gilles Peskine449bd832023-01-11 14:50:10 +01009023 if (session->peer_cert != NULL) {
9024 memcpy(p, session->peer_cert->raw.p, cert_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009025 p += cert_len;
9026 }
9027 }
9028#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009029 if (session->peer_cert_digest != NULL) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009030 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01009031 if (used <= buf_len) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009032 *p++ = (unsigned char) session->peer_cert_digest_type;
9033 *p++ = (unsigned char) session->peer_cert_digest_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01009034 memcpy(p, session->peer_cert_digest,
9035 session->peer_cert_digest_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009036 p += session->peer_cert_digest_len;
9037 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009038 } else {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009039 used += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01009040 if (used <= buf_len) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009041 *p++ = (unsigned char) MBEDTLS_MD_NONE;
9042 *p++ = 0;
9043 }
9044 }
9045#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9046#endif /* MBEDTLS_X509_CRT_PARSE_C */
9047
9048 /*
9049 * Session ticket if any, plus associated data
9050 */
9051#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
9052 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
9053
Gilles Peskine449bd832023-01-11 14:50:10 +01009054 if (used <= buf_len) {
9055 *p++ = MBEDTLS_BYTE_2(session->ticket_len);
9056 *p++ = MBEDTLS_BYTE_1(session->ticket_len);
9057 *p++ = MBEDTLS_BYTE_0(session->ticket_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009058
Gilles Peskine449bd832023-01-11 14:50:10 +01009059 if (session->ticket != NULL) {
9060 memcpy(p, session->ticket, session->ticket_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009061 p += session->ticket_len;
9062 }
9063
Gilles Peskine449bd832023-01-11 14:50:10 +01009064 MBEDTLS_PUT_UINT32_BE(session->ticket_lifetime, p, 0);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009065 p += 4;
9066 }
9067#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
9068
9069 /*
9070 * Misc extension-related info
9071 */
9072#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9073 used += 1;
9074
Gilles Peskine449bd832023-01-11 14:50:10 +01009075 if (used <= buf_len) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009076 *p++ = session->mfl_code;
Gilles Peskine449bd832023-01-11 14:50:10 +01009077 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009078#endif
9079
9080#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
9081 used += 1;
9082
Gilles Peskine449bd832023-01-11 14:50:10 +01009083 if (used <= buf_len) {
9084 *p++ = MBEDTLS_BYTE_0(session->encrypt_then_mac);
9085 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009086#endif
9087
Gilles Peskine449bd832023-01-11 14:50:10 +01009088 return used;
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009089}
9090
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02009091MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01009092static int ssl_tls12_session_load(mbedtls_ssl_session *session,
9093 const unsigned char *buf,
9094 size_t len)
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009095{
9096#if defined(MBEDTLS_HAVE_TIME)
9097 uint64_t start;
9098#endif
9099#if defined(MBEDTLS_X509_CRT_PARSE_C)
9100#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
9101 size_t cert_len;
9102#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9103#endif /* MBEDTLS_X509_CRT_PARSE_C */
9104
9105 const unsigned char *p = buf;
9106 const unsigned char * const end = buf + len;
9107
9108 /*
9109 * Time
9110 */
9111#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01009112 if (8 > (size_t) (end - p)) {
9113 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9114 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009115
Gilles Peskine449bd832023-01-11 14:50:10 +01009116 start = ((uint64_t) p[0] << 56) |
9117 ((uint64_t) p[1] << 48) |
9118 ((uint64_t) p[2] << 40) |
9119 ((uint64_t) p[3] << 32) |
9120 ((uint64_t) p[4] << 24) |
9121 ((uint64_t) p[5] << 16) |
9122 ((uint64_t) p[6] << 8) |
9123 ((uint64_t) p[7]);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009124 p += 8;
9125
9126 session->start = (time_t) start;
9127#endif /* MBEDTLS_HAVE_TIME */
9128
9129 /*
9130 * Basic mandatory fields
9131 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009132 if (2 + 1 + 32 + 48 + 4 > (size_t) (end - p)) {
9133 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9134 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009135
Gilles Peskine449bd832023-01-11 14:50:10 +01009136 session->ciphersuite = (p[0] << 8) | p[1];
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009137 p += 2;
9138
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009139 session->id_len = *p++;
Gilles Peskine449bd832023-01-11 14:50:10 +01009140 memcpy(session->id, p, 32);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009141 p += 32;
9142
Gilles Peskine449bd832023-01-11 14:50:10 +01009143 memcpy(session->master, p, 48);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009144 p += 48;
9145
Gilles Peskine449bd832023-01-11 14:50:10 +01009146 session->verify_result = ((uint32_t) p[0] << 24) |
9147 ((uint32_t) p[1] << 16) |
9148 ((uint32_t) p[2] << 8) |
9149 ((uint32_t) p[3]);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009150 p += 4;
9151
9152 /* Immediately clear invalid pointer values that have been read, in case
9153 * we exit early before we replaced them with valid ones. */
9154#if defined(MBEDTLS_X509_CRT_PARSE_C)
9155#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
9156 session->peer_cert = NULL;
9157#else
9158 session->peer_cert_digest = NULL;
9159#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9160#endif /* MBEDTLS_X509_CRT_PARSE_C */
9161#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
9162 session->ticket = NULL;
9163#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
9164
9165 /*
9166 * Peer certificate
9167 */
9168#if defined(MBEDTLS_X509_CRT_PARSE_C)
9169#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
9170 /* Deserialize CRT from the end of the ticket. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009171 if (3 > (size_t) (end - p)) {
9172 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9173 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009174
Gilles Peskine449bd832023-01-11 14:50:10 +01009175 cert_len = (p[0] << 16) | (p[1] << 8) | p[2];
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009176 p += 3;
9177
Gilles Peskine449bd832023-01-11 14:50:10 +01009178 if (cert_len != 0) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009179 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
9180
Gilles Peskine449bd832023-01-11 14:50:10 +01009181 if (cert_len > (size_t) (end - p)) {
9182 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9183 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009184
Gilles Peskine449bd832023-01-11 14:50:10 +01009185 session->peer_cert = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009186
Gilles Peskine449bd832023-01-11 14:50:10 +01009187 if (session->peer_cert == NULL) {
9188 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
9189 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009190
Gilles Peskine449bd832023-01-11 14:50:10 +01009191 mbedtls_x509_crt_init(session->peer_cert);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009192
Gilles Peskine449bd832023-01-11 14:50:10 +01009193 if ((ret = mbedtls_x509_crt_parse_der(session->peer_cert,
9194 p, cert_len)) != 0) {
9195 mbedtls_x509_crt_free(session->peer_cert);
9196 mbedtls_free(session->peer_cert);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009197 session->peer_cert = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01009198 return ret;
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009199 }
9200
9201 p += cert_len;
9202 }
9203#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9204 /* Deserialize CRT digest from the end of the ticket. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009205 if (2 > (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_type = (mbedtls_md_type_t) *p++;
9210 session->peer_cert_digest_len = (size_t) *p++;
9211
Gilles Peskine449bd832023-01-11 14:50:10 +01009212 if (session->peer_cert_digest_len != 0) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009213 const mbedtls_md_info_t *md_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01009214 mbedtls_md_info_from_type(session->peer_cert_digest_type);
9215 if (md_info == NULL) {
9216 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9217 }
9218 if (session->peer_cert_digest_len != mbedtls_md_get_size(md_info)) {
9219 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9220 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009221
Gilles Peskine449bd832023-01-11 14:50:10 +01009222 if (session->peer_cert_digest_len > (size_t) (end - p)) {
9223 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9224 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009225
9226 session->peer_cert_digest =
Gilles Peskine449bd832023-01-11 14:50:10 +01009227 mbedtls_calloc(1, session->peer_cert_digest_len);
9228 if (session->peer_cert_digest == NULL) {
9229 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
9230 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009231
Gilles Peskine449bd832023-01-11 14:50:10 +01009232 memcpy(session->peer_cert_digest, p,
9233 session->peer_cert_digest_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009234 p += session->peer_cert_digest_len;
9235 }
9236#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9237#endif /* MBEDTLS_X509_CRT_PARSE_C */
9238
9239 /*
9240 * Session ticket and associated data
9241 */
9242#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01009243 if (3 > (size_t) (end - p)) {
9244 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9245 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009246
Gilles Peskine449bd832023-01-11 14:50:10 +01009247 session->ticket_len = (p[0] << 16) | (p[1] << 8) | p[2];
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009248 p += 3;
9249
Gilles Peskine449bd832023-01-11 14:50:10 +01009250 if (session->ticket_len != 0) {
9251 if (session->ticket_len > (size_t) (end - p)) {
9252 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9253 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009254
Gilles Peskine449bd832023-01-11 14:50:10 +01009255 session->ticket = mbedtls_calloc(1, session->ticket_len);
9256 if (session->ticket == NULL) {
9257 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
9258 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009259
Gilles Peskine449bd832023-01-11 14:50:10 +01009260 memcpy(session->ticket, p, session->ticket_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009261 p += session->ticket_len;
9262 }
9263
Gilles Peskine449bd832023-01-11 14:50:10 +01009264 if (4 > (size_t) (end - p)) {
9265 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9266 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009267
Gilles Peskine449bd832023-01-11 14:50:10 +01009268 session->ticket_lifetime = ((uint32_t) p[0] << 24) |
9269 ((uint32_t) p[1] << 16) |
9270 ((uint32_t) p[2] << 8) |
9271 ((uint32_t) p[3]);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009272 p += 4;
9273#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
9274
9275 /*
9276 * Misc extension-related info
9277 */
9278#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01009279 if (1 > (size_t) (end - p)) {
9280 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9281 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009282
9283 session->mfl_code = *p++;
9284#endif
9285
9286#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01009287 if (1 > (size_t) (end - p)) {
9288 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9289 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009290
9291 session->encrypt_then_mac = *p++;
9292#endif
9293
9294 /* Done, should have consumed entire buffer */
Gilles Peskine449bd832023-01-11 14:50:10 +01009295 if (p != end) {
9296 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9297 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009298
Gilles Peskine449bd832023-01-11 14:50:10 +01009299 return 0;
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009300}
Jerry Yudc7bd172022-02-17 13:44:15 +08009301#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9302
XiaokangQian75d40ef2022-04-20 11:05:24 +00009303int mbedtls_ssl_validate_ciphersuite(
9304 const mbedtls_ssl_context *ssl,
9305 const mbedtls_ssl_ciphersuite_t *suite_info,
9306 mbedtls_ssl_protocol_version min_tls_version,
Gilles Peskine449bd832023-01-11 14:50:10 +01009307 mbedtls_ssl_protocol_version max_tls_version)
XiaokangQian75d40ef2022-04-20 11:05:24 +00009308{
9309 (void) ssl;
9310
Gilles Peskine449bd832023-01-11 14:50:10 +01009311 if (suite_info == NULL) {
9312 return -1;
9313 }
XiaokangQian75d40ef2022-04-20 11:05:24 +00009314
Gilles Peskine449bd832023-01-11 14:50:10 +01009315 if ((suite_info->min_tls_version > max_tls_version) ||
9316 (suite_info->max_tls_version < min_tls_version)) {
9317 return -1;
XiaokangQian75d40ef2022-04-20 11:05:24 +00009318 }
9319
XiaokangQian060d8672022-04-21 09:24:56 +00009320#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_CLI_C)
XiaokangQian75d40ef2022-04-20 11:05:24 +00009321#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Neil Armstrongca7d5062022-05-31 14:43:23 +02009322#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01009323 if (suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
9324 ssl->handshake->psa_pake_ctx_is_ok != 1)
Neil Armstrongca7d5062022-05-31 14:43:23 +02009325#else
Gilles Peskine449bd832023-01-11 14:50:10 +01009326 if (suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
9327 mbedtls_ecjpake_check(&ssl->handshake->ecjpake_ctx) != 0)
Neil Armstrongca7d5062022-05-31 14:43:23 +02009328#endif /* MBEDTLS_USE_PSA_CRYPTO */
XiaokangQian75d40ef2022-04-20 11:05:24 +00009329 {
Gilles Peskine449bd832023-01-11 14:50:10 +01009330 return -1;
XiaokangQian75d40ef2022-04-20 11:05:24 +00009331 }
9332#endif
9333
9334 /* Don't suggest PSK-based ciphersuite if no PSK is available. */
9335#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01009336 if (mbedtls_ssl_ciphersuite_uses_psk(suite_info) &&
9337 mbedtls_ssl_conf_has_static_psk(ssl->conf) == 0) {
9338 return -1;
XiaokangQian75d40ef2022-04-20 11:05:24 +00009339 }
9340#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
9341#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9342
Gilles Peskine449bd832023-01-11 14:50:10 +01009343 return 0;
XiaokangQian75d40ef2022-04-20 11:05:24 +00009344}
9345
Ronald Crone68ab4f2022-10-05 12:46:29 +02009346#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
XiaokangQianeaf36512022-04-24 09:07:44 +00009347/*
9348 * Function for writing a signature algorithm extension.
9349 *
9350 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
9351 * value (TLS 1.3 RFC8446):
9352 * enum {
9353 * ....
9354 * ecdsa_secp256r1_sha256( 0x0403 ),
9355 * ecdsa_secp384r1_sha384( 0x0503 ),
9356 * ecdsa_secp521r1_sha512( 0x0603 ),
9357 * ....
9358 * } SignatureScheme;
9359 *
9360 * struct {
9361 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
9362 * } SignatureSchemeList;
9363 *
9364 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
9365 * value (TLS 1.2 RFC5246):
9366 * enum {
9367 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
9368 * sha512(6), (255)
9369 * } HashAlgorithm;
9370 *
9371 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
9372 * SignatureAlgorithm;
9373 *
9374 * struct {
9375 * HashAlgorithm hash;
9376 * SignatureAlgorithm signature;
9377 * } SignatureAndHashAlgorithm;
9378 *
9379 * SignatureAndHashAlgorithm
9380 * supported_signature_algorithms<2..2^16-2>;
9381 *
9382 * The TLS 1.3 signature algorithm extension was defined to be a compatible
9383 * generalization of the TLS 1.2 signature algorithm extension.
9384 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
9385 * `SignatureScheme` field of TLS 1.3
9386 *
9387 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009388int mbedtls_ssl_write_sig_alg_ext(mbedtls_ssl_context *ssl, unsigned char *buf,
9389 const unsigned char *end, size_t *out_len)
XiaokangQianeaf36512022-04-24 09:07:44 +00009390{
9391 unsigned char *p = buf;
9392 unsigned char *supported_sig_alg; /* Start of supported_signature_algorithms */
9393 size_t supported_sig_alg_len = 0; /* Length of supported_signature_algorithms */
9394
9395 *out_len = 0;
9396
Gilles Peskine449bd832023-01-11 14:50:10 +01009397 MBEDTLS_SSL_DEBUG_MSG(3, ("adding signature_algorithms extension"));
XiaokangQianeaf36512022-04-24 09:07:44 +00009398
9399 /* Check if we have space for header and length field:
9400 * - extension_type (2 bytes)
9401 * - extension_data_length (2 bytes)
9402 * - supported_signature_algorithms_length (2 bytes)
9403 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009404 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 6);
XiaokangQianeaf36512022-04-24 09:07:44 +00009405 p += 6;
9406
9407 /*
9408 * Write supported_signature_algorithms
9409 */
9410 supported_sig_alg = p;
Gilles Peskine449bd832023-01-11 14:50:10 +01009411 const uint16_t *sig_alg = mbedtls_ssl_get_sig_algs(ssl);
9412 if (sig_alg == NULL) {
9413 return MBEDTLS_ERR_SSL_BAD_CONFIG;
9414 }
XiaokangQianeaf36512022-04-24 09:07:44 +00009415
Gilles Peskine449bd832023-01-11 14:50:10 +01009416 for (; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++) {
9417 MBEDTLS_SSL_DEBUG_MSG(3, ("got signature scheme [%x] %s",
9418 *sig_alg,
9419 mbedtls_ssl_sig_alg_to_str(*sig_alg)));
9420 if (!mbedtls_ssl_sig_alg_is_supported(ssl, *sig_alg)) {
XiaokangQianeaf36512022-04-24 09:07:44 +00009421 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01009422 }
9423 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
9424 MBEDTLS_PUT_UINT16_BE(*sig_alg, p, 0);
XiaokangQianeaf36512022-04-24 09:07:44 +00009425 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01009426 MBEDTLS_SSL_DEBUG_MSG(3, ("sent signature scheme [%x] %s",
9427 *sig_alg,
9428 mbedtls_ssl_sig_alg_to_str(*sig_alg)));
XiaokangQianeaf36512022-04-24 09:07:44 +00009429 }
9430
9431 /* Length of supported_signature_algorithms */
9432 supported_sig_alg_len = p - supported_sig_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01009433 if (supported_sig_alg_len == 0) {
9434 MBEDTLS_SSL_DEBUG_MSG(1, ("No signature algorithms defined."));
9435 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
XiaokangQianeaf36512022-04-24 09:07:44 +00009436 }
9437
Gilles Peskine449bd832023-01-11 14:50:10 +01009438 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SIG_ALG, buf, 0);
9439 MBEDTLS_PUT_UINT16_BE(supported_sig_alg_len + 2, buf, 2);
9440 MBEDTLS_PUT_UINT16_BE(supported_sig_alg_len, buf, 4);
XiaokangQianeaf36512022-04-24 09:07:44 +00009441
XiaokangQianeaf36512022-04-24 09:07:44 +00009442 *out_len = p - buf;
9443
9444#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01009445 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_SIG_ALG);
XiaokangQianeaf36512022-04-24 09:07:44 +00009446#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu0c354a22022-08-29 15:25:36 +08009447
Gilles Peskine449bd832023-01-11 14:50:10 +01009448 return 0;
XiaokangQianeaf36512022-04-24 09:07:44 +00009449}
Ronald Crone68ab4f2022-10-05 12:46:29 +02009450#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
XiaokangQianeaf36512022-04-24 09:07:44 +00009451
XiaokangQian40a35232022-05-07 09:02:40 +00009452#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
XiaokangQian9b2b7712022-05-17 02:57:00 +00009453/*
9454 * mbedtls_ssl_parse_server_name_ext
9455 *
9456 * Structure of server_name extension:
9457 *
9458 * enum {
9459 * host_name(0), (255)
9460 * } NameType;
9461 * opaque HostName<1..2^16-1>;
9462 *
9463 * struct {
9464 * NameType name_type;
9465 * select (name_type) {
9466 * case host_name: HostName;
9467 * } name;
9468 * } ServerName;
9469 * struct {
9470 * ServerName server_name_list<1..2^16-1>
9471 * } ServerNameList;
9472 */
Ronald Cronce7d76e2022-07-08 18:56:49 +02009473MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01009474int mbedtls_ssl_parse_server_name_ext(mbedtls_ssl_context *ssl,
9475 const unsigned char *buf,
9476 const unsigned char *end)
XiaokangQian40a35232022-05-07 09:02:40 +00009477{
9478 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
9479 const unsigned char *p = buf;
XiaokangQian9b2b7712022-05-17 02:57:00 +00009480 size_t server_name_list_len, hostname_len;
9481 const unsigned char *server_name_list_end;
XiaokangQian40a35232022-05-07 09:02:40 +00009482
Gilles Peskine449bd832023-01-11 14:50:10 +01009483 MBEDTLS_SSL_DEBUG_MSG(3, ("parse ServerName extension"));
XiaokangQian40a35232022-05-07 09:02:40 +00009484
Gilles Peskine449bd832023-01-11 14:50:10 +01009485 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
9486 server_name_list_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian40a35232022-05-07 09:02:40 +00009487 p += 2;
9488
Gilles Peskine449bd832023-01-11 14:50:10 +01009489 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, server_name_list_len);
XiaokangQian9b2b7712022-05-17 02:57:00 +00009490 server_name_list_end = p + server_name_list_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01009491 while (p < server_name_list_end) {
9492 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, server_name_list_end, 3);
9493 hostname_len = MBEDTLS_GET_UINT16_BE(p, 1);
9494 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, server_name_list_end,
9495 hostname_len + 3);
XiaokangQian40a35232022-05-07 09:02:40 +00009496
Gilles Peskine449bd832023-01-11 14:50:10 +01009497 if (p[0] == MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME) {
XiaokangQian75fe8c72022-06-15 09:42:45 +00009498 /* sni_name is intended to be used only during the parsing of the
9499 * ClientHello message (it is reset to NULL before the end of
9500 * the message parsing). Thus it is ok to just point to the
9501 * reception buffer and not make a copy of it.
9502 */
XiaokangQianf2a94202022-05-20 06:44:24 +00009503 ssl->handshake->sni_name = p + 3;
9504 ssl->handshake->sni_name_len = hostname_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01009505 if (ssl->conf->f_sni == NULL) {
9506 return 0;
XiaokangQian40a35232022-05-07 09:02:40 +00009507 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009508 ret = ssl->conf->f_sni(ssl->conf->p_sni,
9509 ssl, p + 3, hostname_len);
9510 if (ret != 0) {
9511 MBEDTLS_SSL_DEBUG_RET(1, "ssl_sni_wrapper", ret);
9512 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_UNRECOGNIZED_NAME,
9513 MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME);
9514 return MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME;
9515 }
9516 return 0;
XiaokangQian40a35232022-05-07 09:02:40 +00009517 }
9518
9519 p += hostname_len + 3;
9520 }
9521
Gilles Peskine449bd832023-01-11 14:50:10 +01009522 return 0;
XiaokangQian40a35232022-05-07 09:02:40 +00009523}
9524#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
9525
XiaokangQianacb39922022-06-17 10:18:48 +00009526#if defined(MBEDTLS_SSL_ALPN)
Ronald Cronce7d76e2022-07-08 18:56:49 +02009527MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01009528int mbedtls_ssl_parse_alpn_ext(mbedtls_ssl_context *ssl,
9529 const unsigned char *buf,
9530 const unsigned char *end)
XiaokangQianacb39922022-06-17 10:18:48 +00009531{
9532 const unsigned char *p = buf;
XiaokangQianc7403452022-06-23 03:24:12 +00009533 size_t protocol_name_list_len;
XiaokangQian95d5f542022-06-24 02:29:26 +00009534 const unsigned char *protocol_name_list;
9535 const unsigned char *protocol_name_list_end;
XiaokangQianc7403452022-06-23 03:24:12 +00009536 size_t protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009537
9538 /* If ALPN not configured, just ignore the extension */
Gilles Peskine449bd832023-01-11 14:50:10 +01009539 if (ssl->conf->alpn_list == NULL) {
9540 return 0;
9541 }
XiaokangQianacb39922022-06-17 10:18:48 +00009542
9543 /*
XiaokangQianc7403452022-06-23 03:24:12 +00009544 * RFC7301, section 3.1
9545 * opaque ProtocolName<1..2^8-1>;
XiaokangQianacb39922022-06-17 10:18:48 +00009546 *
XiaokangQianc7403452022-06-23 03:24:12 +00009547 * struct {
9548 * ProtocolName protocol_name_list<2..2^16-1>
9549 * } ProtocolNameList;
XiaokangQianacb39922022-06-17 10:18:48 +00009550 */
9551
XiaokangQianc7403452022-06-23 03:24:12 +00009552 /*
XiaokangQian0b776e22022-06-24 09:04:59 +00009553 * protocol_name_list_len 2 bytes
9554 * protocol_name_len 1 bytes
9555 * protocol_name >=1 byte
XiaokangQianc7403452022-06-23 03:24:12 +00009556 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009557 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 4);
XiaokangQianacb39922022-06-17 10:18:48 +00009558
Gilles Peskine449bd832023-01-11 14:50:10 +01009559 protocol_name_list_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQianacb39922022-06-17 10:18:48 +00009560 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01009561 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, protocol_name_list_len);
XiaokangQian95d5f542022-06-24 02:29:26 +00009562 protocol_name_list = p;
9563 protocol_name_list_end = p + protocol_name_list_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009564
9565 /* Validate peer's list (lengths) */
Gilles Peskine449bd832023-01-11 14:50:10 +01009566 while (p < protocol_name_list_end) {
XiaokangQian95d5f542022-06-24 02:29:26 +00009567 protocol_name_len = *p++;
Gilles Peskine449bd832023-01-11 14:50:10 +01009568 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, protocol_name_list_end,
9569 protocol_name_len);
9570 if (protocol_name_len == 0) {
XiaokangQian95d5f542022-06-24 02:29:26 +00009571 MBEDTLS_SSL_PEND_FATAL_ALERT(
9572 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
Gilles Peskine449bd832023-01-11 14:50:10 +01009573 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
9574 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
XiaokangQian95d5f542022-06-24 02:29:26 +00009575 }
9576
9577 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009578 }
9579
9580 /* Use our order of preference */
Gilles Peskine449bd832023-01-11 14:50:10 +01009581 for (const char **alpn = ssl->conf->alpn_list; *alpn != NULL; alpn++) {
9582 size_t const alpn_len = strlen(*alpn);
XiaokangQian95d5f542022-06-24 02:29:26 +00009583 p = protocol_name_list;
Gilles Peskine449bd832023-01-11 14:50:10 +01009584 while (p < protocol_name_list_end) {
XiaokangQian95d5f542022-06-24 02:29:26 +00009585 protocol_name_len = *p++;
Gilles Peskine449bd832023-01-11 14:50:10 +01009586 if (protocol_name_len == alpn_len &&
9587 memcmp(p, *alpn, alpn_len) == 0) {
XiaokangQianacb39922022-06-17 10:18:48 +00009588 ssl->alpn_chosen = *alpn;
Gilles Peskine449bd832023-01-11 14:50:10 +01009589 return 0;
XiaokangQianacb39922022-06-17 10:18:48 +00009590 }
XiaokangQian95d5f542022-06-24 02:29:26 +00009591
9592 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009593 }
9594 }
9595
XiaokangQian95d5f542022-06-24 02:29:26 +00009596 /* If we get here, no match was found */
XiaokangQianacb39922022-06-17 10:18:48 +00009597 MBEDTLS_SSL_PEND_FATAL_ALERT(
Gilles Peskine449bd832023-01-11 14:50:10 +01009598 MBEDTLS_SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL,
9599 MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL);
9600 return MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL;
XiaokangQianacb39922022-06-17 10:18:48 +00009601}
9602
Gilles Peskine449bd832023-01-11 14:50:10 +01009603int mbedtls_ssl_write_alpn_ext(mbedtls_ssl_context *ssl,
9604 unsigned char *buf,
9605 unsigned char *end,
9606 size_t *out_len)
XiaokangQianacb39922022-06-17 10:18:48 +00009607{
9608 unsigned char *p = buf;
XiaokangQian95d5f542022-06-24 02:29:26 +00009609 size_t protocol_name_len;
XiaokangQianc7403452022-06-23 03:24:12 +00009610 *out_len = 0;
XiaokangQianacb39922022-06-17 10:18:48 +00009611
Gilles Peskine449bd832023-01-11 14:50:10 +01009612 if (ssl->alpn_chosen == NULL) {
9613 return 0;
XiaokangQianacb39922022-06-17 10:18:48 +00009614 }
9615
Gilles Peskine449bd832023-01-11 14:50:10 +01009616 protocol_name_len = strlen(ssl->alpn_chosen);
9617 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 7 + protocol_name_len);
XiaokangQianacb39922022-06-17 10:18:48 +00009618
Gilles Peskine449bd832023-01-11 14:50:10 +01009619 MBEDTLS_SSL_DEBUG_MSG(3, ("server side, adding alpn extension"));
XiaokangQianacb39922022-06-17 10:18:48 +00009620 /*
9621 * 0 . 1 ext identifier
9622 * 2 . 3 ext length
9623 * 4 . 5 protocol list length
9624 * 6 . 6 protocol name length
9625 * 7 . 7+n protocol name
9626 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009627 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_ALPN, p, 0);
XiaokangQianacb39922022-06-17 10:18:48 +00009628
XiaokangQian95d5f542022-06-24 02:29:26 +00009629 *out_len = 7 + protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009630
Gilles Peskine449bd832023-01-11 14:50:10 +01009631 MBEDTLS_PUT_UINT16_BE(protocol_name_len + 3, p, 2);
9632 MBEDTLS_PUT_UINT16_BE(protocol_name_len + 1, p, 4);
XiaokangQian0b776e22022-06-24 09:04:59 +00009633 /* Note: the length of the chosen protocol has been checked to be less
9634 * than 255 bytes in `mbedtls_ssl_conf_alpn_protocols`.
9635 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009636 p[6] = MBEDTLS_BYTE_0(protocol_name_len);
XiaokangQianacb39922022-06-17 10:18:48 +00009637
Gilles Peskine449bd832023-01-11 14:50:10 +01009638 memcpy(p + 7, ssl->alpn_chosen, protocol_name_len);
Jerry Yub95dd362022-11-08 21:19:34 +08009639
9640#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01009641 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_ALPN);
Jerry Yub95dd362022-11-08 21:19:34 +08009642#endif
9643
Gilles Peskine449bd832023-01-11 14:50:10 +01009644 return 0;
XiaokangQianacb39922022-06-17 10:18:48 +00009645}
9646#endif /* MBEDTLS_SSL_ALPN */
9647
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009648#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
Xiaokang Qian03409292022-10-12 02:49:52 +00009649 defined(MBEDTLS_SSL_SESSION_TICKETS) && \
Xiaokang Qianed0620c2022-10-12 06:58:13 +00009650 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \
Xiaokang Qianed3afcd2022-10-12 08:31:11 +00009651 defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01009652int mbedtls_ssl_session_set_hostname(mbedtls_ssl_session *session,
9653 const char *hostname)
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009654{
9655 /* Initialize to suppress unnecessary compiler warning */
9656 size_t hostname_len = 0;
9657
9658 /* Check if new hostname is valid before
9659 * making any change to current one */
Gilles Peskine449bd832023-01-11 14:50:10 +01009660 if (hostname != NULL) {
9661 hostname_len = strlen(hostname);
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009662
Gilles Peskine449bd832023-01-11 14:50:10 +01009663 if (hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN) {
9664 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9665 }
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009666 }
9667
9668 /* Now it's clear that we will overwrite the old hostname,
9669 * so we can free it safely */
Gilles Peskine449bd832023-01-11 14:50:10 +01009670 if (session->hostname != NULL) {
9671 mbedtls_platform_zeroize(session->hostname,
9672 strlen(session->hostname));
9673 mbedtls_free(session->hostname);
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009674 }
9675
9676 /* Passing NULL as hostname shall clear the old one */
Gilles Peskine449bd832023-01-11 14:50:10 +01009677 if (hostname == NULL) {
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009678 session->hostname = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01009679 } else {
9680 session->hostname = mbedtls_calloc(1, hostname_len + 1);
9681 if (session->hostname == NULL) {
9682 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
9683 }
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009684
Gilles Peskine449bd832023-01-11 14:50:10 +01009685 memcpy(session->hostname, hostname, hostname_len);
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009686 }
9687
Gilles Peskine449bd832023-01-11 14:50:10 +01009688 return 0;
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009689}
Xiaokang Qian03409292022-10-12 02:49:52 +00009690#endif /* MBEDTLS_SSL_PROTO_TLS1_3 &&
9691 MBEDTLS_SSL_SESSION_TICKETS &&
Xiaokang Qianed0620c2022-10-12 06:58:13 +00009692 MBEDTLS_SSL_SERVER_NAME_INDICATION &&
Xiaokang Qianed3afcd2022-10-12 08:31:11 +00009693 MBEDTLS_SSL_CLI_C */
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009694
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009695#endif /* MBEDTLS_SSL_TLS_C */