blob: cbc60ec96e2009f08169ab66a50ac6f9621ef3bb [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01002 * TLS shared functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakker5121ce52009-01-03 21:22:43 +000018 */
19/*
Paul Bakker5121ce52009-01-03 21:22:43 +000020 * http://www.ietf.org/rfc/rfc2246.txt
21 * http://www.ietf.org/rfc/rfc4346.txt
22 */
23
Gilles Peskinedb09ef62020-06-03 01:43:33 +020024#include "common.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#if defined(MBEDTLS_SSL_TLS_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000027
Jerry Yub476a442022-01-21 18:14:45 +080028#include <assert.h>
29
SimonBd5800b72016-04-26 07:43:27 +010030#include "mbedtls/platform.h"
SimonBd5800b72016-04-26 07:43:27 +010031
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000032#include "mbedtls/ssl.h"
Ronald Cron9f0fba32022-02-10 16:45:15 +010033#include "ssl_client.h"
Ronald Cron27c85e72022-03-08 11:37:55 +010034#include "ssl_debug_helpers.h"
Chris Jones84a773f2021-03-05 18:38:47 +000035#include "ssl_misc.h"
Andrzej Kurek25f27152022-08-17 16:09:31 -040036
Janos Follath73c616b2019-12-18 15:07:04 +000037#include "mbedtls/debug.h"
38#include "mbedtls/error.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050039#include "mbedtls/platform_util.h"
Hanno Beckera835da52019-05-16 12:39:07 +010040#include "mbedtls/version.h"
Gabor Mezei765862c2021-10-19 12:22:25 +020041#include "mbedtls/constant_time.h"
Paul Bakker0be444a2013-08-27 21:55:01 +020042
Rich Evans00ab4702015-02-06 13:43:58 +000043#include <string.h>
44
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050045#if defined(MBEDTLS_USE_PSA_CRYPTO)
46#include "mbedtls/psa_util.h"
47#include "psa/crypto.h"
48#endif
Manuel Pégourié-Gonnard07018f92022-09-15 11:29:35 +020049#include "mbedtls/legacy_or_psa.h"
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050050
Janos Follath23bdca02016-10-07 14:47:14 +010051#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000052#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020053#endif
54
Ronald Cronad8c17b2022-06-10 17:18:09 +020055#if defined(MBEDTLS_TEST_HOOKS)
56static mbedtls_ssl_chk_buf_ptr_args chk_buf_ptr_fail_args;
57
58void mbedtls_ssl_set_chk_buf_ptr_fail_args(
Gilles Peskine449bd832023-01-11 14:50:10 +010059 const uint8_t *cur, const uint8_t *end, size_t need)
Ronald Cronad8c17b2022-06-10 17:18:09 +020060{
61 chk_buf_ptr_fail_args.cur = cur;
62 chk_buf_ptr_fail_args.end = end;
63 chk_buf_ptr_fail_args.need = need;
64}
65
Gilles Peskine449bd832023-01-11 14:50:10 +010066void mbedtls_ssl_reset_chk_buf_ptr_fail_args(void)
Ronald Cronad8c17b2022-06-10 17:18:09 +020067{
Gilles Peskine449bd832023-01-11 14:50:10 +010068 memset(&chk_buf_ptr_fail_args, 0, sizeof(chk_buf_ptr_fail_args));
Ronald Cronad8c17b2022-06-10 17:18:09 +020069}
70
Gilles Peskine449bd832023-01-11 14:50:10 +010071int mbedtls_ssl_cmp_chk_buf_ptr_fail_args(mbedtls_ssl_chk_buf_ptr_args *args)
Ronald Cronad8c17b2022-06-10 17:18:09 +020072{
Gilles Peskine449bd832023-01-11 14:50:10 +010073 return (chk_buf_ptr_fail_args.cur != args->cur) ||
74 (chk_buf_ptr_fail_args.end != args->end) ||
75 (chk_buf_ptr_fail_args.need != args->need);
Ronald Cronad8c17b2022-06-10 17:18:09 +020076}
77#endif /* MBEDTLS_TEST_HOOKS */
78
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020079#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +010080
Hanno Beckera0e20d02019-05-15 14:03:01 +010081#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf8542cf2019-04-09 15:22:03 +010082/* Top-level Connection ID API */
83
Gilles Peskine449bd832023-01-11 14:50:10 +010084int mbedtls_ssl_conf_cid(mbedtls_ssl_config *conf,
85 size_t len,
86 int ignore_other_cid)
Hanno Beckerad4a1372019-05-03 13:06:44 +010087{
Gilles Peskine449bd832023-01-11 14:50:10 +010088 if (len > MBEDTLS_SSL_CID_IN_LEN_MAX) {
89 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
90 }
Hanno Beckerad4a1372019-05-03 13:06:44 +010091
Gilles Peskine449bd832023-01-11 14:50:10 +010092 if (ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_FAIL &&
93 ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE) {
94 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Becker611ac772019-05-14 11:45:26 +010095 }
96
97 conf->ignore_unexpected_cid = ignore_other_cid;
Hanno Beckerad4a1372019-05-03 13:06:44 +010098 conf->cid_len = len;
Gilles Peskine449bd832023-01-11 14:50:10 +010099 return 0;
Hanno Beckerad4a1372019-05-03 13:06:44 +0100100}
101
Gilles Peskine449bd832023-01-11 14:50:10 +0100102int mbedtls_ssl_set_cid(mbedtls_ssl_context *ssl,
103 int enable,
104 unsigned char const *own_cid,
105 size_t own_cid_len)
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100106{
Gilles Peskine449bd832023-01-11 14:50:10 +0100107 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
108 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
109 }
Hanno Becker76a79ab2019-05-03 14:38:32 +0100110
Hanno Beckerca092242019-04-25 16:01:49 +0100111 ssl->negotiate_cid = enable;
Gilles Peskine449bd832023-01-11 14:50:10 +0100112 if (enable == MBEDTLS_SSL_CID_DISABLED) {
113 MBEDTLS_SSL_DEBUG_MSG(3, ("Disable use of CID extension."));
114 return 0;
Hanno Beckerca092242019-04-25 16:01:49 +0100115 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100116 MBEDTLS_SSL_DEBUG_MSG(3, ("Enable use of CID extension."));
117 MBEDTLS_SSL_DEBUG_BUF(3, "Own CID", own_cid, own_cid_len);
Hanno Beckerca092242019-04-25 16:01:49 +0100118
Gilles Peskine449bd832023-01-11 14:50:10 +0100119 if (own_cid_len != ssl->conf->cid_len) {
120 MBEDTLS_SSL_DEBUG_MSG(3, ("CID length %u does not match CID length %u in config",
121 (unsigned) own_cid_len,
122 (unsigned) ssl->conf->cid_len));
123 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Beckerca092242019-04-25 16:01:49 +0100124 }
125
Gilles Peskine449bd832023-01-11 14:50:10 +0100126 memcpy(ssl->own_cid, own_cid, own_cid_len);
Hanno Beckerb7ee0cf2019-04-30 14:07:31 +0100127 /* Truncation is not an issue here because
128 * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */
129 ssl->own_cid_len = (uint8_t) own_cid_len;
Hanno Beckerca092242019-04-25 16:01:49 +0100130
Gilles Peskine449bd832023-01-11 14:50:10 +0100131 return 0;
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100132}
133
Gilles Peskine449bd832023-01-11 14:50:10 +0100134int mbedtls_ssl_get_own_cid(mbedtls_ssl_context *ssl,
135 int *enabled,
136 unsigned char own_cid[MBEDTLS_SSL_CID_OUT_LEN_MAX],
137 size_t *own_cid_len)
Paul Elliott0113cf12022-03-11 20:26:47 +0000138{
139 *enabled = MBEDTLS_SSL_CID_DISABLED;
140
Gilles Peskine449bd832023-01-11 14:50:10 +0100141 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
142 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
143 }
Paul Elliott0113cf12022-03-11 20:26:47 +0000144
145 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID length is
146 * zero as this is indistinguishable from not requesting to use
147 * the CID extension. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100148 if (ssl->own_cid_len == 0 || ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED) {
149 return 0;
150 }
Paul Elliott0113cf12022-03-11 20:26:47 +0000151
Gilles Peskine449bd832023-01-11 14:50:10 +0100152 if (own_cid_len != NULL) {
Paul Elliott0113cf12022-03-11 20:26:47 +0000153 *own_cid_len = ssl->own_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100154 if (own_cid != NULL) {
155 memcpy(own_cid, ssl->own_cid, ssl->own_cid_len);
156 }
Paul Elliott0113cf12022-03-11 20:26:47 +0000157 }
158
159 *enabled = MBEDTLS_SSL_CID_ENABLED;
160
Gilles Peskine449bd832023-01-11 14:50:10 +0100161 return 0;
Paul Elliott0113cf12022-03-11 20:26:47 +0000162}
163
Gilles Peskine449bd832023-01-11 14:50:10 +0100164int mbedtls_ssl_get_peer_cid(mbedtls_ssl_context *ssl,
165 int *enabled,
166 unsigned char peer_cid[MBEDTLS_SSL_CID_OUT_LEN_MAX],
167 size_t *peer_cid_len)
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100168{
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100169 *enabled = MBEDTLS_SSL_CID_DISABLED;
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100170
Gilles Peskine449bd832023-01-11 14:50:10 +0100171 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
172 mbedtls_ssl_is_handshake_over(ssl) == 0) {
173 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Becker76a79ab2019-05-03 14:38:32 +0100174 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100175
Hanno Beckerc5f24222019-05-03 12:54:52 +0100176 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions
177 * were used, but client and server requested the empty CID.
178 * This is indistinguishable from not using the CID extension
179 * in the first place. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100180 if (ssl->transform_in->in_cid_len == 0 &&
181 ssl->transform_in->out_cid_len == 0) {
182 return 0;
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100183 }
184
Gilles Peskine449bd832023-01-11 14:50:10 +0100185 if (peer_cid_len != NULL) {
Hanno Becker615ef172019-05-22 16:50:35 +0100186 *peer_cid_len = ssl->transform_in->out_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100187 if (peer_cid != NULL) {
188 memcpy(peer_cid, ssl->transform_in->out_cid,
189 ssl->transform_in->out_cid_len);
Hanno Becker615ef172019-05-22 16:50:35 +0100190 }
191 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100192
193 *enabled = MBEDTLS_SSL_CID_ENABLED;
194
Gilles Peskine449bd832023-01-11 14:50:10 +0100195 return 0;
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100196}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100197#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100198
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200199#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200200
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200201#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200202/*
203 * Convert max_fragment_length codes to length.
204 * RFC 6066 says:
205 * enum{
206 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
207 * } MaxFragmentLength;
208 * and we add 0 -> extension unused
209 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100210static unsigned int ssl_mfl_code_to_length(int mfl)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200211{
Gilles Peskine449bd832023-01-11 14:50:10 +0100212 switch (mfl) {
213 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
214 return MBEDTLS_TLS_EXT_ADV_CONTENT_LEN;
215 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
216 return 512;
217 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
218 return 1024;
219 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
220 return 2048;
221 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
222 return 4096;
223 default:
224 return MBEDTLS_TLS_EXT_ADV_CONTENT_LEN;
Angus Grattond8213d02016-05-25 20:56:48 +1000225 }
226}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200227#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200228
Gilles Peskine449bd832023-01-11 14:50:10 +0100229int mbedtls_ssl_session_copy(mbedtls_ssl_session *dst,
230 const mbedtls_ssl_session *src)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200231{
Gilles Peskine449bd832023-01-11 14:50:10 +0100232 mbedtls_ssl_session_free(dst);
233 memcpy(dst, src, sizeof(mbedtls_ssl_session));
吴敬辉0b716112021-11-29 10:46:35 +0800234#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
235 dst->ticket = NULL;
Xiaokang Qian87306442022-10-12 09:47:38 +0000236#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
237 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaokang Qian126bf8e2022-10-13 02:22:40 +0000238 dst->hostname = NULL;
吴敬辉0b716112021-11-29 10:46:35 +0800239#endif
Xiaokang Qian87306442022-10-12 09:47:38 +0000240#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
吴敬辉0b716112021-11-29 10:46:35 +0800241
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200242#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker6d1986e2019-02-07 12:27:42 +0000243
244#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100245 if (src->peer_cert != NULL) {
Janos Follath865b3eb2019-12-16 11:46:15 +0000246 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker2292d1f2013-09-15 17:06:49 +0200247
Gilles Peskine449bd832023-01-11 14:50:10 +0100248 dst->peer_cert = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
249 if (dst->peer_cert == NULL) {
250 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
251 }
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200252
Gilles Peskine449bd832023-01-11 14:50:10 +0100253 mbedtls_x509_crt_init(dst->peer_cert);
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200254
Gilles Peskine449bd832023-01-11 14:50:10 +0100255 if ((ret = mbedtls_x509_crt_parse_der(dst->peer_cert, src->peer_cert->raw.p,
256 src->peer_cert->raw.len)) != 0) {
257 mbedtls_free(dst->peer_cert);
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200258 dst->peer_cert = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100259 return ret;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200260 }
261 }
Hanno Becker6d1986e2019-02-07 12:27:42 +0000262#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100263 if (src->peer_cert_digest != NULL) {
Hanno Becker9198ad12019-02-05 17:00:50 +0000264 dst->peer_cert_digest =
Gilles Peskine449bd832023-01-11 14:50:10 +0100265 mbedtls_calloc(1, src->peer_cert_digest_len);
266 if (dst->peer_cert_digest == NULL) {
267 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
268 }
Hanno Becker9198ad12019-02-05 17:00:50 +0000269
Gilles Peskine449bd832023-01-11 14:50:10 +0100270 memcpy(dst->peer_cert_digest, src->peer_cert_digest,
271 src->peer_cert_digest_len);
Hanno Becker9198ad12019-02-05 17:00:50 +0000272 dst->peer_cert_digest_type = src->peer_cert_digest_type;
Hanno Beckeraccc5992019-02-25 10:06:59 +0000273 dst->peer_cert_digest_len = src->peer_cert_digest_len;
Hanno Becker9198ad12019-02-05 17:00:50 +0000274 }
275#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
276
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200277#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200278
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200279#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100280 if (src->ticket != NULL) {
281 dst->ticket = mbedtls_calloc(1, src->ticket_len);
282 if (dst->ticket == NULL) {
283 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
284 }
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200285
Gilles Peskine449bd832023-01-11 14:50:10 +0100286 memcpy(dst->ticket, src->ticket, src->ticket_len);
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200287 }
Xiaokang Qian126bf8e2022-10-13 02:22:40 +0000288
289#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
290 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +0100291 if (src->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Xiaokang Qian126bf8e2022-10-13 02:22:40 +0000292 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100293 ret = mbedtls_ssl_session_set_hostname(dst, src->hostname);
294 if (ret != 0) {
295 return ret;
296 }
Xiaokang Qian126bf8e2022-10-13 02:22:40 +0000297 }
298#endif /* MBEDTLS_SSL_PROTO_TLS1_3 &&
299 MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200300#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200301
Gilles Peskine449bd832023-01-11 14:50:10 +0100302 return 0;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200303}
304
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500305#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200306MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100307static int resize_buffer(unsigned char **buffer, size_t len_new, size_t *len_old)
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500308{
Gilles Peskine449bd832023-01-11 14:50:10 +0100309 unsigned char *resized_buffer = mbedtls_calloc(1, len_new);
310 if (resized_buffer == NULL) {
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500311 return -1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100312 }
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500313
314 /* We want to copy len_new bytes when downsizing the buffer, and
315 * len_old bytes when upsizing, so we choose the smaller of two sizes,
316 * to fit one buffer into another. Size checks, ensuring that no data is
317 * lost, are done outside of this function. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100318 memcpy(resized_buffer, *buffer,
319 (len_new < *len_old) ? len_new : *len_old);
320 mbedtls_platform_zeroize(*buffer, *len_old);
321 mbedtls_free(*buffer);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500322
323 *buffer = resized_buffer;
324 *len_old = len_new;
325
326 return 0;
327}
Andrzej Kurek4a063792020-10-21 15:08:44 +0200328
Gilles Peskine449bd832023-01-11 14:50:10 +0100329static void handle_buffer_resizing(mbedtls_ssl_context *ssl, int downsizing,
330 size_t in_buf_new_len,
331 size_t out_buf_new_len)
Andrzej Kurek4a063792020-10-21 15:08:44 +0200332{
333 int modified = 0;
334 size_t written_in = 0, iv_offset_in = 0, len_offset_in = 0;
335 size_t written_out = 0, iv_offset_out = 0, len_offset_out = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100336 if (ssl->in_buf != NULL) {
Andrzej Kurek4a063792020-10-21 15:08:44 +0200337 written_in = ssl->in_msg - ssl->in_buf;
338 iv_offset_in = ssl->in_iv - ssl->in_buf;
339 len_offset_in = ssl->in_len - ssl->in_buf;
Gilles Peskine449bd832023-01-11 14:50:10 +0100340 if (downsizing ?
Andrzej Kurek4a063792020-10-21 15:08:44 +0200341 ssl->in_buf_len > in_buf_new_len && ssl->in_left < in_buf_new_len :
Gilles Peskine449bd832023-01-11 14:50:10 +0100342 ssl->in_buf_len < in_buf_new_len) {
343 if (resize_buffer(&ssl->in_buf, in_buf_new_len, &ssl->in_buf_len) != 0) {
344 MBEDTLS_SSL_DEBUG_MSG(1, ("input buffer resizing failed - out of memory"));
345 } else {
346 MBEDTLS_SSL_DEBUG_MSG(2, ("Reallocating in_buf to %" MBEDTLS_PRINTF_SIZET,
347 in_buf_new_len));
Andrzej Kurek4a063792020-10-21 15:08:44 +0200348 modified = 1;
349 }
350 }
351 }
352
Gilles Peskine449bd832023-01-11 14:50:10 +0100353 if (ssl->out_buf != NULL) {
Andrzej Kurek4a063792020-10-21 15:08:44 +0200354 written_out = ssl->out_msg - ssl->out_buf;
355 iv_offset_out = ssl->out_iv - ssl->out_buf;
356 len_offset_out = ssl->out_len - ssl->out_buf;
Gilles Peskine449bd832023-01-11 14:50:10 +0100357 if (downsizing ?
Andrzej Kurek4a063792020-10-21 15:08:44 +0200358 ssl->out_buf_len > out_buf_new_len && ssl->out_left < out_buf_new_len :
Gilles Peskine449bd832023-01-11 14:50:10 +0100359 ssl->out_buf_len < out_buf_new_len) {
360 if (resize_buffer(&ssl->out_buf, out_buf_new_len, &ssl->out_buf_len) != 0) {
361 MBEDTLS_SSL_DEBUG_MSG(1, ("output buffer resizing failed - out of memory"));
362 } else {
363 MBEDTLS_SSL_DEBUG_MSG(2, ("Reallocating out_buf to %" MBEDTLS_PRINTF_SIZET,
364 out_buf_new_len));
Andrzej Kurek4a063792020-10-21 15:08:44 +0200365 modified = 1;
366 }
367 }
368 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100369 if (modified) {
Andrzej Kurek4a063792020-10-21 15:08:44 +0200370 /* Update pointers here to avoid doing it twice. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100371 mbedtls_ssl_reset_in_out_pointers(ssl);
Andrzej Kurek4a063792020-10-21 15:08:44 +0200372 /* Fields below might not be properly updated with record
373 * splitting or with CID, so they are manually updated here. */
374 ssl->out_msg = ssl->out_buf + written_out;
375 ssl->out_len = ssl->out_buf + len_offset_out;
376 ssl->out_iv = ssl->out_buf + iv_offset_out;
377
378 ssl->in_msg = ssl->in_buf + written_in;
379 ssl->in_len = ssl->in_buf + len_offset_in;
380 ssl->in_iv = ssl->in_buf + iv_offset_in;
381 }
382}
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500383#endif /* MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH */
384
Jerry Yudb8c48a2022-01-27 14:54:54 +0800385#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yued14c932022-02-17 13:40:45 +0800386
387#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Gilles Peskine449bd832023-01-11 14:50:10 +0100388typedef int (*tls_prf_fn)(const unsigned char *secret, size_t slen,
389 const char *label,
390 const unsigned char *random, size_t rlen,
391 unsigned char *dstbuf, size_t dlen);
Jerry Yued14c932022-02-17 13:40:45 +0800392
Gilles Peskine449bd832023-01-11 14:50:10 +0100393static tls_prf_fn ssl_tls12prf_from_cs(int ciphersuite_id);
Jerry Yued14c932022-02-17 13:40:45 +0800394
395#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
396
397/* Type for the TLS PRF */
398typedef int ssl_tls_prf_t(const unsigned char *, size_t, const char *,
399 const unsigned char *, size_t,
400 unsigned char *, size_t);
401
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200402MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100403static int ssl_tls12_populate_transform(mbedtls_ssl_transform *transform,
404 int ciphersuite,
405 const unsigned char master[48],
Neil Armstrongf2c82f02022-04-05 11:16:53 +0200406#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +0100407 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +0200408#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +0100409 ssl_tls_prf_t tls_prf,
410 const unsigned char randbytes[64],
411 mbedtls_ssl_protocol_version tls_version,
412 unsigned endpoint,
413 const mbedtls_ssl_context *ssl);
Jerry Yued14c932022-02-17 13:40:45 +0800414
Andrzej Kurek25f27152022-08-17 16:09:31 -0400415#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200416MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100417static int tls_prf_sha256(const unsigned char *secret, size_t slen,
Ron Eldor51d3ab52019-05-12 14:54:30 +0300418 const char *label,
419 const unsigned char *random, size_t rlen,
Gilles Peskine449bd832023-01-11 14:50:10 +0100420 unsigned char *dstbuf, size_t dlen);
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100421static int ssl_calc_verify_tls_sha256(const mbedtls_ssl_context *, unsigned char *, size_t *);
422static int ssl_calc_finished_tls_sha256(mbedtls_ssl_context *, unsigned char *, int);
Gilles Peskine449bd832023-01-11 14:50:10 +0100423
424#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
425
426#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
427MBEDTLS_CHECK_RETURN_CRITICAL
428static int tls_prf_sha384(const unsigned char *secret, size_t slen,
429 const char *label,
430 const unsigned char *random, size_t rlen,
431 unsigned char *dstbuf, size_t dlen);
432
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100433static int ssl_calc_verify_tls_sha384(const mbedtls_ssl_context *, unsigned char *, size_t *);
434static int ssl_calc_finished_tls_sha384(mbedtls_ssl_context *, unsigned char *, int);
Gilles Peskine449bd832023-01-11 14:50:10 +0100435#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
436
437static size_t ssl_tls12_session_save(const mbedtls_ssl_session *session,
438 unsigned char *buf,
439 size_t buf_len);
440
441MBEDTLS_CHECK_RETURN_CRITICAL
442static int ssl_tls12_session_load(mbedtls_ssl_session *session,
443 const unsigned char *buf,
444 size_t len);
445#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
446
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100447static int ssl_update_checksum_start(mbedtls_ssl_context *, const unsigned char *, size_t);
Gilles Peskine449bd832023-01-11 14:50:10 +0100448
449#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100450static int ssl_update_checksum_sha256(mbedtls_ssl_context *, const unsigned char *, size_t);
Gilles Peskine449bd832023-01-11 14:50:10 +0100451#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
452
453#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100454static int ssl_update_checksum_sha384(mbedtls_ssl_context *, const unsigned char *, size_t);
Gilles Peskine449bd832023-01-11 14:50:10 +0100455#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
456
457int mbedtls_ssl_tls_prf(const mbedtls_tls_prf_types prf,
458 const unsigned char *secret, size_t slen,
459 const char *label,
460 const unsigned char *random, size_t rlen,
461 unsigned char *dstbuf, size_t dlen)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300462{
463 mbedtls_ssl_tls_prf_cb *tls_prf = NULL;
464
Gilles Peskine449bd832023-01-11 14:50:10 +0100465 switch (prf) {
Ron Eldord2f25f72019-05-15 14:54:22 +0300466#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek25f27152022-08-17 16:09:31 -0400467#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300468 case MBEDTLS_SSL_TLS_PRF_SHA384:
469 tls_prf = tls_prf_sha384;
Gilles Peskine449bd832023-01-11 14:50:10 +0100470 break;
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400471#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -0400472#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300473 case MBEDTLS_SSL_TLS_PRF_SHA256:
474 tls_prf = tls_prf_sha256;
Gilles Peskine449bd832023-01-11 14:50:10 +0100475 break;
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400476#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Ron Eldord2f25f72019-05-15 14:54:22 +0300477#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100478 default:
479 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Ron Eldor51d3ab52019-05-12 14:54:30 +0300480 }
481
Gilles Peskine449bd832023-01-11 14:50:10 +0100482 return tls_prf(secret, slen, label, random, rlen, dstbuf, dlen);
Ron Eldor51d3ab52019-05-12 14:54:30 +0300483}
484
Jerry Yuc73c6182022-02-08 20:29:25 +0800485#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100486static void ssl_clear_peer_cert(mbedtls_ssl_session *session)
Jerry Yuc73c6182022-02-08 20:29:25 +0800487{
488#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100489 if (session->peer_cert != NULL) {
490 mbedtls_x509_crt_free(session->peer_cert);
491 mbedtls_free(session->peer_cert);
Jerry Yuc73c6182022-02-08 20:29:25 +0800492 session->peer_cert = NULL;
493 }
494#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100495 if (session->peer_cert_digest != NULL) {
Jerry Yuc73c6182022-02-08 20:29:25 +0800496 /* Zeroization is not necessary. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100497 mbedtls_free(session->peer_cert_digest);
Jerry Yuc73c6182022-02-08 20:29:25 +0800498 session->peer_cert_digest = NULL;
499 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
500 session->peer_cert_digest_len = 0;
501 }
502#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
503}
504#endif /* MBEDTLS_X509_CRT_PARSE_C */
505
Gilles Peskine449bd832023-01-11 14:50:10 +0100506uint32_t mbedtls_ssl_get_extension_id(unsigned int extension_type)
Jerry Yu7a485c12022-10-31 13:08:18 +0800507{
Gilles Peskine449bd832023-01-11 14:50:10 +0100508 switch (extension_type) {
Jerry Yu7a485c12022-10-31 13:08:18 +0800509 case MBEDTLS_TLS_EXT_SERVERNAME:
Gilles Peskine449bd832023-01-11 14:50:10 +0100510 return MBEDTLS_SSL_EXT_ID_SERVERNAME;
Jerry Yu7a485c12022-10-31 13:08:18 +0800511
512 case MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100513 return MBEDTLS_SSL_EXT_ID_MAX_FRAGMENT_LENGTH;
Jerry Yu7a485c12022-10-31 13:08:18 +0800514
515 case MBEDTLS_TLS_EXT_STATUS_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +0100516 return MBEDTLS_SSL_EXT_ID_STATUS_REQUEST;
Jerry Yu7a485c12022-10-31 13:08:18 +0800517
518 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
Gilles Peskine449bd832023-01-11 14:50:10 +0100519 return MBEDTLS_SSL_EXT_ID_SUPPORTED_GROUPS;
Jerry Yu7a485c12022-10-31 13:08:18 +0800520
521 case MBEDTLS_TLS_EXT_SIG_ALG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100522 return MBEDTLS_SSL_EXT_ID_SIG_ALG;
Jerry Yu7a485c12022-10-31 13:08:18 +0800523
524 case MBEDTLS_TLS_EXT_USE_SRTP:
Gilles Peskine449bd832023-01-11 14:50:10 +0100525 return MBEDTLS_SSL_EXT_ID_USE_SRTP;
Jerry Yu7a485c12022-10-31 13:08:18 +0800526
527 case MBEDTLS_TLS_EXT_HEARTBEAT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100528 return MBEDTLS_SSL_EXT_ID_HEARTBEAT;
Jerry Yu7a485c12022-10-31 13:08:18 +0800529
530 case MBEDTLS_TLS_EXT_ALPN:
Gilles Peskine449bd832023-01-11 14:50:10 +0100531 return MBEDTLS_SSL_EXT_ID_ALPN;
Jerry Yu7a485c12022-10-31 13:08:18 +0800532
533 case MBEDTLS_TLS_EXT_SCT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100534 return MBEDTLS_SSL_EXT_ID_SCT;
Jerry Yu7a485c12022-10-31 13:08:18 +0800535
536 case MBEDTLS_TLS_EXT_CLI_CERT_TYPE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100537 return MBEDTLS_SSL_EXT_ID_CLI_CERT_TYPE;
Jerry Yu7a485c12022-10-31 13:08:18 +0800538
539 case MBEDTLS_TLS_EXT_SERV_CERT_TYPE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100540 return MBEDTLS_SSL_EXT_ID_SERV_CERT_TYPE;
Jerry Yu7a485c12022-10-31 13:08:18 +0800541
542 case MBEDTLS_TLS_EXT_PADDING:
Gilles Peskine449bd832023-01-11 14:50:10 +0100543 return MBEDTLS_SSL_EXT_ID_PADDING;
Jerry Yu7a485c12022-10-31 13:08:18 +0800544
545 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100546 return MBEDTLS_SSL_EXT_ID_PRE_SHARED_KEY;
Jerry Yu7a485c12022-10-31 13:08:18 +0800547
548 case MBEDTLS_TLS_EXT_EARLY_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100549 return MBEDTLS_SSL_EXT_ID_EARLY_DATA;
Jerry Yu7a485c12022-10-31 13:08:18 +0800550
551 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
Gilles Peskine449bd832023-01-11 14:50:10 +0100552 return MBEDTLS_SSL_EXT_ID_SUPPORTED_VERSIONS;
Jerry Yu7a485c12022-10-31 13:08:18 +0800553
554 case MBEDTLS_TLS_EXT_COOKIE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100555 return MBEDTLS_SSL_EXT_ID_COOKIE;
Jerry Yu7a485c12022-10-31 13:08:18 +0800556
557 case MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES:
Gilles Peskine449bd832023-01-11 14:50:10 +0100558 return MBEDTLS_SSL_EXT_ID_PSK_KEY_EXCHANGE_MODES;
Jerry Yu7a485c12022-10-31 13:08:18 +0800559
560 case MBEDTLS_TLS_EXT_CERT_AUTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100561 return MBEDTLS_SSL_EXT_ID_CERT_AUTH;
Jerry Yu7a485c12022-10-31 13:08:18 +0800562
563 case MBEDTLS_TLS_EXT_OID_FILTERS:
Gilles Peskine449bd832023-01-11 14:50:10 +0100564 return MBEDTLS_SSL_EXT_ID_OID_FILTERS;
Jerry Yu7a485c12022-10-31 13:08:18 +0800565
566 case MBEDTLS_TLS_EXT_POST_HANDSHAKE_AUTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100567 return MBEDTLS_SSL_EXT_ID_POST_HANDSHAKE_AUTH;
Jerry Yu7a485c12022-10-31 13:08:18 +0800568
569 case MBEDTLS_TLS_EXT_SIG_ALG_CERT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100570 return MBEDTLS_SSL_EXT_ID_SIG_ALG_CERT;
Jerry Yu7a485c12022-10-31 13:08:18 +0800571
572 case MBEDTLS_TLS_EXT_KEY_SHARE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100573 return MBEDTLS_SSL_EXT_ID_KEY_SHARE;
Jerry Yu7a485c12022-10-31 13:08:18 +0800574
575 case MBEDTLS_TLS_EXT_TRUNCATED_HMAC:
Gilles Peskine449bd832023-01-11 14:50:10 +0100576 return MBEDTLS_SSL_EXT_ID_TRUNCATED_HMAC;
Jerry Yu7a485c12022-10-31 13:08:18 +0800577
578 case MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS:
Gilles Peskine449bd832023-01-11 14:50:10 +0100579 return MBEDTLS_SSL_EXT_ID_SUPPORTED_POINT_FORMATS;
Jerry Yu7a485c12022-10-31 13:08:18 +0800580
581 case MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC:
Gilles Peskine449bd832023-01-11 14:50:10 +0100582 return MBEDTLS_SSL_EXT_ID_ENCRYPT_THEN_MAC;
Jerry Yu7a485c12022-10-31 13:08:18 +0800583
584 case MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +0100585 return MBEDTLS_SSL_EXT_ID_EXTENDED_MASTER_SECRET;
Jerry Yu7a485c12022-10-31 13:08:18 +0800586
587 case MBEDTLS_TLS_EXT_SESSION_TICKET:
Gilles Peskine449bd832023-01-11 14:50:10 +0100588 return MBEDTLS_SSL_EXT_ID_SESSION_TICKET;
Jerry Yu7a485c12022-10-31 13:08:18 +0800589
590 }
591
Gilles Peskine449bd832023-01-11 14:50:10 +0100592 return MBEDTLS_SSL_EXT_ID_UNRECOGNIZED;
Jerry Yu7a485c12022-10-31 13:08:18 +0800593}
594
Gilles Peskine449bd832023-01-11 14:50:10 +0100595uint32_t mbedtls_ssl_get_extension_mask(unsigned int extension_type)
Jerry Yu7a485c12022-10-31 13:08:18 +0800596{
Gilles Peskine449bd832023-01-11 14:50:10 +0100597 return 1 << mbedtls_ssl_get_extension_id(extension_type);
Jerry Yu7a485c12022-10-31 13:08:18 +0800598}
599
Jerry Yud25cab02022-10-31 12:48:30 +0800600#if defined(MBEDTLS_DEBUG_C)
601static const char *extension_name_table[] = {
Jerry Yuea52ed92022-11-08 21:01:17 +0800602 [MBEDTLS_SSL_EXT_ID_UNRECOGNIZED] = "unrecognized",
Jerry Yud25cab02022-10-31 12:48:30 +0800603 [MBEDTLS_SSL_EXT_ID_SERVERNAME] = "server_name",
604 [MBEDTLS_SSL_EXT_ID_MAX_FRAGMENT_LENGTH] = "max_fragment_length",
605 [MBEDTLS_SSL_EXT_ID_STATUS_REQUEST] = "status_request",
606 [MBEDTLS_SSL_EXT_ID_SUPPORTED_GROUPS] = "supported_groups",
607 [MBEDTLS_SSL_EXT_ID_SIG_ALG] = "signature_algorithms",
608 [MBEDTLS_SSL_EXT_ID_USE_SRTP] = "use_srtp",
609 [MBEDTLS_SSL_EXT_ID_HEARTBEAT] = "heartbeat",
610 [MBEDTLS_SSL_EXT_ID_ALPN] = "application_layer_protocol_negotiation",
611 [MBEDTLS_SSL_EXT_ID_SCT] = "signed_certificate_timestamp",
612 [MBEDTLS_SSL_EXT_ID_CLI_CERT_TYPE] = "client_certificate_type",
613 [MBEDTLS_SSL_EXT_ID_SERV_CERT_TYPE] = "server_certificate_type",
614 [MBEDTLS_SSL_EXT_ID_PADDING] = "padding",
615 [MBEDTLS_SSL_EXT_ID_PRE_SHARED_KEY] = "pre_shared_key",
616 [MBEDTLS_SSL_EXT_ID_EARLY_DATA] = "early_data",
617 [MBEDTLS_SSL_EXT_ID_SUPPORTED_VERSIONS] = "supported_versions",
618 [MBEDTLS_SSL_EXT_ID_COOKIE] = "cookie",
619 [MBEDTLS_SSL_EXT_ID_PSK_KEY_EXCHANGE_MODES] = "psk_key_exchange_modes",
620 [MBEDTLS_SSL_EXT_ID_CERT_AUTH] = "certificate_authorities",
621 [MBEDTLS_SSL_EXT_ID_OID_FILTERS] = "oid_filters",
622 [MBEDTLS_SSL_EXT_ID_POST_HANDSHAKE_AUTH] = "post_handshake_auth",
623 [MBEDTLS_SSL_EXT_ID_SIG_ALG_CERT] = "signature_algorithms_cert",
624 [MBEDTLS_SSL_EXT_ID_KEY_SHARE] = "key_share",
625 [MBEDTLS_SSL_EXT_ID_TRUNCATED_HMAC] = "truncated_hmac",
626 [MBEDTLS_SSL_EXT_ID_SUPPORTED_POINT_FORMATS] = "supported_point_formats",
627 [MBEDTLS_SSL_EXT_ID_ENCRYPT_THEN_MAC] = "encrypt_then_mac",
628 [MBEDTLS_SSL_EXT_ID_EXTENDED_MASTER_SECRET] = "extended_master_secret",
629 [MBEDTLS_SSL_EXT_ID_SESSION_TICKET] = "session_ticket"
630};
631
Gilles Peskine449bd832023-01-11 14:50:10 +0100632static unsigned int extension_type_table[] = {
Jerry Yud25cab02022-10-31 12:48:30 +0800633 [MBEDTLS_SSL_EXT_ID_UNRECOGNIZED] = 0xff,
634 [MBEDTLS_SSL_EXT_ID_SERVERNAME] = MBEDTLS_TLS_EXT_SERVERNAME,
635 [MBEDTLS_SSL_EXT_ID_MAX_FRAGMENT_LENGTH] = MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH,
636 [MBEDTLS_SSL_EXT_ID_STATUS_REQUEST] = MBEDTLS_TLS_EXT_STATUS_REQUEST,
637 [MBEDTLS_SSL_EXT_ID_SUPPORTED_GROUPS] = MBEDTLS_TLS_EXT_SUPPORTED_GROUPS,
638 [MBEDTLS_SSL_EXT_ID_SIG_ALG] = MBEDTLS_TLS_EXT_SIG_ALG,
639 [MBEDTLS_SSL_EXT_ID_USE_SRTP] = MBEDTLS_TLS_EXT_USE_SRTP,
640 [MBEDTLS_SSL_EXT_ID_HEARTBEAT] = MBEDTLS_TLS_EXT_HEARTBEAT,
641 [MBEDTLS_SSL_EXT_ID_ALPN] = MBEDTLS_TLS_EXT_ALPN,
642 [MBEDTLS_SSL_EXT_ID_SCT] = MBEDTLS_TLS_EXT_SCT,
643 [MBEDTLS_SSL_EXT_ID_CLI_CERT_TYPE] = MBEDTLS_TLS_EXT_CLI_CERT_TYPE,
644 [MBEDTLS_SSL_EXT_ID_SERV_CERT_TYPE] = MBEDTLS_TLS_EXT_SERV_CERT_TYPE,
645 [MBEDTLS_SSL_EXT_ID_PADDING] = MBEDTLS_TLS_EXT_PADDING,
646 [MBEDTLS_SSL_EXT_ID_PRE_SHARED_KEY] = MBEDTLS_TLS_EXT_PRE_SHARED_KEY,
647 [MBEDTLS_SSL_EXT_ID_EARLY_DATA] = MBEDTLS_TLS_EXT_EARLY_DATA,
648 [MBEDTLS_SSL_EXT_ID_SUPPORTED_VERSIONS] = MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS,
649 [MBEDTLS_SSL_EXT_ID_COOKIE] = MBEDTLS_TLS_EXT_COOKIE,
650 [MBEDTLS_SSL_EXT_ID_PSK_KEY_EXCHANGE_MODES] = MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES,
651 [MBEDTLS_SSL_EXT_ID_CERT_AUTH] = MBEDTLS_TLS_EXT_CERT_AUTH,
652 [MBEDTLS_SSL_EXT_ID_OID_FILTERS] = MBEDTLS_TLS_EXT_OID_FILTERS,
653 [MBEDTLS_SSL_EXT_ID_POST_HANDSHAKE_AUTH] = MBEDTLS_TLS_EXT_POST_HANDSHAKE_AUTH,
654 [MBEDTLS_SSL_EXT_ID_SIG_ALG_CERT] = MBEDTLS_TLS_EXT_SIG_ALG_CERT,
655 [MBEDTLS_SSL_EXT_ID_KEY_SHARE] = MBEDTLS_TLS_EXT_KEY_SHARE,
656 [MBEDTLS_SSL_EXT_ID_TRUNCATED_HMAC] = MBEDTLS_TLS_EXT_TRUNCATED_HMAC,
657 [MBEDTLS_SSL_EXT_ID_SUPPORTED_POINT_FORMATS] = MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS,
658 [MBEDTLS_SSL_EXT_ID_ENCRYPT_THEN_MAC] = MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC,
659 [MBEDTLS_SSL_EXT_ID_EXTENDED_MASTER_SECRET] = MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET,
660 [MBEDTLS_SSL_EXT_ID_SESSION_TICKET] = MBEDTLS_TLS_EXT_SESSION_TICKET
661};
662
Gilles Peskine449bd832023-01-11 14:50:10 +0100663const char *mbedtls_ssl_get_extension_name(unsigned int extension_type)
Jerry Yud25cab02022-10-31 12:48:30 +0800664{
Gilles Peskine449bd832023-01-11 14:50:10 +0100665 return extension_name_table[
666 mbedtls_ssl_get_extension_id(extension_type)];
Jerry Yud25cab02022-10-31 12:48:30 +0800667}
668
Gilles Peskine449bd832023-01-11 14:50:10 +0100669static const char *ssl_tls13_get_hs_msg_name(int hs_msg_type)
Jerry Yud25cab02022-10-31 12:48:30 +0800670{
Gilles Peskine449bd832023-01-11 14:50:10 +0100671 switch (hs_msg_type) {
Jerry Yud25cab02022-10-31 12:48:30 +0800672 case MBEDTLS_SSL_HS_CLIENT_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +0100673 return "ClientHello";
Jerry Yud25cab02022-10-31 12:48:30 +0800674 case MBEDTLS_SSL_HS_SERVER_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +0100675 return "ServerHello";
Jerry Yud25cab02022-10-31 12:48:30 +0800676 case MBEDTLS_SSL_TLS1_3_HS_HELLO_RETRY_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +0100677 return "HelloRetryRequest";
Jerry Yud25cab02022-10-31 12:48:30 +0800678 case MBEDTLS_SSL_HS_NEW_SESSION_TICKET:
Gilles Peskine449bd832023-01-11 14:50:10 +0100679 return "NewSessionTicket";
Jerry Yud25cab02022-10-31 12:48:30 +0800680 case MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS:
Gilles Peskine449bd832023-01-11 14:50:10 +0100681 return "EncryptedExtensions";
Jerry Yud25cab02022-10-31 12:48:30 +0800682 case MBEDTLS_SSL_HS_CERTIFICATE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100683 return "Certificate";
Jerry Yud25cab02022-10-31 12:48:30 +0800684 case MBEDTLS_SSL_HS_CERTIFICATE_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +0100685 return "CertificateRequest";
Jerry Yud25cab02022-10-31 12:48:30 +0800686 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100687 return "Unknown";
Jerry Yud25cab02022-10-31 12:48:30 +0800688}
689
Gilles Peskine449bd832023-01-11 14:50:10 +0100690void mbedtls_ssl_print_extension(const mbedtls_ssl_context *ssl,
691 int level, const char *file, int line,
692 int hs_msg_type, unsigned int extension_type,
693 const char *extra_msg0, const char *extra_msg1)
Jerry Yud25cab02022-10-31 12:48:30 +0800694{
695 const char *extra_msg;
Gilles Peskine449bd832023-01-11 14:50:10 +0100696 if (extra_msg0 && extra_msg1) {
Jerry Yud25cab02022-10-31 12:48:30 +0800697 mbedtls_debug_print_msg(
698 ssl, level, file, line,
699 "%s: %s(%u) extension %s %s.",
Gilles Peskine449bd832023-01-11 14:50:10 +0100700 ssl_tls13_get_hs_msg_name(hs_msg_type),
701 mbedtls_ssl_get_extension_name(extension_type),
Jerry Yud25cab02022-10-31 12:48:30 +0800702 extension_type,
Gilles Peskine449bd832023-01-11 14:50:10 +0100703 extra_msg0, extra_msg1);
Jerry Yud25cab02022-10-31 12:48:30 +0800704 return;
705 }
706
707 extra_msg = extra_msg0 ? extra_msg0 : extra_msg1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100708 if (extra_msg) {
Jerry Yud25cab02022-10-31 12:48:30 +0800709 mbedtls_debug_print_msg(
710 ssl, level, file, line,
Gilles Peskine449bd832023-01-11 14:50:10 +0100711 "%s: %s(%u) extension %s.", ssl_tls13_get_hs_msg_name(hs_msg_type),
712 mbedtls_ssl_get_extension_name(extension_type), extension_type,
713 extra_msg);
Jerry Yud25cab02022-10-31 12:48:30 +0800714 return;
715 }
716
717 mbedtls_debug_print_msg(
718 ssl, level, file, line,
Gilles Peskine449bd832023-01-11 14:50:10 +0100719 "%s: %s(%u) extension.", ssl_tls13_get_hs_msg_name(hs_msg_type),
720 mbedtls_ssl_get_extension_name(extension_type), extension_type);
Jerry Yud25cab02022-10-31 12:48:30 +0800721}
722
Gilles Peskine449bd832023-01-11 14:50:10 +0100723void mbedtls_ssl_print_extensions(const mbedtls_ssl_context *ssl,
724 int level, const char *file, int line,
725 int hs_msg_type, uint32_t extensions_mask,
726 const char *extra)
Jerry Yud25cab02022-10-31 12:48:30 +0800727{
728
Gilles Peskine449bd832023-01-11 14:50:10 +0100729 for (unsigned i = 0;
730 i < sizeof(extension_name_table) / sizeof(extension_name_table[0]);
731 i++) {
Jerry Yu79aa7212022-11-08 21:30:21 +0800732 mbedtls_ssl_print_extension(
Jerry Yuea52ed92022-11-08 21:01:17 +0800733 ssl, level, file, line, hs_msg_type, extension_type_table[i],
Gilles Peskine449bd832023-01-11 14:50:10 +0100734 extensions_mask & (1 << i) ? "exists" : "does not exist", extra);
Jerry Yud25cab02022-10-31 12:48:30 +0800735 }
736}
737
Pengyu Lvee455c02023-01-12 14:37:24 +0800738#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS)
739#define ARRAY_LENGTH(a) (sizeof(a) / sizeof(*(a)))
740
741static const char *ticket_flag_name_table[] =
742{
743 [0] = "ALLOW_PSK_RESUMPTION",
744 [2] = "ALLOW_PSK_EPHEMERAL_RESUMPTION",
745 [3] = "ALLOW_EARLY_DATA",
746};
747
Pengyu Lv4938a562023-01-16 11:28:49 +0800748void mbedtls_ssl_print_ticket_flags(const mbedtls_ssl_context *ssl,
749 int level, const char *file, int line,
750 unsigned int flags)
Pengyu Lvee455c02023-01-12 14:37:24 +0800751{
752 size_t i;
753
754 mbedtls_debug_print_msg(ssl, level, file, line,
Pengyu Lv4938a562023-01-16 11:28:49 +0800755 "print ticket_flags (0x%02x)", flags);
756
757 flags = flags & MBEDTLS_SSL_TLS1_3_TICKET_FLAGS_MASK;
Pengyu Lvee455c02023-01-12 14:37:24 +0800758
759 for (i = 0; i < ARRAY_LENGTH(ticket_flag_name_table); i++) {
Pengyu Lv4938a562023-01-16 11:28:49 +0800760 if ((flags & (1 << i))) {
Pengyu Lvee455c02023-01-12 14:37:24 +0800761 mbedtls_debug_print_msg(ssl, level, file, line, "- %s is set.",
762 ticket_flag_name_table[i]);
763 }
764 }
765}
766#endif /* MBEDTLS_SSL_PROTO_TLS1_3 && MBEDTLS_SSL_SESSION_TICKETS */
767
Jerry Yud25cab02022-10-31 12:48:30 +0800768#endif /* MBEDTLS_DEBUG_C */
769
Gilles Peskine449bd832023-01-11 14:50:10 +0100770void mbedtls_ssl_optimize_checksum(mbedtls_ssl_context *ssl,
771 const mbedtls_ssl_ciphersuite_t *ciphersuite_info)
Jerry Yuc73c6182022-02-08 20:29:25 +0800772{
773 ((void) ciphersuite_info);
774
Andrzej Kurek25f27152022-08-17 16:09:31 -0400775#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +0100776 if (ciphersuite_info->mac == MBEDTLS_MD_SHA384) {
Jerry Yuc73c6182022-02-08 20:29:25 +0800777 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
Gilles Peskine449bd832023-01-11 14:50:10 +0100778 } else
Jerry Yuc73c6182022-02-08 20:29:25 +0800779#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400780#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +0100781 if (ciphersuite_info->mac != MBEDTLS_MD_SHA384) {
Jerry Yuc73c6182022-02-08 20:29:25 +0800782 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Gilles Peskine449bd832023-01-11 14:50:10 +0100783 } else
Jerry Yuc73c6182022-02-08 20:29:25 +0800784#endif
785 {
Gilles Peskine449bd832023-01-11 14:50:10 +0100786 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
Jerry Yuc73c6182022-02-08 20:29:25 +0800787 return;
788 }
789}
790
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100791int mbedtls_ssl_add_hs_hdr_to_checksum(mbedtls_ssl_context *ssl,
Gilles Peskine449bd832023-01-11 14:50:10 +0100792 unsigned hs_type,
793 size_t total_hs_len)
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100794{
795 unsigned char hs_hdr[4];
796
797 /* Build HS header for checksum update. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100798 hs_hdr[0] = MBEDTLS_BYTE_0(hs_type);
799 hs_hdr[1] = MBEDTLS_BYTE_2(total_hs_len);
800 hs_hdr[2] = MBEDTLS_BYTE_1(total_hs_len);
801 hs_hdr[3] = MBEDTLS_BYTE_0(total_hs_len);
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100802
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100803 return ssl->handshake->update_checksum(ssl, hs_hdr, sizeof(hs_hdr));
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100804}
805
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100806int mbedtls_ssl_add_hs_msg_to_checksum(mbedtls_ssl_context *ssl,
Gilles Peskine449bd832023-01-11 14:50:10 +0100807 unsigned hs_type,
808 unsigned char const *msg,
809 size_t msg_len)
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100810{
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100811 int ret;
812 ret = mbedtls_ssl_add_hs_hdr_to_checksum(ssl, hs_type, msg_len);
813 if (ret != 0)
814 return ret;
815 return ssl->handshake->update_checksum(ssl, msg, msg_len);
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100816}
817
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100818int mbedtls_ssl_reset_checksum(mbedtls_ssl_context *ssl)
Jerry Yuc73c6182022-02-08 20:29:25 +0800819{
820 ((void) ssl);
Andrzej Kurek25f27152022-08-17 16:09:31 -0400821#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800822#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100823 psa_hash_abort(&ssl->handshake->fin_sha256_psa);
824 psa_hash_setup(&ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256);
Jerry Yuc73c6182022-02-08 20:29:25 +0800825#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100826 mbedtls_sha256_starts(&ssl->handshake->fin_sha256, 0);
Jerry Yuc73c6182022-02-08 20:29:25 +0800827#endif
828#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400829#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800830#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100831 psa_hash_abort(&ssl->handshake->fin_sha384_psa);
832 psa_hash_setup(&ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384);
Jerry Yuc73c6182022-02-08 20:29:25 +0800833#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100834 mbedtls_sha512_starts(&ssl->handshake->fin_sha384, 1);
Jerry Yuc73c6182022-02-08 20:29:25 +0800835#endif
836#endif
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100837 return 0;
Jerry Yuc73c6182022-02-08 20:29:25 +0800838}
839
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100840static int ssl_update_checksum_start(mbedtls_ssl_context *ssl,
Gilles Peskine449bd832023-01-11 14:50:10 +0100841 const unsigned char *buf, size_t len)
Jerry Yuc73c6182022-02-08 20:29:25 +0800842{
Andrzej Kurek25f27152022-08-17 16:09:31 -0400843#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800844#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100845 psa_hash_update(&ssl->handshake->fin_sha256_psa, buf, len);
Jerry Yuc73c6182022-02-08 20:29:25 +0800846#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100847 mbedtls_sha256_update(&ssl->handshake->fin_sha256, buf, len);
Jerry Yuc73c6182022-02-08 20:29:25 +0800848#endif
849#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400850#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800851#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100852 psa_hash_update(&ssl->handshake->fin_sha384_psa, buf, len);
Jerry Yuc73c6182022-02-08 20:29:25 +0800853#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100854 mbedtls_sha512_update(&ssl->handshake->fin_sha384, buf, len);
Jerry Yuc73c6182022-02-08 20:29:25 +0800855#endif
856#endif
Andrzej Kurekeabeb302022-10-17 07:52:51 -0400857#if !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
858 !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
859 (void) ssl;
860 (void) buf;
861 (void) len;
862#endif
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100863 return 0;
Jerry Yuc73c6182022-02-08 20:29:25 +0800864}
865
Andrzej Kurek25f27152022-08-17 16:09:31 -0400866#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100867static int ssl_update_checksum_sha256(mbedtls_ssl_context *ssl,
Gilles Peskine449bd832023-01-11 14:50:10 +0100868 const unsigned char *buf, size_t len)
Jerry Yuc73c6182022-02-08 20:29:25 +0800869{
870#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100871 psa_hash_update(&ssl->handshake->fin_sha256_psa, buf, len);
Jerry Yuc73c6182022-02-08 20:29:25 +0800872#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100873 mbedtls_sha256_update(&ssl->handshake->fin_sha256, buf, len);
Jerry Yuc73c6182022-02-08 20:29:25 +0800874#endif
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100875 return 0;
Jerry Yuc73c6182022-02-08 20:29:25 +0800876}
877#endif
878
Andrzej Kurek25f27152022-08-17 16:09:31 -0400879#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100880static int ssl_update_checksum_sha384(mbedtls_ssl_context *ssl,
Gilles Peskine449bd832023-01-11 14:50:10 +0100881 const unsigned char *buf, size_t len)
Jerry Yuc73c6182022-02-08 20:29:25 +0800882{
883#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100884 psa_hash_update(&ssl->handshake->fin_sha384_psa, buf, len);
Jerry Yuc73c6182022-02-08 20:29:25 +0800885#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100886 mbedtls_sha512_update(&ssl->handshake->fin_sha384, buf, len);
Jerry Yuc73c6182022-02-08 20:29:25 +0800887#endif
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100888 return 0;
Jerry Yuc73c6182022-02-08 20:29:25 +0800889}
890#endif
891
Gilles Peskine449bd832023-01-11 14:50:10 +0100892static void ssl_handshake_params_init(mbedtls_ssl_handshake_params *handshake)
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200893{
Gilles Peskine449bd832023-01-11 14:50:10 +0100894 memset(handshake, 0, sizeof(mbedtls_ssl_handshake_params));
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200895
Andrzej Kurek25f27152022-08-17 16:09:31 -0400896#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -0500897#if defined(MBEDTLS_USE_PSA_CRYPTO)
898 handshake->fin_sha256_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -0500899#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100900 mbedtls_sha256_init(&handshake->fin_sha256);
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200901#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -0500902#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400903#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -0500904#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -0500905 handshake->fin_sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -0500906#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100907 mbedtls_sha512_init(&handshake->fin_sha384);
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200908#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -0500909#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200910
911 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +0100912
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200913#if defined(MBEDTLS_DHM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100914 mbedtls_dhm_init(&handshake->dhm_ctx);
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200915#endif
Neil Armstrongf3f46412022-04-12 14:43:39 +0200916#if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100917 mbedtls_ecdh_init(&handshake->ecdh_ctx);
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200918#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +0200919#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Neil Armstrongca7d5062022-05-31 14:43:23 +0200920#if defined(MBEDTLS_USE_PSA_CRYPTO)
921 handshake->psa_pake_ctx = psa_pake_operation_init();
922 handshake->psa_pake_password = MBEDTLS_SVC_KEY_ID_INIT;
923#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100924 mbedtls_ecjpake_init(&handshake->ecjpake_ctx);
Neil Armstrongca7d5062022-05-31 14:43:23 +0200925#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +0200926#if defined(MBEDTLS_SSL_CLI_C)
927 handshake->ecjpake_cache = NULL;
928 handshake->ecjpake_cache_len = 0;
929#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +0200930#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +0200931
Gilles Peskineeccd8882020-03-10 12:19:08 +0100932#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100933 mbedtls_x509_crt_restart_init(&handshake->ecrs_ctx);
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +0200934#endif
935
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +0200936#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
937 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
938#endif
Hanno Becker75173122019-02-06 16:18:31 +0000939
940#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
941 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100942 mbedtls_pk_init(&handshake->peer_pubkey);
Hanno Becker75173122019-02-06 16:18:31 +0000943#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200944}
945
Gilles Peskine449bd832023-01-11 14:50:10 +0100946void mbedtls_ssl_transform_init(mbedtls_ssl_transform *transform)
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200947{
Gilles Peskine449bd832023-01-11 14:50:10 +0100948 memset(transform, 0, sizeof(mbedtls_ssl_transform));
Paul Bakker84bbeb52014-07-01 14:53:22 +0200949
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +0100950#if defined(MBEDTLS_USE_PSA_CRYPTO)
951 transform->psa_key_enc = MBEDTLS_SVC_KEY_ID_INIT;
952 transform->psa_key_dec = MBEDTLS_SVC_KEY_ID_INIT;
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +0100953#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100954 mbedtls_cipher_init(&transform->cipher_ctx_enc);
955 mbedtls_cipher_init(&transform->cipher_ctx_dec);
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +0100956#endif
957
Hanno Beckerfd86ca82020-11-30 08:54:23 +0000958#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong39b8e7d2022-02-23 09:24:45 +0100959#if defined(MBEDTLS_USE_PSA_CRYPTO)
960 transform->psa_mac_enc = MBEDTLS_SVC_KEY_ID_INIT;
961 transform->psa_mac_dec = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrongcf8841a2022-02-24 11:17:45 +0100962#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100963 mbedtls_md_init(&transform->md_ctx_enc);
964 mbedtls_md_init(&transform->md_ctx_dec);
Hanno Beckerd56ed242018-01-03 15:32:51 +0000965#endif
Neil Armstrongcf8841a2022-02-24 11:17:45 +0100966#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200967}
968
Gilles Peskine449bd832023-01-11 14:50:10 +0100969void mbedtls_ssl_session_init(mbedtls_ssl_session *session)
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200970{
Gilles Peskine449bd832023-01-11 14:50:10 +0100971 memset(session, 0, sizeof(mbedtls_ssl_session));
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200972}
973
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200974MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100975static int ssl_handshake_init(mbedtls_ssl_context *ssl)
Paul Bakker48916f92012-09-16 19:57:18 +0000976{
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100977 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
978
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200979 /* Clear old handshake information if present */
Jerry Yu2e199812022-12-01 18:57:19 +0800980#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +0100981 if (ssl->transform_negotiate) {
982 mbedtls_ssl_transform_free(ssl->transform_negotiate);
983 }
Jerry Yu2e199812022-12-01 18:57:19 +0800984#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100985 if (ssl->session_negotiate) {
986 mbedtls_ssl_session_free(ssl->session_negotiate);
987 }
988 if (ssl->handshake) {
989 mbedtls_ssl_handshake_free(ssl);
990 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200991
Jerry Yu2e199812022-12-01 18:57:19 +0800992#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200993 /*
994 * Either the pointers are now NULL or cleared properly and can be freed.
995 * Now allocate missing structures.
996 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100997 if (ssl->transform_negotiate == NULL) {
998 ssl->transform_negotiate = mbedtls_calloc(1, sizeof(mbedtls_ssl_transform));
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200999 }
Jerry Yu2e199812022-12-01 18:57:19 +08001000#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker48916f92012-09-16 19:57:18 +00001001
Gilles Peskine449bd832023-01-11 14:50:10 +01001002 if (ssl->session_negotiate == NULL) {
1003 ssl->session_negotiate = mbedtls_calloc(1, sizeof(mbedtls_ssl_session));
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001004 }
Paul Bakker48916f92012-09-16 19:57:18 +00001005
Gilles Peskine449bd832023-01-11 14:50:10 +01001006 if (ssl->handshake == NULL) {
1007 ssl->handshake = mbedtls_calloc(1, sizeof(mbedtls_ssl_handshake_params));
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001008 }
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05001009#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1010 /* If the buffers are too small - reallocate */
Andrzej Kurek8ea68722020-04-03 06:40:47 -04001011
Gilles Peskine449bd832023-01-11 14:50:10 +01001012 handle_buffer_resizing(ssl, 0, MBEDTLS_SSL_IN_BUFFER_LEN,
1013 MBEDTLS_SSL_OUT_BUFFER_LEN);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05001014#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001015
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001016 /* All pointers should exist and can be directly freed without issue */
Gilles Peskine449bd832023-01-11 14:50:10 +01001017 if (ssl->handshake == NULL ||
Jerry Yu2e199812022-12-01 18:57:19 +08001018#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker48916f92012-09-16 19:57:18 +00001019 ssl->transform_negotiate == NULL ||
Jerry Yu2e199812022-12-01 18:57:19 +08001020#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001021 ssl->session_negotiate == NULL) {
1022 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc() of ssl sub-contexts failed"));
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001023
Gilles Peskine449bd832023-01-11 14:50:10 +01001024 mbedtls_free(ssl->handshake);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001025 ssl->handshake = NULL;
Jerry Yu2e199812022-12-01 18:57:19 +08001026
1027#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01001028 mbedtls_free(ssl->transform_negotiate);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001029 ssl->transform_negotiate = NULL;
Jerry Yu2e199812022-12-01 18:57:19 +08001030#endif
1031
Gilles Peskine449bd832023-01-11 14:50:10 +01001032 mbedtls_free(ssl->session_negotiate);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001033 ssl->session_negotiate = NULL;
1034
Gilles Peskine449bd832023-01-11 14:50:10 +01001035 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Paul Bakker48916f92012-09-16 19:57:18 +00001036 }
1037
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001038 /* Initialize structures */
Gilles Peskine449bd832023-01-11 14:50:10 +01001039 mbedtls_ssl_session_init(ssl->session_negotiate);
1040 ssl_handshake_params_init(ssl->handshake);
Paul Bakker968afaa2014-07-09 11:09:24 +02001041
Jerry Yu2e199812022-12-01 18:57:19 +08001042#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01001043 mbedtls_ssl_transform_init(ssl->transform_negotiate);
Jerry Yu2e199812022-12-01 18:57:19 +08001044#endif
1045
Manuel Pégourié-Gonnard537f2312023-02-05 10:17:45 +01001046 /* Setup handshake checksums */
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001047 ret = mbedtls_ssl_reset_checksum(ssl);
1048 if (ret != 0) {
1049 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_reset_checksum", ret);
1050 return ret;
1051 }
Manuel Pégourié-Gonnard537f2312023-02-05 10:17:45 +01001052
Jerry Yud0766ec2022-09-22 10:46:57 +08001053#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
1054 defined(MBEDTLS_SSL_SRV_C) && \
1055 defined(MBEDTLS_SSL_SESSION_TICKETS)
1056 ssl->handshake->new_session_tickets_count =
Gilles Peskine449bd832023-01-11 14:50:10 +01001057 ssl->conf->new_session_tickets_count;
Jerry Yud0766ec2022-09-22 10:46:57 +08001058#endif
1059
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001060#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001061 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001062 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001063
Gilles Peskine449bd832023-01-11 14:50:10 +01001064 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001065 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Gilles Peskine449bd832023-01-11 14:50:10 +01001066 } else {
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001067 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Gilles Peskine449bd832023-01-11 14:50:10 +01001068 }
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001069
Gilles Peskine449bd832023-01-11 14:50:10 +01001070 mbedtls_ssl_set_timer(ssl, 0);
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001071 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001072#endif
1073
Brett Warrene0edc842021-08-17 09:53:13 +01001074/*
1075 * curve_list is translated to IANA TLS group identifiers here because
1076 * mbedtls_ssl_conf_curves returns void and so can't return
1077 * any error codes.
1078 */
1079#if defined(MBEDTLS_ECP_C)
1080#if !defined(MBEDTLS_DEPRECATED_REMOVED)
1081 /* Heap allocate and translate curve_list from internal to IANA group ids */
Gilles Peskine449bd832023-01-11 14:50:10 +01001082 if (ssl->conf->curve_list != NULL) {
Brett Warrene0edc842021-08-17 09:53:13 +01001083 size_t length;
1084 const mbedtls_ecp_group_id *curve_list = ssl->conf->curve_list;
1085
Gilles Peskine449bd832023-01-11 14:50:10 +01001086 for (length = 0; (curve_list[length] != MBEDTLS_ECP_DP_NONE) &&
1087 (length < MBEDTLS_ECP_DP_MAX); length++) {
1088 }
Brett Warrene0edc842021-08-17 09:53:13 +01001089
1090 /* Leave room for zero termination */
Gilles Peskine449bd832023-01-11 14:50:10 +01001091 uint16_t *group_list = mbedtls_calloc(length + 1, sizeof(uint16_t));
1092 if (group_list == NULL) {
1093 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
1094 }
Brett Warrene0edc842021-08-17 09:53:13 +01001095
Gilles Peskine449bd832023-01-11 14:50:10 +01001096 for (size_t i = 0; i < length; i++) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01001097 uint16_t tls_id = mbedtls_ssl_get_tls_id_from_ecp_group_id(
Gilles Peskine449bd832023-01-11 14:50:10 +01001098 curve_list[i]);
1099 if (tls_id == 0) {
1100 mbedtls_free(group_list);
1101 return MBEDTLS_ERR_SSL_BAD_CONFIG;
Brett Warrene0edc842021-08-17 09:53:13 +01001102 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01001103 group_list[i] = tls_id;
Brett Warrene0edc842021-08-17 09:53:13 +01001104 }
1105
1106 group_list[length] = 0;
1107
1108 ssl->handshake->group_list = group_list;
1109 ssl->handshake->group_list_heap_allocated = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001110 } else {
Brett Warrene0edc842021-08-17 09:53:13 +01001111 ssl->handshake->group_list = ssl->conf->group_list;
1112 ssl->handshake->group_list_heap_allocated = 0;
1113 }
1114#endif /* MBEDTLS_DEPRECATED_REMOVED */
1115#endif /* MBEDTLS_ECP_C */
1116
Ronald Crone68ab4f2022-10-05 12:46:29 +02001117#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +08001118#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Jerry Yua69269a2022-01-17 21:06:01 +08001119#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yu713013f2022-01-17 18:16:35 +08001120 /* Heap allocate and translate sig_hashes from internal hash identifiers to
1121 signature algorithms IANA identifiers. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001122 if (mbedtls_ssl_conf_is_tls12_only(ssl->conf) &&
1123 ssl->conf->sig_hashes != NULL) {
Jerry Yuf017ee42022-01-12 15:49:48 +08001124 const int *md;
1125 const int *sig_hashes = ssl->conf->sig_hashes;
Jerry Yub476a442022-01-21 18:14:45 +08001126 size_t sig_algs_len = 0;
Jerry Yuf017ee42022-01-12 15:49:48 +08001127 uint16_t *p;
1128
Jerry Yub476a442022-01-21 18:14:45 +08001129#if defined(static_assert)
Gilles Peskine449bd832023-01-11 14:50:10 +01001130 static_assert(MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN
1131 <= (SIZE_MAX - (2 * sizeof(uint16_t))),
1132 "MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN too big");
Jerry Yua68dca22022-01-20 16:28:27 +08001133#endif
1134
Gilles Peskine449bd832023-01-11 14:50:10 +01001135 for (md = sig_hashes; *md != MBEDTLS_MD_NONE; md++) {
1136 if (mbedtls_ssl_hash_from_md_alg(*md) == MBEDTLS_SSL_HASH_NONE) {
Jerry Yuf017ee42022-01-12 15:49:48 +08001137 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001138 }
Jerry Yub476a442022-01-21 18:14:45 +08001139#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001140 sig_algs_len += sizeof(uint16_t);
Jerry Yub476a442022-01-21 18:14:45 +08001141#endif
Jerry Yua68dca22022-01-20 16:28:27 +08001142
Jerry Yub476a442022-01-21 18:14:45 +08001143#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001144 sig_algs_len += sizeof(uint16_t);
Jerry Yub476a442022-01-21 18:14:45 +08001145#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001146 if (sig_algs_len > MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN) {
1147 return MBEDTLS_ERR_SSL_BAD_CONFIG;
1148 }
Jerry Yuf017ee42022-01-12 15:49:48 +08001149 }
1150
Gilles Peskine449bd832023-01-11 14:50:10 +01001151 if (sig_algs_len < MBEDTLS_SSL_MIN_SIG_ALG_LIST_LEN) {
1152 return MBEDTLS_ERR_SSL_BAD_CONFIG;
1153 }
Jerry Yuf017ee42022-01-12 15:49:48 +08001154
Gilles Peskine449bd832023-01-11 14:50:10 +01001155 ssl->handshake->sig_algs = mbedtls_calloc(1, sig_algs_len +
1156 sizeof(uint16_t));
1157 if (ssl->handshake->sig_algs == NULL) {
1158 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
1159 }
Jerry Yuf017ee42022-01-12 15:49:48 +08001160
Gilles Peskine449bd832023-01-11 14:50:10 +01001161 p = (uint16_t *) ssl->handshake->sig_algs;
1162 for (md = sig_hashes; *md != MBEDTLS_MD_NONE; md++) {
1163 unsigned char hash = mbedtls_ssl_hash_from_md_alg(*md);
1164 if (hash == MBEDTLS_SSL_HASH_NONE) {
Jerry Yuf017ee42022-01-12 15:49:48 +08001165 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001166 }
Jerry Yu6106fdc2022-01-12 16:36:14 +08001167#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001168 *p = ((hash << 8) | MBEDTLS_SSL_SIG_ECDSA);
Jerry Yuf017ee42022-01-12 15:49:48 +08001169 p++;
Jerry Yu6106fdc2022-01-12 16:36:14 +08001170#endif
1171#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001172 *p = ((hash << 8) | MBEDTLS_SSL_SIG_RSA);
Jerry Yuf017ee42022-01-12 15:49:48 +08001173 p++;
Jerry Yu6106fdc2022-01-12 16:36:14 +08001174#endif
Jerry Yuf017ee42022-01-12 15:49:48 +08001175 }
Gabor Mezei15b95a62022-05-09 16:37:58 +02001176 *p = MBEDTLS_TLS_SIG_NONE;
Jerry Yuf017ee42022-01-12 15:49:48 +08001177 ssl->handshake->sig_algs_heap_allocated = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001178 } else
Jerry Yua69269a2022-01-17 21:06:01 +08001179#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Jerry Yuf017ee42022-01-12 15:49:48 +08001180 {
Jerry Yuf017ee42022-01-12 15:49:48 +08001181 ssl->handshake->sig_algs_heap_allocated = 0;
1182 }
Jerry Yucc539102022-06-27 16:27:35 +08001183#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Ronald Crone68ab4f2022-10-05 12:46:29 +02001184#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Gilles Peskine449bd832023-01-11 14:50:10 +01001185 return 0;
Paul Bakker48916f92012-09-16 19:57:18 +00001186}
1187
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001188#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001189/* Dummy cookie callbacks for defaults */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001190MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001191static int ssl_cookie_write_dummy(void *ctx,
1192 unsigned char **p, unsigned char *end,
1193 const unsigned char *cli_id, size_t cli_id_len)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001194{
1195 ((void) ctx);
1196 ((void) p);
1197 ((void) end);
1198 ((void) cli_id);
1199 ((void) cli_id_len);
1200
Gilles Peskine449bd832023-01-11 14:50:10 +01001201 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001202}
1203
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001204MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001205static int ssl_cookie_check_dummy(void *ctx,
1206 const unsigned char *cookie, size_t cookie_len,
1207 const unsigned char *cli_id, size_t cli_id_len)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001208{
1209 ((void) ctx);
1210 ((void) cookie);
1211 ((void) cookie_len);
1212 ((void) cli_id);
1213 ((void) cli_id_len);
1214
Gilles Peskine449bd832023-01-11 14:50:10 +01001215 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001216}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001217#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001218
Paul Bakker5121ce52009-01-03 21:22:43 +00001219/*
1220 * Initialize an SSL context
1221 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001222void mbedtls_ssl_init(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001223{
Gilles Peskine449bd832023-01-11 14:50:10 +01001224 memset(ssl, 0, sizeof(mbedtls_ssl_context));
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001225}
1226
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001227MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001228static int ssl_conf_version_check(const mbedtls_ssl_context *ssl)
Jerry Yu60835a82021-08-04 10:13:52 +08001229{
Ronald Cron086ee0b2022-03-15 15:18:51 +01001230 const mbedtls_ssl_config *conf = ssl->conf;
1231
Ronald Cron6f135e12021-12-08 16:57:54 +01001232#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001233 if (mbedtls_ssl_conf_is_tls13_only(conf)) {
1234 if (conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
1235 MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS 1.3 is not yet supported."));
1236 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQianed582dd2022-04-13 08:21:05 +00001237 }
1238
Gilles Peskine449bd832023-01-11 14:50:10 +01001239 MBEDTLS_SSL_DEBUG_MSG(4, ("The SSL configuration is tls13 only."));
1240 return 0;
Jerry Yu60835a82021-08-04 10:13:52 +08001241 }
1242#endif
1243
1244#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01001245 if (mbedtls_ssl_conf_is_tls12_only(conf)) {
1246 MBEDTLS_SSL_DEBUG_MSG(4, ("The SSL configuration is tls12 only."));
1247 return 0;
Jerry Yu60835a82021-08-04 10:13:52 +08001248 }
1249#endif
1250
Ronald Cron6f135e12021-12-08 16:57:54 +01001251#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001252 if (mbedtls_ssl_conf_is_hybrid_tls12_tls13(conf)) {
1253 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
1254 MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS not yet supported in Hybrid TLS 1.3 + TLS 1.2"));
1255 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQianed582dd2022-04-13 08:21:05 +00001256 }
1257
Gilles Peskine449bd832023-01-11 14:50:10 +01001258 if (conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
1259 MBEDTLS_SSL_DEBUG_MSG(1, ("TLS 1.3 server is not supported yet."));
1260 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQian8f9dfe42022-04-15 02:52:39 +00001261 }
1262
1263
Gilles Peskine449bd832023-01-11 14:50:10 +01001264 MBEDTLS_SSL_DEBUG_MSG(4, ("The SSL configuration is TLS 1.3 or TLS 1.2."));
1265 return 0;
Jerry Yu60835a82021-08-04 10:13:52 +08001266 }
1267#endif
1268
Gilles Peskine449bd832023-01-11 14:50:10 +01001269 MBEDTLS_SSL_DEBUG_MSG(1, ("The SSL configuration is invalid."));
1270 return MBEDTLS_ERR_SSL_BAD_CONFIG;
Jerry Yu60835a82021-08-04 10:13:52 +08001271}
1272
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001273MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu60835a82021-08-04 10:13:52 +08001274static int ssl_conf_check(const mbedtls_ssl_context *ssl)
1275{
1276 int ret;
Gilles Peskine449bd832023-01-11 14:50:10 +01001277 ret = ssl_conf_version_check(ssl);
1278 if (ret != 0) {
1279 return ret;
1280 }
Jerry Yu60835a82021-08-04 10:13:52 +08001281
Jerry Yudef7ae42022-10-30 14:13:19 +08001282#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1283 /* RFC 8446 section 4.4.3
1284 *
1285 * If the verification fails, the receiver MUST terminate the handshake with
1286 * a "decrypt_error" alert.
1287 *
1288 * If the client is configured as TLS 1.3 only with optional verify, return
1289 * bad config.
1290 *
1291 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001292 if (mbedtls_ssl_conf_tls13_ephemeral_enabled(
1293 (mbedtls_ssl_context *) ssl) &&
Jerry Yudef7ae42022-10-30 14:13:19 +08001294 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
1295 ssl->conf->max_tls_version == MBEDTLS_SSL_VERSION_TLS1_3 &&
1296 ssl->conf->min_tls_version == MBEDTLS_SSL_VERSION_TLS1_3 &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001297 ssl->conf->authmode == MBEDTLS_SSL_VERIFY_OPTIONAL) {
Jerry Yudef7ae42022-10-30 14:13:19 +08001298 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +01001299 1, ("Optional verify auth mode "
1300 "is not available for TLS 1.3 client"));
1301 return MBEDTLS_ERR_SSL_BAD_CONFIG;
Jerry Yudef7ae42022-10-30 14:13:19 +08001302 }
1303#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
1304
Jerry Yu60835a82021-08-04 10:13:52 +08001305 /* Space for further checks */
1306
Gilles Peskine449bd832023-01-11 14:50:10 +01001307 return 0;
Jerry Yu60835a82021-08-04 10:13:52 +08001308}
1309
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001310/*
1311 * Setup an SSL context
1312 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01001313
Gilles Peskine449bd832023-01-11 14:50:10 +01001314int mbedtls_ssl_setup(mbedtls_ssl_context *ssl,
1315 const mbedtls_ssl_config *conf)
Paul Bakker5121ce52009-01-03 21:22:43 +00001316{
Janos Follath865b3eb2019-12-16 11:46:15 +00001317 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Darryl Greenb33cc762019-11-28 14:29:44 +00001318 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1319 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00001320
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001321 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00001322
Gilles Peskine449bd832023-01-11 14:50:10 +01001323 if ((ret = ssl_conf_check(ssl)) != 0) {
1324 return ret;
1325 }
Jerry Yu60835a82021-08-04 10:13:52 +08001326
Paul Bakker62f2dee2012-09-28 07:31:51 +00001327 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001328 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00001329 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02001330
1331 /* Set to NULL in case of an error condition */
1332 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02001333
Darryl Greenb33cc762019-11-28 14:29:44 +00001334#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1335 ssl->in_buf_len = in_buf_len;
1336#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001337 ssl->in_buf = mbedtls_calloc(1, in_buf_len);
1338 if (ssl->in_buf == NULL) {
1339 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", in_buf_len));
k-stachowiak9f7798e2018-07-31 16:52:32 +02001340 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02001341 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10001342 }
1343
Darryl Greenb33cc762019-11-28 14:29:44 +00001344#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1345 ssl->out_buf_len = out_buf_len;
1346#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001347 ssl->out_buf = mbedtls_calloc(1, out_buf_len);
1348 if (ssl->out_buf == NULL) {
1349 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", out_buf_len));
k-stachowiak9f7798e2018-07-31 16:52:32 +02001350 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02001351 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00001352 }
1353
Gilles Peskine449bd832023-01-11 14:50:10 +01001354 mbedtls_ssl_reset_in_out_pointers(ssl);
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02001355
Johan Pascalb62bb512015-12-03 21:56:45 +01001356#if defined(MBEDTLS_SSL_DTLS_SRTP)
Gilles Peskine449bd832023-01-11 14:50:10 +01001357 memset(&ssl->dtls_srtp_info, 0, sizeof(ssl->dtls_srtp_info));
Johan Pascalb62bb512015-12-03 21:56:45 +01001358#endif
1359
Gilles Peskine449bd832023-01-11 14:50:10 +01001360 if ((ret = ssl_handshake_init(ssl)) != 0) {
k-stachowiaka47911c2018-07-04 17:41:58 +02001361 goto error;
Gilles Peskine449bd832023-01-11 14:50:10 +01001362 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001363
Gilles Peskine449bd832023-01-11 14:50:10 +01001364 return 0;
k-stachowiaka47911c2018-07-04 17:41:58 +02001365
1366error:
Gilles Peskine449bd832023-01-11 14:50:10 +01001367 mbedtls_free(ssl->in_buf);
1368 mbedtls_free(ssl->out_buf);
k-stachowiaka47911c2018-07-04 17:41:58 +02001369
1370 ssl->conf = NULL;
1371
Darryl Greenb33cc762019-11-28 14:29:44 +00001372#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1373 ssl->in_buf_len = 0;
1374 ssl->out_buf_len = 0;
1375#endif
k-stachowiaka47911c2018-07-04 17:41:58 +02001376 ssl->in_buf = NULL;
1377 ssl->out_buf = NULL;
1378
1379 ssl->in_hdr = NULL;
1380 ssl->in_ctr = NULL;
1381 ssl->in_len = NULL;
1382 ssl->in_iv = NULL;
1383 ssl->in_msg = NULL;
1384
1385 ssl->out_hdr = NULL;
1386 ssl->out_ctr = NULL;
1387 ssl->out_len = NULL;
1388 ssl->out_iv = NULL;
1389 ssl->out_msg = NULL;
1390
Gilles Peskine449bd832023-01-11 14:50:10 +01001391 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001392}
1393
1394/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00001395 * Reset an initialized and used SSL context for re-use while retaining
1396 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001397 *
1398 * If partial is non-zero, keep data in the input buffer and client ID.
1399 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00001400 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001401void mbedtls_ssl_session_reset_msg_layer(mbedtls_ssl_context *ssl,
1402 int partial)
Paul Bakker7eb013f2011-10-06 12:37:39 +00001403{
Darryl Greenb33cc762019-11-28 14:29:44 +00001404#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1405 size_t in_buf_len = ssl->in_buf_len;
1406 size_t out_buf_len = ssl->out_buf_len;
1407#else
1408 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1409 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
1410#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001411
Hanno Beckerb0302c42021-08-03 09:39:42 +01001412#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || !defined(MBEDTLS_SSL_SRV_C)
1413 partial = 0;
Hanno Becker7e772132018-08-10 12:38:21 +01001414#endif
1415
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001416 /* Cancel any possibly running timer */
Gilles Peskine449bd832023-01-11 14:50:10 +01001417 mbedtls_ssl_set_timer(ssl, 0);
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001418
Gilles Peskine449bd832023-01-11 14:50:10 +01001419 mbedtls_ssl_reset_in_out_pointers(ssl);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001420
1421 /* Reset incoming message parsing */
1422 ssl->in_offt = NULL;
1423 ssl->nb_zero = 0;
1424 ssl->in_msgtype = 0;
1425 ssl->in_msglen = 0;
1426 ssl->in_hslen = 0;
1427 ssl->keep_current_message = 0;
1428 ssl->transform_in = NULL;
1429
1430#if defined(MBEDTLS_SSL_PROTO_DTLS)
1431 ssl->next_record_offset = 0;
1432 ssl->in_epoch = 0;
1433#endif
1434
1435 /* Keep current datagram if partial == 1 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001436 if (partial == 0) {
Hanno Beckerb0302c42021-08-03 09:39:42 +01001437 ssl->in_left = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001438 memset(ssl->in_buf, 0, in_buf_len);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001439 }
1440
Ronald Cronad8c17b2022-06-10 17:18:09 +02001441 ssl->send_alert = 0;
1442
Hanno Beckerb0302c42021-08-03 09:39:42 +01001443 /* Reset outgoing message writing */
1444 ssl->out_msgtype = 0;
1445 ssl->out_msglen = 0;
1446 ssl->out_left = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001447 memset(ssl->out_buf, 0, out_buf_len);
1448 memset(ssl->cur_out_ctr, 0, sizeof(ssl->cur_out_ctr));
Hanno Beckerb0302c42021-08-03 09:39:42 +01001449 ssl->transform_out = NULL;
1450
1451#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine449bd832023-01-11 14:50:10 +01001452 mbedtls_ssl_dtls_replay_reset(ssl);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001453#endif
1454
XiaokangQian2b01dc32022-01-21 02:53:13 +00001455#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01001456 if (ssl->transform) {
1457 mbedtls_ssl_transform_free(ssl->transform);
1458 mbedtls_free(ssl->transform);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001459 ssl->transform = NULL;
1460 }
XiaokangQian2b01dc32022-01-21 02:53:13 +00001461#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1462
XiaokangQian2b01dc32022-01-21 02:53:13 +00001463#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001464 mbedtls_ssl_transform_free(ssl->transform_application);
1465 mbedtls_free(ssl->transform_application);
XiaokangQian2b01dc32022-01-21 02:53:13 +00001466 ssl->transform_application = NULL;
1467
Gilles Peskine449bd832023-01-11 14:50:10 +01001468 if (ssl->handshake != NULL) {
Jerry Yu3d9b5902022-11-04 14:07:25 +08001469#if defined(MBEDTLS_SSL_EARLY_DATA)
Gilles Peskine449bd832023-01-11 14:50:10 +01001470 mbedtls_ssl_transform_free(ssl->handshake->transform_earlydata);
1471 mbedtls_free(ssl->handshake->transform_earlydata);
XiaokangQian2b01dc32022-01-21 02:53:13 +00001472 ssl->handshake->transform_earlydata = NULL;
Jerry Yu3d9b5902022-11-04 14:07:25 +08001473#endif
XiaokangQian2b01dc32022-01-21 02:53:13 +00001474
Gilles Peskine449bd832023-01-11 14:50:10 +01001475 mbedtls_ssl_transform_free(ssl->handshake->transform_handshake);
1476 mbedtls_free(ssl->handshake->transform_handshake);
XiaokangQian2b01dc32022-01-21 02:53:13 +00001477 ssl->handshake->transform_handshake = NULL;
1478 }
1479
XiaokangQian2b01dc32022-01-21 02:53:13 +00001480#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerb0302c42021-08-03 09:39:42 +01001481}
1482
Gilles Peskine449bd832023-01-11 14:50:10 +01001483int mbedtls_ssl_session_reset_int(mbedtls_ssl_context *ssl, int partial)
Hanno Beckerb0302c42021-08-03 09:39:42 +01001484{
1485 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1486
1487 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
1488
Gilles Peskine449bd832023-01-11 14:50:10 +01001489 mbedtls_ssl_session_reset_msg_layer(ssl, partial);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001490
1491 /* Reset renegotiation state */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001492#if defined(MBEDTLS_SSL_RENEGOTIATION)
1493 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001494 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00001495
1496 ssl->verify_data_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001497 memset(ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN);
1498 memset(ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN);
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001499#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001500 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00001501
Hanno Beckerb0302c42021-08-03 09:39:42 +01001502 ssl->session_in = NULL;
Hanno Becker78640902018-08-13 16:35:15 +01001503 ssl->session_out = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001504 if (ssl->session) {
1505 mbedtls_ssl_session_free(ssl->session);
1506 mbedtls_free(ssl->session);
Paul Bakkerc0463502013-02-14 11:19:38 +01001507 ssl->session = NULL;
1508 }
1509
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001510#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02001511 ssl->alpn_chosen = NULL;
1512#endif
1513
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001514#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
David Horstmann3b2276a2022-10-06 14:49:08 +01001515 int free_cli_id = 1;
Hanno Becker4ccbf062018-08-10 11:20:38 +01001516#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Gilles Peskine449bd832023-01-11 14:50:10 +01001517 free_cli_id = (partial == 0);
Hanno Becker4ccbf062018-08-10 11:20:38 +01001518#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001519 if (free_cli_id) {
1520 mbedtls_free(ssl->cli_id);
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001521 ssl->cli_id = NULL;
1522 ssl->cli_id_len = 0;
1523 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02001524#endif
1525
Gilles Peskine449bd832023-01-11 14:50:10 +01001526 if ((ret = ssl_handshake_init(ssl)) != 0) {
1527 return ret;
1528 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00001529
Gilles Peskine449bd832023-01-11 14:50:10 +01001530 return 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00001531}
1532
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02001533/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001534 * Reset an initialized and used SSL context for re-use while retaining
1535 * all application-set variables, function pointers and data.
1536 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001537int mbedtls_ssl_session_reset(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001538{
Gilles Peskine449bd832023-01-11 14:50:10 +01001539 return mbedtls_ssl_session_reset_int(ssl, 0);
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001540}
1541
1542/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001543 * SSL set accessors
1544 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001545void mbedtls_ssl_conf_endpoint(mbedtls_ssl_config *conf, int endpoint)
Paul Bakker5121ce52009-01-03 21:22:43 +00001546{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001547 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00001548}
1549
Gilles Peskine449bd832023-01-11 14:50:10 +01001550void mbedtls_ssl_conf_transport(mbedtls_ssl_config *conf, int transport)
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001551{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001552 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001553}
1554
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001555#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine449bd832023-01-11 14:50:10 +01001556void mbedtls_ssl_conf_dtls_anti_replay(mbedtls_ssl_config *conf, char mode)
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001557{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001558 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001559}
1560#endif
1561
Gilles Peskine449bd832023-01-11 14:50:10 +01001562void mbedtls_ssl_conf_dtls_badmac_limit(mbedtls_ssl_config *conf, unsigned limit)
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001563{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001564 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001565}
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001566
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001567#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01001568
Gilles Peskine449bd832023-01-11 14:50:10 +01001569void mbedtls_ssl_set_datagram_packing(mbedtls_ssl_context *ssl,
1570 unsigned allow_packing)
Hanno Becker04da1892018-08-14 13:22:10 +01001571{
1572 ssl->disable_datagram_packing = !allow_packing;
1573}
1574
Gilles Peskine449bd832023-01-11 14:50:10 +01001575void mbedtls_ssl_conf_handshake_timeout(mbedtls_ssl_config *conf,
1576 uint32_t min, uint32_t max)
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001577{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001578 conf->hs_timeout_min = min;
1579 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001580}
1581#endif
1582
Gilles Peskine449bd832023-01-11 14:50:10 +01001583void mbedtls_ssl_conf_authmode(mbedtls_ssl_config *conf, int authmode)
Paul Bakker5121ce52009-01-03 21:22:43 +00001584{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001585 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00001586}
1587
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001588#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001589void mbedtls_ssl_conf_verify(mbedtls_ssl_config *conf,
1590 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
1591 void *p_vrfy)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001592{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001593 conf->f_vrfy = f_vrfy;
1594 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001595}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001596#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001597
Gilles Peskine449bd832023-01-11 14:50:10 +01001598void mbedtls_ssl_conf_rng(mbedtls_ssl_config *conf,
1599 int (*f_rng)(void *, unsigned char *, size_t),
1600 void *p_rng)
Paul Bakker5121ce52009-01-03 21:22:43 +00001601{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001602 conf->f_rng = f_rng;
1603 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00001604}
1605
Gilles Peskine449bd832023-01-11 14:50:10 +01001606void mbedtls_ssl_conf_dbg(mbedtls_ssl_config *conf,
1607 void (*f_dbg)(void *, int, const char *, int, const char *),
1608 void *p_dbg)
Paul Bakker5121ce52009-01-03 21:22:43 +00001609{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001610 conf->f_dbg = f_dbg;
1611 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00001612}
1613
Gilles Peskine449bd832023-01-11 14:50:10 +01001614void mbedtls_ssl_set_bio(mbedtls_ssl_context *ssl,
1615 void *p_bio,
1616 mbedtls_ssl_send_t *f_send,
1617 mbedtls_ssl_recv_t *f_recv,
1618 mbedtls_ssl_recv_timeout_t *f_recv_timeout)
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001619{
1620 ssl->p_bio = p_bio;
1621 ssl->f_send = f_send;
1622 ssl->f_recv = f_recv;
1623 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001624}
1625
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02001626#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001627void mbedtls_ssl_set_mtu(mbedtls_ssl_context *ssl, uint16_t mtu)
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02001628{
1629 ssl->mtu = mtu;
1630}
1631#endif
1632
Gilles Peskine449bd832023-01-11 14:50:10 +01001633void mbedtls_ssl_conf_read_timeout(mbedtls_ssl_config *conf, uint32_t timeout)
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001634{
1635 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001636}
1637
Gilles Peskine449bd832023-01-11 14:50:10 +01001638void mbedtls_ssl_set_timer_cb(mbedtls_ssl_context *ssl,
1639 void *p_timer,
1640 mbedtls_ssl_set_timer_t *f_set_timer,
1641 mbedtls_ssl_get_timer_t *f_get_timer)
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001642{
1643 ssl->p_timer = p_timer;
1644 ssl->f_set_timer = f_set_timer;
1645 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001646
1647 /* Make sure we start with no timer running */
Gilles Peskine449bd832023-01-11 14:50:10 +01001648 mbedtls_ssl_set_timer(ssl, 0);
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001649}
1650
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001651#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001652void mbedtls_ssl_conf_session_cache(mbedtls_ssl_config *conf,
1653 void *p_cache,
1654 mbedtls_ssl_cache_get_t *f_get_cache,
1655 mbedtls_ssl_cache_set_t *f_set_cache)
Paul Bakker5121ce52009-01-03 21:22:43 +00001656{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01001657 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001658 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001659 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00001660}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001661#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001662
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001663#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001664int mbedtls_ssl_set_session(mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session)
Paul Bakker5121ce52009-01-03 21:22:43 +00001665{
Janos Follath865b3eb2019-12-16 11:46:15 +00001666 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001667
Gilles Peskine449bd832023-01-11 14:50:10 +01001668 if (ssl == NULL ||
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001669 session == NULL ||
1670 ssl->session_negotiate == NULL ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001671 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT) {
1672 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001673 }
1674
Gilles Peskine449bd832023-01-11 14:50:10 +01001675 if (ssl->handshake->resume == 1) {
1676 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
1677 }
Hanno Beckere810bbc2021-05-14 16:01:05 +01001678
Jerry Yu21092062022-10-10 21:21:31 +08001679#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001680 if (session->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
Jerry Yu21092062022-10-10 21:21:31 +08001681 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01001682 mbedtls_ssl_ciphersuite_from_id(session->ciphersuite);
Jerry Yu21092062022-10-10 21:21:31 +08001683
Gilles Peskine449bd832023-01-11 14:50:10 +01001684 if (mbedtls_ssl_validate_ciphersuite(
Jerry Yu21092062022-10-10 21:21:31 +08001685 ssl, ciphersuite_info, MBEDTLS_SSL_VERSION_TLS1_3,
Gilles Peskine449bd832023-01-11 14:50:10 +01001686 MBEDTLS_SSL_VERSION_TLS1_3) != 0) {
1687 MBEDTLS_SSL_DEBUG_MSG(4, ("%d is not a valid TLS 1.3 ciphersuite.",
1688 session->ciphersuite));
1689 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jerry Yu21092062022-10-10 21:21:31 +08001690 }
Jerry Yu40afab62022-10-08 10:42:13 +08001691 }
Jerry Yu21092062022-10-10 21:21:31 +08001692#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu40afab62022-10-08 10:42:13 +08001693
Gilles Peskine449bd832023-01-11 14:50:10 +01001694 if ((ret = mbedtls_ssl_session_copy(ssl->session_negotiate,
1695 session)) != 0) {
1696 return ret;
1697 }
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001698
Paul Bakker0a597072012-09-25 21:55:46 +00001699 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001700
Gilles Peskine449bd832023-01-11 14:50:10 +01001701 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001702}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001703#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001704
Gilles Peskine449bd832023-01-11 14:50:10 +01001705void mbedtls_ssl_conf_ciphersuites(mbedtls_ssl_config *conf,
1706 const int *ciphersuites)
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001707{
Hanno Beckerd60b6c62021-04-29 12:04:11 +01001708 conf->ciphersuite_list = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00001709}
1710
Ronald Cron6f135e12021-12-08 16:57:54 +01001711#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001712void mbedtls_ssl_conf_tls13_key_exchange_modes(mbedtls_ssl_config *conf,
1713 const int kex_modes)
Hanno Becker71f1ed62021-07-24 06:01:47 +01001714{
Xiaofei Bai746f9482021-11-12 08:53:56 +00001715 conf->tls13_kex_modes = kex_modes & MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Hanno Becker71f1ed62021-07-24 06:01:47 +01001716}
Xiaokang Qian72de95d2022-10-25 02:54:33 +00001717
1718#if defined(MBEDTLS_SSL_EARLY_DATA)
Gilles Peskine449bd832023-01-11 14:50:10 +01001719void mbedtls_ssl_tls13_conf_early_data(mbedtls_ssl_config *conf,
1720 int early_data_enabled)
Xiaokang Qian72de95d2022-10-25 02:54:33 +00001721{
1722 conf->early_data_enabled = early_data_enabled;
1723}
Jerry Yucc4e0072022-11-22 17:22:22 +08001724
1725#if defined(MBEDTLS_SSL_SRV_C)
1726void mbedtls_ssl_tls13_conf_max_early_data_size(
Gilles Peskine449bd832023-01-11 14:50:10 +01001727 mbedtls_ssl_config *conf, uint32_t max_early_data_size)
Jerry Yucc4e0072022-11-22 17:22:22 +08001728{
Jerry Yu39da9852022-12-06 16:58:36 +08001729 conf->max_early_data_size = max_early_data_size;
Jerry Yucc4e0072022-11-22 17:22:22 +08001730}
1731#endif /* MBEDTLS_SSL_SRV_C */
1732
Xiaokang Qian72de95d2022-10-25 02:54:33 +00001733#endif /* MBEDTLS_SSL_EARLY_DATA */
Ronald Cron6f135e12021-12-08 16:57:54 +01001734#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01001735
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001736#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001737void mbedtls_ssl_conf_cert_profile(mbedtls_ssl_config *conf,
1738 const mbedtls_x509_crt_profile *profile)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02001739{
1740 conf->cert_profile = profile;
1741}
1742
Gilles Peskine449bd832023-01-11 14:50:10 +01001743static void ssl_key_cert_free(mbedtls_ssl_key_cert *key_cert)
Glenn Strauss36872db2022-01-22 05:06:31 -05001744{
1745 mbedtls_ssl_key_cert *cur = key_cert, *next;
1746
Gilles Peskine449bd832023-01-11 14:50:10 +01001747 while (cur != NULL) {
Glenn Strauss36872db2022-01-22 05:06:31 -05001748 next = cur->next;
Gilles Peskine449bd832023-01-11 14:50:10 +01001749 mbedtls_free(cur);
Glenn Strauss36872db2022-01-22 05:06:31 -05001750 cur = next;
1751 }
1752}
1753
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001754/* Append a new keycert entry to a (possibly empty) list */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001755MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001756static int ssl_append_key_cert(mbedtls_ssl_key_cert **head,
1757 mbedtls_x509_crt *cert,
1758 mbedtls_pk_context *key)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001759{
niisato8ee24222018-06-25 19:05:48 +09001760 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001761
Gilles Peskine449bd832023-01-11 14:50:10 +01001762 if (cert == NULL) {
Glenn Strauss36872db2022-01-22 05:06:31 -05001763 /* Free list if cert is null */
Gilles Peskine449bd832023-01-11 14:50:10 +01001764 ssl_key_cert_free(*head);
Glenn Strauss36872db2022-01-22 05:06:31 -05001765 *head = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001766 return 0;
Glenn Strauss36872db2022-01-22 05:06:31 -05001767 }
1768
Gilles Peskine449bd832023-01-11 14:50:10 +01001769 new_cert = mbedtls_calloc(1, sizeof(mbedtls_ssl_key_cert));
1770 if (new_cert == NULL) {
1771 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
1772 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001773
niisato8ee24222018-06-25 19:05:48 +09001774 new_cert->cert = cert;
1775 new_cert->key = key;
1776 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001777
Glenn Strauss36872db2022-01-22 05:06:31 -05001778 /* Update head if the list was null, else add to the end */
Gilles Peskine449bd832023-01-11 14:50:10 +01001779 if (*head == NULL) {
niisato8ee24222018-06-25 19:05:48 +09001780 *head = new_cert;
Gilles Peskine449bd832023-01-11 14:50:10 +01001781 } else {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001782 mbedtls_ssl_key_cert *cur = *head;
Gilles Peskine449bd832023-01-11 14:50:10 +01001783 while (cur->next != NULL) {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001784 cur = cur->next;
Gilles Peskine449bd832023-01-11 14:50:10 +01001785 }
niisato8ee24222018-06-25 19:05:48 +09001786 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001787 }
1788
Gilles Peskine449bd832023-01-11 14:50:10 +01001789 return 0;
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001790}
1791
Gilles Peskine449bd832023-01-11 14:50:10 +01001792int mbedtls_ssl_conf_own_cert(mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001793 mbedtls_x509_crt *own_cert,
Gilles Peskine449bd832023-01-11 14:50:10 +01001794 mbedtls_pk_context *pk_key)
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001795{
Gilles Peskine449bd832023-01-11 14:50:10 +01001796 return ssl_append_key_cert(&conf->key_cert, own_cert, pk_key);
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001797}
1798
Gilles Peskine449bd832023-01-11 14:50:10 +01001799void mbedtls_ssl_conf_ca_chain(mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001800 mbedtls_x509_crt *ca_chain,
Gilles Peskine449bd832023-01-11 14:50:10 +01001801 mbedtls_x509_crl *ca_crl)
Paul Bakker5121ce52009-01-03 21:22:43 +00001802{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001803 conf->ca_chain = ca_chain;
1804 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00001805
1806#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1807 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1808 * cannot be used together. */
1809 conf->f_ca_cb = NULL;
1810 conf->p_ca_cb = NULL;
1811#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00001812}
Hanno Becker5adaad92019-03-27 16:54:37 +00001813
1814#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Gilles Peskine449bd832023-01-11 14:50:10 +01001815void mbedtls_ssl_conf_ca_cb(mbedtls_ssl_config *conf,
1816 mbedtls_x509_crt_ca_cb_t f_ca_cb,
1817 void *p_ca_cb)
Hanno Becker5adaad92019-03-27 16:54:37 +00001818{
1819 conf->f_ca_cb = f_ca_cb;
1820 conf->p_ca_cb = p_ca_cb;
1821
1822 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1823 * cannot be used together. */
1824 conf->ca_chain = NULL;
1825 conf->ca_crl = NULL;
1826}
1827#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001828#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00001829
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001830#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01001831const unsigned char *mbedtls_ssl_get_hs_sni(mbedtls_ssl_context *ssl,
1832 size_t *name_len)
Glenn Strauss69894072022-01-24 12:58:00 -05001833{
1834 *name_len = ssl->handshake->sni_name_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001835 return ssl->handshake->sni_name;
Glenn Strauss69894072022-01-24 12:58:00 -05001836}
1837
Gilles Peskine449bd832023-01-11 14:50:10 +01001838int mbedtls_ssl_set_hs_own_cert(mbedtls_ssl_context *ssl,
1839 mbedtls_x509_crt *own_cert,
1840 mbedtls_pk_context *pk_key)
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001841{
Gilles Peskine449bd832023-01-11 14:50:10 +01001842 return ssl_append_key_cert(&ssl->handshake->sni_key_cert,
1843 own_cert, pk_key);
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001844}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02001845
Gilles Peskine449bd832023-01-11 14:50:10 +01001846void mbedtls_ssl_set_hs_ca_chain(mbedtls_ssl_context *ssl,
1847 mbedtls_x509_crt *ca_chain,
1848 mbedtls_x509_crl *ca_crl)
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02001849{
1850 ssl->handshake->sni_ca_chain = ca_chain;
1851 ssl->handshake->sni_ca_crl = ca_crl;
1852}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001853
Glenn Strauss999ef702022-03-11 01:37:23 -05001854#if defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001855void mbedtls_ssl_set_hs_dn_hints(mbedtls_ssl_context *ssl,
1856 const mbedtls_x509_crt *crt)
Glenn Strauss999ef702022-03-11 01:37:23 -05001857{
1858 ssl->handshake->dn_hints = crt;
1859}
1860#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
1861
Gilles Peskine449bd832023-01-11 14:50:10 +01001862void mbedtls_ssl_set_hs_authmode(mbedtls_ssl_context *ssl,
1863 int authmode)
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001864{
1865 ssl->handshake->sni_authmode = authmode;
1866}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001867#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
1868
Hanno Becker8927c832019-04-03 12:52:50 +01001869#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001870void mbedtls_ssl_set_verify(mbedtls_ssl_context *ssl,
1871 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
1872 void *p_vrfy)
Hanno Becker8927c832019-04-03 12:52:50 +01001873{
1874 ssl->f_vrfy = f_vrfy;
1875 ssl->p_vrfy = p_vrfy;
1876}
1877#endif
1878
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02001879#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001880
Valerio Setti016f6822022-12-09 14:17:50 +01001881#if defined(MBEDTLS_USE_PSA_CRYPTO)
1882static psa_status_t mbedtls_ssl_set_hs_ecjpake_password_common(
Gilles Peskine449bd832023-01-11 14:50:10 +01001883 mbedtls_ssl_context *ssl,
1884 mbedtls_svc_key_id_t pwd)
Valerio Setti016f6822022-12-09 14:17:50 +01001885{
1886 psa_status_t status;
1887 psa_pake_role_t psa_role;
1888 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
1889
Gilles Peskine449bd832023-01-11 14:50:10 +01001890 psa_pake_cs_set_algorithm(&cipher_suite, PSA_ALG_JPAKE);
1891 psa_pake_cs_set_primitive(&cipher_suite,
1892 PSA_PAKE_PRIMITIVE(PSA_PAKE_PRIMITIVE_TYPE_ECC,
1893 PSA_ECC_FAMILY_SECP_R1,
1894 256));
1895 psa_pake_cs_set_hash(&cipher_suite, PSA_ALG_SHA_256);
Valerio Setti016f6822022-12-09 14:17:50 +01001896
Gilles Peskine449bd832023-01-11 14:50:10 +01001897 status = psa_pake_setup(&ssl->handshake->psa_pake_ctx, &cipher_suite);
1898 if (status != PSA_SUCCESS) {
Valerio Setti016f6822022-12-09 14:17:50 +01001899 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001900 }
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001901
Gilles Peskine449bd832023-01-11 14:50:10 +01001902 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Neil Armstrongca7d5062022-05-31 14:43:23 +02001903 psa_role = PSA_PAKE_ROLE_SERVER;
Gilles Peskine449bd832023-01-11 14:50:10 +01001904 } else {
Neil Armstrongca7d5062022-05-31 14:43:23 +02001905 psa_role = PSA_PAKE_ROLE_CLIENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001906 }
Neil Armstrongca7d5062022-05-31 14:43:23 +02001907
Gilles Peskine449bd832023-01-11 14:50:10 +01001908 status = psa_pake_set_role(&ssl->handshake->psa_pake_ctx, psa_role);
1909 if (status != PSA_SUCCESS) {
Valerio Setti016f6822022-12-09 14:17:50 +01001910 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001911 }
Valerio Setti016f6822022-12-09 14:17:50 +01001912
Gilles Peskine449bd832023-01-11 14:50:10 +01001913 status = psa_pake_set_password_key(&ssl->handshake->psa_pake_ctx, pwd);
1914 if (status != PSA_SUCCESS) {
Valerio Setti016f6822022-12-09 14:17:50 +01001915 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001916 }
Valerio Setti016f6822022-12-09 14:17:50 +01001917
1918 ssl->handshake->psa_pake_ctx_is_ok = 1;
1919
Gilles Peskine449bd832023-01-11 14:50:10 +01001920 return PSA_SUCCESS;
Valerio Setti016f6822022-12-09 14:17:50 +01001921}
1922
Gilles Peskine449bd832023-01-11 14:50:10 +01001923int mbedtls_ssl_set_hs_ecjpake_password(mbedtls_ssl_context *ssl,
1924 const unsigned char *pw,
1925 size_t pw_len)
Valerio Setti016f6822022-12-09 14:17:50 +01001926{
1927 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1928 psa_status_t status;
1929
Gilles Peskine449bd832023-01-11 14:50:10 +01001930 if (ssl->handshake == NULL || ssl->conf == NULL) {
1931 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Neil Armstrongca7d5062022-05-31 14:43:23 +02001932 }
1933
Gilles Peskine449bd832023-01-11 14:50:10 +01001934 /* Empty password is not valid */
1935 if ((pw == NULL) || (pw_len == 0)) {
1936 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
1937 }
1938
1939 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
1940 psa_set_key_algorithm(&attributes, PSA_ALG_JPAKE);
1941 psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD);
1942
1943 status = psa_import_key(&attributes, pw, pw_len,
1944 &ssl->handshake->psa_pake_password);
1945 if (status != PSA_SUCCESS) {
1946 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
1947 }
1948
1949 status = mbedtls_ssl_set_hs_ecjpake_password_common(ssl,
1950 ssl->handshake->psa_pake_password);
1951 if (status != PSA_SUCCESS) {
1952 psa_destroy_key(ssl->handshake->psa_pake_password);
1953 psa_pake_abort(&ssl->handshake->psa_pake_ctx);
1954 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
1955 }
1956
1957 return 0;
Valerio Setti02c25b52022-11-15 14:08:42 +01001958}
Valerio Settia9a97dc2022-11-28 18:26:16 +01001959
Gilles Peskine449bd832023-01-11 14:50:10 +01001960int mbedtls_ssl_set_hs_ecjpake_password_opaque(mbedtls_ssl_context *ssl,
1961 mbedtls_svc_key_id_t pwd)
Valerio Settia9a97dc2022-11-28 18:26:16 +01001962{
Valerio Settia9a97dc2022-11-28 18:26:16 +01001963 psa_status_t status;
1964
Gilles Peskine449bd832023-01-11 14:50:10 +01001965 if (ssl->handshake == NULL || ssl->conf == NULL) {
1966 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Neil Armstrongca7d5062022-05-31 14:43:23 +02001967 }
1968
Gilles Peskine449bd832023-01-11 14:50:10 +01001969 if (mbedtls_svc_key_id_is_null(pwd)) {
1970 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
1971 }
1972
1973 status = mbedtls_ssl_set_hs_ecjpake_password_common(ssl, pwd);
1974 if (status != PSA_SUCCESS) {
1975 psa_pake_abort(&ssl->handshake->psa_pake_ctx);
1976 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
1977 }
1978
1979 return 0;
Valerio Setti02c25b52022-11-15 14:08:42 +01001980}
1981#else /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01001982int mbedtls_ssl_set_hs_ecjpake_password(mbedtls_ssl_context *ssl,
1983 const unsigned char *pw,
1984 size_t pw_len)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001985{
1986 mbedtls_ecjpake_role role;
1987
Gilles Peskine449bd832023-01-11 14:50:10 +01001988 if (ssl->handshake == NULL || ssl->conf == NULL) {
1989 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
1990 }
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001991
Valerio Settic689ed82022-12-07 14:40:38 +01001992 /* Empty password is not valid */
Gilles Peskine449bd832023-01-11 14:50:10 +01001993 if ((pw == NULL) || (pw_len == 0)) {
1994 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
1995 }
Valerio Settic689ed82022-12-07 14:40:38 +01001996
Gilles Peskine449bd832023-01-11 14:50:10 +01001997 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001998 role = MBEDTLS_ECJPAKE_SERVER;
Gilles Peskine449bd832023-01-11 14:50:10 +01001999 } else {
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002000 role = MBEDTLS_ECJPAKE_CLIENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01002001 }
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002002
Gilles Peskine449bd832023-01-11 14:50:10 +01002003 return mbedtls_ecjpake_setup(&ssl->handshake->ecjpake_ctx,
2004 role,
2005 MBEDTLS_MD_SHA256,
2006 MBEDTLS_ECP_DP_SECP256R1,
2007 pw, pw_len);
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002008}
Valerio Setti02c25b52022-11-15 14:08:42 +01002009#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02002010#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002011
Ronald Cron73fe8df2022-10-05 14:31:43 +02002012#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002013int mbedtls_ssl_conf_has_static_psk(mbedtls_ssl_config const *conf)
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002014{
Gilles Peskine449bd832023-01-11 14:50:10 +01002015 if (conf->psk_identity == NULL ||
2016 conf->psk_identity_len == 0) {
2017 return 0;
Ronald Crond29e13e2022-10-19 10:33:48 +02002018 }
2019
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002020#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002021 if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) {
2022 return 1;
2023 }
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002024#endif /* MBEDTLS_USE_PSA_CRYPTO */
Ronald Crond29e13e2022-10-19 10:33:48 +02002025
Gilles Peskine449bd832023-01-11 14:50:10 +01002026 if (conf->psk != NULL && conf->psk_len != 0) {
2027 return 1;
2028 }
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002029
Gilles Peskine449bd832023-01-11 14:50:10 +01002030 return 0;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002031}
2032
Gilles Peskine449bd832023-01-11 14:50:10 +01002033static void ssl_conf_remove_psk(mbedtls_ssl_config *conf)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002034{
2035 /* Remove reference to existing PSK, if any. */
2036#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002037 if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) {
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002038 /* The maintenance of the PSK key slot is the
2039 * user's responsibility. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002040 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002041 }
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002042#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01002043 if (conf->psk != NULL) {
2044 mbedtls_platform_zeroize(conf->psk, conf->psk_len);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002045
Gilles Peskine449bd832023-01-11 14:50:10 +01002046 mbedtls_free(conf->psk);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002047 conf->psk = NULL;
2048 conf->psk_len = 0;
2049 }
2050
2051 /* Remove reference to PSK identity, if any. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002052 if (conf->psk_identity != NULL) {
2053 mbedtls_free(conf->psk_identity);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002054 conf->psk_identity = NULL;
2055 conf->psk_identity_len = 0;
2056 }
2057}
2058
Hanno Becker7390c712018-11-15 13:33:04 +00002059/* This function assumes that PSK identity in the SSL config is unset.
2060 * It checks that the provided identity is well-formed and attempts
2061 * to make a copy of it in the SSL config.
2062 * On failure, the PSK identity in the config remains unset. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002063MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002064static int ssl_conf_set_psk_identity(mbedtls_ssl_config *conf,
2065 unsigned char const *psk_identity,
2066 size_t psk_identity_len)
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002067{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02002068 /* Identity len will be encoded on two bytes */
Gilles Peskine449bd832023-01-11 14:50:10 +01002069 if (psk_identity == NULL ||
Ronald Cron2a87e9b2022-10-19 10:55:26 +02002070 psk_identity_len == 0 ||
Gilles Peskine449bd832023-01-11 14:50:10 +01002071 (psk_identity_len >> 16) != 0 ||
2072 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN) {
2073 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02002074 }
2075
Gilles Peskine449bd832023-01-11 14:50:10 +01002076 conf->psk_identity = mbedtls_calloc(1, psk_identity_len);
2077 if (conf->psk_identity == NULL) {
2078 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2079 }
Paul Bakker6db455e2013-09-18 17:29:31 +02002080
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01002081 conf->psk_identity_len = psk_identity_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002082 memcpy(conf->psk_identity, psk_identity, conf->psk_identity_len);
Paul Bakker5ad403f2013-09-18 21:21:30 +02002083
Gilles Peskine449bd832023-01-11 14:50:10 +01002084 return 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02002085}
2086
Gilles Peskine449bd832023-01-11 14:50:10 +01002087int mbedtls_ssl_conf_psk(mbedtls_ssl_config *conf,
2088 const unsigned char *psk, size_t psk_len,
2089 const unsigned char *psk_identity, size_t psk_identity_len)
Hanno Becker7390c712018-11-15 13:33:04 +00002090{
Janos Follath865b3eb2019-12-16 11:46:15 +00002091 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002092
2093 /* We currently only support one PSK, raw or opaque. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002094 if (mbedtls_ssl_conf_has_static_psk(conf)) {
2095 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
2096 }
Hanno Becker7390c712018-11-15 13:33:04 +00002097
2098 /* Check and set raw PSK */
Gilles Peskine449bd832023-01-11 14:50:10 +01002099 if (psk == NULL) {
2100 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2101 }
2102 if (psk_len == 0) {
2103 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2104 }
2105 if (psk_len > MBEDTLS_PSK_MAX_LEN) {
2106 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2107 }
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01002108
Gilles Peskine449bd832023-01-11 14:50:10 +01002109 if ((conf->psk = mbedtls_calloc(1, psk_len)) == NULL) {
2110 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2111 }
Hanno Becker7390c712018-11-15 13:33:04 +00002112 conf->psk_len = psk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002113 memcpy(conf->psk, psk, conf->psk_len);
Hanno Becker7390c712018-11-15 13:33:04 +00002114
2115 /* Check and set PSK Identity */
Gilles Peskine449bd832023-01-11 14:50:10 +01002116 ret = ssl_conf_set_psk_identity(conf, psk_identity, psk_identity_len);
2117 if (ret != 0) {
2118 ssl_conf_remove_psk(conf);
2119 }
Hanno Becker7390c712018-11-15 13:33:04 +00002120
Gilles Peskine449bd832023-01-11 14:50:10 +01002121 return ret;
Hanno Becker7390c712018-11-15 13:33:04 +00002122}
2123
Gilles Peskine449bd832023-01-11 14:50:10 +01002124static void ssl_remove_psk(mbedtls_ssl_context *ssl)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002125{
2126#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002127 if (!mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque)) {
Neil Armstrong501c9322022-05-03 09:35:09 +02002128 /* The maintenance of the external PSK key slot is the
2129 * user's responsibility. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002130 if (ssl->handshake->psk_opaque_is_internal) {
2131 psa_destroy_key(ssl->handshake->psk_opaque);
Neil Armstrong501c9322022-05-03 09:35:09 +02002132 ssl->handshake->psk_opaque_is_internal = 0;
2133 }
Ronald Croncf56a0a2020-08-04 09:51:30 +02002134 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002135 }
Neil Armstronge952a302022-05-03 10:22:14 +02002136#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002137 if (ssl->handshake->psk != NULL) {
2138 mbedtls_platform_zeroize(ssl->handshake->psk,
2139 ssl->handshake->psk_len);
2140 mbedtls_free(ssl->handshake->psk);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002141 ssl->handshake->psk_len = 0;
2142 }
Neil Armstronge952a302022-05-03 10:22:14 +02002143#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002144}
2145
Gilles Peskine449bd832023-01-11 14:50:10 +01002146int mbedtls_ssl_set_hs_psk(mbedtls_ssl_context *ssl,
2147 const unsigned char *psk, size_t psk_len)
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002148{
Neil Armstrong501c9322022-05-03 09:35:09 +02002149#if defined(MBEDTLS_USE_PSA_CRYPTO)
2150 psa_key_attributes_t key_attributes = psa_key_attributes_init();
Jerry Yu5d01c052022-08-17 10:18:10 +08002151 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jerry Yu24b8c812022-08-20 19:06:56 +08002152 psa_algorithm_t alg = PSA_ALG_NONE;
Jerry Yu5d01c052022-08-17 10:18:10 +08002153 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrong501c9322022-05-03 09:35:09 +02002154#endif /* MBEDTLS_USE_PSA_CRYPTO */
2155
Gilles Peskine449bd832023-01-11 14:50:10 +01002156 if (psk == NULL || ssl->handshake == NULL) {
2157 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2158 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002159
Gilles Peskine449bd832023-01-11 14:50:10 +01002160 if (psk_len > MBEDTLS_PSK_MAX_LEN) {
2161 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2162 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002163
Gilles Peskine449bd832023-01-11 14:50:10 +01002164 ssl_remove_psk(ssl);
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002165
Neil Armstrong501c9322022-05-03 09:35:09 +02002166#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yuccc68a42022-07-26 16:39:20 +08002167#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01002168 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2) {
2169 if (ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384) {
2170 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
2171 } else {
2172 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
2173 }
2174 psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE);
Jerry Yuccc68a42022-07-26 16:39:20 +08002175 }
2176#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Neil Armstrong501c9322022-05-03 09:35:09 +02002177
Jerry Yu568ec252022-07-22 21:27:34 +08002178#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01002179 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
2180 alg = PSA_ALG_HKDF_EXTRACT(PSA_ALG_ANY_HASH);
2181 psa_set_key_usage_flags(&key_attributes,
2182 PSA_KEY_USAGE_DERIVE | PSA_KEY_USAGE_EXPORT);
Jerry Yuccc68a42022-07-26 16:39:20 +08002183 }
2184#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
2185
Gilles Peskine449bd832023-01-11 14:50:10 +01002186 psa_set_key_algorithm(&key_attributes, alg);
2187 psa_set_key_type(&key_attributes, PSA_KEY_TYPE_DERIVE);
Neil Armstrong501c9322022-05-03 09:35:09 +02002188
Gilles Peskine449bd832023-01-11 14:50:10 +01002189 status = psa_import_key(&key_attributes, psk, psk_len, &key);
2190 if (status != PSA_SUCCESS) {
2191 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2192 }
Neil Armstrong501c9322022-05-03 09:35:09 +02002193
2194 /* Allow calling psa_destroy_key() on psk remove */
2195 ssl->handshake->psk_opaque_is_internal = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01002196 return mbedtls_ssl_set_hs_psk_opaque(ssl, key);
Neil Armstrong501c9322022-05-03 09:35:09 +02002197#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002198 if ((ssl->handshake->psk = mbedtls_calloc(1, psk_len)) == NULL) {
2199 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2200 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002201
2202 ssl->handshake->psk_len = psk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002203 memcpy(ssl->handshake->psk, psk, ssl->handshake->psk_len);
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002204
Gilles Peskine449bd832023-01-11 14:50:10 +01002205 return 0;
Neil Armstrong501c9322022-05-03 09:35:09 +02002206#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002207}
2208
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002209#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002210int mbedtls_ssl_conf_psk_opaque(mbedtls_ssl_config *conf,
2211 mbedtls_svc_key_id_t psk,
2212 const unsigned char *psk_identity,
2213 size_t psk_identity_len)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002214{
Janos Follath865b3eb2019-12-16 11:46:15 +00002215 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002216
2217 /* We currently only support one PSK, raw or opaque. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002218 if (mbedtls_ssl_conf_has_static_psk(conf)) {
2219 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
2220 }
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002221
Hanno Becker7390c712018-11-15 13:33:04 +00002222 /* Check and set opaque PSK */
Gilles Peskine449bd832023-01-11 14:50:10 +01002223 if (mbedtls_svc_key_id_is_null(psk)) {
2224 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2225 }
Ronald Croncf56a0a2020-08-04 09:51:30 +02002226 conf->psk_opaque = psk;
Hanno Becker7390c712018-11-15 13:33:04 +00002227
2228 /* Check and set PSK Identity */
Gilles Peskine449bd832023-01-11 14:50:10 +01002229 ret = ssl_conf_set_psk_identity(conf, psk_identity,
2230 psk_identity_len);
2231 if (ret != 0) {
2232 ssl_conf_remove_psk(conf);
2233 }
Hanno Becker7390c712018-11-15 13:33:04 +00002234
Gilles Peskine449bd832023-01-11 14:50:10 +01002235 return ret;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002236}
2237
Gilles Peskine449bd832023-01-11 14:50:10 +01002238int mbedtls_ssl_set_hs_psk_opaque(mbedtls_ssl_context *ssl,
2239 mbedtls_svc_key_id_t psk)
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002240{
Gilles Peskine449bd832023-01-11 14:50:10 +01002241 if ((mbedtls_svc_key_id_is_null(psk)) ||
2242 (ssl->handshake == NULL)) {
2243 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2244 }
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002245
Gilles Peskine449bd832023-01-11 14:50:10 +01002246 ssl_remove_psk(ssl);
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002247 ssl->handshake->psk_opaque = psk;
Gilles Peskine449bd832023-01-11 14:50:10 +01002248 return 0;
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002249}
2250#endif /* MBEDTLS_USE_PSA_CRYPTO */
2251
Jerry Yu8897c072022-08-12 13:56:53 +08002252#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002253void mbedtls_ssl_conf_psk_cb(mbedtls_ssl_config *conf,
2254 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
2255 size_t),
2256 void *p_psk)
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002257{
2258 conf->f_psk = f_psk;
2259 conf->p_psk = p_psk;
2260}
Jerry Yu8897c072022-08-12 13:56:53 +08002261#endif /* MBEDTLS_SSL_SRV_C */
2262
Ronald Cron73fe8df2022-10-05 14:31:43 +02002263#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002264
2265#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine301711e2022-04-26 16:57:05 +02002266static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
Gilles Peskine449bd832023-01-11 14:50:10 +01002267 psa_algorithm_t alg)
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002268{
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002269#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01002270 if (alg == PSA_ALG_CBC_NO_PADDING) {
2271 return MBEDTLS_SSL_MODE_CBC;
2272 }
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002273#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
Gilles Peskine449bd832023-01-11 14:50:10 +01002274 if (PSA_ALG_IS_AEAD(alg)) {
2275 return MBEDTLS_SSL_MODE_AEAD;
2276 }
2277 return MBEDTLS_SSL_MODE_STREAM;
Gilles Peskine301711e2022-04-26 16:57:05 +02002278}
2279
2280#else /* MBEDTLS_USE_PSA_CRYPTO */
2281
2282static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
Gilles Peskine449bd832023-01-11 14:50:10 +01002283 mbedtls_cipher_mode_t mode)
Gilles Peskine301711e2022-04-26 16:57:05 +02002284{
2285#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01002286 if (mode == MBEDTLS_MODE_CBC) {
2287 return MBEDTLS_SSL_MODE_CBC;
2288 }
Gilles Peskine301711e2022-04-26 16:57:05 +02002289#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
2290
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002291#if defined(MBEDTLS_GCM_C) || \
2292 defined(MBEDTLS_CCM_C) || \
2293 defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002294 if (mode == MBEDTLS_MODE_GCM ||
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002295 mode == MBEDTLS_MODE_CCM ||
Gilles Peskine449bd832023-01-11 14:50:10 +01002296 mode == MBEDTLS_MODE_CHACHAPOLY) {
2297 return MBEDTLS_SSL_MODE_AEAD;
2298 }
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002299#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002300
Gilles Peskine449bd832023-01-11 14:50:10 +01002301 return MBEDTLS_SSL_MODE_STREAM;
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002302}
Gilles Peskine301711e2022-04-26 16:57:05 +02002303#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002304
Gilles Peskinee108d982022-04-26 16:50:40 +02002305static mbedtls_ssl_mode_t mbedtls_ssl_get_actual_mode(
2306 mbedtls_ssl_mode_t base_mode,
Gilles Peskine449bd832023-01-11 14:50:10 +01002307 int encrypt_then_mac)
Gilles Peskinee108d982022-04-26 16:50:40 +02002308{
2309#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01002310 if (encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED &&
2311 base_mode == MBEDTLS_SSL_MODE_CBC) {
2312 return MBEDTLS_SSL_MODE_CBC_ETM;
Gilles Peskinee108d982022-04-26 16:50:40 +02002313 }
2314#else
2315 (void) encrypt_then_mac;
2316#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002317 return base_mode;
Gilles Peskinee108d982022-04-26 16:50:40 +02002318}
2319
Neil Armstrongab555e02022-04-04 11:07:59 +02002320mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_transform(
Gilles Peskine449bd832023-01-11 14:50:10 +01002321 const mbedtls_ssl_transform *transform)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002322{
Gilles Peskinee108d982022-04-26 16:50:40 +02002323 mbedtls_ssl_mode_t base_mode = mbedtls_ssl_get_base_mode(
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002324#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002325 transform->psa_alg
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002326#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002327 mbedtls_cipher_get_cipher_mode(&transform->cipher_ctx_enc)
Gilles Peskinee108d982022-04-26 16:50:40 +02002328#endif
2329 );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002330
Gilles Peskinee108d982022-04-26 16:50:40 +02002331 int encrypt_then_mac = 0;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002332#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskinee108d982022-04-26 16:50:40 +02002333 encrypt_then_mac = transform->encrypt_then_mac;
2334#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002335 return mbedtls_ssl_get_actual_mode(base_mode, encrypt_then_mac);
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002336}
2337
Neil Armstrongab555e02022-04-04 11:07:59 +02002338mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002339#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01002340 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002341#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01002342 const mbedtls_ssl_ciphersuite_t *suite)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002343{
Gilles Peskinee108d982022-04-26 16:50:40 +02002344 mbedtls_ssl_mode_t base_mode = MBEDTLS_SSL_MODE_STREAM;
2345
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002346#if defined(MBEDTLS_USE_PSA_CRYPTO)
2347 psa_status_t status;
2348 psa_algorithm_t alg;
2349 psa_key_type_t type;
2350 size_t size;
Gilles Peskine449bd832023-01-11 14:50:10 +01002351 status = mbedtls_ssl_cipher_to_psa(suite->cipher, 0, &alg, &type, &size);
2352 if (status == PSA_SUCCESS) {
2353 base_mode = mbedtls_ssl_get_base_mode(alg);
2354 }
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002355#else
2356 const mbedtls_cipher_info_t *cipher =
Gilles Peskine449bd832023-01-11 14:50:10 +01002357 mbedtls_cipher_info_from_type(suite->cipher);
2358 if (cipher != NULL) {
Gilles Peskinee108d982022-04-26 16:50:40 +02002359 base_mode =
2360 mbedtls_ssl_get_base_mode(
Gilles Peskine449bd832023-01-11 14:50:10 +01002361 mbedtls_cipher_info_get_mode(cipher));
Gilles Peskinee108d982022-04-26 16:50:40 +02002362 }
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002363#endif /* MBEDTLS_USE_PSA_CRYPTO */
2364
Gilles Peskinee108d982022-04-26 16:50:40 +02002365#if !defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
2366 int encrypt_then_mac = 0;
2367#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002368 return mbedtls_ssl_get_actual_mode(base_mode, encrypt_then_mac);
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002369}
2370
Neil Armstrong8395d7a2022-05-18 11:44:56 +02002371#if defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu251a12e2022-07-13 15:15:48 +08002372
2373#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu438ddd82022-07-07 06:55:50 +00002374/* Serialization of TLS 1.3 sessions:
2375 *
2376 * struct {
Xiaokang Qian126bf8e2022-10-13 02:22:40 +00002377 * opaque hostname<0..2^16-1>;
Jerry Yu438ddd82022-07-07 06:55:50 +00002378 * uint64 ticket_received;
2379 * uint32 ticket_lifetime;
Jerry Yu34191072022-08-18 10:32:09 +08002380 * opaque ticket<1..2^16-1>;
Jerry Yu438ddd82022-07-07 06:55:50 +00002381 * } ClientOnlyData;
2382 *
2383 * struct {
2384 * uint8 endpoint;
2385 * uint8 ciphersuite[2];
2386 * uint32 ticket_age_add;
2387 * uint8 ticket_flags;
2388 * opaque resumption_key<0..255>;
2389 * select ( endpoint ) {
2390 * case client: ClientOnlyData;
2391 * case server: uint64 start_time;
2392 * };
2393 * } serialized_session_tls13;
2394 *
2395 */
2396#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yue36fdd62022-08-17 21:31:36 +08002397MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002398static int ssl_tls13_session_save(const mbedtls_ssl_session *session,
2399 unsigned char *buf,
2400 size_t buf_len,
2401 size_t *olen)
Jerry Yu438ddd82022-07-07 06:55:50 +00002402{
2403 unsigned char *p = buf;
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00002404#if defined(MBEDTLS_SSL_CLI_C) && \
2405 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01002406 size_t hostname_len = (session->hostname == NULL) ?
2407 0 : strlen(session->hostname) + 1;
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00002408#endif
Jerry Yubc7c1a42022-07-21 22:57:37 +08002409 size_t needed = 1 /* endpoint */
2410 + 2 /* ciphersuite */
2411 + 4 /* ticket_age_add */
Jerry Yu34191072022-08-18 10:32:09 +08002412 + 1 /* ticket_flags */
2413 + 1; /* resumption_key length */
Jerry Yue36fdd62022-08-17 21:31:36 +08002414 *olen = 0;
Jerry Yu34191072022-08-18 10:32:09 +08002415
Gilles Peskine449bd832023-01-11 14:50:10 +01002416 if (session->resumption_key_len > MBEDTLS_SSL_TLS1_3_TICKET_RESUMPTION_KEY_LEN) {
2417 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2418 }
Jerry Yu34191072022-08-18 10:32:09 +08002419 needed += session->resumption_key_len; /* resumption_key */
2420
Jerry Yu438ddd82022-07-07 06:55:50 +00002421#if defined(MBEDTLS_HAVE_TIME)
2422 needed += 8; /* start_time or ticket_received */
2423#endif
2424
2425#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002426 if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Xiaokang Qian126bf8e2022-10-13 02:22:40 +00002427#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaokang Qianbc663a02022-10-09 11:14:39 +00002428 needed += 2 /* hostname_len */
Gilles Peskine449bd832023-01-11 14:50:10 +01002429 + hostname_len; /* hostname */
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00002430#endif
2431
Jerry Yu438ddd82022-07-07 06:55:50 +00002432 needed += 4 /* ticket_lifetime */
Jerry Yue36fdd62022-08-17 21:31:36 +08002433 + 2; /* ticket_len */
Jerry Yu34191072022-08-18 10:32:09 +08002434
2435 /* Check size_t overflow */
Gilles Peskine449bd832023-01-11 14:50:10 +01002436 if (session->ticket_len > SIZE_MAX - needed) {
2437 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2438 }
Jerry Yu34191072022-08-18 10:32:09 +08002439
Jerry Yue28d9742022-08-18 15:44:03 +08002440 needed += session->ticket_len; /* ticket */
Jerry Yu438ddd82022-07-07 06:55:50 +00002441 }
2442#endif /* MBEDTLS_SSL_CLI_C */
2443
Jerry Yue36fdd62022-08-17 21:31:36 +08002444 *olen = needed;
Gilles Peskine449bd832023-01-11 14:50:10 +01002445 if (needed > buf_len) {
2446 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
2447 }
Jerry Yu438ddd82022-07-07 06:55:50 +00002448
2449 p[0] = session->endpoint;
Gilles Peskine449bd832023-01-11 14:50:10 +01002450 MBEDTLS_PUT_UINT16_BE(session->ciphersuite, p, 1);
2451 MBEDTLS_PUT_UINT32_BE(session->ticket_age_add, p, 3);
Jerry Yu438ddd82022-07-07 06:55:50 +00002452 p[7] = session->ticket_flags;
2453
2454 /* save resumption_key */
Jerry Yubc7c1a42022-07-21 22:57:37 +08002455 p[8] = session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002456 p += 9;
Gilles Peskine449bd832023-01-11 14:50:10 +01002457 memcpy(p, session->resumption_key, session->resumption_key_len);
Jerry Yubc7c1a42022-07-21 22:57:37 +08002458 p += session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002459
2460#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002461 if (session->endpoint == MBEDTLS_SSL_IS_SERVER) {
2462 MBEDTLS_PUT_UINT64_BE((uint64_t) session->start, p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002463 p += 8;
2464 }
2465#endif /* MBEDTLS_HAVE_TIME */
2466
2467#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002468 if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002469#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01002470 MBEDTLS_PUT_UINT16_BE(hostname_len, p, 0);
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002471 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002472 if (hostname_len > 0) {
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002473 /* save host name */
Gilles Peskine449bd832023-01-11 14:50:10 +01002474 memcpy(p, session->hostname, hostname_len);
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002475 p += hostname_len;
2476 }
2477#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
2478
Jerry Yu438ddd82022-07-07 06:55:50 +00002479#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01002480 MBEDTLS_PUT_UINT64_BE((uint64_t) session->ticket_received, p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002481 p += 8;
2482#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002483 MBEDTLS_PUT_UINT32_BE(session->ticket_lifetime, p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002484 p += 4;
2485
Gilles Peskine449bd832023-01-11 14:50:10 +01002486 MBEDTLS_PUT_UINT16_BE(session->ticket_len, p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002487 p += 2;
Jerry Yu34191072022-08-18 10:32:09 +08002488
Gilles Peskine449bd832023-01-11 14:50:10 +01002489 if (session->ticket != NULL && session->ticket_len > 0) {
2490 memcpy(p, session->ticket, session->ticket_len);
Jerry Yu438ddd82022-07-07 06:55:50 +00002491 p += session->ticket_len;
2492 }
2493 }
2494#endif /* MBEDTLS_SSL_CLI_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01002495 return 0;
Jerry Yu438ddd82022-07-07 06:55:50 +00002496}
2497
2498MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002499static int ssl_tls13_session_load(mbedtls_ssl_session *session,
2500 const unsigned char *buf,
2501 size_t len)
Jerry Yu438ddd82022-07-07 06:55:50 +00002502{
2503 const unsigned char *p = buf;
2504 const unsigned char *end = buf + len;
2505
Gilles Peskine449bd832023-01-11 14:50:10 +01002506 if (end - p < 9) {
2507 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2508 }
Jerry Yu438ddd82022-07-07 06:55:50 +00002509 session->endpoint = p[0];
Gilles Peskine449bd832023-01-11 14:50:10 +01002510 session->ciphersuite = MBEDTLS_GET_UINT16_BE(p, 1);
2511 session->ticket_age_add = MBEDTLS_GET_UINT32_BE(p, 3);
Jerry Yu438ddd82022-07-07 06:55:50 +00002512 session->ticket_flags = p[7];
2513
2514 /* load resumption_key */
Jerry Yubc7c1a42022-07-21 22:57:37 +08002515 session->resumption_key_len = p[8];
Jerry Yu438ddd82022-07-07 06:55:50 +00002516 p += 9;
2517
Gilles Peskine449bd832023-01-11 14:50:10 +01002518 if (end - p < session->resumption_key_len) {
2519 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2520 }
Jerry Yu438ddd82022-07-07 06:55:50 +00002521
Gilles Peskine449bd832023-01-11 14:50:10 +01002522 if (sizeof(session->resumption_key) < session->resumption_key_len) {
2523 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2524 }
2525 memcpy(session->resumption_key, p, session->resumption_key_len);
Jerry Yubc7c1a42022-07-21 22:57:37 +08002526 p += session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002527
2528#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002529 if (session->endpoint == MBEDTLS_SSL_IS_SERVER) {
2530 if (end - p < 8) {
2531 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2532 }
2533 session->start = MBEDTLS_GET_UINT64_BE(p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002534 p += 8;
2535 }
2536#endif /* MBEDTLS_HAVE_TIME */
2537
2538#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002539 if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002540#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01002541 defined(MBEDTLS_SSL_SESSION_TICKETS)
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002542 size_t hostname_len;
2543 /* load host name */
Gilles Peskine449bd832023-01-11 14:50:10 +01002544 if (end - p < 2) {
2545 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2546 }
2547 hostname_len = MBEDTLS_GET_UINT16_BE(p, 0);
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002548 p += 2;
2549
Gilles Peskine449bd832023-01-11 14:50:10 +01002550 if (end - p < (long int) hostname_len) {
2551 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2552 }
2553 if (hostname_len > 0) {
2554 session->hostname = mbedtls_calloc(1, hostname_len);
2555 if (session->hostname == NULL) {
2556 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2557 }
2558 memcpy(session->hostname, p, hostname_len);
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002559 p += hostname_len;
2560 }
2561#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION &&
2562 MBEDTLS_SSL_SESSION_TICKETS */
2563
Jerry Yu438ddd82022-07-07 06:55:50 +00002564#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01002565 if (end - p < 8) {
2566 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2567 }
2568 session->ticket_received = MBEDTLS_GET_UINT64_BE(p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002569 p += 8;
2570#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002571 if (end - p < 4) {
2572 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2573 }
2574 session->ticket_lifetime = MBEDTLS_GET_UINT32_BE(p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002575 p += 4;
2576
Gilles Peskine449bd832023-01-11 14:50:10 +01002577 if (end - p < 2) {
2578 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2579 }
2580 session->ticket_len = MBEDTLS_GET_UINT16_BE(p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002581 p += 2;
2582
Gilles Peskine449bd832023-01-11 14:50:10 +01002583 if (end - p < (long int) session->ticket_len) {
2584 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2585 }
2586 if (session->ticket_len > 0) {
2587 session->ticket = mbedtls_calloc(1, session->ticket_len);
2588 if (session->ticket == NULL) {
2589 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2590 }
2591 memcpy(session->ticket, p, session->ticket_len);
Jerry Yu438ddd82022-07-07 06:55:50 +00002592 p += session->ticket_len;
2593 }
2594 }
2595#endif /* MBEDTLS_SSL_CLI_C */
2596
Gilles Peskine449bd832023-01-11 14:50:10 +01002597 return 0;
Jerry Yu438ddd82022-07-07 06:55:50 +00002598
2599}
2600#else /* MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yue36fdd62022-08-17 21:31:36 +08002601MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002602static int ssl_tls13_session_save(const mbedtls_ssl_session *session,
2603 unsigned char *buf,
2604 size_t buf_len,
2605 size_t *olen)
Jerry Yu251a12e2022-07-13 15:15:48 +08002606{
2607 ((void) session);
2608 ((void) buf);
2609 ((void) buf_len);
Jerry Yue36fdd62022-08-17 21:31:36 +08002610 *olen = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002611 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Jerry Yu251a12e2022-07-13 15:15:48 +08002612}
Jerry Yu438ddd82022-07-07 06:55:50 +00002613
Gilles Peskine449bd832023-01-11 14:50:10 +01002614static int ssl_tls13_session_load(const mbedtls_ssl_session *session,
2615 unsigned char *buf,
2616 size_t buf_len)
Jerry Yu438ddd82022-07-07 06:55:50 +00002617{
2618 ((void) session);
2619 ((void) buf);
2620 ((void) buf_len);
Gilles Peskine449bd832023-01-11 14:50:10 +01002621 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Jerry Yu438ddd82022-07-07 06:55:50 +00002622}
2623#endif /* !MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yu251a12e2022-07-13 15:15:48 +08002624#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
2625
Gilles Peskine449bd832023-01-11 14:50:10 +01002626psa_status_t mbedtls_ssl_cipher_to_psa(mbedtls_cipher_type_t mbedtls_cipher_type,
2627 size_t taglen,
2628 psa_algorithm_t *alg,
2629 psa_key_type_t *key_type,
2630 size_t *key_size)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002631{
Gilles Peskine449bd832023-01-11 14:50:10 +01002632 switch (mbedtls_cipher_type) {
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002633 case MBEDTLS_CIPHER_AES_128_CBC:
2634 *alg = PSA_ALG_CBC_NO_PADDING;
2635 *key_type = PSA_KEY_TYPE_AES;
2636 *key_size = 128;
2637 break;
2638 case MBEDTLS_CIPHER_AES_128_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002639 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002640 *key_type = PSA_KEY_TYPE_AES;
2641 *key_size = 128;
2642 break;
2643 case MBEDTLS_CIPHER_AES_128_GCM:
2644 *alg = PSA_ALG_GCM;
2645 *key_type = PSA_KEY_TYPE_AES;
2646 *key_size = 128;
2647 break;
2648 case MBEDTLS_CIPHER_AES_192_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002649 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002650 *key_type = PSA_KEY_TYPE_AES;
2651 *key_size = 192;
2652 break;
2653 case MBEDTLS_CIPHER_AES_192_GCM:
2654 *alg = PSA_ALG_GCM;
2655 *key_type = PSA_KEY_TYPE_AES;
2656 *key_size = 192;
2657 break;
2658 case MBEDTLS_CIPHER_AES_256_CBC:
2659 *alg = PSA_ALG_CBC_NO_PADDING;
2660 *key_type = PSA_KEY_TYPE_AES;
2661 *key_size = 256;
2662 break;
2663 case MBEDTLS_CIPHER_AES_256_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002664 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002665 *key_type = PSA_KEY_TYPE_AES;
2666 *key_size = 256;
2667 break;
2668 case MBEDTLS_CIPHER_AES_256_GCM:
2669 *alg = PSA_ALG_GCM;
2670 *key_type = PSA_KEY_TYPE_AES;
2671 *key_size = 256;
2672 break;
2673 case MBEDTLS_CIPHER_ARIA_128_CBC:
2674 *alg = PSA_ALG_CBC_NO_PADDING;
2675 *key_type = PSA_KEY_TYPE_ARIA;
2676 *key_size = 128;
2677 break;
2678 case MBEDTLS_CIPHER_ARIA_128_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002679 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002680 *key_type = PSA_KEY_TYPE_ARIA;
2681 *key_size = 128;
2682 break;
2683 case MBEDTLS_CIPHER_ARIA_128_GCM:
2684 *alg = PSA_ALG_GCM;
2685 *key_type = PSA_KEY_TYPE_ARIA;
2686 *key_size = 128;
2687 break;
2688 case MBEDTLS_CIPHER_ARIA_192_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002689 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002690 *key_type = PSA_KEY_TYPE_ARIA;
2691 *key_size = 192;
2692 break;
2693 case MBEDTLS_CIPHER_ARIA_192_GCM:
2694 *alg = PSA_ALG_GCM;
2695 *key_type = PSA_KEY_TYPE_ARIA;
2696 *key_size = 192;
2697 break;
2698 case MBEDTLS_CIPHER_ARIA_256_CBC:
2699 *alg = PSA_ALG_CBC_NO_PADDING;
2700 *key_type = PSA_KEY_TYPE_ARIA;
2701 *key_size = 256;
2702 break;
2703 case MBEDTLS_CIPHER_ARIA_256_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002704 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002705 *key_type = PSA_KEY_TYPE_ARIA;
2706 *key_size = 256;
2707 break;
2708 case MBEDTLS_CIPHER_ARIA_256_GCM:
2709 *alg = PSA_ALG_GCM;
2710 *key_type = PSA_KEY_TYPE_ARIA;
2711 *key_size = 256;
2712 break;
2713 case MBEDTLS_CIPHER_CAMELLIA_128_CBC:
2714 *alg = PSA_ALG_CBC_NO_PADDING;
2715 *key_type = PSA_KEY_TYPE_CAMELLIA;
2716 *key_size = 128;
2717 break;
2718 case MBEDTLS_CIPHER_CAMELLIA_128_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002719 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002720 *key_type = PSA_KEY_TYPE_CAMELLIA;
2721 *key_size = 128;
2722 break;
2723 case MBEDTLS_CIPHER_CAMELLIA_128_GCM:
2724 *alg = PSA_ALG_GCM;
2725 *key_type = PSA_KEY_TYPE_CAMELLIA;
2726 *key_size = 128;
2727 break;
2728 case MBEDTLS_CIPHER_CAMELLIA_192_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002729 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002730 *key_type = PSA_KEY_TYPE_CAMELLIA;
2731 *key_size = 192;
2732 break;
2733 case MBEDTLS_CIPHER_CAMELLIA_192_GCM:
2734 *alg = PSA_ALG_GCM;
2735 *key_type = PSA_KEY_TYPE_CAMELLIA;
2736 *key_size = 192;
2737 break;
2738 case MBEDTLS_CIPHER_CAMELLIA_256_CBC:
2739 *alg = PSA_ALG_CBC_NO_PADDING;
2740 *key_type = PSA_KEY_TYPE_CAMELLIA;
2741 *key_size = 256;
2742 break;
2743 case MBEDTLS_CIPHER_CAMELLIA_256_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002744 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002745 *key_type = PSA_KEY_TYPE_CAMELLIA;
2746 *key_size = 256;
2747 break;
2748 case MBEDTLS_CIPHER_CAMELLIA_256_GCM:
2749 *alg = PSA_ALG_GCM;
2750 *key_type = PSA_KEY_TYPE_CAMELLIA;
2751 *key_size = 256;
2752 break;
2753 case MBEDTLS_CIPHER_CHACHA20_POLY1305:
2754 *alg = PSA_ALG_CHACHA20_POLY1305;
2755 *key_type = PSA_KEY_TYPE_CHACHA20;
2756 *key_size = 256;
2757 break;
2758 case MBEDTLS_CIPHER_NULL:
2759 *alg = MBEDTLS_SSL_NULL_CIPHER;
2760 *key_type = 0;
2761 *key_size = 0;
2762 break;
2763 default:
2764 return PSA_ERROR_NOT_SUPPORTED;
2765 }
2766
2767 return PSA_SUCCESS;
2768}
Neil Armstrong8395d7a2022-05-18 11:44:56 +02002769#endif /* MBEDTLS_USE_PSA_CRYPTO || MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002770
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002771#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002772int mbedtls_ssl_conf_dh_param_bin(mbedtls_ssl_config *conf,
2773 const unsigned char *dhm_P, size_t P_len,
2774 const unsigned char *dhm_G, size_t G_len)
Hanno Beckera90658f2017-10-04 15:29:08 +01002775{
Janos Follath865b3eb2019-12-16 11:46:15 +00002776 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckera90658f2017-10-04 15:29:08 +01002777
Gilles Peskine449bd832023-01-11 14:50:10 +01002778 mbedtls_mpi_free(&conf->dhm_P);
2779 mbedtls_mpi_free(&conf->dhm_G);
Glenn Strausscee11292021-12-20 01:43:17 -05002780
Gilles Peskine449bd832023-01-11 14:50:10 +01002781 if ((ret = mbedtls_mpi_read_binary(&conf->dhm_P, dhm_P, P_len)) != 0 ||
2782 (ret = mbedtls_mpi_read_binary(&conf->dhm_G, dhm_G, G_len)) != 0) {
2783 mbedtls_mpi_free(&conf->dhm_P);
2784 mbedtls_mpi_free(&conf->dhm_G);
2785 return ret;
Hanno Beckera90658f2017-10-04 15:29:08 +01002786 }
2787
Gilles Peskine449bd832023-01-11 14:50:10 +01002788 return 0;
Hanno Beckera90658f2017-10-04 15:29:08 +01002789}
Paul Bakker5121ce52009-01-03 21:22:43 +00002790
Gilles Peskine449bd832023-01-11 14:50:10 +01002791int mbedtls_ssl_conf_dh_param_ctx(mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx)
Paul Bakker1b57b062011-01-06 15:48:19 +00002792{
Janos Follath865b3eb2019-12-16 11:46:15 +00002793 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1b57b062011-01-06 15:48:19 +00002794
Gilles Peskine449bd832023-01-11 14:50:10 +01002795 mbedtls_mpi_free(&conf->dhm_P);
2796 mbedtls_mpi_free(&conf->dhm_G);
Glenn Strausscee11292021-12-20 01:43:17 -05002797
Gilles Peskine449bd832023-01-11 14:50:10 +01002798 if ((ret = mbedtls_dhm_get_value(dhm_ctx, MBEDTLS_DHM_PARAM_P,
2799 &conf->dhm_P)) != 0 ||
2800 (ret = mbedtls_dhm_get_value(dhm_ctx, MBEDTLS_DHM_PARAM_G,
2801 &conf->dhm_G)) != 0) {
2802 mbedtls_mpi_free(&conf->dhm_P);
2803 mbedtls_mpi_free(&conf->dhm_G);
2804 return ret;
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01002805 }
Paul Bakker1b57b062011-01-06 15:48:19 +00002806
Gilles Peskine449bd832023-01-11 14:50:10 +01002807 return 0;
Paul Bakker1b57b062011-01-06 15:48:19 +00002808}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002809#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00002810
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02002811#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
2812/*
2813 * Set the minimum length for Diffie-Hellman parameters
2814 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002815void mbedtls_ssl_conf_dhm_min_bitlen(mbedtls_ssl_config *conf,
2816 unsigned int bitlen)
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02002817{
2818 conf->dhm_min_bitlen = bitlen;
2819}
2820#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
2821
Ronald Crone68ab4f2022-10-05 12:46:29 +02002822#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002823#if !defined(MBEDTLS_DEPRECATED_REMOVED) && defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002824/*
2825 * Set allowed/preferred hashes for handshake signatures
2826 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002827void mbedtls_ssl_conf_sig_hashes(mbedtls_ssl_config *conf,
2828 const int *hashes)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002829{
2830 conf->sig_hashes = hashes;
2831}
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002832#endif /* !MBEDTLS_DEPRECATED_REMOVED && MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker1cd6e002021-08-10 13:27:10 +01002833
Jerry Yuf017ee42022-01-12 15:49:48 +08002834/* Configure allowed signature algorithms for handshake */
Gilles Peskine449bd832023-01-11 14:50:10 +01002835void mbedtls_ssl_conf_sig_algs(mbedtls_ssl_config *conf,
2836 const uint16_t *sig_algs)
Hanno Becker1cd6e002021-08-10 13:27:10 +01002837{
Jerry Yuf017ee42022-01-12 15:49:48 +08002838#if !defined(MBEDTLS_DEPRECATED_REMOVED)
2839 conf->sig_hashes = NULL;
2840#endif /* !MBEDTLS_DEPRECATED_REMOVED */
2841 conf->sig_algs = sig_algs;
Hanno Becker1cd6e002021-08-10 13:27:10 +01002842}
Ronald Crone68ab4f2022-10-05 12:46:29 +02002843#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002844
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02002845#if defined(MBEDTLS_ECP_C)
Brett Warrene0edc842021-08-17 09:53:13 +01002846#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002847/*
2848 * Set the allowed elliptic curves
Brett Warrene0edc842021-08-17 09:53:13 +01002849 *
2850 * mbedtls_ssl_setup() takes the provided list
2851 * and translates it to a list of IANA TLS group identifiers,
2852 * stored in ssl->handshake->group_list.
2853 *
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002854 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002855void mbedtls_ssl_conf_curves(mbedtls_ssl_config *conf,
2856 const mbedtls_ecp_group_id *curve_list)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002857{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002858 conf->curve_list = curve_list;
Brett Warrene0edc842021-08-17 09:53:13 +01002859 conf->group_list = NULL;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002860}
Brett Warrene0edc842021-08-17 09:53:13 +01002861#endif /* MBEDTLS_DEPRECATED_REMOVED */
Hanno Becker947194e2017-04-07 13:25:49 +01002862#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002863
Brett Warrene0edc842021-08-17 09:53:13 +01002864/*
2865 * Set the allowed groups
2866 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002867void mbedtls_ssl_conf_groups(mbedtls_ssl_config *conf,
2868 const uint16_t *group_list)
Brett Warrene0edc842021-08-17 09:53:13 +01002869{
2870#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
2871 conf->curve_list = NULL;
2872#endif
2873 conf->group_list = group_list;
2874}
2875
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002876#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002877int mbedtls_ssl_set_hostname(mbedtls_ssl_context *ssl, const char *hostname)
Paul Bakker5121ce52009-01-03 21:22:43 +00002878{
Hanno Becker947194e2017-04-07 13:25:49 +01002879 /* Initialize to suppress unnecessary compiler warning */
2880 size_t hostname_len = 0;
2881
2882 /* Check if new hostname is valid before
2883 * making any change to current one */
Gilles Peskine449bd832023-01-11 14:50:10 +01002884 if (hostname != NULL) {
2885 hostname_len = strlen(hostname);
Hanno Becker947194e2017-04-07 13:25:49 +01002886
Gilles Peskine449bd832023-01-11 14:50:10 +01002887 if (hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN) {
2888 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2889 }
Hanno Becker947194e2017-04-07 13:25:49 +01002890 }
2891
2892 /* Now it's clear that we will overwrite the old hostname,
2893 * so we can free it safely */
2894
Gilles Peskine449bd832023-01-11 14:50:10 +01002895 if (ssl->hostname != NULL) {
2896 mbedtls_platform_zeroize(ssl->hostname, strlen(ssl->hostname));
2897 mbedtls_free(ssl->hostname);
Hanno Becker947194e2017-04-07 13:25:49 +01002898 }
2899
2900 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01002901
Gilles Peskine449bd832023-01-11 14:50:10 +01002902 if (hostname == NULL) {
Hanno Becker947194e2017-04-07 13:25:49 +01002903 ssl->hostname = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01002904 } else {
2905 ssl->hostname = mbedtls_calloc(1, hostname_len + 1);
2906 if (ssl->hostname == NULL) {
2907 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2908 }
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002909
Gilles Peskine449bd832023-01-11 14:50:10 +01002910 memcpy(ssl->hostname, hostname, hostname_len);
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002911
Hanno Becker947194e2017-04-07 13:25:49 +01002912 ssl->hostname[hostname_len] = '\0';
2913 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002914
Gilles Peskine449bd832023-01-11 14:50:10 +01002915 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002916}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01002917#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002918
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002919#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01002920void mbedtls_ssl_conf_sni(mbedtls_ssl_config *conf,
2921 int (*f_sni)(void *, mbedtls_ssl_context *,
2922 const unsigned char *, size_t),
2923 void *p_sni)
Paul Bakker5701cdc2012-09-27 21:49:42 +00002924{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002925 conf->f_sni = f_sni;
2926 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00002927}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002928#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00002929
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002930#if defined(MBEDTLS_SSL_ALPN)
Gilles Peskine449bd832023-01-11 14:50:10 +01002931int mbedtls_ssl_conf_alpn_protocols(mbedtls_ssl_config *conf, const char **protos)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002932{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002933 size_t cur_len, tot_len;
2934 const char **p;
2935
2936 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08002937 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
2938 * MUST NOT be truncated."
2939 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002940 */
2941 tot_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002942 for (p = protos; *p != NULL; p++) {
2943 cur_len = strlen(*p);
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002944 tot_len += cur_len;
2945
Gilles Peskine449bd832023-01-11 14:50:10 +01002946 if ((cur_len == 0) ||
2947 (cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN) ||
2948 (tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN)) {
2949 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2950 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002951 }
2952
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002953 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002954
Gilles Peskine449bd832023-01-11 14:50:10 +01002955 return 0;
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002956}
2957
Gilles Peskine449bd832023-01-11 14:50:10 +01002958const char *mbedtls_ssl_get_alpn_protocol(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002959{
Gilles Peskine449bd832023-01-11 14:50:10 +01002960 return ssl->alpn_chosen;
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002961}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002962#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002963
Johan Pascalb62bb512015-12-03 21:56:45 +01002964#if defined(MBEDTLS_SSL_DTLS_SRTP)
Gilles Peskine449bd832023-01-11 14:50:10 +01002965void mbedtls_ssl_conf_srtp_mki_value_supported(mbedtls_ssl_config *conf,
2966 int support_mki_value)
Ron Eldor591f1622018-01-22 12:30:04 +02002967{
2968 conf->dtls_srtp_mki_support = support_mki_value;
2969}
2970
Gilles Peskine449bd832023-01-11 14:50:10 +01002971int mbedtls_ssl_dtls_srtp_set_mki_value(mbedtls_ssl_context *ssl,
2972 unsigned char *mki_value,
2973 uint16_t mki_len)
Ron Eldor591f1622018-01-22 12:30:04 +02002974{
Gilles Peskine449bd832023-01-11 14:50:10 +01002975 if (mki_len > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH) {
2976 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Ron Eldora9788042018-12-05 11:04:31 +02002977 }
Ron Eldor591f1622018-01-22 12:30:04 +02002978
Gilles Peskine449bd832023-01-11 14:50:10 +01002979 if (ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_UNSUPPORTED) {
2980 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Ron Eldora9788042018-12-05 11:04:31 +02002981 }
Ron Eldor591f1622018-01-22 12:30:04 +02002982
Gilles Peskine449bd832023-01-11 14:50:10 +01002983 memcpy(ssl->dtls_srtp_info.mki_value, mki_value, mki_len);
Ron Eldor591f1622018-01-22 12:30:04 +02002984 ssl->dtls_srtp_info.mki_len = mki_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002985 return 0;
Ron Eldor591f1622018-01-22 12:30:04 +02002986}
2987
Gilles Peskine449bd832023-01-11 14:50:10 +01002988int mbedtls_ssl_conf_dtls_srtp_protection_profiles(mbedtls_ssl_config *conf,
2989 const mbedtls_ssl_srtp_profile *profiles)
Johan Pascalb62bb512015-12-03 21:56:45 +01002990{
Johan Pascal253d0262020-09-22 13:04:45 +02002991 const mbedtls_ssl_srtp_profile *p;
2992 size_t list_size = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01002993
Johan Pascal253d0262020-09-22 13:04:45 +02002994 /* check the profiles list: all entry must be valid,
2995 * its size cannot be more than the total number of supported profiles, currently 4 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002996 for (p = profiles; *p != MBEDTLS_TLS_SRTP_UNSET &&
2997 list_size <= MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH;
2998 p++) {
2999 if (mbedtls_ssl_check_srtp_profile_value(*p) != MBEDTLS_TLS_SRTP_UNSET) {
Johan Pascal76fdf1d2020-10-22 23:31:00 +02003000 list_size++;
Gilles Peskine449bd832023-01-11 14:50:10 +01003001 } else {
Johan Pascal76fdf1d2020-10-22 23:31:00 +02003002 /* unsupported value, stop parsing and set the size to an error value */
3003 list_size = MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH + 1;
Johan Pascalb62bb512015-12-03 21:56:45 +01003004 }
3005 }
3006
Gilles Peskine449bd832023-01-11 14:50:10 +01003007 if (list_size > MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH) {
3008 conf->dtls_srtp_profile_list = NULL;
3009 conf->dtls_srtp_profile_list_len = 0;
3010 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Johan Pascal253d0262020-09-22 13:04:45 +02003011 }
3012
Johan Pascal9bc97ca2020-09-21 23:44:45 +02003013 conf->dtls_srtp_profile_list = profiles;
Johan Pascal253d0262020-09-22 13:04:45 +02003014 conf->dtls_srtp_profile_list_len = list_size;
Johan Pascalb62bb512015-12-03 21:56:45 +01003015
Gilles Peskine449bd832023-01-11 14:50:10 +01003016 return 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01003017}
3018
Gilles Peskine449bd832023-01-11 14:50:10 +01003019void mbedtls_ssl_get_dtls_srtp_negotiation_result(const mbedtls_ssl_context *ssl,
3020 mbedtls_dtls_srtp_info *dtls_srtp_info)
Johan Pascalb62bb512015-12-03 21:56:45 +01003021{
Johan Pascal2258a4f2020-10-28 13:53:09 +01003022 dtls_srtp_info->chosen_dtls_srtp_profile = ssl->dtls_srtp_info.chosen_dtls_srtp_profile;
3023 /* do not copy the mki value if there is no chosen profile */
Gilles Peskine449bd832023-01-11 14:50:10 +01003024 if (dtls_srtp_info->chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET) {
Johan Pascal2258a4f2020-10-28 13:53:09 +01003025 dtls_srtp_info->mki_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003026 } else {
Johan Pascal2258a4f2020-10-28 13:53:09 +01003027 dtls_srtp_info->mki_len = ssl->dtls_srtp_info.mki_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01003028 memcpy(dtls_srtp_info->mki_value, ssl->dtls_srtp_info.mki_value,
3029 ssl->dtls_srtp_info.mki_len);
Johan Pascal2258a4f2020-10-28 13:53:09 +01003030 }
Johan Pascalb62bb512015-12-03 21:56:45 +01003031}
Johan Pascalb62bb512015-12-03 21:56:45 +01003032#endif /* MBEDTLS_SSL_DTLS_SRTP */
3033
Aditya Patwardhan3096f332022-07-26 14:31:46 +05303034#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003035void mbedtls_ssl_conf_max_version(mbedtls_ssl_config *conf, int major, int minor)
Paul Bakker490ecc82011-10-06 13:04:09 +00003036{
Glenn Strauss2dfcea22022-03-14 17:26:42 -04003037 conf->max_tls_version = (major << 8) | minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00003038}
3039
Gilles Peskine449bd832023-01-11 14:50:10 +01003040void mbedtls_ssl_conf_min_version(mbedtls_ssl_config *conf, int major, int minor)
Paul Bakker1d29fb52012-09-28 13:28:45 +00003041{
Glenn Strauss2dfcea22022-03-14 17:26:42 -04003042 conf->min_tls_version = (major << 8) | minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00003043}
Aditya Patwardhan3096f332022-07-26 14:31:46 +05303044#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker1d29fb52012-09-28 13:28:45 +00003045
Janos Follath088ce432017-04-10 12:42:31 +01003046#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003047void mbedtls_ssl_conf_cert_req_ca_list(mbedtls_ssl_config *conf,
3048 char cert_req_ca_list)
Janos Follath088ce432017-04-10 12:42:31 +01003049{
3050 conf->cert_req_ca_list = cert_req_ca_list;
3051}
3052#endif
3053
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003054#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01003055void mbedtls_ssl_conf_encrypt_then_mac(mbedtls_ssl_config *conf, char etm)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01003056{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003057 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01003058}
3059#endif
3060
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003061#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Gilles Peskine449bd832023-01-11 14:50:10 +01003062void mbedtls_ssl_conf_extended_master_secret(mbedtls_ssl_config *conf, char ems)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02003063{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003064 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02003065}
3066#endif
3067
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003068#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01003069int mbedtls_ssl_conf_max_frag_len(mbedtls_ssl_config *conf, unsigned char mfl_code)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003070{
Gilles Peskine449bd832023-01-11 14:50:10 +01003071 if (mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
3072 ssl_mfl_code_to_length(mfl_code) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN) {
3073 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003074 }
3075
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01003076 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003077
Gilles Peskine449bd832023-01-11 14:50:10 +01003078 return 0;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003079}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003080#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003081
Gilles Peskine449bd832023-01-11 14:50:10 +01003082void mbedtls_ssl_conf_legacy_renegotiation(mbedtls_ssl_config *conf, int allow_legacy)
Paul Bakker48916f92012-09-16 19:57:18 +00003083{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003084 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00003085}
3086
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003087#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01003088void mbedtls_ssl_conf_renegotiation(mbedtls_ssl_config *conf, int renegotiation)
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003089{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003090 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003091}
3092
Gilles Peskine449bd832023-01-11 14:50:10 +01003093void mbedtls_ssl_conf_renegotiation_enforced(mbedtls_ssl_config *conf, int max_records)
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003094{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003095 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003096}
3097
Gilles Peskine449bd832023-01-11 14:50:10 +01003098void mbedtls_ssl_conf_renegotiation_period(mbedtls_ssl_config *conf,
3099 const unsigned char period[8])
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01003100{
Gilles Peskine449bd832023-01-11 14:50:10 +01003101 memcpy(conf->renego_period, period, 8);
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01003102}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003103#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00003104
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003105#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003106#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003107void mbedtls_ssl_conf_session_tickets(mbedtls_ssl_config *conf, int use_tickets)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003108{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01003109 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003110}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003111#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02003112
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003113#if defined(MBEDTLS_SSL_SRV_C)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08003114
Jerry Yud0766ec2022-09-22 10:46:57 +08003115#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003116void mbedtls_ssl_conf_new_session_tickets(mbedtls_ssl_config *conf,
3117 uint16_t num_tickets)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08003118{
Jerry Yud0766ec2022-09-22 10:46:57 +08003119 conf->new_session_tickets_count = num_tickets;
Jerry Yu1ad7ace2022-08-09 13:28:39 +08003120}
3121#endif
3122
Gilles Peskine449bd832023-01-11 14:50:10 +01003123void mbedtls_ssl_conf_session_tickets_cb(mbedtls_ssl_config *conf,
3124 mbedtls_ssl_ticket_write_t *f_ticket_write,
3125 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
3126 void *p_ticket)
Paul Bakker606b4ba2013-08-14 16:52:14 +02003127{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02003128 conf->f_ticket_write = f_ticket_write;
3129 conf->f_ticket_parse = f_ticket_parse;
3130 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02003131}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003132#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003133#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003134
Gilles Peskine449bd832023-01-11 14:50:10 +01003135void mbedtls_ssl_set_export_keys_cb(mbedtls_ssl_context *ssl,
3136 mbedtls_ssl_export_keys_t *f_export_keys,
3137 void *p_export_keys)
Robert Cragie4feb7ae2015-10-02 13:33:37 +01003138{
Hanno Becker7e6c1782021-06-08 09:24:55 +01003139 ssl->f_export_keys = f_export_keys;
3140 ssl->p_export_keys = p_export_keys;
Ron Eldorf5cc10d2019-05-07 18:33:40 +03003141}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01003142
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003143#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003144void mbedtls_ssl_conf_async_private_cb(
3145 mbedtls_ssl_config *conf,
3146 mbedtls_ssl_async_sign_t *f_async_sign,
3147 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
3148 mbedtls_ssl_async_resume_t *f_async_resume,
3149 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskine449bd832023-01-11 14:50:10 +01003150 void *async_config_data)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003151{
3152 conf->f_async_sign_start = f_async_sign;
3153 conf->f_async_decrypt_start = f_async_decrypt;
3154 conf->f_async_resume = f_async_resume;
3155 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003156 conf->p_async_config_data = async_config_data;
3157}
3158
Gilles Peskine449bd832023-01-11 14:50:10 +01003159void *mbedtls_ssl_conf_get_async_config_data(const mbedtls_ssl_config *conf)
Gilles Peskine8f97af72018-04-26 11:46:10 +02003160{
Gilles Peskine449bd832023-01-11 14:50:10 +01003161 return conf->p_async_config_data;
Gilles Peskine8f97af72018-04-26 11:46:10 +02003162}
3163
Gilles Peskine449bd832023-01-11 14:50:10 +01003164void *mbedtls_ssl_get_async_operation_data(const mbedtls_ssl_context *ssl)
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003165{
Gilles Peskine449bd832023-01-11 14:50:10 +01003166 if (ssl->handshake == NULL) {
3167 return NULL;
3168 } else {
3169 return ssl->handshake->user_async_ctx;
3170 }
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003171}
3172
Gilles Peskine449bd832023-01-11 14:50:10 +01003173void mbedtls_ssl_set_async_operation_data(mbedtls_ssl_context *ssl,
3174 void *ctx)
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003175{
Gilles Peskine449bd832023-01-11 14:50:10 +01003176 if (ssl->handshake != NULL) {
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003177 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +01003178 }
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003179}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003180#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003181
Paul Bakker5121ce52009-01-03 21:22:43 +00003182/*
3183 * SSL get accessors
3184 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003185uint32_t mbedtls_ssl_get_verify_result(const mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00003186{
Gilles Peskine449bd832023-01-11 14:50:10 +01003187 if (ssl->session != NULL) {
3188 return ssl->session->verify_result;
3189 }
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00003190
Gilles Peskine449bd832023-01-11 14:50:10 +01003191 if (ssl->session_negotiate != NULL) {
3192 return ssl->session_negotiate->verify_result;
3193 }
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00003194
Gilles Peskine449bd832023-01-11 14:50:10 +01003195 return 0xFFFFFFFF;
Paul Bakker5121ce52009-01-03 21:22:43 +00003196}
3197
Gilles Peskine449bd832023-01-11 14:50:10 +01003198int mbedtls_ssl_get_ciphersuite_id_from_ssl(const mbedtls_ssl_context *ssl)
Glenn Strauss8f526902022-01-13 00:04:49 -05003199{
Gilles Peskine449bd832023-01-11 14:50:10 +01003200 if (ssl == NULL || ssl->session == NULL) {
3201 return 0;
3202 }
Glenn Strauss8f526902022-01-13 00:04:49 -05003203
Gilles Peskine449bd832023-01-11 14:50:10 +01003204 return ssl->session->ciphersuite;
Glenn Strauss8f526902022-01-13 00:04:49 -05003205}
3206
Gilles Peskine449bd832023-01-11 14:50:10 +01003207const char *mbedtls_ssl_get_ciphersuite(const mbedtls_ssl_context *ssl)
Paul Bakker72f62662011-01-16 21:27:44 +00003208{
Gilles Peskine449bd832023-01-11 14:50:10 +01003209 if (ssl == NULL || ssl->session == NULL) {
3210 return NULL;
3211 }
Paul Bakker926c8e42013-03-06 10:23:34 +01003212
Gilles Peskine449bd832023-01-11 14:50:10 +01003213 return mbedtls_ssl_get_ciphersuite_name(ssl->session->ciphersuite);
Paul Bakker72f62662011-01-16 21:27:44 +00003214}
3215
Gilles Peskine449bd832023-01-11 14:50:10 +01003216const char *mbedtls_ssl_get_version(const mbedtls_ssl_context *ssl)
Paul Bakker43ca69c2011-01-15 17:35:19 +00003217{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003218#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003219 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
3220 switch (ssl->tls_version) {
Glenn Strauss60bfe602022-03-14 19:04:24 -04003221 case MBEDTLS_SSL_VERSION_TLS1_2:
Gilles Peskine449bd832023-01-11 14:50:10 +01003222 return "DTLSv1.2";
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003223 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01003224 return "unknown (DTLS)";
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003225 }
3226 }
3227#endif
3228
Gilles Peskine449bd832023-01-11 14:50:10 +01003229 switch (ssl->tls_version) {
Glenn Strauss60bfe602022-03-14 19:04:24 -04003230 case MBEDTLS_SSL_VERSION_TLS1_2:
Gilles Peskine449bd832023-01-11 14:50:10 +01003231 return "TLSv1.2";
Glenn Strauss60bfe602022-03-14 19:04:24 -04003232 case MBEDTLS_SSL_VERSION_TLS1_3:
Gilles Peskine449bd832023-01-11 14:50:10 +01003233 return "TLSv1.3";
Paul Bakker43ca69c2011-01-15 17:35:19 +00003234 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01003235 return "unknown";
Paul Bakker43ca69c2011-01-15 17:35:19 +00003236 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00003237}
3238
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003239#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01003240size_t mbedtls_ssl_get_input_max_frag_len(const mbedtls_ssl_context *ssl)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003241{
David Horstmann95d516f2021-05-04 18:36:56 +01003242 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003243 size_t read_mfl;
3244
Jerry Yuddda0502022-12-01 19:43:12 +08003245#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003246 /* Use the configured MFL for the client if we're past SERVER_HELLO_DONE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003247 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
3248 ssl->state >= MBEDTLS_SSL_SERVER_HELLO_DONE) {
3249 return ssl_mfl_code_to_length(ssl->conf->mfl_code);
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003250 }
Jerry Yuddda0502022-12-01 19:43:12 +08003251#endif
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003252
3253 /* Check if a smaller max length was negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01003254 if (ssl->session_out != NULL) {
3255 read_mfl = ssl_mfl_code_to_length(ssl->session_out->mfl_code);
3256 if (read_mfl < max_len) {
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003257 max_len = read_mfl;
3258 }
3259 }
3260
Jerry Yuddda0502022-12-01 19:43:12 +08003261 /* During a handshake, use the value being negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01003262 if (ssl->session_negotiate != NULL) {
3263 read_mfl = ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code);
3264 if (read_mfl < max_len) {
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003265 max_len = read_mfl;
3266 }
3267 }
3268
Gilles Peskine449bd832023-01-11 14:50:10 +01003269 return max_len;
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003270}
3271
Gilles Peskine449bd832023-01-11 14:50:10 +01003272size_t mbedtls_ssl_get_output_max_frag_len(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003273{
3274 size_t max_len;
3275
3276 /*
3277 * Assume mfl_code is correct since it was checked when set
3278 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003279 max_len = ssl_mfl_code_to_length(ssl->conf->mfl_code);
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003280
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02003281 /* Check if a smaller max length was negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01003282 if (ssl->session_out != NULL &&
3283 ssl_mfl_code_to_length(ssl->session_out->mfl_code) < max_len) {
3284 max_len = ssl_mfl_code_to_length(ssl->session_out->mfl_code);
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003285 }
3286
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02003287 /* During a handshake, use the value being negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01003288 if (ssl->session_negotiate != NULL &&
3289 ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code) < max_len) {
3290 max_len = ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code);
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02003291 }
3292
Gilles Peskine449bd832023-01-11 14:50:10 +01003293 return max_len;
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003294}
3295#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
3296
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003297#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003298size_t mbedtls_ssl_get_current_mtu(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003299{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04003300 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003301 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
3302 (ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
3303 ssl->state == MBEDTLS_SSL_SERVER_HELLO)) {
3304 return 0;
3305 }
Andrzej Kurekef43ce62018-10-09 08:24:12 -04003306
Gilles Peskine449bd832023-01-11 14:50:10 +01003307 if (ssl->handshake == NULL || ssl->handshake->mtu == 0) {
3308 return ssl->mtu;
3309 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003310
Gilles Peskine449bd832023-01-11 14:50:10 +01003311 if (ssl->mtu == 0) {
3312 return ssl->handshake->mtu;
3313 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003314
Gilles Peskine449bd832023-01-11 14:50:10 +01003315 return ssl->mtu < ssl->handshake->mtu ?
3316 ssl->mtu : ssl->handshake->mtu;
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003317}
3318#endif /* MBEDTLS_SSL_PROTO_DTLS */
3319
Gilles Peskine449bd832023-01-11 14:50:10 +01003320int mbedtls_ssl_get_max_out_record_payload(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003321{
3322 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
3323
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02003324#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
3325 !defined(MBEDTLS_SSL_PROTO_DTLS)
3326 (void) ssl;
3327#endif
3328
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003329#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01003330 const size_t mfl = mbedtls_ssl_get_output_max_frag_len(ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003331
Gilles Peskine449bd832023-01-11 14:50:10 +01003332 if (max_len > mfl) {
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003333 max_len = mfl;
Gilles Peskine449bd832023-01-11 14:50:10 +01003334 }
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003335#endif
3336
3337#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003338 if (mbedtls_ssl_get_current_mtu(ssl) != 0) {
3339 const size_t mtu = mbedtls_ssl_get_current_mtu(ssl);
3340 const int ret = mbedtls_ssl_get_record_expansion(ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003341 const size_t overhead = (size_t) ret;
3342
Gilles Peskine449bd832023-01-11 14:50:10 +01003343 if (ret < 0) {
3344 return ret;
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003345 }
3346
Gilles Peskine449bd832023-01-11 14:50:10 +01003347 if (mtu <= overhead) {
3348 MBEDTLS_SSL_DEBUG_MSG(1, ("MTU too low for record expansion"));
3349 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
3350 }
3351
3352 if (max_len > mtu - overhead) {
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003353 max_len = mtu - overhead;
Gilles Peskine449bd832023-01-11 14:50:10 +01003354 }
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003355 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003356#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003357
Hanno Becker0defedb2018-08-10 12:35:02 +01003358#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
3359 !defined(MBEDTLS_SSL_PROTO_DTLS)
3360 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003361#endif
3362
Gilles Peskine449bd832023-01-11 14:50:10 +01003363 return (int) max_len;
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003364}
3365
Gilles Peskine449bd832023-01-11 14:50:10 +01003366int mbedtls_ssl_get_max_in_record_payload(const mbedtls_ssl_context *ssl)
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003367{
3368 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
3369
3370#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
3371 (void) ssl;
3372#endif
3373
3374#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01003375 const size_t mfl = mbedtls_ssl_get_input_max_frag_len(ssl);
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003376
Gilles Peskine449bd832023-01-11 14:50:10 +01003377 if (max_len > mfl) {
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003378 max_len = mfl;
Gilles Peskine449bd832023-01-11 14:50:10 +01003379 }
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003380#endif
3381
Gilles Peskine449bd832023-01-11 14:50:10 +01003382 return (int) max_len;
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003383}
3384
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003385#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003386const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert(const mbedtls_ssl_context *ssl)
Paul Bakkerb0550d92012-10-30 07:51:03 +00003387{
Gilles Peskine449bd832023-01-11 14:50:10 +01003388 if (ssl == NULL || ssl->session == NULL) {
3389 return NULL;
3390 }
Paul Bakkerb0550d92012-10-30 07:51:03 +00003391
Hanno Beckere6824572019-02-07 13:18:46 +00003392#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01003393 return ssl->session->peer_cert;
Hanno Beckere6824572019-02-07 13:18:46 +00003394#else
Gilles Peskine449bd832023-01-11 14:50:10 +01003395 return NULL;
Hanno Beckere6824572019-02-07 13:18:46 +00003396#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00003397}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003398#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00003399
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003400#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003401int mbedtls_ssl_get_session(const mbedtls_ssl_context *ssl,
3402 mbedtls_ssl_session *dst)
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003403{
Hanno Beckere810bbc2021-05-14 16:01:05 +01003404 int ret;
3405
Gilles Peskine449bd832023-01-11 14:50:10 +01003406 if (ssl == NULL ||
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003407 dst == NULL ||
3408 ssl->session == NULL ||
Gilles Peskine449bd832023-01-11 14:50:10 +01003409 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT) {
3410 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003411 }
3412
Hanno Beckere810bbc2021-05-14 16:01:05 +01003413 /* Since Mbed TLS 3.0, mbedtls_ssl_get_session() is no longer
3414 * idempotent: Each session can only be exported once.
3415 *
3416 * (This is in preparation for TLS 1.3 support where we will
3417 * need the ability to export multiple sessions (aka tickets),
3418 * which will be achieved by calling mbedtls_ssl_get_session()
3419 * multiple times until it fails.)
3420 *
3421 * Check whether we have already exported the current session,
3422 * and fail if so.
3423 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003424 if (ssl->session->exported == 1) {
3425 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
3426 }
Hanno Beckere810bbc2021-05-14 16:01:05 +01003427
Gilles Peskine449bd832023-01-11 14:50:10 +01003428 ret = mbedtls_ssl_session_copy(dst, ssl->session);
3429 if (ret != 0) {
3430 return ret;
3431 }
Hanno Beckere810bbc2021-05-14 16:01:05 +01003432
3433 /* Remember that we've exported the session. */
3434 ssl->session->exported = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003435 return 0;
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003436}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003437#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003438
Paul Bakker5121ce52009-01-03 21:22:43 +00003439/*
Hanno Beckera835da52019-05-16 12:39:07 +01003440 * Define ticket header determining Mbed TLS version
3441 * and structure of the ticket.
3442 */
3443
Hanno Becker94ef3b32019-05-16 12:50:45 +01003444/*
Hanno Becker50b59662019-05-28 14:30:45 +01003445 * Define bitflag determining compile-time settings influencing
3446 * structure of serialized SSL sessions.
Hanno Becker94ef3b32019-05-16 12:50:45 +01003447 */
3448
Hanno Becker50b59662019-05-28 14:30:45 +01003449#if defined(MBEDTLS_HAVE_TIME)
Hanno Becker3e088662019-05-29 11:10:18 +01003450#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker50b59662019-05-28 14:30:45 +01003451#else
Hanno Becker3e088662019-05-29 11:10:18 +01003452#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003453#endif /* MBEDTLS_HAVE_TIME */
3454
3455#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker3e088662019-05-29 11:10:18 +01003456#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003457#else
Hanno Becker3e088662019-05-29 11:10:18 +01003458#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003459#endif /* MBEDTLS_X509_CRT_PARSE_C */
3460
3461#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Becker3e088662019-05-29 11:10:18 +01003462#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003463#else
Hanno Becker3e088662019-05-29 11:10:18 +01003464#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003465#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
3466
3467#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Becker3e088662019-05-29 11:10:18 +01003468#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003469#else
Hanno Becker3e088662019-05-29 11:10:18 +01003470#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003471#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
3472
Hanno Becker94ef3b32019-05-16 12:50:45 +01003473#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3e088662019-05-29 11:10:18 +01003474#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003475#else
Hanno Becker3e088662019-05-29 11:10:18 +01003476#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003477#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
3478
Hanno Becker94ef3b32019-05-16 12:50:45 +01003479#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3480#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
3481#else
3482#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
3483#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3484
Hanno Becker3e088662019-05-29 11:10:18 +01003485#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
3486#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
3487#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
3488#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
Hanno Becker37bdbe62021-08-01 05:38:58 +01003489#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 4
3490#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 5
Hanno Becker3e088662019-05-29 11:10:18 +01003491
Hanno Becker50b59662019-05-28 14:30:45 +01003492#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Gilles Peskine449bd832023-01-11 14:50:10 +01003493 ((uint16_t) ( \
3494 (SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT) | \
3495 (SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT) | \
3496 (SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << \
3497 SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT) | \
3498 (SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT) | \
3499 (SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT) | \
3500 (SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT)))
Hanno Becker94ef3b32019-05-16 12:50:45 +01003501
Hanno Beckerf8787072019-05-16 12:41:07 +01003502static unsigned char ssl_serialized_session_header[] = {
Hanno Becker94ef3b32019-05-16 12:50:45 +01003503 MBEDTLS_VERSION_MAJOR,
3504 MBEDTLS_VERSION_MINOR,
3505 MBEDTLS_VERSION_PATCH,
Gilles Peskine449bd832023-01-11 14:50:10 +01003506 MBEDTLS_BYTE_1(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
3507 MBEDTLS_BYTE_0(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
Hanno Beckerf8787072019-05-16 12:41:07 +01003508};
Hanno Beckera835da52019-05-16 12:39:07 +01003509
3510/*
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003511 * Serialize a session in the following format:
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02003512 * (in the presentation language of TLS, RFC 8446 section 3)
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003513 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003514 * struct {
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01003515 *
Hanno Beckerdce50972021-08-01 05:39:23 +01003516 * opaque mbedtls_version[3]; // library version: major, minor, patch
3517 * opaque session_format[2]; // library-version specific 16-bit field
3518 * // determining the format of the remaining
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003519 * // serialized data.
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01003520 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003521 * Note: When updating the format, remember to keep
3522 * these version+format bytes.
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003523 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003524 * // In this version, `session_format` determines
3525 * // the setting of those compile-time
3526 * // configuration options which influence
3527 * // the structure of mbedtls_ssl_session.
3528 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003529 * uint8_t minor_ver; // Protocol minor version. Possible values:
Jerry Yu0c2a7382022-12-06 13:27:25 +08003530 * // - TLS 1.2 (0x0303)
3531 * // - TLS 1.3 (0x0304)
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003532 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003533 * select (serialized_session.tls_version) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003534 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003535 * case MBEDTLS_SSL_VERSION_TLS1_2:
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003536 * serialized_session_tls12 data;
Jerry Yu0c2a7382022-12-06 13:27:25 +08003537 * case MBEDTLS_SSL_VERSION_TLS1_3:
Jerry Yuddda0502022-12-01 19:43:12 +08003538 * serialized_session_tls13 data;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003539 *
3540 * };
3541 *
3542 * } serialized_session;
3543 *
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003544 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003545
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003546MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003547static int ssl_session_save(const mbedtls_ssl_session *session,
3548 unsigned char omit_header,
3549 unsigned char *buf,
3550 size_t buf_len,
3551 size_t *olen)
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003552{
3553 unsigned char *p = buf;
3554 size_t used = 0;
Jerry Yu251a12e2022-07-13 15:15:48 +08003555 size_t remaining_len;
Jerry Yue36fdd62022-08-17 21:31:36 +08003556#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3557 size_t out_len;
3558 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3559#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01003560 if (session == NULL) {
3561 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
3562 }
Jerry Yu438ddd82022-07-07 06:55:50 +00003563
Gilles Peskine449bd832023-01-11 14:50:10 +01003564 if (!omit_header) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003565 /*
3566 * Add Mbed TLS version identifier
3567 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003568 used += sizeof(ssl_serialized_session_header);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003569
Gilles Peskine449bd832023-01-11 14:50:10 +01003570 if (used <= buf_len) {
3571 memcpy(p, ssl_serialized_session_header,
3572 sizeof(ssl_serialized_session_header));
3573 p += sizeof(ssl_serialized_session_header);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003574 }
3575 }
3576
3577 /*
3578 * TLS version identifier
3579 */
3580 used += 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003581 if (used <= buf_len) {
3582 *p++ = MBEDTLS_BYTE_0(session->tls_version);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003583 }
3584
3585 /* Forward to version-specific serialization routine. */
Jerry Yufca4d572022-07-21 10:37:48 +08003586 remaining_len = (buf_len >= used) ? buf_len - used : 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003587 switch (session->tls_version) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003588#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01003589 case MBEDTLS_SSL_VERSION_TLS1_2:
3590 used += ssl_tls12_session_save(session, p, remaining_len);
3591 break;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003592#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3593
Jerry Yu251a12e2022-07-13 15:15:48 +08003594#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01003595 case MBEDTLS_SSL_VERSION_TLS1_3:
3596 ret = ssl_tls13_session_save(session, p, remaining_len, &out_len);
3597 if (ret != 0 && ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL) {
3598 return ret;
3599 }
3600 used += out_len;
3601 break;
Jerry Yu251a12e2022-07-13 15:15:48 +08003602#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3603
Gilles Peskine449bd832023-01-11 14:50:10 +01003604 default:
3605 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003606 }
3607
3608 *olen = used;
Gilles Peskine449bd832023-01-11 14:50:10 +01003609 if (used > buf_len) {
3610 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
3611 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003612
Gilles Peskine449bd832023-01-11 14:50:10 +01003613 return 0;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003614}
3615
3616/*
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003617 * Public wrapper for ssl_session_save()
3618 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003619int mbedtls_ssl_session_save(const mbedtls_ssl_session *session,
3620 unsigned char *buf,
3621 size_t buf_len,
3622 size_t *olen)
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003623{
Gilles Peskine449bd832023-01-11 14:50:10 +01003624 return ssl_session_save(session, 0, buf, buf_len, olen);
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003625}
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003626
Jerry Yuf1b23ca2022-02-18 11:48:47 +08003627/*
3628 * Deserialize session, see mbedtls_ssl_session_save() for format.
3629 *
3630 * This internal version is wrapped by a public function that cleans up in
3631 * case of error, and has an extra option omit_header.
3632 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003633MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003634static int ssl_session_load(mbedtls_ssl_session *session,
3635 unsigned char omit_header,
3636 const unsigned char *buf,
3637 size_t len)
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003638{
3639 const unsigned char *p = buf;
3640 const unsigned char * const end = buf + len;
Jerry Yu438ddd82022-07-07 06:55:50 +00003641 size_t remaining_len;
3642
3643
Gilles Peskine449bd832023-01-11 14:50:10 +01003644 if (session == NULL) {
3645 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
3646 }
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003647
Gilles Peskine449bd832023-01-11 14:50:10 +01003648 if (!omit_header) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003649 /*
3650 * Check Mbed TLS version identifier
3651 */
3652
Gilles Peskine449bd832023-01-11 14:50:10 +01003653 if ((size_t) (end - p) < sizeof(ssl_serialized_session_header)) {
3654 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003655 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003656
3657 if (memcmp(p, ssl_serialized_session_header,
3658 sizeof(ssl_serialized_session_header)) != 0) {
3659 return MBEDTLS_ERR_SSL_VERSION_MISMATCH;
3660 }
3661 p += sizeof(ssl_serialized_session_header);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003662 }
3663
3664 /*
3665 * TLS version identifier
3666 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003667 if (1 > (size_t) (end - p)) {
3668 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3669 }
Glenn Straussda7851c2022-03-14 13:29:48 -04003670 session->tls_version = 0x0300 | *p++;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003671
3672 /* Dispatch according to TLS version. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003673 remaining_len = (end - p);
3674 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 return ssl_tls12_session_load(session, p, remaining_len);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003678#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3679
Jerry Yu438ddd82022-07-07 06:55:50 +00003680#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01003681 case MBEDTLS_SSL_VERSION_TLS1_3:
3682 return ssl_tls13_session_load(session, p, remaining_len);
Jerry Yu438ddd82022-07-07 06:55:50 +00003683#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3684
Gilles Peskine449bd832023-01-11 14:50:10 +01003685 default:
3686 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003687 }
3688}
3689
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003690/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02003691 * Deserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003692 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003693int mbedtls_ssl_session_load(mbedtls_ssl_session *session,
3694 const unsigned char *buf,
3695 size_t len)
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003696{
Gilles Peskine449bd832023-01-11 14:50:10 +01003697 int ret = ssl_session_load(session, 0, buf, len);
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003698
Gilles Peskine449bd832023-01-11 14:50:10 +01003699 if (ret != 0) {
3700 mbedtls_ssl_session_free(session);
3701 }
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003702
Gilles Peskine449bd832023-01-11 14:50:10 +01003703 return ret;
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003704}
3705
3706/*
Paul Bakker1961b702013-01-25 14:49:24 +01003707 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00003708 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003709MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003710static int ssl_prepare_handshake_step(mbedtls_ssl_context *ssl)
Hanno Becker41934dd2021-08-07 19:13:43 +01003711{
3712 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3713
Ronald Cron66dbf912022-02-02 15:33:46 +01003714 /*
3715 * We may have not been able to send to the peer all the handshake data
Ronald Cron3f20b772022-03-08 16:00:02 +01003716 * that were written into the output buffer by the previous handshake step,
3717 * if the write to the network callback returned with the
Ronald Cron66dbf912022-02-02 15:33:46 +01003718 * #MBEDTLS_ERR_SSL_WANT_WRITE error code.
3719 * We proceed to the next handshake step only when all data from the
3720 * previous one have been sent to the peer, thus we make sure that this is
3721 * the case here by calling `mbedtls_ssl_flush_output()`. The function may
3722 * return with the #MBEDTLS_ERR_SSL_WANT_WRITE error code in which case
3723 * we have to wait before to go ahead.
3724 * In the case of TLS 1.3, handshake step handlers do not send data to the
3725 * peer. Data are only sent here and through
3726 * `mbedtls_ssl_handle_pending_alert` in case an error that triggered an
Andrzej Kurek5c65c572022-04-13 14:28:52 -04003727 * alert occurred.
Ronald Cron66dbf912022-02-02 15:33:46 +01003728 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003729 if ((ret = mbedtls_ssl_flush_output(ssl)) != 0) {
3730 return ret;
3731 }
Hanno Becker41934dd2021-08-07 19:13:43 +01003732
3733#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003734 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3735 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING) {
3736 if ((ret = mbedtls_ssl_flight_transmit(ssl)) != 0) {
3737 return ret;
3738 }
Hanno Becker41934dd2021-08-07 19:13:43 +01003739 }
3740#endif /* MBEDTLS_SSL_PROTO_DTLS */
3741
Gilles Peskine449bd832023-01-11 14:50:10 +01003742 return ret;
Hanno Becker41934dd2021-08-07 19:13:43 +01003743}
3744
Gilles Peskine449bd832023-01-11 14:50:10 +01003745int mbedtls_ssl_handshake_step(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00003746{
Hanno Becker41934dd2021-08-07 19:13:43 +01003747 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003748
Gilles Peskine449bd832023-01-11 14:50:10 +01003749 if (ssl == NULL ||
Hanno Becker41934dd2021-08-07 19:13:43 +01003750 ssl->conf == NULL ||
3751 ssl->handshake == NULL ||
Gilles Peskine449bd832023-01-11 14:50:10 +01003752 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER) {
3753 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Becker41934dd2021-08-07 19:13:43 +01003754 }
3755
Gilles Peskine449bd832023-01-11 14:50:10 +01003756 ret = ssl_prepare_handshake_step(ssl);
3757 if (ret != 0) {
3758 return ret;
3759 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003760
Gilles Peskine449bd832023-01-11 14:50:10 +01003761 ret = mbedtls_ssl_handle_pending_alert(ssl);
3762 if (ret != 0) {
Jerry Yue7047812021-09-13 19:26:39 +08003763 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01003764 }
Jerry Yue7047812021-09-13 19:26:39 +08003765
Tom Cosgrove2fdc7b32022-09-21 12:33:17 +01003766 /* If ssl->conf->endpoint is not one of MBEDTLS_SSL_IS_CLIENT or
3767 * MBEDTLS_SSL_IS_SERVER, this is the return code we give */
3768 ret = MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3769
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003770#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003771 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
3772 MBEDTLS_SSL_DEBUG_MSG(2, ("client state: %s",
3773 mbedtls_ssl_states_str(ssl->state)));
Jerry Yub9930e72021-08-06 17:11:51 +08003774
Gilles Peskine449bd832023-01-11 14:50:10 +01003775 switch (ssl->state) {
Ronald Cron9f0fba32022-02-10 16:45:15 +01003776 case MBEDTLS_SSL_HELLO_REQUEST:
3777 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
Tom Cosgrove87d9c6c2022-09-22 09:27:56 +01003778 ret = 0;
Ronald Cron9f0fba32022-02-10 16:45:15 +01003779 break;
Jerry Yub9930e72021-08-06 17:11:51 +08003780
Ronald Cron9f0fba32022-02-10 16:45:15 +01003781 case MBEDTLS_SSL_CLIENT_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01003782 ret = mbedtls_ssl_write_client_hello(ssl);
Ronald Cron9f0fba32022-02-10 16:45:15 +01003783 break;
3784
3785 default:
3786#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01003787 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
3788 ret = mbedtls_ssl_tls13_handshake_client_step(ssl);
3789 } else {
3790 ret = mbedtls_ssl_handshake_client_step(ssl);
3791 }
Ronald Cron9f0fba32022-02-10 16:45:15 +01003792#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01003793 ret = mbedtls_ssl_handshake_client_step(ssl);
Ronald Cron9f0fba32022-02-10 16:45:15 +01003794#else
Gilles Peskine449bd832023-01-11 14:50:10 +01003795 ret = mbedtls_ssl_tls13_handshake_client_step(ssl);
Ronald Cron9f0fba32022-02-10 16:45:15 +01003796#endif
3797 }
Jerry Yub9930e72021-08-06 17:11:51 +08003798 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003799#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003800#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003801 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Ronald Cron6f135e12021-12-08 16:57:54 +01003802#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01003803 if (mbedtls_ssl_conf_is_tls13_only(ssl->conf)) {
3804 ret = mbedtls_ssl_tls13_handshake_server_step(ssl);
3805 }
Ronald Cron6f135e12021-12-08 16:57:54 +01003806#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yub9930e72021-08-06 17:11:51 +08003807
3808#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01003809 if (mbedtls_ssl_conf_is_tls12_only(ssl->conf)) {
3810 ret = mbedtls_ssl_handshake_server_step(ssl);
3811 }
Jerry Yub9930e72021-08-06 17:11:51 +08003812#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3813 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003814#endif
3815
Gilles Peskine449bd832023-01-11 14:50:10 +01003816 if (ret != 0) {
Jerry Yubbd5a3f2021-09-18 20:50:22 +08003817 /* handshake_step return error. And it is same
3818 * with alert_reason.
3819 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003820 if (ssl->send_alert) {
3821 ret = mbedtls_ssl_handle_pending_alert(ssl);
Jerry Yue7047812021-09-13 19:26:39 +08003822 goto cleanup;
3823 }
3824 }
3825
3826cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01003827 return ret;
Paul Bakker1961b702013-01-25 14:49:24 +01003828}
3829
3830/*
3831 * Perform the SSL handshake
3832 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003833int mbedtls_ssl_handshake(mbedtls_ssl_context *ssl)
Paul Bakker1961b702013-01-25 14:49:24 +01003834{
3835 int ret = 0;
3836
Hanno Beckera817ea42020-10-20 15:20:23 +01003837 /* Sanity checks */
3838
Gilles Peskine449bd832023-01-11 14:50:10 +01003839 if (ssl == NULL || ssl->conf == NULL) {
3840 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3841 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003842
Hanno Beckera817ea42020-10-20 15:20:23 +01003843#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003844 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3845 (ssl->f_set_timer == NULL || ssl->f_get_timer == NULL)) {
3846 MBEDTLS_SSL_DEBUG_MSG(1, ("You must use "
3847 "mbedtls_ssl_set_timer_cb() for DTLS"));
3848 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Beckera817ea42020-10-20 15:20:23 +01003849 }
3850#endif /* MBEDTLS_SSL_PROTO_DTLS */
3851
Gilles Peskine449bd832023-01-11 14:50:10 +01003852 MBEDTLS_SSL_DEBUG_MSG(2, ("=> handshake"));
Paul Bakker1961b702013-01-25 14:49:24 +01003853
Hanno Beckera817ea42020-10-20 15:20:23 +01003854 /* Main handshake loop */
Gilles Peskine449bd832023-01-11 14:50:10 +01003855 while (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER) {
3856 ret = mbedtls_ssl_handshake_step(ssl);
Paul Bakker1961b702013-01-25 14:49:24 +01003857
Gilles Peskine449bd832023-01-11 14:50:10 +01003858 if (ret != 0) {
Paul Bakker1961b702013-01-25 14:49:24 +01003859 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01003860 }
Paul Bakker1961b702013-01-25 14:49:24 +01003861 }
3862
Gilles Peskine449bd832023-01-11 14:50:10 +01003863 MBEDTLS_SSL_DEBUG_MSG(2, ("<= handshake"));
Paul Bakker5121ce52009-01-03 21:22:43 +00003864
Gilles Peskine449bd832023-01-11 14:50:10 +01003865 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003866}
3867
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003868#if defined(MBEDTLS_SSL_RENEGOTIATION)
3869#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003870/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003871 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00003872 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003873MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003874static int ssl_write_hello_request(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003875{
Janos Follath865b3eb2019-12-16 11:46:15 +00003876 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003877
Gilles Peskine449bd832023-01-11 14:50:10 +01003878 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write hello request"));
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003879
3880 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003881 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3882 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003883
Gilles Peskine449bd832023-01-11 14:50:10 +01003884 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
3885 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
3886 return ret;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003887 }
3888
Gilles Peskine449bd832023-01-11 14:50:10 +01003889 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write hello request"));
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003890
Gilles Peskine449bd832023-01-11 14:50:10 +01003891 return 0;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003892}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003893#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003894
3895/*
3896 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003897 * - any side: calling mbedtls_ssl_renegotiate(),
3898 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
3899 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02003900 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003901 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003902 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003903 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003904int mbedtls_ssl_start_renegotiation(mbedtls_ssl_context *ssl)
Paul Bakker48916f92012-09-16 19:57:18 +00003905{
Janos Follath865b3eb2019-12-16 11:46:15 +00003906 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker48916f92012-09-16 19:57:18 +00003907
Gilles Peskine449bd832023-01-11 14:50:10 +01003908 MBEDTLS_SSL_DEBUG_MSG(2, ("=> renegotiate"));
Paul Bakker48916f92012-09-16 19:57:18 +00003909
Gilles Peskine449bd832023-01-11 14:50:10 +01003910 if ((ret = ssl_handshake_init(ssl)) != 0) {
3911 return ret;
3912 }
Paul Bakker48916f92012-09-16 19:57:18 +00003913
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003914 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
3915 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003916#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003917 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3918 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING) {
3919 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003920 ssl->handshake->out_msg_seq = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003921 } else {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003922 ssl->handshake->in_msg_seq = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003923 }
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003924 }
3925#endif
3926
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003927 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
3928 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00003929
Gilles Peskine449bd832023-01-11 14:50:10 +01003930 if ((ret = mbedtls_ssl_handshake(ssl)) != 0) {
3931 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_handshake", ret);
3932 return ret;
Paul Bakker48916f92012-09-16 19:57:18 +00003933 }
3934
Gilles Peskine449bd832023-01-11 14:50:10 +01003935 MBEDTLS_SSL_DEBUG_MSG(2, ("<= renegotiate"));
Paul Bakker48916f92012-09-16 19:57:18 +00003936
Gilles Peskine449bd832023-01-11 14:50:10 +01003937 return 0;
Paul Bakker48916f92012-09-16 19:57:18 +00003938}
3939
3940/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003941 * Renegotiate current connection on client,
3942 * or request renegotiation on server
3943 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003944int mbedtls_ssl_renegotiate(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003945{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003946 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003947
Gilles Peskine449bd832023-01-11 14:50:10 +01003948 if (ssl == NULL || ssl->conf == NULL) {
3949 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3950 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003951
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003952#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003953 /* On server, just send the request */
Gilles Peskine449bd832023-01-11 14:50:10 +01003954 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
3955 if (mbedtls_ssl_is_handshake_over(ssl) == 0) {
3956 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3957 }
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003958
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003959 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02003960
3961 /* Did we already try/start sending HelloRequest? */
Gilles Peskine449bd832023-01-11 14:50:10 +01003962 if (ssl->out_left != 0) {
3963 return mbedtls_ssl_flush_output(ssl);
3964 }
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02003965
Gilles Peskine449bd832023-01-11 14:50:10 +01003966 return ssl_write_hello_request(ssl);
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003967 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003968#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003969
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003970#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003971 /*
3972 * On client, either start the renegotiation process or,
3973 * if already in progress, continue the handshake
3974 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003975 if (ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
3976 if (mbedtls_ssl_is_handshake_over(ssl) == 0) {
3977 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003978 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003979
3980 if ((ret = mbedtls_ssl_start_renegotiation(ssl)) != 0) {
3981 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_start_renegotiation", ret);
3982 return ret;
3983 }
3984 } else {
3985 if ((ret = mbedtls_ssl_handshake(ssl)) != 0) {
3986 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_handshake", ret);
3987 return ret;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003988 }
3989 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003990#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003991
Gilles Peskine449bd832023-01-11 14:50:10 +01003992 return ret;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003993}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003994#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003995
Gilles Peskine449bd832023-01-11 14:50:10 +01003996void mbedtls_ssl_handshake_free(mbedtls_ssl_context *ssl)
Paul Bakker48916f92012-09-16 19:57:18 +00003997{
Gilles Peskine9b562d52018-04-25 20:32:43 +02003998 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
3999
Gilles Peskine449bd832023-01-11 14:50:10 +01004000 if (handshake == NULL) {
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004001 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01004002 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004003
Brett Warrene0edc842021-08-17 09:53:13 +01004004#if defined(MBEDTLS_ECP_C)
4005#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01004006 if (ssl->handshake->group_list_heap_allocated) {
4007 mbedtls_free((void *) handshake->group_list);
4008 }
Brett Warrene0edc842021-08-17 09:53:13 +01004009 handshake->group_list = NULL;
4010#endif /* MBEDTLS_DEPRECATED_REMOVED */
4011#endif /* MBEDTLS_ECP_C */
4012
Ronald Crone68ab4f2022-10-05 12:46:29 +02004013#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +08004014#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01004015 if (ssl->handshake->sig_algs_heap_allocated) {
4016 mbedtls_free((void *) handshake->sig_algs);
4017 }
Jerry Yuf017ee42022-01-12 15:49:48 +08004018 handshake->sig_algs = NULL;
4019#endif /* MBEDTLS_DEPRECATED_REMOVED */
Xiaofei Baic234ecf2022-02-08 09:59:23 +00004020#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01004021 if (ssl->handshake->certificate_request_context) {
4022 mbedtls_free((void *) handshake->certificate_request_context);
Xiaofei Baic234ecf2022-02-08 09:59:23 +00004023 }
4024#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Ronald Crone68ab4f2022-10-05 12:46:29 +02004025#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yuf017ee42022-01-12 15:49:48 +08004026
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02004027#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01004028 if (ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0) {
4029 ssl->conf->f_async_cancel(ssl);
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02004030 handshake->async_in_progress = 0;
4031 }
4032#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
4033
Andrzej Kurek25f27152022-08-17 16:09:31 -04004034#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -05004035#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01004036 psa_hash_abort(&handshake->fin_sha256_psa);
Andrzej Kurekeb342242019-01-29 09:14:33 -05004037#else
Gilles Peskine449bd832023-01-11 14:50:10 +01004038 mbedtls_sha256_free(&handshake->fin_sha256);
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02004039#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05004040#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004041#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -05004042#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01004043 psa_hash_abort(&handshake->fin_sha384_psa);
Andrzej Kurekeb342242019-01-29 09:14:33 -05004044#else
Gilles Peskine449bd832023-01-11 14:50:10 +01004045 mbedtls_sha512_free(&handshake->fin_sha384);
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02004046#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05004047#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02004048
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004049#if defined(MBEDTLS_DHM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004050 mbedtls_dhm_free(&handshake->dhm_ctx);
Paul Bakker48916f92012-09-16 19:57:18 +00004051#endif
Neil Armstrongf3f46412022-04-12 14:43:39 +02004052#if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004053 mbedtls_ecdh_free(&handshake->ecdh_ctx);
Paul Bakker61d113b2013-07-04 11:51:43 +02004054#endif
Valerio Setti02c25b52022-11-15 14:08:42 +01004055
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02004056#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Neil Armstrongca7d5062022-05-31 14:43:23 +02004057#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01004058 psa_pake_abort(&handshake->psa_pake_ctx);
Valerio Settieb3f7882022-12-08 18:42:58 +01004059 /*
4060 * Opaque keys are not stored in the handshake's data and it's the user
4061 * responsibility to destroy them. Clear ones, instead, are created by
4062 * the TLS library and should be destroyed at the same level
4063 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004064 if (!mbedtls_svc_key_id_is_null(handshake->psa_pake_password)) {
4065 psa_destroy_key(handshake->psa_pake_password);
Valerio Settieb3f7882022-12-08 18:42:58 +01004066 }
Neil Armstrongca7d5062022-05-31 14:43:23 +02004067 handshake->psa_pake_password = MBEDTLS_SVC_KEY_ID_INIT;
4068#else
Gilles Peskine449bd832023-01-11 14:50:10 +01004069 mbedtls_ecjpake_free(&handshake->ecjpake_ctx);
Neil Armstrongca7d5062022-05-31 14:43:23 +02004070#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02004071#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004072 mbedtls_free(handshake->ecjpake_cache);
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02004073 handshake->ecjpake_cache = NULL;
4074 handshake->ecjpake_cache_len = 0;
4075#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02004076#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02004077
Janos Follath4ae5c292016-02-10 11:27:43 +00004078#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
4079 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02004080 /* explicit void pointer cast for buggy MS compiler */
Gilles Peskine449bd832023-01-11 14:50:10 +01004081 mbedtls_free((void *) handshake->curves_tls_id);
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004082#endif
4083
Ronald Cron73fe8df2022-10-05 14:31:43 +02004084#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02004085#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01004086 if (!mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque)) {
Neil Armstrong501c9322022-05-03 09:35:09 +02004087 /* The maintenance of the external PSK key slot is the
4088 * user's responsibility. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004089 if (ssl->handshake->psk_opaque_is_internal) {
4090 psa_destroy_key(ssl->handshake->psk_opaque);
Neil Armstrong501c9322022-05-03 09:35:09 +02004091 ssl->handshake->psk_opaque_is_internal = 0;
4092 }
4093 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
4094 }
Neil Armstronge952a302022-05-03 10:22:14 +02004095#else
Gilles Peskine449bd832023-01-11 14:50:10 +01004096 if (handshake->psk != NULL) {
4097 mbedtls_platform_zeroize(handshake->psk, handshake->psk_len);
4098 mbedtls_free(handshake->psk);
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004099 }
Neil Armstronge952a302022-05-03 10:22:14 +02004100#endif /* MBEDTLS_USE_PSA_CRYPTO */
Ronald Cron73fe8df2022-10-05 14:31:43 +02004101#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004102
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004103#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
4104 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02004105 /*
4106 * Free only the linked list wrapper, not the keys themselves
4107 * since the belong to the SNI callback
4108 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004109 ssl_key_cert_free(handshake->sni_key_cert);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004110#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004111
Gilles Peskineeccd8882020-03-10 12:19:08 +01004112#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01004113 mbedtls_x509_crt_restart_free(&handshake->ecrs_ctx);
4114 if (handshake->ecrs_peer_cert != NULL) {
4115 mbedtls_x509_crt_free(handshake->ecrs_peer_cert);
4116 mbedtls_free(handshake->ecrs_peer_cert);
Hanno Becker3dad3112019-02-05 17:19:52 +00004117 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02004118#endif
4119
Hanno Becker75173122019-02-06 16:18:31 +00004120#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
4121 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01004122 mbedtls_pk_free(&handshake->peer_pubkey);
Hanno Becker75173122019-02-06 16:18:31 +00004123#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
4124
XiaokangQian9b93c0d2022-02-09 06:02:25 +00004125#if defined(MBEDTLS_SSL_CLI_C) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01004126 (defined(MBEDTLS_SSL_PROTO_DTLS) || defined(MBEDTLS_SSL_PROTO_TLS1_3))
4127 mbedtls_free(handshake->cookie);
XiaokangQian9b93c0d2022-02-09 06:02:25 +00004128#endif /* MBEDTLS_SSL_CLI_C &&
4129 ( MBEDTLS_SSL_PROTO_DTLS || MBEDTLS_SSL_PROTO_TLS1_3 ) */
XiaokangQian8499b6c2022-01-27 09:00:11 +00004130
4131#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004132 mbedtls_ssl_flight_free(handshake->flight);
4133 mbedtls_ssl_buffering_free(ssl);
XiaokangQian8499b6c2022-01-27 09:00:11 +00004134#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02004135
Ronald Cronf12b81d2022-03-15 10:42:41 +01004136#if defined(MBEDTLS_ECDH_C) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01004137 (defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3))
4138 if (handshake->ecdh_psa_privkey_is_external == 0) {
4139 psa_destroy_key(handshake->ecdh_psa_privkey);
4140 }
Hanno Becker4a63ed42019-01-08 11:39:35 +00004141#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
4142
Ronald Cron6f135e12021-12-08 16:57:54 +01004143#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01004144 mbedtls_ssl_transform_free(handshake->transform_handshake);
4145 mbedtls_free(handshake->transform_handshake);
Jerry Yu3d9b5902022-11-04 14:07:25 +08004146#if defined(MBEDTLS_SSL_EARLY_DATA)
Gilles Peskine449bd832023-01-11 14:50:10 +01004147 mbedtls_ssl_transform_free(handshake->transform_earlydata);
4148 mbedtls_free(handshake->transform_earlydata);
Jerry Yu3d9b5902022-11-04 14:07:25 +08004149#endif
Ronald Cron6f135e12021-12-08 16:57:54 +01004150#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yuba9c7272021-10-30 11:54:10 +08004151
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004152
4153#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4154 /* If the buffers are too big - reallocate. Because of the way Mbed TLS
4155 * processes datagrams and the fact that a datagram is allowed to have
4156 * several records in it, it is possible that the I/O buffers are not
4157 * empty at this stage */
Gilles Peskine449bd832023-01-11 14:50:10 +01004158 handle_buffer_resizing(ssl, 1, mbedtls_ssl_get_input_buflen(ssl),
4159 mbedtls_ssl_get_output_buflen(ssl));
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004160#endif
Hanno Becker3aa186f2021-08-10 09:24:19 +01004161
Jerry Yuba9c7272021-10-30 11:54:10 +08004162 /* mbedtls_platform_zeroize MUST be last one in this function */
Gilles Peskine449bd832023-01-11 14:50:10 +01004163 mbedtls_platform_zeroize(handshake,
4164 sizeof(mbedtls_ssl_handshake_params));
Paul Bakker48916f92012-09-16 19:57:18 +00004165}
4166
Gilles Peskine449bd832023-01-11 14:50:10 +01004167void mbedtls_ssl_session_free(mbedtls_ssl_session *session)
Paul Bakker48916f92012-09-16 19:57:18 +00004168{
Gilles Peskine449bd832023-01-11 14:50:10 +01004169 if (session == NULL) {
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004170 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01004171 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004172
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004173#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004174 ssl_clear_peer_cert(session);
Paul Bakkered27a042013-04-18 22:46:23 +02004175#endif
Paul Bakker0a597072012-09-25 21:55:46 +00004176
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02004177#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Xiaokang Qianbc663a02022-10-09 11:14:39 +00004178#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
4179 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01004180 mbedtls_free(session->hostname);
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00004181#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01004182 mbedtls_free(session->ticket);
Paul Bakkera503a632013-08-14 13:48:06 +02004183#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004184
Gilles Peskine449bd832023-01-11 14:50:10 +01004185 mbedtls_platform_zeroize(session, sizeof(mbedtls_ssl_session));
Paul Bakker48916f92012-09-16 19:57:18 +00004186}
4187
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02004188#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004189
4190#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4191#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u
4192#else
4193#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u
4194#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4195
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004196#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004197
4198#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4199#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u
4200#else
4201#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u
4202#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4203
4204#if defined(MBEDTLS_SSL_ALPN)
4205#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u
4206#else
4207#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u
4208#endif /* MBEDTLS_SSL_ALPN */
4209
4210#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0
4211#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1
4212#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2
4213#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3
4214
4215#define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \
Gilles Peskine449bd832023-01-11 14:50:10 +01004216 ((uint32_t) ( \
4217 (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << \
4218 SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT) | \
4219 (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << \
4220 SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT) | \
4221 (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << \
4222 SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT) | \
4223 (SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT) | \
4224 0u))
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004225
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004226static unsigned char ssl_serialized_context_header[] = {
4227 MBEDTLS_VERSION_MAJOR,
4228 MBEDTLS_VERSION_MINOR,
4229 MBEDTLS_VERSION_PATCH,
Gilles Peskine449bd832023-01-11 14:50:10 +01004230 MBEDTLS_BYTE_1(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
4231 MBEDTLS_BYTE_0(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
4232 MBEDTLS_BYTE_2(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG),
4233 MBEDTLS_BYTE_1(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG),
4234 MBEDTLS_BYTE_0(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG),
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004235};
4236
Paul Bakker5121ce52009-01-03 21:22:43 +00004237/*
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004238 * Serialize a full SSL context
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02004239 *
4240 * The format of the serialized data is:
4241 * (in the presentation language of TLS, RFC 8446 section 3)
4242 *
4243 * // header
4244 * opaque mbedtls_version[3]; // major, minor, patch
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004245 * opaque context_format[5]; // version-specific field determining
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02004246 * // the format of the remaining
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004247 * // serialized data.
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004248 * Note: When updating the format, remember to keep these
4249 * version+format bytes. (We may make their size part of the API.)
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02004250 *
4251 * // session sub-structure
4252 * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save()
4253 * // transform sub-structure
4254 * uint8 random[64]; // ServerHello.random+ClientHello.random
4255 * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value
4256 * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use
4257 * // fields from ssl_context
4258 * uint32 badmac_seen; // DTLS: number of records with failing MAC
4259 * uint64 in_window_top; // DTLS: last validated record seq_num
4260 * uint64 in_window; // DTLS: bitmask for replay protection
4261 * uint8 disable_datagram_packing; // DTLS: only one record per datagram
4262 * uint64 cur_out_ctr; // Record layer: outgoing sequence number
4263 * uint16 mtu; // DTLS: path mtu (max outgoing fragment size)
4264 * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol
4265 *
4266 * Note that many fields of the ssl_context or sub-structures are not
4267 * serialized, as they fall in one of the following categories:
4268 *
4269 * 1. forced value (eg in_left must be 0)
4270 * 2. pointer to dynamically-allocated memory (eg session, transform)
4271 * 3. value can be re-derived from other data (eg session keys from MS)
4272 * 4. value was temporary (eg content of input buffer)
4273 * 5. value will be provided by the user again (eg I/O callbacks and context)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004274 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004275int mbedtls_ssl_context_save(mbedtls_ssl_context *ssl,
4276 unsigned char *buf,
4277 size_t buf_len,
4278 size_t *olen)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004279{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004280 unsigned char *p = buf;
4281 size_t used = 0;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004282 size_t session_len;
4283 int ret = 0;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004284
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02004285 /*
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004286 * Enforce usage restrictions, see "return BAD_INPUT_DATA" in
4287 * this function's documentation.
4288 *
4289 * These are due to assumptions/limitations in the implementation. Some of
4290 * them are likely to stay (no handshake in progress) some might go away
4291 * (only DTLS) but are currently used to simplify the implementation.
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02004292 */
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004293 /* The initial handshake must be over */
Gilles Peskine449bd832023-01-11 14:50:10 +01004294 if (mbedtls_ssl_is_handshake_over(ssl) == 0) {
4295 MBEDTLS_SSL_DEBUG_MSG(1, ("Initial handshake isn't over"));
4296 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004297 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004298 if (ssl->handshake != NULL) {
4299 MBEDTLS_SSL_DEBUG_MSG(1, ("Handshake isn't completed"));
4300 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004301 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004302 /* Double-check that sub-structures are indeed ready */
Gilles Peskine449bd832023-01-11 14:50:10 +01004303 if (ssl->transform == NULL || ssl->session == NULL) {
4304 MBEDTLS_SSL_DEBUG_MSG(1, ("Serialised structures aren't ready"));
4305 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004306 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004307 /* There must be no pending incoming or outgoing data */
Gilles Peskine449bd832023-01-11 14:50:10 +01004308 if (mbedtls_ssl_check_pending(ssl) != 0) {
4309 MBEDTLS_SSL_DEBUG_MSG(1, ("There is pending incoming data"));
4310 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004311 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004312 if (ssl->out_left != 0) {
4313 MBEDTLS_SSL_DEBUG_MSG(1, ("There is pending outgoing data"));
4314 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004315 }
Dave Rodgman556e8a32022-12-06 16:31:25 +00004316 /* Protocol must be DTLS, not TLS */
Gilles Peskine449bd832023-01-11 14:50:10 +01004317 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
4318 MBEDTLS_SSL_DEBUG_MSG(1, ("Only DTLS is supported"));
4319 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004320 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004321 /* Version must be 1.2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004322 if (ssl->tls_version != MBEDTLS_SSL_VERSION_TLS1_2) {
4323 MBEDTLS_SSL_DEBUG_MSG(1, ("Only version 1.2 supported"));
4324 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004325 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004326 /* We must be using an AEAD ciphersuite */
Gilles Peskine449bd832023-01-11 14:50:10 +01004327 if (mbedtls_ssl_transform_uses_aead(ssl->transform) != 1) {
4328 MBEDTLS_SSL_DEBUG_MSG(1, ("Only AEAD ciphersuites supported"));
4329 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004330 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004331 /* Renegotiation must not be enabled */
4332#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01004333 if (ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED) {
4334 MBEDTLS_SSL_DEBUG_MSG(1, ("Renegotiation must not be enabled"));
4335 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004336 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004337#endif
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004338
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004339 /*
4340 * Version and format identifier
4341 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004342 used += sizeof(ssl_serialized_context_header);
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004343
Gilles Peskine449bd832023-01-11 14:50:10 +01004344 if (used <= buf_len) {
4345 memcpy(p, ssl_serialized_context_header,
4346 sizeof(ssl_serialized_context_header));
4347 p += sizeof(ssl_serialized_context_header);
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004348 }
4349
4350 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004351 * Session (length + data)
4352 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004353 ret = ssl_session_save(ssl->session, 1, NULL, 0, &session_len);
4354 if (ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL) {
4355 return ret;
4356 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004357
4358 used += 4 + session_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004359 if (used <= buf_len) {
4360 MBEDTLS_PUT_UINT32_BE(session_len, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004361 p += 4;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004362
Gilles Peskine449bd832023-01-11 14:50:10 +01004363 ret = ssl_session_save(ssl->session, 1,
4364 p, session_len, &session_len);
4365 if (ret != 0) {
4366 return ret;
4367 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004368
4369 p += session_len;
4370 }
4371
4372 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004373 * Transform
4374 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004375 used += sizeof(ssl->transform->randbytes);
4376 if (used <= buf_len) {
4377 memcpy(p, ssl->transform->randbytes,
4378 sizeof(ssl->transform->randbytes));
4379 p += sizeof(ssl->transform->randbytes);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004380 }
4381
4382#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4383 used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004384 if (used <= buf_len) {
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004385 *p++ = ssl->transform->in_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004386 memcpy(p, ssl->transform->in_cid, ssl->transform->in_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004387 p += ssl->transform->in_cid_len;
4388
4389 *p++ = ssl->transform->out_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004390 memcpy(p, ssl->transform->out_cid, ssl->transform->out_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004391 p += ssl->transform->out_cid_len;
4392 }
4393#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4394
4395 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004396 * Saved fields from top-level ssl_context structure
4397 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004398 used += 4;
Gilles Peskine449bd832023-01-11 14:50:10 +01004399 if (used <= buf_len) {
4400 MBEDTLS_PUT_UINT32_BE(ssl->badmac_seen, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004401 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004402 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004403
4404#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4405 used += 16;
Gilles Peskine449bd832023-01-11 14:50:10 +01004406 if (used <= buf_len) {
4407 MBEDTLS_PUT_UINT64_BE(ssl->in_window_top, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004408 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004409
Gilles Peskine449bd832023-01-11 14:50:10 +01004410 MBEDTLS_PUT_UINT64_BE(ssl->in_window, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004411 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004412 }
4413#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4414
4415#if defined(MBEDTLS_SSL_PROTO_DTLS)
4416 used += 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004417 if (used <= buf_len) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004418 *p++ = ssl->disable_datagram_packing;
4419 }
4420#endif /* MBEDTLS_SSL_PROTO_DTLS */
4421
Jerry Yuae0b2e22021-10-08 15:21:19 +08004422 used += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Gilles Peskine449bd832023-01-11 14:50:10 +01004423 if (used <= buf_len) {
4424 memcpy(p, ssl->cur_out_ctr, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN);
Jerry Yuae0b2e22021-10-08 15:21:19 +08004425 p += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004426 }
4427
4428#if defined(MBEDTLS_SSL_PROTO_DTLS)
4429 used += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01004430 if (used <= buf_len) {
4431 MBEDTLS_PUT_UINT16_BE(ssl->mtu, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004432 p += 2;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004433 }
4434#endif /* MBEDTLS_SSL_PROTO_DTLS */
4435
4436#if defined(MBEDTLS_SSL_ALPN)
4437 {
4438 const uint8_t alpn_len = ssl->alpn_chosen
Gilles Peskine449bd832023-01-11 14:50:10 +01004439 ? (uint8_t) strlen(ssl->alpn_chosen)
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004440 : 0;
4441
4442 used += 1 + alpn_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004443 if (used <= buf_len) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004444 *p++ = alpn_len;
4445
Gilles Peskine449bd832023-01-11 14:50:10 +01004446 if (ssl->alpn_chosen != NULL) {
4447 memcpy(p, ssl->alpn_chosen, alpn_len);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004448 p += alpn_len;
4449 }
4450 }
4451 }
4452#endif /* MBEDTLS_SSL_ALPN */
4453
4454 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004455 * Done
4456 */
4457 *olen = used;
4458
Gilles Peskine449bd832023-01-11 14:50:10 +01004459 if (used > buf_len) {
4460 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
4461 }
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004462
Gilles Peskine449bd832023-01-11 14:50:10 +01004463 MBEDTLS_SSL_DEBUG_BUF(4, "saved context", buf, used);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004464
Gilles Peskine449bd832023-01-11 14:50:10 +01004465 return mbedtls_ssl_session_reset_int(ssl, 0);
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004466}
4467
4468/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02004469 * Deserialize context, see mbedtls_ssl_context_save() for format.
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004470 *
4471 * This internal version is wrapped by a public function that cleans up in
4472 * case of error.
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004473 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004474MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01004475static int ssl_context_load(mbedtls_ssl_context *ssl,
4476 const unsigned char *buf,
4477 size_t len)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004478{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004479 const unsigned char *p = buf;
4480 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004481 size_t session_len;
Janos Follath865b3eb2019-12-16 11:46:15 +00004482 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004483#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek894edde2022-09-29 06:31:14 -04004484 tls_prf_fn prf_func = NULL;
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004485#endif
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004486
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004487 /*
4488 * The context should have been freshly setup or reset.
4489 * Give the user an error in case of obvious misuse.
Manuel Pégourié-Gonnard4ca930f2019-07-26 16:31:53 +02004490 * (Checking session is useful because it won't be NULL if we're
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004491 * renegotiating, or if the user mistakenly loaded a session first.)
4492 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004493 if (ssl->state != MBEDTLS_SSL_HELLO_REQUEST ||
4494 ssl->session != NULL) {
4495 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004496 }
4497
4498 /*
4499 * We can't check that the config matches the initial one, but we can at
4500 * least check it matches the requirements for serializing.
4501 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004502 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
Glenn Strauss2dfcea22022-03-14 17:26:42 -04004503 ssl->conf->max_tls_version < MBEDTLS_SSL_VERSION_TLS1_2 ||
4504 ssl->conf->min_tls_version > MBEDTLS_SSL_VERSION_TLS1_2 ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004505#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02004506 ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004507#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01004508 0) {
4509 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004510 }
4511
Gilles Peskine449bd832023-01-11 14:50:10 +01004512 MBEDTLS_SSL_DEBUG_BUF(4, "context to load", buf, len);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004513
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004514 /*
4515 * Check version identifier
4516 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004517 if ((size_t) (end - p) < sizeof(ssl_serialized_context_header)) {
4518 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004519 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004520
4521 if (memcmp(p, ssl_serialized_context_header,
4522 sizeof(ssl_serialized_context_header)) != 0) {
4523 return MBEDTLS_ERR_SSL_VERSION_MISMATCH;
4524 }
4525 p += sizeof(ssl_serialized_context_header);
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004526
4527 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004528 * Session
4529 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004530 if ((size_t) (end - p) < 4) {
4531 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4532 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004533
Gilles Peskine449bd832023-01-11 14:50:10 +01004534 session_len = ((size_t) p[0] << 24) |
4535 ((size_t) p[1] << 16) |
4536 ((size_t) p[2] << 8) |
4537 ((size_t) p[3]);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004538 p += 4;
4539
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004540 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00004541 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004542 ssl->session = ssl->session_negotiate;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004543 ssl->session_in = ssl->session;
4544 ssl->session_out = ssl->session;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004545 ssl->session_negotiate = NULL;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004546
Gilles Peskine449bd832023-01-11 14:50:10 +01004547 if ((size_t) (end - p) < session_len) {
4548 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4549 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004550
Gilles Peskine449bd832023-01-11 14:50:10 +01004551 ret = ssl_session_load(ssl->session, 1, p, session_len);
4552 if (ret != 0) {
4553 mbedtls_ssl_session_free(ssl->session);
4554 return ret;
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004555 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004556
4557 p += session_len;
4558
4559 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004560 * Transform
4561 */
4562
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004563 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00004564 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Jerry Yu2e199812022-12-01 18:57:19 +08004565#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004566 ssl->transform = ssl->transform_negotiate;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004567 ssl->transform_in = ssl->transform;
4568 ssl->transform_out = ssl->transform;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004569 ssl->transform_negotiate = NULL;
Jerry Yu2e199812022-12-01 18:57:19 +08004570#endif
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004571
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004572#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01004573 prf_func = ssl_tls12prf_from_cs(ssl->session->ciphersuite);
4574 if (prf_func == NULL) {
4575 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4576 }
Andrzej Kurek894edde2022-09-29 06:31:14 -04004577
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004578 /* Read random bytes and populate structure */
Gilles Peskine449bd832023-01-11 14:50:10 +01004579 if ((size_t) (end - p) < sizeof(ssl->transform->randbytes)) {
4580 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4581 }
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004582
Gilles Peskine449bd832023-01-11 14:50:10 +01004583 ret = ssl_tls12_populate_transform(ssl->transform,
4584 ssl->session->ciphersuite,
4585 ssl->session->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02004586#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004587 ssl->session->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02004588#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01004589 prf_func,
4590 p, /* currently pointing to randbytes */
4591 MBEDTLS_SSL_VERSION_TLS1_2, /* (D)TLS 1.2 is forced */
4592 ssl->conf->endpoint,
4593 ssl);
4594 if (ret != 0) {
4595 return ret;
4596 }
Jerry Yu840fbb22022-02-17 14:59:29 +08004597#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004598 p += sizeof(ssl->transform->randbytes);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004599
4600#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4601 /* Read connection IDs and store them */
Gilles Peskine449bd832023-01-11 14:50:10 +01004602 if ((size_t) (end - p) < 1) {
4603 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4604 }
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004605
4606 ssl->transform->in_cid_len = *p++;
4607
Gilles Peskine449bd832023-01-11 14:50:10 +01004608 if ((size_t) (end - p) < ssl->transform->in_cid_len + 1u) {
4609 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4610 }
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004611
Gilles Peskine449bd832023-01-11 14:50:10 +01004612 memcpy(ssl->transform->in_cid, p, ssl->transform->in_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004613 p += ssl->transform->in_cid_len;
4614
4615 ssl->transform->out_cid_len = *p++;
4616
Gilles Peskine449bd832023-01-11 14:50:10 +01004617 if ((size_t) (end - p) < ssl->transform->out_cid_len) {
4618 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4619 }
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004620
Gilles Peskine449bd832023-01-11 14:50:10 +01004621 memcpy(ssl->transform->out_cid, p, ssl->transform->out_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004622 p += ssl->transform->out_cid_len;
4623#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4624
4625 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004626 * Saved fields from top-level ssl_context structure
4627 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004628 if ((size_t) (end - p) < 4) {
4629 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4630 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004631
Gilles Peskine449bd832023-01-11 14:50:10 +01004632 ssl->badmac_seen = ((uint32_t) p[0] << 24) |
4633 ((uint32_t) p[1] << 16) |
4634 ((uint32_t) p[2] << 8) |
4635 ((uint32_t) p[3]);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004636 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004637
4638#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine449bd832023-01-11 14:50:10 +01004639 if ((size_t) (end - p) < 16) {
4640 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4641 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004642
Gilles Peskine449bd832023-01-11 14:50:10 +01004643 ssl->in_window_top = ((uint64_t) p[0] << 56) |
4644 ((uint64_t) p[1] << 48) |
4645 ((uint64_t) p[2] << 40) |
4646 ((uint64_t) p[3] << 32) |
4647 ((uint64_t) p[4] << 24) |
4648 ((uint64_t) p[5] << 16) |
4649 ((uint64_t) p[6] << 8) |
4650 ((uint64_t) p[7]);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004651 p += 8;
4652
Gilles Peskine449bd832023-01-11 14:50:10 +01004653 ssl->in_window = ((uint64_t) p[0] << 56) |
4654 ((uint64_t) p[1] << 48) |
4655 ((uint64_t) p[2] << 40) |
4656 ((uint64_t) p[3] << 32) |
4657 ((uint64_t) p[4] << 24) |
4658 ((uint64_t) p[5] << 16) |
4659 ((uint64_t) p[6] << 8) |
4660 ((uint64_t) p[7]);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004661 p += 8;
4662#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4663
4664#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004665 if ((size_t) (end - p) < 1) {
4666 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4667 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004668
4669 ssl->disable_datagram_packing = *p++;
4670#endif /* MBEDTLS_SSL_PROTO_DTLS */
4671
Gilles Peskine449bd832023-01-11 14:50:10 +01004672 if ((size_t) (end - p) < sizeof(ssl->cur_out_ctr)) {
4673 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4674 }
4675 memcpy(ssl->cur_out_ctr, p, sizeof(ssl->cur_out_ctr));
4676 p += sizeof(ssl->cur_out_ctr);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004677
4678#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004679 if ((size_t) (end - p) < 2) {
4680 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4681 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004682
Gilles Peskine449bd832023-01-11 14:50:10 +01004683 ssl->mtu = (p[0] << 8) | p[1];
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004684 p += 2;
4685#endif /* MBEDTLS_SSL_PROTO_DTLS */
4686
4687#if defined(MBEDTLS_SSL_ALPN)
4688 {
4689 uint8_t alpn_len;
4690 const char **cur;
4691
Gilles Peskine449bd832023-01-11 14:50:10 +01004692 if ((size_t) (end - p) < 1) {
4693 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4694 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004695
4696 alpn_len = *p++;
4697
Gilles Peskine449bd832023-01-11 14:50:10 +01004698 if (alpn_len != 0 && ssl->conf->alpn_list != NULL) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004699 /* alpn_chosen should point to an item in the configured list */
Gilles Peskine449bd832023-01-11 14:50:10 +01004700 for (cur = ssl->conf->alpn_list; *cur != NULL; cur++) {
4701 if (strlen(*cur) == alpn_len &&
4702 memcmp(p, cur, alpn_len) == 0) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004703 ssl->alpn_chosen = *cur;
4704 break;
4705 }
4706 }
4707 }
4708
4709 /* can only happen on conf mismatch */
Gilles Peskine449bd832023-01-11 14:50:10 +01004710 if (alpn_len != 0 && ssl->alpn_chosen == NULL) {
4711 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4712 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004713
4714 p += alpn_len;
4715 }
4716#endif /* MBEDTLS_SSL_ALPN */
4717
4718 /*
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004719 * Forced fields from top-level ssl_context structure
4720 *
4721 * Most of them already set to the correct value by mbedtls_ssl_init() and
4722 * mbedtls_ssl_reset(), so we only need to set the remaining ones.
4723 */
4724 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
Glenn Strauss60bfe602022-03-14 19:04:24 -04004725 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004726
Hanno Becker361b10d2019-08-30 10:42:49 +01004727 /* Adjust pointers for header fields of outgoing records to
4728 * the given transform, accounting for explicit IV and CID. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004729 mbedtls_ssl_update_out_pointers(ssl, ssl->transform);
Hanno Becker361b10d2019-08-30 10:42:49 +01004730
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004731#if defined(MBEDTLS_SSL_PROTO_DTLS)
4732 ssl->in_epoch = 1;
4733#endif
4734
4735 /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated,
4736 * which we don't want - otherwise we'd end up freeing the wrong transform
Hanno Beckerce5f5fd2020-02-05 10:47:44 +00004737 * by calling mbedtls_ssl_handshake_wrapup_free_hs_transform()
4738 * inappropriately. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004739 if (ssl->handshake != NULL) {
4740 mbedtls_ssl_handshake_free(ssl);
4741 mbedtls_free(ssl->handshake);
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004742 ssl->handshake = NULL;
4743 }
4744
4745 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004746 * Done - should have consumed entire buffer
4747 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004748 if (p != end) {
4749 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4750 }
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004751
Gilles Peskine449bd832023-01-11 14:50:10 +01004752 return 0;
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004753}
4754
4755/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02004756 * Deserialize context: public wrapper for error cleaning
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004757 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004758int mbedtls_ssl_context_load(mbedtls_ssl_context *context,
4759 const unsigned char *buf,
4760 size_t len)
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004761{
Gilles Peskine449bd832023-01-11 14:50:10 +01004762 int ret = ssl_context_load(context, buf, len);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004763
Gilles Peskine449bd832023-01-11 14:50:10 +01004764 if (ret != 0) {
4765 mbedtls_ssl_free(context);
4766 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004767
Gilles Peskine449bd832023-01-11 14:50:10 +01004768 return ret;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004769}
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02004770#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004771
4772/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004773 * Free an SSL context
4774 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004775void mbedtls_ssl_free(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00004776{
Gilles Peskine449bd832023-01-11 14:50:10 +01004777 if (ssl == NULL) {
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004778 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01004779 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004780
Gilles Peskine449bd832023-01-11 14:50:10 +01004781 MBEDTLS_SSL_DEBUG_MSG(2, ("=> free"));
Paul Bakker5121ce52009-01-03 21:22:43 +00004782
Gilles Peskine449bd832023-01-11 14:50:10 +01004783 if (ssl->out_buf != NULL) {
sander-visserb8aa2072020-05-06 22:05:13 +02004784#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4785 size_t out_buf_len = ssl->out_buf_len;
4786#else
4787 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
4788#endif
4789
Gilles Peskine449bd832023-01-11 14:50:10 +01004790 mbedtls_platform_zeroize(ssl->out_buf, out_buf_len);
4791 mbedtls_free(ssl->out_buf);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004792 ssl->out_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004793 }
4794
Gilles Peskine449bd832023-01-11 14:50:10 +01004795 if (ssl->in_buf != NULL) {
sander-visserb8aa2072020-05-06 22:05:13 +02004796#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4797 size_t in_buf_len = ssl->in_buf_len;
4798#else
4799 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
4800#endif
4801
Gilles Peskine449bd832023-01-11 14:50:10 +01004802 mbedtls_platform_zeroize(ssl->in_buf, in_buf_len);
4803 mbedtls_free(ssl->in_buf);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004804 ssl->in_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004805 }
4806
Gilles Peskine449bd832023-01-11 14:50:10 +01004807 if (ssl->transform) {
4808 mbedtls_ssl_transform_free(ssl->transform);
4809 mbedtls_free(ssl->transform);
Paul Bakker48916f92012-09-16 19:57:18 +00004810 }
4811
Gilles Peskine449bd832023-01-11 14:50:10 +01004812 if (ssl->handshake) {
4813 mbedtls_ssl_handshake_free(ssl);
4814 mbedtls_free(ssl->handshake);
Jerry Yu2e199812022-12-01 18:57:19 +08004815
4816#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01004817 mbedtls_ssl_transform_free(ssl->transform_negotiate);
4818 mbedtls_free(ssl->transform_negotiate);
Jerry Yu2e199812022-12-01 18:57:19 +08004819#endif
4820
Gilles Peskine449bd832023-01-11 14:50:10 +01004821 mbedtls_ssl_session_free(ssl->session_negotiate);
4822 mbedtls_free(ssl->session_negotiate);
Paul Bakker48916f92012-09-16 19:57:18 +00004823 }
4824
Ronald Cron6f135e12021-12-08 16:57:54 +01004825#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01004826 mbedtls_ssl_transform_free(ssl->transform_application);
4827 mbedtls_free(ssl->transform_application);
Ronald Cron6f135e12021-12-08 16:57:54 +01004828#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker3aa186f2021-08-10 09:24:19 +01004829
Gilles Peskine449bd832023-01-11 14:50:10 +01004830 if (ssl->session) {
4831 mbedtls_ssl_session_free(ssl->session);
4832 mbedtls_free(ssl->session);
Paul Bakkerc0463502013-02-14 11:19:38 +01004833 }
4834
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02004835#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004836 if (ssl->hostname != NULL) {
4837 mbedtls_platform_zeroize(ssl->hostname, strlen(ssl->hostname));
4838 mbedtls_free(ssl->hostname);
Paul Bakker5121ce52009-01-03 21:22:43 +00004839 }
Paul Bakker0be444a2013-08-27 21:55:01 +02004840#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004841
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02004842#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004843 mbedtls_free(ssl->cli_id);
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02004844#endif
4845
Gilles Peskine449bd832023-01-11 14:50:10 +01004846 MBEDTLS_SSL_DEBUG_MSG(2, ("<= free"));
Paul Bakker2da561c2009-02-05 18:00:28 +00004847
Paul Bakker86f04f42013-02-14 11:20:09 +01004848 /* Actually clear after last debug message */
Gilles Peskine449bd832023-01-11 14:50:10 +01004849 mbedtls_platform_zeroize(ssl, sizeof(mbedtls_ssl_context));
Paul Bakker5121ce52009-01-03 21:22:43 +00004850}
4851
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004852/*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08004853 * Initialize mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004854 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004855void mbedtls_ssl_config_init(mbedtls_ssl_config *conf)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004856{
Gilles Peskine449bd832023-01-11 14:50:10 +01004857 memset(conf, 0, sizeof(mbedtls_ssl_config));
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004858}
4859
Gilles Peskineae270bf2021-06-02 00:05:29 +02004860/* The selection should be the same as mbedtls_x509_crt_profile_default in
4861 * x509_crt.c, plus Montgomery curves for ECDHE. Here, the order matters:
Gilles Peskineb1940a72021-06-02 15:18:12 +02004862 * curves with a lower resource usage come first.
Gilles Peskineae270bf2021-06-02 00:05:29 +02004863 * See the documentation of mbedtls_ssl_conf_curves() for what we promise
Gilles Peskineb1940a72021-06-02 15:18:12 +02004864 * about this list.
4865 */
Brett Warrene0edc842021-08-17 09:53:13 +01004866static uint16_t ssl_preset_default_groups[] = {
Gilles Peskineae270bf2021-06-02 00:05:29 +02004867#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004868 MBEDTLS_SSL_IANA_TLS_GROUP_X25519,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004869#endif
4870#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004871 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004872#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02004873#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004874 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004875#endif
4876#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004877 MBEDTLS_SSL_IANA_TLS_GROUP_X448,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004878#endif
4879#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004880 MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004881#endif
Gilles Peskineae270bf2021-06-02 00:05:29 +02004882#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004883 MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004884#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02004885#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004886 MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004887#endif
4888#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004889 MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004890#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004891 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Gilles Peskineae270bf2021-06-02 00:05:29 +02004892};
Gilles Peskineae270bf2021-06-02 00:05:29 +02004893
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004894static int ssl_preset_suiteb_ciphersuites[] = {
4895 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
4896 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
4897 0
4898};
4899
Ronald Crone68ab4f2022-10-05 12:46:29 +02004900#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004901
Jerry Yu909df7b2022-01-22 11:56:27 +08004902/* NOTICE:
Jerry Yu0b994b82022-01-25 17:22:12 +08004903 * For ssl_preset_*_sig_algs and ssl_tls12_preset_*_sig_algs, the following
Jerry Yu370e1462022-01-25 10:36:53 +08004904 * rules SHOULD be upheld.
4905 * - No duplicate entries.
4906 * - But if there is a good reason, do not change the order of the algorithms.
Jerry Yu09a99fc2022-07-28 14:22:17 +08004907 * - ssl_tls12_preset* is for TLS 1.2 use only.
Jerry Yu370e1462022-01-25 10:36:53 +08004908 * - ssl_preset_* is for TLS 1.3 only or hybrid TLS 1.3/1.2 handshakes.
Jerry Yu1a8b4812022-01-20 17:56:50 +08004909 */
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004910static uint16_t ssl_preset_default_sig_algs[] = {
Jerry Yu1a8b4812022-01-20 17:56:50 +08004911
Andrzej Kurek25f27152022-08-17 16:09:31 -04004912#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
Jerry Yued5e9f42022-01-26 11:21:34 +08004913 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
4914 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004915#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA &&
Jerry Yued5e9f42022-01-26 11:21:34 +08004916 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004917
Andrzej Kurek25f27152022-08-17 16:09:31 -04004918#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
Jerry Yu909df7b2022-01-22 11:56:27 +08004919 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
4920 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004921#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu909df7b2022-01-22 11:56:27 +08004922 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
4923
Andrzej Kurek25f27152022-08-17 16:09:31 -04004924#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
Jerry Yued5e9f42022-01-26 11:21:34 +08004925 defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
4926 MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004927#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yued5e9f42022-01-26 11:21:34 +08004928 MBEDTLS_ECP_DP_SECP521R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004929
Gilles Peskine449bd832023-01-11 14:50:10 +01004930#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && \
4931 defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu9bb3ee42022-06-23 10:16:33 +08004932 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
Gilles Peskine449bd832023-01-11 14:50:10 +01004933#endif \
4934 /* 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 +08004935
Gilles Peskine449bd832023-01-11 14:50:10 +01004936#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && \
4937 defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu9bb3ee42022-06-23 10:16:33 +08004938 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
Gilles Peskine449bd832023-01-11 14:50:10 +01004939#endif \
4940 /* 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 +08004941
Gilles Peskine449bd832023-01-11 14:50:10 +01004942#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && \
4943 defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu9bb3ee42022-06-23 10:16:33 +08004944 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Gilles Peskine449bd832023-01-11 14:50:10 +01004945#endif \
4946 /* 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 +08004947
Andrzej Kurek25f27152022-08-17 16:09:31 -04004948#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 +08004949 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512,
4950#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA512_C */
4951
Andrzej Kurek25f27152022-08-17 16:09:31 -04004952#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 +08004953 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384,
4954#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA384_C */
4955
Andrzej Kurek25f27152022-08-17 16:09:31 -04004956#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 +08004957 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
4958#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA256_C */
4959
Gabor Mezei15b95a62022-05-09 16:37:58 +02004960 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08004961};
4962
4963/* NOTICE: see above */
4964#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4965static uint16_t ssl_tls12_preset_default_sig_algs[] = {
Andrzej Kurek25f27152022-08-17 16:09:31 -04004966#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004967#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004968 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA512),
Jerry Yu713013f2022-01-17 18:16:35 +08004969#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004970#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4971 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
4972#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004973#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004974 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA512),
Gabor Mezeic1051b62022-05-10 13:13:58 +02004975#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004976#endif /* MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04004977#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004978#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004979 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08004980#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004981#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4982 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
4983#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004984#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004985 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384),
Gabor Mezeic1051b62022-05-10 13:13:58 +02004986#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004987#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04004988#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004989#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004990 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08004991#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004992#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4993 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
4994#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004995#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004996 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256),
Gabor Mezeic1051b62022-05-10 13:13:58 +02004997#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004998#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Gabor Mezei15b95a62022-05-09 16:37:58 +02004999 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08005000};
5001#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5002/* NOTICE: see above */
5003static uint16_t ssl_preset_suiteb_sig_algs[] = {
5004
Andrzej Kurek25f27152022-08-17 16:09:31 -04005005#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 +08005006 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
5007 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005008#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu909df7b2022-01-22 11:56:27 +08005009 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
5010
Andrzej Kurek25f27152022-08-17 16:09:31 -04005011#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 +08005012 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
5013 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005014#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu53037892022-01-25 11:02:06 +08005015 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08005016
Gilles Peskine449bd832023-01-11 14:50:10 +01005017#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && \
5018 defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu909df7b2022-01-22 11:56:27 +08005019 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Gilles Peskine449bd832023-01-11 14:50:10 +01005020#endif \
5021 /* 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 +08005022
Andrzej Kurek25f27152022-08-17 16:09:31 -04005023#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 +08005024 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005025#endif /* MBEDTLS_RSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yu53037892022-01-25 11:02:06 +08005026
Gabor Mezei15b95a62022-05-09 16:37:58 +02005027 MBEDTLS_TLS_SIG_NONE
Jerry Yu713013f2022-01-17 18:16:35 +08005028};
Jerry Yu6106fdc2022-01-12 16:36:14 +08005029
Jerry Yu909df7b2022-01-22 11:56:27 +08005030/* NOTICE: see above */
5031#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5032static uint16_t ssl_tls12_preset_suiteb_sig_algs[] = {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005033#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02005034#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005035 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256),
Jerry Yu713013f2022-01-17 18:16:35 +08005036#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02005037#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005038 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256),
Gabor Mezeic1051b62022-05-10 13:13:58 +02005039#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005040#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04005041#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02005042#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005043 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384),
Jerry Yu18c833e2022-01-25 10:55:47 +08005044#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02005045#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005046 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384),
Gabor Mezeic1051b62022-05-10 13:13:58 +02005047#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005048#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Gabor Mezei15b95a62022-05-09 16:37:58 +02005049 MBEDTLS_TLS_SIG_NONE
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01005050};
Jerry Yu909df7b2022-01-22 11:56:27 +08005051#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5052
Ronald Crone68ab4f2022-10-05 12:46:29 +02005053#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005054
Brett Warrene0edc842021-08-17 09:53:13 +01005055static uint16_t ssl_preset_suiteb_groups[] = {
Jaeden Amerod4311042019-06-03 08:27:16 +01005056#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01005057 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01005058#endif
5059#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01005060 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01005061#endif
Brett Warrene0edc842021-08-17 09:53:13 +01005062 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005063};
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005064
Ronald Crone68ab4f2022-10-05 12:46:29 +02005065#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08005066/* Function for checking `ssl_preset_*_sig_algs` and `ssl_tls12_preset_*_sig_algs`
Jerry Yu370e1462022-01-25 10:36:53 +08005067 * to make sure there are no duplicated signature algorithm entries. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005068MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01005069static int ssl_check_no_sig_alg_duplication(uint16_t *sig_algs)
Jerry Yu1a8b4812022-01-20 17:56:50 +08005070{
5071 size_t i, j;
5072 int ret = 0;
Jerry Yu909df7b2022-01-22 11:56:27 +08005073
Gilles Peskine449bd832023-01-11 14:50:10 +01005074 for (i = 0; sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++) {
5075 for (j = 0; j < i; j++) {
5076 if (sig_algs[i] != sig_algs[j]) {
Jerry Yuf377d642022-01-25 10:43:59 +08005077 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01005078 }
5079 mbedtls_printf(" entry(%04x,%" MBEDTLS_PRINTF_SIZET
5080 ") is duplicated at %" MBEDTLS_PRINTF_SIZET "\n",
5081 sig_algs[i], j, i);
Jerry Yuf377d642022-01-25 10:43:59 +08005082 ret = -1;
Jerry Yu1a8b4812022-01-20 17:56:50 +08005083 }
5084 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005085 return ret;
Jerry Yu1a8b4812022-01-20 17:56:50 +08005086}
5087
Ronald Crone68ab4f2022-10-05 12:46:29 +02005088#endif /* MBEDTLS_DEBUG_C && MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yu1a8b4812022-01-20 17:56:50 +08005089
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005090/*
Tillmann Karras588ad502015-09-25 04:27:22 +02005091 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005092 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005093int mbedtls_ssl_config_defaults(mbedtls_ssl_config *conf,
5094 int endpoint, int transport, int preset)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005095{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005096#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Janos Follath865b3eb2019-12-16 11:46:15 +00005097 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005098#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005099
Ronald Crone68ab4f2022-10-05 12:46:29 +02005100#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01005101 if (ssl_check_no_sig_alg_duplication(ssl_preset_suiteb_sig_algs)) {
5102 mbedtls_printf("ssl_preset_suiteb_sig_algs has duplicated entries\n");
5103 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu1a8b4812022-01-20 17:56:50 +08005104 }
5105
Gilles Peskine449bd832023-01-11 14:50:10 +01005106 if (ssl_check_no_sig_alg_duplication(ssl_preset_default_sig_algs)) {
5107 mbedtls_printf("ssl_preset_default_sig_algs has duplicated entries\n");
5108 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu1a8b4812022-01-20 17:56:50 +08005109 }
Jerry Yu909df7b2022-01-22 11:56:27 +08005110
5111#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01005112 if (ssl_check_no_sig_alg_duplication(ssl_tls12_preset_suiteb_sig_algs)) {
5113 mbedtls_printf("ssl_tls12_preset_suiteb_sig_algs has duplicated entries\n");
5114 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu909df7b2022-01-22 11:56:27 +08005115 }
5116
Gilles Peskine449bd832023-01-11 14:50:10 +01005117 if (ssl_check_no_sig_alg_duplication(ssl_tls12_preset_default_sig_algs)) {
5118 mbedtls_printf("ssl_tls12_preset_default_sig_algs has duplicated entries\n");
5119 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu909df7b2022-01-22 11:56:27 +08005120 }
5121#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Ronald Crone68ab4f2022-10-05 12:46:29 +02005122#endif /* MBEDTLS_DEBUG_C && MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yu1a8b4812022-01-20 17:56:50 +08005123
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02005124 /* Use the functions here so that they are covered in tests,
5125 * but otherwise access member directly for efficiency */
Gilles Peskine449bd832023-01-11 14:50:10 +01005126 mbedtls_ssl_conf_endpoint(conf, endpoint);
5127 mbedtls_ssl_conf_transport(conf, transport);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005128
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005129 /*
5130 * Things that are common to all presets
5131 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02005132#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005133 if (endpoint == MBEDTLS_SSL_IS_CLIENT) {
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02005134 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
5135#if defined(MBEDTLS_SSL_SESSION_TICKETS)
5136 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
5137#endif
5138 }
5139#endif
5140
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005141#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
5142 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
5143#endif
5144
5145#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
5146 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
5147#endif
5148
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02005149#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005150 conf->f_cookie_write = ssl_cookie_write_dummy;
5151 conf->f_cookie_check = ssl_cookie_check_dummy;
5152#endif
5153
5154#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
5155 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
5156#endif
5157
Janos Follath088ce432017-04-10 12:42:31 +01005158#if defined(MBEDTLS_SSL_SRV_C)
5159 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
TRodziewicz3946f792021-06-14 12:11:18 +02005160 conf->respect_cli_pref = MBEDTLS_SSL_SRV_CIPHERSUITE_ORDER_SERVER;
Janos Follath088ce432017-04-10 12:42:31 +01005161#endif
5162
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005163#if defined(MBEDTLS_SSL_PROTO_DTLS)
5164 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
5165 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
5166#endif
5167
5168#if defined(MBEDTLS_SSL_RENEGOTIATION)
5169 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005170 memset(conf->renego_period, 0x00, 2);
5171 memset(conf->renego_period + 2, 0xFF, 6);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005172#endif
5173
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005174#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005175 if (endpoint == MBEDTLS_SSL_IS_SERVER) {
Hanno Beckere2defad2021-07-24 05:59:17 +01005176 const unsigned char dhm_p[] =
5177 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
5178 const unsigned char dhm_g[] =
5179 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
Hanno Becker00d0a682017-10-04 13:14:29 +01005180
Gilles Peskine449bd832023-01-11 14:50:10 +01005181 if ((ret = mbedtls_ssl_conf_dh_param_bin(conf,
5182 dhm_p, sizeof(dhm_p),
5183 dhm_g, sizeof(dhm_g))) != 0) {
5184 return ret;
Hanno Beckere2defad2021-07-24 05:59:17 +01005185 }
5186 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02005187#endif
5188
Ronald Cron6f135e12021-12-08 16:57:54 +01005189#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu6ee56aa2022-12-06 17:47:22 +08005190
5191#if defined(MBEDTLS_SSL_EARLY_DATA)
Gilles Peskine449bd832023-01-11 14:50:10 +01005192 mbedtls_ssl_tls13_conf_early_data(conf, MBEDTLS_SSL_EARLY_DATA_DISABLED);
Jerry Yu6ee56aa2022-12-06 17:47:22 +08005193#if defined(MBEDTLS_SSL_SRV_C)
5194 mbedtls_ssl_tls13_conf_max_early_data_size(
Gilles Peskine449bd832023-01-11 14:50:10 +01005195 conf, MBEDTLS_SSL_MAX_EARLY_DATA_SIZE);
Jerry Yu6ee56aa2022-12-06 17:47:22 +08005196#endif
5197#endif /* MBEDTLS_SSL_EARLY_DATA */
5198
Jerry Yud0766ec2022-09-22 10:46:57 +08005199#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08005200 mbedtls_ssl_conf_new_session_tickets(
Gilles Peskine449bd832023-01-11 14:50:10 +01005201 conf, MBEDTLS_SSL_TLS1_3_DEFAULT_NEW_SESSION_TICKETS);
Jerry Yu1ad7ace2022-08-09 13:28:39 +08005202#endif
Hanno Becker71f1ed62021-07-24 06:01:47 +01005203 /*
5204 * Allow all TLS 1.3 key exchange modes by default.
5205 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00005206 conf->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Ronald Cron6f135e12021-12-08 16:57:54 +01005207#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01005208
Gilles Peskine449bd832023-01-11 14:50:10 +01005209 if (transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Glenn Strauss2dfcea22022-03-14 17:26:42 -04005210#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
XiaokangQian4d3a6042022-04-21 13:46:17 +00005211 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian060d8672022-04-21 09:24:56 +00005212 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian4d3a6042022-04-21 13:46:17 +00005213#else
Gilles Peskine449bd832023-01-11 14:50:10 +01005214 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQian060d8672022-04-21 09:24:56 +00005215#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005216 } else {
XiaokangQian4d3a6042022-04-21 13:46:17 +00005217#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01005218 if (endpoint == MBEDTLS_SSL_IS_CLIENT) {
XiaokangQian4d3a6042022-04-21 13:46:17 +00005219 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5220 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
Gilles Peskine449bd832023-01-11 14:50:10 +01005221 } else {
5222 /* Hybrid TLS 1.2 / 1.3 is not supported on server side yet */
XiaokangQian4d3a6042022-04-21 13:46:17 +00005223 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5224 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5225 }
5226#elif defined(MBEDTLS_SSL_PROTO_TLS1_3)
5227 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
5228 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
5229#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
5230 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5231 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5232#else
Gilles Peskine449bd832023-01-11 14:50:10 +01005233 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQian4d3a6042022-04-21 13:46:17 +00005234#endif
5235 }
Glenn Strauss2dfcea22022-03-14 17:26:42 -04005236
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005237 /*
5238 * Preset-specific defaults
5239 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005240 switch (preset) {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005241 /*
5242 * NSA Suite B
5243 */
5244 case MBEDTLS_SSL_PRESET_SUITEB:
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005245
Hanno Beckerd60b6c62021-04-29 12:04:11 +01005246 conf->ciphersuite_list = ssl_preset_suiteb_ciphersuites;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005247
5248#if defined(MBEDTLS_X509_CRT_PARSE_C)
5249 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005250#endif
5251
Ronald Crone68ab4f2022-10-05 12:46:29 +02005252#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08005253#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01005254 if (mbedtls_ssl_conf_is_tls12_only(conf)) {
Jerry Yu909df7b2022-01-22 11:56:27 +08005255 conf->sig_algs = ssl_tls12_preset_suiteb_sig_algs;
Gilles Peskine449bd832023-01-11 14:50:10 +01005256 } else
Jerry Yu909df7b2022-01-22 11:56:27 +08005257#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005258 conf->sig_algs = ssl_preset_suiteb_sig_algs;
Ronald Crone68ab4f2022-10-05 12:46:29 +02005259#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005260
Brett Warrene0edc842021-08-17 09:53:13 +01005261#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
5262 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005263#endif
Brett Warrene0edc842021-08-17 09:53:13 +01005264 conf->group_list = ssl_preset_suiteb_groups;
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02005265 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005266
5267 /*
5268 * Default
5269 */
5270 default:
Ronald Cronf6606552022-03-15 11:23:25 +01005271
Hanno Beckerd60b6c62021-04-29 12:04:11 +01005272 conf->ciphersuite_list = mbedtls_ssl_list_ciphersuites();
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005273
5274#if defined(MBEDTLS_X509_CRT_PARSE_C)
5275 conf->cert_profile = &mbedtls_x509_crt_profile_default;
5276#endif
5277
Ronald Crone68ab4f2022-10-05 12:46:29 +02005278#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08005279#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01005280 if (mbedtls_ssl_conf_is_tls12_only(conf)) {
Jerry Yu909df7b2022-01-22 11:56:27 +08005281 conf->sig_algs = ssl_tls12_preset_default_sig_algs;
Gilles Peskine449bd832023-01-11 14:50:10 +01005282 } else
Jerry Yu909df7b2022-01-22 11:56:27 +08005283#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005284 conf->sig_algs = ssl_preset_default_sig_algs;
Ronald Crone68ab4f2022-10-05 12:46:29 +02005285#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005286
Brett Warrene0edc842021-08-17 09:53:13 +01005287#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
5288 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005289#endif
Brett Warrene0edc842021-08-17 09:53:13 +01005290 conf->group_list = ssl_preset_default_groups;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005291
5292#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
5293 conf->dhm_min_bitlen = 1024;
5294#endif
5295 }
5296
Gilles Peskine449bd832023-01-11 14:50:10 +01005297 return 0;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005298}
5299
5300/*
5301 * Free mbedtls_ssl_config
5302 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005303void mbedtls_ssl_config_free(mbedtls_ssl_config *conf)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005304{
5305#if defined(MBEDTLS_DHM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005306 mbedtls_mpi_free(&conf->dhm_P);
5307 mbedtls_mpi_free(&conf->dhm_G);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005308#endif
5309
Ronald Cron73fe8df2022-10-05 14:31:43 +02005310#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02005311#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01005312 if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) {
Neil Armstrong501c9322022-05-03 09:35:09 +02005313 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
5314 }
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005315#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01005316 if (conf->psk != NULL) {
5317 mbedtls_platform_zeroize(conf->psk, conf->psk_len);
5318 mbedtls_free(conf->psk);
Azim Khan27e8a122018-03-21 14:24:11 +00005319 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005320 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09005321 }
5322
Gilles Peskine449bd832023-01-11 14:50:10 +01005323 if (conf->psk_identity != NULL) {
5324 mbedtls_platform_zeroize(conf->psk_identity, conf->psk_identity_len);
5325 mbedtls_free(conf->psk_identity);
Azim Khan27e8a122018-03-21 14:24:11 +00005326 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005327 conf->psk_identity_len = 0;
5328 }
Ronald Cron73fe8df2022-10-05 14:31:43 +02005329#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005330
5331#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005332 ssl_key_cert_free(conf->key_cert);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005333#endif
5334
Gilles Peskine449bd832023-01-11 14:50:10 +01005335 mbedtls_platform_zeroize(conf, sizeof(mbedtls_ssl_config));
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005336}
5337
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02005338#if defined(MBEDTLS_PK_C) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01005339 (defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C))
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005340/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005341 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005342 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005343unsigned char mbedtls_ssl_sig_from_pk(mbedtls_pk_context *pk)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005344{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005345#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005346 if (mbedtls_pk_can_do(pk, MBEDTLS_PK_RSA)) {
5347 return MBEDTLS_SSL_SIG_RSA;
5348 }
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005349#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005350#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005351 if (mbedtls_pk_can_do(pk, MBEDTLS_PK_ECDSA)) {
5352 return MBEDTLS_SSL_SIG_ECDSA;
5353 }
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005354#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005355 return MBEDTLS_SSL_SIG_ANON;
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005356}
5357
Gilles Peskine449bd832023-01-11 14:50:10 +01005358unsigned char mbedtls_ssl_sig_from_pk_alg(mbedtls_pk_type_t type)
Hanno Becker7e5437a2017-04-28 17:15:26 +01005359{
Gilles Peskine449bd832023-01-11 14:50:10 +01005360 switch (type) {
Hanno Becker7e5437a2017-04-28 17:15:26 +01005361 case MBEDTLS_PK_RSA:
Gilles Peskine449bd832023-01-11 14:50:10 +01005362 return MBEDTLS_SSL_SIG_RSA;
Hanno Becker7e5437a2017-04-28 17:15:26 +01005363 case MBEDTLS_PK_ECDSA:
5364 case MBEDTLS_PK_ECKEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01005365 return MBEDTLS_SSL_SIG_ECDSA;
Hanno Becker7e5437a2017-04-28 17:15:26 +01005366 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005367 return MBEDTLS_SSL_SIG_ANON;
Hanno Becker7e5437a2017-04-28 17:15:26 +01005368 }
5369}
5370
Gilles Peskine449bd832023-01-11 14:50:10 +01005371mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig(unsigned char sig)
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005372{
Gilles Peskine449bd832023-01-11 14:50:10 +01005373 switch (sig) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005374#if defined(MBEDTLS_RSA_C)
5375 case MBEDTLS_SSL_SIG_RSA:
Gilles Peskine449bd832023-01-11 14:50:10 +01005376 return MBEDTLS_PK_RSA;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005377#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005378#if defined(MBEDTLS_ECDSA_C)
5379 case MBEDTLS_SSL_SIG_ECDSA:
Gilles Peskine449bd832023-01-11 14:50:10 +01005380 return MBEDTLS_PK_ECDSA;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005381#endif
5382 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005383 return MBEDTLS_PK_NONE;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005384 }
5385}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02005386#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005387
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02005388/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005389 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02005390 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005391mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash(unsigned char hash)
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005392{
Gilles Peskine449bd832023-01-11 14:50:10 +01005393 switch (hash) {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005394#if defined(MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005395 case MBEDTLS_SSL_HASH_MD5:
Gilles Peskine449bd832023-01-11 14:50:10 +01005396 return MBEDTLS_MD_MD5;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005397#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005398#if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005399 case MBEDTLS_SSL_HASH_SHA1:
Gilles Peskine449bd832023-01-11 14:50:10 +01005400 return MBEDTLS_MD_SHA1;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005401#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005402#if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005403 case MBEDTLS_SSL_HASH_SHA224:
Gilles Peskine449bd832023-01-11 14:50:10 +01005404 return MBEDTLS_MD_SHA224;
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02005405#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005406#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005407 case MBEDTLS_SSL_HASH_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01005408 return MBEDTLS_MD_SHA256;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005409#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005410#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005411 case MBEDTLS_SSL_HASH_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01005412 return MBEDTLS_MD_SHA384;
Mateusz Starzyk3352a532021-04-06 14:28:22 +02005413#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005414#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005415 case MBEDTLS_SSL_HASH_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01005416 return MBEDTLS_MD_SHA512;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005417#endif
5418 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005419 return MBEDTLS_MD_NONE;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005420 }
5421}
5422
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005423/*
5424 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
5425 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005426unsigned char mbedtls_ssl_hash_from_md_alg(int md)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005427{
Gilles Peskine449bd832023-01-11 14:50:10 +01005428 switch (md) {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005429#if defined(MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005430 case MBEDTLS_MD_MD5:
Gilles Peskine449bd832023-01-11 14:50:10 +01005431 return MBEDTLS_SSL_HASH_MD5;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005432#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005433#if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005434 case MBEDTLS_MD_SHA1:
Gilles Peskine449bd832023-01-11 14:50:10 +01005435 return MBEDTLS_SSL_HASH_SHA1;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005436#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005437#if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005438 case MBEDTLS_MD_SHA224:
Gilles Peskine449bd832023-01-11 14:50:10 +01005439 return MBEDTLS_SSL_HASH_SHA224;
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02005440#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005441#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005442 case MBEDTLS_MD_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01005443 return MBEDTLS_SSL_HASH_SHA256;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005444#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005445#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005446 case MBEDTLS_MD_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01005447 return MBEDTLS_SSL_HASH_SHA384;
Mateusz Starzyk3352a532021-04-06 14:28:22 +02005448#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005449#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005450 case MBEDTLS_MD_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01005451 return MBEDTLS_SSL_HASH_SHA512;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005452#endif
5453 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005454 return MBEDTLS_SSL_HASH_NONE;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005455 }
5456}
5457
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005458/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005459 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005460 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005461 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005462int mbedtls_ssl_check_curve_tls_id(const mbedtls_ssl_context *ssl, uint16_t tls_id)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005463{
Gilles Peskine449bd832023-01-11 14:50:10 +01005464 const uint16_t *group_list = mbedtls_ssl_get_groups(ssl);
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005465
Gilles Peskine449bd832023-01-11 14:50:10 +01005466 if (group_list == NULL) {
5467 return -1;
Brett Warrene0edc842021-08-17 09:53:13 +01005468 }
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005469
Gilles Peskine449bd832023-01-11 14:50:10 +01005470 for (; *group_list != 0; group_list++) {
5471 if (*group_list == tls_id) {
5472 return 0;
5473 }
5474 }
5475
5476 return -1;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005477}
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005478
5479#if defined(MBEDTLS_ECP_C)
5480/*
5481 * Same as mbedtls_ssl_check_curve_tls_id() but with a mbedtls_ecp_group_id.
5482 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005483int mbedtls_ssl_check_curve(const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id)
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005484{
Gilles Peskine449bd832023-01-11 14:50:10 +01005485 uint16_t tls_id = mbedtls_ssl_get_tls_id_from_ecp_group_id(grp_id);
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005486
Gilles Peskine449bd832023-01-11 14:50:10 +01005487 if (tls_id == 0) {
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005488 return -1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005489 }
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005490
Gilles Peskine449bd832023-01-11 14:50:10 +01005491 return mbedtls_ssl_check_curve_tls_id(ssl, tls_id);
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005492}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02005493#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005494
Gilles Peskine449bd832023-01-11 14:50:10 +01005495#if defined(MBEDTLS_DEBUG_C)
Valerio Setti67419f02023-01-04 16:12:42 +01005496#define EC_NAME(_name_) _name_
5497#else
5498#define EC_NAME(_name_) NULL
5499#endif
5500
Valerio Setti18c9fed2022-12-30 17:44:24 +01005501static const struct {
5502 uint16_t tls_id;
5503 mbedtls_ecp_group_id ecp_group_id;
5504 psa_ecc_family_t psa_family;
5505 uint16_t bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01005506 const char *name;
Valerio Setti18c9fed2022-12-30 17:44:24 +01005507} tls_id_match_table[] =
5508{
5509#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_521)
Gilles Peskine449bd832023-01-11 14:50:10 +01005510 { 25, MBEDTLS_ECP_DP_SECP521R1, PSA_ECC_FAMILY_SECP_R1, 521, EC_NAME("secp521r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005511#endif
5512#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED) || defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512)
Gilles Peskine449bd832023-01-11 14:50:10 +01005513 { 28, MBEDTLS_ECP_DP_BP512R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 512, EC_NAME("brainpoolP512r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005514#endif
Valerio Setti67419f02023-01-04 16:12:42 +01005515#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_384)
Gilles Peskine449bd832023-01-11 14:50:10 +01005516 { 24, MBEDTLS_ECP_DP_SECP384R1, PSA_ECC_FAMILY_SECP_R1, 384, EC_NAME("secp384r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005517#endif
Valerio Setti67419f02023-01-04 16:12:42 +01005518#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED) || defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384)
Gilles Peskine449bd832023-01-11 14:50:10 +01005519 { 27, MBEDTLS_ECP_DP_BP384R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 384, EC_NAME("brainpoolP384r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005520#endif
Valerio Setti67419f02023-01-04 16:12:42 +01005521#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_256)
Gilles Peskine449bd832023-01-11 14:50:10 +01005522 { 23, MBEDTLS_ECP_DP_SECP256R1, PSA_ECC_FAMILY_SECP_R1, 256, EC_NAME("secp256r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005523#endif
5524#if defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED) || defined(PSA_WANT_ECC_SECP_K1_256)
Gilles Peskine449bd832023-01-11 14:50:10 +01005525 { 22, MBEDTLS_ECP_DP_SECP256K1, PSA_ECC_FAMILY_SECP_K1, 256, EC_NAME("secp256k1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005526#endif
Valerio Setti67419f02023-01-04 16:12:42 +01005527#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED) || defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256)
Gilles Peskine449bd832023-01-11 14:50:10 +01005528 { 26, MBEDTLS_ECP_DP_BP256R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 256, EC_NAME("brainpoolP256r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005529#endif
5530#if defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_224)
Gilles Peskine449bd832023-01-11 14:50:10 +01005531 { 21, MBEDTLS_ECP_DP_SECP224R1, PSA_ECC_FAMILY_SECP_R1, 224, EC_NAME("secp224r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005532#endif
5533#if defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED) || defined(PSA_WANT_ECC_SECP_K1_224)
Gilles Peskine449bd832023-01-11 14:50:10 +01005534 { 20, MBEDTLS_ECP_DP_SECP224K1, PSA_ECC_FAMILY_SECP_K1, 224, EC_NAME("secp224k1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005535#endif
5536#if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_192)
Gilles Peskine449bd832023-01-11 14:50:10 +01005537 { 19, MBEDTLS_ECP_DP_SECP192R1, PSA_ECC_FAMILY_SECP_R1, 192, EC_NAME("secp192r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005538#endif
5539#if defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED) || defined(PSA_WANT_ECC_SECP_K1_192)
Gilles Peskine449bd832023-01-11 14:50:10 +01005540 { 18, MBEDTLS_ECP_DP_SECP192K1, PSA_ECC_FAMILY_SECP_K1, 192, EC_NAME("secp192k1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005541#endif
5542#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED) || defined(PSA_WANT_ECC_MONTGOMERY_255)
Gilles Peskine449bd832023-01-11 14:50:10 +01005543 { 29, MBEDTLS_ECP_DP_CURVE25519, PSA_ECC_FAMILY_MONTGOMERY, 255, EC_NAME("x25519") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005544#endif
5545#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED) || defined(PSA_WANT_ECC_MONTGOMERY_448)
Gilles Peskine449bd832023-01-11 14:50:10 +01005546 { 30, MBEDTLS_ECP_DP_CURVE448, PSA_ECC_FAMILY_MONTGOMERY, 448, EC_NAME("x448") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005547#endif
5548 { 0, MBEDTLS_ECP_DP_NONE, 0, 0, NULL },
5549};
5550
Gilles Peskine449bd832023-01-11 14:50:10 +01005551int mbedtls_ssl_get_psa_curve_info_from_tls_id(uint16_t tls_id,
5552 psa_ecc_family_t *family,
5553 size_t *bits)
Valerio Setti18c9fed2022-12-30 17:44:24 +01005554{
Gilles Peskine449bd832023-01-11 14:50:10 +01005555 for (int i = 0; tls_id_match_table[i].tls_id != 0; i++) {
5556 if (tls_id_match_table[i].tls_id == tls_id) {
5557 if (family != NULL) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01005558 *family = tls_id_match_table[i].psa_family;
Gilles Peskine449bd832023-01-11 14:50:10 +01005559 }
5560 if (bits != NULL) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01005561 *bits = tls_id_match_table[i].bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01005562 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01005563 return PSA_SUCCESS;
5564 }
5565 }
5566
5567 return PSA_ERROR_NOT_SUPPORTED;
5568}
5569
Gilles Peskine449bd832023-01-11 14:50:10 +01005570mbedtls_ecp_group_id mbedtls_ssl_get_ecp_group_id_from_tls_id(uint16_t tls_id)
Valerio Setti18c9fed2022-12-30 17:44:24 +01005571{
Gilles Peskine449bd832023-01-11 14:50:10 +01005572 for (int i = 0; tls_id_match_table[i].tls_id != 0; i++) {
5573 if (tls_id_match_table[i].tls_id == tls_id) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01005574 return tls_id_match_table[i].ecp_group_id;
Gilles Peskine449bd832023-01-11 14:50:10 +01005575 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01005576 }
5577
5578 return MBEDTLS_ECP_DP_NONE;
5579}
5580
Gilles Peskine449bd832023-01-11 14:50:10 +01005581uint16_t mbedtls_ssl_get_tls_id_from_ecp_group_id(mbedtls_ecp_group_id grp_id)
Valerio Setti18c9fed2022-12-30 17:44:24 +01005582{
Gilles Peskine449bd832023-01-11 14:50:10 +01005583 for (int i = 0; tls_id_match_table[i].ecp_group_id != MBEDTLS_ECP_DP_NONE;
5584 i++) {
5585 if (tls_id_match_table[i].ecp_group_id == grp_id) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01005586 return tls_id_match_table[i].tls_id;
Gilles Peskine449bd832023-01-11 14:50:10 +01005587 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01005588 }
5589
5590 return 0;
5591}
5592
Valerio Setti67419f02023-01-04 16:12:42 +01005593#if defined(MBEDTLS_DEBUG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005594const char *mbedtls_ssl_get_curve_name_from_tls_id(uint16_t tls_id)
Valerio Setti18c9fed2022-12-30 17:44:24 +01005595{
Gilles Peskine449bd832023-01-11 14:50:10 +01005596 for (int i = 0; tls_id_match_table[i].tls_id != 0; i++) {
5597 if (tls_id_match_table[i].tls_id == tls_id) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01005598 return tls_id_match_table[i].name;
Gilles Peskine449bd832023-01-11 14:50:10 +01005599 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01005600 }
5601
5602 return NULL;
5603}
Valerio Setti67419f02023-01-04 16:12:42 +01005604#endif
Valerio Setti18c9fed2022-12-30 17:44:24 +01005605
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005606#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005607int mbedtls_ssl_check_cert_usage(const mbedtls_x509_crt *cert,
5608 const mbedtls_ssl_ciphersuite_t *ciphersuite,
5609 int cert_endpoint,
5610 uint32_t *flags)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005611{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005612 int ret = 0;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005613 int usage = 0;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005614 const char *ext_oid;
5615 size_t ext_len;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005616
Gilles Peskine449bd832023-01-11 14:50:10 +01005617 if (cert_endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005618 /* Server part of the key exchange */
Gilles Peskine449bd832023-01-11 14:50:10 +01005619 switch (ciphersuite->key_exchange) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005620 case MBEDTLS_KEY_EXCHANGE_RSA:
5621 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005622 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005623 break;
5624
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005625 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
5626 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
5627 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
5628 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005629 break;
5630
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005631 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
5632 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005633 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005634 break;
5635
5636 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005637 case MBEDTLS_KEY_EXCHANGE_NONE:
5638 case MBEDTLS_KEY_EXCHANGE_PSK:
5639 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
5640 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02005641 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005642 usage = 0;
5643 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005644 } else {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005645 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
5646 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005647 }
5648
Gilles Peskine449bd832023-01-11 14:50:10 +01005649 if (mbedtls_x509_crt_check_key_usage(cert, usage) != 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005650 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005651 ret = -1;
5652 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005653
Gilles Peskine449bd832023-01-11 14:50:10 +01005654 if (cert_endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005655 ext_oid = MBEDTLS_OID_SERVER_AUTH;
Gilles Peskine449bd832023-01-11 14:50:10 +01005656 ext_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_SERVER_AUTH);
5657 } else {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005658 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
Gilles Peskine449bd832023-01-11 14:50:10 +01005659 ext_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_CLIENT_AUTH);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005660 }
5661
Gilles Peskine449bd832023-01-11 14:50:10 +01005662 if (mbedtls_x509_crt_check_extended_key_usage(cert, ext_oid, ext_len) != 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005663 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005664 ret = -1;
5665 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005666
Gilles Peskine449bd832023-01-11 14:50:10 +01005667 return ret;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005668}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005669#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02005670
Jerry Yu148165c2021-09-24 23:20:59 +08005671#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01005672int mbedtls_ssl_get_handshake_transcript(mbedtls_ssl_context *ssl,
5673 const mbedtls_md_type_t md,
5674 unsigned char *dst,
5675 size_t dst_len,
5676 size_t *olen)
Jerry Yu148165c2021-09-24 23:20:59 +08005677{
Ronald Cronf6893e12022-01-07 22:09:01 +01005678 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5679 psa_hash_operation_t *hash_operation_to_clone;
5680 psa_hash_operation_t hash_operation = psa_hash_operation_init();
5681
Jerry Yu148165c2021-09-24 23:20:59 +08005682 *olen = 0;
Ronald Cronf6893e12022-01-07 22:09:01 +01005683
Gilles Peskine449bd832023-01-11 14:50:10 +01005684 switch (md) {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005685#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01005686 case MBEDTLS_MD_SHA384:
5687 hash_operation_to_clone = &ssl->handshake->fin_sha384_psa;
5688 break;
Ronald Cronf6893e12022-01-07 22:09:01 +01005689#endif
5690
Andrzej Kurek25f27152022-08-17 16:09:31 -04005691#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01005692 case MBEDTLS_MD_SHA256:
5693 hash_operation_to_clone = &ssl->handshake->fin_sha256_psa;
5694 break;
Ronald Cronf6893e12022-01-07 22:09:01 +01005695#endif
5696
Gilles Peskine449bd832023-01-11 14:50:10 +01005697 default:
5698 goto exit;
5699 }
5700
5701 status = psa_hash_clone(hash_operation_to_clone, &hash_operation);
5702 if (status != PSA_SUCCESS) {
Ronald Cronf6893e12022-01-07 22:09:01 +01005703 goto exit;
5704 }
5705
Gilles Peskine449bd832023-01-11 14:50:10 +01005706 status = psa_hash_finish(&hash_operation, dst, dst_len, olen);
5707 if (status != PSA_SUCCESS) {
Ronald Cronf6893e12022-01-07 22:09:01 +01005708 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005709 }
Ronald Cronf6893e12022-01-07 22:09:01 +01005710
5711exit:
Andrzej Kurekeabeb302022-10-17 07:52:51 -04005712#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
5713 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
5714 (void) ssl;
5715#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005716 return psa_ssl_status_to_mbedtls(status);
Jerry Yu148165c2021-09-24 23:20:59 +08005717}
5718#else /* MBEDTLS_USE_PSA_CRYPTO */
5719
Andrzej Kurek25f27152022-08-17 16:09:31 -04005720#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005721MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01005722static int ssl_get_handshake_transcript_sha384(mbedtls_ssl_context *ssl,
5723 unsigned char *dst,
5724 size_t dst_len,
5725 size_t *olen)
Jerry Yu24c0ec32021-09-09 14:21:07 +08005726{
Jerry Yu24c0ec32021-09-09 14:21:07 +08005727 int ret;
5728 mbedtls_sha512_context sha512;
5729
Gilles Peskine449bd832023-01-11 14:50:10 +01005730 if (dst_len < 48) {
5731 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
5732 }
Jerry Yu24c0ec32021-09-09 14:21:07 +08005733
Gilles Peskine449bd832023-01-11 14:50:10 +01005734 mbedtls_sha512_init(&sha512);
5735 mbedtls_sha512_clone(&sha512, &ssl->handshake->fin_sha384);
Jerry Yu24c0ec32021-09-09 14:21:07 +08005736
Gilles Peskine449bd832023-01-11 14:50:10 +01005737 if ((ret = mbedtls_sha512_finish(&sha512, dst)) != 0) {
5738 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_sha512_finish", ret);
Jerry Yu24c0ec32021-09-09 14:21:07 +08005739 goto exit;
5740 }
5741
5742 *olen = 48;
5743
5744exit:
5745
Gilles Peskine449bd832023-01-11 14:50:10 +01005746 mbedtls_sha512_free(&sha512);
5747 return ret;
Jerry Yu24c0ec32021-09-09 14:21:07 +08005748}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005749#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005750
Andrzej Kurek25f27152022-08-17 16:09:31 -04005751#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005752MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01005753static int ssl_get_handshake_transcript_sha256(mbedtls_ssl_context *ssl,
5754 unsigned char *dst,
5755 size_t dst_len,
5756 size_t *olen)
Jerry Yu24c0ec32021-09-09 14:21:07 +08005757{
Jerry Yu24c0ec32021-09-09 14:21:07 +08005758 int ret;
5759 mbedtls_sha256_context sha256;
5760
Gilles Peskine449bd832023-01-11 14:50:10 +01005761 if (dst_len < 32) {
5762 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
5763 }
Jerry Yu24c0ec32021-09-09 14:21:07 +08005764
Gilles Peskine449bd832023-01-11 14:50:10 +01005765 mbedtls_sha256_init(&sha256);
5766 mbedtls_sha256_clone(&sha256, &ssl->handshake->fin_sha256);
Jerry Yuc5aef882021-12-23 20:15:02 +08005767
Gilles Peskine449bd832023-01-11 14:50:10 +01005768 if ((ret = mbedtls_sha256_finish(&sha256, dst)) != 0) {
5769 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_sha256_finish", ret);
Jerry Yu24c0ec32021-09-09 14:21:07 +08005770 goto exit;
5771 }
5772
5773 *olen = 32;
5774
5775exit:
5776
Gilles Peskine449bd832023-01-11 14:50:10 +01005777 mbedtls_sha256_free(&sha256);
5778 return ret;
Jerry Yu24c0ec32021-09-09 14:21:07 +08005779}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005780#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005781
Gilles Peskine449bd832023-01-11 14:50:10 +01005782int mbedtls_ssl_get_handshake_transcript(mbedtls_ssl_context *ssl,
5783 const mbedtls_md_type_t md,
5784 unsigned char *dst,
5785 size_t dst_len,
5786 size_t *olen)
Jerry Yu24c0ec32021-09-09 14:21:07 +08005787{
Gilles Peskine449bd832023-01-11 14:50:10 +01005788 switch (md) {
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005789
Andrzej Kurek25f27152022-08-17 16:09:31 -04005790#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01005791 case MBEDTLS_MD_SHA384:
5792 return ssl_get_handshake_transcript_sha384(ssl, dst, dst_len, olen);
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005793#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005794
Andrzej Kurek25f27152022-08-17 16:09:31 -04005795#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01005796 case MBEDTLS_MD_SHA256:
5797 return ssl_get_handshake_transcript_sha256(ssl, dst, dst_len, olen);
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005798#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005799
Gilles Peskine449bd832023-01-11 14:50:10 +01005800 default:
Andrzej Kurek409248a2022-10-24 10:33:21 -04005801#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01005802 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
5803 (void) ssl;
5804 (void) dst;
5805 (void) dst_len;
5806 (void) olen;
Andrzej Kurek409248a2022-10-24 10:33:21 -04005807#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005808 break;
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005809 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005810 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu24c0ec32021-09-09 14:21:07 +08005811}
XiaokangQian647719a2021-12-07 09:16:29 +00005812
Jerry Yu148165c2021-09-24 23:20:59 +08005813#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005814
Ronald Crone68ab4f2022-10-05 12:46:29 +02005815#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Gabor Mezei078e8032022-04-27 21:17:56 +02005816/* mbedtls_ssl_parse_sig_alg_ext()
5817 *
5818 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
5819 * value (TLS 1.3 RFC8446):
5820 * enum {
5821 * ....
5822 * ecdsa_secp256r1_sha256( 0x0403 ),
5823 * ecdsa_secp384r1_sha384( 0x0503 ),
5824 * ecdsa_secp521r1_sha512( 0x0603 ),
5825 * ....
5826 * } SignatureScheme;
5827 *
5828 * struct {
5829 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
5830 * } SignatureSchemeList;
5831 *
5832 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
5833 * value (TLS 1.2 RFC5246):
5834 * enum {
5835 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
5836 * sha512(6), (255)
5837 * } HashAlgorithm;
5838 *
5839 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
5840 * SignatureAlgorithm;
5841 *
5842 * struct {
5843 * HashAlgorithm hash;
5844 * SignatureAlgorithm signature;
5845 * } SignatureAndHashAlgorithm;
5846 *
5847 * SignatureAndHashAlgorithm
5848 * supported_signature_algorithms<2..2^16-2>;
5849 *
5850 * The TLS 1.3 signature algorithm extension was defined to be a compatible
5851 * generalization of the TLS 1.2 signature algorithm extension.
5852 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
5853 * `SignatureScheme` field of TLS 1.3
5854 *
5855 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005856int mbedtls_ssl_parse_sig_alg_ext(mbedtls_ssl_context *ssl,
5857 const unsigned char *buf,
5858 const unsigned char *end)
Gabor Mezei078e8032022-04-27 21:17:56 +02005859{
5860 const unsigned char *p = buf;
5861 size_t supported_sig_algs_len = 0;
5862 const unsigned char *supported_sig_algs_end;
5863 uint16_t sig_alg;
5864 uint32_t common_idx = 0;
5865
Gilles Peskine449bd832023-01-11 14:50:10 +01005866 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
5867 supported_sig_algs_len = MBEDTLS_GET_UINT16_BE(p, 0);
Gabor Mezei078e8032022-04-27 21:17:56 +02005868 p += 2;
5869
Gilles Peskine449bd832023-01-11 14:50:10 +01005870 memset(ssl->handshake->received_sig_algs, 0,
5871 sizeof(ssl->handshake->received_sig_algs));
Gabor Mezei078e8032022-04-27 21:17:56 +02005872
Gilles Peskine449bd832023-01-11 14:50:10 +01005873 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, supported_sig_algs_len);
Gabor Mezei078e8032022-04-27 21:17:56 +02005874 supported_sig_algs_end = p + supported_sig_algs_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01005875 while (p < supported_sig_algs_end) {
5876 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, supported_sig_algs_end, 2);
5877 sig_alg = MBEDTLS_GET_UINT16_BE(p, 0);
Gabor Mezei078e8032022-04-27 21:17:56 +02005878 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01005879 MBEDTLS_SSL_DEBUG_MSG(4, ("received signature algorithm: 0x%x %s",
5880 sig_alg,
5881 mbedtls_ssl_sig_alg_to_str(sig_alg)));
Jerry Yu2fe6c632022-06-29 10:02:38 +08005882#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01005883 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 &&
5884 (!(mbedtls_ssl_sig_alg_is_supported(ssl, sig_alg) &&
5885 mbedtls_ssl_sig_alg_is_offered(ssl, sig_alg)))) {
Gabor Mezei078e8032022-04-27 21:17:56 +02005886 continue;
Jerry Yu2fe6c632022-06-29 10:02:38 +08005887 }
5888#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gabor Mezei078e8032022-04-27 21:17:56 +02005889
Gilles Peskine449bd832023-01-11 14:50:10 +01005890 MBEDTLS_SSL_DEBUG_MSG(4, ("valid signature algorithm: %s",
5891 mbedtls_ssl_sig_alg_to_str(sig_alg)));
Gabor Mezei078e8032022-04-27 21:17:56 +02005892
Gilles Peskine449bd832023-01-11 14:50:10 +01005893 if (common_idx + 1 < MBEDTLS_RECEIVED_SIG_ALGS_SIZE) {
Gabor Mezei078e8032022-04-27 21:17:56 +02005894 ssl->handshake->received_sig_algs[common_idx] = sig_alg;
5895 common_idx += 1;
5896 }
5897 }
5898 /* Check that we consumed all the message. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005899 if (p != end) {
5900 MBEDTLS_SSL_DEBUG_MSG(1,
5901 ("Signature algorithms extension length misaligned"));
5902 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
5903 MBEDTLS_ERR_SSL_DECODE_ERROR);
5904 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Gabor Mezei078e8032022-04-27 21:17:56 +02005905 }
5906
Gilles Peskine449bd832023-01-11 14:50:10 +01005907 if (common_idx == 0) {
5908 MBEDTLS_SSL_DEBUG_MSG(3, ("no signature algorithm in common"));
5909 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
5910 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
5911 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Gabor Mezei078e8032022-04-27 21:17:56 +02005912 }
5913
Gabor Mezei15b95a62022-05-09 16:37:58 +02005914 ssl->handshake->received_sig_algs[common_idx] = MBEDTLS_TLS_SIG_NONE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005915 return 0;
Gabor Mezei078e8032022-04-27 21:17:56 +02005916}
5917
Ronald Crone68ab4f2022-10-05 12:46:29 +02005918#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Gabor Mezei078e8032022-04-27 21:17:56 +02005919
Jerry Yudc7bd172022-02-17 13:44:15 +08005920#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5921
5922#if defined(MBEDTLS_USE_PSA_CRYPTO)
5923
Gilles Peskine449bd832023-01-11 14:50:10 +01005924static psa_status_t setup_psa_key_derivation(psa_key_derivation_operation_t *derivation,
5925 mbedtls_svc_key_id_t key,
5926 psa_algorithm_t alg,
5927 const unsigned char *raw_psk, size_t raw_psk_length,
5928 const unsigned char *seed, size_t seed_length,
5929 const unsigned char *label, size_t label_length,
5930 const unsigned char *other_secret,
5931 size_t other_secret_length,
5932 size_t capacity)
Jerry Yudc7bd172022-02-17 13:44:15 +08005933{
5934 psa_status_t status;
5935
Gilles Peskine449bd832023-01-11 14:50:10 +01005936 status = psa_key_derivation_setup(derivation, alg);
5937 if (status != PSA_SUCCESS) {
5938 return status;
5939 }
Jerry Yudc7bd172022-02-17 13:44:15 +08005940
Gilles Peskine449bd832023-01-11 14:50:10 +01005941 if (PSA_ALG_IS_TLS12_PRF(alg) || PSA_ALG_IS_TLS12_PSK_TO_MS(alg)) {
5942 status = psa_key_derivation_input_bytes(derivation,
5943 PSA_KEY_DERIVATION_INPUT_SEED,
5944 seed, seed_length);
5945 if (status != PSA_SUCCESS) {
5946 return status;
Przemek Stekiel1f027032022-04-05 17:12:11 +02005947 }
5948
Gilles Peskine449bd832023-01-11 14:50:10 +01005949 if (other_secret != NULL) {
5950 status = psa_key_derivation_input_bytes(derivation,
5951 PSA_KEY_DERIVATION_INPUT_OTHER_SECRET,
5952 other_secret, other_secret_length);
5953 if (status != PSA_SUCCESS) {
5954 return status;
5955 }
5956 }
5957
5958 if (mbedtls_svc_key_id_is_null(key)) {
Jerry Yudc7bd172022-02-17 13:44:15 +08005959 status = psa_key_derivation_input_bytes(
5960 derivation, PSA_KEY_DERIVATION_INPUT_SECRET,
Gilles Peskine449bd832023-01-11 14:50:10 +01005961 raw_psk, raw_psk_length);
5962 } else {
Jerry Yudc7bd172022-02-17 13:44:15 +08005963 status = psa_key_derivation_input_key(
Gilles Peskine449bd832023-01-11 14:50:10 +01005964 derivation, PSA_KEY_DERIVATION_INPUT_SECRET, key);
Jerry Yudc7bd172022-02-17 13:44:15 +08005965 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005966 if (status != PSA_SUCCESS) {
5967 return status;
5968 }
Jerry Yudc7bd172022-02-17 13:44:15 +08005969
Gilles Peskine449bd832023-01-11 14:50:10 +01005970 status = psa_key_derivation_input_bytes(derivation,
5971 PSA_KEY_DERIVATION_INPUT_LABEL,
5972 label, label_length);
5973 if (status != PSA_SUCCESS) {
5974 return status;
5975 }
5976 } else {
5977 return PSA_ERROR_NOT_SUPPORTED;
Jerry Yudc7bd172022-02-17 13:44:15 +08005978 }
5979
Gilles Peskine449bd832023-01-11 14:50:10 +01005980 status = psa_key_derivation_set_capacity(derivation, capacity);
5981 if (status != PSA_SUCCESS) {
5982 return status;
5983 }
Jerry Yudc7bd172022-02-17 13:44:15 +08005984
Gilles Peskine449bd832023-01-11 14:50:10 +01005985 return PSA_SUCCESS;
Jerry Yudc7bd172022-02-17 13:44:15 +08005986}
5987
Andrzej Kurek57d10632022-10-24 10:32:01 -04005988#if defined(PSA_WANT_ALG_SHA_384) || \
5989 defined(PSA_WANT_ALG_SHA_256)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005990MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01005991static int tls_prf_generic(mbedtls_md_type_t md_type,
5992 const unsigned char *secret, size_t slen,
5993 const char *label,
5994 const unsigned char *random, size_t rlen,
5995 unsigned char *dstbuf, size_t dlen)
Jerry Yudc7bd172022-02-17 13:44:15 +08005996{
5997 psa_status_t status;
5998 psa_algorithm_t alg;
5999 mbedtls_svc_key_id_t master_key = MBEDTLS_SVC_KEY_ID_INIT;
6000 psa_key_derivation_operation_t derivation =
6001 PSA_KEY_DERIVATION_OPERATION_INIT;
6002
Gilles Peskine449bd832023-01-11 14:50:10 +01006003 if (md_type == MBEDTLS_MD_SHA384) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006004 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
Gilles Peskine449bd832023-01-11 14:50:10 +01006005 } else {
Jerry Yudc7bd172022-02-17 13:44:15 +08006006 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
Gilles Peskine449bd832023-01-11 14:50:10 +01006007 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006008
6009 /* Normally a "secret" should be long enough to be impossible to
6010 * find by brute force, and in particular should not be empty. But
6011 * this PRF is also used to derive an IV, in particular in EAP-TLS,
6012 * and for this use case it makes sense to have a 0-length "secret".
6013 * Since the key API doesn't allow importing a key of length 0,
6014 * keep master_key=0, which setup_psa_key_derivation() understands
6015 * to mean a 0-length "secret" input. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006016 if (slen != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006017 psa_key_attributes_t key_attributes = psa_key_attributes_init();
Gilles Peskine449bd832023-01-11 14:50:10 +01006018 psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE);
6019 psa_set_key_algorithm(&key_attributes, alg);
6020 psa_set_key_type(&key_attributes, PSA_KEY_TYPE_DERIVE);
Jerry Yudc7bd172022-02-17 13:44:15 +08006021
Gilles Peskine449bd832023-01-11 14:50:10 +01006022 status = psa_import_key(&key_attributes, secret, slen, &master_key);
6023 if (status != PSA_SUCCESS) {
6024 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
6025 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006026 }
6027
Gilles Peskine449bd832023-01-11 14:50:10 +01006028 status = setup_psa_key_derivation(&derivation,
6029 master_key, alg,
6030 NULL, 0,
6031 random, rlen,
6032 (unsigned char const *) label,
6033 (size_t) strlen(label),
6034 NULL, 0,
6035 dlen);
6036 if (status != PSA_SUCCESS) {
6037 psa_key_derivation_abort(&derivation);
6038 psa_destroy_key(master_key);
6039 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yudc7bd172022-02-17 13:44:15 +08006040 }
6041
Gilles Peskine449bd832023-01-11 14:50:10 +01006042 status = psa_key_derivation_output_bytes(&derivation, dstbuf, dlen);
6043 if (status != PSA_SUCCESS) {
6044 psa_key_derivation_abort(&derivation);
6045 psa_destroy_key(master_key);
6046 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yudc7bd172022-02-17 13:44:15 +08006047 }
6048
Gilles Peskine449bd832023-01-11 14:50:10 +01006049 status = psa_key_derivation_abort(&derivation);
6050 if (status != PSA_SUCCESS) {
6051 psa_destroy_key(master_key);
6052 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yudc7bd172022-02-17 13:44:15 +08006053 }
6054
Gilles Peskine449bd832023-01-11 14:50:10 +01006055 if (!mbedtls_svc_key_id_is_null(master_key)) {
6056 status = psa_destroy_key(master_key);
6057 }
6058 if (status != PSA_SUCCESS) {
6059 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
6060 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006061
Gilles Peskine449bd832023-01-11 14:50:10 +01006062 return 0;
Jerry Yudc7bd172022-02-17 13:44:15 +08006063}
Andrzej Kurek57d10632022-10-24 10:32:01 -04006064#endif /* PSA_WANT_ALG_SHA_256 || PSA_WANT_ALG_SHA_384 */
Jerry Yudc7bd172022-02-17 13:44:15 +08006065#else /* MBEDTLS_USE_PSA_CRYPTO */
6066
Andrzej Kurek57d10632022-10-24 10:32:01 -04006067#if defined(MBEDTLS_MD_C) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01006068 (defined(MBEDTLS_SHA256_C) || \
6069 defined(MBEDTLS_SHA384_C))
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006070MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006071static int tls_prf_generic(mbedtls_md_type_t md_type,
6072 const unsigned char *secret, size_t slen,
6073 const char *label,
6074 const unsigned char *random, size_t rlen,
6075 unsigned char *dstbuf, size_t dlen)
Jerry Yudc7bd172022-02-17 13:44:15 +08006076{
6077 size_t nb;
6078 size_t i, j, k, md_len;
6079 unsigned char *tmp;
6080 size_t tmp_len = 0;
6081 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
6082 const mbedtls_md_info_t *md_info;
6083 mbedtls_md_context_t md_ctx;
6084 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6085
Gilles Peskine449bd832023-01-11 14:50:10 +01006086 mbedtls_md_init(&md_ctx);
Jerry Yudc7bd172022-02-17 13:44:15 +08006087
Gilles Peskine449bd832023-01-11 14:50:10 +01006088 if ((md_info = mbedtls_md_info_from_type(md_type)) == NULL) {
6089 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
6090 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006091
Gilles Peskine449bd832023-01-11 14:50:10 +01006092 md_len = mbedtls_md_get_size(md_info);
Jerry Yudc7bd172022-02-17 13:44:15 +08006093
Gilles Peskine449bd832023-01-11 14:50:10 +01006094 tmp_len = md_len + strlen(label) + rlen;
6095 tmp = mbedtls_calloc(1, tmp_len);
6096 if (tmp == NULL) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006097 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
6098 goto exit;
6099 }
6100
Gilles Peskine449bd832023-01-11 14:50:10 +01006101 nb = strlen(label);
6102 memcpy(tmp + md_len, label, nb);
6103 memcpy(tmp + md_len + nb, random, rlen);
Jerry Yudc7bd172022-02-17 13:44:15 +08006104 nb += rlen;
6105
6106 /*
6107 * Compute P_<hash>(secret, label + random)[0..dlen]
6108 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006109 if ((ret = mbedtls_md_setup(&md_ctx, md_info, 1)) != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006110 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006111 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006112
Gilles Peskine449bd832023-01-11 14:50:10 +01006113 ret = mbedtls_md_hmac_starts(&md_ctx, secret, slen);
6114 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006115 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006116 }
6117 ret = mbedtls_md_hmac_update(&md_ctx, tmp + md_len, nb);
6118 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006119 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006120 }
6121 ret = mbedtls_md_hmac_finish(&md_ctx, tmp);
6122 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006123 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006124 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006125
Gilles Peskine449bd832023-01-11 14:50:10 +01006126 for (i = 0; i < dlen; i += md_len) {
6127 ret = mbedtls_md_hmac_reset(&md_ctx);
6128 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006129 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006130 }
6131 ret = mbedtls_md_hmac_update(&md_ctx, tmp, md_len + nb);
6132 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006133 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006134 }
6135 ret = mbedtls_md_hmac_finish(&md_ctx, h_i);
6136 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006137 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006138 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006139
Gilles Peskine449bd832023-01-11 14:50:10 +01006140 ret = mbedtls_md_hmac_reset(&md_ctx);
6141 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006142 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006143 }
6144 ret = mbedtls_md_hmac_update(&md_ctx, tmp, md_len);
6145 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006146 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006147 }
6148 ret = mbedtls_md_hmac_finish(&md_ctx, tmp);
6149 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006150 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006151 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006152
Gilles Peskine449bd832023-01-11 14:50:10 +01006153 k = (i + md_len > dlen) ? dlen % md_len : md_len;
Jerry Yudc7bd172022-02-17 13:44:15 +08006154
Gilles Peskine449bd832023-01-11 14:50:10 +01006155 for (j = 0; j < k; j++) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006156 dstbuf[i + j] = h_i[j];
Gilles Peskine449bd832023-01-11 14:50:10 +01006157 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006158 }
6159
6160exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006161 mbedtls_md_free(&md_ctx);
Jerry Yudc7bd172022-02-17 13:44:15 +08006162
Gilles Peskine449bd832023-01-11 14:50:10 +01006163 if (tmp != NULL) {
6164 mbedtls_platform_zeroize(tmp, tmp_len);
6165 }
Dave Rodgman29b9b2b2022-11-01 16:08:14 +00006166
Gilles Peskine449bd832023-01-11 14:50:10 +01006167 mbedtls_platform_zeroize(h_i, sizeof(h_i));
Jerry Yudc7bd172022-02-17 13:44:15 +08006168
Gilles Peskine449bd832023-01-11 14:50:10 +01006169 mbedtls_free(tmp);
Jerry Yudc7bd172022-02-17 13:44:15 +08006170
Gilles Peskine449bd832023-01-11 14:50:10 +01006171 return ret;
Jerry Yudc7bd172022-02-17 13:44:15 +08006172}
Andrzej Kurek57d10632022-10-24 10:32:01 -04006173#endif /* MBEDTLS_MD_C && ( MBEDTLS_SHA256_C || MBEDTLS_SHA384_C ) */
Jerry Yudc7bd172022-02-17 13:44:15 +08006174#endif /* MBEDTLS_USE_PSA_CRYPTO */
6175
Andrzej Kurek25f27152022-08-17 16:09:31 -04006176#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006177MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006178static int tls_prf_sha256(const unsigned char *secret, size_t slen,
6179 const char *label,
6180 const unsigned char *random, size_t rlen,
6181 unsigned char *dstbuf, size_t dlen)
Jerry Yudc7bd172022-02-17 13:44:15 +08006182{
Gilles Peskine449bd832023-01-11 14:50:10 +01006183 return tls_prf_generic(MBEDTLS_MD_SHA256, secret, slen,
6184 label, random, rlen, dstbuf, dlen);
Jerry Yudc7bd172022-02-17 13:44:15 +08006185}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04006186#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudc7bd172022-02-17 13:44:15 +08006187
Andrzej Kurek25f27152022-08-17 16:09:31 -04006188#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006189MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006190static int tls_prf_sha384(const unsigned char *secret, size_t slen,
6191 const char *label,
6192 const unsigned char *random, size_t rlen,
6193 unsigned char *dstbuf, size_t dlen)
Jerry Yudc7bd172022-02-17 13:44:15 +08006194{
Gilles Peskine449bd832023-01-11 14:50:10 +01006195 return tls_prf_generic(MBEDTLS_MD_SHA384, secret, slen,
6196 label, random, rlen, dstbuf, dlen);
Jerry Yudc7bd172022-02-17 13:44:15 +08006197}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04006198#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudc7bd172022-02-17 13:44:15 +08006199
Jerry Yuf009d862022-02-17 14:01:37 +08006200/*
6201 * Set appropriate PRF function and other SSL / TLS1.2 functions
6202 *
6203 * Inputs:
Jerry Yuf009d862022-02-17 14:01:37 +08006204 * - hash associated with the ciphersuite (only used by TLS 1.2)
6205 *
6206 * Outputs:
6207 * - the tls_prf, calc_verify and calc_finished members of handshake structure
6208 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006209MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006210static int ssl_set_handshake_prfs(mbedtls_ssl_handshake_params *handshake,
6211 mbedtls_md_type_t hash)
Jerry Yuf009d862022-02-17 14:01:37 +08006212{
Andrzej Kurek25f27152022-08-17 16:09:31 -04006213#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01006214 if (hash == MBEDTLS_MD_SHA384) {
Jerry Yuf009d862022-02-17 14:01:37 +08006215 handshake->tls_prf = tls_prf_sha384;
6216 handshake->calc_verify = ssl_calc_verify_tls_sha384;
6217 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Gilles Peskine449bd832023-01-11 14:50:10 +01006218 } else
Jerry Yuf009d862022-02-17 14:01:37 +08006219#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04006220#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuf009d862022-02-17 14:01:37 +08006221 {
Ronald Cron81591aa2022-03-07 09:05:51 +01006222 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08006223 handshake->tls_prf = tls_prf_sha256;
6224 handshake->calc_verify = ssl_calc_verify_tls_sha256;
6225 handshake->calc_finished = ssl_calc_finished_tls_sha256;
6226 }
Ronald Cron81591aa2022-03-07 09:05:51 +01006227#else
Jerry Yuf009d862022-02-17 14:01:37 +08006228 {
Jerry Yuf009d862022-02-17 14:01:37 +08006229 (void) handshake;
Ronald Cron81591aa2022-03-07 09:05:51 +01006230 (void) hash;
Gilles Peskine449bd832023-01-11 14:50:10 +01006231 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yuf009d862022-02-17 14:01:37 +08006232 }
Ronald Cron81591aa2022-03-07 09:05:51 +01006233#endif
Jerry Yuf009d862022-02-17 14:01:37 +08006234
Gilles Peskine449bd832023-01-11 14:50:10 +01006235 return 0;
Jerry Yuf009d862022-02-17 14:01:37 +08006236}
Jerry Yud6ab2352022-02-17 14:03:43 +08006237
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006238/*
6239 * Compute master secret if needed
6240 *
6241 * Parameters:
6242 * [in/out] handshake
6243 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
6244 * (PSA-PSK) ciphersuite_info, psk_opaque
6245 * [out] premaster (cleared)
6246 * [out] master
6247 * [in] ssl: optionally used for debugging, EMS and PSA-PSK
6248 * debug: conf->f_dbg, conf->p_dbg
6249 * EMS: passed to calc_verify (debug + session_negotiate)
Ronald Crona25cf582022-03-07 11:10:36 +01006250 * PSA-PSA: conf
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006251 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006252MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006253static int ssl_compute_master(mbedtls_ssl_handshake_params *handshake,
6254 unsigned char *master,
6255 const mbedtls_ssl_context *ssl)
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006256{
6257 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6258
6259 /* cf. RFC 5246, Section 8.1:
6260 * "The master secret is always exactly 48 bytes in length." */
6261 size_t const master_secret_len = 48;
6262
6263#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
6264 unsigned char session_hash[48];
6265#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
6266
6267 /* The label for the KDF used for key expansion.
6268 * This is either "master secret" or "extended master secret"
6269 * depending on whether the Extended Master Secret extension
6270 * is used. */
6271 char const *lbl = "master secret";
6272
Przemek Stekielae4ed302022-04-05 17:15:55 +02006273 /* The seed for the KDF used for key expansion.
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006274 * - If the Extended Master Secret extension is not used,
6275 * this is ClientHello.Random + ServerHello.Random
6276 * (see Sect. 8.1 in RFC 5246).
6277 * - If the Extended Master Secret extension is used,
6278 * this is the transcript of the handshake so far.
6279 * (see Sect. 4 in RFC 7627). */
Przemek Stekielae4ed302022-04-05 17:15:55 +02006280 unsigned char const *seed = handshake->randbytes;
6281 size_t seed_len = 64;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006282
6283#if !defined(MBEDTLS_DEBUG_C) && \
6284 !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \
6285 !(defined(MBEDTLS_USE_PSA_CRYPTO) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01006286 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED))
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006287 ssl = NULL; /* make sure we don't use it except for those cases */
6288 (void) ssl;
6289#endif
6290
Gilles Peskine449bd832023-01-11 14:50:10 +01006291 if (handshake->resume != 0) {
6292 MBEDTLS_SSL_DEBUG_MSG(3, ("no premaster (session resumed)"));
6293 return 0;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006294 }
6295
6296#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Gilles Peskine449bd832023-01-11 14:50:10 +01006297 if (handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED) {
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006298 lbl = "extended master secret";
Przemek Stekielae4ed302022-04-05 17:15:55 +02006299 seed = session_hash;
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01006300 ret = handshake->calc_verify(ssl, session_hash, &seed_len);
6301 if (ret != 0) {
6302 MBEDTLS_SSL_DEBUG_RET(1, "calc_verify", ret);
6303 }
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006304
Gilles Peskine449bd832023-01-11 14:50:10 +01006305 MBEDTLS_SSL_DEBUG_BUF(3, "session hash for extended master secret",
6306 session_hash, seed_len);
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006307 }
Przemek Stekiel169bf0b2022-04-29 07:53:29 +02006308#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006309
Przemek Stekiel99114f32022-04-22 11:20:09 +02006310#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Przemek Stekiel8a4b7fd2022-04-28 09:22:22 +02006311 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006312 if (mbedtls_ssl_ciphersuite_uses_psk(handshake->ciphersuite_info) == 1) {
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006313 /* Perform PSK-to-MS expansion in a single step. */
6314 psa_status_t status;
6315 psa_algorithm_t alg;
6316 mbedtls_svc_key_id_t psk;
6317 psa_key_derivation_operation_t derivation =
6318 PSA_KEY_DERIVATION_OPERATION_INIT;
6319 mbedtls_md_type_t hash_alg = handshake->ciphersuite_info->mac;
6320
Gilles Peskine449bd832023-01-11 14:50:10 +01006321 MBEDTLS_SSL_DEBUG_MSG(2, ("perform PSA-based PSK-to-MS expansion"));
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006322
Gilles Peskine449bd832023-01-11 14:50:10 +01006323 psk = mbedtls_ssl_get_opaque_psk(ssl);
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006324
Gilles Peskine449bd832023-01-11 14:50:10 +01006325 if (hash_alg == MBEDTLS_MD_SHA384) {
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006326 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
Gilles Peskine449bd832023-01-11 14:50:10 +01006327 } else {
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006328 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
Gilles Peskine449bd832023-01-11 14:50:10 +01006329 }
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006330
Przemek Stekiel51a1f362022-04-13 08:57:06 +02006331 size_t other_secret_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006332 unsigned char *other_secret = NULL;
Przemek Stekielc2033402022-04-05 17:19:41 +02006333
Gilles Peskine449bd832023-01-11 14:50:10 +01006334 switch (handshake->ciphersuite_info->key_exchange) {
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006335 /* Provide other secret.
Przemek Stekiel51a1f362022-04-13 08:57:06 +02006336 * Other secret is stored in premaster, where first 2 bytes hold the
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006337 * length of the other key.
Przemek Stekielc2033402022-04-05 17:19:41 +02006338 */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006339 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Przemek Stekiel8abcee92022-04-28 09:16:28 +02006340 /* For RSA-PSK other key length is always 48 bytes. */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006341 other_secret_len = 48;
6342 other_secret = handshake->premaster + 2;
6343 break;
6344 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006345 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006346 other_secret_len = MBEDTLS_GET_UINT16_BE(handshake->premaster, 0);
6347 other_secret = handshake->premaster + 2;
6348 break;
6349 default:
6350 break;
Przemek Stekielc2033402022-04-05 17:19:41 +02006351 }
6352
Gilles Peskine449bd832023-01-11 14:50:10 +01006353 status = setup_psa_key_derivation(&derivation, psk, alg,
6354 ssl->conf->psk, ssl->conf->psk_len,
6355 seed, seed_len,
6356 (unsigned char const *) lbl,
6357 (size_t) strlen(lbl),
6358 other_secret, other_secret_len,
6359 master_secret_len);
6360 if (status != PSA_SUCCESS) {
6361 psa_key_derivation_abort(&derivation);
6362 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006363 }
6364
Gilles Peskine449bd832023-01-11 14:50:10 +01006365 status = psa_key_derivation_output_bytes(&derivation,
6366 master,
6367 master_secret_len);
6368 if (status != PSA_SUCCESS) {
6369 psa_key_derivation_abort(&derivation);
6370 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006371 }
6372
Gilles Peskine449bd832023-01-11 14:50:10 +01006373 status = psa_key_derivation_abort(&derivation);
6374 if (status != PSA_SUCCESS) {
6375 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
6376 }
6377 } else
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006378#endif
6379 {
Neil Armstrongca7d5062022-05-31 14:43:23 +02006380#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01006381 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
6382 if (handshake->ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE) {
Neil Armstrongca7d5062022-05-31 14:43:23 +02006383 psa_status_t status;
6384 psa_algorithm_t alg = PSA_ALG_TLS12_ECJPAKE_TO_PMS;
6385 psa_key_derivation_operation_t derivation =
6386 PSA_KEY_DERIVATION_OPERATION_INIT;
6387
Gilles Peskine449bd832023-01-11 14:50:10 +01006388 MBEDTLS_SSL_DEBUG_MSG(2, ("perform PSA-based PMS KDF for ECJPAKE"));
Neil Armstrongca7d5062022-05-31 14:43:23 +02006389
6390 handshake->pmslen = PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE;
6391
Gilles Peskine449bd832023-01-11 14:50:10 +01006392 status = psa_key_derivation_setup(&derivation, alg);
6393 if (status != PSA_SUCCESS) {
6394 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Neil Armstrongca7d5062022-05-31 14:43:23 +02006395 }
6396
Gilles Peskine449bd832023-01-11 14:50:10 +01006397 status = psa_key_derivation_set_capacity(&derivation,
6398 PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE);
6399 if (status != PSA_SUCCESS) {
6400 psa_key_derivation_abort(&derivation);
6401 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Neil Armstrongca7d5062022-05-31 14:43:23 +02006402 }
6403
Gilles Peskine449bd832023-01-11 14:50:10 +01006404 status = psa_pake_get_implicit_key(&handshake->psa_pake_ctx,
6405 &derivation);
6406 if (status != PSA_SUCCESS) {
6407 psa_key_derivation_abort(&derivation);
6408 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Neil Armstrongca7d5062022-05-31 14:43:23 +02006409 }
6410
Gilles Peskine449bd832023-01-11 14:50:10 +01006411 status = psa_key_derivation_output_bytes(&derivation,
6412 handshake->premaster,
6413 handshake->pmslen);
6414 if (status != PSA_SUCCESS) {
6415 psa_key_derivation_abort(&derivation);
6416 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
6417 }
6418
6419 status = psa_key_derivation_abort(&derivation);
6420 if (status != PSA_SUCCESS) {
6421 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Neil Armstrongca7d5062022-05-31 14:43:23 +02006422 }
6423 }
6424#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01006425 ret = handshake->tls_prf(handshake->premaster, handshake->pmslen,
6426 lbl, seed, seed_len,
6427 master,
6428 master_secret_len);
6429 if (ret != 0) {
6430 MBEDTLS_SSL_DEBUG_RET(1, "prf", ret);
6431 return ret;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006432 }
6433
Gilles Peskine449bd832023-01-11 14:50:10 +01006434 MBEDTLS_SSL_DEBUG_BUF(3, "premaster secret",
6435 handshake->premaster,
6436 handshake->pmslen);
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006437
Gilles Peskine449bd832023-01-11 14:50:10 +01006438 mbedtls_platform_zeroize(handshake->premaster,
6439 sizeof(handshake->premaster));
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006440 }
6441
Gilles Peskine449bd832023-01-11 14:50:10 +01006442 return 0;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006443}
6444
Gilles Peskine449bd832023-01-11 14:50:10 +01006445int mbedtls_ssl_derive_keys(mbedtls_ssl_context *ssl)
Jerry Yud62f87e2022-02-17 14:09:02 +08006446{
6447 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6448 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
6449 ssl->handshake->ciphersuite_info;
6450
Gilles Peskine449bd832023-01-11 14:50:10 +01006451 MBEDTLS_SSL_DEBUG_MSG(2, ("=> derive keys"));
Jerry Yud62f87e2022-02-17 14:09:02 +08006452
6453 /* Set PRF, calc_verify and calc_finished function pointers */
Gilles Peskine449bd832023-01-11 14:50:10 +01006454 ret = ssl_set_handshake_prfs(ssl->handshake,
6455 ciphersuite_info->mac);
6456 if (ret != 0) {
6457 MBEDTLS_SSL_DEBUG_RET(1, "ssl_set_handshake_prfs", ret);
6458 return ret;
Jerry Yud62f87e2022-02-17 14:09:02 +08006459 }
6460
6461 /* Compute master secret if needed */
Gilles Peskine449bd832023-01-11 14:50:10 +01006462 ret = ssl_compute_master(ssl->handshake,
6463 ssl->session_negotiate->master,
6464 ssl);
6465 if (ret != 0) {
6466 MBEDTLS_SSL_DEBUG_RET(1, "ssl_compute_master", ret);
6467 return ret;
Jerry Yud62f87e2022-02-17 14:09:02 +08006468 }
6469
6470 /* Swap the client and server random values:
6471 * - MS derivation wanted client+server (RFC 5246 8.1)
6472 * - key derivation wants server+client (RFC 5246 6.3) */
6473 {
6474 unsigned char tmp[64];
Gilles Peskine449bd832023-01-11 14:50:10 +01006475 memcpy(tmp, ssl->handshake->randbytes, 64);
6476 memcpy(ssl->handshake->randbytes, tmp + 32, 32);
6477 memcpy(ssl->handshake->randbytes + 32, tmp, 32);
6478 mbedtls_platform_zeroize(tmp, sizeof(tmp));
Jerry Yud62f87e2022-02-17 14:09:02 +08006479 }
6480
6481 /* Populate transform structure */
Gilles Peskine449bd832023-01-11 14:50:10 +01006482 ret = ssl_tls12_populate_transform(ssl->transform_negotiate,
6483 ssl->session_negotiate->ciphersuite,
6484 ssl->session_negotiate->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02006485#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01006486 ssl->session_negotiate->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02006487#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01006488 ssl->handshake->tls_prf,
6489 ssl->handshake->randbytes,
6490 ssl->tls_version,
6491 ssl->conf->endpoint,
6492 ssl);
6493 if (ret != 0) {
6494 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls12_populate_transform", ret);
6495 return ret;
Jerry Yud62f87e2022-02-17 14:09:02 +08006496 }
6497
6498 /* We no longer need Server/ClientHello.random values */
Gilles Peskine449bd832023-01-11 14:50:10 +01006499 mbedtls_platform_zeroize(ssl->handshake->randbytes,
6500 sizeof(ssl->handshake->randbytes));
Jerry Yud62f87e2022-02-17 14:09:02 +08006501
Gilles Peskine449bd832023-01-11 14:50:10 +01006502 MBEDTLS_SSL_DEBUG_MSG(2, ("<= derive keys"));
Jerry Yud62f87e2022-02-17 14:09:02 +08006503
Gilles Peskine449bd832023-01-11 14:50:10 +01006504 return 0;
Jerry Yud62f87e2022-02-17 14:09:02 +08006505}
Jerry Yu8392e0d2022-02-17 14:10:24 +08006506
Gilles Peskine449bd832023-01-11 14:50:10 +01006507int mbedtls_ssl_set_calc_verify_md(mbedtls_ssl_context *ssl, int md)
Ronald Cron4dcbca92022-03-07 10:21:40 +01006508{
Gilles Peskine449bd832023-01-11 14:50:10 +01006509 switch (md) {
Andrzej Kurek25f27152022-08-17 16:09:31 -04006510#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron4dcbca92022-03-07 10:21:40 +01006511 case MBEDTLS_SSL_HASH_SHA384:
6512 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
6513 break;
6514#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04006515#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron4dcbca92022-03-07 10:21:40 +01006516 case MBEDTLS_SSL_HASH_SHA256:
6517 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
6518 break;
6519#endif
6520 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006521 return -1;
Ronald Cron4dcbca92022-03-07 10:21:40 +01006522 }
Andrzej Kurekeabeb302022-10-17 07:52:51 -04006523#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
6524 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
6525 (void) ssl;
6526#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01006527 return 0;
Ronald Cron4dcbca92022-03-07 10:21:40 +01006528}
6529
Andrzej Kurek25f27152022-08-17 16:09:31 -04006530#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01006531int ssl_calc_verify_tls_sha256(const mbedtls_ssl_context *ssl,
6532 unsigned char *hash,
6533 size_t *hlen)
Jerry Yu8392e0d2022-02-17 14:10:24 +08006534{
6535#if defined(MBEDTLS_USE_PSA_CRYPTO)
6536 size_t hash_size;
6537 psa_status_t status;
6538 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
6539
Gilles Peskine449bd832023-01-11 14:50:10 +01006540 MBEDTLS_SSL_DEBUG_MSG(2, ("=> PSA calc verify sha256"));
6541 status = psa_hash_clone(&ssl->handshake->fin_sha256_psa, &sha256_psa);
6542 if (status != PSA_SUCCESS) {
6543 MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash clone failed"));
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01006544 return 0;
Jerry Yu8392e0d2022-02-17 14:10:24 +08006545 }
6546
Gilles Peskine449bd832023-01-11 14:50:10 +01006547 status = psa_hash_finish(&sha256_psa, hash, 32, &hash_size);
6548 if (status != PSA_SUCCESS) {
6549 MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash finish failed"));
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01006550 return 0;
Jerry Yu8392e0d2022-02-17 14:10:24 +08006551 }
6552
6553 *hlen = 32;
Gilles Peskine449bd832023-01-11 14:50:10 +01006554 MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated verify result", hash, *hlen);
6555 MBEDTLS_SSL_DEBUG_MSG(2, ("<= PSA calc verify"));
Jerry Yu8392e0d2022-02-17 14:10:24 +08006556#else
6557 mbedtls_sha256_context sha256;
6558
Gilles Peskine449bd832023-01-11 14:50:10 +01006559 mbedtls_sha256_init(&sha256);
Jerry Yu8392e0d2022-02-17 14:10:24 +08006560
Gilles Peskine449bd832023-01-11 14:50:10 +01006561 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc verify sha256"));
Jerry Yu8392e0d2022-02-17 14:10:24 +08006562
Gilles Peskine449bd832023-01-11 14:50:10 +01006563 mbedtls_sha256_clone(&sha256, &ssl->handshake->fin_sha256);
6564 mbedtls_sha256_finish(&sha256, hash);
Jerry Yu8392e0d2022-02-17 14:10:24 +08006565
6566 *hlen = 32;
6567
Gilles Peskine449bd832023-01-11 14:50:10 +01006568 MBEDTLS_SSL_DEBUG_BUF(3, "calculated verify result", hash, *hlen);
6569 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc verify"));
Jerry Yu8392e0d2022-02-17 14:10:24 +08006570
Gilles Peskine449bd832023-01-11 14:50:10 +01006571 mbedtls_sha256_free(&sha256);
Jerry Yu8392e0d2022-02-17 14:10:24 +08006572#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01006573 return 0;
Jerry Yu8392e0d2022-02-17 14:10:24 +08006574}
Andrzej Kurek25f27152022-08-17 16:09:31 -04006575#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu8392e0d2022-02-17 14:10:24 +08006576
Andrzej Kurek25f27152022-08-17 16:09:31 -04006577#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01006578int ssl_calc_verify_tls_sha384(const mbedtls_ssl_context *ssl,
6579 unsigned char *hash,
6580 size_t *hlen)
Jerry Yuc1cb3842022-02-17 14:13:48 +08006581{
6582#if defined(MBEDTLS_USE_PSA_CRYPTO)
6583 size_t hash_size;
6584 psa_status_t status;
6585 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
6586
Gilles Peskine449bd832023-01-11 14:50:10 +01006587 MBEDTLS_SSL_DEBUG_MSG(2, ("=> PSA calc verify sha384"));
6588 status = psa_hash_clone(&ssl->handshake->fin_sha384_psa, &sha384_psa);
6589 if (status != PSA_SUCCESS) {
6590 MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash clone failed"));
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01006591 return 0;
Jerry Yuc1cb3842022-02-17 14:13:48 +08006592 }
6593
Gilles Peskine449bd832023-01-11 14:50:10 +01006594 status = psa_hash_finish(&sha384_psa, hash, 48, &hash_size);
6595 if (status != PSA_SUCCESS) {
6596 MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash finish failed"));
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01006597 return 0;
Jerry Yuc1cb3842022-02-17 14:13:48 +08006598 }
6599
6600 *hlen = 48;
Gilles Peskine449bd832023-01-11 14:50:10 +01006601 MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated verify result", hash, *hlen);
6602 MBEDTLS_SSL_DEBUG_MSG(2, ("<= PSA calc verify"));
Jerry Yuc1cb3842022-02-17 14:13:48 +08006603#else
6604 mbedtls_sha512_context sha512;
6605
Gilles Peskine449bd832023-01-11 14:50:10 +01006606 mbedtls_sha512_init(&sha512);
Jerry Yuc1cb3842022-02-17 14:13:48 +08006607
Gilles Peskine449bd832023-01-11 14:50:10 +01006608 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc verify sha384"));
Jerry Yuc1cb3842022-02-17 14:13:48 +08006609
Gilles Peskine449bd832023-01-11 14:50:10 +01006610 mbedtls_sha512_clone(&sha512, &ssl->handshake->fin_sha384);
6611 mbedtls_sha512_finish(&sha512, hash);
Jerry Yuc1cb3842022-02-17 14:13:48 +08006612
6613 *hlen = 48;
6614
Gilles Peskine449bd832023-01-11 14:50:10 +01006615 MBEDTLS_SSL_DEBUG_BUF(3, "calculated verify result", hash, *hlen);
6616 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc verify"));
Jerry Yuc1cb3842022-02-17 14:13:48 +08006617
Gilles Peskine449bd832023-01-11 14:50:10 +01006618 mbedtls_sha512_free(&sha512);
Jerry Yuc1cb3842022-02-17 14:13:48 +08006619#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01006620 return 0;
Jerry Yuc1cb3842022-02-17 14:13:48 +08006621}
Andrzej Kurek25f27152022-08-17 16:09:31 -04006622#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yuc1cb3842022-02-17 14:13:48 +08006623
Neil Armstrong80f6f322022-05-03 17:56:38 +02006624#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
6625 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006626int mbedtls_ssl_psk_derive_premaster(mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex)
Jerry Yuce3dca42022-02-17 14:16:37 +08006627{
6628 unsigned char *p = ssl->handshake->premaster;
Gilles Peskine449bd832023-01-11 14:50:10 +01006629 unsigned char *end = p + sizeof(ssl->handshake->premaster);
Jerry Yuce3dca42022-02-17 14:16:37 +08006630 const unsigned char *psk = NULL;
6631 size_t psk_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006632 int psk_ret = mbedtls_ssl_get_psk(ssl, &psk, &psk_len);
Jerry Yuce3dca42022-02-17 14:16:37 +08006633
Gilles Peskine449bd832023-01-11 14:50:10 +01006634 if (psk_ret == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED) {
Jerry Yuce3dca42022-02-17 14:16:37 +08006635 /*
6636 * This should never happen because the existence of a PSK is always
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006637 * checked before calling this function.
6638 *
6639 * The exception is opaque DHE-PSK. For DHE-PSK fill premaster with
Przemek Stekiel8abcee92022-04-28 09:16:28 +02006640 * the shared secret without PSK.
Jerry Yuce3dca42022-02-17 14:16:37 +08006641 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006642 if (key_ex != MBEDTLS_KEY_EXCHANGE_DHE_PSK) {
6643 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
6644 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006645 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006646 }
6647
6648 /*
6649 * PMS = struct {
6650 * opaque other_secret<0..2^16-1>;
6651 * opaque psk<0..2^16-1>;
6652 * };
6653 * with "other_secret" depending on the particular key exchange
6654 */
6655#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006656 if (key_ex == MBEDTLS_KEY_EXCHANGE_PSK) {
6657 if (end - p < 2) {
6658 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6659 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006660
Gilles Peskine449bd832023-01-11 14:50:10 +01006661 MBEDTLS_PUT_UINT16_BE(psk_len, p, 0);
Jerry Yuce3dca42022-02-17 14:16:37 +08006662 p += 2;
6663
Gilles Peskine449bd832023-01-11 14:50:10 +01006664 if (end < p || (size_t) (end - p) < psk_len) {
6665 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6666 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006667
Gilles Peskine449bd832023-01-11 14:50:10 +01006668 memset(p, 0, psk_len);
Jerry Yuce3dca42022-02-17 14:16:37 +08006669 p += psk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01006670 } else
Jerry Yuce3dca42022-02-17 14:16:37 +08006671#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
6672#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006673 if (key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK) {
Jerry Yuce3dca42022-02-17 14:16:37 +08006674 /*
6675 * other_secret already set by the ClientKeyExchange message,
6676 * and is 48 bytes long
6677 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006678 if (end - p < 2) {
6679 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6680 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006681
6682 *p++ = 0;
6683 *p++ = 48;
6684 p += 48;
Gilles Peskine449bd832023-01-11 14:50:10 +01006685 } else
Jerry Yuce3dca42022-02-17 14:16:37 +08006686#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
6687#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006688 if (key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK) {
Jerry Yuce3dca42022-02-17 14:16:37 +08006689 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6690 size_t len;
6691
6692 /* Write length only when we know the actual value */
Gilles Peskine449bd832023-01-11 14:50:10 +01006693 if ((ret = mbedtls_dhm_calc_secret(&ssl->handshake->dhm_ctx,
6694 p + 2, end - (p + 2), &len,
6695 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
6696 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_calc_secret", ret);
6697 return ret;
Jerry Yuce3dca42022-02-17 14:16:37 +08006698 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006699 MBEDTLS_PUT_UINT16_BE(len, p, 0);
Jerry Yuce3dca42022-02-17 14:16:37 +08006700 p += 2 + len;
6701
Gilles Peskine449bd832023-01-11 14:50:10 +01006702 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: K ", &ssl->handshake->dhm_ctx.K);
6703 } else
Jerry Yuce3dca42022-02-17 14:16:37 +08006704#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Neil Armstrong80f6f322022-05-03 17:56:38 +02006705#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006706 if (key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK) {
Jerry Yuce3dca42022-02-17 14:16:37 +08006707 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6708 size_t zlen;
6709
Gilles Peskine449bd832023-01-11 14:50:10 +01006710 if ((ret = mbedtls_ecdh_calc_secret(&ssl->handshake->ecdh_ctx, &zlen,
6711 p + 2, end - (p + 2),
6712 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
6713 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecdh_calc_secret", ret);
6714 return ret;
Jerry Yuce3dca42022-02-17 14:16:37 +08006715 }
6716
Gilles Peskine449bd832023-01-11 14:50:10 +01006717 MBEDTLS_PUT_UINT16_BE(zlen, p, 0);
Jerry Yuce3dca42022-02-17 14:16:37 +08006718 p += 2 + zlen;
6719
Gilles Peskine449bd832023-01-11 14:50:10 +01006720 MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx,
6721 MBEDTLS_DEBUG_ECDH_Z);
6722 } else
Neil Armstrong80f6f322022-05-03 17:56:38 +02006723#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Jerry Yuce3dca42022-02-17 14:16:37 +08006724 {
Gilles Peskine449bd832023-01-11 14:50:10 +01006725 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
6726 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yuce3dca42022-02-17 14:16:37 +08006727 }
6728
6729 /* opaque psk<0..2^16-1>; */
Gilles Peskine449bd832023-01-11 14:50:10 +01006730 if (end - p < 2) {
6731 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6732 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006733
Gilles Peskine449bd832023-01-11 14:50:10 +01006734 MBEDTLS_PUT_UINT16_BE(psk_len, p, 0);
Jerry Yuce3dca42022-02-17 14:16:37 +08006735 p += 2;
6736
Gilles Peskine449bd832023-01-11 14:50:10 +01006737 if (end < p || (size_t) (end - p) < psk_len) {
6738 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6739 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006740
Gilles Peskine449bd832023-01-11 14:50:10 +01006741 memcpy(p, psk, psk_len);
Jerry Yuce3dca42022-02-17 14:16:37 +08006742 p += psk_len;
6743
6744 ssl->handshake->pmslen = p - ssl->handshake->premaster;
6745
Gilles Peskine449bd832023-01-11 14:50:10 +01006746 return 0;
Jerry Yuce3dca42022-02-17 14:16:37 +08006747}
Neil Armstrong80f6f322022-05-03 17:56:38 +02006748#endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Jerry Yuc2c673d2022-02-17 14:20:39 +08006749
6750#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006751MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006752static int ssl_write_hello_request(mbedtls_ssl_context *ssl);
Jerry Yuc2c673d2022-02-17 14:20:39 +08006753
6754#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006755int mbedtls_ssl_resend_hello_request(mbedtls_ssl_context *ssl)
Jerry Yuc2c673d2022-02-17 14:20:39 +08006756{
6757 /* If renegotiation is not enforced, retransmit until we would reach max
6758 * timeout if we were using the usual handshake doubling scheme */
Gilles Peskine449bd832023-01-11 14:50:10 +01006759 if (ssl->conf->renego_max_records < 0) {
Jerry Yuc2c673d2022-02-17 14:20:39 +08006760 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
6761 unsigned char doublings = 1;
6762
Gilles Peskine449bd832023-01-11 14:50:10 +01006763 while (ratio != 0) {
Jerry Yuc2c673d2022-02-17 14:20:39 +08006764 ++doublings;
6765 ratio >>= 1;
6766 }
6767
Gilles Peskine449bd832023-01-11 14:50:10 +01006768 if (++ssl->renego_records_seen > doublings) {
6769 MBEDTLS_SSL_DEBUG_MSG(2, ("no longer retransmitting hello request"));
6770 return 0;
Jerry Yuc2c673d2022-02-17 14:20:39 +08006771 }
6772 }
6773
Gilles Peskine449bd832023-01-11 14:50:10 +01006774 return ssl_write_hello_request(ssl);
Jerry Yuc2c673d2022-02-17 14:20:39 +08006775}
6776#endif
6777#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Jerry Yud9526692022-02-17 14:23:47 +08006778
Jerry Yud9526692022-02-17 14:23:47 +08006779/*
6780 * Handshake functions
6781 */
6782#if !defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
6783/* No certificate support -> dummy functions */
Gilles Peskine449bd832023-01-11 14:50:10 +01006784int mbedtls_ssl_write_certificate(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08006785{
6786 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6787 ssl->handshake->ciphersuite_info;
6788
Gilles Peskine449bd832023-01-11 14:50:10 +01006789 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006790
Gilles Peskine449bd832023-01-11 14:50:10 +01006791 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
6792 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006793 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01006794 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08006795 }
6796
Gilles Peskine449bd832023-01-11 14:50:10 +01006797 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
6798 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08006799}
6800
Gilles Peskine449bd832023-01-11 14:50:10 +01006801int mbedtls_ssl_parse_certificate(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08006802{
6803 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6804 ssl->handshake->ciphersuite_info;
6805
Gilles Peskine449bd832023-01-11 14:50:10 +01006806 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006807
Gilles Peskine449bd832023-01-11 14:50:10 +01006808 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
6809 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006810 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01006811 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08006812 }
6813
Gilles Peskine449bd832023-01-11 14:50:10 +01006814 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
6815 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08006816}
6817
6818#else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
6819/* Some certificate support -> implement write and parse */
6820
Gilles Peskine449bd832023-01-11 14:50:10 +01006821int mbedtls_ssl_write_certificate(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08006822{
6823 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
6824 size_t i, n;
6825 const mbedtls_x509_crt *crt;
6826 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6827 ssl->handshake->ciphersuite_info;
6828
Gilles Peskine449bd832023-01-11 14:50:10 +01006829 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006830
Gilles Peskine449bd832023-01-11 14:50:10 +01006831 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
6832 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006833 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01006834 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08006835 }
6836
6837#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01006838 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
6839 if (ssl->handshake->client_auth == 0) {
6840 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006841 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01006842 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08006843 }
6844 }
6845#endif /* MBEDTLS_SSL_CLI_C */
6846#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01006847 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
6848 if (mbedtls_ssl_own_cert(ssl) == NULL) {
Jerry Yud9526692022-02-17 14:23:47 +08006849 /* Should never happen because we shouldn't have picked the
6850 * ciphersuite if we don't have a certificate. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006851 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08006852 }
6853 }
6854#endif
6855
Gilles Peskine449bd832023-01-11 14:50:10 +01006856 MBEDTLS_SSL_DEBUG_CRT(3, "own certificate", mbedtls_ssl_own_cert(ssl));
Jerry Yud9526692022-02-17 14:23:47 +08006857
6858 /*
6859 * 0 . 0 handshake type
6860 * 1 . 3 handshake length
6861 * 4 . 6 length of all certs
6862 * 7 . 9 length of cert. 1
6863 * 10 . n-1 peer certificate
6864 * n . n+2 length of cert. 2
6865 * n+3 . ... upper level cert, etc.
6866 */
6867 i = 7;
Gilles Peskine449bd832023-01-11 14:50:10 +01006868 crt = mbedtls_ssl_own_cert(ssl);
Jerry Yud9526692022-02-17 14:23:47 +08006869
Gilles Peskine449bd832023-01-11 14:50:10 +01006870 while (crt != NULL) {
Jerry Yud9526692022-02-17 14:23:47 +08006871 n = crt->raw.len;
Gilles Peskine449bd832023-01-11 14:50:10 +01006872 if (n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i) {
6873 MBEDTLS_SSL_DEBUG_MSG(1, ("certificate too large, %" MBEDTLS_PRINTF_SIZET
6874 " > %" MBEDTLS_PRINTF_SIZET,
6875 i + 3 + n, (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN));
6876 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
Jerry Yud9526692022-02-17 14:23:47 +08006877 }
6878
Gilles Peskine449bd832023-01-11 14:50:10 +01006879 ssl->out_msg[i] = MBEDTLS_BYTE_2(n);
6880 ssl->out_msg[i + 1] = MBEDTLS_BYTE_1(n);
6881 ssl->out_msg[i + 2] = MBEDTLS_BYTE_0(n);
Jerry Yud9526692022-02-17 14:23:47 +08006882
Gilles Peskine449bd832023-01-11 14:50:10 +01006883 i += 3; memcpy(ssl->out_msg + i, crt->raw.p, n);
Jerry Yud9526692022-02-17 14:23:47 +08006884 i += n; crt = crt->next;
6885 }
6886
Gilles Peskine449bd832023-01-11 14:50:10 +01006887 ssl->out_msg[4] = MBEDTLS_BYTE_2(i - 7);
6888 ssl->out_msg[5] = MBEDTLS_BYTE_1(i - 7);
6889 ssl->out_msg[6] = MBEDTLS_BYTE_0(i - 7);
Jerry Yud9526692022-02-17 14:23:47 +08006890
6891 ssl->out_msglen = i;
6892 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6893 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
6894
6895 ssl->state++;
6896
Gilles Peskine449bd832023-01-11 14:50:10 +01006897 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
6898 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
6899 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08006900 }
6901
Gilles Peskine449bd832023-01-11 14:50:10 +01006902 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006903
Gilles Peskine449bd832023-01-11 14:50:10 +01006904 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08006905}
6906
6907#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6908
6909#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006910MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006911static int ssl_check_peer_crt_unchanged(mbedtls_ssl_context *ssl,
6912 unsigned char *crt_buf,
6913 size_t crt_buf_len)
Jerry Yud9526692022-02-17 14:23:47 +08006914{
6915 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6916
Gilles Peskine449bd832023-01-11 14:50:10 +01006917 if (peer_crt == NULL) {
6918 return -1;
6919 }
Jerry Yud9526692022-02-17 14:23:47 +08006920
Gilles Peskine449bd832023-01-11 14:50:10 +01006921 if (peer_crt->raw.len != crt_buf_len) {
6922 return -1;
6923 }
Jerry Yud9526692022-02-17 14:23:47 +08006924
Gilles Peskine449bd832023-01-11 14:50:10 +01006925 return memcmp(peer_crt->raw.p, crt_buf, peer_crt->raw.len);
Jerry Yud9526692022-02-17 14:23:47 +08006926}
6927#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006928MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006929static int ssl_check_peer_crt_unchanged(mbedtls_ssl_context *ssl,
6930 unsigned char *crt_buf,
6931 size_t crt_buf_len)
Jerry Yud9526692022-02-17 14:23:47 +08006932{
6933 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6934 unsigned char const * const peer_cert_digest =
6935 ssl->session->peer_cert_digest;
6936 mbedtls_md_type_t const peer_cert_digest_type =
6937 ssl->session->peer_cert_digest_type;
6938 mbedtls_md_info_t const * const digest_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01006939 mbedtls_md_info_from_type(peer_cert_digest_type);
Jerry Yud9526692022-02-17 14:23:47 +08006940 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6941 size_t digest_len;
6942
Gilles Peskine449bd832023-01-11 14:50:10 +01006943 if (peer_cert_digest == NULL || digest_info == NULL) {
6944 return -1;
6945 }
Jerry Yud9526692022-02-17 14:23:47 +08006946
Gilles Peskine449bd832023-01-11 14:50:10 +01006947 digest_len = mbedtls_md_get_size(digest_info);
6948 if (digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN) {
6949 return -1;
6950 }
Jerry Yud9526692022-02-17 14:23:47 +08006951
Gilles Peskine449bd832023-01-11 14:50:10 +01006952 ret = mbedtls_md(digest_info, crt_buf, crt_buf_len, tmp_digest);
6953 if (ret != 0) {
6954 return -1;
6955 }
Jerry Yud9526692022-02-17 14:23:47 +08006956
Gilles Peskine449bd832023-01-11 14:50:10 +01006957 return memcmp(tmp_digest, peer_cert_digest, digest_len);
Jerry Yud9526692022-02-17 14:23:47 +08006958}
6959#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6960#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6961
6962/*
6963 * Once the certificate message is read, parse it into a cert chain and
6964 * perform basic checks, but leave actual verification to the caller
6965 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006966MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006967static int ssl_parse_certificate_chain(mbedtls_ssl_context *ssl,
6968 mbedtls_x509_crt *chain)
Jerry Yud9526692022-02-17 14:23:47 +08006969{
6970 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6971#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01006972 int crt_cnt = 0;
Jerry Yud9526692022-02-17 14:23:47 +08006973#endif
6974 size_t i, n;
6975 uint8_t alert;
6976
Gilles Peskine449bd832023-01-11 14:50:10 +01006977 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
6978 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
6979 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6980 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
6981 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Jerry Yud9526692022-02-17 14:23:47 +08006982 }
6983
Gilles Peskine449bd832023-01-11 14:50:10 +01006984 if (ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE) {
6985 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6986 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
6987 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Jerry Yud9526692022-02-17 14:23:47 +08006988 }
6989
Gilles Peskine449bd832023-01-11 14:50:10 +01006990 if (ssl->in_hslen < mbedtls_ssl_hs_hdr_len(ssl) + 3 + 3) {
6991 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
6992 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6993 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
6994 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08006995 }
6996
Gilles Peskine449bd832023-01-11 14:50:10 +01006997 i = mbedtls_ssl_hs_hdr_len(ssl);
Jerry Yud9526692022-02-17 14:23:47 +08006998
6999 /*
7000 * Same message structure as in mbedtls_ssl_write_certificate()
7001 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007002 n = (ssl->in_msg[i+1] << 8) | ssl->in_msg[i+2];
Jerry Yud9526692022-02-17 14:23:47 +08007003
Gilles Peskine449bd832023-01-11 14:50:10 +01007004 if (ssl->in_msg[i] != 0 ||
7005 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len(ssl)) {
7006 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7007 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7008 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
7009 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007010 }
7011
7012 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
7013 i += 3;
7014
7015 /* Iterate through and parse the CRTs in the provided chain. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007016 while (i < ssl->in_hslen) {
Jerry Yud9526692022-02-17 14:23:47 +08007017 /* Check that there's room for the next CRT's length fields. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007018 if (i + 3 > ssl->in_hslen) {
7019 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7020 mbedtls_ssl_send_alert_message(ssl,
7021 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7022 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
7023 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007024 }
7025 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
7026 * anything beyond 2**16 ~ 64K. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007027 if (ssl->in_msg[i] != 0) {
7028 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7029 mbedtls_ssl_send_alert_message(ssl,
7030 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7031 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT);
7032 return MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Jerry Yud9526692022-02-17 14:23:47 +08007033 }
7034
7035 /* Read length of the next CRT in the chain. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007036 n = ((unsigned int) ssl->in_msg[i + 1] << 8)
Jerry Yud9526692022-02-17 14:23:47 +08007037 | (unsigned int) ssl->in_msg[i + 2];
7038 i += 3;
7039
Gilles Peskine449bd832023-01-11 14:50:10 +01007040 if (n < 128 || i + n > ssl->in_hslen) {
7041 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7042 mbedtls_ssl_send_alert_message(ssl,
7043 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7044 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
7045 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007046 }
7047
7048 /* Check if we're handling the first CRT in the chain. */
7049#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007050 if (crt_cnt++ == 0 &&
Jerry Yud9526692022-02-17 14:23:47 +08007051 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Gilles Peskine449bd832023-01-11 14:50:10 +01007052 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
Jerry Yud9526692022-02-17 14:23:47 +08007053 /* During client-side renegotiation, check that the server's
7054 * end-CRTs hasn't changed compared to the initial handshake,
7055 * mitigating the triple handshake attack. On success, reuse
7056 * the original end-CRT instead of parsing it again. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007057 MBEDTLS_SSL_DEBUG_MSG(3, ("Check that peer CRT hasn't changed during renegotiation"));
7058 if (ssl_check_peer_crt_unchanged(ssl,
7059 &ssl->in_msg[i],
7060 n) != 0) {
7061 MBEDTLS_SSL_DEBUG_MSG(1, ("new server cert during renegotiation"));
7062 mbedtls_ssl_send_alert_message(ssl,
7063 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7064 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED);
7065 return MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Jerry Yud9526692022-02-17 14:23:47 +08007066 }
7067
7068 /* Now we can safely free the original chain. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007069 ssl_clear_peer_cert(ssl->session);
Jerry Yud9526692022-02-17 14:23:47 +08007070 }
7071#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
7072
7073 /* Parse the next certificate in the chain. */
7074#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007075 ret = mbedtls_x509_crt_parse_der(chain, ssl->in_msg + i, n);
Jerry Yud9526692022-02-17 14:23:47 +08007076#else
7077 /* If we don't need to store the CRT chain permanently, parse
7078 * it in-place from the input buffer instead of making a copy. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007079 ret = mbedtls_x509_crt_parse_der_nocopy(chain, ssl->in_msg + i, n);
Jerry Yud9526692022-02-17 14:23:47 +08007080#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007081 switch (ret) {
Jerry Yud9526692022-02-17 14:23:47 +08007082 case 0: /*ok*/
7083 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
7084 /* Ignore certificate with an unknown algorithm: maybe a
7085 prior certificate was already trusted. */
7086 break;
7087
7088 case MBEDTLS_ERR_X509_ALLOC_FAILED:
7089 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
7090 goto crt_parse_der_failed;
7091
7092 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
7093 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7094 goto crt_parse_der_failed;
7095
7096 default:
7097 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007098crt_parse_der_failed:
7099 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert);
7100 MBEDTLS_SSL_DEBUG_RET(1, " mbedtls_x509_crt_parse_der", ret);
7101 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007102 }
7103
7104 i += n;
7105 }
7106
Gilles Peskine449bd832023-01-11 14:50:10 +01007107 MBEDTLS_SSL_DEBUG_CRT(3, "peer certificate", chain);
7108 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08007109}
7110
7111#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007112MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007113static int ssl_srv_check_client_no_crt_notification(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08007114{
Gilles Peskine449bd832023-01-11 14:50:10 +01007115 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
7116 return -1;
7117 }
Jerry Yud9526692022-02-17 14:23:47 +08007118
Gilles Peskine449bd832023-01-11 14:50:10 +01007119 if (ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len(ssl) &&
Jerry Yud9526692022-02-17 14:23:47 +08007120 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
7121 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
Gilles Peskine449bd832023-01-11 14:50:10 +01007122 memcmp(ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl), "\0\0\0", 3) == 0) {
7123 MBEDTLS_SSL_DEBUG_MSG(1, ("peer has no certificate"));
7124 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08007125 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007126 return -1;
Jerry Yud9526692022-02-17 14:23:47 +08007127}
7128#endif /* MBEDTLS_SSL_SRV_C */
7129
7130/* Check if a certificate message is expected.
7131 * Return either
7132 * - SSL_CERTIFICATE_EXPECTED, or
7133 * - SSL_CERTIFICATE_SKIP
7134 * indicating whether a Certificate message is expected or not.
7135 */
7136#define SSL_CERTIFICATE_EXPECTED 0
7137#define SSL_CERTIFICATE_SKIP 1
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007138MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007139static int ssl_parse_certificate_coordinate(mbedtls_ssl_context *ssl,
7140 int authmode)
Jerry Yud9526692022-02-17 14:23:47 +08007141{
7142 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
7143 ssl->handshake->ciphersuite_info;
7144
Gilles Peskine449bd832023-01-11 14:50:10 +01007145 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
7146 return SSL_CERTIFICATE_SKIP;
7147 }
Jerry Yud9526692022-02-17 14:23:47 +08007148
7149#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007150 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
7151 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK) {
7152 return SSL_CERTIFICATE_SKIP;
7153 }
Jerry Yud9526692022-02-17 14:23:47 +08007154
Gilles Peskine449bd832023-01-11 14:50:10 +01007155 if (authmode == MBEDTLS_SSL_VERIFY_NONE) {
Jerry Yud9526692022-02-17 14:23:47 +08007156 ssl->session_negotiate->verify_result =
7157 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
Gilles Peskine449bd832023-01-11 14:50:10 +01007158 return SSL_CERTIFICATE_SKIP;
Jerry Yud9526692022-02-17 14:23:47 +08007159 }
7160 }
7161#else
7162 ((void) authmode);
7163#endif /* MBEDTLS_SSL_SRV_C */
7164
Gilles Peskine449bd832023-01-11 14:50:10 +01007165 return SSL_CERTIFICATE_EXPECTED;
Jerry Yud9526692022-02-17 14:23:47 +08007166}
7167
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007168MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007169static int ssl_parse_certificate_verify(mbedtls_ssl_context *ssl,
7170 int authmode,
7171 mbedtls_x509_crt *chain,
7172 void *rs_ctx)
Jerry Yud9526692022-02-17 14:23:47 +08007173{
7174 int ret = 0;
7175 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
7176 ssl->handshake->ciphersuite_info;
7177 int have_ca_chain = 0;
7178
7179 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
7180 void *p_vrfy;
7181
Gilles Peskine449bd832023-01-11 14:50:10 +01007182 if (authmode == MBEDTLS_SSL_VERIFY_NONE) {
7183 return 0;
7184 }
Jerry Yud9526692022-02-17 14:23:47 +08007185
Gilles Peskine449bd832023-01-11 14:50:10 +01007186 if (ssl->f_vrfy != NULL) {
7187 MBEDTLS_SSL_DEBUG_MSG(3, ("Use context-specific verification callback"));
Jerry Yud9526692022-02-17 14:23:47 +08007188 f_vrfy = ssl->f_vrfy;
7189 p_vrfy = ssl->p_vrfy;
Gilles Peskine449bd832023-01-11 14:50:10 +01007190 } else {
7191 MBEDTLS_SSL_DEBUG_MSG(3, ("Use configuration-specific verification callback"));
Jerry Yud9526692022-02-17 14:23:47 +08007192 f_vrfy = ssl->conf->f_vrfy;
7193 p_vrfy = ssl->conf->p_vrfy;
7194 }
7195
7196 /*
7197 * Main check: verify certificate
7198 */
7199#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Gilles Peskine449bd832023-01-11 14:50:10 +01007200 if (ssl->conf->f_ca_cb != NULL) {
Jerry Yud9526692022-02-17 14:23:47 +08007201 ((void) rs_ctx);
7202 have_ca_chain = 1;
7203
Gilles Peskine449bd832023-01-11 14:50:10 +01007204 MBEDTLS_SSL_DEBUG_MSG(3, ("use CA callback for X.509 CRT verification"));
Jerry Yud9526692022-02-17 14:23:47 +08007205 ret = mbedtls_x509_crt_verify_with_ca_cb(
7206 chain,
7207 ssl->conf->f_ca_cb,
7208 ssl->conf->p_ca_cb,
7209 ssl->conf->cert_profile,
7210 ssl->hostname,
7211 &ssl->session_negotiate->verify_result,
Gilles Peskine449bd832023-01-11 14:50:10 +01007212 f_vrfy, p_vrfy);
7213 } else
Jerry Yud9526692022-02-17 14:23:47 +08007214#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
7215 {
7216 mbedtls_x509_crt *ca_chain;
7217 mbedtls_x509_crl *ca_crl;
7218
7219#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01007220 if (ssl->handshake->sni_ca_chain != NULL) {
Jerry Yud9526692022-02-17 14:23:47 +08007221 ca_chain = ssl->handshake->sni_ca_chain;
7222 ca_crl = ssl->handshake->sni_ca_crl;
Gilles Peskine449bd832023-01-11 14:50:10 +01007223 } else
Jerry Yud9526692022-02-17 14:23:47 +08007224#endif
7225 {
7226 ca_chain = ssl->conf->ca_chain;
7227 ca_crl = ssl->conf->ca_crl;
7228 }
7229
Gilles Peskine449bd832023-01-11 14:50:10 +01007230 if (ca_chain != NULL) {
Jerry Yud9526692022-02-17 14:23:47 +08007231 have_ca_chain = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01007232 }
Jerry Yud9526692022-02-17 14:23:47 +08007233
7234 ret = mbedtls_x509_crt_verify_restartable(
7235 chain,
7236 ca_chain, ca_crl,
7237 ssl->conf->cert_profile,
7238 ssl->hostname,
7239 &ssl->session_negotiate->verify_result,
Gilles Peskine449bd832023-01-11 14:50:10 +01007240 f_vrfy, p_vrfy, rs_ctx);
Jerry Yud9526692022-02-17 14:23:47 +08007241 }
7242
Gilles Peskine449bd832023-01-11 14:50:10 +01007243 if (ret != 0) {
7244 MBEDTLS_SSL_DEBUG_RET(1, "x509_verify_cert", ret);
Jerry Yud9526692022-02-17 14:23:47 +08007245 }
7246
7247#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007248 if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
7249 return MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
7250 }
Jerry Yud9526692022-02-17 14:23:47 +08007251#endif
7252
7253 /*
7254 * Secondary checks: always done, but change 'ret' only if it was 0
7255 */
7256
7257#if defined(MBEDTLS_ECP_C)
7258 {
7259 const mbedtls_pk_context *pk = &chain->pk;
7260
Manuel Pégourié-Gonnard66b0d612022-06-17 10:49:29 +02007261 /* If certificate uses an EC key, make sure the curve is OK.
7262 * This is a public key, so it can't be opaque, so can_do() is a good
7263 * enough check to ensure pk_ec() is safe to use here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007264 if (mbedtls_pk_can_do(pk, MBEDTLS_PK_ECKEY)) {
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007265 /* and in the unlikely case the above assumption no longer holds
7266 * we are making sure that pk_ec() here does not return a NULL
7267 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007268 const mbedtls_ecp_keypair *ec = mbedtls_pk_ec(*pk);
7269 if (ec == NULL) {
7270 MBEDTLS_SSL_DEBUG_MSG(1, ("mbedtls_pk_ec() returned NULL"));
7271 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007272 }
Jerry Yud9526692022-02-17 14:23:47 +08007273
Gilles Peskine449bd832023-01-11 14:50:10 +01007274 if (mbedtls_ssl_check_curve(ssl, ec->grp.id) != 0) {
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007275 ssl->session_negotiate->verify_result |=
7276 MBEDTLS_X509_BADCERT_BAD_KEY;
7277
Gilles Peskine449bd832023-01-11 14:50:10 +01007278 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate (EC key curve)"));
7279 if (ret == 0) {
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007280 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Gilles Peskine449bd832023-01-11 14:50:10 +01007281 }
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007282 }
Jerry Yud9526692022-02-17 14:23:47 +08007283 }
7284 }
7285#endif /* MBEDTLS_ECP_C */
7286
Gilles Peskine449bd832023-01-11 14:50:10 +01007287 if (mbedtls_ssl_check_cert_usage(chain,
7288 ciphersuite_info,
7289 !ssl->conf->endpoint,
7290 &ssl->session_negotiate->verify_result) != 0) {
7291 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate (usage extensions)"));
7292 if (ret == 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007293 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Gilles Peskine449bd832023-01-11 14:50:10 +01007294 }
Jerry Yud9526692022-02-17 14:23:47 +08007295 }
7296
7297 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
7298 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
7299 * with details encoded in the verification flags. All other kinds
7300 * of error codes, including those from the user provided f_vrfy
7301 * functions, are treated as fatal and lead to a failure of
7302 * ssl_parse_certificate even if verification was optional. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007303 if (authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
7304 (ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
7305 ret == MBEDTLS_ERR_SSL_BAD_CERTIFICATE)) {
Jerry Yud9526692022-02-17 14:23:47 +08007306 ret = 0;
7307 }
7308
Gilles Peskine449bd832023-01-11 14:50:10 +01007309 if (have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED) {
7310 MBEDTLS_SSL_DEBUG_MSG(1, ("got no CA chain"));
Jerry Yud9526692022-02-17 14:23:47 +08007311 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
7312 }
7313
Gilles Peskine449bd832023-01-11 14:50:10 +01007314 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007315 uint8_t alert;
7316
7317 /* The certificate may have been rejected for several reasons.
7318 Pick one and send the corresponding alert. Which alert to send
7319 may be a subject of debate in some cases. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007320 if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER) {
Jerry Yud9526692022-02-17 14:23:47 +08007321 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
Gilles Peskine449bd832023-01-11 14:50:10 +01007322 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH) {
Jerry Yud9526692022-02-17 14:23:47 +08007323 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007324 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE) {
Jerry Yud9526692022-02-17 14:23:47 +08007325 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007326 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE) {
Jerry Yud9526692022-02-17 14:23:47 +08007327 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007328 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE) {
Jerry Yud9526692022-02-17 14:23:47 +08007329 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007330 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK) {
Jerry Yud9526692022-02-17 14:23:47 +08007331 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007332 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY) {
Jerry Yud9526692022-02-17 14:23:47 +08007333 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007334 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED) {
Jerry Yud9526692022-02-17 14:23:47 +08007335 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
Gilles Peskine449bd832023-01-11 14:50:10 +01007336 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED) {
Jerry Yud9526692022-02-17 14:23:47 +08007337 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
Gilles Peskine449bd832023-01-11 14:50:10 +01007338 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED) {
Jerry Yud9526692022-02-17 14:23:47 +08007339 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
Gilles Peskine449bd832023-01-11 14:50:10 +01007340 } else {
Jerry Yud9526692022-02-17 14:23:47 +08007341 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
Gilles Peskine449bd832023-01-11 14:50:10 +01007342 }
7343 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7344 alert);
Jerry Yud9526692022-02-17 14:23:47 +08007345 }
7346
7347#if defined(MBEDTLS_DEBUG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007348 if (ssl->session_negotiate->verify_result != 0) {
7349 MBEDTLS_SSL_DEBUG_MSG(3, ("! Certificate verification flags %08x",
7350 (unsigned int) ssl->session_negotiate->verify_result));
7351 } else {
7352 MBEDTLS_SSL_DEBUG_MSG(3, ("Certificate verification flags clear"));
Jerry Yud9526692022-02-17 14:23:47 +08007353 }
7354#endif /* MBEDTLS_DEBUG_C */
7355
Gilles Peskine449bd832023-01-11 14:50:10 +01007356 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007357}
7358
7359#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007360MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007361static int ssl_remember_peer_crt_digest(mbedtls_ssl_context *ssl,
7362 unsigned char *start, size_t len)
Jerry Yud9526692022-02-17 14:23:47 +08007363{
7364 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7365 /* Remember digest of the peer's end-CRT. */
7366 ssl->session_negotiate->peer_cert_digest =
Gilles Peskine449bd832023-01-11 14:50:10 +01007367 mbedtls_calloc(1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN);
7368 if (ssl->session_negotiate->peer_cert_digest == NULL) {
7369 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%d bytes) failed",
7370 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN));
7371 mbedtls_ssl_send_alert_message(ssl,
7372 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7373 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
Jerry Yud9526692022-02-17 14:23:47 +08007374
Gilles Peskine449bd832023-01-11 14:50:10 +01007375 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Jerry Yud9526692022-02-17 14:23:47 +08007376 }
7377
Gilles Peskine449bd832023-01-11 14:50:10 +01007378 ret = mbedtls_md(mbedtls_md_info_from_type(
7379 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE),
7380 start, len,
7381 ssl->session_negotiate->peer_cert_digest);
Jerry Yud9526692022-02-17 14:23:47 +08007382
7383 ssl->session_negotiate->peer_cert_digest_type =
7384 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
7385 ssl->session_negotiate->peer_cert_digest_len =
7386 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
7387
Gilles Peskine449bd832023-01-11 14:50:10 +01007388 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007389}
7390
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007391MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007392static int ssl_remember_peer_pubkey(mbedtls_ssl_context *ssl,
7393 unsigned char *start, size_t len)
Jerry Yud9526692022-02-17 14:23:47 +08007394{
7395 unsigned char *end = start + len;
7396 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7397
7398 /* Make a copy of the peer's raw public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007399 mbedtls_pk_init(&ssl->handshake->peer_pubkey);
7400 ret = mbedtls_pk_parse_subpubkey(&start, end,
7401 &ssl->handshake->peer_pubkey);
7402 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007403 /* We should have parsed the public key before. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007404 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007405 }
7406
Gilles Peskine449bd832023-01-11 14:50:10 +01007407 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08007408}
7409#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7410
Gilles Peskine449bd832023-01-11 14:50:10 +01007411int mbedtls_ssl_parse_certificate(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08007412{
7413 int ret = 0;
7414 int crt_expected;
7415#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7416 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
7417 ? ssl->handshake->sni_authmode
7418 : ssl->conf->authmode;
7419#else
7420 const int authmode = ssl->conf->authmode;
7421#endif
7422 void *rs_ctx = NULL;
7423 mbedtls_x509_crt *chain = NULL;
7424
Gilles Peskine449bd832023-01-11 14:50:10 +01007425 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007426
Gilles Peskine449bd832023-01-11 14:50:10 +01007427 crt_expected = ssl_parse_certificate_coordinate(ssl, authmode);
7428 if (crt_expected == SSL_CERTIFICATE_SKIP) {
7429 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007430 goto exit;
7431 }
7432
7433#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007434 if (ssl->handshake->ecrs_enabled &&
7435 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify) {
Jerry Yud9526692022-02-17 14:23:47 +08007436 chain = ssl->handshake->ecrs_peer_cert;
7437 ssl->handshake->ecrs_peer_cert = NULL;
7438 goto crt_verify;
7439 }
7440#endif
7441
Gilles Peskine449bd832023-01-11 14:50:10 +01007442 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007443 /* mbedtls_ssl_read_record may have sent an alert already. We
7444 let it decide whether to alert. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007445 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
Jerry Yud9526692022-02-17 14:23:47 +08007446 goto exit;
7447 }
7448
7449#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007450 if (ssl_srv_check_client_no_crt_notification(ssl) == 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007451 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
7452
Gilles Peskine449bd832023-01-11 14:50:10 +01007453 if (authmode != MBEDTLS_SSL_VERIFY_OPTIONAL) {
Jerry Yud9526692022-02-17 14:23:47 +08007454 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Gilles Peskine449bd832023-01-11 14:50:10 +01007455 }
Jerry Yud9526692022-02-17 14:23:47 +08007456
7457 goto exit;
7458 }
7459#endif /* MBEDTLS_SSL_SRV_C */
7460
7461 /* Clear existing peer CRT structure in case we tried to
7462 * reuse a session but it failed, and allocate a new one. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007463 ssl_clear_peer_cert(ssl->session_negotiate);
Jerry Yud9526692022-02-17 14:23:47 +08007464
Gilles Peskine449bd832023-01-11 14:50:10 +01007465 chain = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
7466 if (chain == NULL) {
7467 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed",
7468 sizeof(mbedtls_x509_crt)));
7469 mbedtls_ssl_send_alert_message(ssl,
7470 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7471 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
Jerry Yud9526692022-02-17 14:23:47 +08007472
7473 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
7474 goto exit;
7475 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007476 mbedtls_x509_crt_init(chain);
Jerry Yud9526692022-02-17 14:23:47 +08007477
Gilles Peskine449bd832023-01-11 14:50:10 +01007478 ret = ssl_parse_certificate_chain(ssl, chain);
7479 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007480 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007481 }
Jerry Yud9526692022-02-17 14:23:47 +08007482
7483#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007484 if (ssl->handshake->ecrs_enabled) {
Jerry Yud9526692022-02-17 14:23:47 +08007485 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Gilles Peskine449bd832023-01-11 14:50:10 +01007486 }
Jerry Yud9526692022-02-17 14:23:47 +08007487
7488crt_verify:
Gilles Peskine449bd832023-01-11 14:50:10 +01007489 if (ssl->handshake->ecrs_enabled) {
Jerry Yud9526692022-02-17 14:23:47 +08007490 rs_ctx = &ssl->handshake->ecrs_ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +01007491 }
Jerry Yud9526692022-02-17 14:23:47 +08007492#endif
7493
Gilles Peskine449bd832023-01-11 14:50:10 +01007494 ret = ssl_parse_certificate_verify(ssl, authmode,
7495 chain, rs_ctx);
7496 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007497 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007498 }
Jerry Yud9526692022-02-17 14:23:47 +08007499
7500#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7501 {
7502 unsigned char *crt_start, *pk_start;
7503 size_t crt_len, pk_len;
7504
7505 /* We parse the CRT chain without copying, so
7506 * these pointers point into the input buffer,
7507 * and are hence still valid after freeing the
7508 * CRT chain. */
7509
7510 crt_start = chain->raw.p;
7511 crt_len = chain->raw.len;
7512
7513 pk_start = chain->pk_raw.p;
7514 pk_len = chain->pk_raw.len;
7515
7516 /* Free the CRT structures before computing
7517 * digest and copying the peer's public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007518 mbedtls_x509_crt_free(chain);
7519 mbedtls_free(chain);
Jerry Yud9526692022-02-17 14:23:47 +08007520 chain = NULL;
7521
Gilles Peskine449bd832023-01-11 14:50:10 +01007522 ret = ssl_remember_peer_crt_digest(ssl, crt_start, crt_len);
7523 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007524 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007525 }
Jerry Yud9526692022-02-17 14:23:47 +08007526
Gilles Peskine449bd832023-01-11 14:50:10 +01007527 ret = ssl_remember_peer_pubkey(ssl, pk_start, pk_len);
7528 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007529 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007530 }
Jerry Yud9526692022-02-17 14:23:47 +08007531 }
7532#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7533 /* Pass ownership to session structure. */
7534 ssl->session_negotiate->peer_cert = chain;
7535 chain = NULL;
7536#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7537
Gilles Peskine449bd832023-01-11 14:50:10 +01007538 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007539
7540exit:
7541
Gilles Peskine449bd832023-01-11 14:50:10 +01007542 if (ret == 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007543 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01007544 }
Jerry Yud9526692022-02-17 14:23:47 +08007545
7546#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007547 if (ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS) {
Jerry Yud9526692022-02-17 14:23:47 +08007548 ssl->handshake->ecrs_peer_cert = chain;
7549 chain = NULL;
7550 }
7551#endif
7552
Gilles Peskine449bd832023-01-11 14:50:10 +01007553 if (chain != NULL) {
7554 mbedtls_x509_crt_free(chain);
7555 mbedtls_free(chain);
Jerry Yud9526692022-02-17 14:23:47 +08007556 }
7557
Gilles Peskine449bd832023-01-11 14:50:10 +01007558 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007559}
7560#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
7561
Andrzej Kurek25f27152022-08-17 16:09:31 -04007562#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01007563static int ssl_calc_finished_tls_sha256(
Gilles Peskine449bd832023-01-11 14:50:10 +01007564 mbedtls_ssl_context *ssl, unsigned char *buf, int from)
Jerry Yu615bd6f2022-02-17 14:25:15 +08007565{
7566 int len = 12;
7567 const char *sender;
7568 unsigned char padbuf[32];
7569#if defined(MBEDTLS_USE_PSA_CRYPTO)
7570 size_t hash_size;
7571 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
7572 psa_status_t status;
7573#else
7574 mbedtls_sha256_context sha256;
7575#endif
7576
7577 mbedtls_ssl_session *session = ssl->session_negotiate;
Gilles Peskine449bd832023-01-11 14:50:10 +01007578 if (!session) {
Jerry Yu615bd6f2022-02-17 14:25:15 +08007579 session = ssl->session;
Gilles Peskine449bd832023-01-11 14:50:10 +01007580 }
Jerry Yu615bd6f2022-02-17 14:25:15 +08007581
Gilles Peskine449bd832023-01-11 14:50:10 +01007582 sender = (from == MBEDTLS_SSL_IS_CLIENT)
Jerry Yu615bd6f2022-02-17 14:25:15 +08007583 ? "client finished"
7584 : "server finished";
7585
7586#if defined(MBEDTLS_USE_PSA_CRYPTO)
7587 sha256_psa = psa_hash_operation_init();
7588
Gilles Peskine449bd832023-01-11 14:50:10 +01007589 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc PSA finished tls sha256"));
Jerry Yu615bd6f2022-02-17 14:25:15 +08007590
Gilles Peskine449bd832023-01-11 14:50:10 +01007591 status = psa_hash_clone(&ssl->handshake->fin_sha256_psa, &sha256_psa);
7592 if (status != PSA_SUCCESS) {
7593 MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash clone failed"));
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01007594 return 0;
Jerry Yu615bd6f2022-02-17 14:25:15 +08007595 }
7596
Gilles Peskine449bd832023-01-11 14:50:10 +01007597 status = psa_hash_finish(&sha256_psa, padbuf, sizeof(padbuf), &hash_size);
7598 if (status != PSA_SUCCESS) {
7599 MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash finish failed"));
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01007600 return 0;
Jerry Yu615bd6f2022-02-17 14:25:15 +08007601 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007602 MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated padbuf", padbuf, 32);
Jerry Yu615bd6f2022-02-17 14:25:15 +08007603#else
7604
Gilles Peskine449bd832023-01-11 14:50:10 +01007605 mbedtls_sha256_init(&sha256);
Jerry Yu615bd6f2022-02-17 14:25:15 +08007606
Gilles Peskine449bd832023-01-11 14:50:10 +01007607 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc finished tls sha256"));
Jerry Yu615bd6f2022-02-17 14:25:15 +08007608
Gilles Peskine449bd832023-01-11 14:50:10 +01007609 mbedtls_sha256_clone(&sha256, &ssl->handshake->fin_sha256);
Jerry Yu615bd6f2022-02-17 14:25:15 +08007610
7611 /*
7612 * TLSv1.2:
7613 * hash = PRF( master, finished_label,
7614 * Hash( handshake ) )[0.11]
7615 */
7616
7617#if !defined(MBEDTLS_SHA256_ALT)
Gilles Peskine449bd832023-01-11 14:50:10 +01007618 MBEDTLS_SSL_DEBUG_BUF(4, "finished sha2 state", (unsigned char *)
7619 sha256.state, sizeof(sha256.state));
Jerry Yu615bd6f2022-02-17 14:25:15 +08007620#endif
7621
Gilles Peskine449bd832023-01-11 14:50:10 +01007622 mbedtls_sha256_finish(&sha256, padbuf);
7623 mbedtls_sha256_free(&sha256);
Jerry Yu615bd6f2022-02-17 14:25:15 +08007624#endif /* MBEDTLS_USE_PSA_CRYPTO */
7625
Gilles Peskine449bd832023-01-11 14:50:10 +01007626 ssl->handshake->tls_prf(session->master, 48, sender,
7627 padbuf, 32, buf, len);
Jerry Yu615bd6f2022-02-17 14:25:15 +08007628
Gilles Peskine449bd832023-01-11 14:50:10 +01007629 MBEDTLS_SSL_DEBUG_BUF(3, "calc finished result", buf, len);
Jerry Yu615bd6f2022-02-17 14:25:15 +08007630
Gilles Peskine449bd832023-01-11 14:50:10 +01007631 mbedtls_platform_zeroize(padbuf, sizeof(padbuf));
Jerry Yu615bd6f2022-02-17 14:25:15 +08007632
Gilles Peskine449bd832023-01-11 14:50:10 +01007633 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc finished"));
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01007634 return 0;
Jerry Yu615bd6f2022-02-17 14:25:15 +08007635}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04007636#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yu615bd6f2022-02-17 14:25:15 +08007637
Jerry Yub7ba49e2022-02-17 14:25:53 +08007638
Andrzej Kurek25f27152022-08-17 16:09:31 -04007639#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01007640static int ssl_calc_finished_tls_sha384(
Gilles Peskine449bd832023-01-11 14:50:10 +01007641 mbedtls_ssl_context *ssl, unsigned char *buf, int from)
Jerry Yub7ba49e2022-02-17 14:25:53 +08007642{
7643 int len = 12;
7644 const char *sender;
7645 unsigned char padbuf[48];
7646#if defined(MBEDTLS_USE_PSA_CRYPTO)
7647 size_t hash_size;
7648 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
7649 psa_status_t status;
7650#else
7651 mbedtls_sha512_context sha512;
7652#endif
7653
7654 mbedtls_ssl_session *session = ssl->session_negotiate;
Gilles Peskine449bd832023-01-11 14:50:10 +01007655 if (!session) {
Jerry Yub7ba49e2022-02-17 14:25:53 +08007656 session = ssl->session;
Gilles Peskine449bd832023-01-11 14:50:10 +01007657 }
Jerry Yub7ba49e2022-02-17 14:25:53 +08007658
Gilles Peskine449bd832023-01-11 14:50:10 +01007659 sender = (from == MBEDTLS_SSL_IS_CLIENT)
Jerry Yub7ba49e2022-02-17 14:25:53 +08007660 ? "client finished"
7661 : "server finished";
7662
7663#if defined(MBEDTLS_USE_PSA_CRYPTO)
7664 sha384_psa = psa_hash_operation_init();
7665
Gilles Peskine449bd832023-01-11 14:50:10 +01007666 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc PSA finished tls sha384"));
Jerry Yub7ba49e2022-02-17 14:25:53 +08007667
Gilles Peskine449bd832023-01-11 14:50:10 +01007668 status = psa_hash_clone(&ssl->handshake->fin_sha384_psa, &sha384_psa);
7669 if (status != PSA_SUCCESS) {
7670 MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash clone failed"));
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01007671 return 0;
Jerry Yub7ba49e2022-02-17 14:25:53 +08007672 }
7673
Gilles Peskine449bd832023-01-11 14:50:10 +01007674 status = psa_hash_finish(&sha384_psa, padbuf, sizeof(padbuf), &hash_size);
7675 if (status != PSA_SUCCESS) {
7676 MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash finish failed"));
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01007677 return 0;
Jerry Yub7ba49e2022-02-17 14:25:53 +08007678 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007679 MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated padbuf", padbuf, 48);
Jerry Yub7ba49e2022-02-17 14:25:53 +08007680#else
Gilles Peskine449bd832023-01-11 14:50:10 +01007681 mbedtls_sha512_init(&sha512);
Jerry Yub7ba49e2022-02-17 14:25:53 +08007682
Gilles Peskine449bd832023-01-11 14:50:10 +01007683 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc finished tls sha384"));
Jerry Yub7ba49e2022-02-17 14:25:53 +08007684
Gilles Peskine449bd832023-01-11 14:50:10 +01007685 mbedtls_sha512_clone(&sha512, &ssl->handshake->fin_sha384);
Jerry Yub7ba49e2022-02-17 14:25:53 +08007686
7687 /*
7688 * TLSv1.2:
7689 * hash = PRF( master, finished_label,
7690 * Hash( handshake ) )[0.11]
7691 */
7692
7693#if !defined(MBEDTLS_SHA512_ALT)
Gilles Peskine449bd832023-01-11 14:50:10 +01007694 MBEDTLS_SSL_DEBUG_BUF(4, "finished sha512 state", (unsigned char *)
7695 sha512.state, sizeof(sha512.state));
Jerry Yub7ba49e2022-02-17 14:25:53 +08007696#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01007697 mbedtls_sha512_finish(&sha512, padbuf);
Jerry Yub7ba49e2022-02-17 14:25:53 +08007698
Gilles Peskine449bd832023-01-11 14:50:10 +01007699 mbedtls_sha512_free(&sha512);
Jerry Yub7ba49e2022-02-17 14:25:53 +08007700#endif
7701
Gilles Peskine449bd832023-01-11 14:50:10 +01007702 ssl->handshake->tls_prf(session->master, 48, sender,
7703 padbuf, 48, buf, len);
Jerry Yub7ba49e2022-02-17 14:25:53 +08007704
Gilles Peskine449bd832023-01-11 14:50:10 +01007705 MBEDTLS_SSL_DEBUG_BUF(3, "calc finished result", buf, len);
Jerry Yub7ba49e2022-02-17 14:25:53 +08007706
Gilles Peskine449bd832023-01-11 14:50:10 +01007707 mbedtls_platform_zeroize(padbuf, sizeof(padbuf));
Jerry Yub7ba49e2022-02-17 14:25:53 +08007708
Gilles Peskine449bd832023-01-11 14:50:10 +01007709 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc finished"));
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01007710 return 0;
Jerry Yub7ba49e2022-02-17 14:25:53 +08007711}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04007712#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yub7ba49e2022-02-17 14:25:53 +08007713
Gilles Peskine449bd832023-01-11 14:50:10 +01007714void mbedtls_ssl_handshake_wrapup_free_hs_transform(mbedtls_ssl_context *ssl)
Jerry Yuaef00152022-02-17 14:27:31 +08007715{
Gilles Peskine449bd832023-01-11 14:50:10 +01007716 MBEDTLS_SSL_DEBUG_MSG(3, ("=> handshake wrapup: final free"));
Jerry Yuaef00152022-02-17 14:27:31 +08007717
7718 /*
7719 * Free our handshake params
7720 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007721 mbedtls_ssl_handshake_free(ssl);
7722 mbedtls_free(ssl->handshake);
Jerry Yuaef00152022-02-17 14:27:31 +08007723 ssl->handshake = NULL;
7724
7725 /*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08007726 * Free the previous transform and switch in the current one
Jerry Yuaef00152022-02-17 14:27:31 +08007727 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007728 if (ssl->transform) {
7729 mbedtls_ssl_transform_free(ssl->transform);
7730 mbedtls_free(ssl->transform);
Jerry Yuaef00152022-02-17 14:27:31 +08007731 }
7732 ssl->transform = ssl->transform_negotiate;
7733 ssl->transform_negotiate = NULL;
7734
Gilles Peskine449bd832023-01-11 14:50:10 +01007735 MBEDTLS_SSL_DEBUG_MSG(3, ("<= handshake wrapup: final free"));
Jerry Yuaef00152022-02-17 14:27:31 +08007736}
7737
Gilles Peskine449bd832023-01-11 14:50:10 +01007738void mbedtls_ssl_handshake_wrapup(mbedtls_ssl_context *ssl)
Jerry Yu2a9fff52022-02-17 14:28:51 +08007739{
7740 int resume = ssl->handshake->resume;
7741
Gilles Peskine449bd832023-01-11 14:50:10 +01007742 MBEDTLS_SSL_DEBUG_MSG(3, ("=> handshake wrapup"));
Jerry Yu2a9fff52022-02-17 14:28:51 +08007743
7744#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01007745 if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
Jerry Yu2a9fff52022-02-17 14:28:51 +08007746 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
7747 ssl->renego_records_seen = 0;
7748 }
7749#endif
7750
7751 /*
7752 * Free the previous session and switch in the current one
7753 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007754 if (ssl->session) {
Jerry Yu2a9fff52022-02-17 14:28:51 +08007755#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
7756 /* RFC 7366 3.1: keep the EtM state */
7757 ssl->session_negotiate->encrypt_then_mac =
Gilles Peskine449bd832023-01-11 14:50:10 +01007758 ssl->session->encrypt_then_mac;
Jerry Yu2a9fff52022-02-17 14:28:51 +08007759#endif
7760
Gilles Peskine449bd832023-01-11 14:50:10 +01007761 mbedtls_ssl_session_free(ssl->session);
7762 mbedtls_free(ssl->session);
Jerry Yu2a9fff52022-02-17 14:28:51 +08007763 }
7764 ssl->session = ssl->session_negotiate;
7765 ssl->session_negotiate = NULL;
7766
7767 /*
7768 * Add cache entry
7769 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007770 if (ssl->conf->f_set_cache != NULL &&
Jerry Yu2a9fff52022-02-17 14:28:51 +08007771 ssl->session->id_len != 0 &&
Gilles Peskine449bd832023-01-11 14:50:10 +01007772 resume == 0) {
7773 if (ssl->conf->f_set_cache(ssl->conf->p_cache,
7774 ssl->session->id,
7775 ssl->session->id_len,
7776 ssl->session) != 0) {
7777 MBEDTLS_SSL_DEBUG_MSG(1, ("cache did not store session"));
7778 }
Jerry Yu2a9fff52022-02-17 14:28:51 +08007779 }
7780
7781#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007782 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7783 ssl->handshake->flight != NULL) {
Jerry Yu2a9fff52022-02-17 14:28:51 +08007784 /* Cancel handshake timer */
Gilles Peskine449bd832023-01-11 14:50:10 +01007785 mbedtls_ssl_set_timer(ssl, 0);
Jerry Yu2a9fff52022-02-17 14:28:51 +08007786
7787 /* Keep last flight around in case we need to resend it:
7788 * we need the handshake and transform structures for that */
Gilles Peskine449bd832023-01-11 14:50:10 +01007789 MBEDTLS_SSL_DEBUG_MSG(3, ("skip freeing handshake and transform"));
7790 } else
Jerry Yu2a9fff52022-02-17 14:28:51 +08007791#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01007792 mbedtls_ssl_handshake_wrapup_free_hs_transform(ssl);
Jerry Yu2a9fff52022-02-17 14:28:51 +08007793
Jerry Yu5ed73ff2022-10-27 13:08:42 +08007794 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
Jerry Yu2a9fff52022-02-17 14:28:51 +08007795
Gilles Peskine449bd832023-01-11 14:50:10 +01007796 MBEDTLS_SSL_DEBUG_MSG(3, ("<= handshake wrapup"));
Jerry Yu2a9fff52022-02-17 14:28:51 +08007797}
7798
Gilles Peskine449bd832023-01-11 14:50:10 +01007799int mbedtls_ssl_write_finished(mbedtls_ssl_context *ssl)
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007800{
7801 int ret, hash_len;
7802
Gilles Peskine449bd832023-01-11 14:50:10 +01007803 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write finished"));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007804
Gilles Peskine449bd832023-01-11 14:50:10 +01007805 mbedtls_ssl_update_out_pointers(ssl, ssl->transform_negotiate);
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007806
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01007807 ret = ssl->handshake->calc_finished(ssl, ssl->out_msg + 4, ssl->conf->endpoint);
7808 if (ret != 0) {
7809 MBEDTLS_SSL_DEBUG_RET(1, "calc_finished", ret);
7810 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007811
7812 /*
7813 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7814 * may define some other value. Currently (early 2016), no defined
7815 * ciphersuite does this (and this is unlikely to change as activity has
7816 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7817 */
7818 hash_len = 12;
7819
7820#if defined(MBEDTLS_SSL_RENEGOTIATION)
7821 ssl->verify_data_len = hash_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01007822 memcpy(ssl->own_verify_data, ssl->out_msg + 4, hash_len);
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007823#endif
7824
7825 ssl->out_msglen = 4 + hash_len;
7826 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7827 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
7828
7829 /*
7830 * In case of session resuming, invert the client and server
7831 * ChangeCipherSpec messages order.
7832 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007833 if (ssl->handshake->resume != 0) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007834#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007835 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007836 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Gilles Peskine449bd832023-01-11 14:50:10 +01007837 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007838#endif
7839#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007840 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007841 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Gilles Peskine449bd832023-01-11 14:50:10 +01007842 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007843#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01007844 } else {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007845 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01007846 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007847
7848 /*
7849 * Switch to our negotiated transform and session parameters for outbound
7850 * data.
7851 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007852 MBEDTLS_SSL_DEBUG_MSG(3, ("switching to new transform spec for outbound data"));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007853
7854#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007855 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007856 unsigned char i;
7857
7858 /* Remember current epoch settings for resending */
7859 ssl->handshake->alt_transform_out = ssl->transform_out;
Gilles Peskine449bd832023-01-11 14:50:10 +01007860 memcpy(ssl->handshake->alt_out_ctr, ssl->cur_out_ctr,
7861 sizeof(ssl->handshake->alt_out_ctr));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007862
7863 /* Set sequence_number to zero */
Gilles Peskine449bd832023-01-11 14:50:10 +01007864 memset(&ssl->cur_out_ctr[2], 0, sizeof(ssl->cur_out_ctr) - 2);
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007865
7866
7867 /* Increment epoch */
Gilles Peskine449bd832023-01-11 14:50:10 +01007868 for (i = 2; i > 0; i--) {
7869 if (++ssl->cur_out_ctr[i - 1] != 0) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007870 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01007871 }
7872 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007873
7874 /* The loop goes to its end iff the counter is wrapping */
Gilles Peskine449bd832023-01-11 14:50:10 +01007875 if (i == 0) {
7876 MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS epoch would wrap"));
7877 return MBEDTLS_ERR_SSL_COUNTER_WRAPPING;
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007878 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007879 } else
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007880#endif /* MBEDTLS_SSL_PROTO_DTLS */
Gilles Peskine449bd832023-01-11 14:50:10 +01007881 memset(ssl->cur_out_ctr, 0, sizeof(ssl->cur_out_ctr));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007882
7883 ssl->transform_out = ssl->transform_negotiate;
7884 ssl->session_out = ssl->session_negotiate;
7885
7886#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007887 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
7888 mbedtls_ssl_send_flight_completed(ssl);
7889 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007890#endif
7891
Gilles Peskine449bd832023-01-11 14:50:10 +01007892 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
7893 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
7894 return ret;
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007895 }
7896
7897#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007898 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7899 (ret = mbedtls_ssl_flight_transmit(ssl)) != 0) {
7900 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_flight_transmit", ret);
7901 return ret;
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007902 }
7903#endif
7904
Gilles Peskine449bd832023-01-11 14:50:10 +01007905 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write finished"));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007906
Gilles Peskine449bd832023-01-11 14:50:10 +01007907 return 0;
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007908}
7909
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007910#define SSL_MAX_HASH_LEN 12
7911
Gilles Peskine449bd832023-01-11 14:50:10 +01007912int mbedtls_ssl_parse_finished(mbedtls_ssl_context *ssl)
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007913{
7914 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7915 unsigned int hash_len = 12;
7916 unsigned char buf[SSL_MAX_HASH_LEN];
7917
Gilles Peskine449bd832023-01-11 14:50:10 +01007918 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse finished"));
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007919
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01007920 ret = ssl->handshake->calc_finished(ssl, buf, ssl->conf->endpoint ^ 1);
7921 if (ret != 0) {
7922 MBEDTLS_SSL_DEBUG_RET(1, "calc_finished", ret);
7923 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007924
Gilles Peskine449bd832023-01-11 14:50:10 +01007925 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
7926 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007927 goto exit;
7928 }
7929
Gilles Peskine449bd832023-01-11 14:50:10 +01007930 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
7931 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
7932 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7933 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007934 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
7935 goto exit;
7936 }
7937
Gilles Peskine449bd832023-01-11 14:50:10 +01007938 if (ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED) {
7939 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7940 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007941 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
7942 goto exit;
7943 }
7944
Gilles Peskine449bd832023-01-11 14:50:10 +01007945 if (ssl->in_hslen != mbedtls_ssl_hs_hdr_len(ssl) + hash_len) {
7946 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
7947 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7948 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007949 ret = MBEDTLS_ERR_SSL_DECODE_ERROR;
7950 goto exit;
7951 }
7952
Gilles Peskine449bd832023-01-11 14:50:10 +01007953 if (mbedtls_ct_memcmp(ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl),
7954 buf, hash_len) != 0) {
7955 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
7956 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7957 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007958 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
7959 goto exit;
7960 }
7961
7962#if defined(MBEDTLS_SSL_RENEGOTIATION)
7963 ssl->verify_data_len = hash_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01007964 memcpy(ssl->peer_verify_data, buf, hash_len);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007965#endif
7966
Gilles Peskine449bd832023-01-11 14:50:10 +01007967 if (ssl->handshake->resume != 0) {
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007968#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007969 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007970 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Gilles Peskine449bd832023-01-11 14:50:10 +01007971 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007972#endif
7973#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007974 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007975 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Gilles Peskine449bd832023-01-11 14:50:10 +01007976 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007977#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01007978 } else {
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007979 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01007980 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007981
7982#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007983 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
7984 mbedtls_ssl_recv_flight_completed(ssl);
7985 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007986#endif
7987
Gilles Peskine449bd832023-01-11 14:50:10 +01007988 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse finished"));
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007989
7990exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007991 mbedtls_platform_zeroize(buf, hash_len);
7992 return ret;
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007993}
7994
Jerry Yu392112c2022-02-17 14:34:10 +08007995#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
7996/*
7997 * Helper to get TLS 1.2 PRF from ciphersuite
7998 * (Duplicates bits of logic from ssl_set_handshake_prfs().)
7999 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008000static tls_prf_fn ssl_tls12prf_from_cs(int ciphersuite_id)
Jerry Yu392112c2022-02-17 14:34:10 +08008001{
Jerry Yu392112c2022-02-17 14:34:10 +08008002 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01008003 mbedtls_ssl_ciphersuite_from_id(ciphersuite_id);
Andrzej Kurek68327742022-10-03 06:18:18 -04008004#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01008005 if (ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA384) {
8006 return tls_prf_sha384;
8007 } else
Jerry Yu392112c2022-02-17 14:34:10 +08008008#endif
Andrzej Kurek894edde2022-09-29 06:31:14 -04008009#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
8010 {
Gilles Peskine449bd832023-01-11 14:50:10 +01008011 if (ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA256) {
8012 return tls_prf_sha256;
8013 }
Andrzej Kurek894edde2022-09-29 06:31:14 -04008014 }
8015#endif
8016#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
8017 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
8018 (void) ciphersuite_info;
8019#endif
Andrzej Kurekeabeb302022-10-17 07:52:51 -04008020
Gilles Peskine449bd832023-01-11 14:50:10 +01008021 return NULL;
Jerry Yu392112c2022-02-17 14:34:10 +08008022}
8023#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Jerry Yue93ffcd2022-02-17 14:37:06 +08008024
Gilles Peskine449bd832023-01-11 14:50:10 +01008025static mbedtls_tls_prf_types tls_prf_get_type(mbedtls_ssl_tls_prf_cb *tls_prf)
Jerry Yue93ffcd2022-02-17 14:37:06 +08008026{
8027 ((void) tls_prf);
Andrzej Kurek25f27152022-08-17 16:09:31 -04008028#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01008029 if (tls_prf == tls_prf_sha384) {
8030 return MBEDTLS_SSL_TLS_PRF_SHA384;
8031 } else
Jerry Yue93ffcd2022-02-17 14:37:06 +08008032#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04008033#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01008034 if (tls_prf == tls_prf_sha256) {
8035 return MBEDTLS_SSL_TLS_PRF_SHA256;
8036 } else
Jerry Yue93ffcd2022-02-17 14:37:06 +08008037#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01008038 return MBEDTLS_SSL_TLS_PRF_NONE;
Jerry Yue93ffcd2022-02-17 14:37:06 +08008039}
8040
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008041/*
8042 * Populate a transform structure with session keys and all the other
8043 * necessary information.
8044 *
8045 * Parameters:
8046 * - [in/out]: transform: structure to populate
8047 * [in] must be just initialised with mbedtls_ssl_transform_init()
8048 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
8049 * - [in] ciphersuite
8050 * - [in] master
8051 * - [in] encrypt_then_mac
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008052 * - [in] tls_prf: pointer to PRF to use for key derivation
8053 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
Glenn Strauss07c64162022-03-14 12:34:51 -04008054 * - [in] tls_version: TLS version
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008055 * - [in] endpoint: client or server
8056 * - [in] ssl: used for:
8057 * - ssl->conf->{f,p}_export_keys
8058 * [in] optionally used for:
8059 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
8060 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02008061MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01008062static int ssl_tls12_populate_transform(mbedtls_ssl_transform *transform,
8063 int ciphersuite,
8064 const unsigned char master[48],
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008065#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01008066 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008067#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01008068 ssl_tls_prf_t tls_prf,
8069 const unsigned char randbytes[64],
8070 mbedtls_ssl_protocol_version tls_version,
8071 unsigned endpoint,
8072 const mbedtls_ssl_context *ssl)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008073{
8074 int ret = 0;
8075 unsigned char keyblk[256];
8076 unsigned char *key1;
8077 unsigned char *key2;
8078 unsigned char *mac_enc;
8079 unsigned char *mac_dec;
8080 size_t mac_key_len = 0;
8081 size_t iv_copy_len;
8082 size_t keylen;
8083 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008084 mbedtls_ssl_mode_t ssl_mode;
Neil Armstronge4512952022-03-08 09:08:22 +01008085#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008086 const mbedtls_cipher_info_t *cipher_info;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008087 const mbedtls_md_info_t *md_info;
Neil Armstronge4512952022-03-08 09:08:22 +01008088#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008089
8090#if defined(MBEDTLS_USE_PSA_CRYPTO)
8091 psa_key_type_t key_type;
8092 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8093 psa_algorithm_t alg;
Neil Armstronge4512952022-03-08 09:08:22 +01008094 psa_algorithm_t mac_alg = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008095 size_t key_bits;
8096 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
8097#endif
8098
8099#if !defined(MBEDTLS_DEBUG_C) && \
8100 !defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Gilles Peskine449bd832023-01-11 14:50:10 +01008101 if (ssl->f_export_keys == NULL) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008102 ssl = NULL; /* make sure we don't use it except for these cases */
8103 (void) ssl;
8104 }
8105#endif
8106
8107 /*
8108 * Some data just needs copying into the structure
8109 */
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008110#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008111 transform->encrypt_then_mac = encrypt_then_mac;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008112#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Glenn Strauss07c64162022-03-14 12:34:51 -04008113 transform->tls_version = tls_version;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008114
8115#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01008116 memcpy(transform->randbytes, randbytes, sizeof(transform->randbytes));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008117#endif
8118
8119#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01008120 if (tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008121 /* At the moment, we keep TLS <= 1.2 and TLS 1.3 transform
8122 * generation separate. This should never happen. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008123 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008124 }
8125#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
8126
8127 /*
8128 * Get various info structures
8129 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008130 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(ciphersuite);
8131 if (ciphersuite_info == NULL) {
8132 MBEDTLS_SSL_DEBUG_MSG(1, ("ciphersuite info for %d not found",
8133 ciphersuite));
8134 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008135 }
8136
Neil Armstrongab555e02022-04-04 11:07:59 +02008137 ssl_mode = mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008138#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01008139 encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008140#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01008141 ciphersuite_info);
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008142
Gilles Peskine449bd832023-01-11 14:50:10 +01008143 if (ssl_mode == MBEDTLS_SSL_MODE_AEAD) {
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008144 transform->taglen =
8145 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Gilles Peskine449bd832023-01-11 14:50:10 +01008146 }
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008147
8148#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008149 if ((status = mbedtls_ssl_cipher_to_psa(ciphersuite_info->cipher,
8150 transform->taglen,
8151 &alg,
8152 &key_type,
8153 &key_bits)) != PSA_SUCCESS) {
8154 ret = psa_ssl_status_to_mbedtls(status);
8155 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_cipher_to_psa", ret);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008156 goto end;
8157 }
8158#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008159 cipher_info = mbedtls_cipher_info_from_type(ciphersuite_info->cipher);
8160 if (cipher_info == NULL) {
8161 MBEDTLS_SSL_DEBUG_MSG(1, ("cipher info for %u not found",
8162 ciphersuite_info->cipher));
8163 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008164 }
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008165#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008166
Neil Armstronge4512952022-03-08 09:08:22 +01008167#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008168 mac_alg = mbedtls_hash_info_psa_from_md(ciphersuite_info->mac);
8169 if (mac_alg == 0) {
8170 MBEDTLS_SSL_DEBUG_MSG(1, ("mbedtls_hash_info_psa_from_md for %u not found",
8171 (unsigned) ciphersuite_info->mac));
8172 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Neil Armstronge4512952022-03-08 09:08:22 +01008173 }
8174#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008175 md_info = mbedtls_md_info_from_type(ciphersuite_info->mac);
8176 if (md_info == NULL) {
8177 MBEDTLS_SSL_DEBUG_MSG(1, ("mbedtls_md info for %u not found",
8178 (unsigned) ciphersuite_info->mac));
8179 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008180 }
Neil Armstronge4512952022-03-08 09:08:22 +01008181#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008182
8183#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
8184 /* Copy own and peer's CID if the use of the CID
8185 * extension has been negotiated. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008186 if (ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED) {
8187 MBEDTLS_SSL_DEBUG_MSG(3, ("Copy CIDs into SSL transform"));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008188
8189 transform->in_cid_len = ssl->own_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008190 memcpy(transform->in_cid, ssl->own_cid, ssl->own_cid_len);
8191 MBEDTLS_SSL_DEBUG_BUF(3, "Incoming CID", transform->in_cid,
8192 transform->in_cid_len);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008193
8194 transform->out_cid_len = ssl->handshake->peer_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008195 memcpy(transform->out_cid, ssl->handshake->peer_cid,
8196 ssl->handshake->peer_cid_len);
8197 MBEDTLS_SSL_DEBUG_BUF(3, "Outgoing CID", transform->out_cid,
8198 transform->out_cid_len);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008199 }
8200#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
8201
8202 /*
8203 * Compute key block using the PRF
8204 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008205 ret = tls_prf(master, 48, "key expansion", randbytes, 64, keyblk, 256);
8206 if (ret != 0) {
8207 MBEDTLS_SSL_DEBUG_RET(1, "prf", ret);
8208 return ret;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008209 }
8210
Gilles Peskine449bd832023-01-11 14:50:10 +01008211 MBEDTLS_SSL_DEBUG_MSG(3, ("ciphersuite = %s",
8212 mbedtls_ssl_get_ciphersuite_name(ciphersuite)));
8213 MBEDTLS_SSL_DEBUG_BUF(3, "master secret", master, 48);
8214 MBEDTLS_SSL_DEBUG_BUF(4, "random bytes", randbytes, 64);
8215 MBEDTLS_SSL_DEBUG_BUF(4, "key block", keyblk, 256);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008216
8217 /*
8218 * Determine the appropriate key, IV and MAC length.
8219 */
8220
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008221#if defined(MBEDTLS_USE_PSA_CRYPTO)
8222 keylen = PSA_BITS_TO_BYTES(key_bits);
8223#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008224 keylen = mbedtls_cipher_info_get_key_bitlen(cipher_info) / 8;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008225#endif
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008226
8227#if defined(MBEDTLS_GCM_C) || \
8228 defined(MBEDTLS_CCM_C) || \
8229 defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008230 if (ssl_mode == MBEDTLS_SSL_MODE_AEAD) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008231 size_t explicit_ivlen;
8232
8233 transform->maclen = 0;
8234 mac_key_len = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008235
8236 /* All modes haves 96-bit IVs, but the length of the static parts vary
8237 * with mode and version:
8238 * - For GCM and CCM in TLS 1.2, there's a static IV of 4 Bytes
8239 * (to be concatenated with a dynamically chosen IV of 8 Bytes)
8240 * - For ChaChaPoly in TLS 1.2, and all modes in TLS 1.3, there's
8241 * a static IV of 12 Bytes (to be XOR'ed with the 8 Byte record
8242 * sequence number).
8243 */
8244 transform->ivlen = 12;
David Horstmann3b2276a2022-10-06 14:49:08 +01008245
8246 int is_chachapoly = 0;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008247#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008248 is_chachapoly = (key_type == PSA_KEY_TYPE_CHACHA20);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008249#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008250 is_chachapoly = (mbedtls_cipher_info_get_mode(cipher_info)
8251 == MBEDTLS_MODE_CHACHAPOLY);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008252#endif /* MBEDTLS_USE_PSA_CRYPTO */
David Horstmann3b2276a2022-10-06 14:49:08 +01008253
Gilles Peskine449bd832023-01-11 14:50:10 +01008254 if (is_chachapoly) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008255 transform->fixed_ivlen = 12;
Gilles Peskine449bd832023-01-11 14:50:10 +01008256 } else {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008257 transform->fixed_ivlen = 4;
Gilles Peskine449bd832023-01-11 14:50:10 +01008258 }
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008259
8260 /* Minimum length of encrypted record */
8261 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
8262 transform->minlen = explicit_ivlen + transform->taglen;
Gilles Peskine449bd832023-01-11 14:50:10 +01008263 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008264#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
8265#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01008266 if (ssl_mode == MBEDTLS_SSL_MODE_STREAM ||
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008267 ssl_mode == MBEDTLS_SSL_MODE_CBC ||
Gilles Peskine449bd832023-01-11 14:50:10 +01008268 ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM) {
Neil Armstronge4512952022-03-08 09:08:22 +01008269#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008270 size_t block_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008271#else
8272 size_t block_size = cipher_info->block_size;
8273#endif /* MBEDTLS_USE_PSA_CRYPTO */
8274
8275#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronge4512952022-03-08 09:08:22 +01008276 /* Get MAC length */
8277 mac_key_len = PSA_HASH_LENGTH(mac_alg);
8278#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008279 /* Initialize HMAC contexts */
Gilles Peskine449bd832023-01-11 14:50:10 +01008280 if ((ret = mbedtls_md_setup(&transform->md_ctx_enc, md_info, 1)) != 0 ||
8281 (ret = mbedtls_md_setup(&transform->md_ctx_dec, md_info, 1)) != 0) {
8282 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_setup", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008283 goto end;
8284 }
8285
8286 /* Get MAC length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008287 mac_key_len = mbedtls_md_get_size(md_info);
Neil Armstronge4512952022-03-08 09:08:22 +01008288#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008289 transform->maclen = mac_key_len;
8290
8291 /* IV length */
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008292#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008293 transform->ivlen = PSA_CIPHER_IV_LENGTH(key_type, alg);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008294#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008295 transform->ivlen = cipher_info->iv_size;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008296#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008297
8298 /* Minimum length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008299 if (ssl_mode == MBEDTLS_SSL_MODE_STREAM) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008300 transform->minlen = transform->maclen;
Gilles Peskine449bd832023-01-11 14:50:10 +01008301 } else {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008302 /*
8303 * GenericBlockCipher:
8304 * 1. if EtM is in use: one block plus MAC
8305 * otherwise: * first multiple of blocklen greater than maclen
8306 * 2. IV
8307 */
8308#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01008309 if (ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008310 transform->minlen = transform->maclen
Gilles Peskine449bd832023-01-11 14:50:10 +01008311 + block_size;
8312 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008313#endif
8314 {
8315 transform->minlen = transform->maclen
Gilles Peskine449bd832023-01-11 14:50:10 +01008316 + block_size
8317 - transform->maclen % block_size;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008318 }
8319
Gilles Peskine449bd832023-01-11 14:50:10 +01008320 if (tls_version == MBEDTLS_SSL_VERSION_TLS1_2) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008321 transform->minlen += transform->ivlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01008322 } else {
8323 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008324 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
8325 goto end;
8326 }
8327 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008328 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008329#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
8330 {
Gilles Peskine449bd832023-01-11 14:50:10 +01008331 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
8332 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008333 }
8334
Gilles Peskine449bd832023-01-11 14:50:10 +01008335 MBEDTLS_SSL_DEBUG_MSG(3, ("keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
8336 (unsigned) keylen,
8337 (unsigned) transform->minlen,
8338 (unsigned) transform->ivlen,
8339 (unsigned) transform->maclen));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008340
8341 /*
8342 * Finally setup the cipher contexts, IVs and MAC secrets.
8343 */
8344#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008345 if (endpoint == MBEDTLS_SSL_IS_CLIENT) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008346 key1 = keyblk + mac_key_len * 2;
8347 key2 = keyblk + mac_key_len * 2 + keylen;
8348
8349 mac_enc = keyblk;
8350 mac_dec = keyblk + mac_key_len;
8351
Gilles Peskine449bd832023-01-11 14:50:10 +01008352 iv_copy_len = (transform->fixed_ivlen) ?
8353 transform->fixed_ivlen : transform->ivlen;
8354 memcpy(transform->iv_enc, key2 + keylen, iv_copy_len);
8355 memcpy(transform->iv_dec, key2 + keylen + iv_copy_len,
8356 iv_copy_len);
8357 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008358#endif /* MBEDTLS_SSL_CLI_C */
8359#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008360 if (endpoint == MBEDTLS_SSL_IS_SERVER) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008361 key1 = keyblk + mac_key_len * 2 + keylen;
8362 key2 = keyblk + mac_key_len * 2;
8363
8364 mac_enc = keyblk + mac_key_len;
8365 mac_dec = keyblk;
8366
Gilles Peskine449bd832023-01-11 14:50:10 +01008367 iv_copy_len = (transform->fixed_ivlen) ?
8368 transform->fixed_ivlen : transform->ivlen;
8369 memcpy(transform->iv_dec, key1 + keylen, iv_copy_len);
8370 memcpy(transform->iv_enc, key1 + keylen + iv_copy_len,
8371 iv_copy_len);
8372 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008373#endif /* MBEDTLS_SSL_SRV_C */
8374 {
Gilles Peskine449bd832023-01-11 14:50:10 +01008375 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008376 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
8377 goto end;
8378 }
8379
Gilles Peskine449bd832023-01-11 14:50:10 +01008380 if (ssl != NULL && ssl->f_export_keys != NULL) {
8381 ssl->f_export_keys(ssl->p_export_keys,
8382 MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET,
8383 master, 48,
8384 randbytes + 32,
8385 randbytes,
8386 tls_prf_get_type(tls_prf));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008387 }
8388
8389#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008390 transform->psa_alg = alg;
8391
Gilles Peskine449bd832023-01-11 14:50:10 +01008392 if (alg != MBEDTLS_SSL_NULL_CIPHER) {
8393 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
8394 psa_set_key_algorithm(&attributes, alg);
8395 psa_set_key_type(&attributes, key_type);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008396
Gilles Peskine449bd832023-01-11 14:50:10 +01008397 if ((status = psa_import_key(&attributes,
8398 key1,
8399 PSA_BITS_TO_BYTES(key_bits),
8400 &transform->psa_key_enc)) != PSA_SUCCESS) {
8401 MBEDTLS_SSL_DEBUG_RET(3, "psa_import_key", (int) status);
8402 ret = psa_ssl_status_to_mbedtls(status);
8403 MBEDTLS_SSL_DEBUG_RET(1, "psa_import_key", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008404 goto end;
8405 }
8406
Gilles Peskine449bd832023-01-11 14:50:10 +01008407 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008408
Gilles Peskine449bd832023-01-11 14:50:10 +01008409 if ((status = psa_import_key(&attributes,
8410 key2,
8411 PSA_BITS_TO_BYTES(key_bits),
8412 &transform->psa_key_dec)) != PSA_SUCCESS) {
8413 ret = psa_ssl_status_to_mbedtls(status);
8414 MBEDTLS_SSL_DEBUG_RET(1, "psa_import_key", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008415 goto end;
8416 }
8417 }
8418#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008419 if ((ret = mbedtls_cipher_setup(&transform->cipher_ctx_enc,
8420 cipher_info)) != 0) {
8421 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008422 goto end;
8423 }
8424
Gilles Peskine449bd832023-01-11 14:50:10 +01008425 if ((ret = mbedtls_cipher_setup(&transform->cipher_ctx_dec,
8426 cipher_info)) != 0) {
8427 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008428 goto end;
8429 }
8430
Gilles Peskine449bd832023-01-11 14:50:10 +01008431 if ((ret = mbedtls_cipher_setkey(&transform->cipher_ctx_enc, key1,
8432 (int) mbedtls_cipher_info_get_key_bitlen(cipher_info),
8433 MBEDTLS_ENCRYPT)) != 0) {
8434 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setkey", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008435 goto end;
8436 }
8437
Gilles Peskine449bd832023-01-11 14:50:10 +01008438 if ((ret = mbedtls_cipher_setkey(&transform->cipher_ctx_dec, key2,
8439 (int) mbedtls_cipher_info_get_key_bitlen(cipher_info),
8440 MBEDTLS_DECRYPT)) != 0) {
8441 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setkey", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008442 goto end;
8443 }
8444
8445#if defined(MBEDTLS_CIPHER_MODE_CBC)
Gilles Peskine449bd832023-01-11 14:50:10 +01008446 if (mbedtls_cipher_info_get_mode(cipher_info) == MBEDTLS_MODE_CBC) {
8447 if ((ret = mbedtls_cipher_set_padding_mode(&transform->cipher_ctx_enc,
8448 MBEDTLS_PADDING_NONE)) != 0) {
8449 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_set_padding_mode", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008450 goto end;
8451 }
8452
Gilles Peskine449bd832023-01-11 14:50:10 +01008453 if ((ret = mbedtls_cipher_set_padding_mode(&transform->cipher_ctx_dec,
8454 MBEDTLS_PADDING_NONE)) != 0) {
8455 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_set_padding_mode", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008456 goto end;
8457 }
8458 }
8459#endif /* MBEDTLS_CIPHER_MODE_CBC */
8460#endif /* MBEDTLS_USE_PSA_CRYPTO */
8461
Neil Armstrong29c0c042022-03-17 17:47:28 +01008462#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
8463 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
8464 For AEAD-based ciphersuites, there is nothing to do here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008465 if (mac_key_len != 0) {
Neil Armstrong29c0c042022-03-17 17:47:28 +01008466#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008467 transform->psa_mac_alg = PSA_ALG_HMAC(mac_alg);
Neil Armstrong29c0c042022-03-17 17:47:28 +01008468
Gilles Peskine449bd832023-01-11 14:50:10 +01008469 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
8470 psa_set_key_algorithm(&attributes, PSA_ALG_HMAC(mac_alg));
8471 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
Neil Armstrong29c0c042022-03-17 17:47:28 +01008472
Gilles Peskine449bd832023-01-11 14:50:10 +01008473 if ((status = psa_import_key(&attributes,
8474 mac_enc, mac_key_len,
8475 &transform->psa_mac_enc)) != PSA_SUCCESS) {
8476 ret = psa_ssl_status_to_mbedtls(status);
8477 MBEDTLS_SSL_DEBUG_RET(1, "psa_import_mac_key", ret);
Neil Armstrong29c0c042022-03-17 17:47:28 +01008478 goto end;
8479 }
8480
Gilles Peskine449bd832023-01-11 14:50:10 +01008481 if ((transform->psa_alg == MBEDTLS_SSL_NULL_CIPHER) ||
8482 ((transform->psa_alg == PSA_ALG_CBC_NO_PADDING)
Andrzej Kurek8c95ac42022-08-17 16:17:00 -04008483#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01008484 && (transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED)
Andrzej Kurek8c95ac42022-08-17 16:17:00 -04008485#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01008486 )) {
Neil Armstrong29c0c042022-03-17 17:47:28 +01008487 /* mbedtls_ct_hmac() requires the key to be exportable */
Gilles Peskine449bd832023-01-11 14:50:10 +01008488 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT |
8489 PSA_KEY_USAGE_VERIFY_HASH);
8490 } else {
8491 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
8492 }
Neil Armstrong29c0c042022-03-17 17:47:28 +01008493
Gilles Peskine449bd832023-01-11 14:50:10 +01008494 if ((status = psa_import_key(&attributes,
8495 mac_dec, mac_key_len,
8496 &transform->psa_mac_dec)) != PSA_SUCCESS) {
8497 ret = psa_ssl_status_to_mbedtls(status);
8498 MBEDTLS_SSL_DEBUG_RET(1, "psa_import_mac_key", ret);
Neil Armstrong29c0c042022-03-17 17:47:28 +01008499 goto end;
8500 }
8501#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008502 ret = mbedtls_md_hmac_starts(&transform->md_ctx_enc, mac_enc, mac_key_len);
8503 if (ret != 0) {
Neil Armstrong29c0c042022-03-17 17:47:28 +01008504 goto end;
Gilles Peskine449bd832023-01-11 14:50:10 +01008505 }
8506 ret = mbedtls_md_hmac_starts(&transform->md_ctx_dec, mac_dec, mac_key_len);
8507 if (ret != 0) {
Neil Armstrong29c0c042022-03-17 17:47:28 +01008508 goto end;
Gilles Peskine449bd832023-01-11 14:50:10 +01008509 }
Neil Armstrong29c0c042022-03-17 17:47:28 +01008510#endif /* MBEDTLS_USE_PSA_CRYPTO */
8511 }
8512#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
8513
8514 ((void) mac_dec);
8515 ((void) mac_enc);
8516
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008517end:
Gilles Peskine449bd832023-01-11 14:50:10 +01008518 mbedtls_platform_zeroize(keyblk, sizeof(keyblk));
8519 return ret;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008520}
8521
Valerio Settia08b1a42022-11-17 15:10:02 +01008522#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) && \
8523 defined(MBEDTLS_USE_PSA_CRYPTO)
Valerio Setti6b3dab02022-11-17 17:14:54 +01008524int mbedtls_psa_ecjpake_read_round(
Gilles Peskine449bd832023-01-11 14:50:10 +01008525 psa_pake_operation_t *pake_ctx,
8526 const unsigned char *buf,
8527 size_t len, mbedtls_ecjpake_rounds_t round)
Valerio Settia08b1a42022-11-17 15:10:02 +01008528{
8529 psa_status_t status;
8530 size_t input_offset = 0;
Valerio Setti819de862022-11-17 18:05:19 +01008531 /*
Valerio Setti6b3dab02022-11-17 17:14:54 +01008532 * At round one repeat the KEY_SHARE, ZK_PUBLIC & ZF_PROOF twice
8533 * At round two perform a single cycle
8534 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008535 unsigned int remaining_steps = (round == MBEDTLS_ECJPAKE_ROUND_ONE) ? 2 : 1;
Valerio Settia08b1a42022-11-17 15:10:02 +01008536
Gilles Peskine449bd832023-01-11 14:50:10 +01008537 for (; remaining_steps > 0; remaining_steps--) {
8538 for (psa_pake_step_t step = PSA_PAKE_STEP_KEY_SHARE;
Valerio Settia08b1a42022-11-17 15:10:02 +01008539 step <= PSA_PAKE_STEP_ZK_PROOF;
Gilles Peskine449bd832023-01-11 14:50:10 +01008540 ++step) {
Valerio Settia08b1a42022-11-17 15:10:02 +01008541 /* Length is stored at the first byte */
8542 size_t length = buf[input_offset];
8543 input_offset += 1;
8544
Gilles Peskine449bd832023-01-11 14:50:10 +01008545 if (input_offset + length > len) {
Valerio Settia08b1a42022-11-17 15:10:02 +01008546 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
8547 }
8548
Gilles Peskine449bd832023-01-11 14:50:10 +01008549 status = psa_pake_input(pake_ctx, step,
8550 buf + input_offset, length);
8551 if (status != PSA_SUCCESS) {
8552 return psa_ssl_status_to_mbedtls(status);
Valerio Settia08b1a42022-11-17 15:10:02 +01008553 }
8554
8555 input_offset += length;
8556 }
8557 }
8558
Gilles Peskine449bd832023-01-11 14:50:10 +01008559 if (input_offset != len) {
Valerio Setti61ea17d2022-11-18 12:11:00 +01008560 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01008561 }
Valerio Setti30ebe112022-11-17 16:23:34 +01008562
Gilles Peskine449bd832023-01-11 14:50:10 +01008563 return 0;
Valerio Settia08b1a42022-11-17 15:10:02 +01008564}
8565
Valerio Setti6b3dab02022-11-17 17:14:54 +01008566int mbedtls_psa_ecjpake_write_round(
Gilles Peskine449bd832023-01-11 14:50:10 +01008567 psa_pake_operation_t *pake_ctx,
8568 unsigned char *buf,
8569 size_t len, size_t *olen,
8570 mbedtls_ecjpake_rounds_t round)
Valerio Settia08b1a42022-11-17 15:10:02 +01008571{
8572 psa_status_t status;
8573 size_t output_offset = 0;
8574 size_t output_len;
Valerio Setti819de862022-11-17 18:05:19 +01008575 /*
Valerio Setti6b3dab02022-11-17 17:14:54 +01008576 * At round one repeat the KEY_SHARE, ZK_PUBLIC & ZF_PROOF twice
8577 * At round two perform a single cycle
8578 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008579 unsigned int remaining_steps = (round == MBEDTLS_ECJPAKE_ROUND_ONE) ? 2 : 1;
Valerio Settia08b1a42022-11-17 15:10:02 +01008580
Gilles Peskine449bd832023-01-11 14:50:10 +01008581 for (; remaining_steps > 0; remaining_steps--) {
8582 for (psa_pake_step_t step = PSA_PAKE_STEP_KEY_SHARE;
8583 step <= PSA_PAKE_STEP_ZK_PROOF;
8584 ++step) {
Valerio Setti79f6b6b2022-11-21 14:17:03 +01008585 /*
Valerio Settid4a9b1a2022-11-22 11:11:10 +01008586 * For each step, prepend 1 byte with the length of the data as
8587 * given by psa_pake_output().
Valerio Setti79f6b6b2022-11-21 14:17:03 +01008588 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008589 status = psa_pake_output(pake_ctx, step,
8590 buf + output_offset + 1,
8591 len - output_offset - 1,
8592 &output_len);
8593 if (status != PSA_SUCCESS) {
8594 return psa_ssl_status_to_mbedtls(status);
Valerio Settia08b1a42022-11-17 15:10:02 +01008595 }
8596
Valerio Setti99d88c12022-11-22 16:03:43 +01008597 *(buf + output_offset) = (uint8_t) output_len;
Valerio Setti79f6b6b2022-11-21 14:17:03 +01008598
8599 output_offset += output_len + 1;
Valerio Settia08b1a42022-11-17 15:10:02 +01008600 }
8601 }
8602
8603 *olen = output_offset;
8604
Gilles Peskine449bd832023-01-11 14:50:10 +01008605 return 0;
Valerio Settia08b1a42022-11-17 15:10:02 +01008606}
Valerio Settia08b1a42022-11-17 15:10:02 +01008607#endif //MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED && MBEDTLS_USE_PSA_CRYPTO
8608
Jerry Yuee40f9d2022-02-17 14:55:16 +08008609#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008610int mbedtls_ssl_get_key_exchange_md_tls1_2(mbedtls_ssl_context *ssl,
8611 unsigned char *hash, size_t *hashlen,
8612 unsigned char *data, size_t data_len,
8613 mbedtls_md_type_t md_alg)
Jerry Yuee40f9d2022-02-17 14:55:16 +08008614{
8615 psa_status_t status;
8616 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01008617 psa_algorithm_t hash_alg = mbedtls_hash_info_psa_from_md(md_alg);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008618
Gilles Peskine449bd832023-01-11 14:50:10 +01008619 MBEDTLS_SSL_DEBUG_MSG(3, ("Perform PSA-based computation of digest of ServerKeyExchange"));
Jerry Yuee40f9d2022-02-17 14:55:16 +08008620
Gilles Peskine449bd832023-01-11 14:50:10 +01008621 if ((status = psa_hash_setup(&hash_operation,
8622 hash_alg)) != PSA_SUCCESS) {
8623 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_setup", status);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008624 goto exit;
8625 }
8626
Gilles Peskine449bd832023-01-11 14:50:10 +01008627 if ((status = psa_hash_update(&hash_operation, ssl->handshake->randbytes,
8628 64)) != PSA_SUCCESS) {
8629 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_update", status);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008630 goto exit;
8631 }
8632
Gilles Peskine449bd832023-01-11 14:50:10 +01008633 if ((status = psa_hash_update(&hash_operation,
8634 data, data_len)) != PSA_SUCCESS) {
8635 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_update", status);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008636 goto exit;
8637 }
8638
Gilles Peskine449bd832023-01-11 14:50:10 +01008639 if ((status = psa_hash_finish(&hash_operation, hash, PSA_HASH_MAX_SIZE,
8640 hashlen)) != PSA_SUCCESS) {
8641 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_finish", status);
8642 goto exit;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008643 }
8644
8645exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008646 if (status != PSA_SUCCESS) {
8647 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8648 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
8649 switch (status) {
Jerry Yuee40f9d2022-02-17 14:55:16 +08008650 case PSA_ERROR_NOT_SUPPORTED:
Gilles Peskine449bd832023-01-11 14:50:10 +01008651 return MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008652 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
8653 case PSA_ERROR_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +01008654 return MBEDTLS_ERR_MD_BAD_INPUT_DATA;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008655 case PSA_ERROR_INSUFFICIENT_MEMORY:
Gilles Peskine449bd832023-01-11 14:50:10 +01008656 return MBEDTLS_ERR_MD_ALLOC_FAILED;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008657 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01008658 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008659 }
8660 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008661 return 0;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008662}
8663
8664#else
8665
Gilles Peskine449bd832023-01-11 14:50:10 +01008666int mbedtls_ssl_get_key_exchange_md_tls1_2(mbedtls_ssl_context *ssl,
8667 unsigned char *hash, size_t *hashlen,
8668 unsigned char *data, size_t data_len,
8669 mbedtls_md_type_t md_alg)
Jerry Yuee40f9d2022-02-17 14:55:16 +08008670{
8671 int ret = 0;
8672 mbedtls_md_context_t ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +01008673 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type(md_alg);
8674 *hashlen = mbedtls_md_get_size(md_info);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008675
Gilles Peskine449bd832023-01-11 14:50:10 +01008676 MBEDTLS_SSL_DEBUG_MSG(3, ("Perform mbedtls-based computation of digest of ServerKeyExchange"));
Jerry Yuee40f9d2022-02-17 14:55:16 +08008677
Gilles Peskine449bd832023-01-11 14:50:10 +01008678 mbedtls_md_init(&ctx);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008679
8680 /*
8681 * digitally-signed struct {
8682 * opaque client_random[32];
8683 * opaque server_random[32];
8684 * ServerDHParams params;
8685 * };
8686 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008687 if ((ret = mbedtls_md_setup(&ctx, md_info, 0)) != 0) {
8688 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_setup", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008689 goto exit;
8690 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008691 if ((ret = mbedtls_md_starts(&ctx)) != 0) {
8692 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_starts", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008693 goto exit;
8694 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008695 if ((ret = mbedtls_md_update(&ctx, ssl->handshake->randbytes, 64)) != 0) {
8696 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_update", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008697 goto exit;
8698 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008699 if ((ret = mbedtls_md_update(&ctx, data, data_len)) != 0) {
8700 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_update", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008701 goto exit;
8702 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008703 if ((ret = mbedtls_md_finish(&ctx, hash)) != 0) {
8704 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_finish", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008705 goto exit;
8706 }
8707
8708exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008709 mbedtls_md_free(&ctx);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008710
Gilles Peskine449bd832023-01-11 14:50:10 +01008711 if (ret != 0) {
8712 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8713 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
8714 }
Jerry Yuee40f9d2022-02-17 14:55:16 +08008715
Gilles Peskine449bd832023-01-11 14:50:10 +01008716 return ret;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008717}
8718#endif /* MBEDTLS_USE_PSA_CRYPTO */
8719
Jerry Yud9d91da2022-02-17 14:57:06 +08008720#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
8721
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008722/* Find the preferred hash for a given signature algorithm. */
8723unsigned int mbedtls_ssl_tls12_get_preferred_hash_for_sig_alg(
Gilles Peskine449bd832023-01-11 14:50:10 +01008724 mbedtls_ssl_context *ssl,
8725 unsigned int sig_alg)
Jerry Yud9d91da2022-02-17 14:57:06 +08008726{
Gabor Mezei078e8032022-04-27 21:17:56 +02008727 unsigned int i;
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008728 uint16_t *received_sig_algs = ssl->handshake->received_sig_algs;
Gabor Mezei078e8032022-04-27 21:17:56 +02008729
Gilles Peskine449bd832023-01-11 14:50:10 +01008730 if (sig_alg == MBEDTLS_SSL_SIG_ANON) {
8731 return MBEDTLS_SSL_HASH_NONE;
8732 }
Gabor Mezei078e8032022-04-27 21:17:56 +02008733
Gilles Peskine449bd832023-01-11 14:50:10 +01008734 for (i = 0; received_sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++) {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008735 unsigned int hash_alg_received =
Gilles Peskine449bd832023-01-11 14:50:10 +01008736 MBEDTLS_SSL_TLS12_HASH_ALG_FROM_SIG_AND_HASH_ALG(
8737 received_sig_algs[i]);
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008738 unsigned int sig_alg_received =
Gilles Peskine449bd832023-01-11 14:50:10 +01008739 MBEDTLS_SSL_TLS12_SIG_ALG_FROM_SIG_AND_HASH_ALG(
8740 received_sig_algs[i]);
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008741
Gilles Peskine449bd832023-01-11 14:50:10 +01008742 if (sig_alg == sig_alg_received) {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008743#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008744 if (ssl->handshake->key_cert && ssl->handshake->key_cert->key) {
Neil Armstrong96eceb82022-06-30 18:05:05 +02008745 psa_algorithm_t psa_hash_alg =
Gilles Peskine449bd832023-01-11 14:50:10 +01008746 mbedtls_hash_info_psa_from_md(hash_alg_received);
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008747
Gilles Peskine449bd832023-01-11 14:50:10 +01008748 if (sig_alg_received == MBEDTLS_SSL_SIG_ECDSA &&
8749 !mbedtls_pk_can_do_ext(ssl->handshake->key_cert->key,
8750 PSA_ALG_ECDSA(psa_hash_alg),
8751 PSA_KEY_USAGE_SIGN_HASH)) {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008752 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01008753 }
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008754
Gilles Peskine449bd832023-01-11 14:50:10 +01008755 if (sig_alg_received == MBEDTLS_SSL_SIG_RSA &&
8756 !mbedtls_pk_can_do_ext(ssl->handshake->key_cert->key,
8757 PSA_ALG_RSA_PKCS1V15_SIGN(
8758 psa_hash_alg),
8759 PSA_KEY_USAGE_SIGN_HASH)) {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008760 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01008761 }
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008762 }
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008763#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong96eceb82022-06-30 18:05:05 +02008764
Gilles Peskine449bd832023-01-11 14:50:10 +01008765 return hash_alg_received;
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008766 }
Jerry Yud9d91da2022-02-17 14:57:06 +08008767 }
Jerry Yud9d91da2022-02-17 14:57:06 +08008768
Gilles Peskine449bd832023-01-11 14:50:10 +01008769 return MBEDTLS_SSL_HASH_NONE;
Jerry Yud9d91da2022-02-17 14:57:06 +08008770}
8771
8772#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
8773
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008774/* Serialization of TLS 1.2 sessions:
8775 *
8776 * struct {
8777 * uint64 start_time;
8778 * uint8 ciphersuite[2]; // defined by the standard
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008779 * uint8 session_id_len; // at most 32
8780 * opaque session_id[32];
8781 * opaque master[48]; // fixed length in the standard
8782 * uint32 verify_result;
8783 * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert
8784 * opaque ticket<0..2^24-1>; // length 0 means no ticket
8785 * uint32 ticket_lifetime;
8786 * uint8 mfl_code; // up to 255 according to standard
8787 * uint8 encrypt_then_mac; // 0 or 1
8788 * } serialized_session_tls12;
8789 *
8790 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008791static size_t ssl_tls12_session_save(const mbedtls_ssl_session *session,
8792 unsigned char *buf,
8793 size_t buf_len)
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008794{
8795 unsigned char *p = buf;
8796 size_t used = 0;
8797
8798#if defined(MBEDTLS_HAVE_TIME)
8799 uint64_t start;
8800#endif
8801#if defined(MBEDTLS_X509_CRT_PARSE_C)
8802#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8803 size_t cert_len;
8804#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8805#endif /* MBEDTLS_X509_CRT_PARSE_C */
8806
8807 /*
8808 * Time
8809 */
8810#if defined(MBEDTLS_HAVE_TIME)
8811 used += 8;
8812
Gilles Peskine449bd832023-01-11 14:50:10 +01008813 if (used <= buf_len) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008814 start = (uint64_t) session->start;
8815
Gilles Peskine449bd832023-01-11 14:50:10 +01008816 MBEDTLS_PUT_UINT64_BE(start, p, 0);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008817 p += 8;
8818 }
8819#endif /* MBEDTLS_HAVE_TIME */
8820
8821 /*
8822 * Basic mandatory fields
8823 */
8824 used += 2 /* ciphersuite */
Gilles Peskine449bd832023-01-11 14:50:10 +01008825 + 1 /* id_len */
8826 + sizeof(session->id)
8827 + sizeof(session->master)
8828 + 4; /* verify_result */
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008829
Gilles Peskine449bd832023-01-11 14:50:10 +01008830 if (used <= buf_len) {
8831 MBEDTLS_PUT_UINT16_BE(session->ciphersuite, p, 0);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008832 p += 2;
8833
Gilles Peskine449bd832023-01-11 14:50:10 +01008834 *p++ = MBEDTLS_BYTE_0(session->id_len);
8835 memcpy(p, session->id, 32);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008836 p += 32;
8837
Gilles Peskine449bd832023-01-11 14:50:10 +01008838 memcpy(p, session->master, 48);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008839 p += 48;
8840
Gilles Peskine449bd832023-01-11 14:50:10 +01008841 MBEDTLS_PUT_UINT32_BE(session->verify_result, p, 0);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008842 p += 4;
8843 }
8844
8845 /*
8846 * Peer's end-entity certificate
8847 */
8848#if defined(MBEDTLS_X509_CRT_PARSE_C)
8849#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01008850 if (session->peer_cert == NULL) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008851 cert_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01008852 } else {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008853 cert_len = session->peer_cert->raw.len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008854 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008855
8856 used += 3 + cert_len;
8857
Gilles Peskine449bd832023-01-11 14:50:10 +01008858 if (used <= buf_len) {
8859 *p++ = MBEDTLS_BYTE_2(cert_len);
8860 *p++ = MBEDTLS_BYTE_1(cert_len);
8861 *p++ = MBEDTLS_BYTE_0(cert_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008862
Gilles Peskine449bd832023-01-11 14:50:10 +01008863 if (session->peer_cert != NULL) {
8864 memcpy(p, session->peer_cert->raw.p, cert_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008865 p += cert_len;
8866 }
8867 }
8868#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008869 if (session->peer_cert_digest != NULL) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008870 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008871 if (used <= buf_len) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008872 *p++ = (unsigned char) session->peer_cert_digest_type;
8873 *p++ = (unsigned char) session->peer_cert_digest_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008874 memcpy(p, session->peer_cert_digest,
8875 session->peer_cert_digest_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008876 p += session->peer_cert_digest_len;
8877 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008878 } else {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008879 used += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01008880 if (used <= buf_len) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008881 *p++ = (unsigned char) MBEDTLS_MD_NONE;
8882 *p++ = 0;
8883 }
8884 }
8885#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8886#endif /* MBEDTLS_X509_CRT_PARSE_C */
8887
8888 /*
8889 * Session ticket if any, plus associated data
8890 */
8891#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8892 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
8893
Gilles Peskine449bd832023-01-11 14:50:10 +01008894 if (used <= buf_len) {
8895 *p++ = MBEDTLS_BYTE_2(session->ticket_len);
8896 *p++ = MBEDTLS_BYTE_1(session->ticket_len);
8897 *p++ = MBEDTLS_BYTE_0(session->ticket_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008898
Gilles Peskine449bd832023-01-11 14:50:10 +01008899 if (session->ticket != NULL) {
8900 memcpy(p, session->ticket, session->ticket_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008901 p += session->ticket_len;
8902 }
8903
Gilles Peskine449bd832023-01-11 14:50:10 +01008904 MBEDTLS_PUT_UINT32_BE(session->ticket_lifetime, p, 0);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008905 p += 4;
8906 }
8907#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8908
8909 /*
8910 * Misc extension-related info
8911 */
8912#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8913 used += 1;
8914
Gilles Peskine449bd832023-01-11 14:50:10 +01008915 if (used <= buf_len) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008916 *p++ = session->mfl_code;
Gilles Peskine449bd832023-01-11 14:50:10 +01008917 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008918#endif
8919
8920#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
8921 used += 1;
8922
Gilles Peskine449bd832023-01-11 14:50:10 +01008923 if (used <= buf_len) {
8924 *p++ = MBEDTLS_BYTE_0(session->encrypt_then_mac);
8925 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008926#endif
8927
Gilles Peskine449bd832023-01-11 14:50:10 +01008928 return used;
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008929}
8930
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02008931MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01008932static int ssl_tls12_session_load(mbedtls_ssl_session *session,
8933 const unsigned char *buf,
8934 size_t len)
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008935{
8936#if defined(MBEDTLS_HAVE_TIME)
8937 uint64_t start;
8938#endif
8939#if defined(MBEDTLS_X509_CRT_PARSE_C)
8940#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8941 size_t cert_len;
8942#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8943#endif /* MBEDTLS_X509_CRT_PARSE_C */
8944
8945 const unsigned char *p = buf;
8946 const unsigned char * const end = buf + len;
8947
8948 /*
8949 * Time
8950 */
8951#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01008952 if (8 > (size_t) (end - p)) {
8953 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
8954 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008955
Gilles Peskine449bd832023-01-11 14:50:10 +01008956 start = ((uint64_t) p[0] << 56) |
8957 ((uint64_t) p[1] << 48) |
8958 ((uint64_t) p[2] << 40) |
8959 ((uint64_t) p[3] << 32) |
8960 ((uint64_t) p[4] << 24) |
8961 ((uint64_t) p[5] << 16) |
8962 ((uint64_t) p[6] << 8) |
8963 ((uint64_t) p[7]);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008964 p += 8;
8965
8966 session->start = (time_t) start;
8967#endif /* MBEDTLS_HAVE_TIME */
8968
8969 /*
8970 * Basic mandatory fields
8971 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008972 if (2 + 1 + 32 + 48 + 4 > (size_t) (end - p)) {
8973 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
8974 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008975
Gilles Peskine449bd832023-01-11 14:50:10 +01008976 session->ciphersuite = (p[0] << 8) | p[1];
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008977 p += 2;
8978
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008979 session->id_len = *p++;
Gilles Peskine449bd832023-01-11 14:50:10 +01008980 memcpy(session->id, p, 32);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008981 p += 32;
8982
Gilles Peskine449bd832023-01-11 14:50:10 +01008983 memcpy(session->master, p, 48);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008984 p += 48;
8985
Gilles Peskine449bd832023-01-11 14:50:10 +01008986 session->verify_result = ((uint32_t) p[0] << 24) |
8987 ((uint32_t) p[1] << 16) |
8988 ((uint32_t) p[2] << 8) |
8989 ((uint32_t) p[3]);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008990 p += 4;
8991
8992 /* Immediately clear invalid pointer values that have been read, in case
8993 * we exit early before we replaced them with valid ones. */
8994#if defined(MBEDTLS_X509_CRT_PARSE_C)
8995#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8996 session->peer_cert = NULL;
8997#else
8998 session->peer_cert_digest = NULL;
8999#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9000#endif /* MBEDTLS_X509_CRT_PARSE_C */
9001#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
9002 session->ticket = NULL;
9003#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
9004
9005 /*
9006 * Peer certificate
9007 */
9008#if defined(MBEDTLS_X509_CRT_PARSE_C)
9009#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
9010 /* Deserialize CRT from the end of the ticket. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009011 if (3 > (size_t) (end - p)) {
9012 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9013 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009014
Gilles Peskine449bd832023-01-11 14:50:10 +01009015 cert_len = (p[0] << 16) | (p[1] << 8) | p[2];
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009016 p += 3;
9017
Gilles Peskine449bd832023-01-11 14:50:10 +01009018 if (cert_len != 0) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009019 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
9020
Gilles Peskine449bd832023-01-11 14:50:10 +01009021 if (cert_len > (size_t) (end - p)) {
9022 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9023 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009024
Gilles Peskine449bd832023-01-11 14:50:10 +01009025 session->peer_cert = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009026
Gilles Peskine449bd832023-01-11 14:50:10 +01009027 if (session->peer_cert == NULL) {
9028 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
9029 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009030
Gilles Peskine449bd832023-01-11 14:50:10 +01009031 mbedtls_x509_crt_init(session->peer_cert);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009032
Gilles Peskine449bd832023-01-11 14:50:10 +01009033 if ((ret = mbedtls_x509_crt_parse_der(session->peer_cert,
9034 p, cert_len)) != 0) {
9035 mbedtls_x509_crt_free(session->peer_cert);
9036 mbedtls_free(session->peer_cert);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009037 session->peer_cert = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01009038 return ret;
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009039 }
9040
9041 p += cert_len;
9042 }
9043#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9044 /* Deserialize CRT digest from the end of the ticket. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009045 if (2 > (size_t) (end - p)) {
9046 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9047 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009048
9049 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
9050 session->peer_cert_digest_len = (size_t) *p++;
9051
Gilles Peskine449bd832023-01-11 14:50:10 +01009052 if (session->peer_cert_digest_len != 0) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009053 const mbedtls_md_info_t *md_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01009054 mbedtls_md_info_from_type(session->peer_cert_digest_type);
9055 if (md_info == NULL) {
9056 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9057 }
9058 if (session->peer_cert_digest_len != mbedtls_md_get_size(md_info)) {
9059 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9060 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009061
Gilles Peskine449bd832023-01-11 14:50:10 +01009062 if (session->peer_cert_digest_len > (size_t) (end - p)) {
9063 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9064 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009065
9066 session->peer_cert_digest =
Gilles Peskine449bd832023-01-11 14:50:10 +01009067 mbedtls_calloc(1, session->peer_cert_digest_len);
9068 if (session->peer_cert_digest == NULL) {
9069 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
9070 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009071
Gilles Peskine449bd832023-01-11 14:50:10 +01009072 memcpy(session->peer_cert_digest, p,
9073 session->peer_cert_digest_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009074 p += session->peer_cert_digest_len;
9075 }
9076#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9077#endif /* MBEDTLS_X509_CRT_PARSE_C */
9078
9079 /*
9080 * Session ticket and associated data
9081 */
9082#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01009083 if (3 > (size_t) (end - p)) {
9084 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9085 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009086
Gilles Peskine449bd832023-01-11 14:50:10 +01009087 session->ticket_len = (p[0] << 16) | (p[1] << 8) | p[2];
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009088 p += 3;
9089
Gilles Peskine449bd832023-01-11 14:50:10 +01009090 if (session->ticket_len != 0) {
9091 if (session->ticket_len > (size_t) (end - p)) {
9092 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9093 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009094
Gilles Peskine449bd832023-01-11 14:50:10 +01009095 session->ticket = mbedtls_calloc(1, session->ticket_len);
9096 if (session->ticket == NULL) {
9097 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
9098 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009099
Gilles Peskine449bd832023-01-11 14:50:10 +01009100 memcpy(session->ticket, p, session->ticket_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009101 p += session->ticket_len;
9102 }
9103
Gilles Peskine449bd832023-01-11 14:50:10 +01009104 if (4 > (size_t) (end - p)) {
9105 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9106 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009107
Gilles Peskine449bd832023-01-11 14:50:10 +01009108 session->ticket_lifetime = ((uint32_t) p[0] << 24) |
9109 ((uint32_t) p[1] << 16) |
9110 ((uint32_t) p[2] << 8) |
9111 ((uint32_t) p[3]);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009112 p += 4;
9113#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
9114
9115 /*
9116 * Misc extension-related info
9117 */
9118#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01009119 if (1 > (size_t) (end - p)) {
9120 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9121 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009122
9123 session->mfl_code = *p++;
9124#endif
9125
9126#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01009127 if (1 > (size_t) (end - p)) {
9128 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9129 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009130
9131 session->encrypt_then_mac = *p++;
9132#endif
9133
9134 /* Done, should have consumed entire buffer */
Gilles Peskine449bd832023-01-11 14:50:10 +01009135 if (p != end) {
9136 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9137 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009138
Gilles Peskine449bd832023-01-11 14:50:10 +01009139 return 0;
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009140}
Jerry Yudc7bd172022-02-17 13:44:15 +08009141#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9142
XiaokangQian75d40ef2022-04-20 11:05:24 +00009143int mbedtls_ssl_validate_ciphersuite(
9144 const mbedtls_ssl_context *ssl,
9145 const mbedtls_ssl_ciphersuite_t *suite_info,
9146 mbedtls_ssl_protocol_version min_tls_version,
Gilles Peskine449bd832023-01-11 14:50:10 +01009147 mbedtls_ssl_protocol_version max_tls_version)
XiaokangQian75d40ef2022-04-20 11:05:24 +00009148{
9149 (void) ssl;
9150
Gilles Peskine449bd832023-01-11 14:50:10 +01009151 if (suite_info == NULL) {
9152 return -1;
9153 }
XiaokangQian75d40ef2022-04-20 11:05:24 +00009154
Gilles Peskine449bd832023-01-11 14:50:10 +01009155 if ((suite_info->min_tls_version > max_tls_version) ||
9156 (suite_info->max_tls_version < min_tls_version)) {
9157 return -1;
XiaokangQian75d40ef2022-04-20 11:05:24 +00009158 }
9159
XiaokangQian060d8672022-04-21 09:24:56 +00009160#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_CLI_C)
XiaokangQian75d40ef2022-04-20 11:05:24 +00009161#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Neil Armstrongca7d5062022-05-31 14:43:23 +02009162#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01009163 if (suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
9164 ssl->handshake->psa_pake_ctx_is_ok != 1)
Neil Armstrongca7d5062022-05-31 14:43:23 +02009165#else
Gilles Peskine449bd832023-01-11 14:50:10 +01009166 if (suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
9167 mbedtls_ecjpake_check(&ssl->handshake->ecjpake_ctx) != 0)
Neil Armstrongca7d5062022-05-31 14:43:23 +02009168#endif /* MBEDTLS_USE_PSA_CRYPTO */
XiaokangQian75d40ef2022-04-20 11:05:24 +00009169 {
Gilles Peskine449bd832023-01-11 14:50:10 +01009170 return -1;
XiaokangQian75d40ef2022-04-20 11:05:24 +00009171 }
9172#endif
9173
9174 /* Don't suggest PSK-based ciphersuite if no PSK is available. */
9175#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01009176 if (mbedtls_ssl_ciphersuite_uses_psk(suite_info) &&
9177 mbedtls_ssl_conf_has_static_psk(ssl->conf) == 0) {
9178 return -1;
XiaokangQian75d40ef2022-04-20 11:05:24 +00009179 }
9180#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
9181#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9182
Gilles Peskine449bd832023-01-11 14:50:10 +01009183 return 0;
XiaokangQian75d40ef2022-04-20 11:05:24 +00009184}
9185
Ronald Crone68ab4f2022-10-05 12:46:29 +02009186#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
XiaokangQianeaf36512022-04-24 09:07:44 +00009187/*
9188 * Function for writing a signature algorithm extension.
9189 *
9190 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
9191 * value (TLS 1.3 RFC8446):
9192 * enum {
9193 * ....
9194 * ecdsa_secp256r1_sha256( 0x0403 ),
9195 * ecdsa_secp384r1_sha384( 0x0503 ),
9196 * ecdsa_secp521r1_sha512( 0x0603 ),
9197 * ....
9198 * } SignatureScheme;
9199 *
9200 * struct {
9201 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
9202 * } SignatureSchemeList;
9203 *
9204 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
9205 * value (TLS 1.2 RFC5246):
9206 * enum {
9207 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
9208 * sha512(6), (255)
9209 * } HashAlgorithm;
9210 *
9211 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
9212 * SignatureAlgorithm;
9213 *
9214 * struct {
9215 * HashAlgorithm hash;
9216 * SignatureAlgorithm signature;
9217 * } SignatureAndHashAlgorithm;
9218 *
9219 * SignatureAndHashAlgorithm
9220 * supported_signature_algorithms<2..2^16-2>;
9221 *
9222 * The TLS 1.3 signature algorithm extension was defined to be a compatible
9223 * generalization of the TLS 1.2 signature algorithm extension.
9224 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
9225 * `SignatureScheme` field of TLS 1.3
9226 *
9227 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009228int mbedtls_ssl_write_sig_alg_ext(mbedtls_ssl_context *ssl, unsigned char *buf,
9229 const unsigned char *end, size_t *out_len)
XiaokangQianeaf36512022-04-24 09:07:44 +00009230{
9231 unsigned char *p = buf;
9232 unsigned char *supported_sig_alg; /* Start of supported_signature_algorithms */
9233 size_t supported_sig_alg_len = 0; /* Length of supported_signature_algorithms */
9234
9235 *out_len = 0;
9236
Gilles Peskine449bd832023-01-11 14:50:10 +01009237 MBEDTLS_SSL_DEBUG_MSG(3, ("adding signature_algorithms extension"));
XiaokangQianeaf36512022-04-24 09:07:44 +00009238
9239 /* Check if we have space for header and length field:
9240 * - extension_type (2 bytes)
9241 * - extension_data_length (2 bytes)
9242 * - supported_signature_algorithms_length (2 bytes)
9243 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009244 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 6);
XiaokangQianeaf36512022-04-24 09:07:44 +00009245 p += 6;
9246
9247 /*
9248 * Write supported_signature_algorithms
9249 */
9250 supported_sig_alg = p;
Gilles Peskine449bd832023-01-11 14:50:10 +01009251 const uint16_t *sig_alg = mbedtls_ssl_get_sig_algs(ssl);
9252 if (sig_alg == NULL) {
9253 return MBEDTLS_ERR_SSL_BAD_CONFIG;
9254 }
XiaokangQianeaf36512022-04-24 09:07:44 +00009255
Gilles Peskine449bd832023-01-11 14:50:10 +01009256 for (; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++) {
9257 MBEDTLS_SSL_DEBUG_MSG(3, ("got signature scheme [%x] %s",
9258 *sig_alg,
9259 mbedtls_ssl_sig_alg_to_str(*sig_alg)));
9260 if (!mbedtls_ssl_sig_alg_is_supported(ssl, *sig_alg)) {
XiaokangQianeaf36512022-04-24 09:07:44 +00009261 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01009262 }
9263 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
9264 MBEDTLS_PUT_UINT16_BE(*sig_alg, p, 0);
XiaokangQianeaf36512022-04-24 09:07:44 +00009265 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01009266 MBEDTLS_SSL_DEBUG_MSG(3, ("sent signature scheme [%x] %s",
9267 *sig_alg,
9268 mbedtls_ssl_sig_alg_to_str(*sig_alg)));
XiaokangQianeaf36512022-04-24 09:07:44 +00009269 }
9270
9271 /* Length of supported_signature_algorithms */
9272 supported_sig_alg_len = p - supported_sig_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01009273 if (supported_sig_alg_len == 0) {
9274 MBEDTLS_SSL_DEBUG_MSG(1, ("No signature algorithms defined."));
9275 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
XiaokangQianeaf36512022-04-24 09:07:44 +00009276 }
9277
Gilles Peskine449bd832023-01-11 14:50:10 +01009278 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SIG_ALG, buf, 0);
9279 MBEDTLS_PUT_UINT16_BE(supported_sig_alg_len + 2, buf, 2);
9280 MBEDTLS_PUT_UINT16_BE(supported_sig_alg_len, buf, 4);
XiaokangQianeaf36512022-04-24 09:07:44 +00009281
XiaokangQianeaf36512022-04-24 09:07:44 +00009282 *out_len = p - buf;
9283
9284#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01009285 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_SIG_ALG);
XiaokangQianeaf36512022-04-24 09:07:44 +00009286#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu0c354a22022-08-29 15:25:36 +08009287
Gilles Peskine449bd832023-01-11 14:50:10 +01009288 return 0;
XiaokangQianeaf36512022-04-24 09:07:44 +00009289}
Ronald Crone68ab4f2022-10-05 12:46:29 +02009290#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
XiaokangQianeaf36512022-04-24 09:07:44 +00009291
XiaokangQian40a35232022-05-07 09:02:40 +00009292#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
XiaokangQian9b2b7712022-05-17 02:57:00 +00009293/*
9294 * mbedtls_ssl_parse_server_name_ext
9295 *
9296 * Structure of server_name extension:
9297 *
9298 * enum {
9299 * host_name(0), (255)
9300 * } NameType;
9301 * opaque HostName<1..2^16-1>;
9302 *
9303 * struct {
9304 * NameType name_type;
9305 * select (name_type) {
9306 * case host_name: HostName;
9307 * } name;
9308 * } ServerName;
9309 * struct {
9310 * ServerName server_name_list<1..2^16-1>
9311 * } ServerNameList;
9312 */
Ronald Cronce7d76e2022-07-08 18:56:49 +02009313MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01009314int mbedtls_ssl_parse_server_name_ext(mbedtls_ssl_context *ssl,
9315 const unsigned char *buf,
9316 const unsigned char *end)
XiaokangQian40a35232022-05-07 09:02:40 +00009317{
9318 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
9319 const unsigned char *p = buf;
XiaokangQian9b2b7712022-05-17 02:57:00 +00009320 size_t server_name_list_len, hostname_len;
9321 const unsigned char *server_name_list_end;
XiaokangQian40a35232022-05-07 09:02:40 +00009322
Gilles Peskine449bd832023-01-11 14:50:10 +01009323 MBEDTLS_SSL_DEBUG_MSG(3, ("parse ServerName extension"));
XiaokangQian40a35232022-05-07 09:02:40 +00009324
Gilles Peskine449bd832023-01-11 14:50:10 +01009325 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
9326 server_name_list_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian40a35232022-05-07 09:02:40 +00009327 p += 2;
9328
Gilles Peskine449bd832023-01-11 14:50:10 +01009329 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, server_name_list_len);
XiaokangQian9b2b7712022-05-17 02:57:00 +00009330 server_name_list_end = p + server_name_list_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01009331 while (p < server_name_list_end) {
9332 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, server_name_list_end, 3);
9333 hostname_len = MBEDTLS_GET_UINT16_BE(p, 1);
9334 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, server_name_list_end,
9335 hostname_len + 3);
XiaokangQian40a35232022-05-07 09:02:40 +00009336
Gilles Peskine449bd832023-01-11 14:50:10 +01009337 if (p[0] == MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME) {
XiaokangQian75fe8c72022-06-15 09:42:45 +00009338 /* sni_name is intended to be used only during the parsing of the
9339 * ClientHello message (it is reset to NULL before the end of
9340 * the message parsing). Thus it is ok to just point to the
9341 * reception buffer and not make a copy of it.
9342 */
XiaokangQianf2a94202022-05-20 06:44:24 +00009343 ssl->handshake->sni_name = p + 3;
9344 ssl->handshake->sni_name_len = hostname_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01009345 if (ssl->conf->f_sni == NULL) {
9346 return 0;
XiaokangQian40a35232022-05-07 09:02:40 +00009347 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009348 ret = ssl->conf->f_sni(ssl->conf->p_sni,
9349 ssl, p + 3, hostname_len);
9350 if (ret != 0) {
9351 MBEDTLS_SSL_DEBUG_RET(1, "ssl_sni_wrapper", ret);
9352 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_UNRECOGNIZED_NAME,
9353 MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME);
9354 return MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME;
9355 }
9356 return 0;
XiaokangQian40a35232022-05-07 09:02:40 +00009357 }
9358
9359 p += hostname_len + 3;
9360 }
9361
Gilles Peskine449bd832023-01-11 14:50:10 +01009362 return 0;
XiaokangQian40a35232022-05-07 09:02:40 +00009363}
9364#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
9365
XiaokangQianacb39922022-06-17 10:18:48 +00009366#if defined(MBEDTLS_SSL_ALPN)
Ronald Cronce7d76e2022-07-08 18:56:49 +02009367MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01009368int mbedtls_ssl_parse_alpn_ext(mbedtls_ssl_context *ssl,
9369 const unsigned char *buf,
9370 const unsigned char *end)
XiaokangQianacb39922022-06-17 10:18:48 +00009371{
9372 const unsigned char *p = buf;
XiaokangQianc7403452022-06-23 03:24:12 +00009373 size_t protocol_name_list_len;
XiaokangQian95d5f542022-06-24 02:29:26 +00009374 const unsigned char *protocol_name_list;
9375 const unsigned char *protocol_name_list_end;
XiaokangQianc7403452022-06-23 03:24:12 +00009376 size_t protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009377
9378 /* If ALPN not configured, just ignore the extension */
Gilles Peskine449bd832023-01-11 14:50:10 +01009379 if (ssl->conf->alpn_list == NULL) {
9380 return 0;
9381 }
XiaokangQianacb39922022-06-17 10:18:48 +00009382
9383 /*
XiaokangQianc7403452022-06-23 03:24:12 +00009384 * RFC7301, section 3.1
9385 * opaque ProtocolName<1..2^8-1>;
XiaokangQianacb39922022-06-17 10:18:48 +00009386 *
XiaokangQianc7403452022-06-23 03:24:12 +00009387 * struct {
9388 * ProtocolName protocol_name_list<2..2^16-1>
9389 * } ProtocolNameList;
XiaokangQianacb39922022-06-17 10:18:48 +00009390 */
9391
XiaokangQianc7403452022-06-23 03:24:12 +00009392 /*
XiaokangQian0b776e22022-06-24 09:04:59 +00009393 * protocol_name_list_len 2 bytes
9394 * protocol_name_len 1 bytes
9395 * protocol_name >=1 byte
XiaokangQianc7403452022-06-23 03:24:12 +00009396 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009397 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 4);
XiaokangQianacb39922022-06-17 10:18:48 +00009398
Gilles Peskine449bd832023-01-11 14:50:10 +01009399 protocol_name_list_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQianacb39922022-06-17 10:18:48 +00009400 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01009401 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, protocol_name_list_len);
XiaokangQian95d5f542022-06-24 02:29:26 +00009402 protocol_name_list = p;
9403 protocol_name_list_end = p + protocol_name_list_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009404
9405 /* Validate peer's list (lengths) */
Gilles Peskine449bd832023-01-11 14:50:10 +01009406 while (p < protocol_name_list_end) {
XiaokangQian95d5f542022-06-24 02:29:26 +00009407 protocol_name_len = *p++;
Gilles Peskine449bd832023-01-11 14:50:10 +01009408 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, protocol_name_list_end,
9409 protocol_name_len);
9410 if (protocol_name_len == 0) {
XiaokangQian95d5f542022-06-24 02:29:26 +00009411 MBEDTLS_SSL_PEND_FATAL_ALERT(
9412 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
Gilles Peskine449bd832023-01-11 14:50:10 +01009413 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
9414 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
XiaokangQian95d5f542022-06-24 02:29:26 +00009415 }
9416
9417 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009418 }
9419
9420 /* Use our order of preference */
Gilles Peskine449bd832023-01-11 14:50:10 +01009421 for (const char **alpn = ssl->conf->alpn_list; *alpn != NULL; alpn++) {
9422 size_t const alpn_len = strlen(*alpn);
XiaokangQian95d5f542022-06-24 02:29:26 +00009423 p = protocol_name_list;
Gilles Peskine449bd832023-01-11 14:50:10 +01009424 while (p < protocol_name_list_end) {
XiaokangQian95d5f542022-06-24 02:29:26 +00009425 protocol_name_len = *p++;
Gilles Peskine449bd832023-01-11 14:50:10 +01009426 if (protocol_name_len == alpn_len &&
9427 memcmp(p, *alpn, alpn_len) == 0) {
XiaokangQianacb39922022-06-17 10:18:48 +00009428 ssl->alpn_chosen = *alpn;
Gilles Peskine449bd832023-01-11 14:50:10 +01009429 return 0;
XiaokangQianacb39922022-06-17 10:18:48 +00009430 }
XiaokangQian95d5f542022-06-24 02:29:26 +00009431
9432 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009433 }
9434 }
9435
XiaokangQian95d5f542022-06-24 02:29:26 +00009436 /* If we get here, no match was found */
XiaokangQianacb39922022-06-17 10:18:48 +00009437 MBEDTLS_SSL_PEND_FATAL_ALERT(
Gilles Peskine449bd832023-01-11 14:50:10 +01009438 MBEDTLS_SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL,
9439 MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL);
9440 return MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL;
XiaokangQianacb39922022-06-17 10:18:48 +00009441}
9442
Gilles Peskine449bd832023-01-11 14:50:10 +01009443int mbedtls_ssl_write_alpn_ext(mbedtls_ssl_context *ssl,
9444 unsigned char *buf,
9445 unsigned char *end,
9446 size_t *out_len)
XiaokangQianacb39922022-06-17 10:18:48 +00009447{
9448 unsigned char *p = buf;
XiaokangQian95d5f542022-06-24 02:29:26 +00009449 size_t protocol_name_len;
XiaokangQianc7403452022-06-23 03:24:12 +00009450 *out_len = 0;
XiaokangQianacb39922022-06-17 10:18:48 +00009451
Gilles Peskine449bd832023-01-11 14:50:10 +01009452 if (ssl->alpn_chosen == NULL) {
9453 return 0;
XiaokangQianacb39922022-06-17 10:18:48 +00009454 }
9455
Gilles Peskine449bd832023-01-11 14:50:10 +01009456 protocol_name_len = strlen(ssl->alpn_chosen);
9457 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 7 + protocol_name_len);
XiaokangQianacb39922022-06-17 10:18:48 +00009458
Gilles Peskine449bd832023-01-11 14:50:10 +01009459 MBEDTLS_SSL_DEBUG_MSG(3, ("server side, adding alpn extension"));
XiaokangQianacb39922022-06-17 10:18:48 +00009460 /*
9461 * 0 . 1 ext identifier
9462 * 2 . 3 ext length
9463 * 4 . 5 protocol list length
9464 * 6 . 6 protocol name length
9465 * 7 . 7+n protocol name
9466 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009467 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_ALPN, p, 0);
XiaokangQianacb39922022-06-17 10:18:48 +00009468
XiaokangQian95d5f542022-06-24 02:29:26 +00009469 *out_len = 7 + protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009470
Gilles Peskine449bd832023-01-11 14:50:10 +01009471 MBEDTLS_PUT_UINT16_BE(protocol_name_len + 3, p, 2);
9472 MBEDTLS_PUT_UINT16_BE(protocol_name_len + 1, p, 4);
XiaokangQian0b776e22022-06-24 09:04:59 +00009473 /* Note: the length of the chosen protocol has been checked to be less
9474 * than 255 bytes in `mbedtls_ssl_conf_alpn_protocols`.
9475 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009476 p[6] = MBEDTLS_BYTE_0(protocol_name_len);
XiaokangQianacb39922022-06-17 10:18:48 +00009477
Gilles Peskine449bd832023-01-11 14:50:10 +01009478 memcpy(p + 7, ssl->alpn_chosen, protocol_name_len);
Jerry Yub95dd362022-11-08 21:19:34 +08009479
9480#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01009481 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_ALPN);
Jerry Yub95dd362022-11-08 21:19:34 +08009482#endif
9483
Gilles Peskine449bd832023-01-11 14:50:10 +01009484 return 0;
XiaokangQianacb39922022-06-17 10:18:48 +00009485}
9486#endif /* MBEDTLS_SSL_ALPN */
9487
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009488#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
Xiaokang Qian03409292022-10-12 02:49:52 +00009489 defined(MBEDTLS_SSL_SESSION_TICKETS) && \
Xiaokang Qianed0620c2022-10-12 06:58:13 +00009490 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \
Xiaokang Qianed3afcd2022-10-12 08:31:11 +00009491 defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01009492int mbedtls_ssl_session_set_hostname(mbedtls_ssl_session *session,
9493 const char *hostname)
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009494{
9495 /* Initialize to suppress unnecessary compiler warning */
9496 size_t hostname_len = 0;
9497
9498 /* Check if new hostname is valid before
9499 * making any change to current one */
Gilles Peskine449bd832023-01-11 14:50:10 +01009500 if (hostname != NULL) {
9501 hostname_len = strlen(hostname);
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009502
Gilles Peskine449bd832023-01-11 14:50:10 +01009503 if (hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN) {
9504 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9505 }
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009506 }
9507
9508 /* Now it's clear that we will overwrite the old hostname,
9509 * so we can free it safely */
Gilles Peskine449bd832023-01-11 14:50:10 +01009510 if (session->hostname != NULL) {
9511 mbedtls_platform_zeroize(session->hostname,
9512 strlen(session->hostname));
9513 mbedtls_free(session->hostname);
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009514 }
9515
9516 /* Passing NULL as hostname shall clear the old one */
Gilles Peskine449bd832023-01-11 14:50:10 +01009517 if (hostname == NULL) {
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009518 session->hostname = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01009519 } else {
9520 session->hostname = mbedtls_calloc(1, hostname_len + 1);
9521 if (session->hostname == NULL) {
9522 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
9523 }
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009524
Gilles Peskine449bd832023-01-11 14:50:10 +01009525 memcpy(session->hostname, hostname, hostname_len);
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009526 }
9527
Gilles Peskine449bd832023-01-11 14:50:10 +01009528 return 0;
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009529}
Xiaokang Qian03409292022-10-12 02:49:52 +00009530#endif /* MBEDTLS_SSL_PROTO_TLS1_3 &&
9531 MBEDTLS_SSL_SESSION_TICKETS &&
Xiaokang Qianed0620c2022-10-12 06:58:13 +00009532 MBEDTLS_SSL_SERVER_NAME_INDICATION &&
Xiaokang Qianed3afcd2022-10-12 08:31:11 +00009533 MBEDTLS_SSL_CLI_C */
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009534
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009535#endif /* MBEDTLS_SSL_TLS_C */