blob: c881872c9424b64128fd49987bccd29e7a031bea [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
Gilles Peskine449bd832023-01-11 14:50:10 +0100791void mbedtls_ssl_add_hs_hdr_to_checksum(mbedtls_ssl_context *ssl,
792 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
Gilles Peskine449bd832023-01-11 14:50:10 +0100803 ssl->handshake->update_checksum(ssl, hs_hdr, sizeof(hs_hdr));
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100804}
805
Gilles Peskine449bd832023-01-11 14:50:10 +0100806void mbedtls_ssl_add_hs_msg_to_checksum(mbedtls_ssl_context *ssl,
807 unsigned hs_type,
808 unsigned char const *msg,
809 size_t msg_len)
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100810{
Gilles Peskine449bd832023-01-11 14:50:10 +0100811 mbedtls_ssl_add_hs_hdr_to_checksum(ssl, hs_type, msg_len);
812 ssl->handshake->update_checksum(ssl, msg, msg_len);
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100813}
814
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100815int mbedtls_ssl_reset_checksum(mbedtls_ssl_context *ssl)
Jerry Yuc73c6182022-02-08 20:29:25 +0800816{
817 ((void) ssl);
Andrzej Kurek25f27152022-08-17 16:09:31 -0400818#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800819#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100820 psa_hash_abort(&ssl->handshake->fin_sha256_psa);
821 psa_hash_setup(&ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256);
Jerry Yuc73c6182022-02-08 20:29:25 +0800822#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100823 mbedtls_sha256_starts(&ssl->handshake->fin_sha256, 0);
Jerry Yuc73c6182022-02-08 20:29:25 +0800824#endif
825#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400826#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800827#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100828 psa_hash_abort(&ssl->handshake->fin_sha384_psa);
829 psa_hash_setup(&ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384);
Jerry Yuc73c6182022-02-08 20:29:25 +0800830#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100831 mbedtls_sha512_starts(&ssl->handshake->fin_sha384, 1);
Jerry Yuc73c6182022-02-08 20:29:25 +0800832#endif
833#endif
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100834 return 0;
Jerry Yuc73c6182022-02-08 20:29:25 +0800835}
836
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100837static int ssl_update_checksum_start(mbedtls_ssl_context *ssl,
Gilles Peskine449bd832023-01-11 14:50:10 +0100838 const unsigned char *buf, size_t len)
Jerry Yuc73c6182022-02-08 20:29:25 +0800839{
Andrzej Kurek25f27152022-08-17 16:09:31 -0400840#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800841#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100842 psa_hash_update(&ssl->handshake->fin_sha256_psa, buf, len);
Jerry Yuc73c6182022-02-08 20:29:25 +0800843#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100844 mbedtls_sha256_update(&ssl->handshake->fin_sha256, buf, len);
Jerry Yuc73c6182022-02-08 20:29:25 +0800845#endif
846#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400847#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800848#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100849 psa_hash_update(&ssl->handshake->fin_sha384_psa, buf, len);
Jerry Yuc73c6182022-02-08 20:29:25 +0800850#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100851 mbedtls_sha512_update(&ssl->handshake->fin_sha384, buf, len);
Jerry Yuc73c6182022-02-08 20:29:25 +0800852#endif
853#endif
Andrzej Kurekeabeb302022-10-17 07:52:51 -0400854#if !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
855 !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
856 (void) ssl;
857 (void) buf;
858 (void) len;
859#endif
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100860 return 0;
Jerry Yuc73c6182022-02-08 20:29:25 +0800861}
862
Andrzej Kurek25f27152022-08-17 16:09:31 -0400863#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100864static int ssl_update_checksum_sha256(mbedtls_ssl_context *ssl,
Gilles Peskine449bd832023-01-11 14:50:10 +0100865 const unsigned char *buf, size_t len)
Jerry Yuc73c6182022-02-08 20:29:25 +0800866{
867#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100868 psa_hash_update(&ssl->handshake->fin_sha256_psa, buf, len);
Jerry Yuc73c6182022-02-08 20:29:25 +0800869#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100870 mbedtls_sha256_update(&ssl->handshake->fin_sha256, buf, len);
Jerry Yuc73c6182022-02-08 20:29:25 +0800871#endif
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100872 return 0;
Jerry Yuc73c6182022-02-08 20:29:25 +0800873}
874#endif
875
Andrzej Kurek25f27152022-08-17 16:09:31 -0400876#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100877static int ssl_update_checksum_sha384(mbedtls_ssl_context *ssl,
Gilles Peskine449bd832023-01-11 14:50:10 +0100878 const unsigned char *buf, size_t len)
Jerry Yuc73c6182022-02-08 20:29:25 +0800879{
880#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100881 psa_hash_update(&ssl->handshake->fin_sha384_psa, buf, len);
Jerry Yuc73c6182022-02-08 20:29:25 +0800882#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100883 mbedtls_sha512_update(&ssl->handshake->fin_sha384, buf, len);
Jerry Yuc73c6182022-02-08 20:29:25 +0800884#endif
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100885 return 0;
Jerry Yuc73c6182022-02-08 20:29:25 +0800886}
887#endif
888
Gilles Peskine449bd832023-01-11 14:50:10 +0100889static void ssl_handshake_params_init(mbedtls_ssl_handshake_params *handshake)
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200890{
Gilles Peskine449bd832023-01-11 14:50:10 +0100891 memset(handshake, 0, sizeof(mbedtls_ssl_handshake_params));
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200892
Andrzej Kurek25f27152022-08-17 16:09:31 -0400893#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -0500894#if defined(MBEDTLS_USE_PSA_CRYPTO)
895 handshake->fin_sha256_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -0500896#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100897 mbedtls_sha256_init(&handshake->fin_sha256);
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200898#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -0500899#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400900#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -0500901#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -0500902 handshake->fin_sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -0500903#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100904 mbedtls_sha512_init(&handshake->fin_sha384);
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200905#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -0500906#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200907
908 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +0100909
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200910#if defined(MBEDTLS_DHM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100911 mbedtls_dhm_init(&handshake->dhm_ctx);
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200912#endif
Neil Armstrongf3f46412022-04-12 14:43:39 +0200913#if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100914 mbedtls_ecdh_init(&handshake->ecdh_ctx);
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200915#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +0200916#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Neil Armstrongca7d5062022-05-31 14:43:23 +0200917#if defined(MBEDTLS_USE_PSA_CRYPTO)
918 handshake->psa_pake_ctx = psa_pake_operation_init();
919 handshake->psa_pake_password = MBEDTLS_SVC_KEY_ID_INIT;
920#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100921 mbedtls_ecjpake_init(&handshake->ecjpake_ctx);
Neil Armstrongca7d5062022-05-31 14:43:23 +0200922#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +0200923#if defined(MBEDTLS_SSL_CLI_C)
924 handshake->ecjpake_cache = NULL;
925 handshake->ecjpake_cache_len = 0;
926#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +0200927#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +0200928
Gilles Peskineeccd8882020-03-10 12:19:08 +0100929#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100930 mbedtls_x509_crt_restart_init(&handshake->ecrs_ctx);
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +0200931#endif
932
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +0200933#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
934 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
935#endif
Hanno Becker75173122019-02-06 16:18:31 +0000936
937#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
938 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100939 mbedtls_pk_init(&handshake->peer_pubkey);
Hanno Becker75173122019-02-06 16:18:31 +0000940#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200941}
942
Gilles Peskine449bd832023-01-11 14:50:10 +0100943void mbedtls_ssl_transform_init(mbedtls_ssl_transform *transform)
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200944{
Gilles Peskine449bd832023-01-11 14:50:10 +0100945 memset(transform, 0, sizeof(mbedtls_ssl_transform));
Paul Bakker84bbeb52014-07-01 14:53:22 +0200946
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +0100947#if defined(MBEDTLS_USE_PSA_CRYPTO)
948 transform->psa_key_enc = MBEDTLS_SVC_KEY_ID_INIT;
949 transform->psa_key_dec = MBEDTLS_SVC_KEY_ID_INIT;
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +0100950#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100951 mbedtls_cipher_init(&transform->cipher_ctx_enc);
952 mbedtls_cipher_init(&transform->cipher_ctx_dec);
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +0100953#endif
954
Hanno Beckerfd86ca82020-11-30 08:54:23 +0000955#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong39b8e7d2022-02-23 09:24:45 +0100956#if defined(MBEDTLS_USE_PSA_CRYPTO)
957 transform->psa_mac_enc = MBEDTLS_SVC_KEY_ID_INIT;
958 transform->psa_mac_dec = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrongcf8841a2022-02-24 11:17:45 +0100959#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100960 mbedtls_md_init(&transform->md_ctx_enc);
961 mbedtls_md_init(&transform->md_ctx_dec);
Hanno Beckerd56ed242018-01-03 15:32:51 +0000962#endif
Neil Armstrongcf8841a2022-02-24 11:17:45 +0100963#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200964}
965
Gilles Peskine449bd832023-01-11 14:50:10 +0100966void mbedtls_ssl_session_init(mbedtls_ssl_session *session)
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200967{
Gilles Peskine449bd832023-01-11 14:50:10 +0100968 memset(session, 0, sizeof(mbedtls_ssl_session));
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200969}
970
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200971MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100972static int ssl_handshake_init(mbedtls_ssl_context *ssl)
Paul Bakker48916f92012-09-16 19:57:18 +0000973{
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200974 /* Clear old handshake information if present */
Jerry Yu2e199812022-12-01 18:57:19 +0800975#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +0100976 if (ssl->transform_negotiate) {
977 mbedtls_ssl_transform_free(ssl->transform_negotiate);
978 }
Jerry Yu2e199812022-12-01 18:57:19 +0800979#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100980 if (ssl->session_negotiate) {
981 mbedtls_ssl_session_free(ssl->session_negotiate);
982 }
983 if (ssl->handshake) {
984 mbedtls_ssl_handshake_free(ssl);
985 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200986
Jerry Yu2e199812022-12-01 18:57:19 +0800987#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200988 /*
989 * Either the pointers are now NULL or cleared properly and can be freed.
990 * Now allocate missing structures.
991 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100992 if (ssl->transform_negotiate == NULL) {
993 ssl->transform_negotiate = mbedtls_calloc(1, sizeof(mbedtls_ssl_transform));
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200994 }
Jerry Yu2e199812022-12-01 18:57:19 +0800995#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker48916f92012-09-16 19:57:18 +0000996
Gilles Peskine449bd832023-01-11 14:50:10 +0100997 if (ssl->session_negotiate == NULL) {
998 ssl->session_negotiate = mbedtls_calloc(1, sizeof(mbedtls_ssl_session));
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200999 }
Paul Bakker48916f92012-09-16 19:57:18 +00001000
Gilles Peskine449bd832023-01-11 14:50:10 +01001001 if (ssl->handshake == NULL) {
1002 ssl->handshake = mbedtls_calloc(1, sizeof(mbedtls_ssl_handshake_params));
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001003 }
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05001004#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1005 /* If the buffers are too small - reallocate */
Andrzej Kurek8ea68722020-04-03 06:40:47 -04001006
Gilles Peskine449bd832023-01-11 14:50:10 +01001007 handle_buffer_resizing(ssl, 0, MBEDTLS_SSL_IN_BUFFER_LEN,
1008 MBEDTLS_SSL_OUT_BUFFER_LEN);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05001009#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001010
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001011 /* All pointers should exist and can be directly freed without issue */
Gilles Peskine449bd832023-01-11 14:50:10 +01001012 if (ssl->handshake == NULL ||
Jerry Yu2e199812022-12-01 18:57:19 +08001013#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker48916f92012-09-16 19:57:18 +00001014 ssl->transform_negotiate == NULL ||
Jerry Yu2e199812022-12-01 18:57:19 +08001015#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001016 ssl->session_negotiate == NULL) {
1017 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc() of ssl sub-contexts failed"));
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001018
Gilles Peskine449bd832023-01-11 14:50:10 +01001019 mbedtls_free(ssl->handshake);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001020 ssl->handshake = NULL;
Jerry Yu2e199812022-12-01 18:57:19 +08001021
1022#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01001023 mbedtls_free(ssl->transform_negotiate);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001024 ssl->transform_negotiate = NULL;
Jerry Yu2e199812022-12-01 18:57:19 +08001025#endif
1026
Gilles Peskine449bd832023-01-11 14:50:10 +01001027 mbedtls_free(ssl->session_negotiate);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001028 ssl->session_negotiate = NULL;
1029
Gilles Peskine449bd832023-01-11 14:50:10 +01001030 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Paul Bakker48916f92012-09-16 19:57:18 +00001031 }
1032
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001033 /* Initialize structures */
Gilles Peskine449bd832023-01-11 14:50:10 +01001034 mbedtls_ssl_session_init(ssl->session_negotiate);
1035 ssl_handshake_params_init(ssl->handshake);
Paul Bakker968afaa2014-07-09 11:09:24 +02001036
Jerry Yu2e199812022-12-01 18:57:19 +08001037#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01001038 mbedtls_ssl_transform_init(ssl->transform_negotiate);
Jerry Yu2e199812022-12-01 18:57:19 +08001039#endif
1040
Manuel Pégourié-Gonnard537f2312023-02-05 10:17:45 +01001041 /* Setup handshake checksums */
1042 mbedtls_ssl_reset_checksum(ssl);
1043
Jerry Yud0766ec2022-09-22 10:46:57 +08001044#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
1045 defined(MBEDTLS_SSL_SRV_C) && \
1046 defined(MBEDTLS_SSL_SESSION_TICKETS)
1047 ssl->handshake->new_session_tickets_count =
Gilles Peskine449bd832023-01-11 14:50:10 +01001048 ssl->conf->new_session_tickets_count;
Jerry Yud0766ec2022-09-22 10:46:57 +08001049#endif
1050
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001051#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001052 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001053 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001054
Gilles Peskine449bd832023-01-11 14:50:10 +01001055 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001056 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Gilles Peskine449bd832023-01-11 14:50:10 +01001057 } else {
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001058 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Gilles Peskine449bd832023-01-11 14:50:10 +01001059 }
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001060
Gilles Peskine449bd832023-01-11 14:50:10 +01001061 mbedtls_ssl_set_timer(ssl, 0);
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001062 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001063#endif
1064
Brett Warrene0edc842021-08-17 09:53:13 +01001065/*
1066 * curve_list is translated to IANA TLS group identifiers here because
1067 * mbedtls_ssl_conf_curves returns void and so can't return
1068 * any error codes.
1069 */
1070#if defined(MBEDTLS_ECP_C)
1071#if !defined(MBEDTLS_DEPRECATED_REMOVED)
1072 /* Heap allocate and translate curve_list from internal to IANA group ids */
Gilles Peskine449bd832023-01-11 14:50:10 +01001073 if (ssl->conf->curve_list != NULL) {
Brett Warrene0edc842021-08-17 09:53:13 +01001074 size_t length;
1075 const mbedtls_ecp_group_id *curve_list = ssl->conf->curve_list;
1076
Gilles Peskine449bd832023-01-11 14:50:10 +01001077 for (length = 0; (curve_list[length] != MBEDTLS_ECP_DP_NONE) &&
1078 (length < MBEDTLS_ECP_DP_MAX); length++) {
1079 }
Brett Warrene0edc842021-08-17 09:53:13 +01001080
1081 /* Leave room for zero termination */
Gilles Peskine449bd832023-01-11 14:50:10 +01001082 uint16_t *group_list = mbedtls_calloc(length + 1, sizeof(uint16_t));
1083 if (group_list == NULL) {
1084 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
1085 }
Brett Warrene0edc842021-08-17 09:53:13 +01001086
Gilles Peskine449bd832023-01-11 14:50:10 +01001087 for (size_t i = 0; i < length; i++) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01001088 uint16_t tls_id = mbedtls_ssl_get_tls_id_from_ecp_group_id(
Gilles Peskine449bd832023-01-11 14:50:10 +01001089 curve_list[i]);
1090 if (tls_id == 0) {
1091 mbedtls_free(group_list);
1092 return MBEDTLS_ERR_SSL_BAD_CONFIG;
Brett Warrene0edc842021-08-17 09:53:13 +01001093 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01001094 group_list[i] = tls_id;
Brett Warrene0edc842021-08-17 09:53:13 +01001095 }
1096
1097 group_list[length] = 0;
1098
1099 ssl->handshake->group_list = group_list;
1100 ssl->handshake->group_list_heap_allocated = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001101 } else {
Brett Warrene0edc842021-08-17 09:53:13 +01001102 ssl->handshake->group_list = ssl->conf->group_list;
1103 ssl->handshake->group_list_heap_allocated = 0;
1104 }
1105#endif /* MBEDTLS_DEPRECATED_REMOVED */
1106#endif /* MBEDTLS_ECP_C */
1107
Ronald Crone68ab4f2022-10-05 12:46:29 +02001108#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +08001109#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Jerry Yua69269a2022-01-17 21:06:01 +08001110#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yu713013f2022-01-17 18:16:35 +08001111 /* Heap allocate and translate sig_hashes from internal hash identifiers to
1112 signature algorithms IANA identifiers. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001113 if (mbedtls_ssl_conf_is_tls12_only(ssl->conf) &&
1114 ssl->conf->sig_hashes != NULL) {
Jerry Yuf017ee42022-01-12 15:49:48 +08001115 const int *md;
1116 const int *sig_hashes = ssl->conf->sig_hashes;
Jerry Yub476a442022-01-21 18:14:45 +08001117 size_t sig_algs_len = 0;
Jerry Yuf017ee42022-01-12 15:49:48 +08001118 uint16_t *p;
1119
Jerry Yub476a442022-01-21 18:14:45 +08001120#if defined(static_assert)
Gilles Peskine449bd832023-01-11 14:50:10 +01001121 static_assert(MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN
1122 <= (SIZE_MAX - (2 * sizeof(uint16_t))),
1123 "MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN too big");
Jerry Yua68dca22022-01-20 16:28:27 +08001124#endif
1125
Gilles Peskine449bd832023-01-11 14:50:10 +01001126 for (md = sig_hashes; *md != MBEDTLS_MD_NONE; md++) {
1127 if (mbedtls_ssl_hash_from_md_alg(*md) == MBEDTLS_SSL_HASH_NONE) {
Jerry Yuf017ee42022-01-12 15:49:48 +08001128 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001129 }
Jerry Yub476a442022-01-21 18:14:45 +08001130#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001131 sig_algs_len += sizeof(uint16_t);
Jerry Yub476a442022-01-21 18:14:45 +08001132#endif
Jerry Yua68dca22022-01-20 16:28:27 +08001133
Jerry Yub476a442022-01-21 18:14:45 +08001134#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001135 sig_algs_len += sizeof(uint16_t);
Jerry Yub476a442022-01-21 18:14:45 +08001136#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001137 if (sig_algs_len > MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN) {
1138 return MBEDTLS_ERR_SSL_BAD_CONFIG;
1139 }
Jerry Yuf017ee42022-01-12 15:49:48 +08001140 }
1141
Gilles Peskine449bd832023-01-11 14:50:10 +01001142 if (sig_algs_len < MBEDTLS_SSL_MIN_SIG_ALG_LIST_LEN) {
1143 return MBEDTLS_ERR_SSL_BAD_CONFIG;
1144 }
Jerry Yuf017ee42022-01-12 15:49:48 +08001145
Gilles Peskine449bd832023-01-11 14:50:10 +01001146 ssl->handshake->sig_algs = mbedtls_calloc(1, sig_algs_len +
1147 sizeof(uint16_t));
1148 if (ssl->handshake->sig_algs == NULL) {
1149 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
1150 }
Jerry Yuf017ee42022-01-12 15:49:48 +08001151
Gilles Peskine449bd832023-01-11 14:50:10 +01001152 p = (uint16_t *) ssl->handshake->sig_algs;
1153 for (md = sig_hashes; *md != MBEDTLS_MD_NONE; md++) {
1154 unsigned char hash = mbedtls_ssl_hash_from_md_alg(*md);
1155 if (hash == MBEDTLS_SSL_HASH_NONE) {
Jerry Yuf017ee42022-01-12 15:49:48 +08001156 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001157 }
Jerry Yu6106fdc2022-01-12 16:36:14 +08001158#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001159 *p = ((hash << 8) | MBEDTLS_SSL_SIG_ECDSA);
Jerry Yuf017ee42022-01-12 15:49:48 +08001160 p++;
Jerry Yu6106fdc2022-01-12 16:36:14 +08001161#endif
1162#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001163 *p = ((hash << 8) | MBEDTLS_SSL_SIG_RSA);
Jerry Yuf017ee42022-01-12 15:49:48 +08001164 p++;
Jerry Yu6106fdc2022-01-12 16:36:14 +08001165#endif
Jerry Yuf017ee42022-01-12 15:49:48 +08001166 }
Gabor Mezei15b95a62022-05-09 16:37:58 +02001167 *p = MBEDTLS_TLS_SIG_NONE;
Jerry Yuf017ee42022-01-12 15:49:48 +08001168 ssl->handshake->sig_algs_heap_allocated = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001169 } else
Jerry Yua69269a2022-01-17 21:06:01 +08001170#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Jerry Yuf017ee42022-01-12 15:49:48 +08001171 {
Jerry Yuf017ee42022-01-12 15:49:48 +08001172 ssl->handshake->sig_algs_heap_allocated = 0;
1173 }
Jerry Yucc539102022-06-27 16:27:35 +08001174#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Ronald Crone68ab4f2022-10-05 12:46:29 +02001175#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Gilles Peskine449bd832023-01-11 14:50:10 +01001176 return 0;
Paul Bakker48916f92012-09-16 19:57:18 +00001177}
1178
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001179#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001180/* Dummy cookie callbacks for defaults */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001181MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001182static int ssl_cookie_write_dummy(void *ctx,
1183 unsigned char **p, unsigned char *end,
1184 const unsigned char *cli_id, size_t cli_id_len)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001185{
1186 ((void) ctx);
1187 ((void) p);
1188 ((void) end);
1189 ((void) cli_id);
1190 ((void) cli_id_len);
1191
Gilles Peskine449bd832023-01-11 14:50:10 +01001192 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001193}
1194
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001195MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001196static int ssl_cookie_check_dummy(void *ctx,
1197 const unsigned char *cookie, size_t cookie_len,
1198 const unsigned char *cli_id, size_t cli_id_len)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001199{
1200 ((void) ctx);
1201 ((void) cookie);
1202 ((void) cookie_len);
1203 ((void) cli_id);
1204 ((void) cli_id_len);
1205
Gilles Peskine449bd832023-01-11 14:50:10 +01001206 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001207}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001208#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001209
Paul Bakker5121ce52009-01-03 21:22:43 +00001210/*
1211 * Initialize an SSL context
1212 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001213void mbedtls_ssl_init(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001214{
Gilles Peskine449bd832023-01-11 14:50:10 +01001215 memset(ssl, 0, sizeof(mbedtls_ssl_context));
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001216}
1217
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001218MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001219static int ssl_conf_version_check(const mbedtls_ssl_context *ssl)
Jerry Yu60835a82021-08-04 10:13:52 +08001220{
Ronald Cron086ee0b2022-03-15 15:18:51 +01001221 const mbedtls_ssl_config *conf = ssl->conf;
1222
Ronald Cron6f135e12021-12-08 16:57:54 +01001223#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001224 if (mbedtls_ssl_conf_is_tls13_only(conf)) {
1225 if (conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
1226 MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS 1.3 is not yet supported."));
1227 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQianed582dd2022-04-13 08:21:05 +00001228 }
1229
Gilles Peskine449bd832023-01-11 14:50:10 +01001230 MBEDTLS_SSL_DEBUG_MSG(4, ("The SSL configuration is tls13 only."));
1231 return 0;
Jerry Yu60835a82021-08-04 10:13:52 +08001232 }
1233#endif
1234
1235#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01001236 if (mbedtls_ssl_conf_is_tls12_only(conf)) {
1237 MBEDTLS_SSL_DEBUG_MSG(4, ("The SSL configuration is tls12 only."));
1238 return 0;
Jerry Yu60835a82021-08-04 10:13:52 +08001239 }
1240#endif
1241
Ronald Cron6f135e12021-12-08 16:57:54 +01001242#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001243 if (mbedtls_ssl_conf_is_hybrid_tls12_tls13(conf)) {
1244 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
1245 MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS not yet supported in Hybrid TLS 1.3 + TLS 1.2"));
1246 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQianed582dd2022-04-13 08:21:05 +00001247 }
1248
Gilles Peskine449bd832023-01-11 14:50:10 +01001249 if (conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
1250 MBEDTLS_SSL_DEBUG_MSG(1, ("TLS 1.3 server is not supported yet."));
1251 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQian8f9dfe42022-04-15 02:52:39 +00001252 }
1253
1254
Gilles Peskine449bd832023-01-11 14:50:10 +01001255 MBEDTLS_SSL_DEBUG_MSG(4, ("The SSL configuration is TLS 1.3 or TLS 1.2."));
1256 return 0;
Jerry Yu60835a82021-08-04 10:13:52 +08001257 }
1258#endif
1259
Gilles Peskine449bd832023-01-11 14:50:10 +01001260 MBEDTLS_SSL_DEBUG_MSG(1, ("The SSL configuration is invalid."));
1261 return MBEDTLS_ERR_SSL_BAD_CONFIG;
Jerry Yu60835a82021-08-04 10:13:52 +08001262}
1263
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001264MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu60835a82021-08-04 10:13:52 +08001265static int ssl_conf_check(const mbedtls_ssl_context *ssl)
1266{
1267 int ret;
Gilles Peskine449bd832023-01-11 14:50:10 +01001268 ret = ssl_conf_version_check(ssl);
1269 if (ret != 0) {
1270 return ret;
1271 }
Jerry Yu60835a82021-08-04 10:13:52 +08001272
Jerry Yudef7ae42022-10-30 14:13:19 +08001273#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1274 /* RFC 8446 section 4.4.3
1275 *
1276 * If the verification fails, the receiver MUST terminate the handshake with
1277 * a "decrypt_error" alert.
1278 *
1279 * If the client is configured as TLS 1.3 only with optional verify, return
1280 * bad config.
1281 *
1282 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001283 if (mbedtls_ssl_conf_tls13_ephemeral_enabled(
1284 (mbedtls_ssl_context *) ssl) &&
Jerry Yudef7ae42022-10-30 14:13:19 +08001285 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
1286 ssl->conf->max_tls_version == MBEDTLS_SSL_VERSION_TLS1_3 &&
1287 ssl->conf->min_tls_version == MBEDTLS_SSL_VERSION_TLS1_3 &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001288 ssl->conf->authmode == MBEDTLS_SSL_VERIFY_OPTIONAL) {
Jerry Yudef7ae42022-10-30 14:13:19 +08001289 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +01001290 1, ("Optional verify auth mode "
1291 "is not available for TLS 1.3 client"));
1292 return MBEDTLS_ERR_SSL_BAD_CONFIG;
Jerry Yudef7ae42022-10-30 14:13:19 +08001293 }
1294#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
1295
Jerry Yu60835a82021-08-04 10:13:52 +08001296 /* Space for further checks */
1297
Gilles Peskine449bd832023-01-11 14:50:10 +01001298 return 0;
Jerry Yu60835a82021-08-04 10:13:52 +08001299}
1300
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001301/*
1302 * Setup an SSL context
1303 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01001304
Gilles Peskine449bd832023-01-11 14:50:10 +01001305int mbedtls_ssl_setup(mbedtls_ssl_context *ssl,
1306 const mbedtls_ssl_config *conf)
Paul Bakker5121ce52009-01-03 21:22:43 +00001307{
Janos Follath865b3eb2019-12-16 11:46:15 +00001308 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Darryl Greenb33cc762019-11-28 14:29:44 +00001309 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1310 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00001311
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001312 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00001313
Gilles Peskine449bd832023-01-11 14:50:10 +01001314 if ((ret = ssl_conf_check(ssl)) != 0) {
1315 return ret;
1316 }
Jerry Yu60835a82021-08-04 10:13:52 +08001317
Paul Bakker62f2dee2012-09-28 07:31:51 +00001318 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001319 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00001320 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02001321
1322 /* Set to NULL in case of an error condition */
1323 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02001324
Darryl Greenb33cc762019-11-28 14:29:44 +00001325#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1326 ssl->in_buf_len = in_buf_len;
1327#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001328 ssl->in_buf = mbedtls_calloc(1, in_buf_len);
1329 if (ssl->in_buf == NULL) {
1330 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", in_buf_len));
k-stachowiak9f7798e2018-07-31 16:52:32 +02001331 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02001332 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10001333 }
1334
Darryl Greenb33cc762019-11-28 14:29:44 +00001335#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1336 ssl->out_buf_len = out_buf_len;
1337#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001338 ssl->out_buf = mbedtls_calloc(1, out_buf_len);
1339 if (ssl->out_buf == NULL) {
1340 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", out_buf_len));
k-stachowiak9f7798e2018-07-31 16:52:32 +02001341 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02001342 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00001343 }
1344
Gilles Peskine449bd832023-01-11 14:50:10 +01001345 mbedtls_ssl_reset_in_out_pointers(ssl);
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02001346
Johan Pascalb62bb512015-12-03 21:56:45 +01001347#if defined(MBEDTLS_SSL_DTLS_SRTP)
Gilles Peskine449bd832023-01-11 14:50:10 +01001348 memset(&ssl->dtls_srtp_info, 0, sizeof(ssl->dtls_srtp_info));
Johan Pascalb62bb512015-12-03 21:56:45 +01001349#endif
1350
Gilles Peskine449bd832023-01-11 14:50:10 +01001351 if ((ret = ssl_handshake_init(ssl)) != 0) {
k-stachowiaka47911c2018-07-04 17:41:58 +02001352 goto error;
Gilles Peskine449bd832023-01-11 14:50:10 +01001353 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001354
Gilles Peskine449bd832023-01-11 14:50:10 +01001355 return 0;
k-stachowiaka47911c2018-07-04 17:41:58 +02001356
1357error:
Gilles Peskine449bd832023-01-11 14:50:10 +01001358 mbedtls_free(ssl->in_buf);
1359 mbedtls_free(ssl->out_buf);
k-stachowiaka47911c2018-07-04 17:41:58 +02001360
1361 ssl->conf = NULL;
1362
Darryl Greenb33cc762019-11-28 14:29:44 +00001363#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1364 ssl->in_buf_len = 0;
1365 ssl->out_buf_len = 0;
1366#endif
k-stachowiaka47911c2018-07-04 17:41:58 +02001367 ssl->in_buf = NULL;
1368 ssl->out_buf = NULL;
1369
1370 ssl->in_hdr = NULL;
1371 ssl->in_ctr = NULL;
1372 ssl->in_len = NULL;
1373 ssl->in_iv = NULL;
1374 ssl->in_msg = NULL;
1375
1376 ssl->out_hdr = NULL;
1377 ssl->out_ctr = NULL;
1378 ssl->out_len = NULL;
1379 ssl->out_iv = NULL;
1380 ssl->out_msg = NULL;
1381
Gilles Peskine449bd832023-01-11 14:50:10 +01001382 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001383}
1384
1385/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00001386 * Reset an initialized and used SSL context for re-use while retaining
1387 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001388 *
1389 * If partial is non-zero, keep data in the input buffer and client ID.
1390 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00001391 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001392void mbedtls_ssl_session_reset_msg_layer(mbedtls_ssl_context *ssl,
1393 int partial)
Paul Bakker7eb013f2011-10-06 12:37:39 +00001394{
Darryl Greenb33cc762019-11-28 14:29:44 +00001395#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1396 size_t in_buf_len = ssl->in_buf_len;
1397 size_t out_buf_len = ssl->out_buf_len;
1398#else
1399 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1400 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
1401#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001402
Hanno Beckerb0302c42021-08-03 09:39:42 +01001403#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || !defined(MBEDTLS_SSL_SRV_C)
1404 partial = 0;
Hanno Becker7e772132018-08-10 12:38:21 +01001405#endif
1406
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001407 /* Cancel any possibly running timer */
Gilles Peskine449bd832023-01-11 14:50:10 +01001408 mbedtls_ssl_set_timer(ssl, 0);
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001409
Gilles Peskine449bd832023-01-11 14:50:10 +01001410 mbedtls_ssl_reset_in_out_pointers(ssl);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001411
1412 /* Reset incoming message parsing */
1413 ssl->in_offt = NULL;
1414 ssl->nb_zero = 0;
1415 ssl->in_msgtype = 0;
1416 ssl->in_msglen = 0;
1417 ssl->in_hslen = 0;
1418 ssl->keep_current_message = 0;
1419 ssl->transform_in = NULL;
1420
1421#if defined(MBEDTLS_SSL_PROTO_DTLS)
1422 ssl->next_record_offset = 0;
1423 ssl->in_epoch = 0;
1424#endif
1425
1426 /* Keep current datagram if partial == 1 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001427 if (partial == 0) {
Hanno Beckerb0302c42021-08-03 09:39:42 +01001428 ssl->in_left = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001429 memset(ssl->in_buf, 0, in_buf_len);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001430 }
1431
Ronald Cronad8c17b2022-06-10 17:18:09 +02001432 ssl->send_alert = 0;
1433
Hanno Beckerb0302c42021-08-03 09:39:42 +01001434 /* Reset outgoing message writing */
1435 ssl->out_msgtype = 0;
1436 ssl->out_msglen = 0;
1437 ssl->out_left = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001438 memset(ssl->out_buf, 0, out_buf_len);
1439 memset(ssl->cur_out_ctr, 0, sizeof(ssl->cur_out_ctr));
Hanno Beckerb0302c42021-08-03 09:39:42 +01001440 ssl->transform_out = NULL;
1441
1442#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine449bd832023-01-11 14:50:10 +01001443 mbedtls_ssl_dtls_replay_reset(ssl);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001444#endif
1445
XiaokangQian2b01dc32022-01-21 02:53:13 +00001446#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01001447 if (ssl->transform) {
1448 mbedtls_ssl_transform_free(ssl->transform);
1449 mbedtls_free(ssl->transform);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001450 ssl->transform = NULL;
1451 }
XiaokangQian2b01dc32022-01-21 02:53:13 +00001452#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1453
XiaokangQian2b01dc32022-01-21 02:53:13 +00001454#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001455 mbedtls_ssl_transform_free(ssl->transform_application);
1456 mbedtls_free(ssl->transform_application);
XiaokangQian2b01dc32022-01-21 02:53:13 +00001457 ssl->transform_application = NULL;
1458
Gilles Peskine449bd832023-01-11 14:50:10 +01001459 if (ssl->handshake != NULL) {
Jerry Yu3d9b5902022-11-04 14:07:25 +08001460#if defined(MBEDTLS_SSL_EARLY_DATA)
Gilles Peskine449bd832023-01-11 14:50:10 +01001461 mbedtls_ssl_transform_free(ssl->handshake->transform_earlydata);
1462 mbedtls_free(ssl->handshake->transform_earlydata);
XiaokangQian2b01dc32022-01-21 02:53:13 +00001463 ssl->handshake->transform_earlydata = NULL;
Jerry Yu3d9b5902022-11-04 14:07:25 +08001464#endif
XiaokangQian2b01dc32022-01-21 02:53:13 +00001465
Gilles Peskine449bd832023-01-11 14:50:10 +01001466 mbedtls_ssl_transform_free(ssl->handshake->transform_handshake);
1467 mbedtls_free(ssl->handshake->transform_handshake);
XiaokangQian2b01dc32022-01-21 02:53:13 +00001468 ssl->handshake->transform_handshake = NULL;
1469 }
1470
XiaokangQian2b01dc32022-01-21 02:53:13 +00001471#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerb0302c42021-08-03 09:39:42 +01001472}
1473
Gilles Peskine449bd832023-01-11 14:50:10 +01001474int mbedtls_ssl_session_reset_int(mbedtls_ssl_context *ssl, int partial)
Hanno Beckerb0302c42021-08-03 09:39:42 +01001475{
1476 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1477
1478 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
1479
Gilles Peskine449bd832023-01-11 14:50:10 +01001480 mbedtls_ssl_session_reset_msg_layer(ssl, partial);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001481
1482 /* Reset renegotiation state */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001483#if defined(MBEDTLS_SSL_RENEGOTIATION)
1484 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001485 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00001486
1487 ssl->verify_data_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001488 memset(ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN);
1489 memset(ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN);
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001490#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001491 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00001492
Hanno Beckerb0302c42021-08-03 09:39:42 +01001493 ssl->session_in = NULL;
Hanno Becker78640902018-08-13 16:35:15 +01001494 ssl->session_out = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001495 if (ssl->session) {
1496 mbedtls_ssl_session_free(ssl->session);
1497 mbedtls_free(ssl->session);
Paul Bakkerc0463502013-02-14 11:19:38 +01001498 ssl->session = NULL;
1499 }
1500
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001501#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02001502 ssl->alpn_chosen = NULL;
1503#endif
1504
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001505#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
David Horstmann3b2276a2022-10-06 14:49:08 +01001506 int free_cli_id = 1;
Hanno Becker4ccbf062018-08-10 11:20:38 +01001507#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Gilles Peskine449bd832023-01-11 14:50:10 +01001508 free_cli_id = (partial == 0);
Hanno Becker4ccbf062018-08-10 11:20:38 +01001509#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001510 if (free_cli_id) {
1511 mbedtls_free(ssl->cli_id);
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001512 ssl->cli_id = NULL;
1513 ssl->cli_id_len = 0;
1514 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02001515#endif
1516
Gilles Peskine449bd832023-01-11 14:50:10 +01001517 if ((ret = ssl_handshake_init(ssl)) != 0) {
1518 return ret;
1519 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00001520
Gilles Peskine449bd832023-01-11 14:50:10 +01001521 return 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00001522}
1523
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02001524/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001525 * Reset an initialized and used SSL context for re-use while retaining
1526 * all application-set variables, function pointers and data.
1527 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001528int mbedtls_ssl_session_reset(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001529{
Gilles Peskine449bd832023-01-11 14:50:10 +01001530 return mbedtls_ssl_session_reset_int(ssl, 0);
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001531}
1532
1533/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001534 * SSL set accessors
1535 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001536void mbedtls_ssl_conf_endpoint(mbedtls_ssl_config *conf, int endpoint)
Paul Bakker5121ce52009-01-03 21:22:43 +00001537{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001538 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00001539}
1540
Gilles Peskine449bd832023-01-11 14:50:10 +01001541void mbedtls_ssl_conf_transport(mbedtls_ssl_config *conf, int transport)
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001542{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001543 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001544}
1545
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001546#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine449bd832023-01-11 14:50:10 +01001547void mbedtls_ssl_conf_dtls_anti_replay(mbedtls_ssl_config *conf, char mode)
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001548{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001549 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001550}
1551#endif
1552
Gilles Peskine449bd832023-01-11 14:50:10 +01001553void mbedtls_ssl_conf_dtls_badmac_limit(mbedtls_ssl_config *conf, unsigned limit)
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001554{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001555 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001556}
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001557
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001558#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01001559
Gilles Peskine449bd832023-01-11 14:50:10 +01001560void mbedtls_ssl_set_datagram_packing(mbedtls_ssl_context *ssl,
1561 unsigned allow_packing)
Hanno Becker04da1892018-08-14 13:22:10 +01001562{
1563 ssl->disable_datagram_packing = !allow_packing;
1564}
1565
Gilles Peskine449bd832023-01-11 14:50:10 +01001566void mbedtls_ssl_conf_handshake_timeout(mbedtls_ssl_config *conf,
1567 uint32_t min, uint32_t max)
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001568{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001569 conf->hs_timeout_min = min;
1570 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001571}
1572#endif
1573
Gilles Peskine449bd832023-01-11 14:50:10 +01001574void mbedtls_ssl_conf_authmode(mbedtls_ssl_config *conf, int authmode)
Paul Bakker5121ce52009-01-03 21:22:43 +00001575{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001576 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00001577}
1578
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001579#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001580void mbedtls_ssl_conf_verify(mbedtls_ssl_config *conf,
1581 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
1582 void *p_vrfy)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001583{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001584 conf->f_vrfy = f_vrfy;
1585 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001586}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001587#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001588
Gilles Peskine449bd832023-01-11 14:50:10 +01001589void mbedtls_ssl_conf_rng(mbedtls_ssl_config *conf,
1590 int (*f_rng)(void *, unsigned char *, size_t),
1591 void *p_rng)
Paul Bakker5121ce52009-01-03 21:22:43 +00001592{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001593 conf->f_rng = f_rng;
1594 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00001595}
1596
Gilles Peskine449bd832023-01-11 14:50:10 +01001597void mbedtls_ssl_conf_dbg(mbedtls_ssl_config *conf,
1598 void (*f_dbg)(void *, int, const char *, int, const char *),
1599 void *p_dbg)
Paul Bakker5121ce52009-01-03 21:22:43 +00001600{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001601 conf->f_dbg = f_dbg;
1602 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00001603}
1604
Gilles Peskine449bd832023-01-11 14:50:10 +01001605void mbedtls_ssl_set_bio(mbedtls_ssl_context *ssl,
1606 void *p_bio,
1607 mbedtls_ssl_send_t *f_send,
1608 mbedtls_ssl_recv_t *f_recv,
1609 mbedtls_ssl_recv_timeout_t *f_recv_timeout)
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001610{
1611 ssl->p_bio = p_bio;
1612 ssl->f_send = f_send;
1613 ssl->f_recv = f_recv;
1614 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001615}
1616
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02001617#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001618void mbedtls_ssl_set_mtu(mbedtls_ssl_context *ssl, uint16_t mtu)
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02001619{
1620 ssl->mtu = mtu;
1621}
1622#endif
1623
Gilles Peskine449bd832023-01-11 14:50:10 +01001624void mbedtls_ssl_conf_read_timeout(mbedtls_ssl_config *conf, uint32_t timeout)
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001625{
1626 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001627}
1628
Gilles Peskine449bd832023-01-11 14:50:10 +01001629void mbedtls_ssl_set_timer_cb(mbedtls_ssl_context *ssl,
1630 void *p_timer,
1631 mbedtls_ssl_set_timer_t *f_set_timer,
1632 mbedtls_ssl_get_timer_t *f_get_timer)
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001633{
1634 ssl->p_timer = p_timer;
1635 ssl->f_set_timer = f_set_timer;
1636 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001637
1638 /* Make sure we start with no timer running */
Gilles Peskine449bd832023-01-11 14:50:10 +01001639 mbedtls_ssl_set_timer(ssl, 0);
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001640}
1641
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001642#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001643void mbedtls_ssl_conf_session_cache(mbedtls_ssl_config *conf,
1644 void *p_cache,
1645 mbedtls_ssl_cache_get_t *f_get_cache,
1646 mbedtls_ssl_cache_set_t *f_set_cache)
Paul Bakker5121ce52009-01-03 21:22:43 +00001647{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01001648 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001649 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001650 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00001651}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001652#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001653
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001654#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001655int mbedtls_ssl_set_session(mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session)
Paul Bakker5121ce52009-01-03 21:22:43 +00001656{
Janos Follath865b3eb2019-12-16 11:46:15 +00001657 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001658
Gilles Peskine449bd832023-01-11 14:50:10 +01001659 if (ssl == NULL ||
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001660 session == NULL ||
1661 ssl->session_negotiate == NULL ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001662 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT) {
1663 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001664 }
1665
Gilles Peskine449bd832023-01-11 14:50:10 +01001666 if (ssl->handshake->resume == 1) {
1667 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
1668 }
Hanno Beckere810bbc2021-05-14 16:01:05 +01001669
Jerry Yu21092062022-10-10 21:21:31 +08001670#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001671 if (session->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
Jerry Yu21092062022-10-10 21:21:31 +08001672 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01001673 mbedtls_ssl_ciphersuite_from_id(session->ciphersuite);
Jerry Yu21092062022-10-10 21:21:31 +08001674
Gilles Peskine449bd832023-01-11 14:50:10 +01001675 if (mbedtls_ssl_validate_ciphersuite(
Jerry Yu21092062022-10-10 21:21:31 +08001676 ssl, ciphersuite_info, MBEDTLS_SSL_VERSION_TLS1_3,
Gilles Peskine449bd832023-01-11 14:50:10 +01001677 MBEDTLS_SSL_VERSION_TLS1_3) != 0) {
1678 MBEDTLS_SSL_DEBUG_MSG(4, ("%d is not a valid TLS 1.3 ciphersuite.",
1679 session->ciphersuite));
1680 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jerry Yu21092062022-10-10 21:21:31 +08001681 }
Jerry Yu40afab62022-10-08 10:42:13 +08001682 }
Jerry Yu21092062022-10-10 21:21:31 +08001683#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu40afab62022-10-08 10:42:13 +08001684
Gilles Peskine449bd832023-01-11 14:50:10 +01001685 if ((ret = mbedtls_ssl_session_copy(ssl->session_negotiate,
1686 session)) != 0) {
1687 return ret;
1688 }
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001689
Paul Bakker0a597072012-09-25 21:55:46 +00001690 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001691
Gilles Peskine449bd832023-01-11 14:50:10 +01001692 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001693}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001694#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001695
Gilles Peskine449bd832023-01-11 14:50:10 +01001696void mbedtls_ssl_conf_ciphersuites(mbedtls_ssl_config *conf,
1697 const int *ciphersuites)
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001698{
Hanno Beckerd60b6c62021-04-29 12:04:11 +01001699 conf->ciphersuite_list = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00001700}
1701
Ronald Cron6f135e12021-12-08 16:57:54 +01001702#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001703void mbedtls_ssl_conf_tls13_key_exchange_modes(mbedtls_ssl_config *conf,
1704 const int kex_modes)
Hanno Becker71f1ed62021-07-24 06:01:47 +01001705{
Xiaofei Bai746f9482021-11-12 08:53:56 +00001706 conf->tls13_kex_modes = kex_modes & MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Hanno Becker71f1ed62021-07-24 06:01:47 +01001707}
Xiaokang Qian72de95d2022-10-25 02:54:33 +00001708
1709#if defined(MBEDTLS_SSL_EARLY_DATA)
Gilles Peskine449bd832023-01-11 14:50:10 +01001710void mbedtls_ssl_tls13_conf_early_data(mbedtls_ssl_config *conf,
1711 int early_data_enabled)
Xiaokang Qian72de95d2022-10-25 02:54:33 +00001712{
1713 conf->early_data_enabled = early_data_enabled;
1714}
Jerry Yucc4e0072022-11-22 17:22:22 +08001715
1716#if defined(MBEDTLS_SSL_SRV_C)
1717void mbedtls_ssl_tls13_conf_max_early_data_size(
Gilles Peskine449bd832023-01-11 14:50:10 +01001718 mbedtls_ssl_config *conf, uint32_t max_early_data_size)
Jerry Yucc4e0072022-11-22 17:22:22 +08001719{
Jerry Yu39da9852022-12-06 16:58:36 +08001720 conf->max_early_data_size = max_early_data_size;
Jerry Yucc4e0072022-11-22 17:22:22 +08001721}
1722#endif /* MBEDTLS_SSL_SRV_C */
1723
Xiaokang Qian72de95d2022-10-25 02:54:33 +00001724#endif /* MBEDTLS_SSL_EARLY_DATA */
Ronald Cron6f135e12021-12-08 16:57:54 +01001725#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01001726
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001727#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001728void mbedtls_ssl_conf_cert_profile(mbedtls_ssl_config *conf,
1729 const mbedtls_x509_crt_profile *profile)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02001730{
1731 conf->cert_profile = profile;
1732}
1733
Gilles Peskine449bd832023-01-11 14:50:10 +01001734static void ssl_key_cert_free(mbedtls_ssl_key_cert *key_cert)
Glenn Strauss36872db2022-01-22 05:06:31 -05001735{
1736 mbedtls_ssl_key_cert *cur = key_cert, *next;
1737
Gilles Peskine449bd832023-01-11 14:50:10 +01001738 while (cur != NULL) {
Glenn Strauss36872db2022-01-22 05:06:31 -05001739 next = cur->next;
Gilles Peskine449bd832023-01-11 14:50:10 +01001740 mbedtls_free(cur);
Glenn Strauss36872db2022-01-22 05:06:31 -05001741 cur = next;
1742 }
1743}
1744
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001745/* Append a new keycert entry to a (possibly empty) list */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001746MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001747static int ssl_append_key_cert(mbedtls_ssl_key_cert **head,
1748 mbedtls_x509_crt *cert,
1749 mbedtls_pk_context *key)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001750{
niisato8ee24222018-06-25 19:05:48 +09001751 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001752
Gilles Peskine449bd832023-01-11 14:50:10 +01001753 if (cert == NULL) {
Glenn Strauss36872db2022-01-22 05:06:31 -05001754 /* Free list if cert is null */
Gilles Peskine449bd832023-01-11 14:50:10 +01001755 ssl_key_cert_free(*head);
Glenn Strauss36872db2022-01-22 05:06:31 -05001756 *head = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001757 return 0;
Glenn Strauss36872db2022-01-22 05:06:31 -05001758 }
1759
Gilles Peskine449bd832023-01-11 14:50:10 +01001760 new_cert = mbedtls_calloc(1, sizeof(mbedtls_ssl_key_cert));
1761 if (new_cert == NULL) {
1762 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
1763 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001764
niisato8ee24222018-06-25 19:05:48 +09001765 new_cert->cert = cert;
1766 new_cert->key = key;
1767 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001768
Glenn Strauss36872db2022-01-22 05:06:31 -05001769 /* Update head if the list was null, else add to the end */
Gilles Peskine449bd832023-01-11 14:50:10 +01001770 if (*head == NULL) {
niisato8ee24222018-06-25 19:05:48 +09001771 *head = new_cert;
Gilles Peskine449bd832023-01-11 14:50:10 +01001772 } else {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001773 mbedtls_ssl_key_cert *cur = *head;
Gilles Peskine449bd832023-01-11 14:50:10 +01001774 while (cur->next != NULL) {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001775 cur = cur->next;
Gilles Peskine449bd832023-01-11 14:50:10 +01001776 }
niisato8ee24222018-06-25 19:05:48 +09001777 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001778 }
1779
Gilles Peskine449bd832023-01-11 14:50:10 +01001780 return 0;
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001781}
1782
Gilles Peskine449bd832023-01-11 14:50:10 +01001783int mbedtls_ssl_conf_own_cert(mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001784 mbedtls_x509_crt *own_cert,
Gilles Peskine449bd832023-01-11 14:50:10 +01001785 mbedtls_pk_context *pk_key)
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001786{
Gilles Peskine449bd832023-01-11 14:50:10 +01001787 return ssl_append_key_cert(&conf->key_cert, own_cert, pk_key);
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001788}
1789
Gilles Peskine449bd832023-01-11 14:50:10 +01001790void mbedtls_ssl_conf_ca_chain(mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001791 mbedtls_x509_crt *ca_chain,
Gilles Peskine449bd832023-01-11 14:50:10 +01001792 mbedtls_x509_crl *ca_crl)
Paul Bakker5121ce52009-01-03 21:22:43 +00001793{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001794 conf->ca_chain = ca_chain;
1795 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00001796
1797#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1798 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1799 * cannot be used together. */
1800 conf->f_ca_cb = NULL;
1801 conf->p_ca_cb = NULL;
1802#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00001803}
Hanno Becker5adaad92019-03-27 16:54:37 +00001804
1805#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Gilles Peskine449bd832023-01-11 14:50:10 +01001806void mbedtls_ssl_conf_ca_cb(mbedtls_ssl_config *conf,
1807 mbedtls_x509_crt_ca_cb_t f_ca_cb,
1808 void *p_ca_cb)
Hanno Becker5adaad92019-03-27 16:54:37 +00001809{
1810 conf->f_ca_cb = f_ca_cb;
1811 conf->p_ca_cb = p_ca_cb;
1812
1813 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1814 * cannot be used together. */
1815 conf->ca_chain = NULL;
1816 conf->ca_crl = NULL;
1817}
1818#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001819#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00001820
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001821#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01001822const unsigned char *mbedtls_ssl_get_hs_sni(mbedtls_ssl_context *ssl,
1823 size_t *name_len)
Glenn Strauss69894072022-01-24 12:58:00 -05001824{
1825 *name_len = ssl->handshake->sni_name_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001826 return ssl->handshake->sni_name;
Glenn Strauss69894072022-01-24 12:58:00 -05001827}
1828
Gilles Peskine449bd832023-01-11 14:50:10 +01001829int mbedtls_ssl_set_hs_own_cert(mbedtls_ssl_context *ssl,
1830 mbedtls_x509_crt *own_cert,
1831 mbedtls_pk_context *pk_key)
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001832{
Gilles Peskine449bd832023-01-11 14:50:10 +01001833 return ssl_append_key_cert(&ssl->handshake->sni_key_cert,
1834 own_cert, pk_key);
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001835}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02001836
Gilles Peskine449bd832023-01-11 14:50:10 +01001837void mbedtls_ssl_set_hs_ca_chain(mbedtls_ssl_context *ssl,
1838 mbedtls_x509_crt *ca_chain,
1839 mbedtls_x509_crl *ca_crl)
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02001840{
1841 ssl->handshake->sni_ca_chain = ca_chain;
1842 ssl->handshake->sni_ca_crl = ca_crl;
1843}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001844
Glenn Strauss999ef702022-03-11 01:37:23 -05001845#if defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001846void mbedtls_ssl_set_hs_dn_hints(mbedtls_ssl_context *ssl,
1847 const mbedtls_x509_crt *crt)
Glenn Strauss999ef702022-03-11 01:37:23 -05001848{
1849 ssl->handshake->dn_hints = crt;
1850}
1851#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
1852
Gilles Peskine449bd832023-01-11 14:50:10 +01001853void mbedtls_ssl_set_hs_authmode(mbedtls_ssl_context *ssl,
1854 int authmode)
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001855{
1856 ssl->handshake->sni_authmode = authmode;
1857}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001858#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
1859
Hanno Becker8927c832019-04-03 12:52:50 +01001860#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001861void mbedtls_ssl_set_verify(mbedtls_ssl_context *ssl,
1862 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
1863 void *p_vrfy)
Hanno Becker8927c832019-04-03 12:52:50 +01001864{
1865 ssl->f_vrfy = f_vrfy;
1866 ssl->p_vrfy = p_vrfy;
1867}
1868#endif
1869
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02001870#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001871
Valerio Setti016f6822022-12-09 14:17:50 +01001872#if defined(MBEDTLS_USE_PSA_CRYPTO)
1873static psa_status_t mbedtls_ssl_set_hs_ecjpake_password_common(
Gilles Peskine449bd832023-01-11 14:50:10 +01001874 mbedtls_ssl_context *ssl,
1875 mbedtls_svc_key_id_t pwd)
Valerio Setti016f6822022-12-09 14:17:50 +01001876{
1877 psa_status_t status;
1878 psa_pake_role_t psa_role;
1879 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
1880
Gilles Peskine449bd832023-01-11 14:50:10 +01001881 psa_pake_cs_set_algorithm(&cipher_suite, PSA_ALG_JPAKE);
1882 psa_pake_cs_set_primitive(&cipher_suite,
1883 PSA_PAKE_PRIMITIVE(PSA_PAKE_PRIMITIVE_TYPE_ECC,
1884 PSA_ECC_FAMILY_SECP_R1,
1885 256));
1886 psa_pake_cs_set_hash(&cipher_suite, PSA_ALG_SHA_256);
Valerio Setti016f6822022-12-09 14:17:50 +01001887
Gilles Peskine449bd832023-01-11 14:50:10 +01001888 status = psa_pake_setup(&ssl->handshake->psa_pake_ctx, &cipher_suite);
1889 if (status != PSA_SUCCESS) {
Valerio Setti016f6822022-12-09 14:17:50 +01001890 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001891 }
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001892
Gilles Peskine449bd832023-01-11 14:50:10 +01001893 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Neil Armstrongca7d5062022-05-31 14:43:23 +02001894 psa_role = PSA_PAKE_ROLE_SERVER;
Gilles Peskine449bd832023-01-11 14:50:10 +01001895 } else {
Neil Armstrongca7d5062022-05-31 14:43:23 +02001896 psa_role = PSA_PAKE_ROLE_CLIENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001897 }
Neil Armstrongca7d5062022-05-31 14:43:23 +02001898
Gilles Peskine449bd832023-01-11 14:50:10 +01001899 status = psa_pake_set_role(&ssl->handshake->psa_pake_ctx, psa_role);
1900 if (status != PSA_SUCCESS) {
Valerio Setti016f6822022-12-09 14:17:50 +01001901 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001902 }
Valerio Setti016f6822022-12-09 14:17:50 +01001903
Gilles Peskine449bd832023-01-11 14:50:10 +01001904 status = psa_pake_set_password_key(&ssl->handshake->psa_pake_ctx, pwd);
1905 if (status != PSA_SUCCESS) {
Valerio Setti016f6822022-12-09 14:17:50 +01001906 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001907 }
Valerio Setti016f6822022-12-09 14:17:50 +01001908
1909 ssl->handshake->psa_pake_ctx_is_ok = 1;
1910
Gilles Peskine449bd832023-01-11 14:50:10 +01001911 return PSA_SUCCESS;
Valerio Setti016f6822022-12-09 14:17:50 +01001912}
1913
Gilles Peskine449bd832023-01-11 14:50:10 +01001914int mbedtls_ssl_set_hs_ecjpake_password(mbedtls_ssl_context *ssl,
1915 const unsigned char *pw,
1916 size_t pw_len)
Valerio Setti016f6822022-12-09 14:17:50 +01001917{
1918 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1919 psa_status_t status;
1920
Gilles Peskine449bd832023-01-11 14:50:10 +01001921 if (ssl->handshake == NULL || ssl->conf == NULL) {
1922 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Neil Armstrongca7d5062022-05-31 14:43:23 +02001923 }
1924
Gilles Peskine449bd832023-01-11 14:50:10 +01001925 /* Empty password is not valid */
1926 if ((pw == NULL) || (pw_len == 0)) {
1927 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
1928 }
1929
1930 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
1931 psa_set_key_algorithm(&attributes, PSA_ALG_JPAKE);
1932 psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD);
1933
1934 status = psa_import_key(&attributes, pw, pw_len,
1935 &ssl->handshake->psa_pake_password);
1936 if (status != PSA_SUCCESS) {
1937 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
1938 }
1939
1940 status = mbedtls_ssl_set_hs_ecjpake_password_common(ssl,
1941 ssl->handshake->psa_pake_password);
1942 if (status != PSA_SUCCESS) {
1943 psa_destroy_key(ssl->handshake->psa_pake_password);
1944 psa_pake_abort(&ssl->handshake->psa_pake_ctx);
1945 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
1946 }
1947
1948 return 0;
Valerio Setti02c25b52022-11-15 14:08:42 +01001949}
Valerio Settia9a97dc2022-11-28 18:26:16 +01001950
Gilles Peskine449bd832023-01-11 14:50:10 +01001951int mbedtls_ssl_set_hs_ecjpake_password_opaque(mbedtls_ssl_context *ssl,
1952 mbedtls_svc_key_id_t pwd)
Valerio Settia9a97dc2022-11-28 18:26:16 +01001953{
Valerio Settia9a97dc2022-11-28 18:26:16 +01001954 psa_status_t status;
1955
Gilles Peskine449bd832023-01-11 14:50:10 +01001956 if (ssl->handshake == NULL || ssl->conf == NULL) {
1957 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Neil Armstrongca7d5062022-05-31 14:43:23 +02001958 }
1959
Gilles Peskine449bd832023-01-11 14:50:10 +01001960 if (mbedtls_svc_key_id_is_null(pwd)) {
1961 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
1962 }
1963
1964 status = mbedtls_ssl_set_hs_ecjpake_password_common(ssl, pwd);
1965 if (status != PSA_SUCCESS) {
1966 psa_pake_abort(&ssl->handshake->psa_pake_ctx);
1967 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
1968 }
1969
1970 return 0;
Valerio Setti02c25b52022-11-15 14:08:42 +01001971}
1972#else /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01001973int mbedtls_ssl_set_hs_ecjpake_password(mbedtls_ssl_context *ssl,
1974 const unsigned char *pw,
1975 size_t pw_len)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001976{
1977 mbedtls_ecjpake_role role;
1978
Gilles Peskine449bd832023-01-11 14:50:10 +01001979 if (ssl->handshake == NULL || ssl->conf == NULL) {
1980 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
1981 }
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001982
Valerio Settic689ed82022-12-07 14:40:38 +01001983 /* Empty password is not valid */
Gilles Peskine449bd832023-01-11 14:50:10 +01001984 if ((pw == NULL) || (pw_len == 0)) {
1985 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
1986 }
Valerio Settic689ed82022-12-07 14:40:38 +01001987
Gilles Peskine449bd832023-01-11 14:50:10 +01001988 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001989 role = MBEDTLS_ECJPAKE_SERVER;
Gilles Peskine449bd832023-01-11 14:50:10 +01001990 } else {
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001991 role = MBEDTLS_ECJPAKE_CLIENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001992 }
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001993
Gilles Peskine449bd832023-01-11 14:50:10 +01001994 return mbedtls_ecjpake_setup(&ssl->handshake->ecjpake_ctx,
1995 role,
1996 MBEDTLS_MD_SHA256,
1997 MBEDTLS_ECP_DP_SECP256R1,
1998 pw, pw_len);
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001999}
Valerio Setti02c25b52022-11-15 14:08:42 +01002000#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02002001#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002002
Ronald Cron73fe8df2022-10-05 14:31:43 +02002003#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002004int mbedtls_ssl_conf_has_static_psk(mbedtls_ssl_config const *conf)
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002005{
Gilles Peskine449bd832023-01-11 14:50:10 +01002006 if (conf->psk_identity == NULL ||
2007 conf->psk_identity_len == 0) {
2008 return 0;
Ronald Crond29e13e2022-10-19 10:33:48 +02002009 }
2010
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002011#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002012 if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) {
2013 return 1;
2014 }
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002015#endif /* MBEDTLS_USE_PSA_CRYPTO */
Ronald Crond29e13e2022-10-19 10:33:48 +02002016
Gilles Peskine449bd832023-01-11 14:50:10 +01002017 if (conf->psk != NULL && conf->psk_len != 0) {
2018 return 1;
2019 }
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002020
Gilles Peskine449bd832023-01-11 14:50:10 +01002021 return 0;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002022}
2023
Gilles Peskine449bd832023-01-11 14:50:10 +01002024static void ssl_conf_remove_psk(mbedtls_ssl_config *conf)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002025{
2026 /* Remove reference to existing PSK, if any. */
2027#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002028 if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) {
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002029 /* The maintenance of the PSK key slot is the
2030 * user's responsibility. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002031 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002032 }
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002033#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01002034 if (conf->psk != NULL) {
2035 mbedtls_platform_zeroize(conf->psk, conf->psk_len);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002036
Gilles Peskine449bd832023-01-11 14:50:10 +01002037 mbedtls_free(conf->psk);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002038 conf->psk = NULL;
2039 conf->psk_len = 0;
2040 }
2041
2042 /* Remove reference to PSK identity, if any. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002043 if (conf->psk_identity != NULL) {
2044 mbedtls_free(conf->psk_identity);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002045 conf->psk_identity = NULL;
2046 conf->psk_identity_len = 0;
2047 }
2048}
2049
Hanno Becker7390c712018-11-15 13:33:04 +00002050/* This function assumes that PSK identity in the SSL config is unset.
2051 * It checks that the provided identity is well-formed and attempts
2052 * to make a copy of it in the SSL config.
2053 * On failure, the PSK identity in the config remains unset. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002054MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002055static int ssl_conf_set_psk_identity(mbedtls_ssl_config *conf,
2056 unsigned char const *psk_identity,
2057 size_t psk_identity_len)
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002058{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02002059 /* Identity len will be encoded on two bytes */
Gilles Peskine449bd832023-01-11 14:50:10 +01002060 if (psk_identity == NULL ||
Ronald Cron2a87e9b2022-10-19 10:55:26 +02002061 psk_identity_len == 0 ||
Gilles Peskine449bd832023-01-11 14:50:10 +01002062 (psk_identity_len >> 16) != 0 ||
2063 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN) {
2064 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02002065 }
2066
Gilles Peskine449bd832023-01-11 14:50:10 +01002067 conf->psk_identity = mbedtls_calloc(1, psk_identity_len);
2068 if (conf->psk_identity == NULL) {
2069 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2070 }
Paul Bakker6db455e2013-09-18 17:29:31 +02002071
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01002072 conf->psk_identity_len = psk_identity_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002073 memcpy(conf->psk_identity, psk_identity, conf->psk_identity_len);
Paul Bakker5ad403f2013-09-18 21:21:30 +02002074
Gilles Peskine449bd832023-01-11 14:50:10 +01002075 return 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02002076}
2077
Gilles Peskine449bd832023-01-11 14:50:10 +01002078int mbedtls_ssl_conf_psk(mbedtls_ssl_config *conf,
2079 const unsigned char *psk, size_t psk_len,
2080 const unsigned char *psk_identity, size_t psk_identity_len)
Hanno Becker7390c712018-11-15 13:33:04 +00002081{
Janos Follath865b3eb2019-12-16 11:46:15 +00002082 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002083
2084 /* We currently only support one PSK, raw or opaque. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002085 if (mbedtls_ssl_conf_has_static_psk(conf)) {
2086 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
2087 }
Hanno Becker7390c712018-11-15 13:33:04 +00002088
2089 /* Check and set raw PSK */
Gilles Peskine449bd832023-01-11 14:50:10 +01002090 if (psk == NULL) {
2091 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2092 }
2093 if (psk_len == 0) {
2094 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2095 }
2096 if (psk_len > MBEDTLS_PSK_MAX_LEN) {
2097 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2098 }
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01002099
Gilles Peskine449bd832023-01-11 14:50:10 +01002100 if ((conf->psk = mbedtls_calloc(1, psk_len)) == NULL) {
2101 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2102 }
Hanno Becker7390c712018-11-15 13:33:04 +00002103 conf->psk_len = psk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002104 memcpy(conf->psk, psk, conf->psk_len);
Hanno Becker7390c712018-11-15 13:33:04 +00002105
2106 /* Check and set PSK Identity */
Gilles Peskine449bd832023-01-11 14:50:10 +01002107 ret = ssl_conf_set_psk_identity(conf, psk_identity, psk_identity_len);
2108 if (ret != 0) {
2109 ssl_conf_remove_psk(conf);
2110 }
Hanno Becker7390c712018-11-15 13:33:04 +00002111
Gilles Peskine449bd832023-01-11 14:50:10 +01002112 return ret;
Hanno Becker7390c712018-11-15 13:33:04 +00002113}
2114
Gilles Peskine449bd832023-01-11 14:50:10 +01002115static void ssl_remove_psk(mbedtls_ssl_context *ssl)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002116{
2117#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002118 if (!mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque)) {
Neil Armstrong501c9322022-05-03 09:35:09 +02002119 /* The maintenance of the external PSK key slot is the
2120 * user's responsibility. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002121 if (ssl->handshake->psk_opaque_is_internal) {
2122 psa_destroy_key(ssl->handshake->psk_opaque);
Neil Armstrong501c9322022-05-03 09:35:09 +02002123 ssl->handshake->psk_opaque_is_internal = 0;
2124 }
Ronald Croncf56a0a2020-08-04 09:51:30 +02002125 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002126 }
Neil Armstronge952a302022-05-03 10:22:14 +02002127#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002128 if (ssl->handshake->psk != NULL) {
2129 mbedtls_platform_zeroize(ssl->handshake->psk,
2130 ssl->handshake->psk_len);
2131 mbedtls_free(ssl->handshake->psk);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002132 ssl->handshake->psk_len = 0;
2133 }
Neil Armstronge952a302022-05-03 10:22:14 +02002134#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002135}
2136
Gilles Peskine449bd832023-01-11 14:50:10 +01002137int mbedtls_ssl_set_hs_psk(mbedtls_ssl_context *ssl,
2138 const unsigned char *psk, size_t psk_len)
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002139{
Neil Armstrong501c9322022-05-03 09:35:09 +02002140#if defined(MBEDTLS_USE_PSA_CRYPTO)
2141 psa_key_attributes_t key_attributes = psa_key_attributes_init();
Jerry Yu5d01c052022-08-17 10:18:10 +08002142 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jerry Yu24b8c812022-08-20 19:06:56 +08002143 psa_algorithm_t alg = PSA_ALG_NONE;
Jerry Yu5d01c052022-08-17 10:18:10 +08002144 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrong501c9322022-05-03 09:35:09 +02002145#endif /* MBEDTLS_USE_PSA_CRYPTO */
2146
Gilles Peskine449bd832023-01-11 14:50:10 +01002147 if (psk == NULL || ssl->handshake == NULL) {
2148 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2149 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002150
Gilles Peskine449bd832023-01-11 14:50:10 +01002151 if (psk_len > MBEDTLS_PSK_MAX_LEN) {
2152 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2153 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002154
Gilles Peskine449bd832023-01-11 14:50:10 +01002155 ssl_remove_psk(ssl);
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002156
Neil Armstrong501c9322022-05-03 09:35:09 +02002157#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yuccc68a42022-07-26 16:39:20 +08002158#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01002159 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2) {
2160 if (ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384) {
2161 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
2162 } else {
2163 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
2164 }
2165 psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE);
Jerry Yuccc68a42022-07-26 16:39:20 +08002166 }
2167#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Neil Armstrong501c9322022-05-03 09:35:09 +02002168
Jerry Yu568ec252022-07-22 21:27:34 +08002169#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01002170 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
2171 alg = PSA_ALG_HKDF_EXTRACT(PSA_ALG_ANY_HASH);
2172 psa_set_key_usage_flags(&key_attributes,
2173 PSA_KEY_USAGE_DERIVE | PSA_KEY_USAGE_EXPORT);
Jerry Yuccc68a42022-07-26 16:39:20 +08002174 }
2175#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
2176
Gilles Peskine449bd832023-01-11 14:50:10 +01002177 psa_set_key_algorithm(&key_attributes, alg);
2178 psa_set_key_type(&key_attributes, PSA_KEY_TYPE_DERIVE);
Neil Armstrong501c9322022-05-03 09:35:09 +02002179
Gilles Peskine449bd832023-01-11 14:50:10 +01002180 status = psa_import_key(&key_attributes, psk, psk_len, &key);
2181 if (status != PSA_SUCCESS) {
2182 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2183 }
Neil Armstrong501c9322022-05-03 09:35:09 +02002184
2185 /* Allow calling psa_destroy_key() on psk remove */
2186 ssl->handshake->psk_opaque_is_internal = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01002187 return mbedtls_ssl_set_hs_psk_opaque(ssl, key);
Neil Armstrong501c9322022-05-03 09:35:09 +02002188#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002189 if ((ssl->handshake->psk = mbedtls_calloc(1, psk_len)) == NULL) {
2190 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2191 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002192
2193 ssl->handshake->psk_len = psk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002194 memcpy(ssl->handshake->psk, psk, ssl->handshake->psk_len);
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002195
Gilles Peskine449bd832023-01-11 14:50:10 +01002196 return 0;
Neil Armstrong501c9322022-05-03 09:35:09 +02002197#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002198}
2199
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002200#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002201int mbedtls_ssl_conf_psk_opaque(mbedtls_ssl_config *conf,
2202 mbedtls_svc_key_id_t psk,
2203 const unsigned char *psk_identity,
2204 size_t psk_identity_len)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002205{
Janos Follath865b3eb2019-12-16 11:46:15 +00002206 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002207
2208 /* We currently only support one PSK, raw or opaque. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002209 if (mbedtls_ssl_conf_has_static_psk(conf)) {
2210 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
2211 }
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002212
Hanno Becker7390c712018-11-15 13:33:04 +00002213 /* Check and set opaque PSK */
Gilles Peskine449bd832023-01-11 14:50:10 +01002214 if (mbedtls_svc_key_id_is_null(psk)) {
2215 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2216 }
Ronald Croncf56a0a2020-08-04 09:51:30 +02002217 conf->psk_opaque = psk;
Hanno Becker7390c712018-11-15 13:33:04 +00002218
2219 /* Check and set PSK Identity */
Gilles Peskine449bd832023-01-11 14:50:10 +01002220 ret = ssl_conf_set_psk_identity(conf, psk_identity,
2221 psk_identity_len);
2222 if (ret != 0) {
2223 ssl_conf_remove_psk(conf);
2224 }
Hanno Becker7390c712018-11-15 13:33:04 +00002225
Gilles Peskine449bd832023-01-11 14:50:10 +01002226 return ret;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002227}
2228
Gilles Peskine449bd832023-01-11 14:50:10 +01002229int mbedtls_ssl_set_hs_psk_opaque(mbedtls_ssl_context *ssl,
2230 mbedtls_svc_key_id_t psk)
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002231{
Gilles Peskine449bd832023-01-11 14:50:10 +01002232 if ((mbedtls_svc_key_id_is_null(psk)) ||
2233 (ssl->handshake == NULL)) {
2234 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2235 }
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002236
Gilles Peskine449bd832023-01-11 14:50:10 +01002237 ssl_remove_psk(ssl);
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002238 ssl->handshake->psk_opaque = psk;
Gilles Peskine449bd832023-01-11 14:50:10 +01002239 return 0;
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002240}
2241#endif /* MBEDTLS_USE_PSA_CRYPTO */
2242
Jerry Yu8897c072022-08-12 13:56:53 +08002243#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002244void mbedtls_ssl_conf_psk_cb(mbedtls_ssl_config *conf,
2245 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
2246 size_t),
2247 void *p_psk)
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002248{
2249 conf->f_psk = f_psk;
2250 conf->p_psk = p_psk;
2251}
Jerry Yu8897c072022-08-12 13:56:53 +08002252#endif /* MBEDTLS_SSL_SRV_C */
2253
Ronald Cron73fe8df2022-10-05 14:31:43 +02002254#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002255
2256#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine301711e2022-04-26 16:57:05 +02002257static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
Gilles Peskine449bd832023-01-11 14:50:10 +01002258 psa_algorithm_t alg)
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002259{
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002260#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01002261 if (alg == PSA_ALG_CBC_NO_PADDING) {
2262 return MBEDTLS_SSL_MODE_CBC;
2263 }
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002264#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
Gilles Peskine449bd832023-01-11 14:50:10 +01002265 if (PSA_ALG_IS_AEAD(alg)) {
2266 return MBEDTLS_SSL_MODE_AEAD;
2267 }
2268 return MBEDTLS_SSL_MODE_STREAM;
Gilles Peskine301711e2022-04-26 16:57:05 +02002269}
2270
2271#else /* MBEDTLS_USE_PSA_CRYPTO */
2272
2273static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
Gilles Peskine449bd832023-01-11 14:50:10 +01002274 mbedtls_cipher_mode_t mode)
Gilles Peskine301711e2022-04-26 16:57:05 +02002275{
2276#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01002277 if (mode == MBEDTLS_MODE_CBC) {
2278 return MBEDTLS_SSL_MODE_CBC;
2279 }
Gilles Peskine301711e2022-04-26 16:57:05 +02002280#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
2281
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002282#if defined(MBEDTLS_GCM_C) || \
2283 defined(MBEDTLS_CCM_C) || \
2284 defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002285 if (mode == MBEDTLS_MODE_GCM ||
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002286 mode == MBEDTLS_MODE_CCM ||
Gilles Peskine449bd832023-01-11 14:50:10 +01002287 mode == MBEDTLS_MODE_CHACHAPOLY) {
2288 return MBEDTLS_SSL_MODE_AEAD;
2289 }
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002290#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002291
Gilles Peskine449bd832023-01-11 14:50:10 +01002292 return MBEDTLS_SSL_MODE_STREAM;
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002293}
Gilles Peskine301711e2022-04-26 16:57:05 +02002294#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002295
Gilles Peskinee108d982022-04-26 16:50:40 +02002296static mbedtls_ssl_mode_t mbedtls_ssl_get_actual_mode(
2297 mbedtls_ssl_mode_t base_mode,
Gilles Peskine449bd832023-01-11 14:50:10 +01002298 int encrypt_then_mac)
Gilles Peskinee108d982022-04-26 16:50:40 +02002299{
2300#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01002301 if (encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED &&
2302 base_mode == MBEDTLS_SSL_MODE_CBC) {
2303 return MBEDTLS_SSL_MODE_CBC_ETM;
Gilles Peskinee108d982022-04-26 16:50:40 +02002304 }
2305#else
2306 (void) encrypt_then_mac;
2307#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002308 return base_mode;
Gilles Peskinee108d982022-04-26 16:50:40 +02002309}
2310
Neil Armstrongab555e02022-04-04 11:07:59 +02002311mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_transform(
Gilles Peskine449bd832023-01-11 14:50:10 +01002312 const mbedtls_ssl_transform *transform)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002313{
Gilles Peskinee108d982022-04-26 16:50:40 +02002314 mbedtls_ssl_mode_t base_mode = mbedtls_ssl_get_base_mode(
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002315#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002316 transform->psa_alg
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002317#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002318 mbedtls_cipher_get_cipher_mode(&transform->cipher_ctx_enc)
Gilles Peskinee108d982022-04-26 16:50:40 +02002319#endif
2320 );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002321
Gilles Peskinee108d982022-04-26 16:50:40 +02002322 int encrypt_then_mac = 0;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002323#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskinee108d982022-04-26 16:50:40 +02002324 encrypt_then_mac = transform->encrypt_then_mac;
2325#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002326 return mbedtls_ssl_get_actual_mode(base_mode, encrypt_then_mac);
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002327}
2328
Neil Armstrongab555e02022-04-04 11:07:59 +02002329mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002330#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01002331 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002332#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01002333 const mbedtls_ssl_ciphersuite_t *suite)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002334{
Gilles Peskinee108d982022-04-26 16:50:40 +02002335 mbedtls_ssl_mode_t base_mode = MBEDTLS_SSL_MODE_STREAM;
2336
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002337#if defined(MBEDTLS_USE_PSA_CRYPTO)
2338 psa_status_t status;
2339 psa_algorithm_t alg;
2340 psa_key_type_t type;
2341 size_t size;
Gilles Peskine449bd832023-01-11 14:50:10 +01002342 status = mbedtls_ssl_cipher_to_psa(suite->cipher, 0, &alg, &type, &size);
2343 if (status == PSA_SUCCESS) {
2344 base_mode = mbedtls_ssl_get_base_mode(alg);
2345 }
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002346#else
2347 const mbedtls_cipher_info_t *cipher =
Gilles Peskine449bd832023-01-11 14:50:10 +01002348 mbedtls_cipher_info_from_type(suite->cipher);
2349 if (cipher != NULL) {
Gilles Peskinee108d982022-04-26 16:50:40 +02002350 base_mode =
2351 mbedtls_ssl_get_base_mode(
Gilles Peskine449bd832023-01-11 14:50:10 +01002352 mbedtls_cipher_info_get_mode(cipher));
Gilles Peskinee108d982022-04-26 16:50:40 +02002353 }
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002354#endif /* MBEDTLS_USE_PSA_CRYPTO */
2355
Gilles Peskinee108d982022-04-26 16:50:40 +02002356#if !defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
2357 int encrypt_then_mac = 0;
2358#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002359 return mbedtls_ssl_get_actual_mode(base_mode, encrypt_then_mac);
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002360}
2361
Neil Armstrong8395d7a2022-05-18 11:44:56 +02002362#if defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu251a12e2022-07-13 15:15:48 +08002363
2364#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu438ddd82022-07-07 06:55:50 +00002365/* Serialization of TLS 1.3 sessions:
2366 *
2367 * struct {
Xiaokang Qian126bf8e2022-10-13 02:22:40 +00002368 * opaque hostname<0..2^16-1>;
Jerry Yu438ddd82022-07-07 06:55:50 +00002369 * uint64 ticket_received;
2370 * uint32 ticket_lifetime;
Jerry Yu34191072022-08-18 10:32:09 +08002371 * opaque ticket<1..2^16-1>;
Jerry Yu438ddd82022-07-07 06:55:50 +00002372 * } ClientOnlyData;
2373 *
2374 * struct {
2375 * uint8 endpoint;
2376 * uint8 ciphersuite[2];
2377 * uint32 ticket_age_add;
2378 * uint8 ticket_flags;
2379 * opaque resumption_key<0..255>;
2380 * select ( endpoint ) {
2381 * case client: ClientOnlyData;
2382 * case server: uint64 start_time;
2383 * };
2384 * } serialized_session_tls13;
2385 *
2386 */
2387#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yue36fdd62022-08-17 21:31:36 +08002388MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002389static int ssl_tls13_session_save(const mbedtls_ssl_session *session,
2390 unsigned char *buf,
2391 size_t buf_len,
2392 size_t *olen)
Jerry Yu438ddd82022-07-07 06:55:50 +00002393{
2394 unsigned char *p = buf;
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00002395#if defined(MBEDTLS_SSL_CLI_C) && \
2396 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01002397 size_t hostname_len = (session->hostname == NULL) ?
2398 0 : strlen(session->hostname) + 1;
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00002399#endif
Jerry Yubc7c1a42022-07-21 22:57:37 +08002400 size_t needed = 1 /* endpoint */
2401 + 2 /* ciphersuite */
2402 + 4 /* ticket_age_add */
Jerry Yu34191072022-08-18 10:32:09 +08002403 + 1 /* ticket_flags */
2404 + 1; /* resumption_key length */
Jerry Yue36fdd62022-08-17 21:31:36 +08002405 *olen = 0;
Jerry Yu34191072022-08-18 10:32:09 +08002406
Gilles Peskine449bd832023-01-11 14:50:10 +01002407 if (session->resumption_key_len > MBEDTLS_SSL_TLS1_3_TICKET_RESUMPTION_KEY_LEN) {
2408 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2409 }
Jerry Yu34191072022-08-18 10:32:09 +08002410 needed += session->resumption_key_len; /* resumption_key */
2411
Jerry Yu438ddd82022-07-07 06:55:50 +00002412#if defined(MBEDTLS_HAVE_TIME)
2413 needed += 8; /* start_time or ticket_received */
2414#endif
2415
2416#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002417 if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Xiaokang Qian126bf8e2022-10-13 02:22:40 +00002418#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaokang Qianbc663a02022-10-09 11:14:39 +00002419 needed += 2 /* hostname_len */
Gilles Peskine449bd832023-01-11 14:50:10 +01002420 + hostname_len; /* hostname */
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00002421#endif
2422
Jerry Yu438ddd82022-07-07 06:55:50 +00002423 needed += 4 /* ticket_lifetime */
Jerry Yue36fdd62022-08-17 21:31:36 +08002424 + 2; /* ticket_len */
Jerry Yu34191072022-08-18 10:32:09 +08002425
2426 /* Check size_t overflow */
Gilles Peskine449bd832023-01-11 14:50:10 +01002427 if (session->ticket_len > SIZE_MAX - needed) {
2428 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2429 }
Jerry Yu34191072022-08-18 10:32:09 +08002430
Jerry Yue28d9742022-08-18 15:44:03 +08002431 needed += session->ticket_len; /* ticket */
Jerry Yu438ddd82022-07-07 06:55:50 +00002432 }
2433#endif /* MBEDTLS_SSL_CLI_C */
2434
Jerry Yue36fdd62022-08-17 21:31:36 +08002435 *olen = needed;
Gilles Peskine449bd832023-01-11 14:50:10 +01002436 if (needed > buf_len) {
2437 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
2438 }
Jerry Yu438ddd82022-07-07 06:55:50 +00002439
2440 p[0] = session->endpoint;
Gilles Peskine449bd832023-01-11 14:50:10 +01002441 MBEDTLS_PUT_UINT16_BE(session->ciphersuite, p, 1);
2442 MBEDTLS_PUT_UINT32_BE(session->ticket_age_add, p, 3);
Jerry Yu438ddd82022-07-07 06:55:50 +00002443 p[7] = session->ticket_flags;
2444
2445 /* save resumption_key */
Jerry Yubc7c1a42022-07-21 22:57:37 +08002446 p[8] = session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002447 p += 9;
Gilles Peskine449bd832023-01-11 14:50:10 +01002448 memcpy(p, session->resumption_key, session->resumption_key_len);
Jerry Yubc7c1a42022-07-21 22:57:37 +08002449 p += session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002450
2451#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002452 if (session->endpoint == MBEDTLS_SSL_IS_SERVER) {
2453 MBEDTLS_PUT_UINT64_BE((uint64_t) session->start, p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002454 p += 8;
2455 }
2456#endif /* MBEDTLS_HAVE_TIME */
2457
2458#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002459 if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002460#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01002461 MBEDTLS_PUT_UINT16_BE(hostname_len, p, 0);
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002462 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002463 if (hostname_len > 0) {
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002464 /* save host name */
Gilles Peskine449bd832023-01-11 14:50:10 +01002465 memcpy(p, session->hostname, hostname_len);
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002466 p += hostname_len;
2467 }
2468#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
2469
Jerry Yu438ddd82022-07-07 06:55:50 +00002470#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01002471 MBEDTLS_PUT_UINT64_BE((uint64_t) session->ticket_received, p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002472 p += 8;
2473#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002474 MBEDTLS_PUT_UINT32_BE(session->ticket_lifetime, p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002475 p += 4;
2476
Gilles Peskine449bd832023-01-11 14:50:10 +01002477 MBEDTLS_PUT_UINT16_BE(session->ticket_len, p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002478 p += 2;
Jerry Yu34191072022-08-18 10:32:09 +08002479
Gilles Peskine449bd832023-01-11 14:50:10 +01002480 if (session->ticket != NULL && session->ticket_len > 0) {
2481 memcpy(p, session->ticket, session->ticket_len);
Jerry Yu438ddd82022-07-07 06:55:50 +00002482 p += session->ticket_len;
2483 }
2484 }
2485#endif /* MBEDTLS_SSL_CLI_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01002486 return 0;
Jerry Yu438ddd82022-07-07 06:55:50 +00002487}
2488
2489MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002490static int ssl_tls13_session_load(mbedtls_ssl_session *session,
2491 const unsigned char *buf,
2492 size_t len)
Jerry Yu438ddd82022-07-07 06:55:50 +00002493{
2494 const unsigned char *p = buf;
2495 const unsigned char *end = buf + len;
2496
Gilles Peskine449bd832023-01-11 14:50:10 +01002497 if (end - p < 9) {
2498 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2499 }
Jerry Yu438ddd82022-07-07 06:55:50 +00002500 session->endpoint = p[0];
Gilles Peskine449bd832023-01-11 14:50:10 +01002501 session->ciphersuite = MBEDTLS_GET_UINT16_BE(p, 1);
2502 session->ticket_age_add = MBEDTLS_GET_UINT32_BE(p, 3);
Jerry Yu438ddd82022-07-07 06:55:50 +00002503 session->ticket_flags = p[7];
2504
2505 /* load resumption_key */
Jerry Yubc7c1a42022-07-21 22:57:37 +08002506 session->resumption_key_len = p[8];
Jerry Yu438ddd82022-07-07 06:55:50 +00002507 p += 9;
2508
Gilles Peskine449bd832023-01-11 14:50:10 +01002509 if (end - p < session->resumption_key_len) {
2510 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2511 }
Jerry Yu438ddd82022-07-07 06:55:50 +00002512
Gilles Peskine449bd832023-01-11 14:50:10 +01002513 if (sizeof(session->resumption_key) < session->resumption_key_len) {
2514 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2515 }
2516 memcpy(session->resumption_key, p, session->resumption_key_len);
Jerry Yubc7c1a42022-07-21 22:57:37 +08002517 p += session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002518
2519#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002520 if (session->endpoint == MBEDTLS_SSL_IS_SERVER) {
2521 if (end - p < 8) {
2522 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2523 }
2524 session->start = MBEDTLS_GET_UINT64_BE(p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002525 p += 8;
2526 }
2527#endif /* MBEDTLS_HAVE_TIME */
2528
2529#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002530 if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002531#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01002532 defined(MBEDTLS_SSL_SESSION_TICKETS)
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002533 size_t hostname_len;
2534 /* load host name */
Gilles Peskine449bd832023-01-11 14:50:10 +01002535 if (end - p < 2) {
2536 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2537 }
2538 hostname_len = MBEDTLS_GET_UINT16_BE(p, 0);
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002539 p += 2;
2540
Gilles Peskine449bd832023-01-11 14:50:10 +01002541 if (end - p < (long int) hostname_len) {
2542 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2543 }
2544 if (hostname_len > 0) {
2545 session->hostname = mbedtls_calloc(1, hostname_len);
2546 if (session->hostname == NULL) {
2547 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2548 }
2549 memcpy(session->hostname, p, hostname_len);
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002550 p += hostname_len;
2551 }
2552#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION &&
2553 MBEDTLS_SSL_SESSION_TICKETS */
2554
Jerry Yu438ddd82022-07-07 06:55:50 +00002555#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01002556 if (end - p < 8) {
2557 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2558 }
2559 session->ticket_received = MBEDTLS_GET_UINT64_BE(p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002560 p += 8;
2561#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002562 if (end - p < 4) {
2563 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2564 }
2565 session->ticket_lifetime = MBEDTLS_GET_UINT32_BE(p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002566 p += 4;
2567
Gilles Peskine449bd832023-01-11 14:50:10 +01002568 if (end - p < 2) {
2569 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2570 }
2571 session->ticket_len = MBEDTLS_GET_UINT16_BE(p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002572 p += 2;
2573
Gilles Peskine449bd832023-01-11 14:50:10 +01002574 if (end - p < (long int) session->ticket_len) {
2575 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2576 }
2577 if (session->ticket_len > 0) {
2578 session->ticket = mbedtls_calloc(1, session->ticket_len);
2579 if (session->ticket == NULL) {
2580 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2581 }
2582 memcpy(session->ticket, p, session->ticket_len);
Jerry Yu438ddd82022-07-07 06:55:50 +00002583 p += session->ticket_len;
2584 }
2585 }
2586#endif /* MBEDTLS_SSL_CLI_C */
2587
Gilles Peskine449bd832023-01-11 14:50:10 +01002588 return 0;
Jerry Yu438ddd82022-07-07 06:55:50 +00002589
2590}
2591#else /* MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yue36fdd62022-08-17 21:31:36 +08002592MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002593static int ssl_tls13_session_save(const mbedtls_ssl_session *session,
2594 unsigned char *buf,
2595 size_t buf_len,
2596 size_t *olen)
Jerry Yu251a12e2022-07-13 15:15:48 +08002597{
2598 ((void) session);
2599 ((void) buf);
2600 ((void) buf_len);
Jerry Yue36fdd62022-08-17 21:31:36 +08002601 *olen = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002602 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Jerry Yu251a12e2022-07-13 15:15:48 +08002603}
Jerry Yu438ddd82022-07-07 06:55:50 +00002604
Gilles Peskine449bd832023-01-11 14:50:10 +01002605static int ssl_tls13_session_load(const mbedtls_ssl_session *session,
2606 unsigned char *buf,
2607 size_t buf_len)
Jerry Yu438ddd82022-07-07 06:55:50 +00002608{
2609 ((void) session);
2610 ((void) buf);
2611 ((void) buf_len);
Gilles Peskine449bd832023-01-11 14:50:10 +01002612 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Jerry Yu438ddd82022-07-07 06:55:50 +00002613}
2614#endif /* !MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yu251a12e2022-07-13 15:15:48 +08002615#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
2616
Gilles Peskine449bd832023-01-11 14:50:10 +01002617psa_status_t mbedtls_ssl_cipher_to_psa(mbedtls_cipher_type_t mbedtls_cipher_type,
2618 size_t taglen,
2619 psa_algorithm_t *alg,
2620 psa_key_type_t *key_type,
2621 size_t *key_size)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002622{
Gilles Peskine449bd832023-01-11 14:50:10 +01002623 switch (mbedtls_cipher_type) {
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002624 case MBEDTLS_CIPHER_AES_128_CBC:
2625 *alg = PSA_ALG_CBC_NO_PADDING;
2626 *key_type = PSA_KEY_TYPE_AES;
2627 *key_size = 128;
2628 break;
2629 case MBEDTLS_CIPHER_AES_128_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002630 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002631 *key_type = PSA_KEY_TYPE_AES;
2632 *key_size = 128;
2633 break;
2634 case MBEDTLS_CIPHER_AES_128_GCM:
2635 *alg = PSA_ALG_GCM;
2636 *key_type = PSA_KEY_TYPE_AES;
2637 *key_size = 128;
2638 break;
2639 case MBEDTLS_CIPHER_AES_192_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002640 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002641 *key_type = PSA_KEY_TYPE_AES;
2642 *key_size = 192;
2643 break;
2644 case MBEDTLS_CIPHER_AES_192_GCM:
2645 *alg = PSA_ALG_GCM;
2646 *key_type = PSA_KEY_TYPE_AES;
2647 *key_size = 192;
2648 break;
2649 case MBEDTLS_CIPHER_AES_256_CBC:
2650 *alg = PSA_ALG_CBC_NO_PADDING;
2651 *key_type = PSA_KEY_TYPE_AES;
2652 *key_size = 256;
2653 break;
2654 case MBEDTLS_CIPHER_AES_256_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002655 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002656 *key_type = PSA_KEY_TYPE_AES;
2657 *key_size = 256;
2658 break;
2659 case MBEDTLS_CIPHER_AES_256_GCM:
2660 *alg = PSA_ALG_GCM;
2661 *key_type = PSA_KEY_TYPE_AES;
2662 *key_size = 256;
2663 break;
2664 case MBEDTLS_CIPHER_ARIA_128_CBC:
2665 *alg = PSA_ALG_CBC_NO_PADDING;
2666 *key_type = PSA_KEY_TYPE_ARIA;
2667 *key_size = 128;
2668 break;
2669 case MBEDTLS_CIPHER_ARIA_128_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002670 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002671 *key_type = PSA_KEY_TYPE_ARIA;
2672 *key_size = 128;
2673 break;
2674 case MBEDTLS_CIPHER_ARIA_128_GCM:
2675 *alg = PSA_ALG_GCM;
2676 *key_type = PSA_KEY_TYPE_ARIA;
2677 *key_size = 128;
2678 break;
2679 case MBEDTLS_CIPHER_ARIA_192_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002680 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002681 *key_type = PSA_KEY_TYPE_ARIA;
2682 *key_size = 192;
2683 break;
2684 case MBEDTLS_CIPHER_ARIA_192_GCM:
2685 *alg = PSA_ALG_GCM;
2686 *key_type = PSA_KEY_TYPE_ARIA;
2687 *key_size = 192;
2688 break;
2689 case MBEDTLS_CIPHER_ARIA_256_CBC:
2690 *alg = PSA_ALG_CBC_NO_PADDING;
2691 *key_type = PSA_KEY_TYPE_ARIA;
2692 *key_size = 256;
2693 break;
2694 case MBEDTLS_CIPHER_ARIA_256_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002695 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002696 *key_type = PSA_KEY_TYPE_ARIA;
2697 *key_size = 256;
2698 break;
2699 case MBEDTLS_CIPHER_ARIA_256_GCM:
2700 *alg = PSA_ALG_GCM;
2701 *key_type = PSA_KEY_TYPE_ARIA;
2702 *key_size = 256;
2703 break;
2704 case MBEDTLS_CIPHER_CAMELLIA_128_CBC:
2705 *alg = PSA_ALG_CBC_NO_PADDING;
2706 *key_type = PSA_KEY_TYPE_CAMELLIA;
2707 *key_size = 128;
2708 break;
2709 case MBEDTLS_CIPHER_CAMELLIA_128_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002710 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002711 *key_type = PSA_KEY_TYPE_CAMELLIA;
2712 *key_size = 128;
2713 break;
2714 case MBEDTLS_CIPHER_CAMELLIA_128_GCM:
2715 *alg = PSA_ALG_GCM;
2716 *key_type = PSA_KEY_TYPE_CAMELLIA;
2717 *key_size = 128;
2718 break;
2719 case MBEDTLS_CIPHER_CAMELLIA_192_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002720 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002721 *key_type = PSA_KEY_TYPE_CAMELLIA;
2722 *key_size = 192;
2723 break;
2724 case MBEDTLS_CIPHER_CAMELLIA_192_GCM:
2725 *alg = PSA_ALG_GCM;
2726 *key_type = PSA_KEY_TYPE_CAMELLIA;
2727 *key_size = 192;
2728 break;
2729 case MBEDTLS_CIPHER_CAMELLIA_256_CBC:
2730 *alg = PSA_ALG_CBC_NO_PADDING;
2731 *key_type = PSA_KEY_TYPE_CAMELLIA;
2732 *key_size = 256;
2733 break;
2734 case MBEDTLS_CIPHER_CAMELLIA_256_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002735 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002736 *key_type = PSA_KEY_TYPE_CAMELLIA;
2737 *key_size = 256;
2738 break;
2739 case MBEDTLS_CIPHER_CAMELLIA_256_GCM:
2740 *alg = PSA_ALG_GCM;
2741 *key_type = PSA_KEY_TYPE_CAMELLIA;
2742 *key_size = 256;
2743 break;
2744 case MBEDTLS_CIPHER_CHACHA20_POLY1305:
2745 *alg = PSA_ALG_CHACHA20_POLY1305;
2746 *key_type = PSA_KEY_TYPE_CHACHA20;
2747 *key_size = 256;
2748 break;
2749 case MBEDTLS_CIPHER_NULL:
2750 *alg = MBEDTLS_SSL_NULL_CIPHER;
2751 *key_type = 0;
2752 *key_size = 0;
2753 break;
2754 default:
2755 return PSA_ERROR_NOT_SUPPORTED;
2756 }
2757
2758 return PSA_SUCCESS;
2759}
Neil Armstrong8395d7a2022-05-18 11:44:56 +02002760#endif /* MBEDTLS_USE_PSA_CRYPTO || MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002761
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002762#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002763int mbedtls_ssl_conf_dh_param_bin(mbedtls_ssl_config *conf,
2764 const unsigned char *dhm_P, size_t P_len,
2765 const unsigned char *dhm_G, size_t G_len)
Hanno Beckera90658f2017-10-04 15:29:08 +01002766{
Janos Follath865b3eb2019-12-16 11:46:15 +00002767 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckera90658f2017-10-04 15:29:08 +01002768
Gilles Peskine449bd832023-01-11 14:50:10 +01002769 mbedtls_mpi_free(&conf->dhm_P);
2770 mbedtls_mpi_free(&conf->dhm_G);
Glenn Strausscee11292021-12-20 01:43:17 -05002771
Gilles Peskine449bd832023-01-11 14:50:10 +01002772 if ((ret = mbedtls_mpi_read_binary(&conf->dhm_P, dhm_P, P_len)) != 0 ||
2773 (ret = mbedtls_mpi_read_binary(&conf->dhm_G, dhm_G, G_len)) != 0) {
2774 mbedtls_mpi_free(&conf->dhm_P);
2775 mbedtls_mpi_free(&conf->dhm_G);
2776 return ret;
Hanno Beckera90658f2017-10-04 15:29:08 +01002777 }
2778
Gilles Peskine449bd832023-01-11 14:50:10 +01002779 return 0;
Hanno Beckera90658f2017-10-04 15:29:08 +01002780}
Paul Bakker5121ce52009-01-03 21:22:43 +00002781
Gilles Peskine449bd832023-01-11 14:50:10 +01002782int mbedtls_ssl_conf_dh_param_ctx(mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx)
Paul Bakker1b57b062011-01-06 15:48:19 +00002783{
Janos Follath865b3eb2019-12-16 11:46:15 +00002784 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1b57b062011-01-06 15:48:19 +00002785
Gilles Peskine449bd832023-01-11 14:50:10 +01002786 mbedtls_mpi_free(&conf->dhm_P);
2787 mbedtls_mpi_free(&conf->dhm_G);
Glenn Strausscee11292021-12-20 01:43:17 -05002788
Gilles Peskine449bd832023-01-11 14:50:10 +01002789 if ((ret = mbedtls_dhm_get_value(dhm_ctx, MBEDTLS_DHM_PARAM_P,
2790 &conf->dhm_P)) != 0 ||
2791 (ret = mbedtls_dhm_get_value(dhm_ctx, MBEDTLS_DHM_PARAM_G,
2792 &conf->dhm_G)) != 0) {
2793 mbedtls_mpi_free(&conf->dhm_P);
2794 mbedtls_mpi_free(&conf->dhm_G);
2795 return ret;
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01002796 }
Paul Bakker1b57b062011-01-06 15:48:19 +00002797
Gilles Peskine449bd832023-01-11 14:50:10 +01002798 return 0;
Paul Bakker1b57b062011-01-06 15:48:19 +00002799}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002800#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00002801
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02002802#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
2803/*
2804 * Set the minimum length for Diffie-Hellman parameters
2805 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002806void mbedtls_ssl_conf_dhm_min_bitlen(mbedtls_ssl_config *conf,
2807 unsigned int bitlen)
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02002808{
2809 conf->dhm_min_bitlen = bitlen;
2810}
2811#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
2812
Ronald Crone68ab4f2022-10-05 12:46:29 +02002813#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002814#if !defined(MBEDTLS_DEPRECATED_REMOVED) && defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002815/*
2816 * Set allowed/preferred hashes for handshake signatures
2817 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002818void mbedtls_ssl_conf_sig_hashes(mbedtls_ssl_config *conf,
2819 const int *hashes)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002820{
2821 conf->sig_hashes = hashes;
2822}
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002823#endif /* !MBEDTLS_DEPRECATED_REMOVED && MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker1cd6e002021-08-10 13:27:10 +01002824
Jerry Yuf017ee42022-01-12 15:49:48 +08002825/* Configure allowed signature algorithms for handshake */
Gilles Peskine449bd832023-01-11 14:50:10 +01002826void mbedtls_ssl_conf_sig_algs(mbedtls_ssl_config *conf,
2827 const uint16_t *sig_algs)
Hanno Becker1cd6e002021-08-10 13:27:10 +01002828{
Jerry Yuf017ee42022-01-12 15:49:48 +08002829#if !defined(MBEDTLS_DEPRECATED_REMOVED)
2830 conf->sig_hashes = NULL;
2831#endif /* !MBEDTLS_DEPRECATED_REMOVED */
2832 conf->sig_algs = sig_algs;
Hanno Becker1cd6e002021-08-10 13:27:10 +01002833}
Ronald Crone68ab4f2022-10-05 12:46:29 +02002834#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002835
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02002836#if defined(MBEDTLS_ECP_C)
Brett Warrene0edc842021-08-17 09:53:13 +01002837#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002838/*
2839 * Set the allowed elliptic curves
Brett Warrene0edc842021-08-17 09:53:13 +01002840 *
2841 * mbedtls_ssl_setup() takes the provided list
2842 * and translates it to a list of IANA TLS group identifiers,
2843 * stored in ssl->handshake->group_list.
2844 *
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002845 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002846void mbedtls_ssl_conf_curves(mbedtls_ssl_config *conf,
2847 const mbedtls_ecp_group_id *curve_list)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002848{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002849 conf->curve_list = curve_list;
Brett Warrene0edc842021-08-17 09:53:13 +01002850 conf->group_list = NULL;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002851}
Brett Warrene0edc842021-08-17 09:53:13 +01002852#endif /* MBEDTLS_DEPRECATED_REMOVED */
Hanno Becker947194e2017-04-07 13:25:49 +01002853#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002854
Brett Warrene0edc842021-08-17 09:53:13 +01002855/*
2856 * Set the allowed groups
2857 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002858void mbedtls_ssl_conf_groups(mbedtls_ssl_config *conf,
2859 const uint16_t *group_list)
Brett Warrene0edc842021-08-17 09:53:13 +01002860{
2861#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
2862 conf->curve_list = NULL;
2863#endif
2864 conf->group_list = group_list;
2865}
2866
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002867#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002868int mbedtls_ssl_set_hostname(mbedtls_ssl_context *ssl, const char *hostname)
Paul Bakker5121ce52009-01-03 21:22:43 +00002869{
Hanno Becker947194e2017-04-07 13:25:49 +01002870 /* Initialize to suppress unnecessary compiler warning */
2871 size_t hostname_len = 0;
2872
2873 /* Check if new hostname is valid before
2874 * making any change to current one */
Gilles Peskine449bd832023-01-11 14:50:10 +01002875 if (hostname != NULL) {
2876 hostname_len = strlen(hostname);
Hanno Becker947194e2017-04-07 13:25:49 +01002877
Gilles Peskine449bd832023-01-11 14:50:10 +01002878 if (hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN) {
2879 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2880 }
Hanno Becker947194e2017-04-07 13:25:49 +01002881 }
2882
2883 /* Now it's clear that we will overwrite the old hostname,
2884 * so we can free it safely */
2885
Gilles Peskine449bd832023-01-11 14:50:10 +01002886 if (ssl->hostname != NULL) {
2887 mbedtls_platform_zeroize(ssl->hostname, strlen(ssl->hostname));
2888 mbedtls_free(ssl->hostname);
Hanno Becker947194e2017-04-07 13:25:49 +01002889 }
2890
2891 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01002892
Gilles Peskine449bd832023-01-11 14:50:10 +01002893 if (hostname == NULL) {
Hanno Becker947194e2017-04-07 13:25:49 +01002894 ssl->hostname = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01002895 } else {
2896 ssl->hostname = mbedtls_calloc(1, hostname_len + 1);
2897 if (ssl->hostname == NULL) {
2898 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2899 }
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002900
Gilles Peskine449bd832023-01-11 14:50:10 +01002901 memcpy(ssl->hostname, hostname, hostname_len);
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002902
Hanno Becker947194e2017-04-07 13:25:49 +01002903 ssl->hostname[hostname_len] = '\0';
2904 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002905
Gilles Peskine449bd832023-01-11 14:50:10 +01002906 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002907}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01002908#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002909
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002910#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01002911void mbedtls_ssl_conf_sni(mbedtls_ssl_config *conf,
2912 int (*f_sni)(void *, mbedtls_ssl_context *,
2913 const unsigned char *, size_t),
2914 void *p_sni)
Paul Bakker5701cdc2012-09-27 21:49:42 +00002915{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002916 conf->f_sni = f_sni;
2917 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00002918}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002919#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00002920
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002921#if defined(MBEDTLS_SSL_ALPN)
Gilles Peskine449bd832023-01-11 14:50:10 +01002922int mbedtls_ssl_conf_alpn_protocols(mbedtls_ssl_config *conf, const char **protos)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002923{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002924 size_t cur_len, tot_len;
2925 const char **p;
2926
2927 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08002928 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
2929 * MUST NOT be truncated."
2930 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002931 */
2932 tot_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002933 for (p = protos; *p != NULL; p++) {
2934 cur_len = strlen(*p);
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002935 tot_len += cur_len;
2936
Gilles Peskine449bd832023-01-11 14:50:10 +01002937 if ((cur_len == 0) ||
2938 (cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN) ||
2939 (tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN)) {
2940 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2941 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002942 }
2943
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002944 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002945
Gilles Peskine449bd832023-01-11 14:50:10 +01002946 return 0;
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002947}
2948
Gilles Peskine449bd832023-01-11 14:50:10 +01002949const char *mbedtls_ssl_get_alpn_protocol(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002950{
Gilles Peskine449bd832023-01-11 14:50:10 +01002951 return ssl->alpn_chosen;
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002952}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002953#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002954
Johan Pascalb62bb512015-12-03 21:56:45 +01002955#if defined(MBEDTLS_SSL_DTLS_SRTP)
Gilles Peskine449bd832023-01-11 14:50:10 +01002956void mbedtls_ssl_conf_srtp_mki_value_supported(mbedtls_ssl_config *conf,
2957 int support_mki_value)
Ron Eldor591f1622018-01-22 12:30:04 +02002958{
2959 conf->dtls_srtp_mki_support = support_mki_value;
2960}
2961
Gilles Peskine449bd832023-01-11 14:50:10 +01002962int mbedtls_ssl_dtls_srtp_set_mki_value(mbedtls_ssl_context *ssl,
2963 unsigned char *mki_value,
2964 uint16_t mki_len)
Ron Eldor591f1622018-01-22 12:30:04 +02002965{
Gilles Peskine449bd832023-01-11 14:50:10 +01002966 if (mki_len > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH) {
2967 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Ron Eldora9788042018-12-05 11:04:31 +02002968 }
Ron Eldor591f1622018-01-22 12:30:04 +02002969
Gilles Peskine449bd832023-01-11 14:50:10 +01002970 if (ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_UNSUPPORTED) {
2971 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Ron Eldora9788042018-12-05 11:04:31 +02002972 }
Ron Eldor591f1622018-01-22 12:30:04 +02002973
Gilles Peskine449bd832023-01-11 14:50:10 +01002974 memcpy(ssl->dtls_srtp_info.mki_value, mki_value, mki_len);
Ron Eldor591f1622018-01-22 12:30:04 +02002975 ssl->dtls_srtp_info.mki_len = mki_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002976 return 0;
Ron Eldor591f1622018-01-22 12:30:04 +02002977}
2978
Gilles Peskine449bd832023-01-11 14:50:10 +01002979int mbedtls_ssl_conf_dtls_srtp_protection_profiles(mbedtls_ssl_config *conf,
2980 const mbedtls_ssl_srtp_profile *profiles)
Johan Pascalb62bb512015-12-03 21:56:45 +01002981{
Johan Pascal253d0262020-09-22 13:04:45 +02002982 const mbedtls_ssl_srtp_profile *p;
2983 size_t list_size = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01002984
Johan Pascal253d0262020-09-22 13:04:45 +02002985 /* check the profiles list: all entry must be valid,
2986 * its size cannot be more than the total number of supported profiles, currently 4 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002987 for (p = profiles; *p != MBEDTLS_TLS_SRTP_UNSET &&
2988 list_size <= MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH;
2989 p++) {
2990 if (mbedtls_ssl_check_srtp_profile_value(*p) != MBEDTLS_TLS_SRTP_UNSET) {
Johan Pascal76fdf1d2020-10-22 23:31:00 +02002991 list_size++;
Gilles Peskine449bd832023-01-11 14:50:10 +01002992 } else {
Johan Pascal76fdf1d2020-10-22 23:31:00 +02002993 /* unsupported value, stop parsing and set the size to an error value */
2994 list_size = MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH + 1;
Johan Pascalb62bb512015-12-03 21:56:45 +01002995 }
2996 }
2997
Gilles Peskine449bd832023-01-11 14:50:10 +01002998 if (list_size > MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH) {
2999 conf->dtls_srtp_profile_list = NULL;
3000 conf->dtls_srtp_profile_list_len = 0;
3001 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Johan Pascal253d0262020-09-22 13:04:45 +02003002 }
3003
Johan Pascal9bc97ca2020-09-21 23:44:45 +02003004 conf->dtls_srtp_profile_list = profiles;
Johan Pascal253d0262020-09-22 13:04:45 +02003005 conf->dtls_srtp_profile_list_len = list_size;
Johan Pascalb62bb512015-12-03 21:56:45 +01003006
Gilles Peskine449bd832023-01-11 14:50:10 +01003007 return 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01003008}
3009
Gilles Peskine449bd832023-01-11 14:50:10 +01003010void mbedtls_ssl_get_dtls_srtp_negotiation_result(const mbedtls_ssl_context *ssl,
3011 mbedtls_dtls_srtp_info *dtls_srtp_info)
Johan Pascalb62bb512015-12-03 21:56:45 +01003012{
Johan Pascal2258a4f2020-10-28 13:53:09 +01003013 dtls_srtp_info->chosen_dtls_srtp_profile = ssl->dtls_srtp_info.chosen_dtls_srtp_profile;
3014 /* do not copy the mki value if there is no chosen profile */
Gilles Peskine449bd832023-01-11 14:50:10 +01003015 if (dtls_srtp_info->chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET) {
Johan Pascal2258a4f2020-10-28 13:53:09 +01003016 dtls_srtp_info->mki_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003017 } else {
Johan Pascal2258a4f2020-10-28 13:53:09 +01003018 dtls_srtp_info->mki_len = ssl->dtls_srtp_info.mki_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01003019 memcpy(dtls_srtp_info->mki_value, ssl->dtls_srtp_info.mki_value,
3020 ssl->dtls_srtp_info.mki_len);
Johan Pascal2258a4f2020-10-28 13:53:09 +01003021 }
Johan Pascalb62bb512015-12-03 21:56:45 +01003022}
Johan Pascalb62bb512015-12-03 21:56:45 +01003023#endif /* MBEDTLS_SSL_DTLS_SRTP */
3024
Aditya Patwardhan3096f332022-07-26 14:31:46 +05303025#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003026void mbedtls_ssl_conf_max_version(mbedtls_ssl_config *conf, int major, int minor)
Paul Bakker490ecc82011-10-06 13:04:09 +00003027{
Glenn Strauss2dfcea22022-03-14 17:26:42 -04003028 conf->max_tls_version = (major << 8) | minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00003029}
3030
Gilles Peskine449bd832023-01-11 14:50:10 +01003031void mbedtls_ssl_conf_min_version(mbedtls_ssl_config *conf, int major, int minor)
Paul Bakker1d29fb52012-09-28 13:28:45 +00003032{
Glenn Strauss2dfcea22022-03-14 17:26:42 -04003033 conf->min_tls_version = (major << 8) | minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00003034}
Aditya Patwardhan3096f332022-07-26 14:31:46 +05303035#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker1d29fb52012-09-28 13:28:45 +00003036
Janos Follath088ce432017-04-10 12:42:31 +01003037#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003038void mbedtls_ssl_conf_cert_req_ca_list(mbedtls_ssl_config *conf,
3039 char cert_req_ca_list)
Janos Follath088ce432017-04-10 12:42:31 +01003040{
3041 conf->cert_req_ca_list = cert_req_ca_list;
3042}
3043#endif
3044
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003045#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01003046void mbedtls_ssl_conf_encrypt_then_mac(mbedtls_ssl_config *conf, char etm)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01003047{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003048 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01003049}
3050#endif
3051
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003052#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Gilles Peskine449bd832023-01-11 14:50:10 +01003053void mbedtls_ssl_conf_extended_master_secret(mbedtls_ssl_config *conf, char ems)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02003054{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003055 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02003056}
3057#endif
3058
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003059#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01003060int mbedtls_ssl_conf_max_frag_len(mbedtls_ssl_config *conf, unsigned char mfl_code)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003061{
Gilles Peskine449bd832023-01-11 14:50:10 +01003062 if (mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
3063 ssl_mfl_code_to_length(mfl_code) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN) {
3064 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003065 }
3066
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01003067 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003068
Gilles Peskine449bd832023-01-11 14:50:10 +01003069 return 0;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003070}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003071#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003072
Gilles Peskine449bd832023-01-11 14:50:10 +01003073void mbedtls_ssl_conf_legacy_renegotiation(mbedtls_ssl_config *conf, int allow_legacy)
Paul Bakker48916f92012-09-16 19:57:18 +00003074{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003075 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00003076}
3077
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003078#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01003079void mbedtls_ssl_conf_renegotiation(mbedtls_ssl_config *conf, int renegotiation)
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003080{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003081 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003082}
3083
Gilles Peskine449bd832023-01-11 14:50:10 +01003084void mbedtls_ssl_conf_renegotiation_enforced(mbedtls_ssl_config *conf, int max_records)
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003085{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003086 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003087}
3088
Gilles Peskine449bd832023-01-11 14:50:10 +01003089void mbedtls_ssl_conf_renegotiation_period(mbedtls_ssl_config *conf,
3090 const unsigned char period[8])
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01003091{
Gilles Peskine449bd832023-01-11 14:50:10 +01003092 memcpy(conf->renego_period, period, 8);
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01003093}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003094#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00003095
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003096#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003097#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003098void mbedtls_ssl_conf_session_tickets(mbedtls_ssl_config *conf, int use_tickets)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003099{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01003100 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003101}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003102#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02003103
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003104#if defined(MBEDTLS_SSL_SRV_C)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08003105
Jerry Yud0766ec2022-09-22 10:46:57 +08003106#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003107void mbedtls_ssl_conf_new_session_tickets(mbedtls_ssl_config *conf,
3108 uint16_t num_tickets)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08003109{
Jerry Yud0766ec2022-09-22 10:46:57 +08003110 conf->new_session_tickets_count = num_tickets;
Jerry Yu1ad7ace2022-08-09 13:28:39 +08003111}
3112#endif
3113
Gilles Peskine449bd832023-01-11 14:50:10 +01003114void mbedtls_ssl_conf_session_tickets_cb(mbedtls_ssl_config *conf,
3115 mbedtls_ssl_ticket_write_t *f_ticket_write,
3116 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
3117 void *p_ticket)
Paul Bakker606b4ba2013-08-14 16:52:14 +02003118{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02003119 conf->f_ticket_write = f_ticket_write;
3120 conf->f_ticket_parse = f_ticket_parse;
3121 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02003122}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003123#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003124#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003125
Gilles Peskine449bd832023-01-11 14:50:10 +01003126void mbedtls_ssl_set_export_keys_cb(mbedtls_ssl_context *ssl,
3127 mbedtls_ssl_export_keys_t *f_export_keys,
3128 void *p_export_keys)
Robert Cragie4feb7ae2015-10-02 13:33:37 +01003129{
Hanno Becker7e6c1782021-06-08 09:24:55 +01003130 ssl->f_export_keys = f_export_keys;
3131 ssl->p_export_keys = p_export_keys;
Ron Eldorf5cc10d2019-05-07 18:33:40 +03003132}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01003133
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003134#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003135void mbedtls_ssl_conf_async_private_cb(
3136 mbedtls_ssl_config *conf,
3137 mbedtls_ssl_async_sign_t *f_async_sign,
3138 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
3139 mbedtls_ssl_async_resume_t *f_async_resume,
3140 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskine449bd832023-01-11 14:50:10 +01003141 void *async_config_data)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003142{
3143 conf->f_async_sign_start = f_async_sign;
3144 conf->f_async_decrypt_start = f_async_decrypt;
3145 conf->f_async_resume = f_async_resume;
3146 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003147 conf->p_async_config_data = async_config_data;
3148}
3149
Gilles Peskine449bd832023-01-11 14:50:10 +01003150void *mbedtls_ssl_conf_get_async_config_data(const mbedtls_ssl_config *conf)
Gilles Peskine8f97af72018-04-26 11:46:10 +02003151{
Gilles Peskine449bd832023-01-11 14:50:10 +01003152 return conf->p_async_config_data;
Gilles Peskine8f97af72018-04-26 11:46:10 +02003153}
3154
Gilles Peskine449bd832023-01-11 14:50:10 +01003155void *mbedtls_ssl_get_async_operation_data(const mbedtls_ssl_context *ssl)
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003156{
Gilles Peskine449bd832023-01-11 14:50:10 +01003157 if (ssl->handshake == NULL) {
3158 return NULL;
3159 } else {
3160 return ssl->handshake->user_async_ctx;
3161 }
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003162}
3163
Gilles Peskine449bd832023-01-11 14:50:10 +01003164void mbedtls_ssl_set_async_operation_data(mbedtls_ssl_context *ssl,
3165 void *ctx)
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003166{
Gilles Peskine449bd832023-01-11 14:50:10 +01003167 if (ssl->handshake != NULL) {
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003168 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +01003169 }
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003170}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003171#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003172
Paul Bakker5121ce52009-01-03 21:22:43 +00003173/*
3174 * SSL get accessors
3175 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003176uint32_t mbedtls_ssl_get_verify_result(const mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00003177{
Gilles Peskine449bd832023-01-11 14:50:10 +01003178 if (ssl->session != NULL) {
3179 return ssl->session->verify_result;
3180 }
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00003181
Gilles Peskine449bd832023-01-11 14:50:10 +01003182 if (ssl->session_negotiate != NULL) {
3183 return ssl->session_negotiate->verify_result;
3184 }
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00003185
Gilles Peskine449bd832023-01-11 14:50:10 +01003186 return 0xFFFFFFFF;
Paul Bakker5121ce52009-01-03 21:22:43 +00003187}
3188
Gilles Peskine449bd832023-01-11 14:50:10 +01003189int mbedtls_ssl_get_ciphersuite_id_from_ssl(const mbedtls_ssl_context *ssl)
Glenn Strauss8f526902022-01-13 00:04:49 -05003190{
Gilles Peskine449bd832023-01-11 14:50:10 +01003191 if (ssl == NULL || ssl->session == NULL) {
3192 return 0;
3193 }
Glenn Strauss8f526902022-01-13 00:04:49 -05003194
Gilles Peskine449bd832023-01-11 14:50:10 +01003195 return ssl->session->ciphersuite;
Glenn Strauss8f526902022-01-13 00:04:49 -05003196}
3197
Gilles Peskine449bd832023-01-11 14:50:10 +01003198const char *mbedtls_ssl_get_ciphersuite(const mbedtls_ssl_context *ssl)
Paul Bakker72f62662011-01-16 21:27:44 +00003199{
Gilles Peskine449bd832023-01-11 14:50:10 +01003200 if (ssl == NULL || ssl->session == NULL) {
3201 return NULL;
3202 }
Paul Bakker926c8e42013-03-06 10:23:34 +01003203
Gilles Peskine449bd832023-01-11 14:50:10 +01003204 return mbedtls_ssl_get_ciphersuite_name(ssl->session->ciphersuite);
Paul Bakker72f62662011-01-16 21:27:44 +00003205}
3206
Gilles Peskine449bd832023-01-11 14:50:10 +01003207const char *mbedtls_ssl_get_version(const mbedtls_ssl_context *ssl)
Paul Bakker43ca69c2011-01-15 17:35:19 +00003208{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003209#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003210 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
3211 switch (ssl->tls_version) {
Glenn Strauss60bfe602022-03-14 19:04:24 -04003212 case MBEDTLS_SSL_VERSION_TLS1_2:
Gilles Peskine449bd832023-01-11 14:50:10 +01003213 return "DTLSv1.2";
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003214 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01003215 return "unknown (DTLS)";
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003216 }
3217 }
3218#endif
3219
Gilles Peskine449bd832023-01-11 14:50:10 +01003220 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 "TLSv1.2";
Glenn Strauss60bfe602022-03-14 19:04:24 -04003223 case MBEDTLS_SSL_VERSION_TLS1_3:
Gilles Peskine449bd832023-01-11 14:50:10 +01003224 return "TLSv1.3";
Paul Bakker43ca69c2011-01-15 17:35:19 +00003225 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01003226 return "unknown";
Paul Bakker43ca69c2011-01-15 17:35:19 +00003227 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00003228}
3229
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003230#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01003231size_t mbedtls_ssl_get_input_max_frag_len(const mbedtls_ssl_context *ssl)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003232{
David Horstmann95d516f2021-05-04 18:36:56 +01003233 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003234 size_t read_mfl;
3235
Jerry Yuddda0502022-12-01 19:43:12 +08003236#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003237 /* Use the configured MFL for the client if we're past SERVER_HELLO_DONE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003238 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
3239 ssl->state >= MBEDTLS_SSL_SERVER_HELLO_DONE) {
3240 return ssl_mfl_code_to_length(ssl->conf->mfl_code);
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003241 }
Jerry Yuddda0502022-12-01 19:43:12 +08003242#endif
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003243
3244 /* Check if a smaller max length was negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01003245 if (ssl->session_out != NULL) {
3246 read_mfl = ssl_mfl_code_to_length(ssl->session_out->mfl_code);
3247 if (read_mfl < max_len) {
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003248 max_len = read_mfl;
3249 }
3250 }
3251
Jerry Yuddda0502022-12-01 19:43:12 +08003252 /* During a handshake, use the value being negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01003253 if (ssl->session_negotiate != NULL) {
3254 read_mfl = ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code);
3255 if (read_mfl < max_len) {
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003256 max_len = read_mfl;
3257 }
3258 }
3259
Gilles Peskine449bd832023-01-11 14:50:10 +01003260 return max_len;
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003261}
3262
Gilles Peskine449bd832023-01-11 14:50:10 +01003263size_t mbedtls_ssl_get_output_max_frag_len(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003264{
3265 size_t max_len;
3266
3267 /*
3268 * Assume mfl_code is correct since it was checked when set
3269 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003270 max_len = ssl_mfl_code_to_length(ssl->conf->mfl_code);
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003271
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02003272 /* Check if a smaller max length was negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01003273 if (ssl->session_out != NULL &&
3274 ssl_mfl_code_to_length(ssl->session_out->mfl_code) < max_len) {
3275 max_len = ssl_mfl_code_to_length(ssl->session_out->mfl_code);
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003276 }
3277
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02003278 /* During a handshake, use the value being negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01003279 if (ssl->session_negotiate != NULL &&
3280 ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code) < max_len) {
3281 max_len = ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code);
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02003282 }
3283
Gilles Peskine449bd832023-01-11 14:50:10 +01003284 return max_len;
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003285}
3286#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
3287
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003288#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003289size_t mbedtls_ssl_get_current_mtu(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003290{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04003291 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003292 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
3293 (ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
3294 ssl->state == MBEDTLS_SSL_SERVER_HELLO)) {
3295 return 0;
3296 }
Andrzej Kurekef43ce62018-10-09 08:24:12 -04003297
Gilles Peskine449bd832023-01-11 14:50:10 +01003298 if (ssl->handshake == NULL || ssl->handshake->mtu == 0) {
3299 return ssl->mtu;
3300 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003301
Gilles Peskine449bd832023-01-11 14:50:10 +01003302 if (ssl->mtu == 0) {
3303 return ssl->handshake->mtu;
3304 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003305
Gilles Peskine449bd832023-01-11 14:50:10 +01003306 return ssl->mtu < ssl->handshake->mtu ?
3307 ssl->mtu : ssl->handshake->mtu;
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003308}
3309#endif /* MBEDTLS_SSL_PROTO_DTLS */
3310
Gilles Peskine449bd832023-01-11 14:50:10 +01003311int mbedtls_ssl_get_max_out_record_payload(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003312{
3313 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
3314
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02003315#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
3316 !defined(MBEDTLS_SSL_PROTO_DTLS)
3317 (void) ssl;
3318#endif
3319
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003320#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01003321 const size_t mfl = mbedtls_ssl_get_output_max_frag_len(ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003322
Gilles Peskine449bd832023-01-11 14:50:10 +01003323 if (max_len > mfl) {
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003324 max_len = mfl;
Gilles Peskine449bd832023-01-11 14:50:10 +01003325 }
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003326#endif
3327
3328#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003329 if (mbedtls_ssl_get_current_mtu(ssl) != 0) {
3330 const size_t mtu = mbedtls_ssl_get_current_mtu(ssl);
3331 const int ret = mbedtls_ssl_get_record_expansion(ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003332 const size_t overhead = (size_t) ret;
3333
Gilles Peskine449bd832023-01-11 14:50:10 +01003334 if (ret < 0) {
3335 return ret;
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003336 }
3337
Gilles Peskine449bd832023-01-11 14:50:10 +01003338 if (mtu <= overhead) {
3339 MBEDTLS_SSL_DEBUG_MSG(1, ("MTU too low for record expansion"));
3340 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
3341 }
3342
3343 if (max_len > mtu - overhead) {
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003344 max_len = mtu - overhead;
Gilles Peskine449bd832023-01-11 14:50:10 +01003345 }
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003346 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003347#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003348
Hanno Becker0defedb2018-08-10 12:35:02 +01003349#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
3350 !defined(MBEDTLS_SSL_PROTO_DTLS)
3351 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003352#endif
3353
Gilles Peskine449bd832023-01-11 14:50:10 +01003354 return (int) max_len;
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003355}
3356
Gilles Peskine449bd832023-01-11 14:50:10 +01003357int mbedtls_ssl_get_max_in_record_payload(const mbedtls_ssl_context *ssl)
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003358{
3359 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
3360
3361#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
3362 (void) ssl;
3363#endif
3364
3365#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01003366 const size_t mfl = mbedtls_ssl_get_input_max_frag_len(ssl);
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003367
Gilles Peskine449bd832023-01-11 14:50:10 +01003368 if (max_len > mfl) {
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003369 max_len = mfl;
Gilles Peskine449bd832023-01-11 14:50:10 +01003370 }
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003371#endif
3372
Gilles Peskine449bd832023-01-11 14:50:10 +01003373 return (int) max_len;
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003374}
3375
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003376#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003377const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert(const mbedtls_ssl_context *ssl)
Paul Bakkerb0550d92012-10-30 07:51:03 +00003378{
Gilles Peskine449bd832023-01-11 14:50:10 +01003379 if (ssl == NULL || ssl->session == NULL) {
3380 return NULL;
3381 }
Paul Bakkerb0550d92012-10-30 07:51:03 +00003382
Hanno Beckere6824572019-02-07 13:18:46 +00003383#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01003384 return ssl->session->peer_cert;
Hanno Beckere6824572019-02-07 13:18:46 +00003385#else
Gilles Peskine449bd832023-01-11 14:50:10 +01003386 return NULL;
Hanno Beckere6824572019-02-07 13:18:46 +00003387#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00003388}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003389#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00003390
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003391#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003392int mbedtls_ssl_get_session(const mbedtls_ssl_context *ssl,
3393 mbedtls_ssl_session *dst)
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003394{
Hanno Beckere810bbc2021-05-14 16:01:05 +01003395 int ret;
3396
Gilles Peskine449bd832023-01-11 14:50:10 +01003397 if (ssl == NULL ||
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003398 dst == NULL ||
3399 ssl->session == NULL ||
Gilles Peskine449bd832023-01-11 14:50:10 +01003400 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT) {
3401 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003402 }
3403
Hanno Beckere810bbc2021-05-14 16:01:05 +01003404 /* Since Mbed TLS 3.0, mbedtls_ssl_get_session() is no longer
3405 * idempotent: Each session can only be exported once.
3406 *
3407 * (This is in preparation for TLS 1.3 support where we will
3408 * need the ability to export multiple sessions (aka tickets),
3409 * which will be achieved by calling mbedtls_ssl_get_session()
3410 * multiple times until it fails.)
3411 *
3412 * Check whether we have already exported the current session,
3413 * and fail if so.
3414 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003415 if (ssl->session->exported == 1) {
3416 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
3417 }
Hanno Beckere810bbc2021-05-14 16:01:05 +01003418
Gilles Peskine449bd832023-01-11 14:50:10 +01003419 ret = mbedtls_ssl_session_copy(dst, ssl->session);
3420 if (ret != 0) {
3421 return ret;
3422 }
Hanno Beckere810bbc2021-05-14 16:01:05 +01003423
3424 /* Remember that we've exported the session. */
3425 ssl->session->exported = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003426 return 0;
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003427}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003428#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003429
Paul Bakker5121ce52009-01-03 21:22:43 +00003430/*
Hanno Beckera835da52019-05-16 12:39:07 +01003431 * Define ticket header determining Mbed TLS version
3432 * and structure of the ticket.
3433 */
3434
Hanno Becker94ef3b32019-05-16 12:50:45 +01003435/*
Hanno Becker50b59662019-05-28 14:30:45 +01003436 * Define bitflag determining compile-time settings influencing
3437 * structure of serialized SSL sessions.
Hanno Becker94ef3b32019-05-16 12:50:45 +01003438 */
3439
Hanno Becker50b59662019-05-28 14:30:45 +01003440#if defined(MBEDTLS_HAVE_TIME)
Hanno Becker3e088662019-05-29 11:10:18 +01003441#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker50b59662019-05-28 14:30:45 +01003442#else
Hanno Becker3e088662019-05-29 11:10:18 +01003443#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003444#endif /* MBEDTLS_HAVE_TIME */
3445
3446#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker3e088662019-05-29 11:10:18 +01003447#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003448#else
Hanno Becker3e088662019-05-29 11:10:18 +01003449#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003450#endif /* MBEDTLS_X509_CRT_PARSE_C */
3451
3452#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Becker3e088662019-05-29 11:10:18 +01003453#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003454#else
Hanno Becker3e088662019-05-29 11:10:18 +01003455#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003456#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
3457
3458#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Becker3e088662019-05-29 11:10:18 +01003459#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003460#else
Hanno Becker3e088662019-05-29 11:10:18 +01003461#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003462#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
3463
Hanno Becker94ef3b32019-05-16 12:50:45 +01003464#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3e088662019-05-29 11:10:18 +01003465#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003466#else
Hanno Becker3e088662019-05-29 11:10:18 +01003467#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003468#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
3469
Hanno Becker94ef3b32019-05-16 12:50:45 +01003470#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3471#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
3472#else
3473#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
3474#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3475
Hanno Becker3e088662019-05-29 11:10:18 +01003476#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
3477#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
3478#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
3479#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
Hanno Becker37bdbe62021-08-01 05:38:58 +01003480#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 4
3481#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 5
Hanno Becker3e088662019-05-29 11:10:18 +01003482
Hanno Becker50b59662019-05-28 14:30:45 +01003483#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Gilles Peskine449bd832023-01-11 14:50:10 +01003484 ((uint16_t) ( \
3485 (SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT) | \
3486 (SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT) | \
3487 (SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << \
3488 SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT) | \
3489 (SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT) | \
3490 (SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT) | \
3491 (SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT)))
Hanno Becker94ef3b32019-05-16 12:50:45 +01003492
Hanno Beckerf8787072019-05-16 12:41:07 +01003493static unsigned char ssl_serialized_session_header[] = {
Hanno Becker94ef3b32019-05-16 12:50:45 +01003494 MBEDTLS_VERSION_MAJOR,
3495 MBEDTLS_VERSION_MINOR,
3496 MBEDTLS_VERSION_PATCH,
Gilles Peskine449bd832023-01-11 14:50:10 +01003497 MBEDTLS_BYTE_1(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
3498 MBEDTLS_BYTE_0(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
Hanno Beckerf8787072019-05-16 12:41:07 +01003499};
Hanno Beckera835da52019-05-16 12:39:07 +01003500
3501/*
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003502 * Serialize a session in the following format:
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02003503 * (in the presentation language of TLS, RFC 8446 section 3)
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003504 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003505 * struct {
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01003506 *
Hanno Beckerdce50972021-08-01 05:39:23 +01003507 * opaque mbedtls_version[3]; // library version: major, minor, patch
3508 * opaque session_format[2]; // library-version specific 16-bit field
3509 * // determining the format of the remaining
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003510 * // serialized data.
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01003511 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003512 * Note: When updating the format, remember to keep
3513 * these version+format bytes.
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003514 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003515 * // In this version, `session_format` determines
3516 * // the setting of those compile-time
3517 * // configuration options which influence
3518 * // the structure of mbedtls_ssl_session.
3519 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003520 * uint8_t minor_ver; // Protocol minor version. Possible values:
Jerry Yu0c2a7382022-12-06 13:27:25 +08003521 * // - TLS 1.2 (0x0303)
3522 * // - TLS 1.3 (0x0304)
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003523 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003524 * select (serialized_session.tls_version) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003525 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003526 * case MBEDTLS_SSL_VERSION_TLS1_2:
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003527 * serialized_session_tls12 data;
Jerry Yu0c2a7382022-12-06 13:27:25 +08003528 * case MBEDTLS_SSL_VERSION_TLS1_3:
Jerry Yuddda0502022-12-01 19:43:12 +08003529 * serialized_session_tls13 data;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003530 *
3531 * };
3532 *
3533 * } serialized_session;
3534 *
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003535 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003536
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003537MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003538static int ssl_session_save(const mbedtls_ssl_session *session,
3539 unsigned char omit_header,
3540 unsigned char *buf,
3541 size_t buf_len,
3542 size_t *olen)
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003543{
3544 unsigned char *p = buf;
3545 size_t used = 0;
Jerry Yu251a12e2022-07-13 15:15:48 +08003546 size_t remaining_len;
Jerry Yue36fdd62022-08-17 21:31:36 +08003547#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3548 size_t out_len;
3549 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3550#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01003551 if (session == NULL) {
3552 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
3553 }
Jerry Yu438ddd82022-07-07 06:55:50 +00003554
Gilles Peskine449bd832023-01-11 14:50:10 +01003555 if (!omit_header) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003556 /*
3557 * Add Mbed TLS version identifier
3558 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003559 used += sizeof(ssl_serialized_session_header);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003560
Gilles Peskine449bd832023-01-11 14:50:10 +01003561 if (used <= buf_len) {
3562 memcpy(p, ssl_serialized_session_header,
3563 sizeof(ssl_serialized_session_header));
3564 p += sizeof(ssl_serialized_session_header);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003565 }
3566 }
3567
3568 /*
3569 * TLS version identifier
3570 */
3571 used += 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003572 if (used <= buf_len) {
3573 *p++ = MBEDTLS_BYTE_0(session->tls_version);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003574 }
3575
3576 /* Forward to version-specific serialization routine. */
Jerry Yufca4d572022-07-21 10:37:48 +08003577 remaining_len = (buf_len >= used) ? buf_len - used : 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003578 switch (session->tls_version) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003579#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01003580 case MBEDTLS_SSL_VERSION_TLS1_2:
3581 used += ssl_tls12_session_save(session, p, remaining_len);
3582 break;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003583#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3584
Jerry Yu251a12e2022-07-13 15:15:48 +08003585#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01003586 case MBEDTLS_SSL_VERSION_TLS1_3:
3587 ret = ssl_tls13_session_save(session, p, remaining_len, &out_len);
3588 if (ret != 0 && ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL) {
3589 return ret;
3590 }
3591 used += out_len;
3592 break;
Jerry Yu251a12e2022-07-13 15:15:48 +08003593#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3594
Gilles Peskine449bd832023-01-11 14:50:10 +01003595 default:
3596 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003597 }
3598
3599 *olen = used;
Gilles Peskine449bd832023-01-11 14:50:10 +01003600 if (used > buf_len) {
3601 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
3602 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003603
Gilles Peskine449bd832023-01-11 14:50:10 +01003604 return 0;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003605}
3606
3607/*
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003608 * Public wrapper for ssl_session_save()
3609 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003610int mbedtls_ssl_session_save(const mbedtls_ssl_session *session,
3611 unsigned char *buf,
3612 size_t buf_len,
3613 size_t *olen)
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003614{
Gilles Peskine449bd832023-01-11 14:50:10 +01003615 return ssl_session_save(session, 0, buf, buf_len, olen);
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003616}
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003617
Jerry Yuf1b23ca2022-02-18 11:48:47 +08003618/*
3619 * Deserialize session, see mbedtls_ssl_session_save() for format.
3620 *
3621 * This internal version is wrapped by a public function that cleans up in
3622 * case of error, and has an extra option omit_header.
3623 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003624MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003625static int ssl_session_load(mbedtls_ssl_session *session,
3626 unsigned char omit_header,
3627 const unsigned char *buf,
3628 size_t len)
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003629{
3630 const unsigned char *p = buf;
3631 const unsigned char * const end = buf + len;
Jerry Yu438ddd82022-07-07 06:55:50 +00003632 size_t remaining_len;
3633
3634
Gilles Peskine449bd832023-01-11 14:50:10 +01003635 if (session == NULL) {
3636 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
3637 }
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003638
Gilles Peskine449bd832023-01-11 14:50:10 +01003639 if (!omit_header) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003640 /*
3641 * Check Mbed TLS version identifier
3642 */
3643
Gilles Peskine449bd832023-01-11 14:50:10 +01003644 if ((size_t) (end - p) < sizeof(ssl_serialized_session_header)) {
3645 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003646 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003647
3648 if (memcmp(p, ssl_serialized_session_header,
3649 sizeof(ssl_serialized_session_header)) != 0) {
3650 return MBEDTLS_ERR_SSL_VERSION_MISMATCH;
3651 }
3652 p += sizeof(ssl_serialized_session_header);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003653 }
3654
3655 /*
3656 * TLS version identifier
3657 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003658 if (1 > (size_t) (end - p)) {
3659 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3660 }
Glenn Straussda7851c2022-03-14 13:29:48 -04003661 session->tls_version = 0x0300 | *p++;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003662
3663 /* Dispatch according to TLS version. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003664 remaining_len = (end - p);
3665 switch (session->tls_version) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003666#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01003667 case MBEDTLS_SSL_VERSION_TLS1_2:
3668 return ssl_tls12_session_load(session, p, remaining_len);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003669#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3670
Jerry Yu438ddd82022-07-07 06:55:50 +00003671#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01003672 case MBEDTLS_SSL_VERSION_TLS1_3:
3673 return ssl_tls13_session_load(session, p, remaining_len);
Jerry Yu438ddd82022-07-07 06:55:50 +00003674#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3675
Gilles Peskine449bd832023-01-11 14:50:10 +01003676 default:
3677 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003678 }
3679}
3680
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003681/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02003682 * Deserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003683 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003684int mbedtls_ssl_session_load(mbedtls_ssl_session *session,
3685 const unsigned char *buf,
3686 size_t len)
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003687{
Gilles Peskine449bd832023-01-11 14:50:10 +01003688 int ret = ssl_session_load(session, 0, buf, len);
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003689
Gilles Peskine449bd832023-01-11 14:50:10 +01003690 if (ret != 0) {
3691 mbedtls_ssl_session_free(session);
3692 }
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003693
Gilles Peskine449bd832023-01-11 14:50:10 +01003694 return ret;
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003695}
3696
3697/*
Paul Bakker1961b702013-01-25 14:49:24 +01003698 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00003699 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003700MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003701static int ssl_prepare_handshake_step(mbedtls_ssl_context *ssl)
Hanno Becker41934dd2021-08-07 19:13:43 +01003702{
3703 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3704
Ronald Cron66dbf912022-02-02 15:33:46 +01003705 /*
3706 * We may have not been able to send to the peer all the handshake data
Ronald Cron3f20b772022-03-08 16:00:02 +01003707 * that were written into the output buffer by the previous handshake step,
3708 * if the write to the network callback returned with the
Ronald Cron66dbf912022-02-02 15:33:46 +01003709 * #MBEDTLS_ERR_SSL_WANT_WRITE error code.
3710 * We proceed to the next handshake step only when all data from the
3711 * previous one have been sent to the peer, thus we make sure that this is
3712 * the case here by calling `mbedtls_ssl_flush_output()`. The function may
3713 * return with the #MBEDTLS_ERR_SSL_WANT_WRITE error code in which case
3714 * we have to wait before to go ahead.
3715 * In the case of TLS 1.3, handshake step handlers do not send data to the
3716 * peer. Data are only sent here and through
3717 * `mbedtls_ssl_handle_pending_alert` in case an error that triggered an
Andrzej Kurek5c65c572022-04-13 14:28:52 -04003718 * alert occurred.
Ronald Cron66dbf912022-02-02 15:33:46 +01003719 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003720 if ((ret = mbedtls_ssl_flush_output(ssl)) != 0) {
3721 return ret;
3722 }
Hanno Becker41934dd2021-08-07 19:13:43 +01003723
3724#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003725 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3726 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING) {
3727 if ((ret = mbedtls_ssl_flight_transmit(ssl)) != 0) {
3728 return ret;
3729 }
Hanno Becker41934dd2021-08-07 19:13:43 +01003730 }
3731#endif /* MBEDTLS_SSL_PROTO_DTLS */
3732
Gilles Peskine449bd832023-01-11 14:50:10 +01003733 return ret;
Hanno Becker41934dd2021-08-07 19:13:43 +01003734}
3735
Gilles Peskine449bd832023-01-11 14:50:10 +01003736int mbedtls_ssl_handshake_step(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00003737{
Hanno Becker41934dd2021-08-07 19:13:43 +01003738 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003739
Gilles Peskine449bd832023-01-11 14:50:10 +01003740 if (ssl == NULL ||
Hanno Becker41934dd2021-08-07 19:13:43 +01003741 ssl->conf == NULL ||
3742 ssl->handshake == NULL ||
Gilles Peskine449bd832023-01-11 14:50:10 +01003743 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER) {
3744 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Becker41934dd2021-08-07 19:13:43 +01003745 }
3746
Gilles Peskine449bd832023-01-11 14:50:10 +01003747 ret = ssl_prepare_handshake_step(ssl);
3748 if (ret != 0) {
3749 return ret;
3750 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003751
Gilles Peskine449bd832023-01-11 14:50:10 +01003752 ret = mbedtls_ssl_handle_pending_alert(ssl);
3753 if (ret != 0) {
Jerry Yue7047812021-09-13 19:26:39 +08003754 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01003755 }
Jerry Yue7047812021-09-13 19:26:39 +08003756
Tom Cosgrove2fdc7b32022-09-21 12:33:17 +01003757 /* If ssl->conf->endpoint is not one of MBEDTLS_SSL_IS_CLIENT or
3758 * MBEDTLS_SSL_IS_SERVER, this is the return code we give */
3759 ret = MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3760
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003761#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003762 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
3763 MBEDTLS_SSL_DEBUG_MSG(2, ("client state: %s",
3764 mbedtls_ssl_states_str(ssl->state)));
Jerry Yub9930e72021-08-06 17:11:51 +08003765
Gilles Peskine449bd832023-01-11 14:50:10 +01003766 switch (ssl->state) {
Ronald Cron9f0fba32022-02-10 16:45:15 +01003767 case MBEDTLS_SSL_HELLO_REQUEST:
3768 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
Tom Cosgrove87d9c6c2022-09-22 09:27:56 +01003769 ret = 0;
Ronald Cron9f0fba32022-02-10 16:45:15 +01003770 break;
Jerry Yub9930e72021-08-06 17:11:51 +08003771
Ronald Cron9f0fba32022-02-10 16:45:15 +01003772 case MBEDTLS_SSL_CLIENT_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01003773 ret = mbedtls_ssl_write_client_hello(ssl);
Ronald Cron9f0fba32022-02-10 16:45:15 +01003774 break;
3775
3776 default:
3777#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01003778 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
3779 ret = mbedtls_ssl_tls13_handshake_client_step(ssl);
3780 } else {
3781 ret = mbedtls_ssl_handshake_client_step(ssl);
3782 }
Ronald Cron9f0fba32022-02-10 16:45:15 +01003783#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01003784 ret = mbedtls_ssl_handshake_client_step(ssl);
Ronald Cron9f0fba32022-02-10 16:45:15 +01003785#else
Gilles Peskine449bd832023-01-11 14:50:10 +01003786 ret = mbedtls_ssl_tls13_handshake_client_step(ssl);
Ronald Cron9f0fba32022-02-10 16:45:15 +01003787#endif
3788 }
Jerry Yub9930e72021-08-06 17:11:51 +08003789 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003790#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003791#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003792 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Ronald Cron6f135e12021-12-08 16:57:54 +01003793#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01003794 if (mbedtls_ssl_conf_is_tls13_only(ssl->conf)) {
3795 ret = mbedtls_ssl_tls13_handshake_server_step(ssl);
3796 }
Ronald Cron6f135e12021-12-08 16:57:54 +01003797#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yub9930e72021-08-06 17:11:51 +08003798
3799#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01003800 if (mbedtls_ssl_conf_is_tls12_only(ssl->conf)) {
3801 ret = mbedtls_ssl_handshake_server_step(ssl);
3802 }
Jerry Yub9930e72021-08-06 17:11:51 +08003803#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3804 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003805#endif
3806
Gilles Peskine449bd832023-01-11 14:50:10 +01003807 if (ret != 0) {
Jerry Yubbd5a3f2021-09-18 20:50:22 +08003808 /* handshake_step return error. And it is same
3809 * with alert_reason.
3810 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003811 if (ssl->send_alert) {
3812 ret = mbedtls_ssl_handle_pending_alert(ssl);
Jerry Yue7047812021-09-13 19:26:39 +08003813 goto cleanup;
3814 }
3815 }
3816
3817cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01003818 return ret;
Paul Bakker1961b702013-01-25 14:49:24 +01003819}
3820
3821/*
3822 * Perform the SSL handshake
3823 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003824int mbedtls_ssl_handshake(mbedtls_ssl_context *ssl)
Paul Bakker1961b702013-01-25 14:49:24 +01003825{
3826 int ret = 0;
3827
Hanno Beckera817ea42020-10-20 15:20:23 +01003828 /* Sanity checks */
3829
Gilles Peskine449bd832023-01-11 14:50:10 +01003830 if (ssl == NULL || ssl->conf == NULL) {
3831 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3832 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003833
Hanno Beckera817ea42020-10-20 15:20:23 +01003834#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003835 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3836 (ssl->f_set_timer == NULL || ssl->f_get_timer == NULL)) {
3837 MBEDTLS_SSL_DEBUG_MSG(1, ("You must use "
3838 "mbedtls_ssl_set_timer_cb() for DTLS"));
3839 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Beckera817ea42020-10-20 15:20:23 +01003840 }
3841#endif /* MBEDTLS_SSL_PROTO_DTLS */
3842
Gilles Peskine449bd832023-01-11 14:50:10 +01003843 MBEDTLS_SSL_DEBUG_MSG(2, ("=> handshake"));
Paul Bakker1961b702013-01-25 14:49:24 +01003844
Hanno Beckera817ea42020-10-20 15:20:23 +01003845 /* Main handshake loop */
Gilles Peskine449bd832023-01-11 14:50:10 +01003846 while (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER) {
3847 ret = mbedtls_ssl_handshake_step(ssl);
Paul Bakker1961b702013-01-25 14:49:24 +01003848
Gilles Peskine449bd832023-01-11 14:50:10 +01003849 if (ret != 0) {
Paul Bakker1961b702013-01-25 14:49:24 +01003850 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01003851 }
Paul Bakker1961b702013-01-25 14:49:24 +01003852 }
3853
Gilles Peskine449bd832023-01-11 14:50:10 +01003854 MBEDTLS_SSL_DEBUG_MSG(2, ("<= handshake"));
Paul Bakker5121ce52009-01-03 21:22:43 +00003855
Gilles Peskine449bd832023-01-11 14:50:10 +01003856 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003857}
3858
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003859#if defined(MBEDTLS_SSL_RENEGOTIATION)
3860#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003861/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003862 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00003863 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003864MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003865static int ssl_write_hello_request(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003866{
Janos Follath865b3eb2019-12-16 11:46:15 +00003867 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003868
Gilles Peskine449bd832023-01-11 14:50:10 +01003869 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write hello request"));
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003870
3871 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003872 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3873 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003874
Gilles Peskine449bd832023-01-11 14:50:10 +01003875 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
3876 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
3877 return ret;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003878 }
3879
Gilles Peskine449bd832023-01-11 14:50:10 +01003880 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write hello request"));
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003881
Gilles Peskine449bd832023-01-11 14:50:10 +01003882 return 0;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003883}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003884#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003885
3886/*
3887 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003888 * - any side: calling mbedtls_ssl_renegotiate(),
3889 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
3890 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02003891 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003892 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003893 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003894 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003895int mbedtls_ssl_start_renegotiation(mbedtls_ssl_context *ssl)
Paul Bakker48916f92012-09-16 19:57:18 +00003896{
Janos Follath865b3eb2019-12-16 11:46:15 +00003897 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker48916f92012-09-16 19:57:18 +00003898
Gilles Peskine449bd832023-01-11 14:50:10 +01003899 MBEDTLS_SSL_DEBUG_MSG(2, ("=> renegotiate"));
Paul Bakker48916f92012-09-16 19:57:18 +00003900
Gilles Peskine449bd832023-01-11 14:50:10 +01003901 if ((ret = ssl_handshake_init(ssl)) != 0) {
3902 return ret;
3903 }
Paul Bakker48916f92012-09-16 19:57:18 +00003904
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003905 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
3906 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003907#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003908 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3909 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING) {
3910 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003911 ssl->handshake->out_msg_seq = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003912 } else {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003913 ssl->handshake->in_msg_seq = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003914 }
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003915 }
3916#endif
3917
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003918 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
3919 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00003920
Gilles Peskine449bd832023-01-11 14:50:10 +01003921 if ((ret = mbedtls_ssl_handshake(ssl)) != 0) {
3922 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_handshake", ret);
3923 return ret;
Paul Bakker48916f92012-09-16 19:57:18 +00003924 }
3925
Gilles Peskine449bd832023-01-11 14:50:10 +01003926 MBEDTLS_SSL_DEBUG_MSG(2, ("<= renegotiate"));
Paul Bakker48916f92012-09-16 19:57:18 +00003927
Gilles Peskine449bd832023-01-11 14:50:10 +01003928 return 0;
Paul Bakker48916f92012-09-16 19:57:18 +00003929}
3930
3931/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003932 * Renegotiate current connection on client,
3933 * or request renegotiation on server
3934 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003935int mbedtls_ssl_renegotiate(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003936{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003937 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003938
Gilles Peskine449bd832023-01-11 14:50:10 +01003939 if (ssl == NULL || ssl->conf == NULL) {
3940 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3941 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003942
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003943#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003944 /* On server, just send the request */
Gilles Peskine449bd832023-01-11 14:50:10 +01003945 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
3946 if (mbedtls_ssl_is_handshake_over(ssl) == 0) {
3947 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3948 }
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003949
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003950 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02003951
3952 /* Did we already try/start sending HelloRequest? */
Gilles Peskine449bd832023-01-11 14:50:10 +01003953 if (ssl->out_left != 0) {
3954 return mbedtls_ssl_flush_output(ssl);
3955 }
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02003956
Gilles Peskine449bd832023-01-11 14:50:10 +01003957 return ssl_write_hello_request(ssl);
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003958 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003959#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003960
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003961#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003962 /*
3963 * On client, either start the renegotiation process or,
3964 * if already in progress, continue the handshake
3965 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003966 if (ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
3967 if (mbedtls_ssl_is_handshake_over(ssl) == 0) {
3968 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003969 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003970
3971 if ((ret = mbedtls_ssl_start_renegotiation(ssl)) != 0) {
3972 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_start_renegotiation", ret);
3973 return ret;
3974 }
3975 } else {
3976 if ((ret = mbedtls_ssl_handshake(ssl)) != 0) {
3977 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_handshake", ret);
3978 return ret;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003979 }
3980 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003981#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003982
Gilles Peskine449bd832023-01-11 14:50:10 +01003983 return ret;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003984}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003985#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003986
Gilles Peskine449bd832023-01-11 14:50:10 +01003987void mbedtls_ssl_handshake_free(mbedtls_ssl_context *ssl)
Paul Bakker48916f92012-09-16 19:57:18 +00003988{
Gilles Peskine9b562d52018-04-25 20:32:43 +02003989 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
3990
Gilles Peskine449bd832023-01-11 14:50:10 +01003991 if (handshake == NULL) {
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003992 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01003993 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003994
Brett Warrene0edc842021-08-17 09:53:13 +01003995#if defined(MBEDTLS_ECP_C)
3996#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003997 if (ssl->handshake->group_list_heap_allocated) {
3998 mbedtls_free((void *) handshake->group_list);
3999 }
Brett Warrene0edc842021-08-17 09:53:13 +01004000 handshake->group_list = NULL;
4001#endif /* MBEDTLS_DEPRECATED_REMOVED */
4002#endif /* MBEDTLS_ECP_C */
4003
Ronald Crone68ab4f2022-10-05 12:46:29 +02004004#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +08004005#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01004006 if (ssl->handshake->sig_algs_heap_allocated) {
4007 mbedtls_free((void *) handshake->sig_algs);
4008 }
Jerry Yuf017ee42022-01-12 15:49:48 +08004009 handshake->sig_algs = NULL;
4010#endif /* MBEDTLS_DEPRECATED_REMOVED */
Xiaofei Baic234ecf2022-02-08 09:59:23 +00004011#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01004012 if (ssl->handshake->certificate_request_context) {
4013 mbedtls_free((void *) handshake->certificate_request_context);
Xiaofei Baic234ecf2022-02-08 09:59:23 +00004014 }
4015#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Ronald Crone68ab4f2022-10-05 12:46:29 +02004016#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yuf017ee42022-01-12 15:49:48 +08004017
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02004018#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01004019 if (ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0) {
4020 ssl->conf->f_async_cancel(ssl);
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02004021 handshake->async_in_progress = 0;
4022 }
4023#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
4024
Andrzej Kurek25f27152022-08-17 16:09:31 -04004025#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -05004026#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01004027 psa_hash_abort(&handshake->fin_sha256_psa);
Andrzej Kurekeb342242019-01-29 09:14:33 -05004028#else
Gilles Peskine449bd832023-01-11 14:50:10 +01004029 mbedtls_sha256_free(&handshake->fin_sha256);
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02004030#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05004031#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004032#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -05004033#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01004034 psa_hash_abort(&handshake->fin_sha384_psa);
Andrzej Kurekeb342242019-01-29 09:14:33 -05004035#else
Gilles Peskine449bd832023-01-11 14:50:10 +01004036 mbedtls_sha512_free(&handshake->fin_sha384);
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02004037#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05004038#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02004039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004040#if defined(MBEDTLS_DHM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004041 mbedtls_dhm_free(&handshake->dhm_ctx);
Paul Bakker48916f92012-09-16 19:57:18 +00004042#endif
Neil Armstrongf3f46412022-04-12 14:43:39 +02004043#if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004044 mbedtls_ecdh_free(&handshake->ecdh_ctx);
Paul Bakker61d113b2013-07-04 11:51:43 +02004045#endif
Valerio Setti02c25b52022-11-15 14:08:42 +01004046
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02004047#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Neil Armstrongca7d5062022-05-31 14:43:23 +02004048#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01004049 psa_pake_abort(&handshake->psa_pake_ctx);
Valerio Settieb3f7882022-12-08 18:42:58 +01004050 /*
4051 * Opaque keys are not stored in the handshake's data and it's the user
4052 * responsibility to destroy them. Clear ones, instead, are created by
4053 * the TLS library and should be destroyed at the same level
4054 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004055 if (!mbedtls_svc_key_id_is_null(handshake->psa_pake_password)) {
4056 psa_destroy_key(handshake->psa_pake_password);
Valerio Settieb3f7882022-12-08 18:42:58 +01004057 }
Neil Armstrongca7d5062022-05-31 14:43:23 +02004058 handshake->psa_pake_password = MBEDTLS_SVC_KEY_ID_INIT;
4059#else
Gilles Peskine449bd832023-01-11 14:50:10 +01004060 mbedtls_ecjpake_free(&handshake->ecjpake_ctx);
Neil Armstrongca7d5062022-05-31 14:43:23 +02004061#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02004062#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004063 mbedtls_free(handshake->ecjpake_cache);
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02004064 handshake->ecjpake_cache = NULL;
4065 handshake->ecjpake_cache_len = 0;
4066#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02004067#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02004068
Janos Follath4ae5c292016-02-10 11:27:43 +00004069#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
4070 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02004071 /* explicit void pointer cast for buggy MS compiler */
Gilles Peskine449bd832023-01-11 14:50:10 +01004072 mbedtls_free((void *) handshake->curves_tls_id);
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004073#endif
4074
Ronald Cron73fe8df2022-10-05 14:31:43 +02004075#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02004076#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01004077 if (!mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque)) {
Neil Armstrong501c9322022-05-03 09:35:09 +02004078 /* The maintenance of the external PSK key slot is the
4079 * user's responsibility. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004080 if (ssl->handshake->psk_opaque_is_internal) {
4081 psa_destroy_key(ssl->handshake->psk_opaque);
Neil Armstrong501c9322022-05-03 09:35:09 +02004082 ssl->handshake->psk_opaque_is_internal = 0;
4083 }
4084 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
4085 }
Neil Armstronge952a302022-05-03 10:22:14 +02004086#else
Gilles Peskine449bd832023-01-11 14:50:10 +01004087 if (handshake->psk != NULL) {
4088 mbedtls_platform_zeroize(handshake->psk, handshake->psk_len);
4089 mbedtls_free(handshake->psk);
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004090 }
Neil Armstronge952a302022-05-03 10:22:14 +02004091#endif /* MBEDTLS_USE_PSA_CRYPTO */
Ronald Cron73fe8df2022-10-05 14:31:43 +02004092#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004093
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004094#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
4095 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02004096 /*
4097 * Free only the linked list wrapper, not the keys themselves
4098 * since the belong to the SNI callback
4099 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004100 ssl_key_cert_free(handshake->sni_key_cert);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004101#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004102
Gilles Peskineeccd8882020-03-10 12:19:08 +01004103#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01004104 mbedtls_x509_crt_restart_free(&handshake->ecrs_ctx);
4105 if (handshake->ecrs_peer_cert != NULL) {
4106 mbedtls_x509_crt_free(handshake->ecrs_peer_cert);
4107 mbedtls_free(handshake->ecrs_peer_cert);
Hanno Becker3dad3112019-02-05 17:19:52 +00004108 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02004109#endif
4110
Hanno Becker75173122019-02-06 16:18:31 +00004111#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
4112 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01004113 mbedtls_pk_free(&handshake->peer_pubkey);
Hanno Becker75173122019-02-06 16:18:31 +00004114#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
4115
XiaokangQian9b93c0d2022-02-09 06:02:25 +00004116#if defined(MBEDTLS_SSL_CLI_C) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01004117 (defined(MBEDTLS_SSL_PROTO_DTLS) || defined(MBEDTLS_SSL_PROTO_TLS1_3))
4118 mbedtls_free(handshake->cookie);
XiaokangQian9b93c0d2022-02-09 06:02:25 +00004119#endif /* MBEDTLS_SSL_CLI_C &&
4120 ( MBEDTLS_SSL_PROTO_DTLS || MBEDTLS_SSL_PROTO_TLS1_3 ) */
XiaokangQian8499b6c2022-01-27 09:00:11 +00004121
4122#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004123 mbedtls_ssl_flight_free(handshake->flight);
4124 mbedtls_ssl_buffering_free(ssl);
XiaokangQian8499b6c2022-01-27 09:00:11 +00004125#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02004126
Ronald Cronf12b81d2022-03-15 10:42:41 +01004127#if defined(MBEDTLS_ECDH_C) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01004128 (defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3))
4129 if (handshake->ecdh_psa_privkey_is_external == 0) {
4130 psa_destroy_key(handshake->ecdh_psa_privkey);
4131 }
Hanno Becker4a63ed42019-01-08 11:39:35 +00004132#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
4133
Ronald Cron6f135e12021-12-08 16:57:54 +01004134#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01004135 mbedtls_ssl_transform_free(handshake->transform_handshake);
4136 mbedtls_free(handshake->transform_handshake);
Jerry Yu3d9b5902022-11-04 14:07:25 +08004137#if defined(MBEDTLS_SSL_EARLY_DATA)
Gilles Peskine449bd832023-01-11 14:50:10 +01004138 mbedtls_ssl_transform_free(handshake->transform_earlydata);
4139 mbedtls_free(handshake->transform_earlydata);
Jerry Yu3d9b5902022-11-04 14:07:25 +08004140#endif
Ronald Cron6f135e12021-12-08 16:57:54 +01004141#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yuba9c7272021-10-30 11:54:10 +08004142
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004143
4144#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4145 /* If the buffers are too big - reallocate. Because of the way Mbed TLS
4146 * processes datagrams and the fact that a datagram is allowed to have
4147 * several records in it, it is possible that the I/O buffers are not
4148 * empty at this stage */
Gilles Peskine449bd832023-01-11 14:50:10 +01004149 handle_buffer_resizing(ssl, 1, mbedtls_ssl_get_input_buflen(ssl),
4150 mbedtls_ssl_get_output_buflen(ssl));
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004151#endif
Hanno Becker3aa186f2021-08-10 09:24:19 +01004152
Jerry Yuba9c7272021-10-30 11:54:10 +08004153 /* mbedtls_platform_zeroize MUST be last one in this function */
Gilles Peskine449bd832023-01-11 14:50:10 +01004154 mbedtls_platform_zeroize(handshake,
4155 sizeof(mbedtls_ssl_handshake_params));
Paul Bakker48916f92012-09-16 19:57:18 +00004156}
4157
Gilles Peskine449bd832023-01-11 14:50:10 +01004158void mbedtls_ssl_session_free(mbedtls_ssl_session *session)
Paul Bakker48916f92012-09-16 19:57:18 +00004159{
Gilles Peskine449bd832023-01-11 14:50:10 +01004160 if (session == NULL) {
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004161 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01004162 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004163
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004164#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004165 ssl_clear_peer_cert(session);
Paul Bakkered27a042013-04-18 22:46:23 +02004166#endif
Paul Bakker0a597072012-09-25 21:55:46 +00004167
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02004168#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Xiaokang Qianbc663a02022-10-09 11:14:39 +00004169#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
4170 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01004171 mbedtls_free(session->hostname);
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00004172#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01004173 mbedtls_free(session->ticket);
Paul Bakkera503a632013-08-14 13:48:06 +02004174#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004175
Gilles Peskine449bd832023-01-11 14:50:10 +01004176 mbedtls_platform_zeroize(session, sizeof(mbedtls_ssl_session));
Paul Bakker48916f92012-09-16 19:57:18 +00004177}
4178
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02004179#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004180
4181#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4182#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u
4183#else
4184#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u
4185#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4186
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004187#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004188
4189#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4190#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u
4191#else
4192#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u
4193#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4194
4195#if defined(MBEDTLS_SSL_ALPN)
4196#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u
4197#else
4198#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u
4199#endif /* MBEDTLS_SSL_ALPN */
4200
4201#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0
4202#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1
4203#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2
4204#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3
4205
4206#define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \
Gilles Peskine449bd832023-01-11 14:50:10 +01004207 ((uint32_t) ( \
4208 (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << \
4209 SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT) | \
4210 (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << \
4211 SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT) | \
4212 (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << \
4213 SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT) | \
4214 (SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT) | \
4215 0u))
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004216
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004217static unsigned char ssl_serialized_context_header[] = {
4218 MBEDTLS_VERSION_MAJOR,
4219 MBEDTLS_VERSION_MINOR,
4220 MBEDTLS_VERSION_PATCH,
Gilles Peskine449bd832023-01-11 14:50:10 +01004221 MBEDTLS_BYTE_1(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
4222 MBEDTLS_BYTE_0(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
4223 MBEDTLS_BYTE_2(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG),
4224 MBEDTLS_BYTE_1(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG),
4225 MBEDTLS_BYTE_0(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG),
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004226};
4227
Paul Bakker5121ce52009-01-03 21:22:43 +00004228/*
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004229 * Serialize a full SSL context
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02004230 *
4231 * The format of the serialized data is:
4232 * (in the presentation language of TLS, RFC 8446 section 3)
4233 *
4234 * // header
4235 * opaque mbedtls_version[3]; // major, minor, patch
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004236 * opaque context_format[5]; // version-specific field determining
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02004237 * // the format of the remaining
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004238 * // serialized data.
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004239 * Note: When updating the format, remember to keep these
4240 * version+format bytes. (We may make their size part of the API.)
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02004241 *
4242 * // session sub-structure
4243 * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save()
4244 * // transform sub-structure
4245 * uint8 random[64]; // ServerHello.random+ClientHello.random
4246 * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value
4247 * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use
4248 * // fields from ssl_context
4249 * uint32 badmac_seen; // DTLS: number of records with failing MAC
4250 * uint64 in_window_top; // DTLS: last validated record seq_num
4251 * uint64 in_window; // DTLS: bitmask for replay protection
4252 * uint8 disable_datagram_packing; // DTLS: only one record per datagram
4253 * uint64 cur_out_ctr; // Record layer: outgoing sequence number
4254 * uint16 mtu; // DTLS: path mtu (max outgoing fragment size)
4255 * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol
4256 *
4257 * Note that many fields of the ssl_context or sub-structures are not
4258 * serialized, as they fall in one of the following categories:
4259 *
4260 * 1. forced value (eg in_left must be 0)
4261 * 2. pointer to dynamically-allocated memory (eg session, transform)
4262 * 3. value can be re-derived from other data (eg session keys from MS)
4263 * 4. value was temporary (eg content of input buffer)
4264 * 5. value will be provided by the user again (eg I/O callbacks and context)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004265 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004266int mbedtls_ssl_context_save(mbedtls_ssl_context *ssl,
4267 unsigned char *buf,
4268 size_t buf_len,
4269 size_t *olen)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004270{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004271 unsigned char *p = buf;
4272 size_t used = 0;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004273 size_t session_len;
4274 int ret = 0;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004275
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02004276 /*
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004277 * Enforce usage restrictions, see "return BAD_INPUT_DATA" in
4278 * this function's documentation.
4279 *
4280 * These are due to assumptions/limitations in the implementation. Some of
4281 * them are likely to stay (no handshake in progress) some might go away
4282 * (only DTLS) but are currently used to simplify the implementation.
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02004283 */
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004284 /* The initial handshake must be over */
Gilles Peskine449bd832023-01-11 14:50:10 +01004285 if (mbedtls_ssl_is_handshake_over(ssl) == 0) {
4286 MBEDTLS_SSL_DEBUG_MSG(1, ("Initial handshake isn't over"));
4287 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004288 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004289 if (ssl->handshake != NULL) {
4290 MBEDTLS_SSL_DEBUG_MSG(1, ("Handshake isn't completed"));
4291 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004292 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004293 /* Double-check that sub-structures are indeed ready */
Gilles Peskine449bd832023-01-11 14:50:10 +01004294 if (ssl->transform == NULL || ssl->session == NULL) {
4295 MBEDTLS_SSL_DEBUG_MSG(1, ("Serialised structures aren't ready"));
4296 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004297 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004298 /* There must be no pending incoming or outgoing data */
Gilles Peskine449bd832023-01-11 14:50:10 +01004299 if (mbedtls_ssl_check_pending(ssl) != 0) {
4300 MBEDTLS_SSL_DEBUG_MSG(1, ("There is pending incoming data"));
4301 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004302 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004303 if (ssl->out_left != 0) {
4304 MBEDTLS_SSL_DEBUG_MSG(1, ("There is pending outgoing data"));
4305 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004306 }
Dave Rodgman556e8a32022-12-06 16:31:25 +00004307 /* Protocol must be DTLS, not TLS */
Gilles Peskine449bd832023-01-11 14:50:10 +01004308 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
4309 MBEDTLS_SSL_DEBUG_MSG(1, ("Only DTLS is supported"));
4310 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004311 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004312 /* Version must be 1.2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004313 if (ssl->tls_version != MBEDTLS_SSL_VERSION_TLS1_2) {
4314 MBEDTLS_SSL_DEBUG_MSG(1, ("Only version 1.2 supported"));
4315 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004316 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004317 /* We must be using an AEAD ciphersuite */
Gilles Peskine449bd832023-01-11 14:50:10 +01004318 if (mbedtls_ssl_transform_uses_aead(ssl->transform) != 1) {
4319 MBEDTLS_SSL_DEBUG_MSG(1, ("Only AEAD ciphersuites supported"));
4320 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004321 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004322 /* Renegotiation must not be enabled */
4323#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01004324 if (ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED) {
4325 MBEDTLS_SSL_DEBUG_MSG(1, ("Renegotiation must not be enabled"));
4326 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004327 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004328#endif
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004329
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004330 /*
4331 * Version and format identifier
4332 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004333 used += sizeof(ssl_serialized_context_header);
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004334
Gilles Peskine449bd832023-01-11 14:50:10 +01004335 if (used <= buf_len) {
4336 memcpy(p, ssl_serialized_context_header,
4337 sizeof(ssl_serialized_context_header));
4338 p += sizeof(ssl_serialized_context_header);
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004339 }
4340
4341 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004342 * Session (length + data)
4343 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004344 ret = ssl_session_save(ssl->session, 1, NULL, 0, &session_len);
4345 if (ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL) {
4346 return ret;
4347 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004348
4349 used += 4 + session_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004350 if (used <= buf_len) {
4351 MBEDTLS_PUT_UINT32_BE(session_len, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004352 p += 4;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004353
Gilles Peskine449bd832023-01-11 14:50:10 +01004354 ret = ssl_session_save(ssl->session, 1,
4355 p, session_len, &session_len);
4356 if (ret != 0) {
4357 return ret;
4358 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004359
4360 p += session_len;
4361 }
4362
4363 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004364 * Transform
4365 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004366 used += sizeof(ssl->transform->randbytes);
4367 if (used <= buf_len) {
4368 memcpy(p, ssl->transform->randbytes,
4369 sizeof(ssl->transform->randbytes));
4370 p += sizeof(ssl->transform->randbytes);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004371 }
4372
4373#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4374 used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004375 if (used <= buf_len) {
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004376 *p++ = ssl->transform->in_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004377 memcpy(p, ssl->transform->in_cid, ssl->transform->in_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004378 p += ssl->transform->in_cid_len;
4379
4380 *p++ = ssl->transform->out_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004381 memcpy(p, ssl->transform->out_cid, ssl->transform->out_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004382 p += ssl->transform->out_cid_len;
4383 }
4384#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4385
4386 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004387 * Saved fields from top-level ssl_context structure
4388 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004389 used += 4;
Gilles Peskine449bd832023-01-11 14:50:10 +01004390 if (used <= buf_len) {
4391 MBEDTLS_PUT_UINT32_BE(ssl->badmac_seen, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004392 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004393 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004394
4395#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4396 used += 16;
Gilles Peskine449bd832023-01-11 14:50:10 +01004397 if (used <= buf_len) {
4398 MBEDTLS_PUT_UINT64_BE(ssl->in_window_top, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004399 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004400
Gilles Peskine449bd832023-01-11 14:50:10 +01004401 MBEDTLS_PUT_UINT64_BE(ssl->in_window, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004402 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004403 }
4404#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4405
4406#if defined(MBEDTLS_SSL_PROTO_DTLS)
4407 used += 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004408 if (used <= buf_len) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004409 *p++ = ssl->disable_datagram_packing;
4410 }
4411#endif /* MBEDTLS_SSL_PROTO_DTLS */
4412
Jerry Yuae0b2e22021-10-08 15:21:19 +08004413 used += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Gilles Peskine449bd832023-01-11 14:50:10 +01004414 if (used <= buf_len) {
4415 memcpy(p, ssl->cur_out_ctr, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN);
Jerry Yuae0b2e22021-10-08 15:21:19 +08004416 p += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004417 }
4418
4419#if defined(MBEDTLS_SSL_PROTO_DTLS)
4420 used += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01004421 if (used <= buf_len) {
4422 MBEDTLS_PUT_UINT16_BE(ssl->mtu, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004423 p += 2;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004424 }
4425#endif /* MBEDTLS_SSL_PROTO_DTLS */
4426
4427#if defined(MBEDTLS_SSL_ALPN)
4428 {
4429 const uint8_t alpn_len = ssl->alpn_chosen
Gilles Peskine449bd832023-01-11 14:50:10 +01004430 ? (uint8_t) strlen(ssl->alpn_chosen)
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004431 : 0;
4432
4433 used += 1 + alpn_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004434 if (used <= buf_len) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004435 *p++ = alpn_len;
4436
Gilles Peskine449bd832023-01-11 14:50:10 +01004437 if (ssl->alpn_chosen != NULL) {
4438 memcpy(p, ssl->alpn_chosen, alpn_len);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004439 p += alpn_len;
4440 }
4441 }
4442 }
4443#endif /* MBEDTLS_SSL_ALPN */
4444
4445 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004446 * Done
4447 */
4448 *olen = used;
4449
Gilles Peskine449bd832023-01-11 14:50:10 +01004450 if (used > buf_len) {
4451 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
4452 }
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004453
Gilles Peskine449bd832023-01-11 14:50:10 +01004454 MBEDTLS_SSL_DEBUG_BUF(4, "saved context", buf, used);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004455
Gilles Peskine449bd832023-01-11 14:50:10 +01004456 return mbedtls_ssl_session_reset_int(ssl, 0);
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004457}
4458
4459/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02004460 * Deserialize context, see mbedtls_ssl_context_save() for format.
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004461 *
4462 * This internal version is wrapped by a public function that cleans up in
4463 * case of error.
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004464 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004465MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01004466static int ssl_context_load(mbedtls_ssl_context *ssl,
4467 const unsigned char *buf,
4468 size_t len)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004469{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004470 const unsigned char *p = buf;
4471 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004472 size_t session_len;
Janos Follath865b3eb2019-12-16 11:46:15 +00004473 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004474#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek894edde2022-09-29 06:31:14 -04004475 tls_prf_fn prf_func = NULL;
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004476#endif
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004477
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004478 /*
4479 * The context should have been freshly setup or reset.
4480 * Give the user an error in case of obvious misuse.
Manuel Pégourié-Gonnard4ca930f2019-07-26 16:31:53 +02004481 * (Checking session is useful because it won't be NULL if we're
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004482 * renegotiating, or if the user mistakenly loaded a session first.)
4483 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004484 if (ssl->state != MBEDTLS_SSL_HELLO_REQUEST ||
4485 ssl->session != NULL) {
4486 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004487 }
4488
4489 /*
4490 * We can't check that the config matches the initial one, but we can at
4491 * least check it matches the requirements for serializing.
4492 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004493 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
Glenn Strauss2dfcea22022-03-14 17:26:42 -04004494 ssl->conf->max_tls_version < MBEDTLS_SSL_VERSION_TLS1_2 ||
4495 ssl->conf->min_tls_version > MBEDTLS_SSL_VERSION_TLS1_2 ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004496#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02004497 ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004498#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01004499 0) {
4500 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004501 }
4502
Gilles Peskine449bd832023-01-11 14:50:10 +01004503 MBEDTLS_SSL_DEBUG_BUF(4, "context to load", buf, len);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004504
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004505 /*
4506 * Check version identifier
4507 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004508 if ((size_t) (end - p) < sizeof(ssl_serialized_context_header)) {
4509 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004510 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004511
4512 if (memcmp(p, ssl_serialized_context_header,
4513 sizeof(ssl_serialized_context_header)) != 0) {
4514 return MBEDTLS_ERR_SSL_VERSION_MISMATCH;
4515 }
4516 p += sizeof(ssl_serialized_context_header);
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004517
4518 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004519 * Session
4520 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004521 if ((size_t) (end - p) < 4) {
4522 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4523 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004524
Gilles Peskine449bd832023-01-11 14:50:10 +01004525 session_len = ((size_t) p[0] << 24) |
4526 ((size_t) p[1] << 16) |
4527 ((size_t) p[2] << 8) |
4528 ((size_t) p[3]);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004529 p += 4;
4530
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004531 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00004532 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004533 ssl->session = ssl->session_negotiate;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004534 ssl->session_in = ssl->session;
4535 ssl->session_out = ssl->session;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004536 ssl->session_negotiate = NULL;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004537
Gilles Peskine449bd832023-01-11 14:50:10 +01004538 if ((size_t) (end - p) < session_len) {
4539 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4540 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004541
Gilles Peskine449bd832023-01-11 14:50:10 +01004542 ret = ssl_session_load(ssl->session, 1, p, session_len);
4543 if (ret != 0) {
4544 mbedtls_ssl_session_free(ssl->session);
4545 return ret;
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004546 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004547
4548 p += session_len;
4549
4550 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004551 * Transform
4552 */
4553
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004554 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00004555 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Jerry Yu2e199812022-12-01 18:57:19 +08004556#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004557 ssl->transform = ssl->transform_negotiate;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004558 ssl->transform_in = ssl->transform;
4559 ssl->transform_out = ssl->transform;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004560 ssl->transform_negotiate = NULL;
Jerry Yu2e199812022-12-01 18:57:19 +08004561#endif
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004562
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004563#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01004564 prf_func = ssl_tls12prf_from_cs(ssl->session->ciphersuite);
4565 if (prf_func == NULL) {
4566 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4567 }
Andrzej Kurek894edde2022-09-29 06:31:14 -04004568
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004569 /* Read random bytes and populate structure */
Gilles Peskine449bd832023-01-11 14:50:10 +01004570 if ((size_t) (end - p) < sizeof(ssl->transform->randbytes)) {
4571 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4572 }
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004573
Gilles Peskine449bd832023-01-11 14:50:10 +01004574 ret = ssl_tls12_populate_transform(ssl->transform,
4575 ssl->session->ciphersuite,
4576 ssl->session->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02004577#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004578 ssl->session->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02004579#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01004580 prf_func,
4581 p, /* currently pointing to randbytes */
4582 MBEDTLS_SSL_VERSION_TLS1_2, /* (D)TLS 1.2 is forced */
4583 ssl->conf->endpoint,
4584 ssl);
4585 if (ret != 0) {
4586 return ret;
4587 }
Jerry Yu840fbb22022-02-17 14:59:29 +08004588#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004589 p += sizeof(ssl->transform->randbytes);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004590
4591#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4592 /* Read connection IDs and store them */
Gilles Peskine449bd832023-01-11 14:50:10 +01004593 if ((size_t) (end - p) < 1) {
4594 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4595 }
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004596
4597 ssl->transform->in_cid_len = *p++;
4598
Gilles Peskine449bd832023-01-11 14:50:10 +01004599 if ((size_t) (end - p) < ssl->transform->in_cid_len + 1u) {
4600 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4601 }
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004602
Gilles Peskine449bd832023-01-11 14:50:10 +01004603 memcpy(ssl->transform->in_cid, p, ssl->transform->in_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004604 p += ssl->transform->in_cid_len;
4605
4606 ssl->transform->out_cid_len = *p++;
4607
Gilles Peskine449bd832023-01-11 14:50:10 +01004608 if ((size_t) (end - p) < ssl->transform->out_cid_len) {
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->out_cid, p, ssl->transform->out_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004613 p += ssl->transform->out_cid_len;
4614#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4615
4616 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004617 * Saved fields from top-level ssl_context structure
4618 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004619 if ((size_t) (end - p) < 4) {
4620 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4621 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004622
Gilles Peskine449bd832023-01-11 14:50:10 +01004623 ssl->badmac_seen = ((uint32_t) p[0] << 24) |
4624 ((uint32_t) p[1] << 16) |
4625 ((uint32_t) p[2] << 8) |
4626 ((uint32_t) p[3]);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004627 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004628
4629#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine449bd832023-01-11 14:50:10 +01004630 if ((size_t) (end - p) < 16) {
4631 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4632 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004633
Gilles Peskine449bd832023-01-11 14:50:10 +01004634 ssl->in_window_top = ((uint64_t) p[0] << 56) |
4635 ((uint64_t) p[1] << 48) |
4636 ((uint64_t) p[2] << 40) |
4637 ((uint64_t) p[3] << 32) |
4638 ((uint64_t) p[4] << 24) |
4639 ((uint64_t) p[5] << 16) |
4640 ((uint64_t) p[6] << 8) |
4641 ((uint64_t) p[7]);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004642 p += 8;
4643
Gilles Peskine449bd832023-01-11 14:50:10 +01004644 ssl->in_window = ((uint64_t) p[0] << 56) |
4645 ((uint64_t) p[1] << 48) |
4646 ((uint64_t) p[2] << 40) |
4647 ((uint64_t) p[3] << 32) |
4648 ((uint64_t) p[4] << 24) |
4649 ((uint64_t) p[5] << 16) |
4650 ((uint64_t) p[6] << 8) |
4651 ((uint64_t) p[7]);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004652 p += 8;
4653#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4654
4655#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004656 if ((size_t) (end - p) < 1) {
4657 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4658 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004659
4660 ssl->disable_datagram_packing = *p++;
4661#endif /* MBEDTLS_SSL_PROTO_DTLS */
4662
Gilles Peskine449bd832023-01-11 14:50:10 +01004663 if ((size_t) (end - p) < sizeof(ssl->cur_out_ctr)) {
4664 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4665 }
4666 memcpy(ssl->cur_out_ctr, p, sizeof(ssl->cur_out_ctr));
4667 p += sizeof(ssl->cur_out_ctr);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004668
4669#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004670 if ((size_t) (end - p) < 2) {
4671 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4672 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004673
Gilles Peskine449bd832023-01-11 14:50:10 +01004674 ssl->mtu = (p[0] << 8) | p[1];
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004675 p += 2;
4676#endif /* MBEDTLS_SSL_PROTO_DTLS */
4677
4678#if defined(MBEDTLS_SSL_ALPN)
4679 {
4680 uint8_t alpn_len;
4681 const char **cur;
4682
Gilles Peskine449bd832023-01-11 14:50:10 +01004683 if ((size_t) (end - p) < 1) {
4684 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4685 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004686
4687 alpn_len = *p++;
4688
Gilles Peskine449bd832023-01-11 14:50:10 +01004689 if (alpn_len != 0 && ssl->conf->alpn_list != NULL) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004690 /* alpn_chosen should point to an item in the configured list */
Gilles Peskine449bd832023-01-11 14:50:10 +01004691 for (cur = ssl->conf->alpn_list; *cur != NULL; cur++) {
4692 if (strlen(*cur) == alpn_len &&
4693 memcmp(p, cur, alpn_len) == 0) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004694 ssl->alpn_chosen = *cur;
4695 break;
4696 }
4697 }
4698 }
4699
4700 /* can only happen on conf mismatch */
Gilles Peskine449bd832023-01-11 14:50:10 +01004701 if (alpn_len != 0 && ssl->alpn_chosen == NULL) {
4702 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4703 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004704
4705 p += alpn_len;
4706 }
4707#endif /* MBEDTLS_SSL_ALPN */
4708
4709 /*
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004710 * Forced fields from top-level ssl_context structure
4711 *
4712 * Most of them already set to the correct value by mbedtls_ssl_init() and
4713 * mbedtls_ssl_reset(), so we only need to set the remaining ones.
4714 */
4715 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
Glenn Strauss60bfe602022-03-14 19:04:24 -04004716 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004717
Hanno Becker361b10d2019-08-30 10:42:49 +01004718 /* Adjust pointers for header fields of outgoing records to
4719 * the given transform, accounting for explicit IV and CID. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004720 mbedtls_ssl_update_out_pointers(ssl, ssl->transform);
Hanno Becker361b10d2019-08-30 10:42:49 +01004721
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004722#if defined(MBEDTLS_SSL_PROTO_DTLS)
4723 ssl->in_epoch = 1;
4724#endif
4725
4726 /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated,
4727 * which we don't want - otherwise we'd end up freeing the wrong transform
Hanno Beckerce5f5fd2020-02-05 10:47:44 +00004728 * by calling mbedtls_ssl_handshake_wrapup_free_hs_transform()
4729 * inappropriately. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004730 if (ssl->handshake != NULL) {
4731 mbedtls_ssl_handshake_free(ssl);
4732 mbedtls_free(ssl->handshake);
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004733 ssl->handshake = NULL;
4734 }
4735
4736 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004737 * Done - should have consumed entire buffer
4738 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004739 if (p != end) {
4740 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4741 }
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004742
Gilles Peskine449bd832023-01-11 14:50:10 +01004743 return 0;
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004744}
4745
4746/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02004747 * Deserialize context: public wrapper for error cleaning
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004748 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004749int mbedtls_ssl_context_load(mbedtls_ssl_context *context,
4750 const unsigned char *buf,
4751 size_t len)
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004752{
Gilles Peskine449bd832023-01-11 14:50:10 +01004753 int ret = ssl_context_load(context, buf, len);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004754
Gilles Peskine449bd832023-01-11 14:50:10 +01004755 if (ret != 0) {
4756 mbedtls_ssl_free(context);
4757 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004758
Gilles Peskine449bd832023-01-11 14:50:10 +01004759 return ret;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004760}
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02004761#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004762
4763/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004764 * Free an SSL context
4765 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004766void mbedtls_ssl_free(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00004767{
Gilles Peskine449bd832023-01-11 14:50:10 +01004768 if (ssl == NULL) {
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004769 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01004770 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004771
Gilles Peskine449bd832023-01-11 14:50:10 +01004772 MBEDTLS_SSL_DEBUG_MSG(2, ("=> free"));
Paul Bakker5121ce52009-01-03 21:22:43 +00004773
Gilles Peskine449bd832023-01-11 14:50:10 +01004774 if (ssl->out_buf != NULL) {
sander-visserb8aa2072020-05-06 22:05:13 +02004775#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4776 size_t out_buf_len = ssl->out_buf_len;
4777#else
4778 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
4779#endif
4780
Gilles Peskine449bd832023-01-11 14:50:10 +01004781 mbedtls_platform_zeroize(ssl->out_buf, out_buf_len);
4782 mbedtls_free(ssl->out_buf);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004783 ssl->out_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004784 }
4785
Gilles Peskine449bd832023-01-11 14:50:10 +01004786 if (ssl->in_buf != NULL) {
sander-visserb8aa2072020-05-06 22:05:13 +02004787#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4788 size_t in_buf_len = ssl->in_buf_len;
4789#else
4790 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
4791#endif
4792
Gilles Peskine449bd832023-01-11 14:50:10 +01004793 mbedtls_platform_zeroize(ssl->in_buf, in_buf_len);
4794 mbedtls_free(ssl->in_buf);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004795 ssl->in_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004796 }
4797
Gilles Peskine449bd832023-01-11 14:50:10 +01004798 if (ssl->transform) {
4799 mbedtls_ssl_transform_free(ssl->transform);
4800 mbedtls_free(ssl->transform);
Paul Bakker48916f92012-09-16 19:57:18 +00004801 }
4802
Gilles Peskine449bd832023-01-11 14:50:10 +01004803 if (ssl->handshake) {
4804 mbedtls_ssl_handshake_free(ssl);
4805 mbedtls_free(ssl->handshake);
Jerry Yu2e199812022-12-01 18:57:19 +08004806
4807#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01004808 mbedtls_ssl_transform_free(ssl->transform_negotiate);
4809 mbedtls_free(ssl->transform_negotiate);
Jerry Yu2e199812022-12-01 18:57:19 +08004810#endif
4811
Gilles Peskine449bd832023-01-11 14:50:10 +01004812 mbedtls_ssl_session_free(ssl->session_negotiate);
4813 mbedtls_free(ssl->session_negotiate);
Paul Bakker48916f92012-09-16 19:57:18 +00004814 }
4815
Ronald Cron6f135e12021-12-08 16:57:54 +01004816#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01004817 mbedtls_ssl_transform_free(ssl->transform_application);
4818 mbedtls_free(ssl->transform_application);
Ronald Cron6f135e12021-12-08 16:57:54 +01004819#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker3aa186f2021-08-10 09:24:19 +01004820
Gilles Peskine449bd832023-01-11 14:50:10 +01004821 if (ssl->session) {
4822 mbedtls_ssl_session_free(ssl->session);
4823 mbedtls_free(ssl->session);
Paul Bakkerc0463502013-02-14 11:19:38 +01004824 }
4825
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02004826#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004827 if (ssl->hostname != NULL) {
4828 mbedtls_platform_zeroize(ssl->hostname, strlen(ssl->hostname));
4829 mbedtls_free(ssl->hostname);
Paul Bakker5121ce52009-01-03 21:22:43 +00004830 }
Paul Bakker0be444a2013-08-27 21:55:01 +02004831#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004832
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02004833#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004834 mbedtls_free(ssl->cli_id);
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02004835#endif
4836
Gilles Peskine449bd832023-01-11 14:50:10 +01004837 MBEDTLS_SSL_DEBUG_MSG(2, ("<= free"));
Paul Bakker2da561c2009-02-05 18:00:28 +00004838
Paul Bakker86f04f42013-02-14 11:20:09 +01004839 /* Actually clear after last debug message */
Gilles Peskine449bd832023-01-11 14:50:10 +01004840 mbedtls_platform_zeroize(ssl, sizeof(mbedtls_ssl_context));
Paul Bakker5121ce52009-01-03 21:22:43 +00004841}
4842
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004843/*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08004844 * Initialize mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004845 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004846void mbedtls_ssl_config_init(mbedtls_ssl_config *conf)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004847{
Gilles Peskine449bd832023-01-11 14:50:10 +01004848 memset(conf, 0, sizeof(mbedtls_ssl_config));
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004849}
4850
Gilles Peskineae270bf2021-06-02 00:05:29 +02004851/* The selection should be the same as mbedtls_x509_crt_profile_default in
4852 * x509_crt.c, plus Montgomery curves for ECDHE. Here, the order matters:
Gilles Peskineb1940a72021-06-02 15:18:12 +02004853 * curves with a lower resource usage come first.
Gilles Peskineae270bf2021-06-02 00:05:29 +02004854 * See the documentation of mbedtls_ssl_conf_curves() for what we promise
Gilles Peskineb1940a72021-06-02 15:18:12 +02004855 * about this list.
4856 */
Brett Warrene0edc842021-08-17 09:53:13 +01004857static uint16_t ssl_preset_default_groups[] = {
Gilles Peskineae270bf2021-06-02 00:05:29 +02004858#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004859 MBEDTLS_SSL_IANA_TLS_GROUP_X25519,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004860#endif
4861#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004862 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004863#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02004864#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004865 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004866#endif
4867#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004868 MBEDTLS_SSL_IANA_TLS_GROUP_X448,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004869#endif
4870#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004871 MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004872#endif
Gilles Peskineae270bf2021-06-02 00:05:29 +02004873#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004874 MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004875#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02004876#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004877 MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004878#endif
4879#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004880 MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004881#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004882 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Gilles Peskineae270bf2021-06-02 00:05:29 +02004883};
Gilles Peskineae270bf2021-06-02 00:05:29 +02004884
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004885static int ssl_preset_suiteb_ciphersuites[] = {
4886 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
4887 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
4888 0
4889};
4890
Ronald Crone68ab4f2022-10-05 12:46:29 +02004891#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004892
Jerry Yu909df7b2022-01-22 11:56:27 +08004893/* NOTICE:
Jerry Yu0b994b82022-01-25 17:22:12 +08004894 * For ssl_preset_*_sig_algs and ssl_tls12_preset_*_sig_algs, the following
Jerry Yu370e1462022-01-25 10:36:53 +08004895 * rules SHOULD be upheld.
4896 * - No duplicate entries.
4897 * - But if there is a good reason, do not change the order of the algorithms.
Jerry Yu09a99fc2022-07-28 14:22:17 +08004898 * - ssl_tls12_preset* is for TLS 1.2 use only.
Jerry Yu370e1462022-01-25 10:36:53 +08004899 * - ssl_preset_* is for TLS 1.3 only or hybrid TLS 1.3/1.2 handshakes.
Jerry Yu1a8b4812022-01-20 17:56:50 +08004900 */
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004901static uint16_t ssl_preset_default_sig_algs[] = {
Jerry Yu1a8b4812022-01-20 17:56:50 +08004902
Andrzej Kurek25f27152022-08-17 16:09:31 -04004903#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 +08004904 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
4905 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004906#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA &&
Jerry Yued5e9f42022-01-26 11:21:34 +08004907 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004908
Andrzej Kurek25f27152022-08-17 16:09:31 -04004909#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 +08004910 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
4911 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004912#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu909df7b2022-01-22 11:56:27 +08004913 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
4914
Andrzej Kurek25f27152022-08-17 16:09:31 -04004915#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 +08004916 defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
4917 MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004918#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yued5e9f42022-01-26 11:21:34 +08004919 MBEDTLS_ECP_DP_SECP521R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004920
Gilles Peskine449bd832023-01-11 14:50:10 +01004921#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && \
4922 defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu9bb3ee42022-06-23 10:16:33 +08004923 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
Gilles Peskine449bd832023-01-11 14:50:10 +01004924#endif \
4925 /* 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 +08004926
Gilles Peskine449bd832023-01-11 14:50:10 +01004927#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && \
4928 defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu9bb3ee42022-06-23 10:16:33 +08004929 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
Gilles Peskine449bd832023-01-11 14:50:10 +01004930#endif \
4931 /* 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 +08004932
Gilles Peskine449bd832023-01-11 14:50:10 +01004933#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && \
4934 defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu9bb3ee42022-06-23 10:16:33 +08004935 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Gilles Peskine449bd832023-01-11 14:50:10 +01004936#endif \
4937 /* 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 +08004938
Andrzej Kurek25f27152022-08-17 16:09:31 -04004939#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 +08004940 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512,
4941#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA512_C */
4942
Andrzej Kurek25f27152022-08-17 16:09:31 -04004943#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 +08004944 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384,
4945#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA384_C */
4946
Andrzej Kurek25f27152022-08-17 16:09:31 -04004947#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 +08004948 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
4949#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA256_C */
4950
Gabor Mezei15b95a62022-05-09 16:37:58 +02004951 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08004952};
4953
4954/* NOTICE: see above */
4955#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4956static uint16_t ssl_tls12_preset_default_sig_algs[] = {
Andrzej Kurek25f27152022-08-17 16:09:31 -04004957#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004958#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004959 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA512),
Jerry Yu713013f2022-01-17 18:16:35 +08004960#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004961#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4962 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
4963#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004964#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004965 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA512),
Gabor Mezeic1051b62022-05-10 13:13:58 +02004966#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004967#endif /* MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04004968#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004969#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004970 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08004971#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004972#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4973 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
4974#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004975#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004976 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384),
Gabor Mezeic1051b62022-05-10 13:13:58 +02004977#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004978#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04004979#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004980#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004981 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08004982#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004983#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4984 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
4985#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004986#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004987 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256),
Gabor Mezeic1051b62022-05-10 13:13:58 +02004988#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004989#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Gabor Mezei15b95a62022-05-09 16:37:58 +02004990 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08004991};
4992#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4993/* NOTICE: see above */
4994static uint16_t ssl_preset_suiteb_sig_algs[] = {
4995
Andrzej Kurek25f27152022-08-17 16:09:31 -04004996#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 +08004997 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
4998 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004999#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu909df7b2022-01-22 11:56:27 +08005000 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
5001
Andrzej Kurek25f27152022-08-17 16:09:31 -04005002#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 +08005003 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
5004 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005005#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu53037892022-01-25 11:02:06 +08005006 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08005007
Gilles Peskine449bd832023-01-11 14:50:10 +01005008#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && \
5009 defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu909df7b2022-01-22 11:56:27 +08005010 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Gilles Peskine449bd832023-01-11 14:50:10 +01005011#endif \
5012 /* 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 +08005013
Andrzej Kurek25f27152022-08-17 16:09:31 -04005014#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 +08005015 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005016#endif /* MBEDTLS_RSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yu53037892022-01-25 11:02:06 +08005017
Gabor Mezei15b95a62022-05-09 16:37:58 +02005018 MBEDTLS_TLS_SIG_NONE
Jerry Yu713013f2022-01-17 18:16:35 +08005019};
Jerry Yu6106fdc2022-01-12 16:36:14 +08005020
Jerry Yu909df7b2022-01-22 11:56:27 +08005021/* NOTICE: see above */
5022#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5023static uint16_t ssl_tls12_preset_suiteb_sig_algs[] = {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005024#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02005025#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005026 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256),
Jerry Yu713013f2022-01-17 18:16:35 +08005027#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02005028#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005029 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256),
Gabor Mezeic1051b62022-05-10 13:13:58 +02005030#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005031#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04005032#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02005033#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005034 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384),
Jerry Yu18c833e2022-01-25 10:55:47 +08005035#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02005036#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005037 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384),
Gabor Mezeic1051b62022-05-10 13:13:58 +02005038#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005039#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Gabor Mezei15b95a62022-05-09 16:37:58 +02005040 MBEDTLS_TLS_SIG_NONE
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01005041};
Jerry Yu909df7b2022-01-22 11:56:27 +08005042#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5043
Ronald Crone68ab4f2022-10-05 12:46:29 +02005044#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005045
Brett Warrene0edc842021-08-17 09:53:13 +01005046static uint16_t ssl_preset_suiteb_groups[] = {
Jaeden Amerod4311042019-06-03 08:27:16 +01005047#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01005048 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01005049#endif
5050#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01005051 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01005052#endif
Brett Warrene0edc842021-08-17 09:53:13 +01005053 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005054};
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005055
Ronald Crone68ab4f2022-10-05 12:46:29 +02005056#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08005057/* Function for checking `ssl_preset_*_sig_algs` and `ssl_tls12_preset_*_sig_algs`
Jerry Yu370e1462022-01-25 10:36:53 +08005058 * to make sure there are no duplicated signature algorithm entries. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005059MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01005060static int ssl_check_no_sig_alg_duplication(uint16_t *sig_algs)
Jerry Yu1a8b4812022-01-20 17:56:50 +08005061{
5062 size_t i, j;
5063 int ret = 0;
Jerry Yu909df7b2022-01-22 11:56:27 +08005064
Gilles Peskine449bd832023-01-11 14:50:10 +01005065 for (i = 0; sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++) {
5066 for (j = 0; j < i; j++) {
5067 if (sig_algs[i] != sig_algs[j]) {
Jerry Yuf377d642022-01-25 10:43:59 +08005068 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01005069 }
5070 mbedtls_printf(" entry(%04x,%" MBEDTLS_PRINTF_SIZET
5071 ") is duplicated at %" MBEDTLS_PRINTF_SIZET "\n",
5072 sig_algs[i], j, i);
Jerry Yuf377d642022-01-25 10:43:59 +08005073 ret = -1;
Jerry Yu1a8b4812022-01-20 17:56:50 +08005074 }
5075 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005076 return ret;
Jerry Yu1a8b4812022-01-20 17:56:50 +08005077}
5078
Ronald Crone68ab4f2022-10-05 12:46:29 +02005079#endif /* MBEDTLS_DEBUG_C && MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yu1a8b4812022-01-20 17:56:50 +08005080
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005081/*
Tillmann Karras588ad502015-09-25 04:27:22 +02005082 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005083 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005084int mbedtls_ssl_config_defaults(mbedtls_ssl_config *conf,
5085 int endpoint, int transport, int preset)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005086{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005087#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Janos Follath865b3eb2019-12-16 11:46:15 +00005088 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005089#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005090
Ronald Crone68ab4f2022-10-05 12:46:29 +02005091#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01005092 if (ssl_check_no_sig_alg_duplication(ssl_preset_suiteb_sig_algs)) {
5093 mbedtls_printf("ssl_preset_suiteb_sig_algs has duplicated entries\n");
5094 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu1a8b4812022-01-20 17:56:50 +08005095 }
5096
Gilles Peskine449bd832023-01-11 14:50:10 +01005097 if (ssl_check_no_sig_alg_duplication(ssl_preset_default_sig_algs)) {
5098 mbedtls_printf("ssl_preset_default_sig_algs has duplicated entries\n");
5099 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu1a8b4812022-01-20 17:56:50 +08005100 }
Jerry Yu909df7b2022-01-22 11:56:27 +08005101
5102#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01005103 if (ssl_check_no_sig_alg_duplication(ssl_tls12_preset_suiteb_sig_algs)) {
5104 mbedtls_printf("ssl_tls12_preset_suiteb_sig_algs has duplicated entries\n");
5105 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu909df7b2022-01-22 11:56:27 +08005106 }
5107
Gilles Peskine449bd832023-01-11 14:50:10 +01005108 if (ssl_check_no_sig_alg_duplication(ssl_tls12_preset_default_sig_algs)) {
5109 mbedtls_printf("ssl_tls12_preset_default_sig_algs has duplicated entries\n");
5110 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu909df7b2022-01-22 11:56:27 +08005111 }
5112#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Ronald Crone68ab4f2022-10-05 12:46:29 +02005113#endif /* MBEDTLS_DEBUG_C && MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yu1a8b4812022-01-20 17:56:50 +08005114
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02005115 /* Use the functions here so that they are covered in tests,
5116 * but otherwise access member directly for efficiency */
Gilles Peskine449bd832023-01-11 14:50:10 +01005117 mbedtls_ssl_conf_endpoint(conf, endpoint);
5118 mbedtls_ssl_conf_transport(conf, transport);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005119
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005120 /*
5121 * Things that are common to all presets
5122 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02005123#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005124 if (endpoint == MBEDTLS_SSL_IS_CLIENT) {
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02005125 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
5126#if defined(MBEDTLS_SSL_SESSION_TICKETS)
5127 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
5128#endif
5129 }
5130#endif
5131
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005132#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
5133 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
5134#endif
5135
5136#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
5137 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
5138#endif
5139
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02005140#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005141 conf->f_cookie_write = ssl_cookie_write_dummy;
5142 conf->f_cookie_check = ssl_cookie_check_dummy;
5143#endif
5144
5145#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
5146 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
5147#endif
5148
Janos Follath088ce432017-04-10 12:42:31 +01005149#if defined(MBEDTLS_SSL_SRV_C)
5150 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
TRodziewicz3946f792021-06-14 12:11:18 +02005151 conf->respect_cli_pref = MBEDTLS_SSL_SRV_CIPHERSUITE_ORDER_SERVER;
Janos Follath088ce432017-04-10 12:42:31 +01005152#endif
5153
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005154#if defined(MBEDTLS_SSL_PROTO_DTLS)
5155 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
5156 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
5157#endif
5158
5159#if defined(MBEDTLS_SSL_RENEGOTIATION)
5160 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005161 memset(conf->renego_period, 0x00, 2);
5162 memset(conf->renego_period + 2, 0xFF, 6);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005163#endif
5164
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005165#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005166 if (endpoint == MBEDTLS_SSL_IS_SERVER) {
Hanno Beckere2defad2021-07-24 05:59:17 +01005167 const unsigned char dhm_p[] =
5168 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
5169 const unsigned char dhm_g[] =
5170 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
Hanno Becker00d0a682017-10-04 13:14:29 +01005171
Gilles Peskine449bd832023-01-11 14:50:10 +01005172 if ((ret = mbedtls_ssl_conf_dh_param_bin(conf,
5173 dhm_p, sizeof(dhm_p),
5174 dhm_g, sizeof(dhm_g))) != 0) {
5175 return ret;
Hanno Beckere2defad2021-07-24 05:59:17 +01005176 }
5177 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02005178#endif
5179
Ronald Cron6f135e12021-12-08 16:57:54 +01005180#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu6ee56aa2022-12-06 17:47:22 +08005181
5182#if defined(MBEDTLS_SSL_EARLY_DATA)
Gilles Peskine449bd832023-01-11 14:50:10 +01005183 mbedtls_ssl_tls13_conf_early_data(conf, MBEDTLS_SSL_EARLY_DATA_DISABLED);
Jerry Yu6ee56aa2022-12-06 17:47:22 +08005184#if defined(MBEDTLS_SSL_SRV_C)
5185 mbedtls_ssl_tls13_conf_max_early_data_size(
Gilles Peskine449bd832023-01-11 14:50:10 +01005186 conf, MBEDTLS_SSL_MAX_EARLY_DATA_SIZE);
Jerry Yu6ee56aa2022-12-06 17:47:22 +08005187#endif
5188#endif /* MBEDTLS_SSL_EARLY_DATA */
5189
Jerry Yud0766ec2022-09-22 10:46:57 +08005190#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08005191 mbedtls_ssl_conf_new_session_tickets(
Gilles Peskine449bd832023-01-11 14:50:10 +01005192 conf, MBEDTLS_SSL_TLS1_3_DEFAULT_NEW_SESSION_TICKETS);
Jerry Yu1ad7ace2022-08-09 13:28:39 +08005193#endif
Hanno Becker71f1ed62021-07-24 06:01:47 +01005194 /*
5195 * Allow all TLS 1.3 key exchange modes by default.
5196 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00005197 conf->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Ronald Cron6f135e12021-12-08 16:57:54 +01005198#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01005199
Gilles Peskine449bd832023-01-11 14:50:10 +01005200 if (transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Glenn Strauss2dfcea22022-03-14 17:26:42 -04005201#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
XiaokangQian4d3a6042022-04-21 13:46:17 +00005202 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian060d8672022-04-21 09:24:56 +00005203 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian4d3a6042022-04-21 13:46:17 +00005204#else
Gilles Peskine449bd832023-01-11 14:50:10 +01005205 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQian060d8672022-04-21 09:24:56 +00005206#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005207 } else {
XiaokangQian4d3a6042022-04-21 13:46:17 +00005208#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01005209 if (endpoint == MBEDTLS_SSL_IS_CLIENT) {
XiaokangQian4d3a6042022-04-21 13:46:17 +00005210 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5211 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
Gilles Peskine449bd832023-01-11 14:50:10 +01005212 } else {
5213 /* Hybrid TLS 1.2 / 1.3 is not supported on server side yet */
XiaokangQian4d3a6042022-04-21 13:46:17 +00005214 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5215 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5216 }
5217#elif defined(MBEDTLS_SSL_PROTO_TLS1_3)
5218 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
5219 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
5220#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
5221 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5222 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5223#else
Gilles Peskine449bd832023-01-11 14:50:10 +01005224 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQian4d3a6042022-04-21 13:46:17 +00005225#endif
5226 }
Glenn Strauss2dfcea22022-03-14 17:26:42 -04005227
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005228 /*
5229 * Preset-specific defaults
5230 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005231 switch (preset) {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005232 /*
5233 * NSA Suite B
5234 */
5235 case MBEDTLS_SSL_PRESET_SUITEB:
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005236
Hanno Beckerd60b6c62021-04-29 12:04:11 +01005237 conf->ciphersuite_list = ssl_preset_suiteb_ciphersuites;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005238
5239#if defined(MBEDTLS_X509_CRT_PARSE_C)
5240 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005241#endif
5242
Ronald Crone68ab4f2022-10-05 12:46:29 +02005243#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08005244#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01005245 if (mbedtls_ssl_conf_is_tls12_only(conf)) {
Jerry Yu909df7b2022-01-22 11:56:27 +08005246 conf->sig_algs = ssl_tls12_preset_suiteb_sig_algs;
Gilles Peskine449bd832023-01-11 14:50:10 +01005247 } else
Jerry Yu909df7b2022-01-22 11:56:27 +08005248#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005249 conf->sig_algs = ssl_preset_suiteb_sig_algs;
Ronald Crone68ab4f2022-10-05 12:46:29 +02005250#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005251
Brett Warrene0edc842021-08-17 09:53:13 +01005252#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
5253 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005254#endif
Brett Warrene0edc842021-08-17 09:53:13 +01005255 conf->group_list = ssl_preset_suiteb_groups;
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02005256 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005257
5258 /*
5259 * Default
5260 */
5261 default:
Ronald Cronf6606552022-03-15 11:23:25 +01005262
Hanno Beckerd60b6c62021-04-29 12:04:11 +01005263 conf->ciphersuite_list = mbedtls_ssl_list_ciphersuites();
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005264
5265#if defined(MBEDTLS_X509_CRT_PARSE_C)
5266 conf->cert_profile = &mbedtls_x509_crt_profile_default;
5267#endif
5268
Ronald Crone68ab4f2022-10-05 12:46:29 +02005269#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08005270#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01005271 if (mbedtls_ssl_conf_is_tls12_only(conf)) {
Jerry Yu909df7b2022-01-22 11:56:27 +08005272 conf->sig_algs = ssl_tls12_preset_default_sig_algs;
Gilles Peskine449bd832023-01-11 14:50:10 +01005273 } else
Jerry Yu909df7b2022-01-22 11:56:27 +08005274#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005275 conf->sig_algs = ssl_preset_default_sig_algs;
Ronald Crone68ab4f2022-10-05 12:46:29 +02005276#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005277
Brett Warrene0edc842021-08-17 09:53:13 +01005278#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
5279 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005280#endif
Brett Warrene0edc842021-08-17 09:53:13 +01005281 conf->group_list = ssl_preset_default_groups;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005282
5283#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
5284 conf->dhm_min_bitlen = 1024;
5285#endif
5286 }
5287
Gilles Peskine449bd832023-01-11 14:50:10 +01005288 return 0;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005289}
5290
5291/*
5292 * Free mbedtls_ssl_config
5293 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005294void mbedtls_ssl_config_free(mbedtls_ssl_config *conf)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005295{
5296#if defined(MBEDTLS_DHM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005297 mbedtls_mpi_free(&conf->dhm_P);
5298 mbedtls_mpi_free(&conf->dhm_G);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005299#endif
5300
Ronald Cron73fe8df2022-10-05 14:31:43 +02005301#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02005302#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01005303 if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) {
Neil Armstrong501c9322022-05-03 09:35:09 +02005304 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
5305 }
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005306#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01005307 if (conf->psk != NULL) {
5308 mbedtls_platform_zeroize(conf->psk, conf->psk_len);
5309 mbedtls_free(conf->psk);
Azim Khan27e8a122018-03-21 14:24:11 +00005310 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005311 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09005312 }
5313
Gilles Peskine449bd832023-01-11 14:50:10 +01005314 if (conf->psk_identity != NULL) {
5315 mbedtls_platform_zeroize(conf->psk_identity, conf->psk_identity_len);
5316 mbedtls_free(conf->psk_identity);
Azim Khan27e8a122018-03-21 14:24:11 +00005317 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005318 conf->psk_identity_len = 0;
5319 }
Ronald Cron73fe8df2022-10-05 14:31:43 +02005320#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005321
5322#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005323 ssl_key_cert_free(conf->key_cert);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005324#endif
5325
Gilles Peskine449bd832023-01-11 14:50:10 +01005326 mbedtls_platform_zeroize(conf, sizeof(mbedtls_ssl_config));
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005327}
5328
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02005329#if defined(MBEDTLS_PK_C) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01005330 (defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C))
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005331/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005332 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005333 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005334unsigned char mbedtls_ssl_sig_from_pk(mbedtls_pk_context *pk)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005335{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005336#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005337 if (mbedtls_pk_can_do(pk, MBEDTLS_PK_RSA)) {
5338 return MBEDTLS_SSL_SIG_RSA;
5339 }
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005340#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005341#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005342 if (mbedtls_pk_can_do(pk, MBEDTLS_PK_ECDSA)) {
5343 return MBEDTLS_SSL_SIG_ECDSA;
5344 }
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005345#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005346 return MBEDTLS_SSL_SIG_ANON;
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005347}
5348
Gilles Peskine449bd832023-01-11 14:50:10 +01005349unsigned char mbedtls_ssl_sig_from_pk_alg(mbedtls_pk_type_t type)
Hanno Becker7e5437a2017-04-28 17:15:26 +01005350{
Gilles Peskine449bd832023-01-11 14:50:10 +01005351 switch (type) {
Hanno Becker7e5437a2017-04-28 17:15:26 +01005352 case MBEDTLS_PK_RSA:
Gilles Peskine449bd832023-01-11 14:50:10 +01005353 return MBEDTLS_SSL_SIG_RSA;
Hanno Becker7e5437a2017-04-28 17:15:26 +01005354 case MBEDTLS_PK_ECDSA:
5355 case MBEDTLS_PK_ECKEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01005356 return MBEDTLS_SSL_SIG_ECDSA;
Hanno Becker7e5437a2017-04-28 17:15:26 +01005357 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005358 return MBEDTLS_SSL_SIG_ANON;
Hanno Becker7e5437a2017-04-28 17:15:26 +01005359 }
5360}
5361
Gilles Peskine449bd832023-01-11 14:50:10 +01005362mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig(unsigned char sig)
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005363{
Gilles Peskine449bd832023-01-11 14:50:10 +01005364 switch (sig) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005365#if defined(MBEDTLS_RSA_C)
5366 case MBEDTLS_SSL_SIG_RSA:
Gilles Peskine449bd832023-01-11 14:50:10 +01005367 return MBEDTLS_PK_RSA;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005368#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005369#if defined(MBEDTLS_ECDSA_C)
5370 case MBEDTLS_SSL_SIG_ECDSA:
Gilles Peskine449bd832023-01-11 14:50:10 +01005371 return MBEDTLS_PK_ECDSA;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005372#endif
5373 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005374 return MBEDTLS_PK_NONE;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005375 }
5376}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02005377#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005378
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02005379/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005380 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02005381 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005382mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash(unsigned char hash)
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005383{
Gilles Peskine449bd832023-01-11 14:50:10 +01005384 switch (hash) {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005385#if defined(MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005386 case MBEDTLS_SSL_HASH_MD5:
Gilles Peskine449bd832023-01-11 14:50:10 +01005387 return MBEDTLS_MD_MD5;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005388#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005389#if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005390 case MBEDTLS_SSL_HASH_SHA1:
Gilles Peskine449bd832023-01-11 14:50:10 +01005391 return MBEDTLS_MD_SHA1;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005392#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005393#if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005394 case MBEDTLS_SSL_HASH_SHA224:
Gilles Peskine449bd832023-01-11 14:50:10 +01005395 return MBEDTLS_MD_SHA224;
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02005396#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005397#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005398 case MBEDTLS_SSL_HASH_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01005399 return MBEDTLS_MD_SHA256;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005400#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005401#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005402 case MBEDTLS_SSL_HASH_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01005403 return MBEDTLS_MD_SHA384;
Mateusz Starzyk3352a532021-04-06 14:28:22 +02005404#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005405#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005406 case MBEDTLS_SSL_HASH_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01005407 return MBEDTLS_MD_SHA512;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005408#endif
5409 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005410 return MBEDTLS_MD_NONE;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005411 }
5412}
5413
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005414/*
5415 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
5416 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005417unsigned char mbedtls_ssl_hash_from_md_alg(int md)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005418{
Gilles Peskine449bd832023-01-11 14:50:10 +01005419 switch (md) {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005420#if defined(MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005421 case MBEDTLS_MD_MD5:
Gilles Peskine449bd832023-01-11 14:50:10 +01005422 return MBEDTLS_SSL_HASH_MD5;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005423#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005424#if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005425 case MBEDTLS_MD_SHA1:
Gilles Peskine449bd832023-01-11 14:50:10 +01005426 return MBEDTLS_SSL_HASH_SHA1;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005427#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005428#if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005429 case MBEDTLS_MD_SHA224:
Gilles Peskine449bd832023-01-11 14:50:10 +01005430 return MBEDTLS_SSL_HASH_SHA224;
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02005431#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005432#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005433 case MBEDTLS_MD_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01005434 return MBEDTLS_SSL_HASH_SHA256;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005435#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005436#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005437 case MBEDTLS_MD_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01005438 return MBEDTLS_SSL_HASH_SHA384;
Mateusz Starzyk3352a532021-04-06 14:28:22 +02005439#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005440#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005441 case MBEDTLS_MD_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01005442 return MBEDTLS_SSL_HASH_SHA512;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005443#endif
5444 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005445 return MBEDTLS_SSL_HASH_NONE;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005446 }
5447}
5448
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005449/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005450 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005451 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005452 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005453int mbedtls_ssl_check_curve_tls_id(const mbedtls_ssl_context *ssl, uint16_t tls_id)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005454{
Gilles Peskine449bd832023-01-11 14:50:10 +01005455 const uint16_t *group_list = mbedtls_ssl_get_groups(ssl);
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005456
Gilles Peskine449bd832023-01-11 14:50:10 +01005457 if (group_list == NULL) {
5458 return -1;
Brett Warrene0edc842021-08-17 09:53:13 +01005459 }
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005460
Gilles Peskine449bd832023-01-11 14:50:10 +01005461 for (; *group_list != 0; group_list++) {
5462 if (*group_list == tls_id) {
5463 return 0;
5464 }
5465 }
5466
5467 return -1;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005468}
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005469
5470#if defined(MBEDTLS_ECP_C)
5471/*
5472 * Same as mbedtls_ssl_check_curve_tls_id() but with a mbedtls_ecp_group_id.
5473 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005474int mbedtls_ssl_check_curve(const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id)
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005475{
Gilles Peskine449bd832023-01-11 14:50:10 +01005476 uint16_t tls_id = mbedtls_ssl_get_tls_id_from_ecp_group_id(grp_id);
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005477
Gilles Peskine449bd832023-01-11 14:50:10 +01005478 if (tls_id == 0) {
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005479 return -1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005480 }
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005481
Gilles Peskine449bd832023-01-11 14:50:10 +01005482 return mbedtls_ssl_check_curve_tls_id(ssl, tls_id);
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005483}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02005484#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005485
Gilles Peskine449bd832023-01-11 14:50:10 +01005486#if defined(MBEDTLS_DEBUG_C)
Valerio Setti67419f02023-01-04 16:12:42 +01005487#define EC_NAME(_name_) _name_
5488#else
5489#define EC_NAME(_name_) NULL
5490#endif
5491
Valerio Setti18c9fed2022-12-30 17:44:24 +01005492static const struct {
5493 uint16_t tls_id;
5494 mbedtls_ecp_group_id ecp_group_id;
5495 psa_ecc_family_t psa_family;
5496 uint16_t bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01005497 const char *name;
Valerio Setti18c9fed2022-12-30 17:44:24 +01005498} tls_id_match_table[] =
5499{
5500#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_521)
Gilles Peskine449bd832023-01-11 14:50:10 +01005501 { 25, MBEDTLS_ECP_DP_SECP521R1, PSA_ECC_FAMILY_SECP_R1, 521, EC_NAME("secp521r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005502#endif
5503#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED) || defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512)
Gilles Peskine449bd832023-01-11 14:50:10 +01005504 { 28, MBEDTLS_ECP_DP_BP512R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 512, EC_NAME("brainpoolP512r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005505#endif
Valerio Setti67419f02023-01-04 16:12:42 +01005506#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_384)
Gilles Peskine449bd832023-01-11 14:50:10 +01005507 { 24, MBEDTLS_ECP_DP_SECP384R1, PSA_ECC_FAMILY_SECP_R1, 384, EC_NAME("secp384r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005508#endif
Valerio Setti67419f02023-01-04 16:12:42 +01005509#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED) || defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384)
Gilles Peskine449bd832023-01-11 14:50:10 +01005510 { 27, MBEDTLS_ECP_DP_BP384R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 384, EC_NAME("brainpoolP384r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005511#endif
Valerio Setti67419f02023-01-04 16:12:42 +01005512#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_256)
Gilles Peskine449bd832023-01-11 14:50:10 +01005513 { 23, MBEDTLS_ECP_DP_SECP256R1, PSA_ECC_FAMILY_SECP_R1, 256, EC_NAME("secp256r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005514#endif
5515#if defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED) || defined(PSA_WANT_ECC_SECP_K1_256)
Gilles Peskine449bd832023-01-11 14:50:10 +01005516 { 22, MBEDTLS_ECP_DP_SECP256K1, PSA_ECC_FAMILY_SECP_K1, 256, EC_NAME("secp256k1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005517#endif
Valerio Setti67419f02023-01-04 16:12:42 +01005518#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED) || defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256)
Gilles Peskine449bd832023-01-11 14:50:10 +01005519 { 26, MBEDTLS_ECP_DP_BP256R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 256, EC_NAME("brainpoolP256r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005520#endif
5521#if defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_224)
Gilles Peskine449bd832023-01-11 14:50:10 +01005522 { 21, MBEDTLS_ECP_DP_SECP224R1, PSA_ECC_FAMILY_SECP_R1, 224, EC_NAME("secp224r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005523#endif
5524#if defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED) || defined(PSA_WANT_ECC_SECP_K1_224)
Gilles Peskine449bd832023-01-11 14:50:10 +01005525 { 20, MBEDTLS_ECP_DP_SECP224K1, PSA_ECC_FAMILY_SECP_K1, 224, EC_NAME("secp224k1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005526#endif
5527#if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_192)
Gilles Peskine449bd832023-01-11 14:50:10 +01005528 { 19, MBEDTLS_ECP_DP_SECP192R1, PSA_ECC_FAMILY_SECP_R1, 192, EC_NAME("secp192r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005529#endif
5530#if defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED) || defined(PSA_WANT_ECC_SECP_K1_192)
Gilles Peskine449bd832023-01-11 14:50:10 +01005531 { 18, MBEDTLS_ECP_DP_SECP192K1, PSA_ECC_FAMILY_SECP_K1, 192, EC_NAME("secp192k1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005532#endif
5533#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED) || defined(PSA_WANT_ECC_MONTGOMERY_255)
Gilles Peskine449bd832023-01-11 14:50:10 +01005534 { 29, MBEDTLS_ECP_DP_CURVE25519, PSA_ECC_FAMILY_MONTGOMERY, 255, EC_NAME("x25519") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005535#endif
5536#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED) || defined(PSA_WANT_ECC_MONTGOMERY_448)
Gilles Peskine449bd832023-01-11 14:50:10 +01005537 { 30, MBEDTLS_ECP_DP_CURVE448, PSA_ECC_FAMILY_MONTGOMERY, 448, EC_NAME("x448") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005538#endif
5539 { 0, MBEDTLS_ECP_DP_NONE, 0, 0, NULL },
5540};
5541
Gilles Peskine449bd832023-01-11 14:50:10 +01005542int mbedtls_ssl_get_psa_curve_info_from_tls_id(uint16_t tls_id,
5543 psa_ecc_family_t *family,
5544 size_t *bits)
Valerio Setti18c9fed2022-12-30 17:44:24 +01005545{
Gilles Peskine449bd832023-01-11 14:50:10 +01005546 for (int i = 0; tls_id_match_table[i].tls_id != 0; i++) {
5547 if (tls_id_match_table[i].tls_id == tls_id) {
5548 if (family != NULL) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01005549 *family = tls_id_match_table[i].psa_family;
Gilles Peskine449bd832023-01-11 14:50:10 +01005550 }
5551 if (bits != NULL) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01005552 *bits = tls_id_match_table[i].bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01005553 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01005554 return PSA_SUCCESS;
5555 }
5556 }
5557
5558 return PSA_ERROR_NOT_SUPPORTED;
5559}
5560
Gilles Peskine449bd832023-01-11 14:50:10 +01005561mbedtls_ecp_group_id mbedtls_ssl_get_ecp_group_id_from_tls_id(uint16_t tls_id)
Valerio Setti18c9fed2022-12-30 17:44:24 +01005562{
Gilles Peskine449bd832023-01-11 14:50:10 +01005563 for (int i = 0; tls_id_match_table[i].tls_id != 0; i++) {
5564 if (tls_id_match_table[i].tls_id == tls_id) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01005565 return tls_id_match_table[i].ecp_group_id;
Gilles Peskine449bd832023-01-11 14:50:10 +01005566 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01005567 }
5568
5569 return MBEDTLS_ECP_DP_NONE;
5570}
5571
Gilles Peskine449bd832023-01-11 14:50:10 +01005572uint16_t mbedtls_ssl_get_tls_id_from_ecp_group_id(mbedtls_ecp_group_id grp_id)
Valerio Setti18c9fed2022-12-30 17:44:24 +01005573{
Gilles Peskine449bd832023-01-11 14:50:10 +01005574 for (int i = 0; tls_id_match_table[i].ecp_group_id != MBEDTLS_ECP_DP_NONE;
5575 i++) {
5576 if (tls_id_match_table[i].ecp_group_id == grp_id) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01005577 return tls_id_match_table[i].tls_id;
Gilles Peskine449bd832023-01-11 14:50:10 +01005578 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01005579 }
5580
5581 return 0;
5582}
5583
Valerio Setti67419f02023-01-04 16:12:42 +01005584#if defined(MBEDTLS_DEBUG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005585const char *mbedtls_ssl_get_curve_name_from_tls_id(uint16_t tls_id)
Valerio Setti18c9fed2022-12-30 17:44:24 +01005586{
Gilles Peskine449bd832023-01-11 14:50:10 +01005587 for (int i = 0; tls_id_match_table[i].tls_id != 0; i++) {
5588 if (tls_id_match_table[i].tls_id == tls_id) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01005589 return tls_id_match_table[i].name;
Gilles Peskine449bd832023-01-11 14:50:10 +01005590 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01005591 }
5592
5593 return NULL;
5594}
Valerio Setti67419f02023-01-04 16:12:42 +01005595#endif
Valerio Setti18c9fed2022-12-30 17:44:24 +01005596
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005597#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005598int mbedtls_ssl_check_cert_usage(const mbedtls_x509_crt *cert,
5599 const mbedtls_ssl_ciphersuite_t *ciphersuite,
5600 int cert_endpoint,
5601 uint32_t *flags)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005602{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005603 int ret = 0;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005604 int usage = 0;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005605 const char *ext_oid;
5606 size_t ext_len;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005607
Gilles Peskine449bd832023-01-11 14:50:10 +01005608 if (cert_endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005609 /* Server part of the key exchange */
Gilles Peskine449bd832023-01-11 14:50:10 +01005610 switch (ciphersuite->key_exchange) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005611 case MBEDTLS_KEY_EXCHANGE_RSA:
5612 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005613 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005614 break;
5615
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005616 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
5617 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
5618 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
5619 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005620 break;
5621
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005622 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
5623 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005624 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005625 break;
5626
5627 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005628 case MBEDTLS_KEY_EXCHANGE_NONE:
5629 case MBEDTLS_KEY_EXCHANGE_PSK:
5630 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
5631 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02005632 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005633 usage = 0;
5634 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005635 } else {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005636 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
5637 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005638 }
5639
Gilles Peskine449bd832023-01-11 14:50:10 +01005640 if (mbedtls_x509_crt_check_key_usage(cert, usage) != 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005641 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005642 ret = -1;
5643 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005644
Gilles Peskine449bd832023-01-11 14:50:10 +01005645 if (cert_endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005646 ext_oid = MBEDTLS_OID_SERVER_AUTH;
Gilles Peskine449bd832023-01-11 14:50:10 +01005647 ext_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_SERVER_AUTH);
5648 } else {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005649 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
Gilles Peskine449bd832023-01-11 14:50:10 +01005650 ext_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_CLIENT_AUTH);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005651 }
5652
Gilles Peskine449bd832023-01-11 14:50:10 +01005653 if (mbedtls_x509_crt_check_extended_key_usage(cert, ext_oid, ext_len) != 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005654 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005655 ret = -1;
5656 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005657
Gilles Peskine449bd832023-01-11 14:50:10 +01005658 return ret;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005659}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005660#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02005661
Jerry Yu148165c2021-09-24 23:20:59 +08005662#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01005663int mbedtls_ssl_get_handshake_transcript(mbedtls_ssl_context *ssl,
5664 const mbedtls_md_type_t md,
5665 unsigned char *dst,
5666 size_t dst_len,
5667 size_t *olen)
Jerry Yu148165c2021-09-24 23:20:59 +08005668{
Ronald Cronf6893e12022-01-07 22:09:01 +01005669 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5670 psa_hash_operation_t *hash_operation_to_clone;
5671 psa_hash_operation_t hash_operation = psa_hash_operation_init();
5672
Jerry Yu148165c2021-09-24 23:20:59 +08005673 *olen = 0;
Ronald Cronf6893e12022-01-07 22:09:01 +01005674
Gilles Peskine449bd832023-01-11 14:50:10 +01005675 switch (md) {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005676#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01005677 case MBEDTLS_MD_SHA384:
5678 hash_operation_to_clone = &ssl->handshake->fin_sha384_psa;
5679 break;
Ronald Cronf6893e12022-01-07 22:09:01 +01005680#endif
5681
Andrzej Kurek25f27152022-08-17 16:09:31 -04005682#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01005683 case MBEDTLS_MD_SHA256:
5684 hash_operation_to_clone = &ssl->handshake->fin_sha256_psa;
5685 break;
Ronald Cronf6893e12022-01-07 22:09:01 +01005686#endif
5687
Gilles Peskine449bd832023-01-11 14:50:10 +01005688 default:
5689 goto exit;
5690 }
5691
5692 status = psa_hash_clone(hash_operation_to_clone, &hash_operation);
5693 if (status != PSA_SUCCESS) {
Ronald Cronf6893e12022-01-07 22:09:01 +01005694 goto exit;
5695 }
5696
Gilles Peskine449bd832023-01-11 14:50:10 +01005697 status = psa_hash_finish(&hash_operation, dst, dst_len, olen);
5698 if (status != PSA_SUCCESS) {
Ronald Cronf6893e12022-01-07 22:09:01 +01005699 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005700 }
Ronald Cronf6893e12022-01-07 22:09:01 +01005701
5702exit:
Andrzej Kurekeabeb302022-10-17 07:52:51 -04005703#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
5704 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
5705 (void) ssl;
5706#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005707 return psa_ssl_status_to_mbedtls(status);
Jerry Yu148165c2021-09-24 23:20:59 +08005708}
5709#else /* MBEDTLS_USE_PSA_CRYPTO */
5710
Andrzej Kurek25f27152022-08-17 16:09:31 -04005711#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005712MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01005713static int ssl_get_handshake_transcript_sha384(mbedtls_ssl_context *ssl,
5714 unsigned char *dst,
5715 size_t dst_len,
5716 size_t *olen)
Jerry Yu24c0ec32021-09-09 14:21:07 +08005717{
Jerry Yu24c0ec32021-09-09 14:21:07 +08005718 int ret;
5719 mbedtls_sha512_context sha512;
5720
Gilles Peskine449bd832023-01-11 14:50:10 +01005721 if (dst_len < 48) {
5722 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
5723 }
Jerry Yu24c0ec32021-09-09 14:21:07 +08005724
Gilles Peskine449bd832023-01-11 14:50:10 +01005725 mbedtls_sha512_init(&sha512);
5726 mbedtls_sha512_clone(&sha512, &ssl->handshake->fin_sha384);
Jerry Yu24c0ec32021-09-09 14:21:07 +08005727
Gilles Peskine449bd832023-01-11 14:50:10 +01005728 if ((ret = mbedtls_sha512_finish(&sha512, dst)) != 0) {
5729 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_sha512_finish", ret);
Jerry Yu24c0ec32021-09-09 14:21:07 +08005730 goto exit;
5731 }
5732
5733 *olen = 48;
5734
5735exit:
5736
Gilles Peskine449bd832023-01-11 14:50:10 +01005737 mbedtls_sha512_free(&sha512);
5738 return ret;
Jerry Yu24c0ec32021-09-09 14:21:07 +08005739}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005740#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005741
Andrzej Kurek25f27152022-08-17 16:09:31 -04005742#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005743MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01005744static int ssl_get_handshake_transcript_sha256(mbedtls_ssl_context *ssl,
5745 unsigned char *dst,
5746 size_t dst_len,
5747 size_t *olen)
Jerry Yu24c0ec32021-09-09 14:21:07 +08005748{
Jerry Yu24c0ec32021-09-09 14:21:07 +08005749 int ret;
5750 mbedtls_sha256_context sha256;
5751
Gilles Peskine449bd832023-01-11 14:50:10 +01005752 if (dst_len < 32) {
5753 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
5754 }
Jerry Yu24c0ec32021-09-09 14:21:07 +08005755
Gilles Peskine449bd832023-01-11 14:50:10 +01005756 mbedtls_sha256_init(&sha256);
5757 mbedtls_sha256_clone(&sha256, &ssl->handshake->fin_sha256);
Jerry Yuc5aef882021-12-23 20:15:02 +08005758
Gilles Peskine449bd832023-01-11 14:50:10 +01005759 if ((ret = mbedtls_sha256_finish(&sha256, dst)) != 0) {
5760 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_sha256_finish", ret);
Jerry Yu24c0ec32021-09-09 14:21:07 +08005761 goto exit;
5762 }
5763
5764 *olen = 32;
5765
5766exit:
5767
Gilles Peskine449bd832023-01-11 14:50:10 +01005768 mbedtls_sha256_free(&sha256);
5769 return ret;
Jerry Yu24c0ec32021-09-09 14:21:07 +08005770}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005771#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005772
Gilles Peskine449bd832023-01-11 14:50:10 +01005773int mbedtls_ssl_get_handshake_transcript(mbedtls_ssl_context *ssl,
5774 const mbedtls_md_type_t md,
5775 unsigned char *dst,
5776 size_t dst_len,
5777 size_t *olen)
Jerry Yu24c0ec32021-09-09 14:21:07 +08005778{
Gilles Peskine449bd832023-01-11 14:50:10 +01005779 switch (md) {
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005780
Andrzej Kurek25f27152022-08-17 16:09:31 -04005781#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01005782 case MBEDTLS_MD_SHA384:
5783 return ssl_get_handshake_transcript_sha384(ssl, dst, dst_len, olen);
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005784#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005785
Andrzej Kurek25f27152022-08-17 16:09:31 -04005786#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01005787 case MBEDTLS_MD_SHA256:
5788 return ssl_get_handshake_transcript_sha256(ssl, dst, dst_len, olen);
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005789#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005790
Gilles Peskine449bd832023-01-11 14:50:10 +01005791 default:
Andrzej Kurek409248a2022-10-24 10:33:21 -04005792#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01005793 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
5794 (void) ssl;
5795 (void) dst;
5796 (void) dst_len;
5797 (void) olen;
Andrzej Kurek409248a2022-10-24 10:33:21 -04005798#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005799 break;
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005800 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005801 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu24c0ec32021-09-09 14:21:07 +08005802}
XiaokangQian647719a2021-12-07 09:16:29 +00005803
Jerry Yu148165c2021-09-24 23:20:59 +08005804#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005805
Ronald Crone68ab4f2022-10-05 12:46:29 +02005806#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Gabor Mezei078e8032022-04-27 21:17:56 +02005807/* mbedtls_ssl_parse_sig_alg_ext()
5808 *
5809 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
5810 * value (TLS 1.3 RFC8446):
5811 * enum {
5812 * ....
5813 * ecdsa_secp256r1_sha256( 0x0403 ),
5814 * ecdsa_secp384r1_sha384( 0x0503 ),
5815 * ecdsa_secp521r1_sha512( 0x0603 ),
5816 * ....
5817 * } SignatureScheme;
5818 *
5819 * struct {
5820 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
5821 * } SignatureSchemeList;
5822 *
5823 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
5824 * value (TLS 1.2 RFC5246):
5825 * enum {
5826 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
5827 * sha512(6), (255)
5828 * } HashAlgorithm;
5829 *
5830 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
5831 * SignatureAlgorithm;
5832 *
5833 * struct {
5834 * HashAlgorithm hash;
5835 * SignatureAlgorithm signature;
5836 * } SignatureAndHashAlgorithm;
5837 *
5838 * SignatureAndHashAlgorithm
5839 * supported_signature_algorithms<2..2^16-2>;
5840 *
5841 * The TLS 1.3 signature algorithm extension was defined to be a compatible
5842 * generalization of the TLS 1.2 signature algorithm extension.
5843 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
5844 * `SignatureScheme` field of TLS 1.3
5845 *
5846 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005847int mbedtls_ssl_parse_sig_alg_ext(mbedtls_ssl_context *ssl,
5848 const unsigned char *buf,
5849 const unsigned char *end)
Gabor Mezei078e8032022-04-27 21:17:56 +02005850{
5851 const unsigned char *p = buf;
5852 size_t supported_sig_algs_len = 0;
5853 const unsigned char *supported_sig_algs_end;
5854 uint16_t sig_alg;
5855 uint32_t common_idx = 0;
5856
Gilles Peskine449bd832023-01-11 14:50:10 +01005857 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
5858 supported_sig_algs_len = MBEDTLS_GET_UINT16_BE(p, 0);
Gabor Mezei078e8032022-04-27 21:17:56 +02005859 p += 2;
5860
Gilles Peskine449bd832023-01-11 14:50:10 +01005861 memset(ssl->handshake->received_sig_algs, 0,
5862 sizeof(ssl->handshake->received_sig_algs));
Gabor Mezei078e8032022-04-27 21:17:56 +02005863
Gilles Peskine449bd832023-01-11 14:50:10 +01005864 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, supported_sig_algs_len);
Gabor Mezei078e8032022-04-27 21:17:56 +02005865 supported_sig_algs_end = p + supported_sig_algs_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01005866 while (p < supported_sig_algs_end) {
5867 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, supported_sig_algs_end, 2);
5868 sig_alg = MBEDTLS_GET_UINT16_BE(p, 0);
Gabor Mezei078e8032022-04-27 21:17:56 +02005869 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01005870 MBEDTLS_SSL_DEBUG_MSG(4, ("received signature algorithm: 0x%x %s",
5871 sig_alg,
5872 mbedtls_ssl_sig_alg_to_str(sig_alg)));
Jerry Yu2fe6c632022-06-29 10:02:38 +08005873#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01005874 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 &&
5875 (!(mbedtls_ssl_sig_alg_is_supported(ssl, sig_alg) &&
5876 mbedtls_ssl_sig_alg_is_offered(ssl, sig_alg)))) {
Gabor Mezei078e8032022-04-27 21:17:56 +02005877 continue;
Jerry Yu2fe6c632022-06-29 10:02:38 +08005878 }
5879#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gabor Mezei078e8032022-04-27 21:17:56 +02005880
Gilles Peskine449bd832023-01-11 14:50:10 +01005881 MBEDTLS_SSL_DEBUG_MSG(4, ("valid signature algorithm: %s",
5882 mbedtls_ssl_sig_alg_to_str(sig_alg)));
Gabor Mezei078e8032022-04-27 21:17:56 +02005883
Gilles Peskine449bd832023-01-11 14:50:10 +01005884 if (common_idx + 1 < MBEDTLS_RECEIVED_SIG_ALGS_SIZE) {
Gabor Mezei078e8032022-04-27 21:17:56 +02005885 ssl->handshake->received_sig_algs[common_idx] = sig_alg;
5886 common_idx += 1;
5887 }
5888 }
5889 /* Check that we consumed all the message. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005890 if (p != end) {
5891 MBEDTLS_SSL_DEBUG_MSG(1,
5892 ("Signature algorithms extension length misaligned"));
5893 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
5894 MBEDTLS_ERR_SSL_DECODE_ERROR);
5895 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Gabor Mezei078e8032022-04-27 21:17:56 +02005896 }
5897
Gilles Peskine449bd832023-01-11 14:50:10 +01005898 if (common_idx == 0) {
5899 MBEDTLS_SSL_DEBUG_MSG(3, ("no signature algorithm in common"));
5900 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
5901 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
5902 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Gabor Mezei078e8032022-04-27 21:17:56 +02005903 }
5904
Gabor Mezei15b95a62022-05-09 16:37:58 +02005905 ssl->handshake->received_sig_algs[common_idx] = MBEDTLS_TLS_SIG_NONE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005906 return 0;
Gabor Mezei078e8032022-04-27 21:17:56 +02005907}
5908
Ronald Crone68ab4f2022-10-05 12:46:29 +02005909#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Gabor Mezei078e8032022-04-27 21:17:56 +02005910
Jerry Yudc7bd172022-02-17 13:44:15 +08005911#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5912
5913#if defined(MBEDTLS_USE_PSA_CRYPTO)
5914
Gilles Peskine449bd832023-01-11 14:50:10 +01005915static psa_status_t setup_psa_key_derivation(psa_key_derivation_operation_t *derivation,
5916 mbedtls_svc_key_id_t key,
5917 psa_algorithm_t alg,
5918 const unsigned char *raw_psk, size_t raw_psk_length,
5919 const unsigned char *seed, size_t seed_length,
5920 const unsigned char *label, size_t label_length,
5921 const unsigned char *other_secret,
5922 size_t other_secret_length,
5923 size_t capacity)
Jerry Yudc7bd172022-02-17 13:44:15 +08005924{
5925 psa_status_t status;
5926
Gilles Peskine449bd832023-01-11 14:50:10 +01005927 status = psa_key_derivation_setup(derivation, alg);
5928 if (status != PSA_SUCCESS) {
5929 return status;
5930 }
Jerry Yudc7bd172022-02-17 13:44:15 +08005931
Gilles Peskine449bd832023-01-11 14:50:10 +01005932 if (PSA_ALG_IS_TLS12_PRF(alg) || PSA_ALG_IS_TLS12_PSK_TO_MS(alg)) {
5933 status = psa_key_derivation_input_bytes(derivation,
5934 PSA_KEY_DERIVATION_INPUT_SEED,
5935 seed, seed_length);
5936 if (status != PSA_SUCCESS) {
5937 return status;
Przemek Stekiel1f027032022-04-05 17:12:11 +02005938 }
5939
Gilles Peskine449bd832023-01-11 14:50:10 +01005940 if (other_secret != NULL) {
5941 status = psa_key_derivation_input_bytes(derivation,
5942 PSA_KEY_DERIVATION_INPUT_OTHER_SECRET,
5943 other_secret, other_secret_length);
5944 if (status != PSA_SUCCESS) {
5945 return status;
5946 }
5947 }
5948
5949 if (mbedtls_svc_key_id_is_null(key)) {
Jerry Yudc7bd172022-02-17 13:44:15 +08005950 status = psa_key_derivation_input_bytes(
5951 derivation, PSA_KEY_DERIVATION_INPUT_SECRET,
Gilles Peskine449bd832023-01-11 14:50:10 +01005952 raw_psk, raw_psk_length);
5953 } else {
Jerry Yudc7bd172022-02-17 13:44:15 +08005954 status = psa_key_derivation_input_key(
Gilles Peskine449bd832023-01-11 14:50:10 +01005955 derivation, PSA_KEY_DERIVATION_INPUT_SECRET, key);
Jerry Yudc7bd172022-02-17 13:44:15 +08005956 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005957 if (status != PSA_SUCCESS) {
5958 return status;
5959 }
Jerry Yudc7bd172022-02-17 13:44:15 +08005960
Gilles Peskine449bd832023-01-11 14:50:10 +01005961 status = psa_key_derivation_input_bytes(derivation,
5962 PSA_KEY_DERIVATION_INPUT_LABEL,
5963 label, label_length);
5964 if (status != PSA_SUCCESS) {
5965 return status;
5966 }
5967 } else {
5968 return PSA_ERROR_NOT_SUPPORTED;
Jerry Yudc7bd172022-02-17 13:44:15 +08005969 }
5970
Gilles Peskine449bd832023-01-11 14:50:10 +01005971 status = psa_key_derivation_set_capacity(derivation, capacity);
5972 if (status != PSA_SUCCESS) {
5973 return status;
5974 }
Jerry Yudc7bd172022-02-17 13:44:15 +08005975
Gilles Peskine449bd832023-01-11 14:50:10 +01005976 return PSA_SUCCESS;
Jerry Yudc7bd172022-02-17 13:44:15 +08005977}
5978
Andrzej Kurek57d10632022-10-24 10:32:01 -04005979#if defined(PSA_WANT_ALG_SHA_384) || \
5980 defined(PSA_WANT_ALG_SHA_256)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005981MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01005982static int tls_prf_generic(mbedtls_md_type_t md_type,
5983 const unsigned char *secret, size_t slen,
5984 const char *label,
5985 const unsigned char *random, size_t rlen,
5986 unsigned char *dstbuf, size_t dlen)
Jerry Yudc7bd172022-02-17 13:44:15 +08005987{
5988 psa_status_t status;
5989 psa_algorithm_t alg;
5990 mbedtls_svc_key_id_t master_key = MBEDTLS_SVC_KEY_ID_INIT;
5991 psa_key_derivation_operation_t derivation =
5992 PSA_KEY_DERIVATION_OPERATION_INIT;
5993
Gilles Peskine449bd832023-01-11 14:50:10 +01005994 if (md_type == MBEDTLS_MD_SHA384) {
Jerry Yudc7bd172022-02-17 13:44:15 +08005995 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
Gilles Peskine449bd832023-01-11 14:50:10 +01005996 } else {
Jerry Yudc7bd172022-02-17 13:44:15 +08005997 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
Gilles Peskine449bd832023-01-11 14:50:10 +01005998 }
Jerry Yudc7bd172022-02-17 13:44:15 +08005999
6000 /* Normally a "secret" should be long enough to be impossible to
6001 * find by brute force, and in particular should not be empty. But
6002 * this PRF is also used to derive an IV, in particular in EAP-TLS,
6003 * and for this use case it makes sense to have a 0-length "secret".
6004 * Since the key API doesn't allow importing a key of length 0,
6005 * keep master_key=0, which setup_psa_key_derivation() understands
6006 * to mean a 0-length "secret" input. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006007 if (slen != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006008 psa_key_attributes_t key_attributes = psa_key_attributes_init();
Gilles Peskine449bd832023-01-11 14:50:10 +01006009 psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE);
6010 psa_set_key_algorithm(&key_attributes, alg);
6011 psa_set_key_type(&key_attributes, PSA_KEY_TYPE_DERIVE);
Jerry Yudc7bd172022-02-17 13:44:15 +08006012
Gilles Peskine449bd832023-01-11 14:50:10 +01006013 status = psa_import_key(&key_attributes, secret, slen, &master_key);
6014 if (status != PSA_SUCCESS) {
6015 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
6016 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006017 }
6018
Gilles Peskine449bd832023-01-11 14:50:10 +01006019 status = setup_psa_key_derivation(&derivation,
6020 master_key, alg,
6021 NULL, 0,
6022 random, rlen,
6023 (unsigned char const *) label,
6024 (size_t) strlen(label),
6025 NULL, 0,
6026 dlen);
6027 if (status != PSA_SUCCESS) {
6028 psa_key_derivation_abort(&derivation);
6029 psa_destroy_key(master_key);
6030 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yudc7bd172022-02-17 13:44:15 +08006031 }
6032
Gilles Peskine449bd832023-01-11 14:50:10 +01006033 status = psa_key_derivation_output_bytes(&derivation, dstbuf, dlen);
6034 if (status != PSA_SUCCESS) {
6035 psa_key_derivation_abort(&derivation);
6036 psa_destroy_key(master_key);
6037 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yudc7bd172022-02-17 13:44:15 +08006038 }
6039
Gilles Peskine449bd832023-01-11 14:50:10 +01006040 status = psa_key_derivation_abort(&derivation);
6041 if (status != PSA_SUCCESS) {
6042 psa_destroy_key(master_key);
6043 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yudc7bd172022-02-17 13:44:15 +08006044 }
6045
Gilles Peskine449bd832023-01-11 14:50:10 +01006046 if (!mbedtls_svc_key_id_is_null(master_key)) {
6047 status = psa_destroy_key(master_key);
6048 }
6049 if (status != PSA_SUCCESS) {
6050 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
6051 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006052
Gilles Peskine449bd832023-01-11 14:50:10 +01006053 return 0;
Jerry Yudc7bd172022-02-17 13:44:15 +08006054}
Andrzej Kurek57d10632022-10-24 10:32:01 -04006055#endif /* PSA_WANT_ALG_SHA_256 || PSA_WANT_ALG_SHA_384 */
Jerry Yudc7bd172022-02-17 13:44:15 +08006056#else /* MBEDTLS_USE_PSA_CRYPTO */
6057
Andrzej Kurek57d10632022-10-24 10:32:01 -04006058#if defined(MBEDTLS_MD_C) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01006059 (defined(MBEDTLS_SHA256_C) || \
6060 defined(MBEDTLS_SHA384_C))
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006061MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006062static int tls_prf_generic(mbedtls_md_type_t md_type,
6063 const unsigned char *secret, size_t slen,
6064 const char *label,
6065 const unsigned char *random, size_t rlen,
6066 unsigned char *dstbuf, size_t dlen)
Jerry Yudc7bd172022-02-17 13:44:15 +08006067{
6068 size_t nb;
6069 size_t i, j, k, md_len;
6070 unsigned char *tmp;
6071 size_t tmp_len = 0;
6072 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
6073 const mbedtls_md_info_t *md_info;
6074 mbedtls_md_context_t md_ctx;
6075 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6076
Gilles Peskine449bd832023-01-11 14:50:10 +01006077 mbedtls_md_init(&md_ctx);
Jerry Yudc7bd172022-02-17 13:44:15 +08006078
Gilles Peskine449bd832023-01-11 14:50:10 +01006079 if ((md_info = mbedtls_md_info_from_type(md_type)) == NULL) {
6080 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
6081 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006082
Gilles Peskine449bd832023-01-11 14:50:10 +01006083 md_len = mbedtls_md_get_size(md_info);
Jerry Yudc7bd172022-02-17 13:44:15 +08006084
Gilles Peskine449bd832023-01-11 14:50:10 +01006085 tmp_len = md_len + strlen(label) + rlen;
6086 tmp = mbedtls_calloc(1, tmp_len);
6087 if (tmp == NULL) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006088 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
6089 goto exit;
6090 }
6091
Gilles Peskine449bd832023-01-11 14:50:10 +01006092 nb = strlen(label);
6093 memcpy(tmp + md_len, label, nb);
6094 memcpy(tmp + md_len + nb, random, rlen);
Jerry Yudc7bd172022-02-17 13:44:15 +08006095 nb += rlen;
6096
6097 /*
6098 * Compute P_<hash>(secret, label + random)[0..dlen]
6099 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006100 if ((ret = mbedtls_md_setup(&md_ctx, md_info, 1)) != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006101 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006102 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006103
Gilles Peskine449bd832023-01-11 14:50:10 +01006104 ret = mbedtls_md_hmac_starts(&md_ctx, secret, slen);
6105 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006106 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006107 }
6108 ret = mbedtls_md_hmac_update(&md_ctx, tmp + md_len, nb);
6109 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006110 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006111 }
6112 ret = mbedtls_md_hmac_finish(&md_ctx, tmp);
6113 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006114 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006115 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006116
Gilles Peskine449bd832023-01-11 14:50:10 +01006117 for (i = 0; i < dlen; i += md_len) {
6118 ret = mbedtls_md_hmac_reset(&md_ctx);
6119 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006120 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006121 }
6122 ret = mbedtls_md_hmac_update(&md_ctx, tmp, md_len + nb);
6123 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006124 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006125 }
6126 ret = mbedtls_md_hmac_finish(&md_ctx, h_i);
6127 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006128 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006129 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006130
Gilles Peskine449bd832023-01-11 14:50:10 +01006131 ret = mbedtls_md_hmac_reset(&md_ctx);
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_update(&md_ctx, tmp, md_len);
6136 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006137 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006138 }
6139 ret = mbedtls_md_hmac_finish(&md_ctx, tmp);
6140 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006141 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006142 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006143
Gilles Peskine449bd832023-01-11 14:50:10 +01006144 k = (i + md_len > dlen) ? dlen % md_len : md_len;
Jerry Yudc7bd172022-02-17 13:44:15 +08006145
Gilles Peskine449bd832023-01-11 14:50:10 +01006146 for (j = 0; j < k; j++) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006147 dstbuf[i + j] = h_i[j];
Gilles Peskine449bd832023-01-11 14:50:10 +01006148 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006149 }
6150
6151exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006152 mbedtls_md_free(&md_ctx);
Jerry Yudc7bd172022-02-17 13:44:15 +08006153
Gilles Peskine449bd832023-01-11 14:50:10 +01006154 if (tmp != NULL) {
6155 mbedtls_platform_zeroize(tmp, tmp_len);
6156 }
Dave Rodgman29b9b2b2022-11-01 16:08:14 +00006157
Gilles Peskine449bd832023-01-11 14:50:10 +01006158 mbedtls_platform_zeroize(h_i, sizeof(h_i));
Jerry Yudc7bd172022-02-17 13:44:15 +08006159
Gilles Peskine449bd832023-01-11 14:50:10 +01006160 mbedtls_free(tmp);
Jerry Yudc7bd172022-02-17 13:44:15 +08006161
Gilles Peskine449bd832023-01-11 14:50:10 +01006162 return ret;
Jerry Yudc7bd172022-02-17 13:44:15 +08006163}
Andrzej Kurek57d10632022-10-24 10:32:01 -04006164#endif /* MBEDTLS_MD_C && ( MBEDTLS_SHA256_C || MBEDTLS_SHA384_C ) */
Jerry Yudc7bd172022-02-17 13:44:15 +08006165#endif /* MBEDTLS_USE_PSA_CRYPTO */
6166
Andrzej Kurek25f27152022-08-17 16:09:31 -04006167#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006168MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006169static int tls_prf_sha256(const unsigned char *secret, size_t slen,
6170 const char *label,
6171 const unsigned char *random, size_t rlen,
6172 unsigned char *dstbuf, size_t dlen)
Jerry Yudc7bd172022-02-17 13:44:15 +08006173{
Gilles Peskine449bd832023-01-11 14:50:10 +01006174 return tls_prf_generic(MBEDTLS_MD_SHA256, secret, slen,
6175 label, random, rlen, dstbuf, dlen);
Jerry Yudc7bd172022-02-17 13:44:15 +08006176}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04006177#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudc7bd172022-02-17 13:44:15 +08006178
Andrzej Kurek25f27152022-08-17 16:09:31 -04006179#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006180MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006181static int tls_prf_sha384(const unsigned char *secret, size_t slen,
6182 const char *label,
6183 const unsigned char *random, size_t rlen,
6184 unsigned char *dstbuf, size_t dlen)
Jerry Yudc7bd172022-02-17 13:44:15 +08006185{
Gilles Peskine449bd832023-01-11 14:50:10 +01006186 return tls_prf_generic(MBEDTLS_MD_SHA384, secret, slen,
6187 label, random, rlen, dstbuf, dlen);
Jerry Yudc7bd172022-02-17 13:44:15 +08006188}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04006189#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudc7bd172022-02-17 13:44:15 +08006190
Jerry Yuf009d862022-02-17 14:01:37 +08006191/*
6192 * Set appropriate PRF function and other SSL / TLS1.2 functions
6193 *
6194 * Inputs:
Jerry Yuf009d862022-02-17 14:01:37 +08006195 * - hash associated with the ciphersuite (only used by TLS 1.2)
6196 *
6197 * Outputs:
6198 * - the tls_prf, calc_verify and calc_finished members of handshake structure
6199 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006200MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006201static int ssl_set_handshake_prfs(mbedtls_ssl_handshake_params *handshake,
6202 mbedtls_md_type_t hash)
Jerry Yuf009d862022-02-17 14:01:37 +08006203{
Andrzej Kurek25f27152022-08-17 16:09:31 -04006204#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01006205 if (hash == MBEDTLS_MD_SHA384) {
Jerry Yuf009d862022-02-17 14:01:37 +08006206 handshake->tls_prf = tls_prf_sha384;
6207 handshake->calc_verify = ssl_calc_verify_tls_sha384;
6208 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Gilles Peskine449bd832023-01-11 14:50:10 +01006209 } else
Jerry Yuf009d862022-02-17 14:01:37 +08006210#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04006211#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuf009d862022-02-17 14:01:37 +08006212 {
Ronald Cron81591aa2022-03-07 09:05:51 +01006213 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08006214 handshake->tls_prf = tls_prf_sha256;
6215 handshake->calc_verify = ssl_calc_verify_tls_sha256;
6216 handshake->calc_finished = ssl_calc_finished_tls_sha256;
6217 }
Ronald Cron81591aa2022-03-07 09:05:51 +01006218#else
Jerry Yuf009d862022-02-17 14:01:37 +08006219 {
Jerry Yuf009d862022-02-17 14:01:37 +08006220 (void) handshake;
Ronald Cron81591aa2022-03-07 09:05:51 +01006221 (void) hash;
Gilles Peskine449bd832023-01-11 14:50:10 +01006222 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yuf009d862022-02-17 14:01:37 +08006223 }
Ronald Cron81591aa2022-03-07 09:05:51 +01006224#endif
Jerry Yuf009d862022-02-17 14:01:37 +08006225
Gilles Peskine449bd832023-01-11 14:50:10 +01006226 return 0;
Jerry Yuf009d862022-02-17 14:01:37 +08006227}
Jerry Yud6ab2352022-02-17 14:03:43 +08006228
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006229/*
6230 * Compute master secret if needed
6231 *
6232 * Parameters:
6233 * [in/out] handshake
6234 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
6235 * (PSA-PSK) ciphersuite_info, psk_opaque
6236 * [out] premaster (cleared)
6237 * [out] master
6238 * [in] ssl: optionally used for debugging, EMS and PSA-PSK
6239 * debug: conf->f_dbg, conf->p_dbg
6240 * EMS: passed to calc_verify (debug + session_negotiate)
Ronald Crona25cf582022-03-07 11:10:36 +01006241 * PSA-PSA: conf
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006242 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006243MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006244static int ssl_compute_master(mbedtls_ssl_handshake_params *handshake,
6245 unsigned char *master,
6246 const mbedtls_ssl_context *ssl)
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006247{
6248 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6249
6250 /* cf. RFC 5246, Section 8.1:
6251 * "The master secret is always exactly 48 bytes in length." */
6252 size_t const master_secret_len = 48;
6253
6254#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
6255 unsigned char session_hash[48];
6256#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
6257
6258 /* The label for the KDF used for key expansion.
6259 * This is either "master secret" or "extended master secret"
6260 * depending on whether the Extended Master Secret extension
6261 * is used. */
6262 char const *lbl = "master secret";
6263
Przemek Stekielae4ed302022-04-05 17:15:55 +02006264 /* The seed for the KDF used for key expansion.
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006265 * - If the Extended Master Secret extension is not used,
6266 * this is ClientHello.Random + ServerHello.Random
6267 * (see Sect. 8.1 in RFC 5246).
6268 * - If the Extended Master Secret extension is used,
6269 * this is the transcript of the handshake so far.
6270 * (see Sect. 4 in RFC 7627). */
Przemek Stekielae4ed302022-04-05 17:15:55 +02006271 unsigned char const *seed = handshake->randbytes;
6272 size_t seed_len = 64;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006273
6274#if !defined(MBEDTLS_DEBUG_C) && \
6275 !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \
6276 !(defined(MBEDTLS_USE_PSA_CRYPTO) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01006277 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED))
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006278 ssl = NULL; /* make sure we don't use it except for those cases */
6279 (void) ssl;
6280#endif
6281
Gilles Peskine449bd832023-01-11 14:50:10 +01006282 if (handshake->resume != 0) {
6283 MBEDTLS_SSL_DEBUG_MSG(3, ("no premaster (session resumed)"));
6284 return 0;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006285 }
6286
6287#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Gilles Peskine449bd832023-01-11 14:50:10 +01006288 if (handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED) {
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006289 lbl = "extended master secret";
Przemek Stekielae4ed302022-04-05 17:15:55 +02006290 seed = session_hash;
Gilles Peskine449bd832023-01-11 14:50:10 +01006291 handshake->calc_verify(ssl, session_hash, &seed_len);
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006292
Gilles Peskine449bd832023-01-11 14:50:10 +01006293 MBEDTLS_SSL_DEBUG_BUF(3, "session hash for extended master secret",
6294 session_hash, seed_len);
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006295 }
Przemek Stekiel169bf0b2022-04-29 07:53:29 +02006296#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006297
Przemek Stekiel99114f32022-04-22 11:20:09 +02006298#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Przemek Stekiel8a4b7fd2022-04-28 09:22:22 +02006299 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006300 if (mbedtls_ssl_ciphersuite_uses_psk(handshake->ciphersuite_info) == 1) {
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006301 /* Perform PSK-to-MS expansion in a single step. */
6302 psa_status_t status;
6303 psa_algorithm_t alg;
6304 mbedtls_svc_key_id_t psk;
6305 psa_key_derivation_operation_t derivation =
6306 PSA_KEY_DERIVATION_OPERATION_INIT;
6307 mbedtls_md_type_t hash_alg = handshake->ciphersuite_info->mac;
6308
Gilles Peskine449bd832023-01-11 14:50:10 +01006309 MBEDTLS_SSL_DEBUG_MSG(2, ("perform PSA-based PSK-to-MS expansion"));
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006310
Gilles Peskine449bd832023-01-11 14:50:10 +01006311 psk = mbedtls_ssl_get_opaque_psk(ssl);
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006312
Gilles Peskine449bd832023-01-11 14:50:10 +01006313 if (hash_alg == MBEDTLS_MD_SHA384) {
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006314 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
Gilles Peskine449bd832023-01-11 14:50:10 +01006315 } else {
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006316 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
Gilles Peskine449bd832023-01-11 14:50:10 +01006317 }
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006318
Przemek Stekiel51a1f362022-04-13 08:57:06 +02006319 size_t other_secret_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006320 unsigned char *other_secret = NULL;
Przemek Stekielc2033402022-04-05 17:19:41 +02006321
Gilles Peskine449bd832023-01-11 14:50:10 +01006322 switch (handshake->ciphersuite_info->key_exchange) {
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006323 /* Provide other secret.
Przemek Stekiel51a1f362022-04-13 08:57:06 +02006324 * Other secret is stored in premaster, where first 2 bytes hold the
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006325 * length of the other key.
Przemek Stekielc2033402022-04-05 17:19:41 +02006326 */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006327 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Przemek Stekiel8abcee92022-04-28 09:16:28 +02006328 /* For RSA-PSK other key length is always 48 bytes. */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006329 other_secret_len = 48;
6330 other_secret = handshake->premaster + 2;
6331 break;
6332 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006333 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006334 other_secret_len = MBEDTLS_GET_UINT16_BE(handshake->premaster, 0);
6335 other_secret = handshake->premaster + 2;
6336 break;
6337 default:
6338 break;
Przemek Stekielc2033402022-04-05 17:19:41 +02006339 }
6340
Gilles Peskine449bd832023-01-11 14:50:10 +01006341 status = setup_psa_key_derivation(&derivation, psk, alg,
6342 ssl->conf->psk, ssl->conf->psk_len,
6343 seed, seed_len,
6344 (unsigned char const *) lbl,
6345 (size_t) strlen(lbl),
6346 other_secret, other_secret_len,
6347 master_secret_len);
6348 if (status != PSA_SUCCESS) {
6349 psa_key_derivation_abort(&derivation);
6350 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006351 }
6352
Gilles Peskine449bd832023-01-11 14:50:10 +01006353 status = psa_key_derivation_output_bytes(&derivation,
6354 master,
6355 master_secret_len);
6356 if (status != PSA_SUCCESS) {
6357 psa_key_derivation_abort(&derivation);
6358 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006359 }
6360
Gilles Peskine449bd832023-01-11 14:50:10 +01006361 status = psa_key_derivation_abort(&derivation);
6362 if (status != PSA_SUCCESS) {
6363 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
6364 }
6365 } else
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006366#endif
6367 {
Neil Armstrongca7d5062022-05-31 14:43:23 +02006368#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01006369 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
6370 if (handshake->ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE) {
Neil Armstrongca7d5062022-05-31 14:43:23 +02006371 psa_status_t status;
6372 psa_algorithm_t alg = PSA_ALG_TLS12_ECJPAKE_TO_PMS;
6373 psa_key_derivation_operation_t derivation =
6374 PSA_KEY_DERIVATION_OPERATION_INIT;
6375
Gilles Peskine449bd832023-01-11 14:50:10 +01006376 MBEDTLS_SSL_DEBUG_MSG(2, ("perform PSA-based PMS KDF for ECJPAKE"));
Neil Armstrongca7d5062022-05-31 14:43:23 +02006377
6378 handshake->pmslen = PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE;
6379
Gilles Peskine449bd832023-01-11 14:50:10 +01006380 status = psa_key_derivation_setup(&derivation, alg);
6381 if (status != PSA_SUCCESS) {
6382 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Neil Armstrongca7d5062022-05-31 14:43:23 +02006383 }
6384
Gilles Peskine449bd832023-01-11 14:50:10 +01006385 status = psa_key_derivation_set_capacity(&derivation,
6386 PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE);
6387 if (status != PSA_SUCCESS) {
6388 psa_key_derivation_abort(&derivation);
6389 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Neil Armstrongca7d5062022-05-31 14:43:23 +02006390 }
6391
Gilles Peskine449bd832023-01-11 14:50:10 +01006392 status = psa_pake_get_implicit_key(&handshake->psa_pake_ctx,
6393 &derivation);
6394 if (status != PSA_SUCCESS) {
6395 psa_key_derivation_abort(&derivation);
6396 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Neil Armstrongca7d5062022-05-31 14:43:23 +02006397 }
6398
Gilles Peskine449bd832023-01-11 14:50:10 +01006399 status = psa_key_derivation_output_bytes(&derivation,
6400 handshake->premaster,
6401 handshake->pmslen);
6402 if (status != PSA_SUCCESS) {
6403 psa_key_derivation_abort(&derivation);
6404 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
6405 }
6406
6407 status = psa_key_derivation_abort(&derivation);
6408 if (status != PSA_SUCCESS) {
6409 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Neil Armstrongca7d5062022-05-31 14:43:23 +02006410 }
6411 }
6412#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01006413 ret = handshake->tls_prf(handshake->premaster, handshake->pmslen,
6414 lbl, seed, seed_len,
6415 master,
6416 master_secret_len);
6417 if (ret != 0) {
6418 MBEDTLS_SSL_DEBUG_RET(1, "prf", ret);
6419 return ret;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006420 }
6421
Gilles Peskine449bd832023-01-11 14:50:10 +01006422 MBEDTLS_SSL_DEBUG_BUF(3, "premaster secret",
6423 handshake->premaster,
6424 handshake->pmslen);
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006425
Gilles Peskine449bd832023-01-11 14:50:10 +01006426 mbedtls_platform_zeroize(handshake->premaster,
6427 sizeof(handshake->premaster));
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006428 }
6429
Gilles Peskine449bd832023-01-11 14:50:10 +01006430 return 0;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006431}
6432
Gilles Peskine449bd832023-01-11 14:50:10 +01006433int mbedtls_ssl_derive_keys(mbedtls_ssl_context *ssl)
Jerry Yud62f87e2022-02-17 14:09:02 +08006434{
6435 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6436 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
6437 ssl->handshake->ciphersuite_info;
6438
Gilles Peskine449bd832023-01-11 14:50:10 +01006439 MBEDTLS_SSL_DEBUG_MSG(2, ("=> derive keys"));
Jerry Yud62f87e2022-02-17 14:09:02 +08006440
6441 /* Set PRF, calc_verify and calc_finished function pointers */
Gilles Peskine449bd832023-01-11 14:50:10 +01006442 ret = ssl_set_handshake_prfs(ssl->handshake,
6443 ciphersuite_info->mac);
6444 if (ret != 0) {
6445 MBEDTLS_SSL_DEBUG_RET(1, "ssl_set_handshake_prfs", ret);
6446 return ret;
Jerry Yud62f87e2022-02-17 14:09:02 +08006447 }
6448
6449 /* Compute master secret if needed */
Gilles Peskine449bd832023-01-11 14:50:10 +01006450 ret = ssl_compute_master(ssl->handshake,
6451 ssl->session_negotiate->master,
6452 ssl);
6453 if (ret != 0) {
6454 MBEDTLS_SSL_DEBUG_RET(1, "ssl_compute_master", ret);
6455 return ret;
Jerry Yud62f87e2022-02-17 14:09:02 +08006456 }
6457
6458 /* Swap the client and server random values:
6459 * - MS derivation wanted client+server (RFC 5246 8.1)
6460 * - key derivation wants server+client (RFC 5246 6.3) */
6461 {
6462 unsigned char tmp[64];
Gilles Peskine449bd832023-01-11 14:50:10 +01006463 memcpy(tmp, ssl->handshake->randbytes, 64);
6464 memcpy(ssl->handshake->randbytes, tmp + 32, 32);
6465 memcpy(ssl->handshake->randbytes + 32, tmp, 32);
6466 mbedtls_platform_zeroize(tmp, sizeof(tmp));
Jerry Yud62f87e2022-02-17 14:09:02 +08006467 }
6468
6469 /* Populate transform structure */
Gilles Peskine449bd832023-01-11 14:50:10 +01006470 ret = ssl_tls12_populate_transform(ssl->transform_negotiate,
6471 ssl->session_negotiate->ciphersuite,
6472 ssl->session_negotiate->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02006473#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01006474 ssl->session_negotiate->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02006475#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01006476 ssl->handshake->tls_prf,
6477 ssl->handshake->randbytes,
6478 ssl->tls_version,
6479 ssl->conf->endpoint,
6480 ssl);
6481 if (ret != 0) {
6482 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls12_populate_transform", ret);
6483 return ret;
Jerry Yud62f87e2022-02-17 14:09:02 +08006484 }
6485
6486 /* We no longer need Server/ClientHello.random values */
Gilles Peskine449bd832023-01-11 14:50:10 +01006487 mbedtls_platform_zeroize(ssl->handshake->randbytes,
6488 sizeof(ssl->handshake->randbytes));
Jerry Yud62f87e2022-02-17 14:09:02 +08006489
Gilles Peskine449bd832023-01-11 14:50:10 +01006490 MBEDTLS_SSL_DEBUG_MSG(2, ("<= derive keys"));
Jerry Yud62f87e2022-02-17 14:09:02 +08006491
Gilles Peskine449bd832023-01-11 14:50:10 +01006492 return 0;
Jerry Yud62f87e2022-02-17 14:09:02 +08006493}
Jerry Yu8392e0d2022-02-17 14:10:24 +08006494
Gilles Peskine449bd832023-01-11 14:50:10 +01006495int mbedtls_ssl_set_calc_verify_md(mbedtls_ssl_context *ssl, int md)
Ronald Cron4dcbca92022-03-07 10:21:40 +01006496{
Gilles Peskine449bd832023-01-11 14:50:10 +01006497 switch (md) {
Andrzej Kurek25f27152022-08-17 16:09:31 -04006498#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron4dcbca92022-03-07 10:21:40 +01006499 case MBEDTLS_SSL_HASH_SHA384:
6500 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
6501 break;
6502#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04006503#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron4dcbca92022-03-07 10:21:40 +01006504 case MBEDTLS_SSL_HASH_SHA256:
6505 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
6506 break;
6507#endif
6508 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006509 return -1;
Ronald Cron4dcbca92022-03-07 10:21:40 +01006510 }
Andrzej Kurekeabeb302022-10-17 07:52:51 -04006511#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
6512 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
6513 (void) ssl;
6514#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01006515 return 0;
Ronald Cron4dcbca92022-03-07 10:21:40 +01006516}
6517
Andrzej Kurek25f27152022-08-17 16:09:31 -04006518#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01006519int ssl_calc_verify_tls_sha256(const mbedtls_ssl_context *ssl,
6520 unsigned char *hash,
6521 size_t *hlen)
Jerry Yu8392e0d2022-02-17 14:10:24 +08006522{
6523#if defined(MBEDTLS_USE_PSA_CRYPTO)
6524 size_t hash_size;
6525 psa_status_t status;
6526 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
6527
Gilles Peskine449bd832023-01-11 14:50:10 +01006528 MBEDTLS_SSL_DEBUG_MSG(2, ("=> PSA calc verify sha256"));
6529 status = psa_hash_clone(&ssl->handshake->fin_sha256_psa, &sha256_psa);
6530 if (status != PSA_SUCCESS) {
6531 MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash clone failed"));
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01006532 return 0;
Jerry Yu8392e0d2022-02-17 14:10:24 +08006533 }
6534
Gilles Peskine449bd832023-01-11 14:50:10 +01006535 status = psa_hash_finish(&sha256_psa, hash, 32, &hash_size);
6536 if (status != PSA_SUCCESS) {
6537 MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash finish failed"));
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01006538 return 0;
Jerry Yu8392e0d2022-02-17 14:10:24 +08006539 }
6540
6541 *hlen = 32;
Gilles Peskine449bd832023-01-11 14:50:10 +01006542 MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated verify result", hash, *hlen);
6543 MBEDTLS_SSL_DEBUG_MSG(2, ("<= PSA calc verify"));
Jerry Yu8392e0d2022-02-17 14:10:24 +08006544#else
6545 mbedtls_sha256_context sha256;
6546
Gilles Peskine449bd832023-01-11 14:50:10 +01006547 mbedtls_sha256_init(&sha256);
Jerry Yu8392e0d2022-02-17 14:10:24 +08006548
Gilles Peskine449bd832023-01-11 14:50:10 +01006549 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc verify sha256"));
Jerry Yu8392e0d2022-02-17 14:10:24 +08006550
Gilles Peskine449bd832023-01-11 14:50:10 +01006551 mbedtls_sha256_clone(&sha256, &ssl->handshake->fin_sha256);
6552 mbedtls_sha256_finish(&sha256, hash);
Jerry Yu8392e0d2022-02-17 14:10:24 +08006553
6554 *hlen = 32;
6555
Gilles Peskine449bd832023-01-11 14:50:10 +01006556 MBEDTLS_SSL_DEBUG_BUF(3, "calculated verify result", hash, *hlen);
6557 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc verify"));
Jerry Yu8392e0d2022-02-17 14:10:24 +08006558
Gilles Peskine449bd832023-01-11 14:50:10 +01006559 mbedtls_sha256_free(&sha256);
Jerry Yu8392e0d2022-02-17 14:10:24 +08006560#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01006561 return 0;
Jerry Yu8392e0d2022-02-17 14:10:24 +08006562}
Andrzej Kurek25f27152022-08-17 16:09:31 -04006563#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu8392e0d2022-02-17 14:10:24 +08006564
Andrzej Kurek25f27152022-08-17 16:09:31 -04006565#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01006566int ssl_calc_verify_tls_sha384(const mbedtls_ssl_context *ssl,
6567 unsigned char *hash,
6568 size_t *hlen)
Jerry Yuc1cb3842022-02-17 14:13:48 +08006569{
6570#if defined(MBEDTLS_USE_PSA_CRYPTO)
6571 size_t hash_size;
6572 psa_status_t status;
6573 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
6574
Gilles Peskine449bd832023-01-11 14:50:10 +01006575 MBEDTLS_SSL_DEBUG_MSG(2, ("=> PSA calc verify sha384"));
6576 status = psa_hash_clone(&ssl->handshake->fin_sha384_psa, &sha384_psa);
6577 if (status != PSA_SUCCESS) {
6578 MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash clone failed"));
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01006579 return 0;
Jerry Yuc1cb3842022-02-17 14:13:48 +08006580 }
6581
Gilles Peskine449bd832023-01-11 14:50:10 +01006582 status = psa_hash_finish(&sha384_psa, hash, 48, &hash_size);
6583 if (status != PSA_SUCCESS) {
6584 MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash finish failed"));
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01006585 return 0;
Jerry Yuc1cb3842022-02-17 14:13:48 +08006586 }
6587
6588 *hlen = 48;
Gilles Peskine449bd832023-01-11 14:50:10 +01006589 MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated verify result", hash, *hlen);
6590 MBEDTLS_SSL_DEBUG_MSG(2, ("<= PSA calc verify"));
Jerry Yuc1cb3842022-02-17 14:13:48 +08006591#else
6592 mbedtls_sha512_context sha512;
6593
Gilles Peskine449bd832023-01-11 14:50:10 +01006594 mbedtls_sha512_init(&sha512);
Jerry Yuc1cb3842022-02-17 14:13:48 +08006595
Gilles Peskine449bd832023-01-11 14:50:10 +01006596 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc verify sha384"));
Jerry Yuc1cb3842022-02-17 14:13:48 +08006597
Gilles Peskine449bd832023-01-11 14:50:10 +01006598 mbedtls_sha512_clone(&sha512, &ssl->handshake->fin_sha384);
6599 mbedtls_sha512_finish(&sha512, hash);
Jerry Yuc1cb3842022-02-17 14:13:48 +08006600
6601 *hlen = 48;
6602
Gilles Peskine449bd832023-01-11 14:50:10 +01006603 MBEDTLS_SSL_DEBUG_BUF(3, "calculated verify result", hash, *hlen);
6604 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc verify"));
Jerry Yuc1cb3842022-02-17 14:13:48 +08006605
Gilles Peskine449bd832023-01-11 14:50:10 +01006606 mbedtls_sha512_free(&sha512);
Jerry Yuc1cb3842022-02-17 14:13:48 +08006607#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01006608 return 0;
Jerry Yuc1cb3842022-02-17 14:13:48 +08006609}
Andrzej Kurek25f27152022-08-17 16:09:31 -04006610#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yuc1cb3842022-02-17 14:13:48 +08006611
Neil Armstrong80f6f322022-05-03 17:56:38 +02006612#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
6613 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006614int mbedtls_ssl_psk_derive_premaster(mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex)
Jerry Yuce3dca42022-02-17 14:16:37 +08006615{
6616 unsigned char *p = ssl->handshake->premaster;
Gilles Peskine449bd832023-01-11 14:50:10 +01006617 unsigned char *end = p + sizeof(ssl->handshake->premaster);
Jerry Yuce3dca42022-02-17 14:16:37 +08006618 const unsigned char *psk = NULL;
6619 size_t psk_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006620 int psk_ret = mbedtls_ssl_get_psk(ssl, &psk, &psk_len);
Jerry Yuce3dca42022-02-17 14:16:37 +08006621
Gilles Peskine449bd832023-01-11 14:50:10 +01006622 if (psk_ret == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED) {
Jerry Yuce3dca42022-02-17 14:16:37 +08006623 /*
6624 * This should never happen because the existence of a PSK is always
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006625 * checked before calling this function.
6626 *
6627 * The exception is opaque DHE-PSK. For DHE-PSK fill premaster with
Przemek Stekiel8abcee92022-04-28 09:16:28 +02006628 * the shared secret without PSK.
Jerry Yuce3dca42022-02-17 14:16:37 +08006629 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006630 if (key_ex != MBEDTLS_KEY_EXCHANGE_DHE_PSK) {
6631 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
6632 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006633 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006634 }
6635
6636 /*
6637 * PMS = struct {
6638 * opaque other_secret<0..2^16-1>;
6639 * opaque psk<0..2^16-1>;
6640 * };
6641 * with "other_secret" depending on the particular key exchange
6642 */
6643#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006644 if (key_ex == MBEDTLS_KEY_EXCHANGE_PSK) {
6645 if (end - p < 2) {
6646 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6647 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006648
Gilles Peskine449bd832023-01-11 14:50:10 +01006649 MBEDTLS_PUT_UINT16_BE(psk_len, p, 0);
Jerry Yuce3dca42022-02-17 14:16:37 +08006650 p += 2;
6651
Gilles Peskine449bd832023-01-11 14:50:10 +01006652 if (end < p || (size_t) (end - p) < psk_len) {
6653 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6654 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006655
Gilles Peskine449bd832023-01-11 14:50:10 +01006656 memset(p, 0, psk_len);
Jerry Yuce3dca42022-02-17 14:16:37 +08006657 p += psk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01006658 } else
Jerry Yuce3dca42022-02-17 14:16:37 +08006659#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
6660#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006661 if (key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK) {
Jerry Yuce3dca42022-02-17 14:16:37 +08006662 /*
6663 * other_secret already set by the ClientKeyExchange message,
6664 * and is 48 bytes long
6665 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006666 if (end - p < 2) {
6667 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6668 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006669
6670 *p++ = 0;
6671 *p++ = 48;
6672 p += 48;
Gilles Peskine449bd832023-01-11 14:50:10 +01006673 } else
Jerry Yuce3dca42022-02-17 14:16:37 +08006674#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
6675#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006676 if (key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK) {
Jerry Yuce3dca42022-02-17 14:16:37 +08006677 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6678 size_t len;
6679
6680 /* Write length only when we know the actual value */
Gilles Peskine449bd832023-01-11 14:50:10 +01006681 if ((ret = mbedtls_dhm_calc_secret(&ssl->handshake->dhm_ctx,
6682 p + 2, end - (p + 2), &len,
6683 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
6684 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_calc_secret", ret);
6685 return ret;
Jerry Yuce3dca42022-02-17 14:16:37 +08006686 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006687 MBEDTLS_PUT_UINT16_BE(len, p, 0);
Jerry Yuce3dca42022-02-17 14:16:37 +08006688 p += 2 + len;
6689
Gilles Peskine449bd832023-01-11 14:50:10 +01006690 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: K ", &ssl->handshake->dhm_ctx.K);
6691 } else
Jerry Yuce3dca42022-02-17 14:16:37 +08006692#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Neil Armstrong80f6f322022-05-03 17:56:38 +02006693#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006694 if (key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK) {
Jerry Yuce3dca42022-02-17 14:16:37 +08006695 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6696 size_t zlen;
6697
Gilles Peskine449bd832023-01-11 14:50:10 +01006698 if ((ret = mbedtls_ecdh_calc_secret(&ssl->handshake->ecdh_ctx, &zlen,
6699 p + 2, end - (p + 2),
6700 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
6701 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecdh_calc_secret", ret);
6702 return ret;
Jerry Yuce3dca42022-02-17 14:16:37 +08006703 }
6704
Gilles Peskine449bd832023-01-11 14:50:10 +01006705 MBEDTLS_PUT_UINT16_BE(zlen, p, 0);
Jerry Yuce3dca42022-02-17 14:16:37 +08006706 p += 2 + zlen;
6707
Gilles Peskine449bd832023-01-11 14:50:10 +01006708 MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx,
6709 MBEDTLS_DEBUG_ECDH_Z);
6710 } else
Neil Armstrong80f6f322022-05-03 17:56:38 +02006711#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Jerry Yuce3dca42022-02-17 14:16:37 +08006712 {
Gilles Peskine449bd832023-01-11 14:50:10 +01006713 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
6714 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yuce3dca42022-02-17 14:16:37 +08006715 }
6716
6717 /* opaque psk<0..2^16-1>; */
Gilles Peskine449bd832023-01-11 14:50:10 +01006718 if (end - p < 2) {
6719 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6720 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006721
Gilles Peskine449bd832023-01-11 14:50:10 +01006722 MBEDTLS_PUT_UINT16_BE(psk_len, p, 0);
Jerry Yuce3dca42022-02-17 14:16:37 +08006723 p += 2;
6724
Gilles Peskine449bd832023-01-11 14:50:10 +01006725 if (end < p || (size_t) (end - p) < psk_len) {
6726 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6727 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006728
Gilles Peskine449bd832023-01-11 14:50:10 +01006729 memcpy(p, psk, psk_len);
Jerry Yuce3dca42022-02-17 14:16:37 +08006730 p += psk_len;
6731
6732 ssl->handshake->pmslen = p - ssl->handshake->premaster;
6733
Gilles Peskine449bd832023-01-11 14:50:10 +01006734 return 0;
Jerry Yuce3dca42022-02-17 14:16:37 +08006735}
Neil Armstrong80f6f322022-05-03 17:56:38 +02006736#endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Jerry Yuc2c673d2022-02-17 14:20:39 +08006737
6738#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006739MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006740static int ssl_write_hello_request(mbedtls_ssl_context *ssl);
Jerry Yuc2c673d2022-02-17 14:20:39 +08006741
6742#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006743int mbedtls_ssl_resend_hello_request(mbedtls_ssl_context *ssl)
Jerry Yuc2c673d2022-02-17 14:20:39 +08006744{
6745 /* If renegotiation is not enforced, retransmit until we would reach max
6746 * timeout if we were using the usual handshake doubling scheme */
Gilles Peskine449bd832023-01-11 14:50:10 +01006747 if (ssl->conf->renego_max_records < 0) {
Jerry Yuc2c673d2022-02-17 14:20:39 +08006748 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
6749 unsigned char doublings = 1;
6750
Gilles Peskine449bd832023-01-11 14:50:10 +01006751 while (ratio != 0) {
Jerry Yuc2c673d2022-02-17 14:20:39 +08006752 ++doublings;
6753 ratio >>= 1;
6754 }
6755
Gilles Peskine449bd832023-01-11 14:50:10 +01006756 if (++ssl->renego_records_seen > doublings) {
6757 MBEDTLS_SSL_DEBUG_MSG(2, ("no longer retransmitting hello request"));
6758 return 0;
Jerry Yuc2c673d2022-02-17 14:20:39 +08006759 }
6760 }
6761
Gilles Peskine449bd832023-01-11 14:50:10 +01006762 return ssl_write_hello_request(ssl);
Jerry Yuc2c673d2022-02-17 14:20:39 +08006763}
6764#endif
6765#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Jerry Yud9526692022-02-17 14:23:47 +08006766
Jerry Yud9526692022-02-17 14:23:47 +08006767/*
6768 * Handshake functions
6769 */
6770#if !defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
6771/* No certificate support -> dummy functions */
Gilles Peskine449bd832023-01-11 14:50:10 +01006772int mbedtls_ssl_write_certificate(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08006773{
6774 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6775 ssl->handshake->ciphersuite_info;
6776
Gilles Peskine449bd832023-01-11 14:50:10 +01006777 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006778
Gilles Peskine449bd832023-01-11 14:50:10 +01006779 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
6780 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006781 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01006782 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08006783 }
6784
Gilles Peskine449bd832023-01-11 14:50:10 +01006785 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
6786 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08006787}
6788
Gilles Peskine449bd832023-01-11 14:50:10 +01006789int mbedtls_ssl_parse_certificate(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08006790{
6791 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6792 ssl->handshake->ciphersuite_info;
6793
Gilles Peskine449bd832023-01-11 14:50:10 +01006794 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006795
Gilles Peskine449bd832023-01-11 14:50:10 +01006796 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
6797 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006798 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01006799 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08006800 }
6801
Gilles Peskine449bd832023-01-11 14:50:10 +01006802 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
6803 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08006804}
6805
6806#else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
6807/* Some certificate support -> implement write and parse */
6808
Gilles Peskine449bd832023-01-11 14:50:10 +01006809int mbedtls_ssl_write_certificate(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08006810{
6811 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
6812 size_t i, n;
6813 const mbedtls_x509_crt *crt;
6814 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6815 ssl->handshake->ciphersuite_info;
6816
Gilles Peskine449bd832023-01-11 14:50:10 +01006817 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006818
Gilles Peskine449bd832023-01-11 14:50:10 +01006819 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
6820 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006821 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01006822 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08006823 }
6824
6825#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01006826 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
6827 if (ssl->handshake->client_auth == 0) {
6828 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006829 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01006830 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08006831 }
6832 }
6833#endif /* MBEDTLS_SSL_CLI_C */
6834#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01006835 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
6836 if (mbedtls_ssl_own_cert(ssl) == NULL) {
Jerry Yud9526692022-02-17 14:23:47 +08006837 /* Should never happen because we shouldn't have picked the
6838 * ciphersuite if we don't have a certificate. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006839 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08006840 }
6841 }
6842#endif
6843
Gilles Peskine449bd832023-01-11 14:50:10 +01006844 MBEDTLS_SSL_DEBUG_CRT(3, "own certificate", mbedtls_ssl_own_cert(ssl));
Jerry Yud9526692022-02-17 14:23:47 +08006845
6846 /*
6847 * 0 . 0 handshake type
6848 * 1 . 3 handshake length
6849 * 4 . 6 length of all certs
6850 * 7 . 9 length of cert. 1
6851 * 10 . n-1 peer certificate
6852 * n . n+2 length of cert. 2
6853 * n+3 . ... upper level cert, etc.
6854 */
6855 i = 7;
Gilles Peskine449bd832023-01-11 14:50:10 +01006856 crt = mbedtls_ssl_own_cert(ssl);
Jerry Yud9526692022-02-17 14:23:47 +08006857
Gilles Peskine449bd832023-01-11 14:50:10 +01006858 while (crt != NULL) {
Jerry Yud9526692022-02-17 14:23:47 +08006859 n = crt->raw.len;
Gilles Peskine449bd832023-01-11 14:50:10 +01006860 if (n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i) {
6861 MBEDTLS_SSL_DEBUG_MSG(1, ("certificate too large, %" MBEDTLS_PRINTF_SIZET
6862 " > %" MBEDTLS_PRINTF_SIZET,
6863 i + 3 + n, (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN));
6864 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
Jerry Yud9526692022-02-17 14:23:47 +08006865 }
6866
Gilles Peskine449bd832023-01-11 14:50:10 +01006867 ssl->out_msg[i] = MBEDTLS_BYTE_2(n);
6868 ssl->out_msg[i + 1] = MBEDTLS_BYTE_1(n);
6869 ssl->out_msg[i + 2] = MBEDTLS_BYTE_0(n);
Jerry Yud9526692022-02-17 14:23:47 +08006870
Gilles Peskine449bd832023-01-11 14:50:10 +01006871 i += 3; memcpy(ssl->out_msg + i, crt->raw.p, n);
Jerry Yud9526692022-02-17 14:23:47 +08006872 i += n; crt = crt->next;
6873 }
6874
Gilles Peskine449bd832023-01-11 14:50:10 +01006875 ssl->out_msg[4] = MBEDTLS_BYTE_2(i - 7);
6876 ssl->out_msg[5] = MBEDTLS_BYTE_1(i - 7);
6877 ssl->out_msg[6] = MBEDTLS_BYTE_0(i - 7);
Jerry Yud9526692022-02-17 14:23:47 +08006878
6879 ssl->out_msglen = i;
6880 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6881 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
6882
6883 ssl->state++;
6884
Gilles Peskine449bd832023-01-11 14:50:10 +01006885 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
6886 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
6887 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08006888 }
6889
Gilles Peskine449bd832023-01-11 14:50:10 +01006890 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006891
Gilles Peskine449bd832023-01-11 14:50:10 +01006892 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08006893}
6894
6895#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6896
6897#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006898MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006899static int ssl_check_peer_crt_unchanged(mbedtls_ssl_context *ssl,
6900 unsigned char *crt_buf,
6901 size_t crt_buf_len)
Jerry Yud9526692022-02-17 14:23:47 +08006902{
6903 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6904
Gilles Peskine449bd832023-01-11 14:50:10 +01006905 if (peer_crt == NULL) {
6906 return -1;
6907 }
Jerry Yud9526692022-02-17 14:23:47 +08006908
Gilles Peskine449bd832023-01-11 14:50:10 +01006909 if (peer_crt->raw.len != crt_buf_len) {
6910 return -1;
6911 }
Jerry Yud9526692022-02-17 14:23:47 +08006912
Gilles Peskine449bd832023-01-11 14:50:10 +01006913 return memcmp(peer_crt->raw.p, crt_buf, peer_crt->raw.len);
Jerry Yud9526692022-02-17 14:23:47 +08006914}
6915#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006916MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006917static int ssl_check_peer_crt_unchanged(mbedtls_ssl_context *ssl,
6918 unsigned char *crt_buf,
6919 size_t crt_buf_len)
Jerry Yud9526692022-02-17 14:23:47 +08006920{
6921 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6922 unsigned char const * const peer_cert_digest =
6923 ssl->session->peer_cert_digest;
6924 mbedtls_md_type_t const peer_cert_digest_type =
6925 ssl->session->peer_cert_digest_type;
6926 mbedtls_md_info_t const * const digest_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01006927 mbedtls_md_info_from_type(peer_cert_digest_type);
Jerry Yud9526692022-02-17 14:23:47 +08006928 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6929 size_t digest_len;
6930
Gilles Peskine449bd832023-01-11 14:50:10 +01006931 if (peer_cert_digest == NULL || digest_info == NULL) {
6932 return -1;
6933 }
Jerry Yud9526692022-02-17 14:23:47 +08006934
Gilles Peskine449bd832023-01-11 14:50:10 +01006935 digest_len = mbedtls_md_get_size(digest_info);
6936 if (digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN) {
6937 return -1;
6938 }
Jerry Yud9526692022-02-17 14:23:47 +08006939
Gilles Peskine449bd832023-01-11 14:50:10 +01006940 ret = mbedtls_md(digest_info, crt_buf, crt_buf_len, tmp_digest);
6941 if (ret != 0) {
6942 return -1;
6943 }
Jerry Yud9526692022-02-17 14:23:47 +08006944
Gilles Peskine449bd832023-01-11 14:50:10 +01006945 return memcmp(tmp_digest, peer_cert_digest, digest_len);
Jerry Yud9526692022-02-17 14:23:47 +08006946}
6947#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6948#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6949
6950/*
6951 * Once the certificate message is read, parse it into a cert chain and
6952 * perform basic checks, but leave actual verification to the caller
6953 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006954MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006955static int ssl_parse_certificate_chain(mbedtls_ssl_context *ssl,
6956 mbedtls_x509_crt *chain)
Jerry Yud9526692022-02-17 14:23:47 +08006957{
6958 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6959#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01006960 int crt_cnt = 0;
Jerry Yud9526692022-02-17 14:23:47 +08006961#endif
6962 size_t i, n;
6963 uint8_t alert;
6964
Gilles Peskine449bd832023-01-11 14:50:10 +01006965 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
6966 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
6967 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6968 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
6969 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Jerry Yud9526692022-02-17 14:23:47 +08006970 }
6971
Gilles Peskine449bd832023-01-11 14:50:10 +01006972 if (ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE) {
6973 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6974 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
6975 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Jerry Yud9526692022-02-17 14:23:47 +08006976 }
6977
Gilles Peskine449bd832023-01-11 14:50:10 +01006978 if (ssl->in_hslen < mbedtls_ssl_hs_hdr_len(ssl) + 3 + 3) {
6979 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
6980 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6981 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
6982 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08006983 }
6984
Gilles Peskine449bd832023-01-11 14:50:10 +01006985 i = mbedtls_ssl_hs_hdr_len(ssl);
Jerry Yud9526692022-02-17 14:23:47 +08006986
6987 /*
6988 * Same message structure as in mbedtls_ssl_write_certificate()
6989 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006990 n = (ssl->in_msg[i+1] << 8) | ssl->in_msg[i+2];
Jerry Yud9526692022-02-17 14:23:47 +08006991
Gilles Peskine449bd832023-01-11 14:50:10 +01006992 if (ssl->in_msg[i] != 0 ||
6993 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len(ssl)) {
6994 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
6995 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6996 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
6997 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08006998 }
6999
7000 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
7001 i += 3;
7002
7003 /* Iterate through and parse the CRTs in the provided chain. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007004 while (i < ssl->in_hslen) {
Jerry Yud9526692022-02-17 14:23:47 +08007005 /* Check that there's room for the next CRT's length fields. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007006 if (i + 3 > ssl->in_hslen) {
7007 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7008 mbedtls_ssl_send_alert_message(ssl,
7009 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7010 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
7011 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007012 }
7013 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
7014 * anything beyond 2**16 ~ 64K. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007015 if (ssl->in_msg[i] != 0) {
7016 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7017 mbedtls_ssl_send_alert_message(ssl,
7018 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7019 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT);
7020 return MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Jerry Yud9526692022-02-17 14:23:47 +08007021 }
7022
7023 /* Read length of the next CRT in the chain. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007024 n = ((unsigned int) ssl->in_msg[i + 1] << 8)
Jerry Yud9526692022-02-17 14:23:47 +08007025 | (unsigned int) ssl->in_msg[i + 2];
7026 i += 3;
7027
Gilles Peskine449bd832023-01-11 14:50:10 +01007028 if (n < 128 || i + n > ssl->in_hslen) {
7029 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7030 mbedtls_ssl_send_alert_message(ssl,
7031 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7032 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
7033 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007034 }
7035
7036 /* Check if we're handling the first CRT in the chain. */
7037#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007038 if (crt_cnt++ == 0 &&
Jerry Yud9526692022-02-17 14:23:47 +08007039 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Gilles Peskine449bd832023-01-11 14:50:10 +01007040 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
Jerry Yud9526692022-02-17 14:23:47 +08007041 /* During client-side renegotiation, check that the server's
7042 * end-CRTs hasn't changed compared to the initial handshake,
7043 * mitigating the triple handshake attack. On success, reuse
7044 * the original end-CRT instead of parsing it again. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007045 MBEDTLS_SSL_DEBUG_MSG(3, ("Check that peer CRT hasn't changed during renegotiation"));
7046 if (ssl_check_peer_crt_unchanged(ssl,
7047 &ssl->in_msg[i],
7048 n) != 0) {
7049 MBEDTLS_SSL_DEBUG_MSG(1, ("new server cert during renegotiation"));
7050 mbedtls_ssl_send_alert_message(ssl,
7051 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7052 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED);
7053 return MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Jerry Yud9526692022-02-17 14:23:47 +08007054 }
7055
7056 /* Now we can safely free the original chain. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007057 ssl_clear_peer_cert(ssl->session);
Jerry Yud9526692022-02-17 14:23:47 +08007058 }
7059#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
7060
7061 /* Parse the next certificate in the chain. */
7062#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007063 ret = mbedtls_x509_crt_parse_der(chain, ssl->in_msg + i, n);
Jerry Yud9526692022-02-17 14:23:47 +08007064#else
7065 /* If we don't need to store the CRT chain permanently, parse
7066 * it in-place from the input buffer instead of making a copy. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007067 ret = mbedtls_x509_crt_parse_der_nocopy(chain, ssl->in_msg + i, n);
Jerry Yud9526692022-02-17 14:23:47 +08007068#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007069 switch (ret) {
Jerry Yud9526692022-02-17 14:23:47 +08007070 case 0: /*ok*/
7071 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
7072 /* Ignore certificate with an unknown algorithm: maybe a
7073 prior certificate was already trusted. */
7074 break;
7075
7076 case MBEDTLS_ERR_X509_ALLOC_FAILED:
7077 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
7078 goto crt_parse_der_failed;
7079
7080 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
7081 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7082 goto crt_parse_der_failed;
7083
7084 default:
7085 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007086crt_parse_der_failed:
7087 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert);
7088 MBEDTLS_SSL_DEBUG_RET(1, " mbedtls_x509_crt_parse_der", ret);
7089 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007090 }
7091
7092 i += n;
7093 }
7094
Gilles Peskine449bd832023-01-11 14:50:10 +01007095 MBEDTLS_SSL_DEBUG_CRT(3, "peer certificate", chain);
7096 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08007097}
7098
7099#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007100MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007101static int ssl_srv_check_client_no_crt_notification(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08007102{
Gilles Peskine449bd832023-01-11 14:50:10 +01007103 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
7104 return -1;
7105 }
Jerry Yud9526692022-02-17 14:23:47 +08007106
Gilles Peskine449bd832023-01-11 14:50:10 +01007107 if (ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len(ssl) &&
Jerry Yud9526692022-02-17 14:23:47 +08007108 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
7109 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
Gilles Peskine449bd832023-01-11 14:50:10 +01007110 memcmp(ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl), "\0\0\0", 3) == 0) {
7111 MBEDTLS_SSL_DEBUG_MSG(1, ("peer has no certificate"));
7112 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08007113 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007114 return -1;
Jerry Yud9526692022-02-17 14:23:47 +08007115}
7116#endif /* MBEDTLS_SSL_SRV_C */
7117
7118/* Check if a certificate message is expected.
7119 * Return either
7120 * - SSL_CERTIFICATE_EXPECTED, or
7121 * - SSL_CERTIFICATE_SKIP
7122 * indicating whether a Certificate message is expected or not.
7123 */
7124#define SSL_CERTIFICATE_EXPECTED 0
7125#define SSL_CERTIFICATE_SKIP 1
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007126MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007127static int ssl_parse_certificate_coordinate(mbedtls_ssl_context *ssl,
7128 int authmode)
Jerry Yud9526692022-02-17 14:23:47 +08007129{
7130 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
7131 ssl->handshake->ciphersuite_info;
7132
Gilles Peskine449bd832023-01-11 14:50:10 +01007133 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
7134 return SSL_CERTIFICATE_SKIP;
7135 }
Jerry Yud9526692022-02-17 14:23:47 +08007136
7137#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007138 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
7139 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK) {
7140 return SSL_CERTIFICATE_SKIP;
7141 }
Jerry Yud9526692022-02-17 14:23:47 +08007142
Gilles Peskine449bd832023-01-11 14:50:10 +01007143 if (authmode == MBEDTLS_SSL_VERIFY_NONE) {
Jerry Yud9526692022-02-17 14:23:47 +08007144 ssl->session_negotiate->verify_result =
7145 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
Gilles Peskine449bd832023-01-11 14:50:10 +01007146 return SSL_CERTIFICATE_SKIP;
Jerry Yud9526692022-02-17 14:23:47 +08007147 }
7148 }
7149#else
7150 ((void) authmode);
7151#endif /* MBEDTLS_SSL_SRV_C */
7152
Gilles Peskine449bd832023-01-11 14:50:10 +01007153 return SSL_CERTIFICATE_EXPECTED;
Jerry Yud9526692022-02-17 14:23:47 +08007154}
7155
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007156MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007157static int ssl_parse_certificate_verify(mbedtls_ssl_context *ssl,
7158 int authmode,
7159 mbedtls_x509_crt *chain,
7160 void *rs_ctx)
Jerry Yud9526692022-02-17 14:23:47 +08007161{
7162 int ret = 0;
7163 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
7164 ssl->handshake->ciphersuite_info;
7165 int have_ca_chain = 0;
7166
7167 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
7168 void *p_vrfy;
7169
Gilles Peskine449bd832023-01-11 14:50:10 +01007170 if (authmode == MBEDTLS_SSL_VERIFY_NONE) {
7171 return 0;
7172 }
Jerry Yud9526692022-02-17 14:23:47 +08007173
Gilles Peskine449bd832023-01-11 14:50:10 +01007174 if (ssl->f_vrfy != NULL) {
7175 MBEDTLS_SSL_DEBUG_MSG(3, ("Use context-specific verification callback"));
Jerry Yud9526692022-02-17 14:23:47 +08007176 f_vrfy = ssl->f_vrfy;
7177 p_vrfy = ssl->p_vrfy;
Gilles Peskine449bd832023-01-11 14:50:10 +01007178 } else {
7179 MBEDTLS_SSL_DEBUG_MSG(3, ("Use configuration-specific verification callback"));
Jerry Yud9526692022-02-17 14:23:47 +08007180 f_vrfy = ssl->conf->f_vrfy;
7181 p_vrfy = ssl->conf->p_vrfy;
7182 }
7183
7184 /*
7185 * Main check: verify certificate
7186 */
7187#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Gilles Peskine449bd832023-01-11 14:50:10 +01007188 if (ssl->conf->f_ca_cb != NULL) {
Jerry Yud9526692022-02-17 14:23:47 +08007189 ((void) rs_ctx);
7190 have_ca_chain = 1;
7191
Gilles Peskine449bd832023-01-11 14:50:10 +01007192 MBEDTLS_SSL_DEBUG_MSG(3, ("use CA callback for X.509 CRT verification"));
Jerry Yud9526692022-02-17 14:23:47 +08007193 ret = mbedtls_x509_crt_verify_with_ca_cb(
7194 chain,
7195 ssl->conf->f_ca_cb,
7196 ssl->conf->p_ca_cb,
7197 ssl->conf->cert_profile,
7198 ssl->hostname,
7199 &ssl->session_negotiate->verify_result,
Gilles Peskine449bd832023-01-11 14:50:10 +01007200 f_vrfy, p_vrfy);
7201 } else
Jerry Yud9526692022-02-17 14:23:47 +08007202#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
7203 {
7204 mbedtls_x509_crt *ca_chain;
7205 mbedtls_x509_crl *ca_crl;
7206
7207#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01007208 if (ssl->handshake->sni_ca_chain != NULL) {
Jerry Yud9526692022-02-17 14:23:47 +08007209 ca_chain = ssl->handshake->sni_ca_chain;
7210 ca_crl = ssl->handshake->sni_ca_crl;
Gilles Peskine449bd832023-01-11 14:50:10 +01007211 } else
Jerry Yud9526692022-02-17 14:23:47 +08007212#endif
7213 {
7214 ca_chain = ssl->conf->ca_chain;
7215 ca_crl = ssl->conf->ca_crl;
7216 }
7217
Gilles Peskine449bd832023-01-11 14:50:10 +01007218 if (ca_chain != NULL) {
Jerry Yud9526692022-02-17 14:23:47 +08007219 have_ca_chain = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01007220 }
Jerry Yud9526692022-02-17 14:23:47 +08007221
7222 ret = mbedtls_x509_crt_verify_restartable(
7223 chain,
7224 ca_chain, ca_crl,
7225 ssl->conf->cert_profile,
7226 ssl->hostname,
7227 &ssl->session_negotiate->verify_result,
Gilles Peskine449bd832023-01-11 14:50:10 +01007228 f_vrfy, p_vrfy, rs_ctx);
Jerry Yud9526692022-02-17 14:23:47 +08007229 }
7230
Gilles Peskine449bd832023-01-11 14:50:10 +01007231 if (ret != 0) {
7232 MBEDTLS_SSL_DEBUG_RET(1, "x509_verify_cert", ret);
Jerry Yud9526692022-02-17 14:23:47 +08007233 }
7234
7235#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007236 if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
7237 return MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
7238 }
Jerry Yud9526692022-02-17 14:23:47 +08007239#endif
7240
7241 /*
7242 * Secondary checks: always done, but change 'ret' only if it was 0
7243 */
7244
7245#if defined(MBEDTLS_ECP_C)
7246 {
7247 const mbedtls_pk_context *pk = &chain->pk;
7248
Manuel Pégourié-Gonnard66b0d612022-06-17 10:49:29 +02007249 /* If certificate uses an EC key, make sure the curve is OK.
7250 * This is a public key, so it can't be opaque, so can_do() is a good
7251 * enough check to ensure pk_ec() is safe to use here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007252 if (mbedtls_pk_can_do(pk, MBEDTLS_PK_ECKEY)) {
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007253 /* and in the unlikely case the above assumption no longer holds
7254 * we are making sure that pk_ec() here does not return a NULL
7255 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007256 const mbedtls_ecp_keypair *ec = mbedtls_pk_ec(*pk);
7257 if (ec == NULL) {
7258 MBEDTLS_SSL_DEBUG_MSG(1, ("mbedtls_pk_ec() returned NULL"));
7259 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007260 }
Jerry Yud9526692022-02-17 14:23:47 +08007261
Gilles Peskine449bd832023-01-11 14:50:10 +01007262 if (mbedtls_ssl_check_curve(ssl, ec->grp.id) != 0) {
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007263 ssl->session_negotiate->verify_result |=
7264 MBEDTLS_X509_BADCERT_BAD_KEY;
7265
Gilles Peskine449bd832023-01-11 14:50:10 +01007266 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate (EC key curve)"));
7267 if (ret == 0) {
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007268 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Gilles Peskine449bd832023-01-11 14:50:10 +01007269 }
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007270 }
Jerry Yud9526692022-02-17 14:23:47 +08007271 }
7272 }
7273#endif /* MBEDTLS_ECP_C */
7274
Gilles Peskine449bd832023-01-11 14:50:10 +01007275 if (mbedtls_ssl_check_cert_usage(chain,
7276 ciphersuite_info,
7277 !ssl->conf->endpoint,
7278 &ssl->session_negotiate->verify_result) != 0) {
7279 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate (usage extensions)"));
7280 if (ret == 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007281 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Gilles Peskine449bd832023-01-11 14:50:10 +01007282 }
Jerry Yud9526692022-02-17 14:23:47 +08007283 }
7284
7285 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
7286 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
7287 * with details encoded in the verification flags. All other kinds
7288 * of error codes, including those from the user provided f_vrfy
7289 * functions, are treated as fatal and lead to a failure of
7290 * ssl_parse_certificate even if verification was optional. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007291 if (authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
7292 (ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
7293 ret == MBEDTLS_ERR_SSL_BAD_CERTIFICATE)) {
Jerry Yud9526692022-02-17 14:23:47 +08007294 ret = 0;
7295 }
7296
Gilles Peskine449bd832023-01-11 14:50:10 +01007297 if (have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED) {
7298 MBEDTLS_SSL_DEBUG_MSG(1, ("got no CA chain"));
Jerry Yud9526692022-02-17 14:23:47 +08007299 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
7300 }
7301
Gilles Peskine449bd832023-01-11 14:50:10 +01007302 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007303 uint8_t alert;
7304
7305 /* The certificate may have been rejected for several reasons.
7306 Pick one and send the corresponding alert. Which alert to send
7307 may be a subject of debate in some cases. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007308 if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER) {
Jerry Yud9526692022-02-17 14:23:47 +08007309 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
Gilles Peskine449bd832023-01-11 14:50:10 +01007310 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH) {
Jerry Yud9526692022-02-17 14:23:47 +08007311 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007312 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE) {
Jerry Yud9526692022-02-17 14:23:47 +08007313 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007314 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE) {
Jerry Yud9526692022-02-17 14:23:47 +08007315 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007316 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE) {
Jerry Yud9526692022-02-17 14:23:47 +08007317 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007318 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK) {
Jerry Yud9526692022-02-17 14:23:47 +08007319 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007320 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY) {
Jerry Yud9526692022-02-17 14:23:47 +08007321 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007322 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED) {
Jerry Yud9526692022-02-17 14:23:47 +08007323 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
Gilles Peskine449bd832023-01-11 14:50:10 +01007324 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED) {
Jerry Yud9526692022-02-17 14:23:47 +08007325 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
Gilles Peskine449bd832023-01-11 14:50:10 +01007326 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED) {
Jerry Yud9526692022-02-17 14:23:47 +08007327 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
Gilles Peskine449bd832023-01-11 14:50:10 +01007328 } else {
Jerry Yud9526692022-02-17 14:23:47 +08007329 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
Gilles Peskine449bd832023-01-11 14:50:10 +01007330 }
7331 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7332 alert);
Jerry Yud9526692022-02-17 14:23:47 +08007333 }
7334
7335#if defined(MBEDTLS_DEBUG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007336 if (ssl->session_negotiate->verify_result != 0) {
7337 MBEDTLS_SSL_DEBUG_MSG(3, ("! Certificate verification flags %08x",
7338 (unsigned int) ssl->session_negotiate->verify_result));
7339 } else {
7340 MBEDTLS_SSL_DEBUG_MSG(3, ("Certificate verification flags clear"));
Jerry Yud9526692022-02-17 14:23:47 +08007341 }
7342#endif /* MBEDTLS_DEBUG_C */
7343
Gilles Peskine449bd832023-01-11 14:50:10 +01007344 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007345}
7346
7347#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007348MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007349static int ssl_remember_peer_crt_digest(mbedtls_ssl_context *ssl,
7350 unsigned char *start, size_t len)
Jerry Yud9526692022-02-17 14:23:47 +08007351{
7352 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7353 /* Remember digest of the peer's end-CRT. */
7354 ssl->session_negotiate->peer_cert_digest =
Gilles Peskine449bd832023-01-11 14:50:10 +01007355 mbedtls_calloc(1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN);
7356 if (ssl->session_negotiate->peer_cert_digest == NULL) {
7357 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%d bytes) failed",
7358 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN));
7359 mbedtls_ssl_send_alert_message(ssl,
7360 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7361 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
Jerry Yud9526692022-02-17 14:23:47 +08007362
Gilles Peskine449bd832023-01-11 14:50:10 +01007363 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Jerry Yud9526692022-02-17 14:23:47 +08007364 }
7365
Gilles Peskine449bd832023-01-11 14:50:10 +01007366 ret = mbedtls_md(mbedtls_md_info_from_type(
7367 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE),
7368 start, len,
7369 ssl->session_negotiate->peer_cert_digest);
Jerry Yud9526692022-02-17 14:23:47 +08007370
7371 ssl->session_negotiate->peer_cert_digest_type =
7372 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
7373 ssl->session_negotiate->peer_cert_digest_len =
7374 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
7375
Gilles Peskine449bd832023-01-11 14:50:10 +01007376 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007377}
7378
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007379MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007380static int ssl_remember_peer_pubkey(mbedtls_ssl_context *ssl,
7381 unsigned char *start, size_t len)
Jerry Yud9526692022-02-17 14:23:47 +08007382{
7383 unsigned char *end = start + len;
7384 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7385
7386 /* Make a copy of the peer's raw public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007387 mbedtls_pk_init(&ssl->handshake->peer_pubkey);
7388 ret = mbedtls_pk_parse_subpubkey(&start, end,
7389 &ssl->handshake->peer_pubkey);
7390 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007391 /* We should have parsed the public key before. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007392 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007393 }
7394
Gilles Peskine449bd832023-01-11 14:50:10 +01007395 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08007396}
7397#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7398
Gilles Peskine449bd832023-01-11 14:50:10 +01007399int mbedtls_ssl_parse_certificate(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08007400{
7401 int ret = 0;
7402 int crt_expected;
7403#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7404 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
7405 ? ssl->handshake->sni_authmode
7406 : ssl->conf->authmode;
7407#else
7408 const int authmode = ssl->conf->authmode;
7409#endif
7410 void *rs_ctx = NULL;
7411 mbedtls_x509_crt *chain = NULL;
7412
Gilles Peskine449bd832023-01-11 14:50:10 +01007413 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007414
Gilles Peskine449bd832023-01-11 14:50:10 +01007415 crt_expected = ssl_parse_certificate_coordinate(ssl, authmode);
7416 if (crt_expected == SSL_CERTIFICATE_SKIP) {
7417 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007418 goto exit;
7419 }
7420
7421#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007422 if (ssl->handshake->ecrs_enabled &&
7423 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify) {
Jerry Yud9526692022-02-17 14:23:47 +08007424 chain = ssl->handshake->ecrs_peer_cert;
7425 ssl->handshake->ecrs_peer_cert = NULL;
7426 goto crt_verify;
7427 }
7428#endif
7429
Gilles Peskine449bd832023-01-11 14:50:10 +01007430 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007431 /* mbedtls_ssl_read_record may have sent an alert already. We
7432 let it decide whether to alert. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007433 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
Jerry Yud9526692022-02-17 14:23:47 +08007434 goto exit;
7435 }
7436
7437#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007438 if (ssl_srv_check_client_no_crt_notification(ssl) == 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007439 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
7440
Gilles Peskine449bd832023-01-11 14:50:10 +01007441 if (authmode != MBEDTLS_SSL_VERIFY_OPTIONAL) {
Jerry Yud9526692022-02-17 14:23:47 +08007442 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Gilles Peskine449bd832023-01-11 14:50:10 +01007443 }
Jerry Yud9526692022-02-17 14:23:47 +08007444
7445 goto exit;
7446 }
7447#endif /* MBEDTLS_SSL_SRV_C */
7448
7449 /* Clear existing peer CRT structure in case we tried to
7450 * reuse a session but it failed, and allocate a new one. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007451 ssl_clear_peer_cert(ssl->session_negotiate);
Jerry Yud9526692022-02-17 14:23:47 +08007452
Gilles Peskine449bd832023-01-11 14:50:10 +01007453 chain = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
7454 if (chain == NULL) {
7455 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed",
7456 sizeof(mbedtls_x509_crt)));
7457 mbedtls_ssl_send_alert_message(ssl,
7458 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7459 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
Jerry Yud9526692022-02-17 14:23:47 +08007460
7461 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
7462 goto exit;
7463 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007464 mbedtls_x509_crt_init(chain);
Jerry Yud9526692022-02-17 14:23:47 +08007465
Gilles Peskine449bd832023-01-11 14:50:10 +01007466 ret = ssl_parse_certificate_chain(ssl, chain);
7467 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007468 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007469 }
Jerry Yud9526692022-02-17 14:23:47 +08007470
7471#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007472 if (ssl->handshake->ecrs_enabled) {
Jerry Yud9526692022-02-17 14:23:47 +08007473 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Gilles Peskine449bd832023-01-11 14:50:10 +01007474 }
Jerry Yud9526692022-02-17 14:23:47 +08007475
7476crt_verify:
Gilles Peskine449bd832023-01-11 14:50:10 +01007477 if (ssl->handshake->ecrs_enabled) {
Jerry Yud9526692022-02-17 14:23:47 +08007478 rs_ctx = &ssl->handshake->ecrs_ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +01007479 }
Jerry Yud9526692022-02-17 14:23:47 +08007480#endif
7481
Gilles Peskine449bd832023-01-11 14:50:10 +01007482 ret = ssl_parse_certificate_verify(ssl, authmode,
7483 chain, rs_ctx);
7484 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007485 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007486 }
Jerry Yud9526692022-02-17 14:23:47 +08007487
7488#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7489 {
7490 unsigned char *crt_start, *pk_start;
7491 size_t crt_len, pk_len;
7492
7493 /* We parse the CRT chain without copying, so
7494 * these pointers point into the input buffer,
7495 * and are hence still valid after freeing the
7496 * CRT chain. */
7497
7498 crt_start = chain->raw.p;
7499 crt_len = chain->raw.len;
7500
7501 pk_start = chain->pk_raw.p;
7502 pk_len = chain->pk_raw.len;
7503
7504 /* Free the CRT structures before computing
7505 * digest and copying the peer's public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007506 mbedtls_x509_crt_free(chain);
7507 mbedtls_free(chain);
Jerry Yud9526692022-02-17 14:23:47 +08007508 chain = NULL;
7509
Gilles Peskine449bd832023-01-11 14:50:10 +01007510 ret = ssl_remember_peer_crt_digest(ssl, crt_start, crt_len);
7511 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007512 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007513 }
Jerry Yud9526692022-02-17 14:23:47 +08007514
Gilles Peskine449bd832023-01-11 14:50:10 +01007515 ret = ssl_remember_peer_pubkey(ssl, pk_start, pk_len);
7516 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007517 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007518 }
Jerry Yud9526692022-02-17 14:23:47 +08007519 }
7520#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7521 /* Pass ownership to session structure. */
7522 ssl->session_negotiate->peer_cert = chain;
7523 chain = NULL;
7524#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7525
Gilles Peskine449bd832023-01-11 14:50:10 +01007526 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007527
7528exit:
7529
Gilles Peskine449bd832023-01-11 14:50:10 +01007530 if (ret == 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007531 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01007532 }
Jerry Yud9526692022-02-17 14:23:47 +08007533
7534#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007535 if (ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS) {
Jerry Yud9526692022-02-17 14:23:47 +08007536 ssl->handshake->ecrs_peer_cert = chain;
7537 chain = NULL;
7538 }
7539#endif
7540
Gilles Peskine449bd832023-01-11 14:50:10 +01007541 if (chain != NULL) {
7542 mbedtls_x509_crt_free(chain);
7543 mbedtls_free(chain);
Jerry Yud9526692022-02-17 14:23:47 +08007544 }
7545
Gilles Peskine449bd832023-01-11 14:50:10 +01007546 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007547}
7548#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
7549
Andrzej Kurek25f27152022-08-17 16:09:31 -04007550#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01007551static int ssl_calc_finished_tls_sha256(
Gilles Peskine449bd832023-01-11 14:50:10 +01007552 mbedtls_ssl_context *ssl, unsigned char *buf, int from)
Jerry Yu615bd6f2022-02-17 14:25:15 +08007553{
7554 int len = 12;
7555 const char *sender;
7556 unsigned char padbuf[32];
7557#if defined(MBEDTLS_USE_PSA_CRYPTO)
7558 size_t hash_size;
7559 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
7560 psa_status_t status;
7561#else
7562 mbedtls_sha256_context sha256;
7563#endif
7564
7565 mbedtls_ssl_session *session = ssl->session_negotiate;
Gilles Peskine449bd832023-01-11 14:50:10 +01007566 if (!session) {
Jerry Yu615bd6f2022-02-17 14:25:15 +08007567 session = ssl->session;
Gilles Peskine449bd832023-01-11 14:50:10 +01007568 }
Jerry Yu615bd6f2022-02-17 14:25:15 +08007569
Gilles Peskine449bd832023-01-11 14:50:10 +01007570 sender = (from == MBEDTLS_SSL_IS_CLIENT)
Jerry Yu615bd6f2022-02-17 14:25:15 +08007571 ? "client finished"
7572 : "server finished";
7573
7574#if defined(MBEDTLS_USE_PSA_CRYPTO)
7575 sha256_psa = psa_hash_operation_init();
7576
Gilles Peskine449bd832023-01-11 14:50:10 +01007577 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc PSA finished tls sha256"));
Jerry Yu615bd6f2022-02-17 14:25:15 +08007578
Gilles Peskine449bd832023-01-11 14:50:10 +01007579 status = psa_hash_clone(&ssl->handshake->fin_sha256_psa, &sha256_psa);
7580 if (status != PSA_SUCCESS) {
7581 MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash clone failed"));
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01007582 return 0;
Jerry Yu615bd6f2022-02-17 14:25:15 +08007583 }
7584
Gilles Peskine449bd832023-01-11 14:50:10 +01007585 status = psa_hash_finish(&sha256_psa, padbuf, sizeof(padbuf), &hash_size);
7586 if (status != PSA_SUCCESS) {
7587 MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash finish failed"));
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01007588 return 0;
Jerry Yu615bd6f2022-02-17 14:25:15 +08007589 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007590 MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated padbuf", padbuf, 32);
Jerry Yu615bd6f2022-02-17 14:25:15 +08007591#else
7592
Gilles Peskine449bd832023-01-11 14:50:10 +01007593 mbedtls_sha256_init(&sha256);
Jerry Yu615bd6f2022-02-17 14:25:15 +08007594
Gilles Peskine449bd832023-01-11 14:50:10 +01007595 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc finished tls sha256"));
Jerry Yu615bd6f2022-02-17 14:25:15 +08007596
Gilles Peskine449bd832023-01-11 14:50:10 +01007597 mbedtls_sha256_clone(&sha256, &ssl->handshake->fin_sha256);
Jerry Yu615bd6f2022-02-17 14:25:15 +08007598
7599 /*
7600 * TLSv1.2:
7601 * hash = PRF( master, finished_label,
7602 * Hash( handshake ) )[0.11]
7603 */
7604
7605#if !defined(MBEDTLS_SHA256_ALT)
Gilles Peskine449bd832023-01-11 14:50:10 +01007606 MBEDTLS_SSL_DEBUG_BUF(4, "finished sha2 state", (unsigned char *)
7607 sha256.state, sizeof(sha256.state));
Jerry Yu615bd6f2022-02-17 14:25:15 +08007608#endif
7609
Gilles Peskine449bd832023-01-11 14:50:10 +01007610 mbedtls_sha256_finish(&sha256, padbuf);
7611 mbedtls_sha256_free(&sha256);
Jerry Yu615bd6f2022-02-17 14:25:15 +08007612#endif /* MBEDTLS_USE_PSA_CRYPTO */
7613
Gilles Peskine449bd832023-01-11 14:50:10 +01007614 ssl->handshake->tls_prf(session->master, 48, sender,
7615 padbuf, 32, buf, len);
Jerry Yu615bd6f2022-02-17 14:25:15 +08007616
Gilles Peskine449bd832023-01-11 14:50:10 +01007617 MBEDTLS_SSL_DEBUG_BUF(3, "calc finished result", buf, len);
Jerry Yu615bd6f2022-02-17 14:25:15 +08007618
Gilles Peskine449bd832023-01-11 14:50:10 +01007619 mbedtls_platform_zeroize(padbuf, sizeof(padbuf));
Jerry Yu615bd6f2022-02-17 14:25:15 +08007620
Gilles Peskine449bd832023-01-11 14:50:10 +01007621 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc finished"));
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01007622 return 0;
Jerry Yu615bd6f2022-02-17 14:25:15 +08007623}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04007624#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yu615bd6f2022-02-17 14:25:15 +08007625
Jerry Yub7ba49e2022-02-17 14:25:53 +08007626
Andrzej Kurek25f27152022-08-17 16:09:31 -04007627#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01007628static int ssl_calc_finished_tls_sha384(
Gilles Peskine449bd832023-01-11 14:50:10 +01007629 mbedtls_ssl_context *ssl, unsigned char *buf, int from)
Jerry Yub7ba49e2022-02-17 14:25:53 +08007630{
7631 int len = 12;
7632 const char *sender;
7633 unsigned char padbuf[48];
7634#if defined(MBEDTLS_USE_PSA_CRYPTO)
7635 size_t hash_size;
7636 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
7637 psa_status_t status;
7638#else
7639 mbedtls_sha512_context sha512;
7640#endif
7641
7642 mbedtls_ssl_session *session = ssl->session_negotiate;
Gilles Peskine449bd832023-01-11 14:50:10 +01007643 if (!session) {
Jerry Yub7ba49e2022-02-17 14:25:53 +08007644 session = ssl->session;
Gilles Peskine449bd832023-01-11 14:50:10 +01007645 }
Jerry Yub7ba49e2022-02-17 14:25:53 +08007646
Gilles Peskine449bd832023-01-11 14:50:10 +01007647 sender = (from == MBEDTLS_SSL_IS_CLIENT)
Jerry Yub7ba49e2022-02-17 14:25:53 +08007648 ? "client finished"
7649 : "server finished";
7650
7651#if defined(MBEDTLS_USE_PSA_CRYPTO)
7652 sha384_psa = psa_hash_operation_init();
7653
Gilles Peskine449bd832023-01-11 14:50:10 +01007654 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc PSA finished tls sha384"));
Jerry Yub7ba49e2022-02-17 14:25:53 +08007655
Gilles Peskine449bd832023-01-11 14:50:10 +01007656 status = psa_hash_clone(&ssl->handshake->fin_sha384_psa, &sha384_psa);
7657 if (status != PSA_SUCCESS) {
7658 MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash clone failed"));
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01007659 return 0;
Jerry Yub7ba49e2022-02-17 14:25:53 +08007660 }
7661
Gilles Peskine449bd832023-01-11 14:50:10 +01007662 status = psa_hash_finish(&sha384_psa, padbuf, sizeof(padbuf), &hash_size);
7663 if (status != PSA_SUCCESS) {
7664 MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash finish failed"));
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01007665 return 0;
Jerry Yub7ba49e2022-02-17 14:25:53 +08007666 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007667 MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated padbuf", padbuf, 48);
Jerry Yub7ba49e2022-02-17 14:25:53 +08007668#else
Gilles Peskine449bd832023-01-11 14:50:10 +01007669 mbedtls_sha512_init(&sha512);
Jerry Yub7ba49e2022-02-17 14:25:53 +08007670
Gilles Peskine449bd832023-01-11 14:50:10 +01007671 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc finished tls sha384"));
Jerry Yub7ba49e2022-02-17 14:25:53 +08007672
Gilles Peskine449bd832023-01-11 14:50:10 +01007673 mbedtls_sha512_clone(&sha512, &ssl->handshake->fin_sha384);
Jerry Yub7ba49e2022-02-17 14:25:53 +08007674
7675 /*
7676 * TLSv1.2:
7677 * hash = PRF( master, finished_label,
7678 * Hash( handshake ) )[0.11]
7679 */
7680
7681#if !defined(MBEDTLS_SHA512_ALT)
Gilles Peskine449bd832023-01-11 14:50:10 +01007682 MBEDTLS_SSL_DEBUG_BUF(4, "finished sha512 state", (unsigned char *)
7683 sha512.state, sizeof(sha512.state));
Jerry Yub7ba49e2022-02-17 14:25:53 +08007684#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01007685 mbedtls_sha512_finish(&sha512, padbuf);
Jerry Yub7ba49e2022-02-17 14:25:53 +08007686
Gilles Peskine449bd832023-01-11 14:50:10 +01007687 mbedtls_sha512_free(&sha512);
Jerry Yub7ba49e2022-02-17 14:25:53 +08007688#endif
7689
Gilles Peskine449bd832023-01-11 14:50:10 +01007690 ssl->handshake->tls_prf(session->master, 48, sender,
7691 padbuf, 48, buf, len);
Jerry Yub7ba49e2022-02-17 14:25:53 +08007692
Gilles Peskine449bd832023-01-11 14:50:10 +01007693 MBEDTLS_SSL_DEBUG_BUF(3, "calc finished result", buf, len);
Jerry Yub7ba49e2022-02-17 14:25:53 +08007694
Gilles Peskine449bd832023-01-11 14:50:10 +01007695 mbedtls_platform_zeroize(padbuf, sizeof(padbuf));
Jerry Yub7ba49e2022-02-17 14:25:53 +08007696
Gilles Peskine449bd832023-01-11 14:50:10 +01007697 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc finished"));
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01007698 return 0;
Jerry Yub7ba49e2022-02-17 14:25:53 +08007699}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04007700#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yub7ba49e2022-02-17 14:25:53 +08007701
Gilles Peskine449bd832023-01-11 14:50:10 +01007702void mbedtls_ssl_handshake_wrapup_free_hs_transform(mbedtls_ssl_context *ssl)
Jerry Yuaef00152022-02-17 14:27:31 +08007703{
Gilles Peskine449bd832023-01-11 14:50:10 +01007704 MBEDTLS_SSL_DEBUG_MSG(3, ("=> handshake wrapup: final free"));
Jerry Yuaef00152022-02-17 14:27:31 +08007705
7706 /*
7707 * Free our handshake params
7708 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007709 mbedtls_ssl_handshake_free(ssl);
7710 mbedtls_free(ssl->handshake);
Jerry Yuaef00152022-02-17 14:27:31 +08007711 ssl->handshake = NULL;
7712
7713 /*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08007714 * Free the previous transform and switch in the current one
Jerry Yuaef00152022-02-17 14:27:31 +08007715 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007716 if (ssl->transform) {
7717 mbedtls_ssl_transform_free(ssl->transform);
7718 mbedtls_free(ssl->transform);
Jerry Yuaef00152022-02-17 14:27:31 +08007719 }
7720 ssl->transform = ssl->transform_negotiate;
7721 ssl->transform_negotiate = NULL;
7722
Gilles Peskine449bd832023-01-11 14:50:10 +01007723 MBEDTLS_SSL_DEBUG_MSG(3, ("<= handshake wrapup: final free"));
Jerry Yuaef00152022-02-17 14:27:31 +08007724}
7725
Gilles Peskine449bd832023-01-11 14:50:10 +01007726void mbedtls_ssl_handshake_wrapup(mbedtls_ssl_context *ssl)
Jerry Yu2a9fff52022-02-17 14:28:51 +08007727{
7728 int resume = ssl->handshake->resume;
7729
Gilles Peskine449bd832023-01-11 14:50:10 +01007730 MBEDTLS_SSL_DEBUG_MSG(3, ("=> handshake wrapup"));
Jerry Yu2a9fff52022-02-17 14:28:51 +08007731
7732#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01007733 if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
Jerry Yu2a9fff52022-02-17 14:28:51 +08007734 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
7735 ssl->renego_records_seen = 0;
7736 }
7737#endif
7738
7739 /*
7740 * Free the previous session and switch in the current one
7741 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007742 if (ssl->session) {
Jerry Yu2a9fff52022-02-17 14:28:51 +08007743#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
7744 /* RFC 7366 3.1: keep the EtM state */
7745 ssl->session_negotiate->encrypt_then_mac =
Gilles Peskine449bd832023-01-11 14:50:10 +01007746 ssl->session->encrypt_then_mac;
Jerry Yu2a9fff52022-02-17 14:28:51 +08007747#endif
7748
Gilles Peskine449bd832023-01-11 14:50:10 +01007749 mbedtls_ssl_session_free(ssl->session);
7750 mbedtls_free(ssl->session);
Jerry Yu2a9fff52022-02-17 14:28:51 +08007751 }
7752 ssl->session = ssl->session_negotiate;
7753 ssl->session_negotiate = NULL;
7754
7755 /*
7756 * Add cache entry
7757 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007758 if (ssl->conf->f_set_cache != NULL &&
Jerry Yu2a9fff52022-02-17 14:28:51 +08007759 ssl->session->id_len != 0 &&
Gilles Peskine449bd832023-01-11 14:50:10 +01007760 resume == 0) {
7761 if (ssl->conf->f_set_cache(ssl->conf->p_cache,
7762 ssl->session->id,
7763 ssl->session->id_len,
7764 ssl->session) != 0) {
7765 MBEDTLS_SSL_DEBUG_MSG(1, ("cache did not store session"));
7766 }
Jerry Yu2a9fff52022-02-17 14:28:51 +08007767 }
7768
7769#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007770 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7771 ssl->handshake->flight != NULL) {
Jerry Yu2a9fff52022-02-17 14:28:51 +08007772 /* Cancel handshake timer */
Gilles Peskine449bd832023-01-11 14:50:10 +01007773 mbedtls_ssl_set_timer(ssl, 0);
Jerry Yu2a9fff52022-02-17 14:28:51 +08007774
7775 /* Keep last flight around in case we need to resend it:
7776 * we need the handshake and transform structures for that */
Gilles Peskine449bd832023-01-11 14:50:10 +01007777 MBEDTLS_SSL_DEBUG_MSG(3, ("skip freeing handshake and transform"));
7778 } else
Jerry Yu2a9fff52022-02-17 14:28:51 +08007779#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01007780 mbedtls_ssl_handshake_wrapup_free_hs_transform(ssl);
Jerry Yu2a9fff52022-02-17 14:28:51 +08007781
Jerry Yu5ed73ff2022-10-27 13:08:42 +08007782 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
Jerry Yu2a9fff52022-02-17 14:28:51 +08007783
Gilles Peskine449bd832023-01-11 14:50:10 +01007784 MBEDTLS_SSL_DEBUG_MSG(3, ("<= handshake wrapup"));
Jerry Yu2a9fff52022-02-17 14:28:51 +08007785}
7786
Gilles Peskine449bd832023-01-11 14:50:10 +01007787int mbedtls_ssl_write_finished(mbedtls_ssl_context *ssl)
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007788{
7789 int ret, hash_len;
7790
Gilles Peskine449bd832023-01-11 14:50:10 +01007791 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write finished"));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007792
Gilles Peskine449bd832023-01-11 14:50:10 +01007793 mbedtls_ssl_update_out_pointers(ssl, ssl->transform_negotiate);
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007794
Gilles Peskine449bd832023-01-11 14:50:10 +01007795 ssl->handshake->calc_finished(ssl, ssl->out_msg + 4, ssl->conf->endpoint);
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007796
7797 /*
7798 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7799 * may define some other value. Currently (early 2016), no defined
7800 * ciphersuite does this (and this is unlikely to change as activity has
7801 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7802 */
7803 hash_len = 12;
7804
7805#if defined(MBEDTLS_SSL_RENEGOTIATION)
7806 ssl->verify_data_len = hash_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01007807 memcpy(ssl->own_verify_data, ssl->out_msg + 4, hash_len);
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007808#endif
7809
7810 ssl->out_msglen = 4 + hash_len;
7811 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7812 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
7813
7814 /*
7815 * In case of session resuming, invert the client and server
7816 * ChangeCipherSpec messages order.
7817 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007818 if (ssl->handshake->resume != 0) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007819#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007820 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007821 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Gilles Peskine449bd832023-01-11 14:50:10 +01007822 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007823#endif
7824#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007825 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007826 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Gilles Peskine449bd832023-01-11 14:50:10 +01007827 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007828#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01007829 } else {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007830 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01007831 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007832
7833 /*
7834 * Switch to our negotiated transform and session parameters for outbound
7835 * data.
7836 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007837 MBEDTLS_SSL_DEBUG_MSG(3, ("switching to new transform spec for outbound data"));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007838
7839#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007840 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007841 unsigned char i;
7842
7843 /* Remember current epoch settings for resending */
7844 ssl->handshake->alt_transform_out = ssl->transform_out;
Gilles Peskine449bd832023-01-11 14:50:10 +01007845 memcpy(ssl->handshake->alt_out_ctr, ssl->cur_out_ctr,
7846 sizeof(ssl->handshake->alt_out_ctr));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007847
7848 /* Set sequence_number to zero */
Gilles Peskine449bd832023-01-11 14:50:10 +01007849 memset(&ssl->cur_out_ctr[2], 0, sizeof(ssl->cur_out_ctr) - 2);
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007850
7851
7852 /* Increment epoch */
Gilles Peskine449bd832023-01-11 14:50:10 +01007853 for (i = 2; i > 0; i--) {
7854 if (++ssl->cur_out_ctr[i - 1] != 0) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007855 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01007856 }
7857 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007858
7859 /* The loop goes to its end iff the counter is wrapping */
Gilles Peskine449bd832023-01-11 14:50:10 +01007860 if (i == 0) {
7861 MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS epoch would wrap"));
7862 return MBEDTLS_ERR_SSL_COUNTER_WRAPPING;
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007863 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007864 } else
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007865#endif /* MBEDTLS_SSL_PROTO_DTLS */
Gilles Peskine449bd832023-01-11 14:50:10 +01007866 memset(ssl->cur_out_ctr, 0, sizeof(ssl->cur_out_ctr));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007867
7868 ssl->transform_out = ssl->transform_negotiate;
7869 ssl->session_out = ssl->session_negotiate;
7870
7871#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007872 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
7873 mbedtls_ssl_send_flight_completed(ssl);
7874 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007875#endif
7876
Gilles Peskine449bd832023-01-11 14:50:10 +01007877 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
7878 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
7879 return ret;
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007880 }
7881
7882#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007883 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7884 (ret = mbedtls_ssl_flight_transmit(ssl)) != 0) {
7885 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_flight_transmit", ret);
7886 return ret;
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007887 }
7888#endif
7889
Gilles Peskine449bd832023-01-11 14:50:10 +01007890 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write finished"));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007891
Gilles Peskine449bd832023-01-11 14:50:10 +01007892 return 0;
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007893}
7894
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007895#define SSL_MAX_HASH_LEN 12
7896
Gilles Peskine449bd832023-01-11 14:50:10 +01007897int mbedtls_ssl_parse_finished(mbedtls_ssl_context *ssl)
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007898{
7899 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7900 unsigned int hash_len = 12;
7901 unsigned char buf[SSL_MAX_HASH_LEN];
7902
Gilles Peskine449bd832023-01-11 14:50:10 +01007903 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse finished"));
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007904
Gilles Peskine449bd832023-01-11 14:50:10 +01007905 ssl->handshake->calc_finished(ssl, buf, ssl->conf->endpoint ^ 1);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007906
Gilles Peskine449bd832023-01-11 14:50:10 +01007907 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
7908 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007909 goto exit;
7910 }
7911
Gilles Peskine449bd832023-01-11 14:50:10 +01007912 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
7913 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
7914 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7915 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007916 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
7917 goto exit;
7918 }
7919
Gilles Peskine449bd832023-01-11 14:50:10 +01007920 if (ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED) {
7921 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7922 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007923 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
7924 goto exit;
7925 }
7926
Gilles Peskine449bd832023-01-11 14:50:10 +01007927 if (ssl->in_hslen != mbedtls_ssl_hs_hdr_len(ssl) + hash_len) {
7928 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
7929 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7930 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007931 ret = MBEDTLS_ERR_SSL_DECODE_ERROR;
7932 goto exit;
7933 }
7934
Gilles Peskine449bd832023-01-11 14:50:10 +01007935 if (mbedtls_ct_memcmp(ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl),
7936 buf, hash_len) != 0) {
7937 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
7938 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7939 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007940 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
7941 goto exit;
7942 }
7943
7944#if defined(MBEDTLS_SSL_RENEGOTIATION)
7945 ssl->verify_data_len = hash_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01007946 memcpy(ssl->peer_verify_data, buf, hash_len);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007947#endif
7948
Gilles Peskine449bd832023-01-11 14:50:10 +01007949 if (ssl->handshake->resume != 0) {
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007950#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007951 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007952 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Gilles Peskine449bd832023-01-11 14:50:10 +01007953 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007954#endif
7955#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007956 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007957 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Gilles Peskine449bd832023-01-11 14:50:10 +01007958 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007959#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01007960 } else {
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007961 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01007962 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007963
7964#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007965 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
7966 mbedtls_ssl_recv_flight_completed(ssl);
7967 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007968#endif
7969
Gilles Peskine449bd832023-01-11 14:50:10 +01007970 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse finished"));
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007971
7972exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007973 mbedtls_platform_zeroize(buf, hash_len);
7974 return ret;
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007975}
7976
Jerry Yu392112c2022-02-17 14:34:10 +08007977#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
7978/*
7979 * Helper to get TLS 1.2 PRF from ciphersuite
7980 * (Duplicates bits of logic from ssl_set_handshake_prfs().)
7981 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007982static tls_prf_fn ssl_tls12prf_from_cs(int ciphersuite_id)
Jerry Yu392112c2022-02-17 14:34:10 +08007983{
Jerry Yu392112c2022-02-17 14:34:10 +08007984 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01007985 mbedtls_ssl_ciphersuite_from_id(ciphersuite_id);
Andrzej Kurek68327742022-10-03 06:18:18 -04007986#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01007987 if (ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA384) {
7988 return tls_prf_sha384;
7989 } else
Jerry Yu392112c2022-02-17 14:34:10 +08007990#endif
Andrzej Kurek894edde2022-09-29 06:31:14 -04007991#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
7992 {
Gilles Peskine449bd832023-01-11 14:50:10 +01007993 if (ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA256) {
7994 return tls_prf_sha256;
7995 }
Andrzej Kurek894edde2022-09-29 06:31:14 -04007996 }
7997#endif
7998#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
7999 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
8000 (void) ciphersuite_info;
8001#endif
Andrzej Kurekeabeb302022-10-17 07:52:51 -04008002
Gilles Peskine449bd832023-01-11 14:50:10 +01008003 return NULL;
Jerry Yu392112c2022-02-17 14:34:10 +08008004}
8005#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Jerry Yue93ffcd2022-02-17 14:37:06 +08008006
Gilles Peskine449bd832023-01-11 14:50:10 +01008007static mbedtls_tls_prf_types tls_prf_get_type(mbedtls_ssl_tls_prf_cb *tls_prf)
Jerry Yue93ffcd2022-02-17 14:37:06 +08008008{
8009 ((void) tls_prf);
Andrzej Kurek25f27152022-08-17 16:09:31 -04008010#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01008011 if (tls_prf == tls_prf_sha384) {
8012 return MBEDTLS_SSL_TLS_PRF_SHA384;
8013 } else
Jerry Yue93ffcd2022-02-17 14:37:06 +08008014#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04008015#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01008016 if (tls_prf == tls_prf_sha256) {
8017 return MBEDTLS_SSL_TLS_PRF_SHA256;
8018 } else
Jerry Yue93ffcd2022-02-17 14:37:06 +08008019#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01008020 return MBEDTLS_SSL_TLS_PRF_NONE;
Jerry Yue93ffcd2022-02-17 14:37:06 +08008021}
8022
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008023/*
8024 * Populate a transform structure with session keys and all the other
8025 * necessary information.
8026 *
8027 * Parameters:
8028 * - [in/out]: transform: structure to populate
8029 * [in] must be just initialised with mbedtls_ssl_transform_init()
8030 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
8031 * - [in] ciphersuite
8032 * - [in] master
8033 * - [in] encrypt_then_mac
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008034 * - [in] tls_prf: pointer to PRF to use for key derivation
8035 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
Glenn Strauss07c64162022-03-14 12:34:51 -04008036 * - [in] tls_version: TLS version
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008037 * - [in] endpoint: client or server
8038 * - [in] ssl: used for:
8039 * - ssl->conf->{f,p}_export_keys
8040 * [in] optionally used for:
8041 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
8042 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02008043MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01008044static int ssl_tls12_populate_transform(mbedtls_ssl_transform *transform,
8045 int ciphersuite,
8046 const unsigned char master[48],
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008047#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01008048 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008049#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01008050 ssl_tls_prf_t tls_prf,
8051 const unsigned char randbytes[64],
8052 mbedtls_ssl_protocol_version tls_version,
8053 unsigned endpoint,
8054 const mbedtls_ssl_context *ssl)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008055{
8056 int ret = 0;
8057 unsigned char keyblk[256];
8058 unsigned char *key1;
8059 unsigned char *key2;
8060 unsigned char *mac_enc;
8061 unsigned char *mac_dec;
8062 size_t mac_key_len = 0;
8063 size_t iv_copy_len;
8064 size_t keylen;
8065 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008066 mbedtls_ssl_mode_t ssl_mode;
Neil Armstronge4512952022-03-08 09:08:22 +01008067#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008068 const mbedtls_cipher_info_t *cipher_info;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008069 const mbedtls_md_info_t *md_info;
Neil Armstronge4512952022-03-08 09:08:22 +01008070#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008071
8072#if defined(MBEDTLS_USE_PSA_CRYPTO)
8073 psa_key_type_t key_type;
8074 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8075 psa_algorithm_t alg;
Neil Armstronge4512952022-03-08 09:08:22 +01008076 psa_algorithm_t mac_alg = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008077 size_t key_bits;
8078 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
8079#endif
8080
8081#if !defined(MBEDTLS_DEBUG_C) && \
8082 !defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Gilles Peskine449bd832023-01-11 14:50:10 +01008083 if (ssl->f_export_keys == NULL) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008084 ssl = NULL; /* make sure we don't use it except for these cases */
8085 (void) ssl;
8086 }
8087#endif
8088
8089 /*
8090 * Some data just needs copying into the structure
8091 */
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008092#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008093 transform->encrypt_then_mac = encrypt_then_mac;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008094#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Glenn Strauss07c64162022-03-14 12:34:51 -04008095 transform->tls_version = tls_version;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008096
8097#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01008098 memcpy(transform->randbytes, randbytes, sizeof(transform->randbytes));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008099#endif
8100
8101#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01008102 if (tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008103 /* At the moment, we keep TLS <= 1.2 and TLS 1.3 transform
8104 * generation separate. This should never happen. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008105 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008106 }
8107#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
8108
8109 /*
8110 * Get various info structures
8111 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008112 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(ciphersuite);
8113 if (ciphersuite_info == NULL) {
8114 MBEDTLS_SSL_DEBUG_MSG(1, ("ciphersuite info for %d not found",
8115 ciphersuite));
8116 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008117 }
8118
Neil Armstrongab555e02022-04-04 11:07:59 +02008119 ssl_mode = mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008120#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01008121 encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008122#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01008123 ciphersuite_info);
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008124
Gilles Peskine449bd832023-01-11 14:50:10 +01008125 if (ssl_mode == MBEDTLS_SSL_MODE_AEAD) {
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008126 transform->taglen =
8127 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Gilles Peskine449bd832023-01-11 14:50:10 +01008128 }
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008129
8130#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008131 if ((status = mbedtls_ssl_cipher_to_psa(ciphersuite_info->cipher,
8132 transform->taglen,
8133 &alg,
8134 &key_type,
8135 &key_bits)) != PSA_SUCCESS) {
8136 ret = psa_ssl_status_to_mbedtls(status);
8137 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_cipher_to_psa", ret);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008138 goto end;
8139 }
8140#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008141 cipher_info = mbedtls_cipher_info_from_type(ciphersuite_info->cipher);
8142 if (cipher_info == NULL) {
8143 MBEDTLS_SSL_DEBUG_MSG(1, ("cipher info for %u not found",
8144 ciphersuite_info->cipher));
8145 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008146 }
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008147#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008148
Neil Armstronge4512952022-03-08 09:08:22 +01008149#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008150 mac_alg = mbedtls_hash_info_psa_from_md(ciphersuite_info->mac);
8151 if (mac_alg == 0) {
8152 MBEDTLS_SSL_DEBUG_MSG(1, ("mbedtls_hash_info_psa_from_md for %u not found",
8153 (unsigned) ciphersuite_info->mac));
8154 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Neil Armstronge4512952022-03-08 09:08:22 +01008155 }
8156#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008157 md_info = mbedtls_md_info_from_type(ciphersuite_info->mac);
8158 if (md_info == NULL) {
8159 MBEDTLS_SSL_DEBUG_MSG(1, ("mbedtls_md info for %u not found",
8160 (unsigned) ciphersuite_info->mac));
8161 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008162 }
Neil Armstronge4512952022-03-08 09:08:22 +01008163#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008164
8165#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
8166 /* Copy own and peer's CID if the use of the CID
8167 * extension has been negotiated. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008168 if (ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED) {
8169 MBEDTLS_SSL_DEBUG_MSG(3, ("Copy CIDs into SSL transform"));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008170
8171 transform->in_cid_len = ssl->own_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008172 memcpy(transform->in_cid, ssl->own_cid, ssl->own_cid_len);
8173 MBEDTLS_SSL_DEBUG_BUF(3, "Incoming CID", transform->in_cid,
8174 transform->in_cid_len);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008175
8176 transform->out_cid_len = ssl->handshake->peer_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008177 memcpy(transform->out_cid, ssl->handshake->peer_cid,
8178 ssl->handshake->peer_cid_len);
8179 MBEDTLS_SSL_DEBUG_BUF(3, "Outgoing CID", transform->out_cid,
8180 transform->out_cid_len);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008181 }
8182#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
8183
8184 /*
8185 * Compute key block using the PRF
8186 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008187 ret = tls_prf(master, 48, "key expansion", randbytes, 64, keyblk, 256);
8188 if (ret != 0) {
8189 MBEDTLS_SSL_DEBUG_RET(1, "prf", ret);
8190 return ret;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008191 }
8192
Gilles Peskine449bd832023-01-11 14:50:10 +01008193 MBEDTLS_SSL_DEBUG_MSG(3, ("ciphersuite = %s",
8194 mbedtls_ssl_get_ciphersuite_name(ciphersuite)));
8195 MBEDTLS_SSL_DEBUG_BUF(3, "master secret", master, 48);
8196 MBEDTLS_SSL_DEBUG_BUF(4, "random bytes", randbytes, 64);
8197 MBEDTLS_SSL_DEBUG_BUF(4, "key block", keyblk, 256);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008198
8199 /*
8200 * Determine the appropriate key, IV and MAC length.
8201 */
8202
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008203#if defined(MBEDTLS_USE_PSA_CRYPTO)
8204 keylen = PSA_BITS_TO_BYTES(key_bits);
8205#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008206 keylen = mbedtls_cipher_info_get_key_bitlen(cipher_info) / 8;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008207#endif
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008208
8209#if defined(MBEDTLS_GCM_C) || \
8210 defined(MBEDTLS_CCM_C) || \
8211 defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008212 if (ssl_mode == MBEDTLS_SSL_MODE_AEAD) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008213 size_t explicit_ivlen;
8214
8215 transform->maclen = 0;
8216 mac_key_len = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008217
8218 /* All modes haves 96-bit IVs, but the length of the static parts vary
8219 * with mode and version:
8220 * - For GCM and CCM in TLS 1.2, there's a static IV of 4 Bytes
8221 * (to be concatenated with a dynamically chosen IV of 8 Bytes)
8222 * - For ChaChaPoly in TLS 1.2, and all modes in TLS 1.3, there's
8223 * a static IV of 12 Bytes (to be XOR'ed with the 8 Byte record
8224 * sequence number).
8225 */
8226 transform->ivlen = 12;
David Horstmann3b2276a2022-10-06 14:49:08 +01008227
8228 int is_chachapoly = 0;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008229#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008230 is_chachapoly = (key_type == PSA_KEY_TYPE_CHACHA20);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008231#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008232 is_chachapoly = (mbedtls_cipher_info_get_mode(cipher_info)
8233 == MBEDTLS_MODE_CHACHAPOLY);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008234#endif /* MBEDTLS_USE_PSA_CRYPTO */
David Horstmann3b2276a2022-10-06 14:49:08 +01008235
Gilles Peskine449bd832023-01-11 14:50:10 +01008236 if (is_chachapoly) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008237 transform->fixed_ivlen = 12;
Gilles Peskine449bd832023-01-11 14:50:10 +01008238 } else {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008239 transform->fixed_ivlen = 4;
Gilles Peskine449bd832023-01-11 14:50:10 +01008240 }
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008241
8242 /* Minimum length of encrypted record */
8243 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
8244 transform->minlen = explicit_ivlen + transform->taglen;
Gilles Peskine449bd832023-01-11 14:50:10 +01008245 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008246#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
8247#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01008248 if (ssl_mode == MBEDTLS_SSL_MODE_STREAM ||
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008249 ssl_mode == MBEDTLS_SSL_MODE_CBC ||
Gilles Peskine449bd832023-01-11 14:50:10 +01008250 ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM) {
Neil Armstronge4512952022-03-08 09:08:22 +01008251#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008252 size_t block_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008253#else
8254 size_t block_size = cipher_info->block_size;
8255#endif /* MBEDTLS_USE_PSA_CRYPTO */
8256
8257#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronge4512952022-03-08 09:08:22 +01008258 /* Get MAC length */
8259 mac_key_len = PSA_HASH_LENGTH(mac_alg);
8260#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008261 /* Initialize HMAC contexts */
Gilles Peskine449bd832023-01-11 14:50:10 +01008262 if ((ret = mbedtls_md_setup(&transform->md_ctx_enc, md_info, 1)) != 0 ||
8263 (ret = mbedtls_md_setup(&transform->md_ctx_dec, md_info, 1)) != 0) {
8264 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_setup", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008265 goto end;
8266 }
8267
8268 /* Get MAC length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008269 mac_key_len = mbedtls_md_get_size(md_info);
Neil Armstronge4512952022-03-08 09:08:22 +01008270#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008271 transform->maclen = mac_key_len;
8272
8273 /* IV length */
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008274#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008275 transform->ivlen = PSA_CIPHER_IV_LENGTH(key_type, alg);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008276#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008277 transform->ivlen = cipher_info->iv_size;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008278#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008279
8280 /* Minimum length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008281 if (ssl_mode == MBEDTLS_SSL_MODE_STREAM) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008282 transform->minlen = transform->maclen;
Gilles Peskine449bd832023-01-11 14:50:10 +01008283 } else {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008284 /*
8285 * GenericBlockCipher:
8286 * 1. if EtM is in use: one block plus MAC
8287 * otherwise: * first multiple of blocklen greater than maclen
8288 * 2. IV
8289 */
8290#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01008291 if (ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008292 transform->minlen = transform->maclen
Gilles Peskine449bd832023-01-11 14:50:10 +01008293 + block_size;
8294 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008295#endif
8296 {
8297 transform->minlen = transform->maclen
Gilles Peskine449bd832023-01-11 14:50:10 +01008298 + block_size
8299 - transform->maclen % block_size;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008300 }
8301
Gilles Peskine449bd832023-01-11 14:50:10 +01008302 if (tls_version == MBEDTLS_SSL_VERSION_TLS1_2) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008303 transform->minlen += transform->ivlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01008304 } else {
8305 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008306 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
8307 goto end;
8308 }
8309 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008310 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008311#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
8312 {
Gilles Peskine449bd832023-01-11 14:50:10 +01008313 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
8314 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008315 }
8316
Gilles Peskine449bd832023-01-11 14:50:10 +01008317 MBEDTLS_SSL_DEBUG_MSG(3, ("keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
8318 (unsigned) keylen,
8319 (unsigned) transform->minlen,
8320 (unsigned) transform->ivlen,
8321 (unsigned) transform->maclen));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008322
8323 /*
8324 * Finally setup the cipher contexts, IVs and MAC secrets.
8325 */
8326#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008327 if (endpoint == MBEDTLS_SSL_IS_CLIENT) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008328 key1 = keyblk + mac_key_len * 2;
8329 key2 = keyblk + mac_key_len * 2 + keylen;
8330
8331 mac_enc = keyblk;
8332 mac_dec = keyblk + mac_key_len;
8333
Gilles Peskine449bd832023-01-11 14:50:10 +01008334 iv_copy_len = (transform->fixed_ivlen) ?
8335 transform->fixed_ivlen : transform->ivlen;
8336 memcpy(transform->iv_enc, key2 + keylen, iv_copy_len);
8337 memcpy(transform->iv_dec, key2 + keylen + iv_copy_len,
8338 iv_copy_len);
8339 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008340#endif /* MBEDTLS_SSL_CLI_C */
8341#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008342 if (endpoint == MBEDTLS_SSL_IS_SERVER) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008343 key1 = keyblk + mac_key_len * 2 + keylen;
8344 key2 = keyblk + mac_key_len * 2;
8345
8346 mac_enc = keyblk + mac_key_len;
8347 mac_dec = keyblk;
8348
Gilles Peskine449bd832023-01-11 14:50:10 +01008349 iv_copy_len = (transform->fixed_ivlen) ?
8350 transform->fixed_ivlen : transform->ivlen;
8351 memcpy(transform->iv_dec, key1 + keylen, iv_copy_len);
8352 memcpy(transform->iv_enc, key1 + keylen + iv_copy_len,
8353 iv_copy_len);
8354 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008355#endif /* MBEDTLS_SSL_SRV_C */
8356 {
Gilles Peskine449bd832023-01-11 14:50:10 +01008357 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008358 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
8359 goto end;
8360 }
8361
Gilles Peskine449bd832023-01-11 14:50:10 +01008362 if (ssl != NULL && ssl->f_export_keys != NULL) {
8363 ssl->f_export_keys(ssl->p_export_keys,
8364 MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET,
8365 master, 48,
8366 randbytes + 32,
8367 randbytes,
8368 tls_prf_get_type(tls_prf));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008369 }
8370
8371#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008372 transform->psa_alg = alg;
8373
Gilles Peskine449bd832023-01-11 14:50:10 +01008374 if (alg != MBEDTLS_SSL_NULL_CIPHER) {
8375 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
8376 psa_set_key_algorithm(&attributes, alg);
8377 psa_set_key_type(&attributes, key_type);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008378
Gilles Peskine449bd832023-01-11 14:50:10 +01008379 if ((status = psa_import_key(&attributes,
8380 key1,
8381 PSA_BITS_TO_BYTES(key_bits),
8382 &transform->psa_key_enc)) != PSA_SUCCESS) {
8383 MBEDTLS_SSL_DEBUG_RET(3, "psa_import_key", (int) status);
8384 ret = psa_ssl_status_to_mbedtls(status);
8385 MBEDTLS_SSL_DEBUG_RET(1, "psa_import_key", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008386 goto end;
8387 }
8388
Gilles Peskine449bd832023-01-11 14:50:10 +01008389 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008390
Gilles Peskine449bd832023-01-11 14:50:10 +01008391 if ((status = psa_import_key(&attributes,
8392 key2,
8393 PSA_BITS_TO_BYTES(key_bits),
8394 &transform->psa_key_dec)) != PSA_SUCCESS) {
8395 ret = psa_ssl_status_to_mbedtls(status);
8396 MBEDTLS_SSL_DEBUG_RET(1, "psa_import_key", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008397 goto end;
8398 }
8399 }
8400#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008401 if ((ret = mbedtls_cipher_setup(&transform->cipher_ctx_enc,
8402 cipher_info)) != 0) {
8403 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008404 goto end;
8405 }
8406
Gilles Peskine449bd832023-01-11 14:50:10 +01008407 if ((ret = mbedtls_cipher_setup(&transform->cipher_ctx_dec,
8408 cipher_info)) != 0) {
8409 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008410 goto end;
8411 }
8412
Gilles Peskine449bd832023-01-11 14:50:10 +01008413 if ((ret = mbedtls_cipher_setkey(&transform->cipher_ctx_enc, key1,
8414 (int) mbedtls_cipher_info_get_key_bitlen(cipher_info),
8415 MBEDTLS_ENCRYPT)) != 0) {
8416 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setkey", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008417 goto end;
8418 }
8419
Gilles Peskine449bd832023-01-11 14:50:10 +01008420 if ((ret = mbedtls_cipher_setkey(&transform->cipher_ctx_dec, key2,
8421 (int) mbedtls_cipher_info_get_key_bitlen(cipher_info),
8422 MBEDTLS_DECRYPT)) != 0) {
8423 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setkey", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008424 goto end;
8425 }
8426
8427#if defined(MBEDTLS_CIPHER_MODE_CBC)
Gilles Peskine449bd832023-01-11 14:50:10 +01008428 if (mbedtls_cipher_info_get_mode(cipher_info) == MBEDTLS_MODE_CBC) {
8429 if ((ret = mbedtls_cipher_set_padding_mode(&transform->cipher_ctx_enc,
8430 MBEDTLS_PADDING_NONE)) != 0) {
8431 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_set_padding_mode", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008432 goto end;
8433 }
8434
Gilles Peskine449bd832023-01-11 14:50:10 +01008435 if ((ret = mbedtls_cipher_set_padding_mode(&transform->cipher_ctx_dec,
8436 MBEDTLS_PADDING_NONE)) != 0) {
8437 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_set_padding_mode", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008438 goto end;
8439 }
8440 }
8441#endif /* MBEDTLS_CIPHER_MODE_CBC */
8442#endif /* MBEDTLS_USE_PSA_CRYPTO */
8443
Neil Armstrong29c0c042022-03-17 17:47:28 +01008444#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
8445 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
8446 For AEAD-based ciphersuites, there is nothing to do here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008447 if (mac_key_len != 0) {
Neil Armstrong29c0c042022-03-17 17:47:28 +01008448#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008449 transform->psa_mac_alg = PSA_ALG_HMAC(mac_alg);
Neil Armstrong29c0c042022-03-17 17:47:28 +01008450
Gilles Peskine449bd832023-01-11 14:50:10 +01008451 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
8452 psa_set_key_algorithm(&attributes, PSA_ALG_HMAC(mac_alg));
8453 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
Neil Armstrong29c0c042022-03-17 17:47:28 +01008454
Gilles Peskine449bd832023-01-11 14:50:10 +01008455 if ((status = psa_import_key(&attributes,
8456 mac_enc, mac_key_len,
8457 &transform->psa_mac_enc)) != PSA_SUCCESS) {
8458 ret = psa_ssl_status_to_mbedtls(status);
8459 MBEDTLS_SSL_DEBUG_RET(1, "psa_import_mac_key", ret);
Neil Armstrong29c0c042022-03-17 17:47:28 +01008460 goto end;
8461 }
8462
Gilles Peskine449bd832023-01-11 14:50:10 +01008463 if ((transform->psa_alg == MBEDTLS_SSL_NULL_CIPHER) ||
8464 ((transform->psa_alg == PSA_ALG_CBC_NO_PADDING)
Andrzej Kurek8c95ac42022-08-17 16:17:00 -04008465#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01008466 && (transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED)
Andrzej Kurek8c95ac42022-08-17 16:17:00 -04008467#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01008468 )) {
Neil Armstrong29c0c042022-03-17 17:47:28 +01008469 /* mbedtls_ct_hmac() requires the key to be exportable */
Gilles Peskine449bd832023-01-11 14:50:10 +01008470 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT |
8471 PSA_KEY_USAGE_VERIFY_HASH);
8472 } else {
8473 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
8474 }
Neil Armstrong29c0c042022-03-17 17:47:28 +01008475
Gilles Peskine449bd832023-01-11 14:50:10 +01008476 if ((status = psa_import_key(&attributes,
8477 mac_dec, mac_key_len,
8478 &transform->psa_mac_dec)) != PSA_SUCCESS) {
8479 ret = psa_ssl_status_to_mbedtls(status);
8480 MBEDTLS_SSL_DEBUG_RET(1, "psa_import_mac_key", ret);
Neil Armstrong29c0c042022-03-17 17:47:28 +01008481 goto end;
8482 }
8483#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008484 ret = mbedtls_md_hmac_starts(&transform->md_ctx_enc, mac_enc, mac_key_len);
8485 if (ret != 0) {
Neil Armstrong29c0c042022-03-17 17:47:28 +01008486 goto end;
Gilles Peskine449bd832023-01-11 14:50:10 +01008487 }
8488 ret = mbedtls_md_hmac_starts(&transform->md_ctx_dec, mac_dec, mac_key_len);
8489 if (ret != 0) {
Neil Armstrong29c0c042022-03-17 17:47:28 +01008490 goto end;
Gilles Peskine449bd832023-01-11 14:50:10 +01008491 }
Neil Armstrong29c0c042022-03-17 17:47:28 +01008492#endif /* MBEDTLS_USE_PSA_CRYPTO */
8493 }
8494#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
8495
8496 ((void) mac_dec);
8497 ((void) mac_enc);
8498
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008499end:
Gilles Peskine449bd832023-01-11 14:50:10 +01008500 mbedtls_platform_zeroize(keyblk, sizeof(keyblk));
8501 return ret;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008502}
8503
Valerio Settia08b1a42022-11-17 15:10:02 +01008504#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) && \
8505 defined(MBEDTLS_USE_PSA_CRYPTO)
Valerio Setti6b3dab02022-11-17 17:14:54 +01008506int mbedtls_psa_ecjpake_read_round(
Gilles Peskine449bd832023-01-11 14:50:10 +01008507 psa_pake_operation_t *pake_ctx,
8508 const unsigned char *buf,
8509 size_t len, mbedtls_ecjpake_rounds_t round)
Valerio Settia08b1a42022-11-17 15:10:02 +01008510{
8511 psa_status_t status;
8512 size_t input_offset = 0;
Valerio Setti819de862022-11-17 18:05:19 +01008513 /*
Valerio Setti6b3dab02022-11-17 17:14:54 +01008514 * At round one repeat the KEY_SHARE, ZK_PUBLIC & ZF_PROOF twice
8515 * At round two perform a single cycle
8516 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008517 unsigned int remaining_steps = (round == MBEDTLS_ECJPAKE_ROUND_ONE) ? 2 : 1;
Valerio Settia08b1a42022-11-17 15:10:02 +01008518
Gilles Peskine449bd832023-01-11 14:50:10 +01008519 for (; remaining_steps > 0; remaining_steps--) {
8520 for (psa_pake_step_t step = PSA_PAKE_STEP_KEY_SHARE;
Valerio Settia08b1a42022-11-17 15:10:02 +01008521 step <= PSA_PAKE_STEP_ZK_PROOF;
Gilles Peskine449bd832023-01-11 14:50:10 +01008522 ++step) {
Valerio Settia08b1a42022-11-17 15:10:02 +01008523 /* Length is stored at the first byte */
8524 size_t length = buf[input_offset];
8525 input_offset += 1;
8526
Gilles Peskine449bd832023-01-11 14:50:10 +01008527 if (input_offset + length > len) {
Valerio Settia08b1a42022-11-17 15:10:02 +01008528 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
8529 }
8530
Gilles Peskine449bd832023-01-11 14:50:10 +01008531 status = psa_pake_input(pake_ctx, step,
8532 buf + input_offset, length);
8533 if (status != PSA_SUCCESS) {
8534 return psa_ssl_status_to_mbedtls(status);
Valerio Settia08b1a42022-11-17 15:10:02 +01008535 }
8536
8537 input_offset += length;
8538 }
8539 }
8540
Gilles Peskine449bd832023-01-11 14:50:10 +01008541 if (input_offset != len) {
Valerio Setti61ea17d2022-11-18 12:11:00 +01008542 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01008543 }
Valerio Setti30ebe112022-11-17 16:23:34 +01008544
Gilles Peskine449bd832023-01-11 14:50:10 +01008545 return 0;
Valerio Settia08b1a42022-11-17 15:10:02 +01008546}
8547
Valerio Setti6b3dab02022-11-17 17:14:54 +01008548int mbedtls_psa_ecjpake_write_round(
Gilles Peskine449bd832023-01-11 14:50:10 +01008549 psa_pake_operation_t *pake_ctx,
8550 unsigned char *buf,
8551 size_t len, size_t *olen,
8552 mbedtls_ecjpake_rounds_t round)
Valerio Settia08b1a42022-11-17 15:10:02 +01008553{
8554 psa_status_t status;
8555 size_t output_offset = 0;
8556 size_t output_len;
Valerio Setti819de862022-11-17 18:05:19 +01008557 /*
Valerio Setti6b3dab02022-11-17 17:14:54 +01008558 * At round one repeat the KEY_SHARE, ZK_PUBLIC & ZF_PROOF twice
8559 * At round two perform a single cycle
8560 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008561 unsigned int remaining_steps = (round == MBEDTLS_ECJPAKE_ROUND_ONE) ? 2 : 1;
Valerio Settia08b1a42022-11-17 15:10:02 +01008562
Gilles Peskine449bd832023-01-11 14:50:10 +01008563 for (; remaining_steps > 0; remaining_steps--) {
8564 for (psa_pake_step_t step = PSA_PAKE_STEP_KEY_SHARE;
8565 step <= PSA_PAKE_STEP_ZK_PROOF;
8566 ++step) {
Valerio Setti79f6b6b2022-11-21 14:17:03 +01008567 /*
Valerio Settid4a9b1a2022-11-22 11:11:10 +01008568 * For each step, prepend 1 byte with the length of the data as
8569 * given by psa_pake_output().
Valerio Setti79f6b6b2022-11-21 14:17:03 +01008570 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008571 status = psa_pake_output(pake_ctx, step,
8572 buf + output_offset + 1,
8573 len - output_offset - 1,
8574 &output_len);
8575 if (status != PSA_SUCCESS) {
8576 return psa_ssl_status_to_mbedtls(status);
Valerio Settia08b1a42022-11-17 15:10:02 +01008577 }
8578
Valerio Setti99d88c12022-11-22 16:03:43 +01008579 *(buf + output_offset) = (uint8_t) output_len;
Valerio Setti79f6b6b2022-11-21 14:17:03 +01008580
8581 output_offset += output_len + 1;
Valerio Settia08b1a42022-11-17 15:10:02 +01008582 }
8583 }
8584
8585 *olen = output_offset;
8586
Gilles Peskine449bd832023-01-11 14:50:10 +01008587 return 0;
Valerio Settia08b1a42022-11-17 15:10:02 +01008588}
Valerio Settia08b1a42022-11-17 15:10:02 +01008589#endif //MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED && MBEDTLS_USE_PSA_CRYPTO
8590
Jerry Yuee40f9d2022-02-17 14:55:16 +08008591#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008592int mbedtls_ssl_get_key_exchange_md_tls1_2(mbedtls_ssl_context *ssl,
8593 unsigned char *hash, size_t *hashlen,
8594 unsigned char *data, size_t data_len,
8595 mbedtls_md_type_t md_alg)
Jerry Yuee40f9d2022-02-17 14:55:16 +08008596{
8597 psa_status_t status;
8598 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01008599 psa_algorithm_t hash_alg = mbedtls_hash_info_psa_from_md(md_alg);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008600
Gilles Peskine449bd832023-01-11 14:50:10 +01008601 MBEDTLS_SSL_DEBUG_MSG(3, ("Perform PSA-based computation of digest of ServerKeyExchange"));
Jerry Yuee40f9d2022-02-17 14:55:16 +08008602
Gilles Peskine449bd832023-01-11 14:50:10 +01008603 if ((status = psa_hash_setup(&hash_operation,
8604 hash_alg)) != PSA_SUCCESS) {
8605 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_setup", status);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008606 goto exit;
8607 }
8608
Gilles Peskine449bd832023-01-11 14:50:10 +01008609 if ((status = psa_hash_update(&hash_operation, ssl->handshake->randbytes,
8610 64)) != PSA_SUCCESS) {
8611 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_update", status);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008612 goto exit;
8613 }
8614
Gilles Peskine449bd832023-01-11 14:50:10 +01008615 if ((status = psa_hash_update(&hash_operation,
8616 data, data_len)) != PSA_SUCCESS) {
8617 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_update", status);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008618 goto exit;
8619 }
8620
Gilles Peskine449bd832023-01-11 14:50:10 +01008621 if ((status = psa_hash_finish(&hash_operation, hash, PSA_HASH_MAX_SIZE,
8622 hashlen)) != PSA_SUCCESS) {
8623 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_finish", status);
8624 goto exit;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008625 }
8626
8627exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008628 if (status != PSA_SUCCESS) {
8629 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8630 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
8631 switch (status) {
Jerry Yuee40f9d2022-02-17 14:55:16 +08008632 case PSA_ERROR_NOT_SUPPORTED:
Gilles Peskine449bd832023-01-11 14:50:10 +01008633 return MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008634 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
8635 case PSA_ERROR_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +01008636 return MBEDTLS_ERR_MD_BAD_INPUT_DATA;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008637 case PSA_ERROR_INSUFFICIENT_MEMORY:
Gilles Peskine449bd832023-01-11 14:50:10 +01008638 return MBEDTLS_ERR_MD_ALLOC_FAILED;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008639 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01008640 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008641 }
8642 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008643 return 0;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008644}
8645
8646#else
8647
Gilles Peskine449bd832023-01-11 14:50:10 +01008648int mbedtls_ssl_get_key_exchange_md_tls1_2(mbedtls_ssl_context *ssl,
8649 unsigned char *hash, size_t *hashlen,
8650 unsigned char *data, size_t data_len,
8651 mbedtls_md_type_t md_alg)
Jerry Yuee40f9d2022-02-17 14:55:16 +08008652{
8653 int ret = 0;
8654 mbedtls_md_context_t ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +01008655 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type(md_alg);
8656 *hashlen = mbedtls_md_get_size(md_info);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008657
Gilles Peskine449bd832023-01-11 14:50:10 +01008658 MBEDTLS_SSL_DEBUG_MSG(3, ("Perform mbedtls-based computation of digest of ServerKeyExchange"));
Jerry Yuee40f9d2022-02-17 14:55:16 +08008659
Gilles Peskine449bd832023-01-11 14:50:10 +01008660 mbedtls_md_init(&ctx);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008661
8662 /*
8663 * digitally-signed struct {
8664 * opaque client_random[32];
8665 * opaque server_random[32];
8666 * ServerDHParams params;
8667 * };
8668 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008669 if ((ret = mbedtls_md_setup(&ctx, md_info, 0)) != 0) {
8670 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_setup", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008671 goto exit;
8672 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008673 if ((ret = mbedtls_md_starts(&ctx)) != 0) {
8674 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_starts", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008675 goto exit;
8676 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008677 if ((ret = mbedtls_md_update(&ctx, ssl->handshake->randbytes, 64)) != 0) {
8678 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_update", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008679 goto exit;
8680 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008681 if ((ret = mbedtls_md_update(&ctx, data, data_len)) != 0) {
8682 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_update", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008683 goto exit;
8684 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008685 if ((ret = mbedtls_md_finish(&ctx, hash)) != 0) {
8686 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_finish", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008687 goto exit;
8688 }
8689
8690exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008691 mbedtls_md_free(&ctx);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008692
Gilles Peskine449bd832023-01-11 14:50:10 +01008693 if (ret != 0) {
8694 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8695 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
8696 }
Jerry Yuee40f9d2022-02-17 14:55:16 +08008697
Gilles Peskine449bd832023-01-11 14:50:10 +01008698 return ret;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008699}
8700#endif /* MBEDTLS_USE_PSA_CRYPTO */
8701
Jerry Yud9d91da2022-02-17 14:57:06 +08008702#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
8703
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008704/* Find the preferred hash for a given signature algorithm. */
8705unsigned int mbedtls_ssl_tls12_get_preferred_hash_for_sig_alg(
Gilles Peskine449bd832023-01-11 14:50:10 +01008706 mbedtls_ssl_context *ssl,
8707 unsigned int sig_alg)
Jerry Yud9d91da2022-02-17 14:57:06 +08008708{
Gabor Mezei078e8032022-04-27 21:17:56 +02008709 unsigned int i;
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008710 uint16_t *received_sig_algs = ssl->handshake->received_sig_algs;
Gabor Mezei078e8032022-04-27 21:17:56 +02008711
Gilles Peskine449bd832023-01-11 14:50:10 +01008712 if (sig_alg == MBEDTLS_SSL_SIG_ANON) {
8713 return MBEDTLS_SSL_HASH_NONE;
8714 }
Gabor Mezei078e8032022-04-27 21:17:56 +02008715
Gilles Peskine449bd832023-01-11 14:50:10 +01008716 for (i = 0; received_sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++) {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008717 unsigned int hash_alg_received =
Gilles Peskine449bd832023-01-11 14:50:10 +01008718 MBEDTLS_SSL_TLS12_HASH_ALG_FROM_SIG_AND_HASH_ALG(
8719 received_sig_algs[i]);
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008720 unsigned int sig_alg_received =
Gilles Peskine449bd832023-01-11 14:50:10 +01008721 MBEDTLS_SSL_TLS12_SIG_ALG_FROM_SIG_AND_HASH_ALG(
8722 received_sig_algs[i]);
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008723
Gilles Peskine449bd832023-01-11 14:50:10 +01008724 if (sig_alg == sig_alg_received) {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008725#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008726 if (ssl->handshake->key_cert && ssl->handshake->key_cert->key) {
Neil Armstrong96eceb82022-06-30 18:05:05 +02008727 psa_algorithm_t psa_hash_alg =
Gilles Peskine449bd832023-01-11 14:50:10 +01008728 mbedtls_hash_info_psa_from_md(hash_alg_received);
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008729
Gilles Peskine449bd832023-01-11 14:50:10 +01008730 if (sig_alg_received == MBEDTLS_SSL_SIG_ECDSA &&
8731 !mbedtls_pk_can_do_ext(ssl->handshake->key_cert->key,
8732 PSA_ALG_ECDSA(psa_hash_alg),
8733 PSA_KEY_USAGE_SIGN_HASH)) {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008734 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01008735 }
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008736
Gilles Peskine449bd832023-01-11 14:50:10 +01008737 if (sig_alg_received == MBEDTLS_SSL_SIG_RSA &&
8738 !mbedtls_pk_can_do_ext(ssl->handshake->key_cert->key,
8739 PSA_ALG_RSA_PKCS1V15_SIGN(
8740 psa_hash_alg),
8741 PSA_KEY_USAGE_SIGN_HASH)) {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008742 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01008743 }
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008744 }
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008745#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong96eceb82022-06-30 18:05:05 +02008746
Gilles Peskine449bd832023-01-11 14:50:10 +01008747 return hash_alg_received;
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008748 }
Jerry Yud9d91da2022-02-17 14:57:06 +08008749 }
Jerry Yud9d91da2022-02-17 14:57:06 +08008750
Gilles Peskine449bd832023-01-11 14:50:10 +01008751 return MBEDTLS_SSL_HASH_NONE;
Jerry Yud9d91da2022-02-17 14:57:06 +08008752}
8753
8754#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
8755
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008756/* Serialization of TLS 1.2 sessions:
8757 *
8758 * struct {
8759 * uint64 start_time;
8760 * uint8 ciphersuite[2]; // defined by the standard
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008761 * uint8 session_id_len; // at most 32
8762 * opaque session_id[32];
8763 * opaque master[48]; // fixed length in the standard
8764 * uint32 verify_result;
8765 * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert
8766 * opaque ticket<0..2^24-1>; // length 0 means no ticket
8767 * uint32 ticket_lifetime;
8768 * uint8 mfl_code; // up to 255 according to standard
8769 * uint8 encrypt_then_mac; // 0 or 1
8770 * } serialized_session_tls12;
8771 *
8772 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008773static size_t ssl_tls12_session_save(const mbedtls_ssl_session *session,
8774 unsigned char *buf,
8775 size_t buf_len)
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008776{
8777 unsigned char *p = buf;
8778 size_t used = 0;
8779
8780#if defined(MBEDTLS_HAVE_TIME)
8781 uint64_t start;
8782#endif
8783#if defined(MBEDTLS_X509_CRT_PARSE_C)
8784#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8785 size_t cert_len;
8786#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8787#endif /* MBEDTLS_X509_CRT_PARSE_C */
8788
8789 /*
8790 * Time
8791 */
8792#if defined(MBEDTLS_HAVE_TIME)
8793 used += 8;
8794
Gilles Peskine449bd832023-01-11 14:50:10 +01008795 if (used <= buf_len) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008796 start = (uint64_t) session->start;
8797
Gilles Peskine449bd832023-01-11 14:50:10 +01008798 MBEDTLS_PUT_UINT64_BE(start, p, 0);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008799 p += 8;
8800 }
8801#endif /* MBEDTLS_HAVE_TIME */
8802
8803 /*
8804 * Basic mandatory fields
8805 */
8806 used += 2 /* ciphersuite */
Gilles Peskine449bd832023-01-11 14:50:10 +01008807 + 1 /* id_len */
8808 + sizeof(session->id)
8809 + sizeof(session->master)
8810 + 4; /* verify_result */
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008811
Gilles Peskine449bd832023-01-11 14:50:10 +01008812 if (used <= buf_len) {
8813 MBEDTLS_PUT_UINT16_BE(session->ciphersuite, p, 0);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008814 p += 2;
8815
Gilles Peskine449bd832023-01-11 14:50:10 +01008816 *p++ = MBEDTLS_BYTE_0(session->id_len);
8817 memcpy(p, session->id, 32);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008818 p += 32;
8819
Gilles Peskine449bd832023-01-11 14:50:10 +01008820 memcpy(p, session->master, 48);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008821 p += 48;
8822
Gilles Peskine449bd832023-01-11 14:50:10 +01008823 MBEDTLS_PUT_UINT32_BE(session->verify_result, p, 0);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008824 p += 4;
8825 }
8826
8827 /*
8828 * Peer's end-entity certificate
8829 */
8830#if defined(MBEDTLS_X509_CRT_PARSE_C)
8831#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01008832 if (session->peer_cert == NULL) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008833 cert_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01008834 } else {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008835 cert_len = session->peer_cert->raw.len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008836 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008837
8838 used += 3 + cert_len;
8839
Gilles Peskine449bd832023-01-11 14:50:10 +01008840 if (used <= buf_len) {
8841 *p++ = MBEDTLS_BYTE_2(cert_len);
8842 *p++ = MBEDTLS_BYTE_1(cert_len);
8843 *p++ = MBEDTLS_BYTE_0(cert_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008844
Gilles Peskine449bd832023-01-11 14:50:10 +01008845 if (session->peer_cert != NULL) {
8846 memcpy(p, session->peer_cert->raw.p, cert_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008847 p += cert_len;
8848 }
8849 }
8850#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008851 if (session->peer_cert_digest != NULL) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008852 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008853 if (used <= buf_len) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008854 *p++ = (unsigned char) session->peer_cert_digest_type;
8855 *p++ = (unsigned char) session->peer_cert_digest_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008856 memcpy(p, session->peer_cert_digest,
8857 session->peer_cert_digest_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008858 p += session->peer_cert_digest_len;
8859 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008860 } else {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008861 used += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01008862 if (used <= buf_len) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008863 *p++ = (unsigned char) MBEDTLS_MD_NONE;
8864 *p++ = 0;
8865 }
8866 }
8867#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8868#endif /* MBEDTLS_X509_CRT_PARSE_C */
8869
8870 /*
8871 * Session ticket if any, plus associated data
8872 */
8873#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8874 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
8875
Gilles Peskine449bd832023-01-11 14:50:10 +01008876 if (used <= buf_len) {
8877 *p++ = MBEDTLS_BYTE_2(session->ticket_len);
8878 *p++ = MBEDTLS_BYTE_1(session->ticket_len);
8879 *p++ = MBEDTLS_BYTE_0(session->ticket_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008880
Gilles Peskine449bd832023-01-11 14:50:10 +01008881 if (session->ticket != NULL) {
8882 memcpy(p, session->ticket, session->ticket_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008883 p += session->ticket_len;
8884 }
8885
Gilles Peskine449bd832023-01-11 14:50:10 +01008886 MBEDTLS_PUT_UINT32_BE(session->ticket_lifetime, p, 0);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008887 p += 4;
8888 }
8889#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8890
8891 /*
8892 * Misc extension-related info
8893 */
8894#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8895 used += 1;
8896
Gilles Peskine449bd832023-01-11 14:50:10 +01008897 if (used <= buf_len) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008898 *p++ = session->mfl_code;
Gilles Peskine449bd832023-01-11 14:50:10 +01008899 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008900#endif
8901
8902#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
8903 used += 1;
8904
Gilles Peskine449bd832023-01-11 14:50:10 +01008905 if (used <= buf_len) {
8906 *p++ = MBEDTLS_BYTE_0(session->encrypt_then_mac);
8907 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008908#endif
8909
Gilles Peskine449bd832023-01-11 14:50:10 +01008910 return used;
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008911}
8912
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02008913MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01008914static int ssl_tls12_session_load(mbedtls_ssl_session *session,
8915 const unsigned char *buf,
8916 size_t len)
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008917{
8918#if defined(MBEDTLS_HAVE_TIME)
8919 uint64_t start;
8920#endif
8921#if defined(MBEDTLS_X509_CRT_PARSE_C)
8922#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8923 size_t cert_len;
8924#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8925#endif /* MBEDTLS_X509_CRT_PARSE_C */
8926
8927 const unsigned char *p = buf;
8928 const unsigned char * const end = buf + len;
8929
8930 /*
8931 * Time
8932 */
8933#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01008934 if (8 > (size_t) (end - p)) {
8935 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
8936 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008937
Gilles Peskine449bd832023-01-11 14:50:10 +01008938 start = ((uint64_t) p[0] << 56) |
8939 ((uint64_t) p[1] << 48) |
8940 ((uint64_t) p[2] << 40) |
8941 ((uint64_t) p[3] << 32) |
8942 ((uint64_t) p[4] << 24) |
8943 ((uint64_t) p[5] << 16) |
8944 ((uint64_t) p[6] << 8) |
8945 ((uint64_t) p[7]);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008946 p += 8;
8947
8948 session->start = (time_t) start;
8949#endif /* MBEDTLS_HAVE_TIME */
8950
8951 /*
8952 * Basic mandatory fields
8953 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008954 if (2 + 1 + 32 + 48 + 4 > (size_t) (end - p)) {
8955 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
8956 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008957
Gilles Peskine449bd832023-01-11 14:50:10 +01008958 session->ciphersuite = (p[0] << 8) | p[1];
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008959 p += 2;
8960
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008961 session->id_len = *p++;
Gilles Peskine449bd832023-01-11 14:50:10 +01008962 memcpy(session->id, p, 32);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008963 p += 32;
8964
Gilles Peskine449bd832023-01-11 14:50:10 +01008965 memcpy(session->master, p, 48);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008966 p += 48;
8967
Gilles Peskine449bd832023-01-11 14:50:10 +01008968 session->verify_result = ((uint32_t) p[0] << 24) |
8969 ((uint32_t) p[1] << 16) |
8970 ((uint32_t) p[2] << 8) |
8971 ((uint32_t) p[3]);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008972 p += 4;
8973
8974 /* Immediately clear invalid pointer values that have been read, in case
8975 * we exit early before we replaced them with valid ones. */
8976#if defined(MBEDTLS_X509_CRT_PARSE_C)
8977#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8978 session->peer_cert = NULL;
8979#else
8980 session->peer_cert_digest = NULL;
8981#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8982#endif /* MBEDTLS_X509_CRT_PARSE_C */
8983#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8984 session->ticket = NULL;
8985#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8986
8987 /*
8988 * Peer certificate
8989 */
8990#if defined(MBEDTLS_X509_CRT_PARSE_C)
8991#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8992 /* Deserialize CRT from the end of the ticket. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008993 if (3 > (size_t) (end - p)) {
8994 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
8995 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008996
Gilles Peskine449bd832023-01-11 14:50:10 +01008997 cert_len = (p[0] << 16) | (p[1] << 8) | p[2];
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008998 p += 3;
8999
Gilles Peskine449bd832023-01-11 14:50:10 +01009000 if (cert_len != 0) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009001 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
9002
Gilles Peskine449bd832023-01-11 14:50:10 +01009003 if (cert_len > (size_t) (end - p)) {
9004 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9005 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009006
Gilles Peskine449bd832023-01-11 14:50:10 +01009007 session->peer_cert = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009008
Gilles Peskine449bd832023-01-11 14:50:10 +01009009 if (session->peer_cert == NULL) {
9010 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
9011 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009012
Gilles Peskine449bd832023-01-11 14:50:10 +01009013 mbedtls_x509_crt_init(session->peer_cert);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009014
Gilles Peskine449bd832023-01-11 14:50:10 +01009015 if ((ret = mbedtls_x509_crt_parse_der(session->peer_cert,
9016 p, cert_len)) != 0) {
9017 mbedtls_x509_crt_free(session->peer_cert);
9018 mbedtls_free(session->peer_cert);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009019 session->peer_cert = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01009020 return ret;
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009021 }
9022
9023 p += cert_len;
9024 }
9025#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9026 /* Deserialize CRT digest from the end of the ticket. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009027 if (2 > (size_t) (end - p)) {
9028 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9029 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009030
9031 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
9032 session->peer_cert_digest_len = (size_t) *p++;
9033
Gilles Peskine449bd832023-01-11 14:50:10 +01009034 if (session->peer_cert_digest_len != 0) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009035 const mbedtls_md_info_t *md_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01009036 mbedtls_md_info_from_type(session->peer_cert_digest_type);
9037 if (md_info == NULL) {
9038 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9039 }
9040 if (session->peer_cert_digest_len != mbedtls_md_get_size(md_info)) {
9041 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9042 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009043
Gilles Peskine449bd832023-01-11 14:50:10 +01009044 if (session->peer_cert_digest_len > (size_t) (end - p)) {
9045 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9046 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009047
9048 session->peer_cert_digest =
Gilles Peskine449bd832023-01-11 14:50:10 +01009049 mbedtls_calloc(1, session->peer_cert_digest_len);
9050 if (session->peer_cert_digest == NULL) {
9051 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
9052 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009053
Gilles Peskine449bd832023-01-11 14:50:10 +01009054 memcpy(session->peer_cert_digest, p,
9055 session->peer_cert_digest_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009056 p += session->peer_cert_digest_len;
9057 }
9058#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9059#endif /* MBEDTLS_X509_CRT_PARSE_C */
9060
9061 /*
9062 * Session ticket and associated data
9063 */
9064#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01009065 if (3 > (size_t) (end - p)) {
9066 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9067 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009068
Gilles Peskine449bd832023-01-11 14:50:10 +01009069 session->ticket_len = (p[0] << 16) | (p[1] << 8) | p[2];
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009070 p += 3;
9071
Gilles Peskine449bd832023-01-11 14:50:10 +01009072 if (session->ticket_len != 0) {
9073 if (session->ticket_len > (size_t) (end - p)) {
9074 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9075 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009076
Gilles Peskine449bd832023-01-11 14:50:10 +01009077 session->ticket = mbedtls_calloc(1, session->ticket_len);
9078 if (session->ticket == NULL) {
9079 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
9080 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009081
Gilles Peskine449bd832023-01-11 14:50:10 +01009082 memcpy(session->ticket, p, session->ticket_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009083 p += session->ticket_len;
9084 }
9085
Gilles Peskine449bd832023-01-11 14:50:10 +01009086 if (4 > (size_t) (end - p)) {
9087 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9088 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009089
Gilles Peskine449bd832023-01-11 14:50:10 +01009090 session->ticket_lifetime = ((uint32_t) p[0] << 24) |
9091 ((uint32_t) p[1] << 16) |
9092 ((uint32_t) p[2] << 8) |
9093 ((uint32_t) p[3]);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009094 p += 4;
9095#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
9096
9097 /*
9098 * Misc extension-related info
9099 */
9100#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01009101 if (1 > (size_t) (end - p)) {
9102 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9103 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009104
9105 session->mfl_code = *p++;
9106#endif
9107
9108#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01009109 if (1 > (size_t) (end - p)) {
9110 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9111 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009112
9113 session->encrypt_then_mac = *p++;
9114#endif
9115
9116 /* Done, should have consumed entire buffer */
Gilles Peskine449bd832023-01-11 14:50:10 +01009117 if (p != end) {
9118 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9119 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009120
Gilles Peskine449bd832023-01-11 14:50:10 +01009121 return 0;
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009122}
Jerry Yudc7bd172022-02-17 13:44:15 +08009123#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9124
XiaokangQian75d40ef2022-04-20 11:05:24 +00009125int mbedtls_ssl_validate_ciphersuite(
9126 const mbedtls_ssl_context *ssl,
9127 const mbedtls_ssl_ciphersuite_t *suite_info,
9128 mbedtls_ssl_protocol_version min_tls_version,
Gilles Peskine449bd832023-01-11 14:50:10 +01009129 mbedtls_ssl_protocol_version max_tls_version)
XiaokangQian75d40ef2022-04-20 11:05:24 +00009130{
9131 (void) ssl;
9132
Gilles Peskine449bd832023-01-11 14:50:10 +01009133 if (suite_info == NULL) {
9134 return -1;
9135 }
XiaokangQian75d40ef2022-04-20 11:05:24 +00009136
Gilles Peskine449bd832023-01-11 14:50:10 +01009137 if ((suite_info->min_tls_version > max_tls_version) ||
9138 (suite_info->max_tls_version < min_tls_version)) {
9139 return -1;
XiaokangQian75d40ef2022-04-20 11:05:24 +00009140 }
9141
XiaokangQian060d8672022-04-21 09:24:56 +00009142#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_CLI_C)
XiaokangQian75d40ef2022-04-20 11:05:24 +00009143#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Neil Armstrongca7d5062022-05-31 14:43:23 +02009144#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01009145 if (suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
9146 ssl->handshake->psa_pake_ctx_is_ok != 1)
Neil Armstrongca7d5062022-05-31 14:43:23 +02009147#else
Gilles Peskine449bd832023-01-11 14:50:10 +01009148 if (suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
9149 mbedtls_ecjpake_check(&ssl->handshake->ecjpake_ctx) != 0)
Neil Armstrongca7d5062022-05-31 14:43:23 +02009150#endif /* MBEDTLS_USE_PSA_CRYPTO */
XiaokangQian75d40ef2022-04-20 11:05:24 +00009151 {
Gilles Peskine449bd832023-01-11 14:50:10 +01009152 return -1;
XiaokangQian75d40ef2022-04-20 11:05:24 +00009153 }
9154#endif
9155
9156 /* Don't suggest PSK-based ciphersuite if no PSK is available. */
9157#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01009158 if (mbedtls_ssl_ciphersuite_uses_psk(suite_info) &&
9159 mbedtls_ssl_conf_has_static_psk(ssl->conf) == 0) {
9160 return -1;
XiaokangQian75d40ef2022-04-20 11:05:24 +00009161 }
9162#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
9163#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9164
Gilles Peskine449bd832023-01-11 14:50:10 +01009165 return 0;
XiaokangQian75d40ef2022-04-20 11:05:24 +00009166}
9167
Ronald Crone68ab4f2022-10-05 12:46:29 +02009168#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
XiaokangQianeaf36512022-04-24 09:07:44 +00009169/*
9170 * Function for writing a signature algorithm extension.
9171 *
9172 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
9173 * value (TLS 1.3 RFC8446):
9174 * enum {
9175 * ....
9176 * ecdsa_secp256r1_sha256( 0x0403 ),
9177 * ecdsa_secp384r1_sha384( 0x0503 ),
9178 * ecdsa_secp521r1_sha512( 0x0603 ),
9179 * ....
9180 * } SignatureScheme;
9181 *
9182 * struct {
9183 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
9184 * } SignatureSchemeList;
9185 *
9186 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
9187 * value (TLS 1.2 RFC5246):
9188 * enum {
9189 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
9190 * sha512(6), (255)
9191 * } HashAlgorithm;
9192 *
9193 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
9194 * SignatureAlgorithm;
9195 *
9196 * struct {
9197 * HashAlgorithm hash;
9198 * SignatureAlgorithm signature;
9199 * } SignatureAndHashAlgorithm;
9200 *
9201 * SignatureAndHashAlgorithm
9202 * supported_signature_algorithms<2..2^16-2>;
9203 *
9204 * The TLS 1.3 signature algorithm extension was defined to be a compatible
9205 * generalization of the TLS 1.2 signature algorithm extension.
9206 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
9207 * `SignatureScheme` field of TLS 1.3
9208 *
9209 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009210int mbedtls_ssl_write_sig_alg_ext(mbedtls_ssl_context *ssl, unsigned char *buf,
9211 const unsigned char *end, size_t *out_len)
XiaokangQianeaf36512022-04-24 09:07:44 +00009212{
9213 unsigned char *p = buf;
9214 unsigned char *supported_sig_alg; /* Start of supported_signature_algorithms */
9215 size_t supported_sig_alg_len = 0; /* Length of supported_signature_algorithms */
9216
9217 *out_len = 0;
9218
Gilles Peskine449bd832023-01-11 14:50:10 +01009219 MBEDTLS_SSL_DEBUG_MSG(3, ("adding signature_algorithms extension"));
XiaokangQianeaf36512022-04-24 09:07:44 +00009220
9221 /* Check if we have space for header and length field:
9222 * - extension_type (2 bytes)
9223 * - extension_data_length (2 bytes)
9224 * - supported_signature_algorithms_length (2 bytes)
9225 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009226 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 6);
XiaokangQianeaf36512022-04-24 09:07:44 +00009227 p += 6;
9228
9229 /*
9230 * Write supported_signature_algorithms
9231 */
9232 supported_sig_alg = p;
Gilles Peskine449bd832023-01-11 14:50:10 +01009233 const uint16_t *sig_alg = mbedtls_ssl_get_sig_algs(ssl);
9234 if (sig_alg == NULL) {
9235 return MBEDTLS_ERR_SSL_BAD_CONFIG;
9236 }
XiaokangQianeaf36512022-04-24 09:07:44 +00009237
Gilles Peskine449bd832023-01-11 14:50:10 +01009238 for (; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++) {
9239 MBEDTLS_SSL_DEBUG_MSG(3, ("got signature scheme [%x] %s",
9240 *sig_alg,
9241 mbedtls_ssl_sig_alg_to_str(*sig_alg)));
9242 if (!mbedtls_ssl_sig_alg_is_supported(ssl, *sig_alg)) {
XiaokangQianeaf36512022-04-24 09:07:44 +00009243 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01009244 }
9245 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
9246 MBEDTLS_PUT_UINT16_BE(*sig_alg, p, 0);
XiaokangQianeaf36512022-04-24 09:07:44 +00009247 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01009248 MBEDTLS_SSL_DEBUG_MSG(3, ("sent signature scheme [%x] %s",
9249 *sig_alg,
9250 mbedtls_ssl_sig_alg_to_str(*sig_alg)));
XiaokangQianeaf36512022-04-24 09:07:44 +00009251 }
9252
9253 /* Length of supported_signature_algorithms */
9254 supported_sig_alg_len = p - supported_sig_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01009255 if (supported_sig_alg_len == 0) {
9256 MBEDTLS_SSL_DEBUG_MSG(1, ("No signature algorithms defined."));
9257 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
XiaokangQianeaf36512022-04-24 09:07:44 +00009258 }
9259
Gilles Peskine449bd832023-01-11 14:50:10 +01009260 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SIG_ALG, buf, 0);
9261 MBEDTLS_PUT_UINT16_BE(supported_sig_alg_len + 2, buf, 2);
9262 MBEDTLS_PUT_UINT16_BE(supported_sig_alg_len, buf, 4);
XiaokangQianeaf36512022-04-24 09:07:44 +00009263
XiaokangQianeaf36512022-04-24 09:07:44 +00009264 *out_len = p - buf;
9265
9266#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01009267 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_SIG_ALG);
XiaokangQianeaf36512022-04-24 09:07:44 +00009268#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu0c354a22022-08-29 15:25:36 +08009269
Gilles Peskine449bd832023-01-11 14:50:10 +01009270 return 0;
XiaokangQianeaf36512022-04-24 09:07:44 +00009271}
Ronald Crone68ab4f2022-10-05 12:46:29 +02009272#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
XiaokangQianeaf36512022-04-24 09:07:44 +00009273
XiaokangQian40a35232022-05-07 09:02:40 +00009274#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
XiaokangQian9b2b7712022-05-17 02:57:00 +00009275/*
9276 * mbedtls_ssl_parse_server_name_ext
9277 *
9278 * Structure of server_name extension:
9279 *
9280 * enum {
9281 * host_name(0), (255)
9282 * } NameType;
9283 * opaque HostName<1..2^16-1>;
9284 *
9285 * struct {
9286 * NameType name_type;
9287 * select (name_type) {
9288 * case host_name: HostName;
9289 * } name;
9290 * } ServerName;
9291 * struct {
9292 * ServerName server_name_list<1..2^16-1>
9293 * } ServerNameList;
9294 */
Ronald Cronce7d76e2022-07-08 18:56:49 +02009295MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01009296int mbedtls_ssl_parse_server_name_ext(mbedtls_ssl_context *ssl,
9297 const unsigned char *buf,
9298 const unsigned char *end)
XiaokangQian40a35232022-05-07 09:02:40 +00009299{
9300 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
9301 const unsigned char *p = buf;
XiaokangQian9b2b7712022-05-17 02:57:00 +00009302 size_t server_name_list_len, hostname_len;
9303 const unsigned char *server_name_list_end;
XiaokangQian40a35232022-05-07 09:02:40 +00009304
Gilles Peskine449bd832023-01-11 14:50:10 +01009305 MBEDTLS_SSL_DEBUG_MSG(3, ("parse ServerName extension"));
XiaokangQian40a35232022-05-07 09:02:40 +00009306
Gilles Peskine449bd832023-01-11 14:50:10 +01009307 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
9308 server_name_list_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian40a35232022-05-07 09:02:40 +00009309 p += 2;
9310
Gilles Peskine449bd832023-01-11 14:50:10 +01009311 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, server_name_list_len);
XiaokangQian9b2b7712022-05-17 02:57:00 +00009312 server_name_list_end = p + server_name_list_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01009313 while (p < server_name_list_end) {
9314 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, server_name_list_end, 3);
9315 hostname_len = MBEDTLS_GET_UINT16_BE(p, 1);
9316 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, server_name_list_end,
9317 hostname_len + 3);
XiaokangQian40a35232022-05-07 09:02:40 +00009318
Gilles Peskine449bd832023-01-11 14:50:10 +01009319 if (p[0] == MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME) {
XiaokangQian75fe8c72022-06-15 09:42:45 +00009320 /* sni_name is intended to be used only during the parsing of the
9321 * ClientHello message (it is reset to NULL before the end of
9322 * the message parsing). Thus it is ok to just point to the
9323 * reception buffer and not make a copy of it.
9324 */
XiaokangQianf2a94202022-05-20 06:44:24 +00009325 ssl->handshake->sni_name = p + 3;
9326 ssl->handshake->sni_name_len = hostname_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01009327 if (ssl->conf->f_sni == NULL) {
9328 return 0;
XiaokangQian40a35232022-05-07 09:02:40 +00009329 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009330 ret = ssl->conf->f_sni(ssl->conf->p_sni,
9331 ssl, p + 3, hostname_len);
9332 if (ret != 0) {
9333 MBEDTLS_SSL_DEBUG_RET(1, "ssl_sni_wrapper", ret);
9334 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_UNRECOGNIZED_NAME,
9335 MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME);
9336 return MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME;
9337 }
9338 return 0;
XiaokangQian40a35232022-05-07 09:02:40 +00009339 }
9340
9341 p += hostname_len + 3;
9342 }
9343
Gilles Peskine449bd832023-01-11 14:50:10 +01009344 return 0;
XiaokangQian40a35232022-05-07 09:02:40 +00009345}
9346#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
9347
XiaokangQianacb39922022-06-17 10:18:48 +00009348#if defined(MBEDTLS_SSL_ALPN)
Ronald Cronce7d76e2022-07-08 18:56:49 +02009349MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01009350int mbedtls_ssl_parse_alpn_ext(mbedtls_ssl_context *ssl,
9351 const unsigned char *buf,
9352 const unsigned char *end)
XiaokangQianacb39922022-06-17 10:18:48 +00009353{
9354 const unsigned char *p = buf;
XiaokangQianc7403452022-06-23 03:24:12 +00009355 size_t protocol_name_list_len;
XiaokangQian95d5f542022-06-24 02:29:26 +00009356 const unsigned char *protocol_name_list;
9357 const unsigned char *protocol_name_list_end;
XiaokangQianc7403452022-06-23 03:24:12 +00009358 size_t protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009359
9360 /* If ALPN not configured, just ignore the extension */
Gilles Peskine449bd832023-01-11 14:50:10 +01009361 if (ssl->conf->alpn_list == NULL) {
9362 return 0;
9363 }
XiaokangQianacb39922022-06-17 10:18:48 +00009364
9365 /*
XiaokangQianc7403452022-06-23 03:24:12 +00009366 * RFC7301, section 3.1
9367 * opaque ProtocolName<1..2^8-1>;
XiaokangQianacb39922022-06-17 10:18:48 +00009368 *
XiaokangQianc7403452022-06-23 03:24:12 +00009369 * struct {
9370 * ProtocolName protocol_name_list<2..2^16-1>
9371 * } ProtocolNameList;
XiaokangQianacb39922022-06-17 10:18:48 +00009372 */
9373
XiaokangQianc7403452022-06-23 03:24:12 +00009374 /*
XiaokangQian0b776e22022-06-24 09:04:59 +00009375 * protocol_name_list_len 2 bytes
9376 * protocol_name_len 1 bytes
9377 * protocol_name >=1 byte
XiaokangQianc7403452022-06-23 03:24:12 +00009378 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009379 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 4);
XiaokangQianacb39922022-06-17 10:18:48 +00009380
Gilles Peskine449bd832023-01-11 14:50:10 +01009381 protocol_name_list_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQianacb39922022-06-17 10:18:48 +00009382 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01009383 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, protocol_name_list_len);
XiaokangQian95d5f542022-06-24 02:29:26 +00009384 protocol_name_list = p;
9385 protocol_name_list_end = p + protocol_name_list_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009386
9387 /* Validate peer's list (lengths) */
Gilles Peskine449bd832023-01-11 14:50:10 +01009388 while (p < protocol_name_list_end) {
XiaokangQian95d5f542022-06-24 02:29:26 +00009389 protocol_name_len = *p++;
Gilles Peskine449bd832023-01-11 14:50:10 +01009390 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, protocol_name_list_end,
9391 protocol_name_len);
9392 if (protocol_name_len == 0) {
XiaokangQian95d5f542022-06-24 02:29:26 +00009393 MBEDTLS_SSL_PEND_FATAL_ALERT(
9394 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
Gilles Peskine449bd832023-01-11 14:50:10 +01009395 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
9396 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
XiaokangQian95d5f542022-06-24 02:29:26 +00009397 }
9398
9399 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009400 }
9401
9402 /* Use our order of preference */
Gilles Peskine449bd832023-01-11 14:50:10 +01009403 for (const char **alpn = ssl->conf->alpn_list; *alpn != NULL; alpn++) {
9404 size_t const alpn_len = strlen(*alpn);
XiaokangQian95d5f542022-06-24 02:29:26 +00009405 p = protocol_name_list;
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 if (protocol_name_len == alpn_len &&
9409 memcmp(p, *alpn, alpn_len) == 0) {
XiaokangQianacb39922022-06-17 10:18:48 +00009410 ssl->alpn_chosen = *alpn;
Gilles Peskine449bd832023-01-11 14:50:10 +01009411 return 0;
XiaokangQianacb39922022-06-17 10:18:48 +00009412 }
XiaokangQian95d5f542022-06-24 02:29:26 +00009413
9414 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009415 }
9416 }
9417
XiaokangQian95d5f542022-06-24 02:29:26 +00009418 /* If we get here, no match was found */
XiaokangQianacb39922022-06-17 10:18:48 +00009419 MBEDTLS_SSL_PEND_FATAL_ALERT(
Gilles Peskine449bd832023-01-11 14:50:10 +01009420 MBEDTLS_SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL,
9421 MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL);
9422 return MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL;
XiaokangQianacb39922022-06-17 10:18:48 +00009423}
9424
Gilles Peskine449bd832023-01-11 14:50:10 +01009425int mbedtls_ssl_write_alpn_ext(mbedtls_ssl_context *ssl,
9426 unsigned char *buf,
9427 unsigned char *end,
9428 size_t *out_len)
XiaokangQianacb39922022-06-17 10:18:48 +00009429{
9430 unsigned char *p = buf;
XiaokangQian95d5f542022-06-24 02:29:26 +00009431 size_t protocol_name_len;
XiaokangQianc7403452022-06-23 03:24:12 +00009432 *out_len = 0;
XiaokangQianacb39922022-06-17 10:18:48 +00009433
Gilles Peskine449bd832023-01-11 14:50:10 +01009434 if (ssl->alpn_chosen == NULL) {
9435 return 0;
XiaokangQianacb39922022-06-17 10:18:48 +00009436 }
9437
Gilles Peskine449bd832023-01-11 14:50:10 +01009438 protocol_name_len = strlen(ssl->alpn_chosen);
9439 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 7 + protocol_name_len);
XiaokangQianacb39922022-06-17 10:18:48 +00009440
Gilles Peskine449bd832023-01-11 14:50:10 +01009441 MBEDTLS_SSL_DEBUG_MSG(3, ("server side, adding alpn extension"));
XiaokangQianacb39922022-06-17 10:18:48 +00009442 /*
9443 * 0 . 1 ext identifier
9444 * 2 . 3 ext length
9445 * 4 . 5 protocol list length
9446 * 6 . 6 protocol name length
9447 * 7 . 7+n protocol name
9448 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009449 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_ALPN, p, 0);
XiaokangQianacb39922022-06-17 10:18:48 +00009450
XiaokangQian95d5f542022-06-24 02:29:26 +00009451 *out_len = 7 + protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009452
Gilles Peskine449bd832023-01-11 14:50:10 +01009453 MBEDTLS_PUT_UINT16_BE(protocol_name_len + 3, p, 2);
9454 MBEDTLS_PUT_UINT16_BE(protocol_name_len + 1, p, 4);
XiaokangQian0b776e22022-06-24 09:04:59 +00009455 /* Note: the length of the chosen protocol has been checked to be less
9456 * than 255 bytes in `mbedtls_ssl_conf_alpn_protocols`.
9457 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009458 p[6] = MBEDTLS_BYTE_0(protocol_name_len);
XiaokangQianacb39922022-06-17 10:18:48 +00009459
Gilles Peskine449bd832023-01-11 14:50:10 +01009460 memcpy(p + 7, ssl->alpn_chosen, protocol_name_len);
Jerry Yub95dd362022-11-08 21:19:34 +08009461
9462#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01009463 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_ALPN);
Jerry Yub95dd362022-11-08 21:19:34 +08009464#endif
9465
Gilles Peskine449bd832023-01-11 14:50:10 +01009466 return 0;
XiaokangQianacb39922022-06-17 10:18:48 +00009467}
9468#endif /* MBEDTLS_SSL_ALPN */
9469
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009470#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
Xiaokang Qian03409292022-10-12 02:49:52 +00009471 defined(MBEDTLS_SSL_SESSION_TICKETS) && \
Xiaokang Qianed0620c2022-10-12 06:58:13 +00009472 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \
Xiaokang Qianed3afcd2022-10-12 08:31:11 +00009473 defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01009474int mbedtls_ssl_session_set_hostname(mbedtls_ssl_session *session,
9475 const char *hostname)
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009476{
9477 /* Initialize to suppress unnecessary compiler warning */
9478 size_t hostname_len = 0;
9479
9480 /* Check if new hostname is valid before
9481 * making any change to current one */
Gilles Peskine449bd832023-01-11 14:50:10 +01009482 if (hostname != NULL) {
9483 hostname_len = strlen(hostname);
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009484
Gilles Peskine449bd832023-01-11 14:50:10 +01009485 if (hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN) {
9486 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9487 }
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009488 }
9489
9490 /* Now it's clear that we will overwrite the old hostname,
9491 * so we can free it safely */
Gilles Peskine449bd832023-01-11 14:50:10 +01009492 if (session->hostname != NULL) {
9493 mbedtls_platform_zeroize(session->hostname,
9494 strlen(session->hostname));
9495 mbedtls_free(session->hostname);
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009496 }
9497
9498 /* Passing NULL as hostname shall clear the old one */
Gilles Peskine449bd832023-01-11 14:50:10 +01009499 if (hostname == NULL) {
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009500 session->hostname = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01009501 } else {
9502 session->hostname = mbedtls_calloc(1, hostname_len + 1);
9503 if (session->hostname == NULL) {
9504 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
9505 }
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009506
Gilles Peskine449bd832023-01-11 14:50:10 +01009507 memcpy(session->hostname, hostname, hostname_len);
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009508 }
9509
Gilles Peskine449bd832023-01-11 14:50:10 +01009510 return 0;
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009511}
Xiaokang Qian03409292022-10-12 02:49:52 +00009512#endif /* MBEDTLS_SSL_PROTO_TLS1_3 &&
9513 MBEDTLS_SSL_SESSION_TICKETS &&
Xiaokang Qianed0620c2022-10-12 06:58:13 +00009514 MBEDTLS_SSL_SERVER_NAME_INDICATION &&
Xiaokang Qianed3afcd2022-10-12 08:31:11 +00009515 MBEDTLS_SSL_CLI_C */
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009516
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009517#endif /* MBEDTLS_SSL_TLS_C */