blob: 86f5c0b555b3da06cc675c708dcf578c7d0d977a [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);
421static void ssl_calc_verify_tls_sha256(const mbedtls_ssl_context *, unsigned char *, size_t *);
422static void ssl_calc_finished_tls_sha256(mbedtls_ssl_context *, unsigned char *, int);
423
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
433static void ssl_calc_verify_tls_sha384(const mbedtls_ssl_context *, unsigned char *, size_t *);
434static void ssl_calc_finished_tls_sha384(mbedtls_ssl_context *, unsigned char *, int);
435#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
447static void ssl_update_checksum_start(mbedtls_ssl_context *, const unsigned char *, size_t);
448
449#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
450static void ssl_update_checksum_sha256(mbedtls_ssl_context *, const unsigned char *, size_t);
451#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)
454static void ssl_update_checksum_sha384(mbedtls_ssl_context *, const unsigned char *, size_t);
455#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
Gilles Peskine449bd832023-01-11 14:50:10 +0100815void 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
834}
835
Gilles Peskine449bd832023-01-11 14:50:10 +0100836static void ssl_update_checksum_start(mbedtls_ssl_context *ssl,
837 const unsigned char *buf, size_t len)
Jerry Yuc73c6182022-02-08 20:29:25 +0800838{
Andrzej Kurek25f27152022-08-17 16:09:31 -0400839#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800840#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100841 psa_hash_update(&ssl->handshake->fin_sha256_psa, buf, len);
Jerry Yuc73c6182022-02-08 20:29:25 +0800842#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100843 mbedtls_sha256_update(&ssl->handshake->fin_sha256, buf, len);
Jerry Yuc73c6182022-02-08 20:29:25 +0800844#endif
845#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400846#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800847#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100848 psa_hash_update(&ssl->handshake->fin_sha384_psa, buf, len);
Jerry Yuc73c6182022-02-08 20:29:25 +0800849#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100850 mbedtls_sha512_update(&ssl->handshake->fin_sha384, buf, len);
Jerry Yuc73c6182022-02-08 20:29:25 +0800851#endif
852#endif
Andrzej Kurekeabeb302022-10-17 07:52:51 -0400853#if !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
854 !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
855 (void) ssl;
856 (void) buf;
857 (void) len;
858#endif
Jerry Yuc73c6182022-02-08 20:29:25 +0800859}
860
Andrzej Kurek25f27152022-08-17 16:09:31 -0400861#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +0100862static void ssl_update_checksum_sha256(mbedtls_ssl_context *ssl,
863 const unsigned char *buf, size_t len)
Jerry Yuc73c6182022-02-08 20:29:25 +0800864{
865#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100866 psa_hash_update(&ssl->handshake->fin_sha256_psa, buf, len);
Jerry Yuc73c6182022-02-08 20:29:25 +0800867#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100868 mbedtls_sha256_update(&ssl->handshake->fin_sha256, buf, len);
Jerry Yuc73c6182022-02-08 20:29:25 +0800869#endif
870}
871#endif
872
Andrzej Kurek25f27152022-08-17 16:09:31 -0400873#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +0100874static void ssl_update_checksum_sha384(mbedtls_ssl_context *ssl,
875 const unsigned char *buf, size_t len)
Jerry Yuc73c6182022-02-08 20:29:25 +0800876{
877#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100878 psa_hash_update(&ssl->handshake->fin_sha384_psa, buf, len);
Jerry Yuc73c6182022-02-08 20:29:25 +0800879#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100880 mbedtls_sha512_update(&ssl->handshake->fin_sha384, buf, len);
Jerry Yuc73c6182022-02-08 20:29:25 +0800881#endif
882}
883#endif
884
Gilles Peskine449bd832023-01-11 14:50:10 +0100885static void ssl_handshake_params_init(mbedtls_ssl_handshake_params *handshake)
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200886{
Gilles Peskine449bd832023-01-11 14:50:10 +0100887 memset(handshake, 0, sizeof(mbedtls_ssl_handshake_params));
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200888
Andrzej Kurek25f27152022-08-17 16:09:31 -0400889#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -0500890#if defined(MBEDTLS_USE_PSA_CRYPTO)
891 handshake->fin_sha256_psa = psa_hash_operation_init();
Gilles Peskine449bd832023-01-11 14:50:10 +0100892 psa_hash_setup(&handshake->fin_sha256_psa, PSA_ALG_SHA_256);
Andrzej Kurekeb342242019-01-29 09:14:33 -0500893#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100894 mbedtls_sha256_init(&handshake->fin_sha256);
895 mbedtls_sha256_starts(&handshake->fin_sha256, 0);
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200896#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -0500897#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400898#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -0500899#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -0500900 handshake->fin_sha384_psa = psa_hash_operation_init();
Gilles Peskine449bd832023-01-11 14:50:10 +0100901 psa_hash_setup(&handshake->fin_sha384_psa, PSA_ALG_SHA_384);
Andrzej Kurekeb342242019-01-29 09:14:33 -0500902#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100903 mbedtls_sha512_init(&handshake->fin_sha384);
904 mbedtls_sha512_starts(&handshake->fin_sha384, 1);
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
Jerry Yud0766ec2022-09-22 10:46:57 +08001041#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
1042 defined(MBEDTLS_SSL_SRV_C) && \
1043 defined(MBEDTLS_SSL_SESSION_TICKETS)
1044 ssl->handshake->new_session_tickets_count =
Gilles Peskine449bd832023-01-11 14:50:10 +01001045 ssl->conf->new_session_tickets_count;
Jerry Yud0766ec2022-09-22 10:46:57 +08001046#endif
1047
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001048#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001049 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001050 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001051
Gilles Peskine449bd832023-01-11 14:50:10 +01001052 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001053 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Gilles Peskine449bd832023-01-11 14:50:10 +01001054 } else {
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001055 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Gilles Peskine449bd832023-01-11 14:50:10 +01001056 }
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001057
Gilles Peskine449bd832023-01-11 14:50:10 +01001058 mbedtls_ssl_set_timer(ssl, 0);
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001059 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001060#endif
1061
Brett Warrene0edc842021-08-17 09:53:13 +01001062/*
1063 * curve_list is translated to IANA TLS group identifiers here because
1064 * mbedtls_ssl_conf_curves returns void and so can't return
1065 * any error codes.
1066 */
1067#if defined(MBEDTLS_ECP_C)
1068#if !defined(MBEDTLS_DEPRECATED_REMOVED)
1069 /* Heap allocate and translate curve_list from internal to IANA group ids */
Gilles Peskine449bd832023-01-11 14:50:10 +01001070 if (ssl->conf->curve_list != NULL) {
Brett Warrene0edc842021-08-17 09:53:13 +01001071 size_t length;
1072 const mbedtls_ecp_group_id *curve_list = ssl->conf->curve_list;
1073
Gilles Peskine449bd832023-01-11 14:50:10 +01001074 for (length = 0; (curve_list[length] != MBEDTLS_ECP_DP_NONE) &&
1075 (length < MBEDTLS_ECP_DP_MAX); length++) {
1076 }
Brett Warrene0edc842021-08-17 09:53:13 +01001077
1078 /* Leave room for zero termination */
Gilles Peskine449bd832023-01-11 14:50:10 +01001079 uint16_t *group_list = mbedtls_calloc(length + 1, sizeof(uint16_t));
1080 if (group_list == NULL) {
1081 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
1082 }
Brett Warrene0edc842021-08-17 09:53:13 +01001083
Gilles Peskine449bd832023-01-11 14:50:10 +01001084 for (size_t i = 0; i < length; i++) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01001085 uint16_t tls_id = mbedtls_ssl_get_tls_id_from_ecp_group_id(
Gilles Peskine449bd832023-01-11 14:50:10 +01001086 curve_list[i]);
1087 if (tls_id == 0) {
1088 mbedtls_free(group_list);
1089 return MBEDTLS_ERR_SSL_BAD_CONFIG;
Brett Warrene0edc842021-08-17 09:53:13 +01001090 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01001091 group_list[i] = tls_id;
Brett Warrene0edc842021-08-17 09:53:13 +01001092 }
1093
1094 group_list[length] = 0;
1095
1096 ssl->handshake->group_list = group_list;
1097 ssl->handshake->group_list_heap_allocated = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001098 } else {
Brett Warrene0edc842021-08-17 09:53:13 +01001099 ssl->handshake->group_list = ssl->conf->group_list;
1100 ssl->handshake->group_list_heap_allocated = 0;
1101 }
1102#endif /* MBEDTLS_DEPRECATED_REMOVED */
1103#endif /* MBEDTLS_ECP_C */
1104
Ronald Crone68ab4f2022-10-05 12:46:29 +02001105#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +08001106#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Jerry Yua69269a2022-01-17 21:06:01 +08001107#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yu713013f2022-01-17 18:16:35 +08001108 /* Heap allocate and translate sig_hashes from internal hash identifiers to
1109 signature algorithms IANA identifiers. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001110 if (mbedtls_ssl_conf_is_tls12_only(ssl->conf) &&
1111 ssl->conf->sig_hashes != NULL) {
Jerry Yuf017ee42022-01-12 15:49:48 +08001112 const int *md;
1113 const int *sig_hashes = ssl->conf->sig_hashes;
Jerry Yub476a442022-01-21 18:14:45 +08001114 size_t sig_algs_len = 0;
Jerry Yuf017ee42022-01-12 15:49:48 +08001115 uint16_t *p;
1116
Jerry Yub476a442022-01-21 18:14:45 +08001117#if defined(static_assert)
Gilles Peskine449bd832023-01-11 14:50:10 +01001118 static_assert(MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN
1119 <= (SIZE_MAX - (2 * sizeof(uint16_t))),
1120 "MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN too big");
Jerry Yua68dca22022-01-20 16:28:27 +08001121#endif
1122
Gilles Peskine449bd832023-01-11 14:50:10 +01001123 for (md = sig_hashes; *md != MBEDTLS_MD_NONE; md++) {
1124 if (mbedtls_ssl_hash_from_md_alg(*md) == MBEDTLS_SSL_HASH_NONE) {
Jerry Yuf017ee42022-01-12 15:49:48 +08001125 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001126 }
Jerry Yub476a442022-01-21 18:14:45 +08001127#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001128 sig_algs_len += sizeof(uint16_t);
Jerry Yub476a442022-01-21 18:14:45 +08001129#endif
Jerry Yua68dca22022-01-20 16:28:27 +08001130
Jerry Yub476a442022-01-21 18:14:45 +08001131#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001132 sig_algs_len += sizeof(uint16_t);
Jerry Yub476a442022-01-21 18:14:45 +08001133#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001134 if (sig_algs_len > MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN) {
1135 return MBEDTLS_ERR_SSL_BAD_CONFIG;
1136 }
Jerry Yuf017ee42022-01-12 15:49:48 +08001137 }
1138
Gilles Peskine449bd832023-01-11 14:50:10 +01001139 if (sig_algs_len < MBEDTLS_SSL_MIN_SIG_ALG_LIST_LEN) {
1140 return MBEDTLS_ERR_SSL_BAD_CONFIG;
1141 }
Jerry Yuf017ee42022-01-12 15:49:48 +08001142
Gilles Peskine449bd832023-01-11 14:50:10 +01001143 ssl->handshake->sig_algs = mbedtls_calloc(1, sig_algs_len +
1144 sizeof(uint16_t));
1145 if (ssl->handshake->sig_algs == NULL) {
1146 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
1147 }
Jerry Yuf017ee42022-01-12 15:49:48 +08001148
Gilles Peskine449bd832023-01-11 14:50:10 +01001149 p = (uint16_t *) ssl->handshake->sig_algs;
1150 for (md = sig_hashes; *md != MBEDTLS_MD_NONE; md++) {
1151 unsigned char hash = mbedtls_ssl_hash_from_md_alg(*md);
1152 if (hash == MBEDTLS_SSL_HASH_NONE) {
Jerry Yuf017ee42022-01-12 15:49:48 +08001153 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001154 }
Jerry Yu6106fdc2022-01-12 16:36:14 +08001155#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001156 *p = ((hash << 8) | MBEDTLS_SSL_SIG_ECDSA);
Jerry Yuf017ee42022-01-12 15:49:48 +08001157 p++;
Jerry Yu6106fdc2022-01-12 16:36:14 +08001158#endif
1159#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001160 *p = ((hash << 8) | MBEDTLS_SSL_SIG_RSA);
Jerry Yuf017ee42022-01-12 15:49:48 +08001161 p++;
Jerry Yu6106fdc2022-01-12 16:36:14 +08001162#endif
Jerry Yuf017ee42022-01-12 15:49:48 +08001163 }
Gabor Mezei15b95a62022-05-09 16:37:58 +02001164 *p = MBEDTLS_TLS_SIG_NONE;
Jerry Yuf017ee42022-01-12 15:49:48 +08001165 ssl->handshake->sig_algs_heap_allocated = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001166 } else
Jerry Yua69269a2022-01-17 21:06:01 +08001167#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Jerry Yuf017ee42022-01-12 15:49:48 +08001168 {
Jerry Yuf017ee42022-01-12 15:49:48 +08001169 ssl->handshake->sig_algs_heap_allocated = 0;
1170 }
Jerry Yucc539102022-06-27 16:27:35 +08001171#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Ronald Crone68ab4f2022-10-05 12:46:29 +02001172#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Gilles Peskine449bd832023-01-11 14:50:10 +01001173 return 0;
Paul Bakker48916f92012-09-16 19:57:18 +00001174}
1175
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001176#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001177/* Dummy cookie callbacks for defaults */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001178MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001179static int ssl_cookie_write_dummy(void *ctx,
1180 unsigned char **p, unsigned char *end,
1181 const unsigned char *cli_id, size_t cli_id_len)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001182{
1183 ((void) ctx);
1184 ((void) p);
1185 ((void) end);
1186 ((void) cli_id);
1187 ((void) cli_id_len);
1188
Gilles Peskine449bd832023-01-11 14:50:10 +01001189 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001190}
1191
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001192MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001193static int ssl_cookie_check_dummy(void *ctx,
1194 const unsigned char *cookie, size_t cookie_len,
1195 const unsigned char *cli_id, size_t cli_id_len)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001196{
1197 ((void) ctx);
1198 ((void) cookie);
1199 ((void) cookie_len);
1200 ((void) cli_id);
1201 ((void) cli_id_len);
1202
Gilles Peskine449bd832023-01-11 14:50:10 +01001203 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001204}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001205#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001206
Paul Bakker5121ce52009-01-03 21:22:43 +00001207/*
1208 * Initialize an SSL context
1209 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001210void mbedtls_ssl_init(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001211{
Gilles Peskine449bd832023-01-11 14:50:10 +01001212 memset(ssl, 0, sizeof(mbedtls_ssl_context));
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001213}
1214
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001215MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001216static int ssl_conf_version_check(const mbedtls_ssl_context *ssl)
Jerry Yu60835a82021-08-04 10:13:52 +08001217{
Ronald Cron086ee0b2022-03-15 15:18:51 +01001218 const mbedtls_ssl_config *conf = ssl->conf;
1219
Ronald Cron6f135e12021-12-08 16:57:54 +01001220#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001221 if (mbedtls_ssl_conf_is_tls13_only(conf)) {
1222 if (conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
1223 MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS 1.3 is not yet supported."));
1224 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQianed582dd2022-04-13 08:21:05 +00001225 }
1226
Gilles Peskine449bd832023-01-11 14:50:10 +01001227 MBEDTLS_SSL_DEBUG_MSG(4, ("The SSL configuration is tls13 only."));
1228 return 0;
Jerry Yu60835a82021-08-04 10:13:52 +08001229 }
1230#endif
1231
1232#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01001233 if (mbedtls_ssl_conf_is_tls12_only(conf)) {
1234 MBEDTLS_SSL_DEBUG_MSG(4, ("The SSL configuration is tls12 only."));
1235 return 0;
Jerry Yu60835a82021-08-04 10:13:52 +08001236 }
1237#endif
1238
Ronald Cron6f135e12021-12-08 16:57:54 +01001239#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001240 if (mbedtls_ssl_conf_is_hybrid_tls12_tls13(conf)) {
1241 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
1242 MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS not yet supported in Hybrid TLS 1.3 + TLS 1.2"));
1243 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQianed582dd2022-04-13 08:21:05 +00001244 }
1245
Gilles Peskine449bd832023-01-11 14:50:10 +01001246 if (conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
1247 MBEDTLS_SSL_DEBUG_MSG(1, ("TLS 1.3 server is not supported yet."));
1248 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQian8f9dfe42022-04-15 02:52:39 +00001249 }
1250
1251
Gilles Peskine449bd832023-01-11 14:50:10 +01001252 MBEDTLS_SSL_DEBUG_MSG(4, ("The SSL configuration is TLS 1.3 or TLS 1.2."));
1253 return 0;
Jerry Yu60835a82021-08-04 10:13:52 +08001254 }
1255#endif
1256
Gilles Peskine449bd832023-01-11 14:50:10 +01001257 MBEDTLS_SSL_DEBUG_MSG(1, ("The SSL configuration is invalid."));
1258 return MBEDTLS_ERR_SSL_BAD_CONFIG;
Jerry Yu60835a82021-08-04 10:13:52 +08001259}
1260
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001261MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu60835a82021-08-04 10:13:52 +08001262static int ssl_conf_check(const mbedtls_ssl_context *ssl)
1263{
1264 int ret;
Gilles Peskine449bd832023-01-11 14:50:10 +01001265 ret = ssl_conf_version_check(ssl);
1266 if (ret != 0) {
1267 return ret;
1268 }
Jerry Yu60835a82021-08-04 10:13:52 +08001269
Jerry Yudef7ae42022-10-30 14:13:19 +08001270#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1271 /* RFC 8446 section 4.4.3
1272 *
1273 * If the verification fails, the receiver MUST terminate the handshake with
1274 * a "decrypt_error" alert.
1275 *
1276 * If the client is configured as TLS 1.3 only with optional verify, return
1277 * bad config.
1278 *
1279 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001280 if (mbedtls_ssl_conf_tls13_ephemeral_enabled(
1281 (mbedtls_ssl_context *) ssl) &&
Jerry Yudef7ae42022-10-30 14:13:19 +08001282 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
1283 ssl->conf->max_tls_version == MBEDTLS_SSL_VERSION_TLS1_3 &&
1284 ssl->conf->min_tls_version == MBEDTLS_SSL_VERSION_TLS1_3 &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001285 ssl->conf->authmode == MBEDTLS_SSL_VERIFY_OPTIONAL) {
Jerry Yudef7ae42022-10-30 14:13:19 +08001286 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +01001287 1, ("Optional verify auth mode "
1288 "is not available for TLS 1.3 client"));
1289 return MBEDTLS_ERR_SSL_BAD_CONFIG;
Jerry Yudef7ae42022-10-30 14:13:19 +08001290 }
1291#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
1292
Jerry Yu60835a82021-08-04 10:13:52 +08001293 /* Space for further checks */
1294
Gilles Peskine449bd832023-01-11 14:50:10 +01001295 return 0;
Jerry Yu60835a82021-08-04 10:13:52 +08001296}
1297
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001298/*
1299 * Setup an SSL context
1300 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01001301
Gilles Peskine449bd832023-01-11 14:50:10 +01001302int mbedtls_ssl_setup(mbedtls_ssl_context *ssl,
1303 const mbedtls_ssl_config *conf)
Paul Bakker5121ce52009-01-03 21:22:43 +00001304{
Janos Follath865b3eb2019-12-16 11:46:15 +00001305 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Darryl Greenb33cc762019-11-28 14:29:44 +00001306 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1307 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00001308
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001309 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00001310
Gilles Peskine449bd832023-01-11 14:50:10 +01001311 if ((ret = ssl_conf_check(ssl)) != 0) {
1312 return ret;
1313 }
Jerry Yu60835a82021-08-04 10:13:52 +08001314
Paul Bakker62f2dee2012-09-28 07:31:51 +00001315 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001316 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00001317 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02001318
1319 /* Set to NULL in case of an error condition */
1320 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02001321
Darryl Greenb33cc762019-11-28 14:29:44 +00001322#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1323 ssl->in_buf_len = in_buf_len;
1324#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001325 ssl->in_buf = mbedtls_calloc(1, in_buf_len);
1326 if (ssl->in_buf == NULL) {
1327 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", in_buf_len));
k-stachowiak9f7798e2018-07-31 16:52:32 +02001328 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02001329 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10001330 }
1331
Darryl Greenb33cc762019-11-28 14:29:44 +00001332#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1333 ssl->out_buf_len = out_buf_len;
1334#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001335 ssl->out_buf = mbedtls_calloc(1, out_buf_len);
1336 if (ssl->out_buf == NULL) {
1337 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", out_buf_len));
k-stachowiak9f7798e2018-07-31 16:52:32 +02001338 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02001339 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00001340 }
1341
Gilles Peskine449bd832023-01-11 14:50:10 +01001342 mbedtls_ssl_reset_in_out_pointers(ssl);
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02001343
Johan Pascalb62bb512015-12-03 21:56:45 +01001344#if defined(MBEDTLS_SSL_DTLS_SRTP)
Gilles Peskine449bd832023-01-11 14:50:10 +01001345 memset(&ssl->dtls_srtp_info, 0, sizeof(ssl->dtls_srtp_info));
Johan Pascalb62bb512015-12-03 21:56:45 +01001346#endif
1347
Gilles Peskine449bd832023-01-11 14:50:10 +01001348 if ((ret = ssl_handshake_init(ssl)) != 0) {
k-stachowiaka47911c2018-07-04 17:41:58 +02001349 goto error;
Gilles Peskine449bd832023-01-11 14:50:10 +01001350 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001351
Gilles Peskine449bd832023-01-11 14:50:10 +01001352 return 0;
k-stachowiaka47911c2018-07-04 17:41:58 +02001353
1354error:
Gilles Peskine449bd832023-01-11 14:50:10 +01001355 mbedtls_free(ssl->in_buf);
1356 mbedtls_free(ssl->out_buf);
k-stachowiaka47911c2018-07-04 17:41:58 +02001357
1358 ssl->conf = NULL;
1359
Darryl Greenb33cc762019-11-28 14:29:44 +00001360#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1361 ssl->in_buf_len = 0;
1362 ssl->out_buf_len = 0;
1363#endif
k-stachowiaka47911c2018-07-04 17:41:58 +02001364 ssl->in_buf = NULL;
1365 ssl->out_buf = NULL;
1366
1367 ssl->in_hdr = NULL;
1368 ssl->in_ctr = NULL;
1369 ssl->in_len = NULL;
1370 ssl->in_iv = NULL;
1371 ssl->in_msg = NULL;
1372
1373 ssl->out_hdr = NULL;
1374 ssl->out_ctr = NULL;
1375 ssl->out_len = NULL;
1376 ssl->out_iv = NULL;
1377 ssl->out_msg = NULL;
1378
Gilles Peskine449bd832023-01-11 14:50:10 +01001379 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001380}
1381
1382/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00001383 * Reset an initialized and used SSL context for re-use while retaining
1384 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001385 *
1386 * If partial is non-zero, keep data in the input buffer and client ID.
1387 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00001388 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001389void mbedtls_ssl_session_reset_msg_layer(mbedtls_ssl_context *ssl,
1390 int partial)
Paul Bakker7eb013f2011-10-06 12:37:39 +00001391{
Darryl Greenb33cc762019-11-28 14:29:44 +00001392#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1393 size_t in_buf_len = ssl->in_buf_len;
1394 size_t out_buf_len = ssl->out_buf_len;
1395#else
1396 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1397 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
1398#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001399
Hanno Beckerb0302c42021-08-03 09:39:42 +01001400#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || !defined(MBEDTLS_SSL_SRV_C)
1401 partial = 0;
Hanno Becker7e772132018-08-10 12:38:21 +01001402#endif
1403
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001404 /* Cancel any possibly running timer */
Gilles Peskine449bd832023-01-11 14:50:10 +01001405 mbedtls_ssl_set_timer(ssl, 0);
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001406
Gilles Peskine449bd832023-01-11 14:50:10 +01001407 mbedtls_ssl_reset_in_out_pointers(ssl);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001408
1409 /* Reset incoming message parsing */
1410 ssl->in_offt = NULL;
1411 ssl->nb_zero = 0;
1412 ssl->in_msgtype = 0;
1413 ssl->in_msglen = 0;
1414 ssl->in_hslen = 0;
1415 ssl->keep_current_message = 0;
1416 ssl->transform_in = NULL;
1417
1418#if defined(MBEDTLS_SSL_PROTO_DTLS)
1419 ssl->next_record_offset = 0;
1420 ssl->in_epoch = 0;
1421#endif
1422
1423 /* Keep current datagram if partial == 1 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001424 if (partial == 0) {
Hanno Beckerb0302c42021-08-03 09:39:42 +01001425 ssl->in_left = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001426 memset(ssl->in_buf, 0, in_buf_len);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001427 }
1428
Ronald Cronad8c17b2022-06-10 17:18:09 +02001429 ssl->send_alert = 0;
1430
Hanno Beckerb0302c42021-08-03 09:39:42 +01001431 /* Reset outgoing message writing */
1432 ssl->out_msgtype = 0;
1433 ssl->out_msglen = 0;
1434 ssl->out_left = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001435 memset(ssl->out_buf, 0, out_buf_len);
1436 memset(ssl->cur_out_ctr, 0, sizeof(ssl->cur_out_ctr));
Hanno Beckerb0302c42021-08-03 09:39:42 +01001437 ssl->transform_out = NULL;
1438
1439#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine449bd832023-01-11 14:50:10 +01001440 mbedtls_ssl_dtls_replay_reset(ssl);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001441#endif
1442
XiaokangQian2b01dc32022-01-21 02:53:13 +00001443#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01001444 if (ssl->transform) {
1445 mbedtls_ssl_transform_free(ssl->transform);
1446 mbedtls_free(ssl->transform);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001447 ssl->transform = NULL;
1448 }
XiaokangQian2b01dc32022-01-21 02:53:13 +00001449#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1450
XiaokangQian2b01dc32022-01-21 02:53:13 +00001451#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001452 mbedtls_ssl_transform_free(ssl->transform_application);
1453 mbedtls_free(ssl->transform_application);
XiaokangQian2b01dc32022-01-21 02:53:13 +00001454 ssl->transform_application = NULL;
1455
Gilles Peskine449bd832023-01-11 14:50:10 +01001456 if (ssl->handshake != NULL) {
Jerry Yu3d9b5902022-11-04 14:07:25 +08001457#if defined(MBEDTLS_SSL_EARLY_DATA)
Gilles Peskine449bd832023-01-11 14:50:10 +01001458 mbedtls_ssl_transform_free(ssl->handshake->transform_earlydata);
1459 mbedtls_free(ssl->handshake->transform_earlydata);
XiaokangQian2b01dc32022-01-21 02:53:13 +00001460 ssl->handshake->transform_earlydata = NULL;
Jerry Yu3d9b5902022-11-04 14:07:25 +08001461#endif
XiaokangQian2b01dc32022-01-21 02:53:13 +00001462
Gilles Peskine449bd832023-01-11 14:50:10 +01001463 mbedtls_ssl_transform_free(ssl->handshake->transform_handshake);
1464 mbedtls_free(ssl->handshake->transform_handshake);
XiaokangQian2b01dc32022-01-21 02:53:13 +00001465 ssl->handshake->transform_handshake = NULL;
1466 }
1467
XiaokangQian2b01dc32022-01-21 02:53:13 +00001468#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerb0302c42021-08-03 09:39:42 +01001469}
1470
Gilles Peskine449bd832023-01-11 14:50:10 +01001471int mbedtls_ssl_session_reset_int(mbedtls_ssl_context *ssl, int partial)
Hanno Beckerb0302c42021-08-03 09:39:42 +01001472{
1473 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1474
1475 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
1476
Gilles Peskine449bd832023-01-11 14:50:10 +01001477 mbedtls_ssl_session_reset_msg_layer(ssl, partial);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001478
1479 /* Reset renegotiation state */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001480#if defined(MBEDTLS_SSL_RENEGOTIATION)
1481 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001482 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00001483
1484 ssl->verify_data_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001485 memset(ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN);
1486 memset(ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN);
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001487#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001488 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00001489
Hanno Beckerb0302c42021-08-03 09:39:42 +01001490 ssl->session_in = NULL;
Hanno Becker78640902018-08-13 16:35:15 +01001491 ssl->session_out = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001492 if (ssl->session) {
1493 mbedtls_ssl_session_free(ssl->session);
1494 mbedtls_free(ssl->session);
Paul Bakkerc0463502013-02-14 11:19:38 +01001495 ssl->session = NULL;
1496 }
1497
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001498#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02001499 ssl->alpn_chosen = NULL;
1500#endif
1501
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001502#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
David Horstmann3b2276a2022-10-06 14:49:08 +01001503 int free_cli_id = 1;
Hanno Becker4ccbf062018-08-10 11:20:38 +01001504#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Gilles Peskine449bd832023-01-11 14:50:10 +01001505 free_cli_id = (partial == 0);
Hanno Becker4ccbf062018-08-10 11:20:38 +01001506#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001507 if (free_cli_id) {
1508 mbedtls_free(ssl->cli_id);
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001509 ssl->cli_id = NULL;
1510 ssl->cli_id_len = 0;
1511 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02001512#endif
1513
Gilles Peskine449bd832023-01-11 14:50:10 +01001514 if ((ret = ssl_handshake_init(ssl)) != 0) {
1515 return ret;
1516 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00001517
Gilles Peskine449bd832023-01-11 14:50:10 +01001518 return 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00001519}
1520
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02001521/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001522 * Reset an initialized and used SSL context for re-use while retaining
1523 * all application-set variables, function pointers and data.
1524 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001525int mbedtls_ssl_session_reset(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001526{
Gilles Peskine449bd832023-01-11 14:50:10 +01001527 return mbedtls_ssl_session_reset_int(ssl, 0);
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001528}
1529
1530/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001531 * SSL set accessors
1532 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001533void mbedtls_ssl_conf_endpoint(mbedtls_ssl_config *conf, int endpoint)
Paul Bakker5121ce52009-01-03 21:22:43 +00001534{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001535 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00001536}
1537
Gilles Peskine449bd832023-01-11 14:50:10 +01001538void mbedtls_ssl_conf_transport(mbedtls_ssl_config *conf, int transport)
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001539{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001540 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001541}
1542
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001543#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine449bd832023-01-11 14:50:10 +01001544void mbedtls_ssl_conf_dtls_anti_replay(mbedtls_ssl_config *conf, char mode)
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001545{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001546 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001547}
1548#endif
1549
Gilles Peskine449bd832023-01-11 14:50:10 +01001550void mbedtls_ssl_conf_dtls_badmac_limit(mbedtls_ssl_config *conf, unsigned limit)
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001551{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001552 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001553}
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001554
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001555#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01001556
Gilles Peskine449bd832023-01-11 14:50:10 +01001557void mbedtls_ssl_set_datagram_packing(mbedtls_ssl_context *ssl,
1558 unsigned allow_packing)
Hanno Becker04da1892018-08-14 13:22:10 +01001559{
1560 ssl->disable_datagram_packing = !allow_packing;
1561}
1562
Gilles Peskine449bd832023-01-11 14:50:10 +01001563void mbedtls_ssl_conf_handshake_timeout(mbedtls_ssl_config *conf,
1564 uint32_t min, uint32_t max)
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001565{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001566 conf->hs_timeout_min = min;
1567 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001568}
1569#endif
1570
Gilles Peskine449bd832023-01-11 14:50:10 +01001571void mbedtls_ssl_conf_authmode(mbedtls_ssl_config *conf, int authmode)
Paul Bakker5121ce52009-01-03 21:22:43 +00001572{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001573 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00001574}
1575
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001576#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001577void mbedtls_ssl_conf_verify(mbedtls_ssl_config *conf,
1578 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
1579 void *p_vrfy)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001580{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001581 conf->f_vrfy = f_vrfy;
1582 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001583}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001584#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001585
Gilles Peskine449bd832023-01-11 14:50:10 +01001586void mbedtls_ssl_conf_rng(mbedtls_ssl_config *conf,
1587 int (*f_rng)(void *, unsigned char *, size_t),
1588 void *p_rng)
Paul Bakker5121ce52009-01-03 21:22:43 +00001589{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001590 conf->f_rng = f_rng;
1591 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00001592}
1593
Gilles Peskine449bd832023-01-11 14:50:10 +01001594void mbedtls_ssl_conf_dbg(mbedtls_ssl_config *conf,
1595 void (*f_dbg)(void *, int, const char *, int, const char *),
1596 void *p_dbg)
Paul Bakker5121ce52009-01-03 21:22:43 +00001597{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001598 conf->f_dbg = f_dbg;
1599 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00001600}
1601
Gilles Peskine449bd832023-01-11 14:50:10 +01001602void mbedtls_ssl_set_bio(mbedtls_ssl_context *ssl,
1603 void *p_bio,
1604 mbedtls_ssl_send_t *f_send,
1605 mbedtls_ssl_recv_t *f_recv,
1606 mbedtls_ssl_recv_timeout_t *f_recv_timeout)
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001607{
1608 ssl->p_bio = p_bio;
1609 ssl->f_send = f_send;
1610 ssl->f_recv = f_recv;
1611 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001612}
1613
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02001614#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001615void mbedtls_ssl_set_mtu(mbedtls_ssl_context *ssl, uint16_t mtu)
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02001616{
1617 ssl->mtu = mtu;
1618}
1619#endif
1620
Gilles Peskine449bd832023-01-11 14:50:10 +01001621void mbedtls_ssl_conf_read_timeout(mbedtls_ssl_config *conf, uint32_t timeout)
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001622{
1623 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001624}
1625
Gilles Peskine449bd832023-01-11 14:50:10 +01001626void mbedtls_ssl_set_timer_cb(mbedtls_ssl_context *ssl,
1627 void *p_timer,
1628 mbedtls_ssl_set_timer_t *f_set_timer,
1629 mbedtls_ssl_get_timer_t *f_get_timer)
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001630{
1631 ssl->p_timer = p_timer;
1632 ssl->f_set_timer = f_set_timer;
1633 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001634
1635 /* Make sure we start with no timer running */
Gilles Peskine449bd832023-01-11 14:50:10 +01001636 mbedtls_ssl_set_timer(ssl, 0);
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001637}
1638
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001639#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001640void mbedtls_ssl_conf_session_cache(mbedtls_ssl_config *conf,
1641 void *p_cache,
1642 mbedtls_ssl_cache_get_t *f_get_cache,
1643 mbedtls_ssl_cache_set_t *f_set_cache)
Paul Bakker5121ce52009-01-03 21:22:43 +00001644{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01001645 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001646 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001647 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00001648}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001649#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001650
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001651#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001652int mbedtls_ssl_set_session(mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session)
Paul Bakker5121ce52009-01-03 21:22:43 +00001653{
Janos Follath865b3eb2019-12-16 11:46:15 +00001654 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001655
Gilles Peskine449bd832023-01-11 14:50:10 +01001656 if (ssl == NULL ||
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001657 session == NULL ||
1658 ssl->session_negotiate == NULL ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001659 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT) {
1660 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001661 }
1662
Gilles Peskine449bd832023-01-11 14:50:10 +01001663 if (ssl->handshake->resume == 1) {
1664 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
1665 }
Hanno Beckere810bbc2021-05-14 16:01:05 +01001666
Jerry Yu21092062022-10-10 21:21:31 +08001667#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001668 if (session->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
Jerry Yu21092062022-10-10 21:21:31 +08001669 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01001670 mbedtls_ssl_ciphersuite_from_id(session->ciphersuite);
Jerry Yu21092062022-10-10 21:21:31 +08001671
Gilles Peskine449bd832023-01-11 14:50:10 +01001672 if (mbedtls_ssl_validate_ciphersuite(
Jerry Yu21092062022-10-10 21:21:31 +08001673 ssl, ciphersuite_info, MBEDTLS_SSL_VERSION_TLS1_3,
Gilles Peskine449bd832023-01-11 14:50:10 +01001674 MBEDTLS_SSL_VERSION_TLS1_3) != 0) {
1675 MBEDTLS_SSL_DEBUG_MSG(4, ("%d is not a valid TLS 1.3 ciphersuite.",
1676 session->ciphersuite));
1677 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jerry Yu21092062022-10-10 21:21:31 +08001678 }
Jerry Yu40afab62022-10-08 10:42:13 +08001679 }
Jerry Yu21092062022-10-10 21:21:31 +08001680#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu40afab62022-10-08 10:42:13 +08001681
Gilles Peskine449bd832023-01-11 14:50:10 +01001682 if ((ret = mbedtls_ssl_session_copy(ssl->session_negotiate,
1683 session)) != 0) {
1684 return ret;
1685 }
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001686
Paul Bakker0a597072012-09-25 21:55:46 +00001687 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001688
Gilles Peskine449bd832023-01-11 14:50:10 +01001689 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001690}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001691#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001692
Gilles Peskine449bd832023-01-11 14:50:10 +01001693void mbedtls_ssl_conf_ciphersuites(mbedtls_ssl_config *conf,
1694 const int *ciphersuites)
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001695{
Hanno Beckerd60b6c62021-04-29 12:04:11 +01001696 conf->ciphersuite_list = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00001697}
1698
Ronald Cron6f135e12021-12-08 16:57:54 +01001699#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001700void mbedtls_ssl_conf_tls13_key_exchange_modes(mbedtls_ssl_config *conf,
1701 const int kex_modes)
Hanno Becker71f1ed62021-07-24 06:01:47 +01001702{
Xiaofei Bai746f9482021-11-12 08:53:56 +00001703 conf->tls13_kex_modes = kex_modes & MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Hanno Becker71f1ed62021-07-24 06:01:47 +01001704}
Xiaokang Qian72de95d2022-10-25 02:54:33 +00001705
1706#if defined(MBEDTLS_SSL_EARLY_DATA)
Gilles Peskine449bd832023-01-11 14:50:10 +01001707void mbedtls_ssl_tls13_conf_early_data(mbedtls_ssl_config *conf,
1708 int early_data_enabled)
Xiaokang Qian72de95d2022-10-25 02:54:33 +00001709{
1710 conf->early_data_enabled = early_data_enabled;
1711}
Jerry Yucc4e0072022-11-22 17:22:22 +08001712
1713#if defined(MBEDTLS_SSL_SRV_C)
1714void mbedtls_ssl_tls13_conf_max_early_data_size(
Gilles Peskine449bd832023-01-11 14:50:10 +01001715 mbedtls_ssl_config *conf, uint32_t max_early_data_size)
Jerry Yucc4e0072022-11-22 17:22:22 +08001716{
Jerry Yu39da9852022-12-06 16:58:36 +08001717 conf->max_early_data_size = max_early_data_size;
Jerry Yucc4e0072022-11-22 17:22:22 +08001718}
1719#endif /* MBEDTLS_SSL_SRV_C */
1720
Xiaokang Qian72de95d2022-10-25 02:54:33 +00001721#endif /* MBEDTLS_SSL_EARLY_DATA */
Ronald Cron6f135e12021-12-08 16:57:54 +01001722#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01001723
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001724#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001725void mbedtls_ssl_conf_cert_profile(mbedtls_ssl_config *conf,
1726 const mbedtls_x509_crt_profile *profile)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02001727{
1728 conf->cert_profile = profile;
1729}
1730
Gilles Peskine449bd832023-01-11 14:50:10 +01001731static void ssl_key_cert_free(mbedtls_ssl_key_cert *key_cert)
Glenn Strauss36872db2022-01-22 05:06:31 -05001732{
1733 mbedtls_ssl_key_cert *cur = key_cert, *next;
1734
Gilles Peskine449bd832023-01-11 14:50:10 +01001735 while (cur != NULL) {
Glenn Strauss36872db2022-01-22 05:06:31 -05001736 next = cur->next;
Gilles Peskine449bd832023-01-11 14:50:10 +01001737 mbedtls_free(cur);
Glenn Strauss36872db2022-01-22 05:06:31 -05001738 cur = next;
1739 }
1740}
1741
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001742/* Append a new keycert entry to a (possibly empty) list */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001743MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001744static int ssl_append_key_cert(mbedtls_ssl_key_cert **head,
1745 mbedtls_x509_crt *cert,
1746 mbedtls_pk_context *key)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001747{
niisato8ee24222018-06-25 19:05:48 +09001748 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001749
Gilles Peskine449bd832023-01-11 14:50:10 +01001750 if (cert == NULL) {
Glenn Strauss36872db2022-01-22 05:06:31 -05001751 /* Free list if cert is null */
Gilles Peskine449bd832023-01-11 14:50:10 +01001752 ssl_key_cert_free(*head);
Glenn Strauss36872db2022-01-22 05:06:31 -05001753 *head = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001754 return 0;
Glenn Strauss36872db2022-01-22 05:06:31 -05001755 }
1756
Gilles Peskine449bd832023-01-11 14:50:10 +01001757 new_cert = mbedtls_calloc(1, sizeof(mbedtls_ssl_key_cert));
1758 if (new_cert == NULL) {
1759 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
1760 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001761
niisato8ee24222018-06-25 19:05:48 +09001762 new_cert->cert = cert;
1763 new_cert->key = key;
1764 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001765
Glenn Strauss36872db2022-01-22 05:06:31 -05001766 /* Update head if the list was null, else add to the end */
Gilles Peskine449bd832023-01-11 14:50:10 +01001767 if (*head == NULL) {
niisato8ee24222018-06-25 19:05:48 +09001768 *head = new_cert;
Gilles Peskine449bd832023-01-11 14:50:10 +01001769 } else {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001770 mbedtls_ssl_key_cert *cur = *head;
Gilles Peskine449bd832023-01-11 14:50:10 +01001771 while (cur->next != NULL) {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001772 cur = cur->next;
Gilles Peskine449bd832023-01-11 14:50:10 +01001773 }
niisato8ee24222018-06-25 19:05:48 +09001774 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001775 }
1776
Gilles Peskine449bd832023-01-11 14:50:10 +01001777 return 0;
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001778}
1779
Gilles Peskine449bd832023-01-11 14:50:10 +01001780int mbedtls_ssl_conf_own_cert(mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001781 mbedtls_x509_crt *own_cert,
Gilles Peskine449bd832023-01-11 14:50:10 +01001782 mbedtls_pk_context *pk_key)
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001783{
Gilles Peskine449bd832023-01-11 14:50:10 +01001784 return ssl_append_key_cert(&conf->key_cert, own_cert, pk_key);
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001785}
1786
Gilles Peskine449bd832023-01-11 14:50:10 +01001787void mbedtls_ssl_conf_ca_chain(mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001788 mbedtls_x509_crt *ca_chain,
Gilles Peskine449bd832023-01-11 14:50:10 +01001789 mbedtls_x509_crl *ca_crl)
Paul Bakker5121ce52009-01-03 21:22:43 +00001790{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001791 conf->ca_chain = ca_chain;
1792 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00001793
1794#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1795 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1796 * cannot be used together. */
1797 conf->f_ca_cb = NULL;
1798 conf->p_ca_cb = NULL;
1799#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00001800}
Hanno Becker5adaad92019-03-27 16:54:37 +00001801
1802#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Gilles Peskine449bd832023-01-11 14:50:10 +01001803void mbedtls_ssl_conf_ca_cb(mbedtls_ssl_config *conf,
1804 mbedtls_x509_crt_ca_cb_t f_ca_cb,
1805 void *p_ca_cb)
Hanno Becker5adaad92019-03-27 16:54:37 +00001806{
1807 conf->f_ca_cb = f_ca_cb;
1808 conf->p_ca_cb = p_ca_cb;
1809
1810 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1811 * cannot be used together. */
1812 conf->ca_chain = NULL;
1813 conf->ca_crl = NULL;
1814}
1815#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001816#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00001817
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001818#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01001819const unsigned char *mbedtls_ssl_get_hs_sni(mbedtls_ssl_context *ssl,
1820 size_t *name_len)
Glenn Strauss69894072022-01-24 12:58:00 -05001821{
1822 *name_len = ssl->handshake->sni_name_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001823 return ssl->handshake->sni_name;
Glenn Strauss69894072022-01-24 12:58:00 -05001824}
1825
Gilles Peskine449bd832023-01-11 14:50:10 +01001826int mbedtls_ssl_set_hs_own_cert(mbedtls_ssl_context *ssl,
1827 mbedtls_x509_crt *own_cert,
1828 mbedtls_pk_context *pk_key)
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001829{
Gilles Peskine449bd832023-01-11 14:50:10 +01001830 return ssl_append_key_cert(&ssl->handshake->sni_key_cert,
1831 own_cert, pk_key);
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001832}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02001833
Gilles Peskine449bd832023-01-11 14:50:10 +01001834void mbedtls_ssl_set_hs_ca_chain(mbedtls_ssl_context *ssl,
1835 mbedtls_x509_crt *ca_chain,
1836 mbedtls_x509_crl *ca_crl)
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02001837{
1838 ssl->handshake->sni_ca_chain = ca_chain;
1839 ssl->handshake->sni_ca_crl = ca_crl;
1840}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001841
Glenn Strauss999ef702022-03-11 01:37:23 -05001842#if defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001843void mbedtls_ssl_set_hs_dn_hints(mbedtls_ssl_context *ssl,
1844 const mbedtls_x509_crt *crt)
Glenn Strauss999ef702022-03-11 01:37:23 -05001845{
1846 ssl->handshake->dn_hints = crt;
1847}
1848#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
1849
Gilles Peskine449bd832023-01-11 14:50:10 +01001850void mbedtls_ssl_set_hs_authmode(mbedtls_ssl_context *ssl,
1851 int authmode)
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001852{
1853 ssl->handshake->sni_authmode = authmode;
1854}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001855#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
1856
Hanno Becker8927c832019-04-03 12:52:50 +01001857#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001858void mbedtls_ssl_set_verify(mbedtls_ssl_context *ssl,
1859 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
1860 void *p_vrfy)
Hanno Becker8927c832019-04-03 12:52:50 +01001861{
1862 ssl->f_vrfy = f_vrfy;
1863 ssl->p_vrfy = p_vrfy;
1864}
1865#endif
1866
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02001867#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001868
Valerio Setti016f6822022-12-09 14:17:50 +01001869#if defined(MBEDTLS_USE_PSA_CRYPTO)
1870static psa_status_t mbedtls_ssl_set_hs_ecjpake_password_common(
Gilles Peskine449bd832023-01-11 14:50:10 +01001871 mbedtls_ssl_context *ssl,
1872 mbedtls_svc_key_id_t pwd)
Valerio Setti016f6822022-12-09 14:17:50 +01001873{
1874 psa_status_t status;
1875 psa_pake_role_t psa_role;
1876 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
1877
Gilles Peskine449bd832023-01-11 14:50:10 +01001878 psa_pake_cs_set_algorithm(&cipher_suite, PSA_ALG_JPAKE);
1879 psa_pake_cs_set_primitive(&cipher_suite,
1880 PSA_PAKE_PRIMITIVE(PSA_PAKE_PRIMITIVE_TYPE_ECC,
1881 PSA_ECC_FAMILY_SECP_R1,
1882 256));
1883 psa_pake_cs_set_hash(&cipher_suite, PSA_ALG_SHA_256);
Valerio Setti016f6822022-12-09 14:17:50 +01001884
Gilles Peskine449bd832023-01-11 14:50:10 +01001885 status = psa_pake_setup(&ssl->handshake->psa_pake_ctx, &cipher_suite);
1886 if (status != PSA_SUCCESS) {
Valerio Setti016f6822022-12-09 14:17:50 +01001887 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001888 }
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001889
Gilles Peskine449bd832023-01-11 14:50:10 +01001890 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Neil Armstrongca7d5062022-05-31 14:43:23 +02001891 psa_role = PSA_PAKE_ROLE_SERVER;
Gilles Peskine449bd832023-01-11 14:50:10 +01001892 } else {
Neil Armstrongca7d5062022-05-31 14:43:23 +02001893 psa_role = PSA_PAKE_ROLE_CLIENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001894 }
Neil Armstrongca7d5062022-05-31 14:43:23 +02001895
Gilles Peskine449bd832023-01-11 14:50:10 +01001896 status = psa_pake_set_role(&ssl->handshake->psa_pake_ctx, psa_role);
1897 if (status != PSA_SUCCESS) {
Valerio Setti016f6822022-12-09 14:17:50 +01001898 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001899 }
Valerio Setti016f6822022-12-09 14:17:50 +01001900
Gilles Peskine449bd832023-01-11 14:50:10 +01001901 status = psa_pake_set_password_key(&ssl->handshake->psa_pake_ctx, pwd);
1902 if (status != PSA_SUCCESS) {
Valerio Setti016f6822022-12-09 14:17:50 +01001903 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001904 }
Valerio Setti016f6822022-12-09 14:17:50 +01001905
1906 ssl->handshake->psa_pake_ctx_is_ok = 1;
1907
Gilles Peskine449bd832023-01-11 14:50:10 +01001908 return PSA_SUCCESS;
Valerio Setti016f6822022-12-09 14:17:50 +01001909}
1910
Gilles Peskine449bd832023-01-11 14:50:10 +01001911int mbedtls_ssl_set_hs_ecjpake_password(mbedtls_ssl_context *ssl,
1912 const unsigned char *pw,
1913 size_t pw_len)
Valerio Setti016f6822022-12-09 14:17:50 +01001914{
1915 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1916 psa_status_t status;
1917
Gilles Peskine449bd832023-01-11 14:50:10 +01001918 if (ssl->handshake == NULL || ssl->conf == NULL) {
1919 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Neil Armstrongca7d5062022-05-31 14:43:23 +02001920 }
1921
Gilles Peskine449bd832023-01-11 14:50:10 +01001922 /* Empty password is not valid */
1923 if ((pw == NULL) || (pw_len == 0)) {
1924 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
1925 }
1926
1927 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
1928 psa_set_key_algorithm(&attributes, PSA_ALG_JPAKE);
1929 psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD);
1930
1931 status = psa_import_key(&attributes, pw, pw_len,
1932 &ssl->handshake->psa_pake_password);
1933 if (status != PSA_SUCCESS) {
1934 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
1935 }
1936
1937 status = mbedtls_ssl_set_hs_ecjpake_password_common(ssl,
1938 ssl->handshake->psa_pake_password);
1939 if (status != PSA_SUCCESS) {
1940 psa_destroy_key(ssl->handshake->psa_pake_password);
1941 psa_pake_abort(&ssl->handshake->psa_pake_ctx);
1942 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
1943 }
1944
1945 return 0;
Valerio Setti02c25b52022-11-15 14:08:42 +01001946}
Valerio Settia9a97dc2022-11-28 18:26:16 +01001947
Gilles Peskine449bd832023-01-11 14:50:10 +01001948int mbedtls_ssl_set_hs_ecjpake_password_opaque(mbedtls_ssl_context *ssl,
1949 mbedtls_svc_key_id_t pwd)
Valerio Settia9a97dc2022-11-28 18:26:16 +01001950{
Valerio Settia9a97dc2022-11-28 18:26:16 +01001951 psa_status_t status;
1952
Gilles Peskine449bd832023-01-11 14:50:10 +01001953 if (ssl->handshake == NULL || ssl->conf == NULL) {
1954 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Neil Armstrongca7d5062022-05-31 14:43:23 +02001955 }
1956
Gilles Peskine449bd832023-01-11 14:50:10 +01001957 if (mbedtls_svc_key_id_is_null(pwd)) {
1958 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
1959 }
1960
1961 status = mbedtls_ssl_set_hs_ecjpake_password_common(ssl, pwd);
1962 if (status != PSA_SUCCESS) {
1963 psa_pake_abort(&ssl->handshake->psa_pake_ctx);
1964 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
1965 }
1966
1967 return 0;
Valerio Setti02c25b52022-11-15 14:08:42 +01001968}
1969#else /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01001970int mbedtls_ssl_set_hs_ecjpake_password(mbedtls_ssl_context *ssl,
1971 const unsigned char *pw,
1972 size_t pw_len)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001973{
1974 mbedtls_ecjpake_role role;
1975
Gilles Peskine449bd832023-01-11 14:50:10 +01001976 if (ssl->handshake == NULL || ssl->conf == NULL) {
1977 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
1978 }
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001979
Valerio Settic689ed82022-12-07 14:40:38 +01001980 /* Empty password is not valid */
Gilles Peskine449bd832023-01-11 14:50:10 +01001981 if ((pw == NULL) || (pw_len == 0)) {
1982 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
1983 }
Valerio Settic689ed82022-12-07 14:40:38 +01001984
Gilles Peskine449bd832023-01-11 14:50:10 +01001985 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001986 role = MBEDTLS_ECJPAKE_SERVER;
Gilles Peskine449bd832023-01-11 14:50:10 +01001987 } else {
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001988 role = MBEDTLS_ECJPAKE_CLIENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001989 }
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001990
Gilles Peskine449bd832023-01-11 14:50:10 +01001991 return mbedtls_ecjpake_setup(&ssl->handshake->ecjpake_ctx,
1992 role,
1993 MBEDTLS_MD_SHA256,
1994 MBEDTLS_ECP_DP_SECP256R1,
1995 pw, pw_len);
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001996}
Valerio Setti02c25b52022-11-15 14:08:42 +01001997#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02001998#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001999
Ronald Cron73fe8df2022-10-05 14:31:43 +02002000#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002001int mbedtls_ssl_conf_has_static_psk(mbedtls_ssl_config const *conf)
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002002{
Gilles Peskine449bd832023-01-11 14:50:10 +01002003 if (conf->psk_identity == NULL ||
2004 conf->psk_identity_len == 0) {
2005 return 0;
Ronald Crond29e13e2022-10-19 10:33:48 +02002006 }
2007
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002008#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002009 if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) {
2010 return 1;
2011 }
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002012#endif /* MBEDTLS_USE_PSA_CRYPTO */
Ronald Crond29e13e2022-10-19 10:33:48 +02002013
Gilles Peskine449bd832023-01-11 14:50:10 +01002014 if (conf->psk != NULL && conf->psk_len != 0) {
2015 return 1;
2016 }
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002017
Gilles Peskine449bd832023-01-11 14:50:10 +01002018 return 0;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002019}
2020
Gilles Peskine449bd832023-01-11 14:50:10 +01002021static void ssl_conf_remove_psk(mbedtls_ssl_config *conf)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002022{
2023 /* Remove reference to existing PSK, if any. */
2024#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002025 if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) {
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002026 /* The maintenance of the PSK key slot is the
2027 * user's responsibility. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002028 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002029 }
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002030#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01002031 if (conf->psk != NULL) {
2032 mbedtls_platform_zeroize(conf->psk, conf->psk_len);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002033
Gilles Peskine449bd832023-01-11 14:50:10 +01002034 mbedtls_free(conf->psk);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002035 conf->psk = NULL;
2036 conf->psk_len = 0;
2037 }
2038
2039 /* Remove reference to PSK identity, if any. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002040 if (conf->psk_identity != NULL) {
2041 mbedtls_free(conf->psk_identity);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002042 conf->psk_identity = NULL;
2043 conf->psk_identity_len = 0;
2044 }
2045}
2046
Hanno Becker7390c712018-11-15 13:33:04 +00002047/* This function assumes that PSK identity in the SSL config is unset.
2048 * It checks that the provided identity is well-formed and attempts
2049 * to make a copy of it in the SSL config.
2050 * On failure, the PSK identity in the config remains unset. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002051MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002052static int ssl_conf_set_psk_identity(mbedtls_ssl_config *conf,
2053 unsigned char const *psk_identity,
2054 size_t psk_identity_len)
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002055{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02002056 /* Identity len will be encoded on two bytes */
Gilles Peskine449bd832023-01-11 14:50:10 +01002057 if (psk_identity == NULL ||
Ronald Cron2a87e9b2022-10-19 10:55:26 +02002058 psk_identity_len == 0 ||
Gilles Peskine449bd832023-01-11 14:50:10 +01002059 (psk_identity_len >> 16) != 0 ||
2060 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN) {
2061 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02002062 }
2063
Gilles Peskine449bd832023-01-11 14:50:10 +01002064 conf->psk_identity = mbedtls_calloc(1, psk_identity_len);
2065 if (conf->psk_identity == NULL) {
2066 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2067 }
Paul Bakker6db455e2013-09-18 17:29:31 +02002068
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01002069 conf->psk_identity_len = psk_identity_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002070 memcpy(conf->psk_identity, psk_identity, conf->psk_identity_len);
Paul Bakker5ad403f2013-09-18 21:21:30 +02002071
Gilles Peskine449bd832023-01-11 14:50:10 +01002072 return 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02002073}
2074
Gilles Peskine449bd832023-01-11 14:50:10 +01002075int mbedtls_ssl_conf_psk(mbedtls_ssl_config *conf,
2076 const unsigned char *psk, size_t psk_len,
2077 const unsigned char *psk_identity, size_t psk_identity_len)
Hanno Becker7390c712018-11-15 13:33:04 +00002078{
Janos Follath865b3eb2019-12-16 11:46:15 +00002079 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002080
2081 /* We currently only support one PSK, raw or opaque. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002082 if (mbedtls_ssl_conf_has_static_psk(conf)) {
2083 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
2084 }
Hanno Becker7390c712018-11-15 13:33:04 +00002085
2086 /* Check and set raw PSK */
Gilles Peskine449bd832023-01-11 14:50:10 +01002087 if (psk == NULL) {
2088 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2089 }
2090 if (psk_len == 0) {
2091 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2092 }
2093 if (psk_len > MBEDTLS_PSK_MAX_LEN) {
2094 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2095 }
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01002096
Gilles Peskine449bd832023-01-11 14:50:10 +01002097 if ((conf->psk = mbedtls_calloc(1, psk_len)) == NULL) {
2098 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2099 }
Hanno Becker7390c712018-11-15 13:33:04 +00002100 conf->psk_len = psk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002101 memcpy(conf->psk, psk, conf->psk_len);
Hanno Becker7390c712018-11-15 13:33:04 +00002102
2103 /* Check and set PSK Identity */
Gilles Peskine449bd832023-01-11 14:50:10 +01002104 ret = ssl_conf_set_psk_identity(conf, psk_identity, psk_identity_len);
2105 if (ret != 0) {
2106 ssl_conf_remove_psk(conf);
2107 }
Hanno Becker7390c712018-11-15 13:33:04 +00002108
Gilles Peskine449bd832023-01-11 14:50:10 +01002109 return ret;
Hanno Becker7390c712018-11-15 13:33:04 +00002110}
2111
Gilles Peskine449bd832023-01-11 14:50:10 +01002112static void ssl_remove_psk(mbedtls_ssl_context *ssl)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002113{
2114#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002115 if (!mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque)) {
Neil Armstrong501c9322022-05-03 09:35:09 +02002116 /* The maintenance of the external PSK key slot is the
2117 * user's responsibility. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002118 if (ssl->handshake->psk_opaque_is_internal) {
2119 psa_destroy_key(ssl->handshake->psk_opaque);
Neil Armstrong501c9322022-05-03 09:35:09 +02002120 ssl->handshake->psk_opaque_is_internal = 0;
2121 }
Ronald Croncf56a0a2020-08-04 09:51:30 +02002122 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002123 }
Neil Armstronge952a302022-05-03 10:22:14 +02002124#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002125 if (ssl->handshake->psk != NULL) {
2126 mbedtls_platform_zeroize(ssl->handshake->psk,
2127 ssl->handshake->psk_len);
2128 mbedtls_free(ssl->handshake->psk);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002129 ssl->handshake->psk_len = 0;
2130 }
Neil Armstronge952a302022-05-03 10:22:14 +02002131#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002132}
2133
Gilles Peskine449bd832023-01-11 14:50:10 +01002134int mbedtls_ssl_set_hs_psk(mbedtls_ssl_context *ssl,
2135 const unsigned char *psk, size_t psk_len)
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002136{
Neil Armstrong501c9322022-05-03 09:35:09 +02002137#if defined(MBEDTLS_USE_PSA_CRYPTO)
2138 psa_key_attributes_t key_attributes = psa_key_attributes_init();
Jerry Yu5d01c052022-08-17 10:18:10 +08002139 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jerry Yu24b8c812022-08-20 19:06:56 +08002140 psa_algorithm_t alg = PSA_ALG_NONE;
Jerry Yu5d01c052022-08-17 10:18:10 +08002141 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrong501c9322022-05-03 09:35:09 +02002142#endif /* MBEDTLS_USE_PSA_CRYPTO */
2143
Gilles Peskine449bd832023-01-11 14:50:10 +01002144 if (psk == NULL || ssl->handshake == NULL) {
2145 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2146 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002147
Gilles Peskine449bd832023-01-11 14:50:10 +01002148 if (psk_len > MBEDTLS_PSK_MAX_LEN) {
2149 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2150 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002151
Gilles Peskine449bd832023-01-11 14:50:10 +01002152 ssl_remove_psk(ssl);
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002153
Neil Armstrong501c9322022-05-03 09:35:09 +02002154#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yuccc68a42022-07-26 16:39:20 +08002155#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01002156 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2) {
2157 if (ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384) {
2158 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
2159 } else {
2160 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
2161 }
2162 psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE);
Jerry Yuccc68a42022-07-26 16:39:20 +08002163 }
2164#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Neil Armstrong501c9322022-05-03 09:35:09 +02002165
Jerry Yu568ec252022-07-22 21:27:34 +08002166#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01002167 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
2168 alg = PSA_ALG_HKDF_EXTRACT(PSA_ALG_ANY_HASH);
2169 psa_set_key_usage_flags(&key_attributes,
2170 PSA_KEY_USAGE_DERIVE | PSA_KEY_USAGE_EXPORT);
Jerry Yuccc68a42022-07-26 16:39:20 +08002171 }
2172#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
2173
Gilles Peskine449bd832023-01-11 14:50:10 +01002174 psa_set_key_algorithm(&key_attributes, alg);
2175 psa_set_key_type(&key_attributes, PSA_KEY_TYPE_DERIVE);
Neil Armstrong501c9322022-05-03 09:35:09 +02002176
Gilles Peskine449bd832023-01-11 14:50:10 +01002177 status = psa_import_key(&key_attributes, psk, psk_len, &key);
2178 if (status != PSA_SUCCESS) {
2179 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2180 }
Neil Armstrong501c9322022-05-03 09:35:09 +02002181
2182 /* Allow calling psa_destroy_key() on psk remove */
2183 ssl->handshake->psk_opaque_is_internal = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01002184 return mbedtls_ssl_set_hs_psk_opaque(ssl, key);
Neil Armstrong501c9322022-05-03 09:35:09 +02002185#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002186 if ((ssl->handshake->psk = mbedtls_calloc(1, psk_len)) == NULL) {
2187 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2188 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002189
2190 ssl->handshake->psk_len = psk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002191 memcpy(ssl->handshake->psk, psk, ssl->handshake->psk_len);
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002192
Gilles Peskine449bd832023-01-11 14:50:10 +01002193 return 0;
Neil Armstrong501c9322022-05-03 09:35:09 +02002194#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002195}
2196
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002197#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002198int mbedtls_ssl_conf_psk_opaque(mbedtls_ssl_config *conf,
2199 mbedtls_svc_key_id_t psk,
2200 const unsigned char *psk_identity,
2201 size_t psk_identity_len)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002202{
Janos Follath865b3eb2019-12-16 11:46:15 +00002203 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002204
2205 /* We currently only support one PSK, raw or opaque. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002206 if (mbedtls_ssl_conf_has_static_psk(conf)) {
2207 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
2208 }
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002209
Hanno Becker7390c712018-11-15 13:33:04 +00002210 /* Check and set opaque PSK */
Gilles Peskine449bd832023-01-11 14:50:10 +01002211 if (mbedtls_svc_key_id_is_null(psk)) {
2212 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2213 }
Ronald Croncf56a0a2020-08-04 09:51:30 +02002214 conf->psk_opaque = psk;
Hanno Becker7390c712018-11-15 13:33:04 +00002215
2216 /* Check and set PSK Identity */
Gilles Peskine449bd832023-01-11 14:50:10 +01002217 ret = ssl_conf_set_psk_identity(conf, psk_identity,
2218 psk_identity_len);
2219 if (ret != 0) {
2220 ssl_conf_remove_psk(conf);
2221 }
Hanno Becker7390c712018-11-15 13:33:04 +00002222
Gilles Peskine449bd832023-01-11 14:50:10 +01002223 return ret;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002224}
2225
Gilles Peskine449bd832023-01-11 14:50:10 +01002226int mbedtls_ssl_set_hs_psk_opaque(mbedtls_ssl_context *ssl,
2227 mbedtls_svc_key_id_t psk)
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002228{
Gilles Peskine449bd832023-01-11 14:50:10 +01002229 if ((mbedtls_svc_key_id_is_null(psk)) ||
2230 (ssl->handshake == NULL)) {
2231 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2232 }
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002233
Gilles Peskine449bd832023-01-11 14:50:10 +01002234 ssl_remove_psk(ssl);
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002235 ssl->handshake->psk_opaque = psk;
Gilles Peskine449bd832023-01-11 14:50:10 +01002236 return 0;
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002237}
2238#endif /* MBEDTLS_USE_PSA_CRYPTO */
2239
Jerry Yu8897c072022-08-12 13:56:53 +08002240#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002241void mbedtls_ssl_conf_psk_cb(mbedtls_ssl_config *conf,
2242 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
2243 size_t),
2244 void *p_psk)
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002245{
2246 conf->f_psk = f_psk;
2247 conf->p_psk = p_psk;
2248}
Jerry Yu8897c072022-08-12 13:56:53 +08002249#endif /* MBEDTLS_SSL_SRV_C */
2250
Ronald Cron73fe8df2022-10-05 14:31:43 +02002251#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002252
2253#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine301711e2022-04-26 16:57:05 +02002254static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
Gilles Peskine449bd832023-01-11 14:50:10 +01002255 psa_algorithm_t alg)
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002256{
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002257#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01002258 if (alg == PSA_ALG_CBC_NO_PADDING) {
2259 return MBEDTLS_SSL_MODE_CBC;
2260 }
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002261#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
Gilles Peskine449bd832023-01-11 14:50:10 +01002262 if (PSA_ALG_IS_AEAD(alg)) {
2263 return MBEDTLS_SSL_MODE_AEAD;
2264 }
2265 return MBEDTLS_SSL_MODE_STREAM;
Gilles Peskine301711e2022-04-26 16:57:05 +02002266}
2267
2268#else /* MBEDTLS_USE_PSA_CRYPTO */
2269
2270static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
Gilles Peskine449bd832023-01-11 14:50:10 +01002271 mbedtls_cipher_mode_t mode)
Gilles Peskine301711e2022-04-26 16:57:05 +02002272{
2273#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01002274 if (mode == MBEDTLS_MODE_CBC) {
2275 return MBEDTLS_SSL_MODE_CBC;
2276 }
Gilles Peskine301711e2022-04-26 16:57:05 +02002277#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
2278
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002279#if defined(MBEDTLS_GCM_C) || \
2280 defined(MBEDTLS_CCM_C) || \
2281 defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002282 if (mode == MBEDTLS_MODE_GCM ||
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002283 mode == MBEDTLS_MODE_CCM ||
Gilles Peskine449bd832023-01-11 14:50:10 +01002284 mode == MBEDTLS_MODE_CHACHAPOLY) {
2285 return MBEDTLS_SSL_MODE_AEAD;
2286 }
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002287#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002288
Gilles Peskine449bd832023-01-11 14:50:10 +01002289 return MBEDTLS_SSL_MODE_STREAM;
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002290}
Gilles Peskine301711e2022-04-26 16:57:05 +02002291#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002292
Gilles Peskinee108d982022-04-26 16:50:40 +02002293static mbedtls_ssl_mode_t mbedtls_ssl_get_actual_mode(
2294 mbedtls_ssl_mode_t base_mode,
Gilles Peskine449bd832023-01-11 14:50:10 +01002295 int encrypt_then_mac)
Gilles Peskinee108d982022-04-26 16:50:40 +02002296{
2297#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01002298 if (encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED &&
2299 base_mode == MBEDTLS_SSL_MODE_CBC) {
2300 return MBEDTLS_SSL_MODE_CBC_ETM;
Gilles Peskinee108d982022-04-26 16:50:40 +02002301 }
2302#else
2303 (void) encrypt_then_mac;
2304#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002305 return base_mode;
Gilles Peskinee108d982022-04-26 16:50:40 +02002306}
2307
Neil Armstrongab555e02022-04-04 11:07:59 +02002308mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_transform(
Gilles Peskine449bd832023-01-11 14:50:10 +01002309 const mbedtls_ssl_transform *transform)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002310{
Gilles Peskinee108d982022-04-26 16:50:40 +02002311 mbedtls_ssl_mode_t base_mode = mbedtls_ssl_get_base_mode(
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002312#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002313 transform->psa_alg
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002314#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002315 mbedtls_cipher_get_cipher_mode(&transform->cipher_ctx_enc)
Gilles Peskinee108d982022-04-26 16:50:40 +02002316#endif
2317 );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002318
Gilles Peskinee108d982022-04-26 16:50:40 +02002319 int encrypt_then_mac = 0;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002320#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskinee108d982022-04-26 16:50:40 +02002321 encrypt_then_mac = transform->encrypt_then_mac;
2322#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002323 return mbedtls_ssl_get_actual_mode(base_mode, encrypt_then_mac);
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002324}
2325
Neil Armstrongab555e02022-04-04 11:07:59 +02002326mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002327#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01002328 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002329#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01002330 const mbedtls_ssl_ciphersuite_t *suite)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002331{
Gilles Peskinee108d982022-04-26 16:50:40 +02002332 mbedtls_ssl_mode_t base_mode = MBEDTLS_SSL_MODE_STREAM;
2333
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002334#if defined(MBEDTLS_USE_PSA_CRYPTO)
2335 psa_status_t status;
2336 psa_algorithm_t alg;
2337 psa_key_type_t type;
2338 size_t size;
Gilles Peskine449bd832023-01-11 14:50:10 +01002339 status = mbedtls_ssl_cipher_to_psa(suite->cipher, 0, &alg, &type, &size);
2340 if (status == PSA_SUCCESS) {
2341 base_mode = mbedtls_ssl_get_base_mode(alg);
2342 }
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002343#else
2344 const mbedtls_cipher_info_t *cipher =
Gilles Peskine449bd832023-01-11 14:50:10 +01002345 mbedtls_cipher_info_from_type(suite->cipher);
2346 if (cipher != NULL) {
Gilles Peskinee108d982022-04-26 16:50:40 +02002347 base_mode =
2348 mbedtls_ssl_get_base_mode(
Gilles Peskine449bd832023-01-11 14:50:10 +01002349 mbedtls_cipher_info_get_mode(cipher));
Gilles Peskinee108d982022-04-26 16:50:40 +02002350 }
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002351#endif /* MBEDTLS_USE_PSA_CRYPTO */
2352
Gilles Peskinee108d982022-04-26 16:50:40 +02002353#if !defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
2354 int encrypt_then_mac = 0;
2355#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002356 return mbedtls_ssl_get_actual_mode(base_mode, encrypt_then_mac);
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002357}
2358
Neil Armstrong8395d7a2022-05-18 11:44:56 +02002359#if defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu251a12e2022-07-13 15:15:48 +08002360
2361#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu438ddd82022-07-07 06:55:50 +00002362/* Serialization of TLS 1.3 sessions:
2363 *
2364 * struct {
Xiaokang Qian126bf8e2022-10-13 02:22:40 +00002365 * opaque hostname<0..2^16-1>;
Jerry Yu438ddd82022-07-07 06:55:50 +00002366 * uint64 ticket_received;
2367 * uint32 ticket_lifetime;
Jerry Yu34191072022-08-18 10:32:09 +08002368 * opaque ticket<1..2^16-1>;
Jerry Yu438ddd82022-07-07 06:55:50 +00002369 * } ClientOnlyData;
2370 *
2371 * struct {
2372 * uint8 endpoint;
2373 * uint8 ciphersuite[2];
2374 * uint32 ticket_age_add;
2375 * uint8 ticket_flags;
2376 * opaque resumption_key<0..255>;
2377 * select ( endpoint ) {
2378 * case client: ClientOnlyData;
2379 * case server: uint64 start_time;
2380 * };
2381 * } serialized_session_tls13;
2382 *
2383 */
2384#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yue36fdd62022-08-17 21:31:36 +08002385MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002386static int ssl_tls13_session_save(const mbedtls_ssl_session *session,
2387 unsigned char *buf,
2388 size_t buf_len,
2389 size_t *olen)
Jerry Yu438ddd82022-07-07 06:55:50 +00002390{
2391 unsigned char *p = buf;
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00002392#if defined(MBEDTLS_SSL_CLI_C) && \
2393 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01002394 size_t hostname_len = (session->hostname == NULL) ?
2395 0 : strlen(session->hostname) + 1;
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00002396#endif
Jerry Yubc7c1a42022-07-21 22:57:37 +08002397 size_t needed = 1 /* endpoint */
2398 + 2 /* ciphersuite */
2399 + 4 /* ticket_age_add */
Jerry Yu34191072022-08-18 10:32:09 +08002400 + 1 /* ticket_flags */
2401 + 1; /* resumption_key length */
Jerry Yue36fdd62022-08-17 21:31:36 +08002402 *olen = 0;
Jerry Yu34191072022-08-18 10:32:09 +08002403
Gilles Peskine449bd832023-01-11 14:50:10 +01002404 if (session->resumption_key_len > MBEDTLS_SSL_TLS1_3_TICKET_RESUMPTION_KEY_LEN) {
2405 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2406 }
Jerry Yu34191072022-08-18 10:32:09 +08002407 needed += session->resumption_key_len; /* resumption_key */
2408
Jerry Yu438ddd82022-07-07 06:55:50 +00002409#if defined(MBEDTLS_HAVE_TIME)
2410 needed += 8; /* start_time or ticket_received */
2411#endif
2412
2413#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002414 if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Xiaokang Qian126bf8e2022-10-13 02:22:40 +00002415#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaokang Qianbc663a02022-10-09 11:14:39 +00002416 needed += 2 /* hostname_len */
Gilles Peskine449bd832023-01-11 14:50:10 +01002417 + hostname_len; /* hostname */
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00002418#endif
2419
Jerry Yu438ddd82022-07-07 06:55:50 +00002420 needed += 4 /* ticket_lifetime */
Jerry Yue36fdd62022-08-17 21:31:36 +08002421 + 2; /* ticket_len */
Jerry Yu34191072022-08-18 10:32:09 +08002422
2423 /* Check size_t overflow */
Gilles Peskine449bd832023-01-11 14:50:10 +01002424 if (session->ticket_len > SIZE_MAX - needed) {
2425 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2426 }
Jerry Yu34191072022-08-18 10:32:09 +08002427
Jerry Yue28d9742022-08-18 15:44:03 +08002428 needed += session->ticket_len; /* ticket */
Jerry Yu438ddd82022-07-07 06:55:50 +00002429 }
2430#endif /* MBEDTLS_SSL_CLI_C */
2431
Jerry Yue36fdd62022-08-17 21:31:36 +08002432 *olen = needed;
Gilles Peskine449bd832023-01-11 14:50:10 +01002433 if (needed > buf_len) {
2434 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
2435 }
Jerry Yu438ddd82022-07-07 06:55:50 +00002436
2437 p[0] = session->endpoint;
Gilles Peskine449bd832023-01-11 14:50:10 +01002438 MBEDTLS_PUT_UINT16_BE(session->ciphersuite, p, 1);
2439 MBEDTLS_PUT_UINT32_BE(session->ticket_age_add, p, 3);
Jerry Yu438ddd82022-07-07 06:55:50 +00002440 p[7] = session->ticket_flags;
2441
2442 /* save resumption_key */
Jerry Yubc7c1a42022-07-21 22:57:37 +08002443 p[8] = session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002444 p += 9;
Gilles Peskine449bd832023-01-11 14:50:10 +01002445 memcpy(p, session->resumption_key, session->resumption_key_len);
Jerry Yubc7c1a42022-07-21 22:57:37 +08002446 p += session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002447
2448#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002449 if (session->endpoint == MBEDTLS_SSL_IS_SERVER) {
2450 MBEDTLS_PUT_UINT64_BE((uint64_t) session->start, p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002451 p += 8;
2452 }
2453#endif /* MBEDTLS_HAVE_TIME */
2454
2455#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002456 if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002457#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01002458 MBEDTLS_PUT_UINT16_BE(hostname_len, p, 0);
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002459 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002460 if (hostname_len > 0) {
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002461 /* save host name */
Gilles Peskine449bd832023-01-11 14:50:10 +01002462 memcpy(p, session->hostname, hostname_len);
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002463 p += hostname_len;
2464 }
2465#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
2466
Jerry Yu438ddd82022-07-07 06:55:50 +00002467#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01002468 MBEDTLS_PUT_UINT64_BE((uint64_t) session->ticket_received, p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002469 p += 8;
2470#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002471 MBEDTLS_PUT_UINT32_BE(session->ticket_lifetime, p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002472 p += 4;
2473
Gilles Peskine449bd832023-01-11 14:50:10 +01002474 MBEDTLS_PUT_UINT16_BE(session->ticket_len, p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002475 p += 2;
Jerry Yu34191072022-08-18 10:32:09 +08002476
Gilles Peskine449bd832023-01-11 14:50:10 +01002477 if (session->ticket != NULL && session->ticket_len > 0) {
2478 memcpy(p, session->ticket, session->ticket_len);
Jerry Yu438ddd82022-07-07 06:55:50 +00002479 p += session->ticket_len;
2480 }
2481 }
2482#endif /* MBEDTLS_SSL_CLI_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01002483 return 0;
Jerry Yu438ddd82022-07-07 06:55:50 +00002484}
2485
2486MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002487static int ssl_tls13_session_load(mbedtls_ssl_session *session,
2488 const unsigned char *buf,
2489 size_t len)
Jerry Yu438ddd82022-07-07 06:55:50 +00002490{
2491 const unsigned char *p = buf;
2492 const unsigned char *end = buf + len;
2493
Gilles Peskine449bd832023-01-11 14:50:10 +01002494 if (end - p < 9) {
2495 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2496 }
Jerry Yu438ddd82022-07-07 06:55:50 +00002497 session->endpoint = p[0];
Gilles Peskine449bd832023-01-11 14:50:10 +01002498 session->ciphersuite = MBEDTLS_GET_UINT16_BE(p, 1);
2499 session->ticket_age_add = MBEDTLS_GET_UINT32_BE(p, 3);
Jerry Yu438ddd82022-07-07 06:55:50 +00002500 session->ticket_flags = p[7];
2501
2502 /* load resumption_key */
Jerry Yubc7c1a42022-07-21 22:57:37 +08002503 session->resumption_key_len = p[8];
Jerry Yu438ddd82022-07-07 06:55:50 +00002504 p += 9;
2505
Gilles Peskine449bd832023-01-11 14:50:10 +01002506 if (end - p < session->resumption_key_len) {
2507 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2508 }
Jerry Yu438ddd82022-07-07 06:55:50 +00002509
Gilles Peskine449bd832023-01-11 14:50:10 +01002510 if (sizeof(session->resumption_key) < session->resumption_key_len) {
2511 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2512 }
2513 memcpy(session->resumption_key, p, session->resumption_key_len);
Jerry Yubc7c1a42022-07-21 22:57:37 +08002514 p += session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002515
2516#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002517 if (session->endpoint == MBEDTLS_SSL_IS_SERVER) {
2518 if (end - p < 8) {
2519 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2520 }
2521 session->start = MBEDTLS_GET_UINT64_BE(p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002522 p += 8;
2523 }
2524#endif /* MBEDTLS_HAVE_TIME */
2525
2526#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002527 if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002528#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01002529 defined(MBEDTLS_SSL_SESSION_TICKETS)
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002530 size_t hostname_len;
2531 /* load host name */
Gilles Peskine449bd832023-01-11 14:50:10 +01002532 if (end - p < 2) {
2533 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2534 }
2535 hostname_len = MBEDTLS_GET_UINT16_BE(p, 0);
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002536 p += 2;
2537
Gilles Peskine449bd832023-01-11 14:50:10 +01002538 if (end - p < (long int) hostname_len) {
2539 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2540 }
2541 if (hostname_len > 0) {
2542 session->hostname = mbedtls_calloc(1, hostname_len);
2543 if (session->hostname == NULL) {
2544 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2545 }
2546 memcpy(session->hostname, p, hostname_len);
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002547 p += hostname_len;
2548 }
2549#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION &&
2550 MBEDTLS_SSL_SESSION_TICKETS */
2551
Jerry Yu438ddd82022-07-07 06:55:50 +00002552#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01002553 if (end - p < 8) {
2554 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2555 }
2556 session->ticket_received = MBEDTLS_GET_UINT64_BE(p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002557 p += 8;
2558#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002559 if (end - p < 4) {
2560 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2561 }
2562 session->ticket_lifetime = MBEDTLS_GET_UINT32_BE(p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002563 p += 4;
2564
Gilles Peskine449bd832023-01-11 14:50:10 +01002565 if (end - p < 2) {
2566 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2567 }
2568 session->ticket_len = MBEDTLS_GET_UINT16_BE(p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002569 p += 2;
2570
Gilles Peskine449bd832023-01-11 14:50:10 +01002571 if (end - p < (long int) session->ticket_len) {
2572 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2573 }
2574 if (session->ticket_len > 0) {
2575 session->ticket = mbedtls_calloc(1, session->ticket_len);
2576 if (session->ticket == NULL) {
2577 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2578 }
2579 memcpy(session->ticket, p, session->ticket_len);
Jerry Yu438ddd82022-07-07 06:55:50 +00002580 p += session->ticket_len;
2581 }
2582 }
2583#endif /* MBEDTLS_SSL_CLI_C */
2584
Gilles Peskine449bd832023-01-11 14:50:10 +01002585 return 0;
Jerry Yu438ddd82022-07-07 06:55:50 +00002586
2587}
2588#else /* MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yue36fdd62022-08-17 21:31:36 +08002589MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002590static int ssl_tls13_session_save(const mbedtls_ssl_session *session,
2591 unsigned char *buf,
2592 size_t buf_len,
2593 size_t *olen)
Jerry Yu251a12e2022-07-13 15:15:48 +08002594{
2595 ((void) session);
2596 ((void) buf);
2597 ((void) buf_len);
Jerry Yue36fdd62022-08-17 21:31:36 +08002598 *olen = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002599 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Jerry Yu251a12e2022-07-13 15:15:48 +08002600}
Jerry Yu438ddd82022-07-07 06:55:50 +00002601
Gilles Peskine449bd832023-01-11 14:50:10 +01002602static int ssl_tls13_session_load(const mbedtls_ssl_session *session,
2603 unsigned char *buf,
2604 size_t buf_len)
Jerry Yu438ddd82022-07-07 06:55:50 +00002605{
2606 ((void) session);
2607 ((void) buf);
2608 ((void) buf_len);
Gilles Peskine449bd832023-01-11 14:50:10 +01002609 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Jerry Yu438ddd82022-07-07 06:55:50 +00002610}
2611#endif /* !MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yu251a12e2022-07-13 15:15:48 +08002612#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
2613
Gilles Peskine449bd832023-01-11 14:50:10 +01002614psa_status_t mbedtls_ssl_cipher_to_psa(mbedtls_cipher_type_t mbedtls_cipher_type,
2615 size_t taglen,
2616 psa_algorithm_t *alg,
2617 psa_key_type_t *key_type,
2618 size_t *key_size)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002619{
Gilles Peskine449bd832023-01-11 14:50:10 +01002620 switch (mbedtls_cipher_type) {
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002621 case MBEDTLS_CIPHER_AES_128_CBC:
2622 *alg = PSA_ALG_CBC_NO_PADDING;
2623 *key_type = PSA_KEY_TYPE_AES;
2624 *key_size = 128;
2625 break;
2626 case MBEDTLS_CIPHER_AES_128_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002627 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002628 *key_type = PSA_KEY_TYPE_AES;
2629 *key_size = 128;
2630 break;
2631 case MBEDTLS_CIPHER_AES_128_GCM:
2632 *alg = PSA_ALG_GCM;
2633 *key_type = PSA_KEY_TYPE_AES;
2634 *key_size = 128;
2635 break;
2636 case MBEDTLS_CIPHER_AES_192_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002637 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002638 *key_type = PSA_KEY_TYPE_AES;
2639 *key_size = 192;
2640 break;
2641 case MBEDTLS_CIPHER_AES_192_GCM:
2642 *alg = PSA_ALG_GCM;
2643 *key_type = PSA_KEY_TYPE_AES;
2644 *key_size = 192;
2645 break;
2646 case MBEDTLS_CIPHER_AES_256_CBC:
2647 *alg = PSA_ALG_CBC_NO_PADDING;
2648 *key_type = PSA_KEY_TYPE_AES;
2649 *key_size = 256;
2650 break;
2651 case MBEDTLS_CIPHER_AES_256_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002652 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002653 *key_type = PSA_KEY_TYPE_AES;
2654 *key_size = 256;
2655 break;
2656 case MBEDTLS_CIPHER_AES_256_GCM:
2657 *alg = PSA_ALG_GCM;
2658 *key_type = PSA_KEY_TYPE_AES;
2659 *key_size = 256;
2660 break;
2661 case MBEDTLS_CIPHER_ARIA_128_CBC:
2662 *alg = PSA_ALG_CBC_NO_PADDING;
2663 *key_type = PSA_KEY_TYPE_ARIA;
2664 *key_size = 128;
2665 break;
2666 case MBEDTLS_CIPHER_ARIA_128_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002667 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002668 *key_type = PSA_KEY_TYPE_ARIA;
2669 *key_size = 128;
2670 break;
2671 case MBEDTLS_CIPHER_ARIA_128_GCM:
2672 *alg = PSA_ALG_GCM;
2673 *key_type = PSA_KEY_TYPE_ARIA;
2674 *key_size = 128;
2675 break;
2676 case MBEDTLS_CIPHER_ARIA_192_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002677 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002678 *key_type = PSA_KEY_TYPE_ARIA;
2679 *key_size = 192;
2680 break;
2681 case MBEDTLS_CIPHER_ARIA_192_GCM:
2682 *alg = PSA_ALG_GCM;
2683 *key_type = PSA_KEY_TYPE_ARIA;
2684 *key_size = 192;
2685 break;
2686 case MBEDTLS_CIPHER_ARIA_256_CBC:
2687 *alg = PSA_ALG_CBC_NO_PADDING;
2688 *key_type = PSA_KEY_TYPE_ARIA;
2689 *key_size = 256;
2690 break;
2691 case MBEDTLS_CIPHER_ARIA_256_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002692 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002693 *key_type = PSA_KEY_TYPE_ARIA;
2694 *key_size = 256;
2695 break;
2696 case MBEDTLS_CIPHER_ARIA_256_GCM:
2697 *alg = PSA_ALG_GCM;
2698 *key_type = PSA_KEY_TYPE_ARIA;
2699 *key_size = 256;
2700 break;
2701 case MBEDTLS_CIPHER_CAMELLIA_128_CBC:
2702 *alg = PSA_ALG_CBC_NO_PADDING;
2703 *key_type = PSA_KEY_TYPE_CAMELLIA;
2704 *key_size = 128;
2705 break;
2706 case MBEDTLS_CIPHER_CAMELLIA_128_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002707 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002708 *key_type = PSA_KEY_TYPE_CAMELLIA;
2709 *key_size = 128;
2710 break;
2711 case MBEDTLS_CIPHER_CAMELLIA_128_GCM:
2712 *alg = PSA_ALG_GCM;
2713 *key_type = PSA_KEY_TYPE_CAMELLIA;
2714 *key_size = 128;
2715 break;
2716 case MBEDTLS_CIPHER_CAMELLIA_192_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002717 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002718 *key_type = PSA_KEY_TYPE_CAMELLIA;
2719 *key_size = 192;
2720 break;
2721 case MBEDTLS_CIPHER_CAMELLIA_192_GCM:
2722 *alg = PSA_ALG_GCM;
2723 *key_type = PSA_KEY_TYPE_CAMELLIA;
2724 *key_size = 192;
2725 break;
2726 case MBEDTLS_CIPHER_CAMELLIA_256_CBC:
2727 *alg = PSA_ALG_CBC_NO_PADDING;
2728 *key_type = PSA_KEY_TYPE_CAMELLIA;
2729 *key_size = 256;
2730 break;
2731 case MBEDTLS_CIPHER_CAMELLIA_256_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002732 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002733 *key_type = PSA_KEY_TYPE_CAMELLIA;
2734 *key_size = 256;
2735 break;
2736 case MBEDTLS_CIPHER_CAMELLIA_256_GCM:
2737 *alg = PSA_ALG_GCM;
2738 *key_type = PSA_KEY_TYPE_CAMELLIA;
2739 *key_size = 256;
2740 break;
2741 case MBEDTLS_CIPHER_CHACHA20_POLY1305:
2742 *alg = PSA_ALG_CHACHA20_POLY1305;
2743 *key_type = PSA_KEY_TYPE_CHACHA20;
2744 *key_size = 256;
2745 break;
2746 case MBEDTLS_CIPHER_NULL:
2747 *alg = MBEDTLS_SSL_NULL_CIPHER;
2748 *key_type = 0;
2749 *key_size = 0;
2750 break;
2751 default:
2752 return PSA_ERROR_NOT_SUPPORTED;
2753 }
2754
2755 return PSA_SUCCESS;
2756}
Neil Armstrong8395d7a2022-05-18 11:44:56 +02002757#endif /* MBEDTLS_USE_PSA_CRYPTO || MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002758
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002759#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002760int mbedtls_ssl_conf_dh_param_bin(mbedtls_ssl_config *conf,
2761 const unsigned char *dhm_P, size_t P_len,
2762 const unsigned char *dhm_G, size_t G_len)
Hanno Beckera90658f2017-10-04 15:29:08 +01002763{
Janos Follath865b3eb2019-12-16 11:46:15 +00002764 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckera90658f2017-10-04 15:29:08 +01002765
Gilles Peskine449bd832023-01-11 14:50:10 +01002766 mbedtls_mpi_free(&conf->dhm_P);
2767 mbedtls_mpi_free(&conf->dhm_G);
Glenn Strausscee11292021-12-20 01:43:17 -05002768
Gilles Peskine449bd832023-01-11 14:50:10 +01002769 if ((ret = mbedtls_mpi_read_binary(&conf->dhm_P, dhm_P, P_len)) != 0 ||
2770 (ret = mbedtls_mpi_read_binary(&conf->dhm_G, dhm_G, G_len)) != 0) {
2771 mbedtls_mpi_free(&conf->dhm_P);
2772 mbedtls_mpi_free(&conf->dhm_G);
2773 return ret;
Hanno Beckera90658f2017-10-04 15:29:08 +01002774 }
2775
Gilles Peskine449bd832023-01-11 14:50:10 +01002776 return 0;
Hanno Beckera90658f2017-10-04 15:29:08 +01002777}
Paul Bakker5121ce52009-01-03 21:22:43 +00002778
Gilles Peskine449bd832023-01-11 14:50:10 +01002779int mbedtls_ssl_conf_dh_param_ctx(mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx)
Paul Bakker1b57b062011-01-06 15:48:19 +00002780{
Janos Follath865b3eb2019-12-16 11:46:15 +00002781 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1b57b062011-01-06 15:48:19 +00002782
Gilles Peskine449bd832023-01-11 14:50:10 +01002783 mbedtls_mpi_free(&conf->dhm_P);
2784 mbedtls_mpi_free(&conf->dhm_G);
Glenn Strausscee11292021-12-20 01:43:17 -05002785
Gilles Peskine449bd832023-01-11 14:50:10 +01002786 if ((ret = mbedtls_dhm_get_value(dhm_ctx, MBEDTLS_DHM_PARAM_P,
2787 &conf->dhm_P)) != 0 ||
2788 (ret = mbedtls_dhm_get_value(dhm_ctx, MBEDTLS_DHM_PARAM_G,
2789 &conf->dhm_G)) != 0) {
2790 mbedtls_mpi_free(&conf->dhm_P);
2791 mbedtls_mpi_free(&conf->dhm_G);
2792 return ret;
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01002793 }
Paul Bakker1b57b062011-01-06 15:48:19 +00002794
Gilles Peskine449bd832023-01-11 14:50:10 +01002795 return 0;
Paul Bakker1b57b062011-01-06 15:48:19 +00002796}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002797#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00002798
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02002799#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
2800/*
2801 * Set the minimum length for Diffie-Hellman parameters
2802 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002803void mbedtls_ssl_conf_dhm_min_bitlen(mbedtls_ssl_config *conf,
2804 unsigned int bitlen)
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02002805{
2806 conf->dhm_min_bitlen = bitlen;
2807}
2808#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
2809
Ronald Crone68ab4f2022-10-05 12:46:29 +02002810#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002811#if !defined(MBEDTLS_DEPRECATED_REMOVED) && defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002812/*
2813 * Set allowed/preferred hashes for handshake signatures
2814 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002815void mbedtls_ssl_conf_sig_hashes(mbedtls_ssl_config *conf,
2816 const int *hashes)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002817{
2818 conf->sig_hashes = hashes;
2819}
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002820#endif /* !MBEDTLS_DEPRECATED_REMOVED && MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker1cd6e002021-08-10 13:27:10 +01002821
Jerry Yuf017ee42022-01-12 15:49:48 +08002822/* Configure allowed signature algorithms for handshake */
Gilles Peskine449bd832023-01-11 14:50:10 +01002823void mbedtls_ssl_conf_sig_algs(mbedtls_ssl_config *conf,
2824 const uint16_t *sig_algs)
Hanno Becker1cd6e002021-08-10 13:27:10 +01002825{
Jerry Yuf017ee42022-01-12 15:49:48 +08002826#if !defined(MBEDTLS_DEPRECATED_REMOVED)
2827 conf->sig_hashes = NULL;
2828#endif /* !MBEDTLS_DEPRECATED_REMOVED */
2829 conf->sig_algs = sig_algs;
Hanno Becker1cd6e002021-08-10 13:27:10 +01002830}
Ronald Crone68ab4f2022-10-05 12:46:29 +02002831#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002832
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02002833#if defined(MBEDTLS_ECP_C)
Brett Warrene0edc842021-08-17 09:53:13 +01002834#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002835/*
2836 * Set the allowed elliptic curves
Brett Warrene0edc842021-08-17 09:53:13 +01002837 *
2838 * mbedtls_ssl_setup() takes the provided list
2839 * and translates it to a list of IANA TLS group identifiers,
2840 * stored in ssl->handshake->group_list.
2841 *
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002842 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002843void mbedtls_ssl_conf_curves(mbedtls_ssl_config *conf,
2844 const mbedtls_ecp_group_id *curve_list)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002845{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002846 conf->curve_list = curve_list;
Brett Warrene0edc842021-08-17 09:53:13 +01002847 conf->group_list = NULL;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002848}
Brett Warrene0edc842021-08-17 09:53:13 +01002849#endif /* MBEDTLS_DEPRECATED_REMOVED */
Hanno Becker947194e2017-04-07 13:25:49 +01002850#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002851
Brett Warrene0edc842021-08-17 09:53:13 +01002852/*
2853 * Set the allowed groups
2854 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002855void mbedtls_ssl_conf_groups(mbedtls_ssl_config *conf,
2856 const uint16_t *group_list)
Brett Warrene0edc842021-08-17 09:53:13 +01002857{
2858#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
2859 conf->curve_list = NULL;
2860#endif
2861 conf->group_list = group_list;
2862}
2863
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002864#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002865int mbedtls_ssl_set_hostname(mbedtls_ssl_context *ssl, const char *hostname)
Paul Bakker5121ce52009-01-03 21:22:43 +00002866{
Hanno Becker947194e2017-04-07 13:25:49 +01002867 /* Initialize to suppress unnecessary compiler warning */
2868 size_t hostname_len = 0;
2869
2870 /* Check if new hostname is valid before
2871 * making any change to current one */
Gilles Peskine449bd832023-01-11 14:50:10 +01002872 if (hostname != NULL) {
2873 hostname_len = strlen(hostname);
Hanno Becker947194e2017-04-07 13:25:49 +01002874
Gilles Peskine449bd832023-01-11 14:50:10 +01002875 if (hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN) {
2876 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2877 }
Hanno Becker947194e2017-04-07 13:25:49 +01002878 }
2879
2880 /* Now it's clear that we will overwrite the old hostname,
2881 * so we can free it safely */
2882
Gilles Peskine449bd832023-01-11 14:50:10 +01002883 if (ssl->hostname != NULL) {
2884 mbedtls_platform_zeroize(ssl->hostname, strlen(ssl->hostname));
2885 mbedtls_free(ssl->hostname);
Hanno Becker947194e2017-04-07 13:25:49 +01002886 }
2887
2888 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01002889
Gilles Peskine449bd832023-01-11 14:50:10 +01002890 if (hostname == NULL) {
Hanno Becker947194e2017-04-07 13:25:49 +01002891 ssl->hostname = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01002892 } else {
2893 ssl->hostname = mbedtls_calloc(1, hostname_len + 1);
2894 if (ssl->hostname == NULL) {
2895 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2896 }
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002897
Gilles Peskine449bd832023-01-11 14:50:10 +01002898 memcpy(ssl->hostname, hostname, hostname_len);
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002899
Hanno Becker947194e2017-04-07 13:25:49 +01002900 ssl->hostname[hostname_len] = '\0';
2901 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002902
Gilles Peskine449bd832023-01-11 14:50:10 +01002903 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002904}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01002905#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002906
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002907#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01002908void mbedtls_ssl_conf_sni(mbedtls_ssl_config *conf,
2909 int (*f_sni)(void *, mbedtls_ssl_context *,
2910 const unsigned char *, size_t),
2911 void *p_sni)
Paul Bakker5701cdc2012-09-27 21:49:42 +00002912{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002913 conf->f_sni = f_sni;
2914 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00002915}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002916#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00002917
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002918#if defined(MBEDTLS_SSL_ALPN)
Gilles Peskine449bd832023-01-11 14:50:10 +01002919int mbedtls_ssl_conf_alpn_protocols(mbedtls_ssl_config *conf, const char **protos)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002920{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002921 size_t cur_len, tot_len;
2922 const char **p;
2923
2924 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08002925 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
2926 * MUST NOT be truncated."
2927 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002928 */
2929 tot_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002930 for (p = protos; *p != NULL; p++) {
2931 cur_len = strlen(*p);
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002932 tot_len += cur_len;
2933
Gilles Peskine449bd832023-01-11 14:50:10 +01002934 if ((cur_len == 0) ||
2935 (cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN) ||
2936 (tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN)) {
2937 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2938 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002939 }
2940
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002941 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002942
Gilles Peskine449bd832023-01-11 14:50:10 +01002943 return 0;
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002944}
2945
Gilles Peskine449bd832023-01-11 14:50:10 +01002946const char *mbedtls_ssl_get_alpn_protocol(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002947{
Gilles Peskine449bd832023-01-11 14:50:10 +01002948 return ssl->alpn_chosen;
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002949}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002950#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002951
Johan Pascalb62bb512015-12-03 21:56:45 +01002952#if defined(MBEDTLS_SSL_DTLS_SRTP)
Gilles Peskine449bd832023-01-11 14:50:10 +01002953void mbedtls_ssl_conf_srtp_mki_value_supported(mbedtls_ssl_config *conf,
2954 int support_mki_value)
Ron Eldor591f1622018-01-22 12:30:04 +02002955{
2956 conf->dtls_srtp_mki_support = support_mki_value;
2957}
2958
Gilles Peskine449bd832023-01-11 14:50:10 +01002959int mbedtls_ssl_dtls_srtp_set_mki_value(mbedtls_ssl_context *ssl,
2960 unsigned char *mki_value,
2961 uint16_t mki_len)
Ron Eldor591f1622018-01-22 12:30:04 +02002962{
Gilles Peskine449bd832023-01-11 14:50:10 +01002963 if (mki_len > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH) {
2964 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Ron Eldora9788042018-12-05 11:04:31 +02002965 }
Ron Eldor591f1622018-01-22 12:30:04 +02002966
Gilles Peskine449bd832023-01-11 14:50:10 +01002967 if (ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_UNSUPPORTED) {
2968 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Ron Eldora9788042018-12-05 11:04:31 +02002969 }
Ron Eldor591f1622018-01-22 12:30:04 +02002970
Gilles Peskine449bd832023-01-11 14:50:10 +01002971 memcpy(ssl->dtls_srtp_info.mki_value, mki_value, mki_len);
Ron Eldor591f1622018-01-22 12:30:04 +02002972 ssl->dtls_srtp_info.mki_len = mki_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002973 return 0;
Ron Eldor591f1622018-01-22 12:30:04 +02002974}
2975
Gilles Peskine449bd832023-01-11 14:50:10 +01002976int mbedtls_ssl_conf_dtls_srtp_protection_profiles(mbedtls_ssl_config *conf,
2977 const mbedtls_ssl_srtp_profile *profiles)
Johan Pascalb62bb512015-12-03 21:56:45 +01002978{
Johan Pascal253d0262020-09-22 13:04:45 +02002979 const mbedtls_ssl_srtp_profile *p;
2980 size_t list_size = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01002981
Johan Pascal253d0262020-09-22 13:04:45 +02002982 /* check the profiles list: all entry must be valid,
2983 * its size cannot be more than the total number of supported profiles, currently 4 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002984 for (p = profiles; *p != MBEDTLS_TLS_SRTP_UNSET &&
2985 list_size <= MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH;
2986 p++) {
2987 if (mbedtls_ssl_check_srtp_profile_value(*p) != MBEDTLS_TLS_SRTP_UNSET) {
Johan Pascal76fdf1d2020-10-22 23:31:00 +02002988 list_size++;
Gilles Peskine449bd832023-01-11 14:50:10 +01002989 } else {
Johan Pascal76fdf1d2020-10-22 23:31:00 +02002990 /* unsupported value, stop parsing and set the size to an error value */
2991 list_size = MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH + 1;
Johan Pascalb62bb512015-12-03 21:56:45 +01002992 }
2993 }
2994
Gilles Peskine449bd832023-01-11 14:50:10 +01002995 if (list_size > MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH) {
2996 conf->dtls_srtp_profile_list = NULL;
2997 conf->dtls_srtp_profile_list_len = 0;
2998 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Johan Pascal253d0262020-09-22 13:04:45 +02002999 }
3000
Johan Pascal9bc97ca2020-09-21 23:44:45 +02003001 conf->dtls_srtp_profile_list = profiles;
Johan Pascal253d0262020-09-22 13:04:45 +02003002 conf->dtls_srtp_profile_list_len = list_size;
Johan Pascalb62bb512015-12-03 21:56:45 +01003003
Gilles Peskine449bd832023-01-11 14:50:10 +01003004 return 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01003005}
3006
Gilles Peskine449bd832023-01-11 14:50:10 +01003007void mbedtls_ssl_get_dtls_srtp_negotiation_result(const mbedtls_ssl_context *ssl,
3008 mbedtls_dtls_srtp_info *dtls_srtp_info)
Johan Pascalb62bb512015-12-03 21:56:45 +01003009{
Johan Pascal2258a4f2020-10-28 13:53:09 +01003010 dtls_srtp_info->chosen_dtls_srtp_profile = ssl->dtls_srtp_info.chosen_dtls_srtp_profile;
3011 /* do not copy the mki value if there is no chosen profile */
Gilles Peskine449bd832023-01-11 14:50:10 +01003012 if (dtls_srtp_info->chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET) {
Johan Pascal2258a4f2020-10-28 13:53:09 +01003013 dtls_srtp_info->mki_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003014 } else {
Johan Pascal2258a4f2020-10-28 13:53:09 +01003015 dtls_srtp_info->mki_len = ssl->dtls_srtp_info.mki_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01003016 memcpy(dtls_srtp_info->mki_value, ssl->dtls_srtp_info.mki_value,
3017 ssl->dtls_srtp_info.mki_len);
Johan Pascal2258a4f2020-10-28 13:53:09 +01003018 }
Johan Pascalb62bb512015-12-03 21:56:45 +01003019}
Johan Pascalb62bb512015-12-03 21:56:45 +01003020#endif /* MBEDTLS_SSL_DTLS_SRTP */
3021
Aditya Patwardhan3096f332022-07-26 14:31:46 +05303022#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003023void mbedtls_ssl_conf_max_version(mbedtls_ssl_config *conf, int major, int minor)
Paul Bakker490ecc82011-10-06 13:04:09 +00003024{
Glenn Strauss2dfcea22022-03-14 17:26:42 -04003025 conf->max_tls_version = (major << 8) | minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00003026}
3027
Gilles Peskine449bd832023-01-11 14:50:10 +01003028void mbedtls_ssl_conf_min_version(mbedtls_ssl_config *conf, int major, int minor)
Paul Bakker1d29fb52012-09-28 13:28:45 +00003029{
Glenn Strauss2dfcea22022-03-14 17:26:42 -04003030 conf->min_tls_version = (major << 8) | minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00003031}
Aditya Patwardhan3096f332022-07-26 14:31:46 +05303032#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker1d29fb52012-09-28 13:28:45 +00003033
Janos Follath088ce432017-04-10 12:42:31 +01003034#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003035void mbedtls_ssl_conf_cert_req_ca_list(mbedtls_ssl_config *conf,
3036 char cert_req_ca_list)
Janos Follath088ce432017-04-10 12:42:31 +01003037{
3038 conf->cert_req_ca_list = cert_req_ca_list;
3039}
3040#endif
3041
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003042#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01003043void mbedtls_ssl_conf_encrypt_then_mac(mbedtls_ssl_config *conf, char etm)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01003044{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003045 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01003046}
3047#endif
3048
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003049#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Gilles Peskine449bd832023-01-11 14:50:10 +01003050void mbedtls_ssl_conf_extended_master_secret(mbedtls_ssl_config *conf, char ems)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02003051{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003052 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02003053}
3054#endif
3055
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003056#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01003057int mbedtls_ssl_conf_max_frag_len(mbedtls_ssl_config *conf, unsigned char mfl_code)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003058{
Gilles Peskine449bd832023-01-11 14:50:10 +01003059 if (mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
3060 ssl_mfl_code_to_length(mfl_code) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN) {
3061 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003062 }
3063
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01003064 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003065
Gilles Peskine449bd832023-01-11 14:50:10 +01003066 return 0;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003067}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003068#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003069
Gilles Peskine449bd832023-01-11 14:50:10 +01003070void mbedtls_ssl_conf_legacy_renegotiation(mbedtls_ssl_config *conf, int allow_legacy)
Paul Bakker48916f92012-09-16 19:57:18 +00003071{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003072 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00003073}
3074
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003075#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01003076void mbedtls_ssl_conf_renegotiation(mbedtls_ssl_config *conf, int renegotiation)
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003077{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003078 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003079}
3080
Gilles Peskine449bd832023-01-11 14:50:10 +01003081void mbedtls_ssl_conf_renegotiation_enforced(mbedtls_ssl_config *conf, int max_records)
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003082{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003083 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003084}
3085
Gilles Peskine449bd832023-01-11 14:50:10 +01003086void mbedtls_ssl_conf_renegotiation_period(mbedtls_ssl_config *conf,
3087 const unsigned char period[8])
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01003088{
Gilles Peskine449bd832023-01-11 14:50:10 +01003089 memcpy(conf->renego_period, period, 8);
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01003090}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003091#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00003092
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003093#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003094#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003095void mbedtls_ssl_conf_session_tickets(mbedtls_ssl_config *conf, int use_tickets)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003096{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01003097 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003098}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003099#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02003100
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003101#if defined(MBEDTLS_SSL_SRV_C)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08003102
Jerry Yud0766ec2022-09-22 10:46:57 +08003103#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003104void mbedtls_ssl_conf_new_session_tickets(mbedtls_ssl_config *conf,
3105 uint16_t num_tickets)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08003106{
Jerry Yud0766ec2022-09-22 10:46:57 +08003107 conf->new_session_tickets_count = num_tickets;
Jerry Yu1ad7ace2022-08-09 13:28:39 +08003108}
3109#endif
3110
Gilles Peskine449bd832023-01-11 14:50:10 +01003111void mbedtls_ssl_conf_session_tickets_cb(mbedtls_ssl_config *conf,
3112 mbedtls_ssl_ticket_write_t *f_ticket_write,
3113 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
3114 void *p_ticket)
Paul Bakker606b4ba2013-08-14 16:52:14 +02003115{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02003116 conf->f_ticket_write = f_ticket_write;
3117 conf->f_ticket_parse = f_ticket_parse;
3118 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02003119}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003120#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003121#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003122
Gilles Peskine449bd832023-01-11 14:50:10 +01003123void mbedtls_ssl_set_export_keys_cb(mbedtls_ssl_context *ssl,
3124 mbedtls_ssl_export_keys_t *f_export_keys,
3125 void *p_export_keys)
Robert Cragie4feb7ae2015-10-02 13:33:37 +01003126{
Hanno Becker7e6c1782021-06-08 09:24:55 +01003127 ssl->f_export_keys = f_export_keys;
3128 ssl->p_export_keys = p_export_keys;
Ron Eldorf5cc10d2019-05-07 18:33:40 +03003129}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01003130
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003131#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003132void mbedtls_ssl_conf_async_private_cb(
3133 mbedtls_ssl_config *conf,
3134 mbedtls_ssl_async_sign_t *f_async_sign,
3135 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
3136 mbedtls_ssl_async_resume_t *f_async_resume,
3137 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskine449bd832023-01-11 14:50:10 +01003138 void *async_config_data)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003139{
3140 conf->f_async_sign_start = f_async_sign;
3141 conf->f_async_decrypt_start = f_async_decrypt;
3142 conf->f_async_resume = f_async_resume;
3143 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003144 conf->p_async_config_data = async_config_data;
3145}
3146
Gilles Peskine449bd832023-01-11 14:50:10 +01003147void *mbedtls_ssl_conf_get_async_config_data(const mbedtls_ssl_config *conf)
Gilles Peskine8f97af72018-04-26 11:46:10 +02003148{
Gilles Peskine449bd832023-01-11 14:50:10 +01003149 return conf->p_async_config_data;
Gilles Peskine8f97af72018-04-26 11:46:10 +02003150}
3151
Gilles Peskine449bd832023-01-11 14:50:10 +01003152void *mbedtls_ssl_get_async_operation_data(const mbedtls_ssl_context *ssl)
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003153{
Gilles Peskine449bd832023-01-11 14:50:10 +01003154 if (ssl->handshake == NULL) {
3155 return NULL;
3156 } else {
3157 return ssl->handshake->user_async_ctx;
3158 }
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003159}
3160
Gilles Peskine449bd832023-01-11 14:50:10 +01003161void mbedtls_ssl_set_async_operation_data(mbedtls_ssl_context *ssl,
3162 void *ctx)
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003163{
Gilles Peskine449bd832023-01-11 14:50:10 +01003164 if (ssl->handshake != NULL) {
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003165 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +01003166 }
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003167}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003168#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003169
Paul Bakker5121ce52009-01-03 21:22:43 +00003170/*
3171 * SSL get accessors
3172 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003173uint32_t mbedtls_ssl_get_verify_result(const mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00003174{
Gilles Peskine449bd832023-01-11 14:50:10 +01003175 if (ssl->session != NULL) {
3176 return ssl->session->verify_result;
3177 }
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00003178
Gilles Peskine449bd832023-01-11 14:50:10 +01003179 if (ssl->session_negotiate != NULL) {
3180 return ssl->session_negotiate->verify_result;
3181 }
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00003182
Gilles Peskine449bd832023-01-11 14:50:10 +01003183 return 0xFFFFFFFF;
Paul Bakker5121ce52009-01-03 21:22:43 +00003184}
3185
Gilles Peskine449bd832023-01-11 14:50:10 +01003186int mbedtls_ssl_get_ciphersuite_id_from_ssl(const mbedtls_ssl_context *ssl)
Glenn Strauss8f526902022-01-13 00:04:49 -05003187{
Gilles Peskine449bd832023-01-11 14:50:10 +01003188 if (ssl == NULL || ssl->session == NULL) {
3189 return 0;
3190 }
Glenn Strauss8f526902022-01-13 00:04:49 -05003191
Gilles Peskine449bd832023-01-11 14:50:10 +01003192 return ssl->session->ciphersuite;
Glenn Strauss8f526902022-01-13 00:04:49 -05003193}
3194
Gilles Peskine449bd832023-01-11 14:50:10 +01003195const char *mbedtls_ssl_get_ciphersuite(const mbedtls_ssl_context *ssl)
Paul Bakker72f62662011-01-16 21:27:44 +00003196{
Gilles Peskine449bd832023-01-11 14:50:10 +01003197 if (ssl == NULL || ssl->session == NULL) {
3198 return NULL;
3199 }
Paul Bakker926c8e42013-03-06 10:23:34 +01003200
Gilles Peskine449bd832023-01-11 14:50:10 +01003201 return mbedtls_ssl_get_ciphersuite_name(ssl->session->ciphersuite);
Paul Bakker72f62662011-01-16 21:27:44 +00003202}
3203
Gilles Peskine449bd832023-01-11 14:50:10 +01003204const char *mbedtls_ssl_get_version(const mbedtls_ssl_context *ssl)
Paul Bakker43ca69c2011-01-15 17:35:19 +00003205{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003206#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003207 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
3208 switch (ssl->tls_version) {
Glenn Strauss60bfe602022-03-14 19:04:24 -04003209 case MBEDTLS_SSL_VERSION_TLS1_2:
Gilles Peskine449bd832023-01-11 14:50:10 +01003210 return "DTLSv1.2";
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003211 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01003212 return "unknown (DTLS)";
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003213 }
3214 }
3215#endif
3216
Gilles Peskine449bd832023-01-11 14:50:10 +01003217 switch (ssl->tls_version) {
Glenn Strauss60bfe602022-03-14 19:04:24 -04003218 case MBEDTLS_SSL_VERSION_TLS1_2:
Gilles Peskine449bd832023-01-11 14:50:10 +01003219 return "TLSv1.2";
Glenn Strauss60bfe602022-03-14 19:04:24 -04003220 case MBEDTLS_SSL_VERSION_TLS1_3:
Gilles Peskine449bd832023-01-11 14:50:10 +01003221 return "TLSv1.3";
Paul Bakker43ca69c2011-01-15 17:35:19 +00003222 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01003223 return "unknown";
Paul Bakker43ca69c2011-01-15 17:35:19 +00003224 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00003225}
3226
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003227#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01003228size_t mbedtls_ssl_get_input_max_frag_len(const mbedtls_ssl_context *ssl)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003229{
David Horstmann95d516f2021-05-04 18:36:56 +01003230 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003231 size_t read_mfl;
3232
Jerry Yuddda0502022-12-01 19:43:12 +08003233#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003234 /* Use the configured MFL for the client if we're past SERVER_HELLO_DONE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003235 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
3236 ssl->state >= MBEDTLS_SSL_SERVER_HELLO_DONE) {
3237 return ssl_mfl_code_to_length(ssl->conf->mfl_code);
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003238 }
Jerry Yuddda0502022-12-01 19:43:12 +08003239#endif
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003240
3241 /* Check if a smaller max length was negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01003242 if (ssl->session_out != NULL) {
3243 read_mfl = ssl_mfl_code_to_length(ssl->session_out->mfl_code);
3244 if (read_mfl < max_len) {
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003245 max_len = read_mfl;
3246 }
3247 }
3248
Jerry Yuddda0502022-12-01 19:43:12 +08003249 /* During a handshake, use the value being negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01003250 if (ssl->session_negotiate != NULL) {
3251 read_mfl = ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code);
3252 if (read_mfl < max_len) {
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003253 max_len = read_mfl;
3254 }
3255 }
3256
Gilles Peskine449bd832023-01-11 14:50:10 +01003257 return max_len;
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003258}
3259
Gilles Peskine449bd832023-01-11 14:50:10 +01003260size_t mbedtls_ssl_get_output_max_frag_len(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003261{
3262 size_t max_len;
3263
3264 /*
3265 * Assume mfl_code is correct since it was checked when set
3266 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003267 max_len = ssl_mfl_code_to_length(ssl->conf->mfl_code);
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003268
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02003269 /* Check if a smaller max length was negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01003270 if (ssl->session_out != NULL &&
3271 ssl_mfl_code_to_length(ssl->session_out->mfl_code) < max_len) {
3272 max_len = ssl_mfl_code_to_length(ssl->session_out->mfl_code);
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003273 }
3274
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02003275 /* During a handshake, use the value being negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01003276 if (ssl->session_negotiate != NULL &&
3277 ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code) < max_len) {
3278 max_len = ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code);
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02003279 }
3280
Gilles Peskine449bd832023-01-11 14:50:10 +01003281 return max_len;
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003282}
3283#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
3284
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003285#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003286size_t mbedtls_ssl_get_current_mtu(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003287{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04003288 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003289 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
3290 (ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
3291 ssl->state == MBEDTLS_SSL_SERVER_HELLO)) {
3292 return 0;
3293 }
Andrzej Kurekef43ce62018-10-09 08:24:12 -04003294
Gilles Peskine449bd832023-01-11 14:50:10 +01003295 if (ssl->handshake == NULL || ssl->handshake->mtu == 0) {
3296 return ssl->mtu;
3297 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003298
Gilles Peskine449bd832023-01-11 14:50:10 +01003299 if (ssl->mtu == 0) {
3300 return ssl->handshake->mtu;
3301 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003302
Gilles Peskine449bd832023-01-11 14:50:10 +01003303 return ssl->mtu < ssl->handshake->mtu ?
3304 ssl->mtu : ssl->handshake->mtu;
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003305}
3306#endif /* MBEDTLS_SSL_PROTO_DTLS */
3307
Gilles Peskine449bd832023-01-11 14:50:10 +01003308int mbedtls_ssl_get_max_out_record_payload(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003309{
3310 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
3311
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02003312#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
3313 !defined(MBEDTLS_SSL_PROTO_DTLS)
3314 (void) ssl;
3315#endif
3316
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003317#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01003318 const size_t mfl = mbedtls_ssl_get_output_max_frag_len(ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003319
Gilles Peskine449bd832023-01-11 14:50:10 +01003320 if (max_len > mfl) {
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003321 max_len = mfl;
Gilles Peskine449bd832023-01-11 14:50:10 +01003322 }
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003323#endif
3324
3325#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003326 if (mbedtls_ssl_get_current_mtu(ssl) != 0) {
3327 const size_t mtu = mbedtls_ssl_get_current_mtu(ssl);
3328 const int ret = mbedtls_ssl_get_record_expansion(ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003329 const size_t overhead = (size_t) ret;
3330
Gilles Peskine449bd832023-01-11 14:50:10 +01003331 if (ret < 0) {
3332 return ret;
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003333 }
3334
Gilles Peskine449bd832023-01-11 14:50:10 +01003335 if (mtu <= overhead) {
3336 MBEDTLS_SSL_DEBUG_MSG(1, ("MTU too low for record expansion"));
3337 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
3338 }
3339
3340 if (max_len > mtu - overhead) {
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003341 max_len = mtu - overhead;
Gilles Peskine449bd832023-01-11 14:50:10 +01003342 }
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003343 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003344#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003345
Hanno Becker0defedb2018-08-10 12:35:02 +01003346#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
3347 !defined(MBEDTLS_SSL_PROTO_DTLS)
3348 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003349#endif
3350
Gilles Peskine449bd832023-01-11 14:50:10 +01003351 return (int) max_len;
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003352}
3353
Gilles Peskine449bd832023-01-11 14:50:10 +01003354int mbedtls_ssl_get_max_in_record_payload(const mbedtls_ssl_context *ssl)
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003355{
3356 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
3357
3358#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
3359 (void) ssl;
3360#endif
3361
3362#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01003363 const size_t mfl = mbedtls_ssl_get_input_max_frag_len(ssl);
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003364
Gilles Peskine449bd832023-01-11 14:50:10 +01003365 if (max_len > mfl) {
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003366 max_len = mfl;
Gilles Peskine449bd832023-01-11 14:50:10 +01003367 }
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003368#endif
3369
Gilles Peskine449bd832023-01-11 14:50:10 +01003370 return (int) max_len;
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003371}
3372
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003373#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003374const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert(const mbedtls_ssl_context *ssl)
Paul Bakkerb0550d92012-10-30 07:51:03 +00003375{
Gilles Peskine449bd832023-01-11 14:50:10 +01003376 if (ssl == NULL || ssl->session == NULL) {
3377 return NULL;
3378 }
Paul Bakkerb0550d92012-10-30 07:51:03 +00003379
Hanno Beckere6824572019-02-07 13:18:46 +00003380#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01003381 return ssl->session->peer_cert;
Hanno Beckere6824572019-02-07 13:18:46 +00003382#else
Gilles Peskine449bd832023-01-11 14:50:10 +01003383 return NULL;
Hanno Beckere6824572019-02-07 13:18:46 +00003384#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00003385}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003386#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00003387
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003388#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003389int mbedtls_ssl_get_session(const mbedtls_ssl_context *ssl,
3390 mbedtls_ssl_session *dst)
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003391{
Hanno Beckere810bbc2021-05-14 16:01:05 +01003392 int ret;
3393
Gilles Peskine449bd832023-01-11 14:50:10 +01003394 if (ssl == NULL ||
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003395 dst == NULL ||
3396 ssl->session == NULL ||
Gilles Peskine449bd832023-01-11 14:50:10 +01003397 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT) {
3398 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003399 }
3400
Hanno Beckere810bbc2021-05-14 16:01:05 +01003401 /* Since Mbed TLS 3.0, mbedtls_ssl_get_session() is no longer
3402 * idempotent: Each session can only be exported once.
3403 *
3404 * (This is in preparation for TLS 1.3 support where we will
3405 * need the ability to export multiple sessions (aka tickets),
3406 * which will be achieved by calling mbedtls_ssl_get_session()
3407 * multiple times until it fails.)
3408 *
3409 * Check whether we have already exported the current session,
3410 * and fail if so.
3411 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003412 if (ssl->session->exported == 1) {
3413 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
3414 }
Hanno Beckere810bbc2021-05-14 16:01:05 +01003415
Gilles Peskine449bd832023-01-11 14:50:10 +01003416 ret = mbedtls_ssl_session_copy(dst, ssl->session);
3417 if (ret != 0) {
3418 return ret;
3419 }
Hanno Beckere810bbc2021-05-14 16:01:05 +01003420
3421 /* Remember that we've exported the session. */
3422 ssl->session->exported = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003423 return 0;
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003424}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003425#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003426
Paul Bakker5121ce52009-01-03 21:22:43 +00003427/*
Hanno Beckera835da52019-05-16 12:39:07 +01003428 * Define ticket header determining Mbed TLS version
3429 * and structure of the ticket.
3430 */
3431
Hanno Becker94ef3b32019-05-16 12:50:45 +01003432/*
Hanno Becker50b59662019-05-28 14:30:45 +01003433 * Define bitflag determining compile-time settings influencing
3434 * structure of serialized SSL sessions.
Hanno Becker94ef3b32019-05-16 12:50:45 +01003435 */
3436
Hanno Becker50b59662019-05-28 14:30:45 +01003437#if defined(MBEDTLS_HAVE_TIME)
Hanno Becker3e088662019-05-29 11:10:18 +01003438#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker50b59662019-05-28 14:30:45 +01003439#else
Hanno Becker3e088662019-05-29 11:10:18 +01003440#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003441#endif /* MBEDTLS_HAVE_TIME */
3442
3443#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker3e088662019-05-29 11:10:18 +01003444#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003445#else
Hanno Becker3e088662019-05-29 11:10:18 +01003446#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003447#endif /* MBEDTLS_X509_CRT_PARSE_C */
3448
3449#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Becker3e088662019-05-29 11:10:18 +01003450#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003451#else
Hanno Becker3e088662019-05-29 11:10:18 +01003452#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003453#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
3454
3455#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Becker3e088662019-05-29 11:10:18 +01003456#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003457#else
Hanno Becker3e088662019-05-29 11:10:18 +01003458#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003459#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
3460
Hanno Becker94ef3b32019-05-16 12:50:45 +01003461#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3e088662019-05-29 11:10:18 +01003462#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003463#else
Hanno Becker3e088662019-05-29 11:10:18 +01003464#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003465#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
3466
Hanno Becker94ef3b32019-05-16 12:50:45 +01003467#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3468#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
3469#else
3470#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
3471#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3472
Hanno Becker3e088662019-05-29 11:10:18 +01003473#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
3474#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
3475#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
3476#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
Hanno Becker37bdbe62021-08-01 05:38:58 +01003477#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 4
3478#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 5
Hanno Becker3e088662019-05-29 11:10:18 +01003479
Hanno Becker50b59662019-05-28 14:30:45 +01003480#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Gilles Peskine449bd832023-01-11 14:50:10 +01003481 ((uint16_t) ( \
3482 (SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT) | \
3483 (SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT) | \
3484 (SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << \
3485 SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT) | \
3486 (SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT) | \
3487 (SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT) | \
3488 (SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT)))
Hanno Becker94ef3b32019-05-16 12:50:45 +01003489
Hanno Beckerf8787072019-05-16 12:41:07 +01003490static unsigned char ssl_serialized_session_header[] = {
Hanno Becker94ef3b32019-05-16 12:50:45 +01003491 MBEDTLS_VERSION_MAJOR,
3492 MBEDTLS_VERSION_MINOR,
3493 MBEDTLS_VERSION_PATCH,
Gilles Peskine449bd832023-01-11 14:50:10 +01003494 MBEDTLS_BYTE_1(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
3495 MBEDTLS_BYTE_0(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
Hanno Beckerf8787072019-05-16 12:41:07 +01003496};
Hanno Beckera835da52019-05-16 12:39:07 +01003497
3498/*
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003499 * Serialize a session in the following format:
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02003500 * (in the presentation language of TLS, RFC 8446 section 3)
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003501 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003502 * struct {
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01003503 *
Hanno Beckerdce50972021-08-01 05:39:23 +01003504 * opaque mbedtls_version[3]; // library version: major, minor, patch
3505 * opaque session_format[2]; // library-version specific 16-bit field
3506 * // determining the format of the remaining
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003507 * // serialized data.
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01003508 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003509 * Note: When updating the format, remember to keep
3510 * these version+format bytes.
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003511 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003512 * // In this version, `session_format` determines
3513 * // the setting of those compile-time
3514 * // configuration options which influence
3515 * // the structure of mbedtls_ssl_session.
3516 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003517 * uint8_t minor_ver; // Protocol minor version. Possible values:
Jerry Yu0c2a7382022-12-06 13:27:25 +08003518 * // - TLS 1.2 (0x0303)
3519 * // - TLS 1.3 (0x0304)
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003520 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003521 * select (serialized_session.tls_version) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003522 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003523 * case MBEDTLS_SSL_VERSION_TLS1_2:
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003524 * serialized_session_tls12 data;
Jerry Yu0c2a7382022-12-06 13:27:25 +08003525 * case MBEDTLS_SSL_VERSION_TLS1_3:
Jerry Yuddda0502022-12-01 19:43:12 +08003526 * serialized_session_tls13 data;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003527 *
3528 * };
3529 *
3530 * } serialized_session;
3531 *
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003532 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003533
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003534MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003535static int ssl_session_save(const mbedtls_ssl_session *session,
3536 unsigned char omit_header,
3537 unsigned char *buf,
3538 size_t buf_len,
3539 size_t *olen)
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003540{
3541 unsigned char *p = buf;
3542 size_t used = 0;
Jerry Yu251a12e2022-07-13 15:15:48 +08003543 size_t remaining_len;
Jerry Yue36fdd62022-08-17 21:31:36 +08003544#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3545 size_t out_len;
3546 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3547#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01003548 if (session == NULL) {
3549 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
3550 }
Jerry Yu438ddd82022-07-07 06:55:50 +00003551
Gilles Peskine449bd832023-01-11 14:50:10 +01003552 if (!omit_header) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003553 /*
3554 * Add Mbed TLS version identifier
3555 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003556 used += sizeof(ssl_serialized_session_header);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003557
Gilles Peskine449bd832023-01-11 14:50:10 +01003558 if (used <= buf_len) {
3559 memcpy(p, ssl_serialized_session_header,
3560 sizeof(ssl_serialized_session_header));
3561 p += sizeof(ssl_serialized_session_header);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003562 }
3563 }
3564
3565 /*
3566 * TLS version identifier
3567 */
3568 used += 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003569 if (used <= buf_len) {
3570 *p++ = MBEDTLS_BYTE_0(session->tls_version);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003571 }
3572
3573 /* Forward to version-specific serialization routine. */
Jerry Yufca4d572022-07-21 10:37:48 +08003574 remaining_len = (buf_len >= used) ? buf_len - used : 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003575 switch (session->tls_version) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003576#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01003577 case MBEDTLS_SSL_VERSION_TLS1_2:
3578 used += ssl_tls12_session_save(session, p, remaining_len);
3579 break;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003580#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3581
Jerry Yu251a12e2022-07-13 15:15:48 +08003582#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01003583 case MBEDTLS_SSL_VERSION_TLS1_3:
3584 ret = ssl_tls13_session_save(session, p, remaining_len, &out_len);
3585 if (ret != 0 && ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL) {
3586 return ret;
3587 }
3588 used += out_len;
3589 break;
Jerry Yu251a12e2022-07-13 15:15:48 +08003590#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3591
Gilles Peskine449bd832023-01-11 14:50:10 +01003592 default:
3593 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003594 }
3595
3596 *olen = used;
Gilles Peskine449bd832023-01-11 14:50:10 +01003597 if (used > buf_len) {
3598 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
3599 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003600
Gilles Peskine449bd832023-01-11 14:50:10 +01003601 return 0;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003602}
3603
3604/*
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003605 * Public wrapper for ssl_session_save()
3606 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003607int mbedtls_ssl_session_save(const mbedtls_ssl_session *session,
3608 unsigned char *buf,
3609 size_t buf_len,
3610 size_t *olen)
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003611{
Gilles Peskine449bd832023-01-11 14:50:10 +01003612 return ssl_session_save(session, 0, buf, buf_len, olen);
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003613}
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003614
Jerry Yuf1b23ca2022-02-18 11:48:47 +08003615/*
3616 * Deserialize session, see mbedtls_ssl_session_save() for format.
3617 *
3618 * This internal version is wrapped by a public function that cleans up in
3619 * case of error, and has an extra option omit_header.
3620 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003621MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003622static int ssl_session_load(mbedtls_ssl_session *session,
3623 unsigned char omit_header,
3624 const unsigned char *buf,
3625 size_t len)
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003626{
3627 const unsigned char *p = buf;
3628 const unsigned char * const end = buf + len;
Jerry Yu438ddd82022-07-07 06:55:50 +00003629 size_t remaining_len;
3630
3631
Gilles Peskine449bd832023-01-11 14:50:10 +01003632 if (session == NULL) {
3633 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
3634 }
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003635
Gilles Peskine449bd832023-01-11 14:50:10 +01003636 if (!omit_header) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003637 /*
3638 * Check Mbed TLS version identifier
3639 */
3640
Gilles Peskine449bd832023-01-11 14:50:10 +01003641 if ((size_t) (end - p) < sizeof(ssl_serialized_session_header)) {
3642 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003643 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003644
3645 if (memcmp(p, ssl_serialized_session_header,
3646 sizeof(ssl_serialized_session_header)) != 0) {
3647 return MBEDTLS_ERR_SSL_VERSION_MISMATCH;
3648 }
3649 p += sizeof(ssl_serialized_session_header);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003650 }
3651
3652 /*
3653 * TLS version identifier
3654 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003655 if (1 > (size_t) (end - p)) {
3656 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3657 }
Glenn Straussda7851c2022-03-14 13:29:48 -04003658 session->tls_version = 0x0300 | *p++;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003659
3660 /* Dispatch according to TLS version. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003661 remaining_len = (end - p);
3662 switch (session->tls_version) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003663#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01003664 case MBEDTLS_SSL_VERSION_TLS1_2:
3665 return ssl_tls12_session_load(session, p, remaining_len);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003666#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3667
Jerry Yu438ddd82022-07-07 06:55:50 +00003668#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01003669 case MBEDTLS_SSL_VERSION_TLS1_3:
3670 return ssl_tls13_session_load(session, p, remaining_len);
Jerry Yu438ddd82022-07-07 06:55:50 +00003671#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3672
Gilles Peskine449bd832023-01-11 14:50:10 +01003673 default:
3674 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003675 }
3676}
3677
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003678/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02003679 * Deserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003680 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003681int mbedtls_ssl_session_load(mbedtls_ssl_session *session,
3682 const unsigned char *buf,
3683 size_t len)
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003684{
Gilles Peskine449bd832023-01-11 14:50:10 +01003685 int ret = ssl_session_load(session, 0, buf, len);
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003686
Gilles Peskine449bd832023-01-11 14:50:10 +01003687 if (ret != 0) {
3688 mbedtls_ssl_session_free(session);
3689 }
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003690
Gilles Peskine449bd832023-01-11 14:50:10 +01003691 return ret;
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003692}
3693
3694/*
Paul Bakker1961b702013-01-25 14:49:24 +01003695 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00003696 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003697MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003698static int ssl_prepare_handshake_step(mbedtls_ssl_context *ssl)
Hanno Becker41934dd2021-08-07 19:13:43 +01003699{
3700 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3701
Ronald Cron66dbf912022-02-02 15:33:46 +01003702 /*
3703 * We may have not been able to send to the peer all the handshake data
Ronald Cron3f20b772022-03-08 16:00:02 +01003704 * that were written into the output buffer by the previous handshake step,
3705 * if the write to the network callback returned with the
Ronald Cron66dbf912022-02-02 15:33:46 +01003706 * #MBEDTLS_ERR_SSL_WANT_WRITE error code.
3707 * We proceed to the next handshake step only when all data from the
3708 * previous one have been sent to the peer, thus we make sure that this is
3709 * the case here by calling `mbedtls_ssl_flush_output()`. The function may
3710 * return with the #MBEDTLS_ERR_SSL_WANT_WRITE error code in which case
3711 * we have to wait before to go ahead.
3712 * In the case of TLS 1.3, handshake step handlers do not send data to the
3713 * peer. Data are only sent here and through
3714 * `mbedtls_ssl_handle_pending_alert` in case an error that triggered an
Andrzej Kurek5c65c572022-04-13 14:28:52 -04003715 * alert occurred.
Ronald Cron66dbf912022-02-02 15:33:46 +01003716 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003717 if ((ret = mbedtls_ssl_flush_output(ssl)) != 0) {
3718 return ret;
3719 }
Hanno Becker41934dd2021-08-07 19:13:43 +01003720
3721#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003722 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3723 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING) {
3724 if ((ret = mbedtls_ssl_flight_transmit(ssl)) != 0) {
3725 return ret;
3726 }
Hanno Becker41934dd2021-08-07 19:13:43 +01003727 }
3728#endif /* MBEDTLS_SSL_PROTO_DTLS */
3729
Gilles Peskine449bd832023-01-11 14:50:10 +01003730 return ret;
Hanno Becker41934dd2021-08-07 19:13:43 +01003731}
3732
Gilles Peskine449bd832023-01-11 14:50:10 +01003733int mbedtls_ssl_handshake_step(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00003734{
Hanno Becker41934dd2021-08-07 19:13:43 +01003735 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003736
Gilles Peskine449bd832023-01-11 14:50:10 +01003737 if (ssl == NULL ||
Hanno Becker41934dd2021-08-07 19:13:43 +01003738 ssl->conf == NULL ||
3739 ssl->handshake == NULL ||
Gilles Peskine449bd832023-01-11 14:50:10 +01003740 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER) {
3741 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Becker41934dd2021-08-07 19:13:43 +01003742 }
3743
Gilles Peskine449bd832023-01-11 14:50:10 +01003744 ret = ssl_prepare_handshake_step(ssl);
3745 if (ret != 0) {
3746 return ret;
3747 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003748
Gilles Peskine449bd832023-01-11 14:50:10 +01003749 ret = mbedtls_ssl_handle_pending_alert(ssl);
3750 if (ret != 0) {
Jerry Yue7047812021-09-13 19:26:39 +08003751 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01003752 }
Jerry Yue7047812021-09-13 19:26:39 +08003753
Tom Cosgrove2fdc7b32022-09-21 12:33:17 +01003754 /* If ssl->conf->endpoint is not one of MBEDTLS_SSL_IS_CLIENT or
3755 * MBEDTLS_SSL_IS_SERVER, this is the return code we give */
3756 ret = MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3757
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003758#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003759 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
3760 MBEDTLS_SSL_DEBUG_MSG(2, ("client state: %s",
3761 mbedtls_ssl_states_str(ssl->state)));
Jerry Yub9930e72021-08-06 17:11:51 +08003762
Gilles Peskine449bd832023-01-11 14:50:10 +01003763 switch (ssl->state) {
Ronald Cron9f0fba32022-02-10 16:45:15 +01003764 case MBEDTLS_SSL_HELLO_REQUEST:
3765 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
Tom Cosgrove87d9c6c2022-09-22 09:27:56 +01003766 ret = 0;
Ronald Cron9f0fba32022-02-10 16:45:15 +01003767 break;
Jerry Yub9930e72021-08-06 17:11:51 +08003768
Ronald Cron9f0fba32022-02-10 16:45:15 +01003769 case MBEDTLS_SSL_CLIENT_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01003770 ret = mbedtls_ssl_write_client_hello(ssl);
Ronald Cron9f0fba32022-02-10 16:45:15 +01003771 break;
3772
3773 default:
3774#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01003775 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
3776 ret = mbedtls_ssl_tls13_handshake_client_step(ssl);
3777 } else {
3778 ret = mbedtls_ssl_handshake_client_step(ssl);
3779 }
Ronald Cron9f0fba32022-02-10 16:45:15 +01003780#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01003781 ret = mbedtls_ssl_handshake_client_step(ssl);
Ronald Cron9f0fba32022-02-10 16:45:15 +01003782#else
Gilles Peskine449bd832023-01-11 14:50:10 +01003783 ret = mbedtls_ssl_tls13_handshake_client_step(ssl);
Ronald Cron9f0fba32022-02-10 16:45:15 +01003784#endif
3785 }
Jerry Yub9930e72021-08-06 17:11:51 +08003786 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003787#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003788#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003789 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Ronald Cron6f135e12021-12-08 16:57:54 +01003790#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01003791 if (mbedtls_ssl_conf_is_tls13_only(ssl->conf)) {
3792 ret = mbedtls_ssl_tls13_handshake_server_step(ssl);
3793 }
Ronald Cron6f135e12021-12-08 16:57:54 +01003794#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yub9930e72021-08-06 17:11:51 +08003795
3796#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01003797 if (mbedtls_ssl_conf_is_tls12_only(ssl->conf)) {
3798 ret = mbedtls_ssl_handshake_server_step(ssl);
3799 }
Jerry Yub9930e72021-08-06 17:11:51 +08003800#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3801 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003802#endif
3803
Gilles Peskine449bd832023-01-11 14:50:10 +01003804 if (ret != 0) {
Jerry Yubbd5a3f2021-09-18 20:50:22 +08003805 /* handshake_step return error. And it is same
3806 * with alert_reason.
3807 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003808 if (ssl->send_alert) {
3809 ret = mbedtls_ssl_handle_pending_alert(ssl);
Jerry Yue7047812021-09-13 19:26:39 +08003810 goto cleanup;
3811 }
3812 }
3813
3814cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01003815 return ret;
Paul Bakker1961b702013-01-25 14:49:24 +01003816}
3817
3818/*
3819 * Perform the SSL handshake
3820 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003821int mbedtls_ssl_handshake(mbedtls_ssl_context *ssl)
Paul Bakker1961b702013-01-25 14:49:24 +01003822{
3823 int ret = 0;
3824
Hanno Beckera817ea42020-10-20 15:20:23 +01003825 /* Sanity checks */
3826
Gilles Peskine449bd832023-01-11 14:50:10 +01003827 if (ssl == NULL || ssl->conf == NULL) {
3828 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3829 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003830
Hanno Beckera817ea42020-10-20 15:20:23 +01003831#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003832 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3833 (ssl->f_set_timer == NULL || ssl->f_get_timer == NULL)) {
3834 MBEDTLS_SSL_DEBUG_MSG(1, ("You must use "
3835 "mbedtls_ssl_set_timer_cb() for DTLS"));
3836 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Beckera817ea42020-10-20 15:20:23 +01003837 }
3838#endif /* MBEDTLS_SSL_PROTO_DTLS */
3839
Gilles Peskine449bd832023-01-11 14:50:10 +01003840 MBEDTLS_SSL_DEBUG_MSG(2, ("=> handshake"));
Paul Bakker1961b702013-01-25 14:49:24 +01003841
Hanno Beckera817ea42020-10-20 15:20:23 +01003842 /* Main handshake loop */
Gilles Peskine449bd832023-01-11 14:50:10 +01003843 while (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER) {
3844 ret = mbedtls_ssl_handshake_step(ssl);
Paul Bakker1961b702013-01-25 14:49:24 +01003845
Gilles Peskine449bd832023-01-11 14:50:10 +01003846 if (ret != 0) {
Paul Bakker1961b702013-01-25 14:49:24 +01003847 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01003848 }
Paul Bakker1961b702013-01-25 14:49:24 +01003849 }
3850
Gilles Peskine449bd832023-01-11 14:50:10 +01003851 MBEDTLS_SSL_DEBUG_MSG(2, ("<= handshake"));
Paul Bakker5121ce52009-01-03 21:22:43 +00003852
Gilles Peskine449bd832023-01-11 14:50:10 +01003853 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003854}
3855
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003856#if defined(MBEDTLS_SSL_RENEGOTIATION)
3857#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003858/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003859 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00003860 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003861MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003862static int ssl_write_hello_request(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003863{
Janos Follath865b3eb2019-12-16 11:46:15 +00003864 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003865
Gilles Peskine449bd832023-01-11 14:50:10 +01003866 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write hello request"));
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003867
3868 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003869 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3870 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003871
Gilles Peskine449bd832023-01-11 14:50:10 +01003872 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
3873 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
3874 return ret;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003875 }
3876
Gilles Peskine449bd832023-01-11 14:50:10 +01003877 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write hello request"));
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003878
Gilles Peskine449bd832023-01-11 14:50:10 +01003879 return 0;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003880}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003881#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003882
3883/*
3884 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003885 * - any side: calling mbedtls_ssl_renegotiate(),
3886 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
3887 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02003888 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003889 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003890 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003891 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003892int mbedtls_ssl_start_renegotiation(mbedtls_ssl_context *ssl)
Paul Bakker48916f92012-09-16 19:57:18 +00003893{
Janos Follath865b3eb2019-12-16 11:46:15 +00003894 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker48916f92012-09-16 19:57:18 +00003895
Gilles Peskine449bd832023-01-11 14:50:10 +01003896 MBEDTLS_SSL_DEBUG_MSG(2, ("=> renegotiate"));
Paul Bakker48916f92012-09-16 19:57:18 +00003897
Gilles Peskine449bd832023-01-11 14:50:10 +01003898 if ((ret = ssl_handshake_init(ssl)) != 0) {
3899 return ret;
3900 }
Paul Bakker48916f92012-09-16 19:57:18 +00003901
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003902 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
3903 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003904#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003905 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3906 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING) {
3907 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003908 ssl->handshake->out_msg_seq = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003909 } else {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003910 ssl->handshake->in_msg_seq = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003911 }
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003912 }
3913#endif
3914
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003915 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
3916 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00003917
Gilles Peskine449bd832023-01-11 14:50:10 +01003918 if ((ret = mbedtls_ssl_handshake(ssl)) != 0) {
3919 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_handshake", ret);
3920 return ret;
Paul Bakker48916f92012-09-16 19:57:18 +00003921 }
3922
Gilles Peskine449bd832023-01-11 14:50:10 +01003923 MBEDTLS_SSL_DEBUG_MSG(2, ("<= renegotiate"));
Paul Bakker48916f92012-09-16 19:57:18 +00003924
Gilles Peskine449bd832023-01-11 14:50:10 +01003925 return 0;
Paul Bakker48916f92012-09-16 19:57:18 +00003926}
3927
3928/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003929 * Renegotiate current connection on client,
3930 * or request renegotiation on server
3931 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003932int mbedtls_ssl_renegotiate(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003933{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003934 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003935
Gilles Peskine449bd832023-01-11 14:50:10 +01003936 if (ssl == NULL || ssl->conf == NULL) {
3937 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3938 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003939
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003940#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003941 /* On server, just send the request */
Gilles Peskine449bd832023-01-11 14:50:10 +01003942 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
3943 if (mbedtls_ssl_is_handshake_over(ssl) == 0) {
3944 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3945 }
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003946
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003947 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02003948
3949 /* Did we already try/start sending HelloRequest? */
Gilles Peskine449bd832023-01-11 14:50:10 +01003950 if (ssl->out_left != 0) {
3951 return mbedtls_ssl_flush_output(ssl);
3952 }
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02003953
Gilles Peskine449bd832023-01-11 14:50:10 +01003954 return ssl_write_hello_request(ssl);
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003955 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003956#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003957
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003958#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003959 /*
3960 * On client, either start the renegotiation process or,
3961 * if already in progress, continue the handshake
3962 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003963 if (ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
3964 if (mbedtls_ssl_is_handshake_over(ssl) == 0) {
3965 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003966 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003967
3968 if ((ret = mbedtls_ssl_start_renegotiation(ssl)) != 0) {
3969 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_start_renegotiation", ret);
3970 return ret;
3971 }
3972 } else {
3973 if ((ret = mbedtls_ssl_handshake(ssl)) != 0) {
3974 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_handshake", ret);
3975 return ret;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003976 }
3977 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003978#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003979
Gilles Peskine449bd832023-01-11 14:50:10 +01003980 return ret;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003981}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003982#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003983
Gilles Peskine449bd832023-01-11 14:50:10 +01003984void mbedtls_ssl_handshake_free(mbedtls_ssl_context *ssl)
Paul Bakker48916f92012-09-16 19:57:18 +00003985{
Gilles Peskine9b562d52018-04-25 20:32:43 +02003986 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
3987
Gilles Peskine449bd832023-01-11 14:50:10 +01003988 if (handshake == NULL) {
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003989 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01003990 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003991
Brett Warrene0edc842021-08-17 09:53:13 +01003992#if defined(MBEDTLS_ECP_C)
3993#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003994 if (ssl->handshake->group_list_heap_allocated) {
3995 mbedtls_free((void *) handshake->group_list);
3996 }
Brett Warrene0edc842021-08-17 09:53:13 +01003997 handshake->group_list = NULL;
3998#endif /* MBEDTLS_DEPRECATED_REMOVED */
3999#endif /* MBEDTLS_ECP_C */
4000
Ronald Crone68ab4f2022-10-05 12:46:29 +02004001#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +08004002#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01004003 if (ssl->handshake->sig_algs_heap_allocated) {
4004 mbedtls_free((void *) handshake->sig_algs);
4005 }
Jerry Yuf017ee42022-01-12 15:49:48 +08004006 handshake->sig_algs = NULL;
4007#endif /* MBEDTLS_DEPRECATED_REMOVED */
Xiaofei Baic234ecf2022-02-08 09:59:23 +00004008#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01004009 if (ssl->handshake->certificate_request_context) {
4010 mbedtls_free((void *) handshake->certificate_request_context);
Xiaofei Baic234ecf2022-02-08 09:59:23 +00004011 }
4012#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Ronald Crone68ab4f2022-10-05 12:46:29 +02004013#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yuf017ee42022-01-12 15:49:48 +08004014
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02004015#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01004016 if (ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0) {
4017 ssl->conf->f_async_cancel(ssl);
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02004018 handshake->async_in_progress = 0;
4019 }
4020#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
4021
Andrzej Kurek25f27152022-08-17 16:09:31 -04004022#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -05004023#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01004024 psa_hash_abort(&handshake->fin_sha256_psa);
Andrzej Kurekeb342242019-01-29 09:14:33 -05004025#else
Gilles Peskine449bd832023-01-11 14:50:10 +01004026 mbedtls_sha256_free(&handshake->fin_sha256);
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02004027#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05004028#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004029#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -05004030#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01004031 psa_hash_abort(&handshake->fin_sha384_psa);
Andrzej Kurekeb342242019-01-29 09:14:33 -05004032#else
Gilles Peskine449bd832023-01-11 14:50:10 +01004033 mbedtls_sha512_free(&handshake->fin_sha384);
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02004034#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05004035#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02004036
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004037#if defined(MBEDTLS_DHM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004038 mbedtls_dhm_free(&handshake->dhm_ctx);
Paul Bakker48916f92012-09-16 19:57:18 +00004039#endif
Neil Armstrongf3f46412022-04-12 14:43:39 +02004040#if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004041 mbedtls_ecdh_free(&handshake->ecdh_ctx);
Paul Bakker61d113b2013-07-04 11:51:43 +02004042#endif
Valerio Setti02c25b52022-11-15 14:08:42 +01004043
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02004044#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Neil Armstrongca7d5062022-05-31 14:43:23 +02004045#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01004046 psa_pake_abort(&handshake->psa_pake_ctx);
Valerio Settieb3f7882022-12-08 18:42:58 +01004047 /*
4048 * Opaque keys are not stored in the handshake's data and it's the user
4049 * responsibility to destroy them. Clear ones, instead, are created by
4050 * the TLS library and should be destroyed at the same level
4051 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004052 if (!mbedtls_svc_key_id_is_null(handshake->psa_pake_password)) {
4053 psa_destroy_key(handshake->psa_pake_password);
Valerio Settieb3f7882022-12-08 18:42:58 +01004054 }
Neil Armstrongca7d5062022-05-31 14:43:23 +02004055 handshake->psa_pake_password = MBEDTLS_SVC_KEY_ID_INIT;
4056#else
Gilles Peskine449bd832023-01-11 14:50:10 +01004057 mbedtls_ecjpake_free(&handshake->ecjpake_ctx);
Neil Armstrongca7d5062022-05-31 14:43:23 +02004058#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02004059#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004060 mbedtls_free(handshake->ecjpake_cache);
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02004061 handshake->ecjpake_cache = NULL;
4062 handshake->ecjpake_cache_len = 0;
4063#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02004064#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02004065
Janos Follath4ae5c292016-02-10 11:27:43 +00004066#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
4067 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02004068 /* explicit void pointer cast for buggy MS compiler */
Gilles Peskine449bd832023-01-11 14:50:10 +01004069 mbedtls_free((void *) handshake->curves_tls_id);
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004070#endif
4071
Ronald Cron73fe8df2022-10-05 14:31:43 +02004072#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02004073#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01004074 if (!mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque)) {
Neil Armstrong501c9322022-05-03 09:35:09 +02004075 /* The maintenance of the external PSK key slot is the
4076 * user's responsibility. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004077 if (ssl->handshake->psk_opaque_is_internal) {
4078 psa_destroy_key(ssl->handshake->psk_opaque);
Neil Armstrong501c9322022-05-03 09:35:09 +02004079 ssl->handshake->psk_opaque_is_internal = 0;
4080 }
4081 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
4082 }
Neil Armstronge952a302022-05-03 10:22:14 +02004083#else
Gilles Peskine449bd832023-01-11 14:50:10 +01004084 if (handshake->psk != NULL) {
4085 mbedtls_platform_zeroize(handshake->psk, handshake->psk_len);
4086 mbedtls_free(handshake->psk);
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004087 }
Neil Armstronge952a302022-05-03 10:22:14 +02004088#endif /* MBEDTLS_USE_PSA_CRYPTO */
Ronald Cron73fe8df2022-10-05 14:31:43 +02004089#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004090
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004091#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
4092 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02004093 /*
4094 * Free only the linked list wrapper, not the keys themselves
4095 * since the belong to the SNI callback
4096 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004097 ssl_key_cert_free(handshake->sni_key_cert);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004098#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004099
Gilles Peskineeccd8882020-03-10 12:19:08 +01004100#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01004101 mbedtls_x509_crt_restart_free(&handshake->ecrs_ctx);
4102 if (handshake->ecrs_peer_cert != NULL) {
4103 mbedtls_x509_crt_free(handshake->ecrs_peer_cert);
4104 mbedtls_free(handshake->ecrs_peer_cert);
Hanno Becker3dad3112019-02-05 17:19:52 +00004105 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02004106#endif
4107
Hanno Becker75173122019-02-06 16:18:31 +00004108#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
4109 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01004110 mbedtls_pk_free(&handshake->peer_pubkey);
Hanno Becker75173122019-02-06 16:18:31 +00004111#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
4112
XiaokangQian9b93c0d2022-02-09 06:02:25 +00004113#if defined(MBEDTLS_SSL_CLI_C) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01004114 (defined(MBEDTLS_SSL_PROTO_DTLS) || defined(MBEDTLS_SSL_PROTO_TLS1_3))
4115 mbedtls_free(handshake->cookie);
XiaokangQian9b93c0d2022-02-09 06:02:25 +00004116#endif /* MBEDTLS_SSL_CLI_C &&
4117 ( MBEDTLS_SSL_PROTO_DTLS || MBEDTLS_SSL_PROTO_TLS1_3 ) */
XiaokangQian8499b6c2022-01-27 09:00:11 +00004118
4119#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004120 mbedtls_ssl_flight_free(handshake->flight);
4121 mbedtls_ssl_buffering_free(ssl);
XiaokangQian8499b6c2022-01-27 09:00:11 +00004122#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02004123
Ronald Cronf12b81d2022-03-15 10:42:41 +01004124#if defined(MBEDTLS_ECDH_C) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01004125 (defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3))
4126 if (handshake->ecdh_psa_privkey_is_external == 0) {
4127 psa_destroy_key(handshake->ecdh_psa_privkey);
4128 }
Hanno Becker4a63ed42019-01-08 11:39:35 +00004129#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
4130
Ronald Cron6f135e12021-12-08 16:57:54 +01004131#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01004132 mbedtls_ssl_transform_free(handshake->transform_handshake);
4133 mbedtls_free(handshake->transform_handshake);
Jerry Yu3d9b5902022-11-04 14:07:25 +08004134#if defined(MBEDTLS_SSL_EARLY_DATA)
Gilles Peskine449bd832023-01-11 14:50:10 +01004135 mbedtls_ssl_transform_free(handshake->transform_earlydata);
4136 mbedtls_free(handshake->transform_earlydata);
Jerry Yu3d9b5902022-11-04 14:07:25 +08004137#endif
Ronald Cron6f135e12021-12-08 16:57:54 +01004138#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yuba9c7272021-10-30 11:54:10 +08004139
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004140
4141#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4142 /* If the buffers are too big - reallocate. Because of the way Mbed TLS
4143 * processes datagrams and the fact that a datagram is allowed to have
4144 * several records in it, it is possible that the I/O buffers are not
4145 * empty at this stage */
Gilles Peskine449bd832023-01-11 14:50:10 +01004146 handle_buffer_resizing(ssl, 1, mbedtls_ssl_get_input_buflen(ssl),
4147 mbedtls_ssl_get_output_buflen(ssl));
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004148#endif
Hanno Becker3aa186f2021-08-10 09:24:19 +01004149
Jerry Yuba9c7272021-10-30 11:54:10 +08004150 /* mbedtls_platform_zeroize MUST be last one in this function */
Gilles Peskine449bd832023-01-11 14:50:10 +01004151 mbedtls_platform_zeroize(handshake,
4152 sizeof(mbedtls_ssl_handshake_params));
Paul Bakker48916f92012-09-16 19:57:18 +00004153}
4154
Gilles Peskine449bd832023-01-11 14:50:10 +01004155void mbedtls_ssl_session_free(mbedtls_ssl_session *session)
Paul Bakker48916f92012-09-16 19:57:18 +00004156{
Gilles Peskine449bd832023-01-11 14:50:10 +01004157 if (session == NULL) {
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004158 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01004159 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004160
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004161#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004162 ssl_clear_peer_cert(session);
Paul Bakkered27a042013-04-18 22:46:23 +02004163#endif
Paul Bakker0a597072012-09-25 21:55:46 +00004164
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02004165#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Xiaokang Qianbc663a02022-10-09 11:14:39 +00004166#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
4167 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01004168 mbedtls_free(session->hostname);
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00004169#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01004170 mbedtls_free(session->ticket);
Paul Bakkera503a632013-08-14 13:48:06 +02004171#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004172
Gilles Peskine449bd832023-01-11 14:50:10 +01004173 mbedtls_platform_zeroize(session, sizeof(mbedtls_ssl_session));
Paul Bakker48916f92012-09-16 19:57:18 +00004174}
4175
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02004176#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004177
4178#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4179#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u
4180#else
4181#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u
4182#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4183
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004184#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004185
4186#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4187#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u
4188#else
4189#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u
4190#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4191
4192#if defined(MBEDTLS_SSL_ALPN)
4193#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u
4194#else
4195#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u
4196#endif /* MBEDTLS_SSL_ALPN */
4197
4198#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0
4199#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1
4200#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2
4201#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3
4202
4203#define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \
Gilles Peskine449bd832023-01-11 14:50:10 +01004204 ((uint32_t) ( \
4205 (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << \
4206 SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT) | \
4207 (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << \
4208 SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT) | \
4209 (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << \
4210 SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT) | \
4211 (SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT) | \
4212 0u))
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004213
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004214static unsigned char ssl_serialized_context_header[] = {
4215 MBEDTLS_VERSION_MAJOR,
4216 MBEDTLS_VERSION_MINOR,
4217 MBEDTLS_VERSION_PATCH,
Gilles Peskine449bd832023-01-11 14:50:10 +01004218 MBEDTLS_BYTE_1(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
4219 MBEDTLS_BYTE_0(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
4220 MBEDTLS_BYTE_2(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG),
4221 MBEDTLS_BYTE_1(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG),
4222 MBEDTLS_BYTE_0(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG),
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004223};
4224
Paul Bakker5121ce52009-01-03 21:22:43 +00004225/*
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004226 * Serialize a full SSL context
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02004227 *
4228 * The format of the serialized data is:
4229 * (in the presentation language of TLS, RFC 8446 section 3)
4230 *
4231 * // header
4232 * opaque mbedtls_version[3]; // major, minor, patch
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004233 * opaque context_format[5]; // version-specific field determining
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02004234 * // the format of the remaining
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004235 * // serialized data.
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004236 * Note: When updating the format, remember to keep these
4237 * version+format bytes. (We may make their size part of the API.)
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02004238 *
4239 * // session sub-structure
4240 * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save()
4241 * // transform sub-structure
4242 * uint8 random[64]; // ServerHello.random+ClientHello.random
4243 * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value
4244 * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use
4245 * // fields from ssl_context
4246 * uint32 badmac_seen; // DTLS: number of records with failing MAC
4247 * uint64 in_window_top; // DTLS: last validated record seq_num
4248 * uint64 in_window; // DTLS: bitmask for replay protection
4249 * uint8 disable_datagram_packing; // DTLS: only one record per datagram
4250 * uint64 cur_out_ctr; // Record layer: outgoing sequence number
4251 * uint16 mtu; // DTLS: path mtu (max outgoing fragment size)
4252 * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol
4253 *
4254 * Note that many fields of the ssl_context or sub-structures are not
4255 * serialized, as they fall in one of the following categories:
4256 *
4257 * 1. forced value (eg in_left must be 0)
4258 * 2. pointer to dynamically-allocated memory (eg session, transform)
4259 * 3. value can be re-derived from other data (eg session keys from MS)
4260 * 4. value was temporary (eg content of input buffer)
4261 * 5. value will be provided by the user again (eg I/O callbacks and context)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004262 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004263int mbedtls_ssl_context_save(mbedtls_ssl_context *ssl,
4264 unsigned char *buf,
4265 size_t buf_len,
4266 size_t *olen)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004267{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004268 unsigned char *p = buf;
4269 size_t used = 0;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004270 size_t session_len;
4271 int ret = 0;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004272
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02004273 /*
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004274 * Enforce usage restrictions, see "return BAD_INPUT_DATA" in
4275 * this function's documentation.
4276 *
4277 * These are due to assumptions/limitations in the implementation. Some of
4278 * them are likely to stay (no handshake in progress) some might go away
4279 * (only DTLS) but are currently used to simplify the implementation.
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02004280 */
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004281 /* The initial handshake must be over */
Gilles Peskine449bd832023-01-11 14:50:10 +01004282 if (mbedtls_ssl_is_handshake_over(ssl) == 0) {
4283 MBEDTLS_SSL_DEBUG_MSG(1, ("Initial handshake isn't over"));
4284 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004285 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004286 if (ssl->handshake != NULL) {
4287 MBEDTLS_SSL_DEBUG_MSG(1, ("Handshake isn't completed"));
4288 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004289 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004290 /* Double-check that sub-structures are indeed ready */
Gilles Peskine449bd832023-01-11 14:50:10 +01004291 if (ssl->transform == NULL || ssl->session == NULL) {
4292 MBEDTLS_SSL_DEBUG_MSG(1, ("Serialised structures aren't ready"));
4293 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004294 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004295 /* There must be no pending incoming or outgoing data */
Gilles Peskine449bd832023-01-11 14:50:10 +01004296 if (mbedtls_ssl_check_pending(ssl) != 0) {
4297 MBEDTLS_SSL_DEBUG_MSG(1, ("There is pending incoming data"));
4298 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004299 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004300 if (ssl->out_left != 0) {
4301 MBEDTLS_SSL_DEBUG_MSG(1, ("There is pending outgoing data"));
4302 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004303 }
Dave Rodgman556e8a32022-12-06 16:31:25 +00004304 /* Protocol must be DTLS, not TLS */
Gilles Peskine449bd832023-01-11 14:50:10 +01004305 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
4306 MBEDTLS_SSL_DEBUG_MSG(1, ("Only DTLS is supported"));
4307 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004308 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004309 /* Version must be 1.2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004310 if (ssl->tls_version != MBEDTLS_SSL_VERSION_TLS1_2) {
4311 MBEDTLS_SSL_DEBUG_MSG(1, ("Only version 1.2 supported"));
4312 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004313 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004314 /* We must be using an AEAD ciphersuite */
Gilles Peskine449bd832023-01-11 14:50:10 +01004315 if (mbedtls_ssl_transform_uses_aead(ssl->transform) != 1) {
4316 MBEDTLS_SSL_DEBUG_MSG(1, ("Only AEAD ciphersuites supported"));
4317 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004318 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004319 /* Renegotiation must not be enabled */
4320#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01004321 if (ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED) {
4322 MBEDTLS_SSL_DEBUG_MSG(1, ("Renegotiation must not be enabled"));
4323 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004324 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004325#endif
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004326
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004327 /*
4328 * Version and format identifier
4329 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004330 used += sizeof(ssl_serialized_context_header);
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004331
Gilles Peskine449bd832023-01-11 14:50:10 +01004332 if (used <= buf_len) {
4333 memcpy(p, ssl_serialized_context_header,
4334 sizeof(ssl_serialized_context_header));
4335 p += sizeof(ssl_serialized_context_header);
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004336 }
4337
4338 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004339 * Session (length + data)
4340 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004341 ret = ssl_session_save(ssl->session, 1, NULL, 0, &session_len);
4342 if (ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL) {
4343 return ret;
4344 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004345
4346 used += 4 + session_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004347 if (used <= buf_len) {
4348 MBEDTLS_PUT_UINT32_BE(session_len, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004349 p += 4;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004350
Gilles Peskine449bd832023-01-11 14:50:10 +01004351 ret = ssl_session_save(ssl->session, 1,
4352 p, session_len, &session_len);
4353 if (ret != 0) {
4354 return ret;
4355 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004356
4357 p += session_len;
4358 }
4359
4360 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004361 * Transform
4362 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004363 used += sizeof(ssl->transform->randbytes);
4364 if (used <= buf_len) {
4365 memcpy(p, ssl->transform->randbytes,
4366 sizeof(ssl->transform->randbytes));
4367 p += sizeof(ssl->transform->randbytes);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004368 }
4369
4370#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4371 used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004372 if (used <= buf_len) {
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004373 *p++ = ssl->transform->in_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004374 memcpy(p, ssl->transform->in_cid, ssl->transform->in_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004375 p += ssl->transform->in_cid_len;
4376
4377 *p++ = ssl->transform->out_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004378 memcpy(p, ssl->transform->out_cid, ssl->transform->out_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004379 p += ssl->transform->out_cid_len;
4380 }
4381#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4382
4383 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004384 * Saved fields from top-level ssl_context structure
4385 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004386 used += 4;
Gilles Peskine449bd832023-01-11 14:50:10 +01004387 if (used <= buf_len) {
4388 MBEDTLS_PUT_UINT32_BE(ssl->badmac_seen, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004389 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004390 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004391
4392#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4393 used += 16;
Gilles Peskine449bd832023-01-11 14:50:10 +01004394 if (used <= buf_len) {
4395 MBEDTLS_PUT_UINT64_BE(ssl->in_window_top, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004396 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004397
Gilles Peskine449bd832023-01-11 14:50:10 +01004398 MBEDTLS_PUT_UINT64_BE(ssl->in_window, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004399 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004400 }
4401#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4402
4403#if defined(MBEDTLS_SSL_PROTO_DTLS)
4404 used += 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004405 if (used <= buf_len) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004406 *p++ = ssl->disable_datagram_packing;
4407 }
4408#endif /* MBEDTLS_SSL_PROTO_DTLS */
4409
Jerry Yuae0b2e22021-10-08 15:21:19 +08004410 used += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Gilles Peskine449bd832023-01-11 14:50:10 +01004411 if (used <= buf_len) {
4412 memcpy(p, ssl->cur_out_ctr, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN);
Jerry Yuae0b2e22021-10-08 15:21:19 +08004413 p += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004414 }
4415
4416#if defined(MBEDTLS_SSL_PROTO_DTLS)
4417 used += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01004418 if (used <= buf_len) {
4419 MBEDTLS_PUT_UINT16_BE(ssl->mtu, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004420 p += 2;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004421 }
4422#endif /* MBEDTLS_SSL_PROTO_DTLS */
4423
4424#if defined(MBEDTLS_SSL_ALPN)
4425 {
4426 const uint8_t alpn_len = ssl->alpn_chosen
Gilles Peskine449bd832023-01-11 14:50:10 +01004427 ? (uint8_t) strlen(ssl->alpn_chosen)
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004428 : 0;
4429
4430 used += 1 + alpn_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004431 if (used <= buf_len) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004432 *p++ = alpn_len;
4433
Gilles Peskine449bd832023-01-11 14:50:10 +01004434 if (ssl->alpn_chosen != NULL) {
4435 memcpy(p, ssl->alpn_chosen, alpn_len);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004436 p += alpn_len;
4437 }
4438 }
4439 }
4440#endif /* MBEDTLS_SSL_ALPN */
4441
4442 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004443 * Done
4444 */
4445 *olen = used;
4446
Gilles Peskine449bd832023-01-11 14:50:10 +01004447 if (used > buf_len) {
4448 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
4449 }
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004450
Gilles Peskine449bd832023-01-11 14:50:10 +01004451 MBEDTLS_SSL_DEBUG_BUF(4, "saved context", buf, used);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004452
Gilles Peskine449bd832023-01-11 14:50:10 +01004453 return mbedtls_ssl_session_reset_int(ssl, 0);
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004454}
4455
4456/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02004457 * Deserialize context, see mbedtls_ssl_context_save() for format.
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004458 *
4459 * This internal version is wrapped by a public function that cleans up in
4460 * case of error.
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004461 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004462MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01004463static int ssl_context_load(mbedtls_ssl_context *ssl,
4464 const unsigned char *buf,
4465 size_t len)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004466{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004467 const unsigned char *p = buf;
4468 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004469 size_t session_len;
Janos Follath865b3eb2019-12-16 11:46:15 +00004470 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004471#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek894edde2022-09-29 06:31:14 -04004472 tls_prf_fn prf_func = NULL;
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004473#endif
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004474
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004475 /*
4476 * The context should have been freshly setup or reset.
4477 * Give the user an error in case of obvious misuse.
Manuel Pégourié-Gonnard4ca930f2019-07-26 16:31:53 +02004478 * (Checking session is useful because it won't be NULL if we're
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004479 * renegotiating, or if the user mistakenly loaded a session first.)
4480 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004481 if (ssl->state != MBEDTLS_SSL_HELLO_REQUEST ||
4482 ssl->session != NULL) {
4483 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004484 }
4485
4486 /*
4487 * We can't check that the config matches the initial one, but we can at
4488 * least check it matches the requirements for serializing.
4489 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004490 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
Glenn Strauss2dfcea22022-03-14 17:26:42 -04004491 ssl->conf->max_tls_version < MBEDTLS_SSL_VERSION_TLS1_2 ||
4492 ssl->conf->min_tls_version > MBEDTLS_SSL_VERSION_TLS1_2 ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004493#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02004494 ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004495#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01004496 0) {
4497 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004498 }
4499
Gilles Peskine449bd832023-01-11 14:50:10 +01004500 MBEDTLS_SSL_DEBUG_BUF(4, "context to load", buf, len);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004501
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004502 /*
4503 * Check version identifier
4504 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004505 if ((size_t) (end - p) < sizeof(ssl_serialized_context_header)) {
4506 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004507 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004508
4509 if (memcmp(p, ssl_serialized_context_header,
4510 sizeof(ssl_serialized_context_header)) != 0) {
4511 return MBEDTLS_ERR_SSL_VERSION_MISMATCH;
4512 }
4513 p += sizeof(ssl_serialized_context_header);
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004514
4515 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004516 * Session
4517 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004518 if ((size_t) (end - p) < 4) {
4519 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4520 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004521
Gilles Peskine449bd832023-01-11 14:50:10 +01004522 session_len = ((size_t) p[0] << 24) |
4523 ((size_t) p[1] << 16) |
4524 ((size_t) p[2] << 8) |
4525 ((size_t) p[3]);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004526 p += 4;
4527
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004528 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00004529 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004530 ssl->session = ssl->session_negotiate;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004531 ssl->session_in = ssl->session;
4532 ssl->session_out = ssl->session;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004533 ssl->session_negotiate = NULL;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004534
Gilles Peskine449bd832023-01-11 14:50:10 +01004535 if ((size_t) (end - p) < session_len) {
4536 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4537 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004538
Gilles Peskine449bd832023-01-11 14:50:10 +01004539 ret = ssl_session_load(ssl->session, 1, p, session_len);
4540 if (ret != 0) {
4541 mbedtls_ssl_session_free(ssl->session);
4542 return ret;
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004543 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004544
4545 p += session_len;
4546
4547 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004548 * Transform
4549 */
4550
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004551 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00004552 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Jerry Yu2e199812022-12-01 18:57:19 +08004553#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004554 ssl->transform = ssl->transform_negotiate;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004555 ssl->transform_in = ssl->transform;
4556 ssl->transform_out = ssl->transform;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004557 ssl->transform_negotiate = NULL;
Jerry Yu2e199812022-12-01 18:57:19 +08004558#endif
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004559
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004560#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01004561 prf_func = ssl_tls12prf_from_cs(ssl->session->ciphersuite);
4562 if (prf_func == NULL) {
4563 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4564 }
Andrzej Kurek894edde2022-09-29 06:31:14 -04004565
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004566 /* Read random bytes and populate structure */
Gilles Peskine449bd832023-01-11 14:50:10 +01004567 if ((size_t) (end - p) < sizeof(ssl->transform->randbytes)) {
4568 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4569 }
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004570
Gilles Peskine449bd832023-01-11 14:50:10 +01004571 ret = ssl_tls12_populate_transform(ssl->transform,
4572 ssl->session->ciphersuite,
4573 ssl->session->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02004574#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004575 ssl->session->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02004576#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01004577 prf_func,
4578 p, /* currently pointing to randbytes */
4579 MBEDTLS_SSL_VERSION_TLS1_2, /* (D)TLS 1.2 is forced */
4580 ssl->conf->endpoint,
4581 ssl);
4582 if (ret != 0) {
4583 return ret;
4584 }
Jerry Yu840fbb22022-02-17 14:59:29 +08004585#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004586 p += sizeof(ssl->transform->randbytes);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004587
4588#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4589 /* Read connection IDs and store them */
Gilles Peskine449bd832023-01-11 14:50:10 +01004590 if ((size_t) (end - p) < 1) {
4591 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4592 }
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004593
4594 ssl->transform->in_cid_len = *p++;
4595
Gilles Peskine449bd832023-01-11 14:50:10 +01004596 if ((size_t) (end - p) < ssl->transform->in_cid_len + 1u) {
4597 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4598 }
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004599
Gilles Peskine449bd832023-01-11 14:50:10 +01004600 memcpy(ssl->transform->in_cid, p, ssl->transform->in_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004601 p += ssl->transform->in_cid_len;
4602
4603 ssl->transform->out_cid_len = *p++;
4604
Gilles Peskine449bd832023-01-11 14:50:10 +01004605 if ((size_t) (end - p) < ssl->transform->out_cid_len) {
4606 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4607 }
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004608
Gilles Peskine449bd832023-01-11 14:50:10 +01004609 memcpy(ssl->transform->out_cid, p, ssl->transform->out_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004610 p += ssl->transform->out_cid_len;
4611#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4612
4613 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004614 * Saved fields from top-level ssl_context structure
4615 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004616 if ((size_t) (end - p) < 4) {
4617 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4618 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004619
Gilles Peskine449bd832023-01-11 14:50:10 +01004620 ssl->badmac_seen = ((uint32_t) p[0] << 24) |
4621 ((uint32_t) p[1] << 16) |
4622 ((uint32_t) p[2] << 8) |
4623 ((uint32_t) p[3]);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004624 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004625
4626#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine449bd832023-01-11 14:50:10 +01004627 if ((size_t) (end - p) < 16) {
4628 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4629 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004630
Gilles Peskine449bd832023-01-11 14:50:10 +01004631 ssl->in_window_top = ((uint64_t) p[0] << 56) |
4632 ((uint64_t) p[1] << 48) |
4633 ((uint64_t) p[2] << 40) |
4634 ((uint64_t) p[3] << 32) |
4635 ((uint64_t) p[4] << 24) |
4636 ((uint64_t) p[5] << 16) |
4637 ((uint64_t) p[6] << 8) |
4638 ((uint64_t) p[7]);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004639 p += 8;
4640
Gilles Peskine449bd832023-01-11 14:50:10 +01004641 ssl->in_window = ((uint64_t) p[0] << 56) |
4642 ((uint64_t) p[1] << 48) |
4643 ((uint64_t) p[2] << 40) |
4644 ((uint64_t) p[3] << 32) |
4645 ((uint64_t) p[4] << 24) |
4646 ((uint64_t) p[5] << 16) |
4647 ((uint64_t) p[6] << 8) |
4648 ((uint64_t) p[7]);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004649 p += 8;
4650#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4651
4652#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004653 if ((size_t) (end - p) < 1) {
4654 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4655 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004656
4657 ssl->disable_datagram_packing = *p++;
4658#endif /* MBEDTLS_SSL_PROTO_DTLS */
4659
Gilles Peskine449bd832023-01-11 14:50:10 +01004660 if ((size_t) (end - p) < sizeof(ssl->cur_out_ctr)) {
4661 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4662 }
4663 memcpy(ssl->cur_out_ctr, p, sizeof(ssl->cur_out_ctr));
4664 p += sizeof(ssl->cur_out_ctr);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004665
4666#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004667 if ((size_t) (end - p) < 2) {
4668 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4669 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004670
Gilles Peskine449bd832023-01-11 14:50:10 +01004671 ssl->mtu = (p[0] << 8) | p[1];
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004672 p += 2;
4673#endif /* MBEDTLS_SSL_PROTO_DTLS */
4674
4675#if defined(MBEDTLS_SSL_ALPN)
4676 {
4677 uint8_t alpn_len;
4678 const char **cur;
4679
Gilles Peskine449bd832023-01-11 14:50:10 +01004680 if ((size_t) (end - p) < 1) {
4681 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4682 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004683
4684 alpn_len = *p++;
4685
Gilles Peskine449bd832023-01-11 14:50:10 +01004686 if (alpn_len != 0 && ssl->conf->alpn_list != NULL) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004687 /* alpn_chosen should point to an item in the configured list */
Gilles Peskine449bd832023-01-11 14:50:10 +01004688 for (cur = ssl->conf->alpn_list; *cur != NULL; cur++) {
4689 if (strlen(*cur) == alpn_len &&
4690 memcmp(p, cur, alpn_len) == 0) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004691 ssl->alpn_chosen = *cur;
4692 break;
4693 }
4694 }
4695 }
4696
4697 /* can only happen on conf mismatch */
Gilles Peskine449bd832023-01-11 14:50:10 +01004698 if (alpn_len != 0 && ssl->alpn_chosen == NULL) {
4699 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4700 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004701
4702 p += alpn_len;
4703 }
4704#endif /* MBEDTLS_SSL_ALPN */
4705
4706 /*
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004707 * Forced fields from top-level ssl_context structure
4708 *
4709 * Most of them already set to the correct value by mbedtls_ssl_init() and
4710 * mbedtls_ssl_reset(), so we only need to set the remaining ones.
4711 */
4712 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
Glenn Strauss60bfe602022-03-14 19:04:24 -04004713 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004714
Hanno Becker361b10d2019-08-30 10:42:49 +01004715 /* Adjust pointers for header fields of outgoing records to
4716 * the given transform, accounting for explicit IV and CID. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004717 mbedtls_ssl_update_out_pointers(ssl, ssl->transform);
Hanno Becker361b10d2019-08-30 10:42:49 +01004718
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004719#if defined(MBEDTLS_SSL_PROTO_DTLS)
4720 ssl->in_epoch = 1;
4721#endif
4722
4723 /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated,
4724 * which we don't want - otherwise we'd end up freeing the wrong transform
Hanno Beckerce5f5fd2020-02-05 10:47:44 +00004725 * by calling mbedtls_ssl_handshake_wrapup_free_hs_transform()
4726 * inappropriately. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004727 if (ssl->handshake != NULL) {
4728 mbedtls_ssl_handshake_free(ssl);
4729 mbedtls_free(ssl->handshake);
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004730 ssl->handshake = NULL;
4731 }
4732
4733 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004734 * Done - should have consumed entire buffer
4735 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004736 if (p != end) {
4737 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4738 }
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004739
Gilles Peskine449bd832023-01-11 14:50:10 +01004740 return 0;
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004741}
4742
4743/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02004744 * Deserialize context: public wrapper for error cleaning
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004745 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004746int mbedtls_ssl_context_load(mbedtls_ssl_context *context,
4747 const unsigned char *buf,
4748 size_t len)
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004749{
Gilles Peskine449bd832023-01-11 14:50:10 +01004750 int ret = ssl_context_load(context, buf, len);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004751
Gilles Peskine449bd832023-01-11 14:50:10 +01004752 if (ret != 0) {
4753 mbedtls_ssl_free(context);
4754 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004755
Gilles Peskine449bd832023-01-11 14:50:10 +01004756 return ret;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004757}
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02004758#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004759
4760/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004761 * Free an SSL context
4762 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004763void mbedtls_ssl_free(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00004764{
Gilles Peskine449bd832023-01-11 14:50:10 +01004765 if (ssl == NULL) {
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004766 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01004767 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004768
Gilles Peskine449bd832023-01-11 14:50:10 +01004769 MBEDTLS_SSL_DEBUG_MSG(2, ("=> free"));
Paul Bakker5121ce52009-01-03 21:22:43 +00004770
Gilles Peskine449bd832023-01-11 14:50:10 +01004771 if (ssl->out_buf != NULL) {
sander-visserb8aa2072020-05-06 22:05:13 +02004772#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4773 size_t out_buf_len = ssl->out_buf_len;
4774#else
4775 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
4776#endif
4777
Gilles Peskine449bd832023-01-11 14:50:10 +01004778 mbedtls_platform_zeroize(ssl->out_buf, out_buf_len);
4779 mbedtls_free(ssl->out_buf);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004780 ssl->out_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004781 }
4782
Gilles Peskine449bd832023-01-11 14:50:10 +01004783 if (ssl->in_buf != NULL) {
sander-visserb8aa2072020-05-06 22:05:13 +02004784#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4785 size_t in_buf_len = ssl->in_buf_len;
4786#else
4787 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
4788#endif
4789
Gilles Peskine449bd832023-01-11 14:50:10 +01004790 mbedtls_platform_zeroize(ssl->in_buf, in_buf_len);
4791 mbedtls_free(ssl->in_buf);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004792 ssl->in_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004793 }
4794
Gilles Peskine449bd832023-01-11 14:50:10 +01004795 if (ssl->transform) {
4796 mbedtls_ssl_transform_free(ssl->transform);
4797 mbedtls_free(ssl->transform);
Paul Bakker48916f92012-09-16 19:57:18 +00004798 }
4799
Gilles Peskine449bd832023-01-11 14:50:10 +01004800 if (ssl->handshake) {
4801 mbedtls_ssl_handshake_free(ssl);
4802 mbedtls_free(ssl->handshake);
Jerry Yu2e199812022-12-01 18:57:19 +08004803
4804#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01004805 mbedtls_ssl_transform_free(ssl->transform_negotiate);
4806 mbedtls_free(ssl->transform_negotiate);
Jerry Yu2e199812022-12-01 18:57:19 +08004807#endif
4808
Gilles Peskine449bd832023-01-11 14:50:10 +01004809 mbedtls_ssl_session_free(ssl->session_negotiate);
4810 mbedtls_free(ssl->session_negotiate);
Paul Bakker48916f92012-09-16 19:57:18 +00004811 }
4812
Ronald Cron6f135e12021-12-08 16:57:54 +01004813#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01004814 mbedtls_ssl_transform_free(ssl->transform_application);
4815 mbedtls_free(ssl->transform_application);
Ronald Cron6f135e12021-12-08 16:57:54 +01004816#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker3aa186f2021-08-10 09:24:19 +01004817
Gilles Peskine449bd832023-01-11 14:50:10 +01004818 if (ssl->session) {
4819 mbedtls_ssl_session_free(ssl->session);
4820 mbedtls_free(ssl->session);
Paul Bakkerc0463502013-02-14 11:19:38 +01004821 }
4822
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02004823#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004824 if (ssl->hostname != NULL) {
4825 mbedtls_platform_zeroize(ssl->hostname, strlen(ssl->hostname));
4826 mbedtls_free(ssl->hostname);
Paul Bakker5121ce52009-01-03 21:22:43 +00004827 }
Paul Bakker0be444a2013-08-27 21:55:01 +02004828#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004829
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02004830#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004831 mbedtls_free(ssl->cli_id);
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02004832#endif
4833
Gilles Peskine449bd832023-01-11 14:50:10 +01004834 MBEDTLS_SSL_DEBUG_MSG(2, ("<= free"));
Paul Bakker2da561c2009-02-05 18:00:28 +00004835
Paul Bakker86f04f42013-02-14 11:20:09 +01004836 /* Actually clear after last debug message */
Gilles Peskine449bd832023-01-11 14:50:10 +01004837 mbedtls_platform_zeroize(ssl, sizeof(mbedtls_ssl_context));
Paul Bakker5121ce52009-01-03 21:22:43 +00004838}
4839
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004840/*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08004841 * Initialize mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004842 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004843void mbedtls_ssl_config_init(mbedtls_ssl_config *conf)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004844{
Gilles Peskine449bd832023-01-11 14:50:10 +01004845 memset(conf, 0, sizeof(mbedtls_ssl_config));
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004846}
4847
Gilles Peskineae270bf2021-06-02 00:05:29 +02004848/* The selection should be the same as mbedtls_x509_crt_profile_default in
4849 * x509_crt.c, plus Montgomery curves for ECDHE. Here, the order matters:
Gilles Peskineb1940a72021-06-02 15:18:12 +02004850 * curves with a lower resource usage come first.
Gilles Peskineae270bf2021-06-02 00:05:29 +02004851 * See the documentation of mbedtls_ssl_conf_curves() for what we promise
Gilles Peskineb1940a72021-06-02 15:18:12 +02004852 * about this list.
4853 */
Brett Warrene0edc842021-08-17 09:53:13 +01004854static uint16_t ssl_preset_default_groups[] = {
Gilles Peskineae270bf2021-06-02 00:05:29 +02004855#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004856 MBEDTLS_SSL_IANA_TLS_GROUP_X25519,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004857#endif
4858#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004859 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004860#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02004861#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004862 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004863#endif
4864#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004865 MBEDTLS_SSL_IANA_TLS_GROUP_X448,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004866#endif
4867#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004868 MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004869#endif
Gilles Peskineae270bf2021-06-02 00:05:29 +02004870#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004871 MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004872#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02004873#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004874 MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004875#endif
4876#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004877 MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004878#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004879 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Gilles Peskineae270bf2021-06-02 00:05:29 +02004880};
Gilles Peskineae270bf2021-06-02 00:05:29 +02004881
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004882static int ssl_preset_suiteb_ciphersuites[] = {
4883 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
4884 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
4885 0
4886};
4887
Ronald Crone68ab4f2022-10-05 12:46:29 +02004888#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004889
Jerry Yu909df7b2022-01-22 11:56:27 +08004890/* NOTICE:
Jerry Yu0b994b82022-01-25 17:22:12 +08004891 * For ssl_preset_*_sig_algs and ssl_tls12_preset_*_sig_algs, the following
Jerry Yu370e1462022-01-25 10:36:53 +08004892 * rules SHOULD be upheld.
4893 * - No duplicate entries.
4894 * - But if there is a good reason, do not change the order of the algorithms.
Jerry Yu09a99fc2022-07-28 14:22:17 +08004895 * - ssl_tls12_preset* is for TLS 1.2 use only.
Jerry Yu370e1462022-01-25 10:36:53 +08004896 * - ssl_preset_* is for TLS 1.3 only or hybrid TLS 1.3/1.2 handshakes.
Jerry Yu1a8b4812022-01-20 17:56:50 +08004897 */
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004898static uint16_t ssl_preset_default_sig_algs[] = {
Jerry Yu1a8b4812022-01-20 17:56:50 +08004899
Andrzej Kurek25f27152022-08-17 16:09:31 -04004900#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 +08004901 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
4902 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004903#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA &&
Jerry Yued5e9f42022-01-26 11:21:34 +08004904 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004905
Andrzej Kurek25f27152022-08-17 16:09:31 -04004906#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 +08004907 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
4908 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004909#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu909df7b2022-01-22 11:56:27 +08004910 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
4911
Andrzej Kurek25f27152022-08-17 16:09:31 -04004912#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 +08004913 defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
4914 MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004915#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yued5e9f42022-01-26 11:21:34 +08004916 MBEDTLS_ECP_DP_SECP521R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004917
Gilles Peskine449bd832023-01-11 14:50:10 +01004918#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && \
4919 defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu9bb3ee42022-06-23 10:16:33 +08004920 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
Gilles Peskine449bd832023-01-11 14:50:10 +01004921#endif \
4922 /* 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 +08004923
Gilles Peskine449bd832023-01-11 14:50:10 +01004924#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && \
4925 defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu9bb3ee42022-06-23 10:16:33 +08004926 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
Gilles Peskine449bd832023-01-11 14:50:10 +01004927#endif \
4928 /* 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 +08004929
Gilles Peskine449bd832023-01-11 14:50:10 +01004930#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && \
4931 defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu9bb3ee42022-06-23 10:16:33 +08004932 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Gilles Peskine449bd832023-01-11 14:50:10 +01004933#endif \
4934 /* 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 +08004935
Andrzej Kurek25f27152022-08-17 16:09:31 -04004936#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 +08004937 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512,
4938#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA512_C */
4939
Andrzej Kurek25f27152022-08-17 16:09:31 -04004940#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 +08004941 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384,
4942#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA384_C */
4943
Andrzej Kurek25f27152022-08-17 16:09:31 -04004944#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 +08004945 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
4946#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA256_C */
4947
Gabor Mezei15b95a62022-05-09 16:37:58 +02004948 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08004949};
4950
4951/* NOTICE: see above */
4952#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4953static uint16_t ssl_tls12_preset_default_sig_algs[] = {
Andrzej Kurek25f27152022-08-17 16:09:31 -04004954#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004955#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004956 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA512),
Jerry Yu713013f2022-01-17 18:16:35 +08004957#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004958#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4959 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
4960#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004961#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004962 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA512),
Gabor Mezeic1051b62022-05-10 13:13:58 +02004963#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004964#endif /* MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04004965#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004966#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004967 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08004968#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004969#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4970 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
4971#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004972#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004973 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384),
Gabor Mezeic1051b62022-05-10 13:13:58 +02004974#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004975#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04004976#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004977#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004978 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08004979#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004980#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4981 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
4982#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004983#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004984 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256),
Gabor Mezeic1051b62022-05-10 13:13:58 +02004985#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004986#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Gabor Mezei15b95a62022-05-09 16:37:58 +02004987 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08004988};
4989#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4990/* NOTICE: see above */
4991static uint16_t ssl_preset_suiteb_sig_algs[] = {
4992
Andrzej Kurek25f27152022-08-17 16:09:31 -04004993#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 +08004994 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
4995 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004996#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu909df7b2022-01-22 11:56:27 +08004997 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
4998
Andrzej Kurek25f27152022-08-17 16:09:31 -04004999#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 +08005000 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
5001 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005002#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu53037892022-01-25 11:02:06 +08005003 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08005004
Gilles Peskine449bd832023-01-11 14:50:10 +01005005#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && \
5006 defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu909df7b2022-01-22 11:56:27 +08005007 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Gilles Peskine449bd832023-01-11 14:50:10 +01005008#endif \
5009 /* 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 +08005010
Andrzej Kurek25f27152022-08-17 16:09:31 -04005011#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu53037892022-01-25 11:02:06 +08005012 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005013#endif /* MBEDTLS_RSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yu53037892022-01-25 11:02:06 +08005014
Gabor Mezei15b95a62022-05-09 16:37:58 +02005015 MBEDTLS_TLS_SIG_NONE
Jerry Yu713013f2022-01-17 18:16:35 +08005016};
Jerry Yu6106fdc2022-01-12 16:36:14 +08005017
Jerry Yu909df7b2022-01-22 11:56:27 +08005018/* NOTICE: see above */
5019#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5020static uint16_t ssl_tls12_preset_suiteb_sig_algs[] = {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005021#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02005022#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005023 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256),
Jerry Yu713013f2022-01-17 18:16:35 +08005024#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02005025#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005026 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256),
Gabor Mezeic1051b62022-05-10 13:13:58 +02005027#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005028#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04005029#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02005030#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005031 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384),
Jerry Yu18c833e2022-01-25 10:55:47 +08005032#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02005033#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005034 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384),
Gabor Mezeic1051b62022-05-10 13:13:58 +02005035#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005036#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Gabor Mezei15b95a62022-05-09 16:37:58 +02005037 MBEDTLS_TLS_SIG_NONE
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01005038};
Jerry Yu909df7b2022-01-22 11:56:27 +08005039#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5040
Ronald Crone68ab4f2022-10-05 12:46:29 +02005041#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005042
Brett Warrene0edc842021-08-17 09:53:13 +01005043static uint16_t ssl_preset_suiteb_groups[] = {
Jaeden Amerod4311042019-06-03 08:27:16 +01005044#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01005045 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01005046#endif
5047#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01005048 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01005049#endif
Brett Warrene0edc842021-08-17 09:53:13 +01005050 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005051};
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005052
Ronald Crone68ab4f2022-10-05 12:46:29 +02005053#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08005054/* Function for checking `ssl_preset_*_sig_algs` and `ssl_tls12_preset_*_sig_algs`
Jerry Yu370e1462022-01-25 10:36:53 +08005055 * to make sure there are no duplicated signature algorithm entries. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005056MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01005057static int ssl_check_no_sig_alg_duplication(uint16_t *sig_algs)
Jerry Yu1a8b4812022-01-20 17:56:50 +08005058{
5059 size_t i, j;
5060 int ret = 0;
Jerry Yu909df7b2022-01-22 11:56:27 +08005061
Gilles Peskine449bd832023-01-11 14:50:10 +01005062 for (i = 0; sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++) {
5063 for (j = 0; j < i; j++) {
5064 if (sig_algs[i] != sig_algs[j]) {
Jerry Yuf377d642022-01-25 10:43:59 +08005065 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01005066 }
5067 mbedtls_printf(" entry(%04x,%" MBEDTLS_PRINTF_SIZET
5068 ") is duplicated at %" MBEDTLS_PRINTF_SIZET "\n",
5069 sig_algs[i], j, i);
Jerry Yuf377d642022-01-25 10:43:59 +08005070 ret = -1;
Jerry Yu1a8b4812022-01-20 17:56:50 +08005071 }
5072 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005073 return ret;
Jerry Yu1a8b4812022-01-20 17:56:50 +08005074}
5075
Ronald Crone68ab4f2022-10-05 12:46:29 +02005076#endif /* MBEDTLS_DEBUG_C && MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yu1a8b4812022-01-20 17:56:50 +08005077
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005078/*
Tillmann Karras588ad502015-09-25 04:27:22 +02005079 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005080 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005081int mbedtls_ssl_config_defaults(mbedtls_ssl_config *conf,
5082 int endpoint, int transport, int preset)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005083{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005084#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Janos Follath865b3eb2019-12-16 11:46:15 +00005085 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005086#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005087
Ronald Crone68ab4f2022-10-05 12:46:29 +02005088#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01005089 if (ssl_check_no_sig_alg_duplication(ssl_preset_suiteb_sig_algs)) {
5090 mbedtls_printf("ssl_preset_suiteb_sig_algs has duplicated entries\n");
5091 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu1a8b4812022-01-20 17:56:50 +08005092 }
5093
Gilles Peskine449bd832023-01-11 14:50:10 +01005094 if (ssl_check_no_sig_alg_duplication(ssl_preset_default_sig_algs)) {
5095 mbedtls_printf("ssl_preset_default_sig_algs has duplicated entries\n");
5096 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu1a8b4812022-01-20 17:56:50 +08005097 }
Jerry Yu909df7b2022-01-22 11:56:27 +08005098
5099#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01005100 if (ssl_check_no_sig_alg_duplication(ssl_tls12_preset_suiteb_sig_algs)) {
5101 mbedtls_printf("ssl_tls12_preset_suiteb_sig_algs has duplicated entries\n");
5102 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu909df7b2022-01-22 11:56:27 +08005103 }
5104
Gilles Peskine449bd832023-01-11 14:50:10 +01005105 if (ssl_check_no_sig_alg_duplication(ssl_tls12_preset_default_sig_algs)) {
5106 mbedtls_printf("ssl_tls12_preset_default_sig_algs has duplicated entries\n");
5107 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu909df7b2022-01-22 11:56:27 +08005108 }
5109#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Ronald Crone68ab4f2022-10-05 12:46:29 +02005110#endif /* MBEDTLS_DEBUG_C && MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yu1a8b4812022-01-20 17:56:50 +08005111
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02005112 /* Use the functions here so that they are covered in tests,
5113 * but otherwise access member directly for efficiency */
Gilles Peskine449bd832023-01-11 14:50:10 +01005114 mbedtls_ssl_conf_endpoint(conf, endpoint);
5115 mbedtls_ssl_conf_transport(conf, transport);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005116
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005117 /*
5118 * Things that are common to all presets
5119 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02005120#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005121 if (endpoint == MBEDTLS_SSL_IS_CLIENT) {
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02005122 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
5123#if defined(MBEDTLS_SSL_SESSION_TICKETS)
5124 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
5125#endif
5126 }
5127#endif
5128
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005129#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
5130 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
5131#endif
5132
5133#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
5134 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
5135#endif
5136
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02005137#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005138 conf->f_cookie_write = ssl_cookie_write_dummy;
5139 conf->f_cookie_check = ssl_cookie_check_dummy;
5140#endif
5141
5142#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
5143 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
5144#endif
5145
Janos Follath088ce432017-04-10 12:42:31 +01005146#if defined(MBEDTLS_SSL_SRV_C)
5147 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
TRodziewicz3946f792021-06-14 12:11:18 +02005148 conf->respect_cli_pref = MBEDTLS_SSL_SRV_CIPHERSUITE_ORDER_SERVER;
Janos Follath088ce432017-04-10 12:42:31 +01005149#endif
5150
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005151#if defined(MBEDTLS_SSL_PROTO_DTLS)
5152 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
5153 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
5154#endif
5155
5156#if defined(MBEDTLS_SSL_RENEGOTIATION)
5157 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005158 memset(conf->renego_period, 0x00, 2);
5159 memset(conf->renego_period + 2, 0xFF, 6);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005160#endif
5161
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005162#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005163 if (endpoint == MBEDTLS_SSL_IS_SERVER) {
Hanno Beckere2defad2021-07-24 05:59:17 +01005164 const unsigned char dhm_p[] =
5165 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
5166 const unsigned char dhm_g[] =
5167 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
Hanno Becker00d0a682017-10-04 13:14:29 +01005168
Gilles Peskine449bd832023-01-11 14:50:10 +01005169 if ((ret = mbedtls_ssl_conf_dh_param_bin(conf,
5170 dhm_p, sizeof(dhm_p),
5171 dhm_g, sizeof(dhm_g))) != 0) {
5172 return ret;
Hanno Beckere2defad2021-07-24 05:59:17 +01005173 }
5174 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02005175#endif
5176
Ronald Cron6f135e12021-12-08 16:57:54 +01005177#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu6ee56aa2022-12-06 17:47:22 +08005178
5179#if defined(MBEDTLS_SSL_EARLY_DATA)
Gilles Peskine449bd832023-01-11 14:50:10 +01005180 mbedtls_ssl_tls13_conf_early_data(conf, MBEDTLS_SSL_EARLY_DATA_DISABLED);
Jerry Yu6ee56aa2022-12-06 17:47:22 +08005181#if defined(MBEDTLS_SSL_SRV_C)
5182 mbedtls_ssl_tls13_conf_max_early_data_size(
Gilles Peskine449bd832023-01-11 14:50:10 +01005183 conf, MBEDTLS_SSL_MAX_EARLY_DATA_SIZE);
Jerry Yu6ee56aa2022-12-06 17:47:22 +08005184#endif
5185#endif /* MBEDTLS_SSL_EARLY_DATA */
5186
Jerry Yud0766ec2022-09-22 10:46:57 +08005187#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08005188 mbedtls_ssl_conf_new_session_tickets(
Gilles Peskine449bd832023-01-11 14:50:10 +01005189 conf, MBEDTLS_SSL_TLS1_3_DEFAULT_NEW_SESSION_TICKETS);
Jerry Yu1ad7ace2022-08-09 13:28:39 +08005190#endif
Hanno Becker71f1ed62021-07-24 06:01:47 +01005191 /*
5192 * Allow all TLS 1.3 key exchange modes by default.
5193 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00005194 conf->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Ronald Cron6f135e12021-12-08 16:57:54 +01005195#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01005196
Gilles Peskine449bd832023-01-11 14:50:10 +01005197 if (transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Glenn Strauss2dfcea22022-03-14 17:26:42 -04005198#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
XiaokangQian4d3a6042022-04-21 13:46:17 +00005199 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian060d8672022-04-21 09:24:56 +00005200 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian4d3a6042022-04-21 13:46:17 +00005201#else
Gilles Peskine449bd832023-01-11 14:50:10 +01005202 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQian060d8672022-04-21 09:24:56 +00005203#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005204 } else {
XiaokangQian4d3a6042022-04-21 13:46:17 +00005205#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01005206 if (endpoint == MBEDTLS_SSL_IS_CLIENT) {
XiaokangQian4d3a6042022-04-21 13:46:17 +00005207 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5208 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
Gilles Peskine449bd832023-01-11 14:50:10 +01005209 } else {
5210 /* Hybrid TLS 1.2 / 1.3 is not supported on server side yet */
XiaokangQian4d3a6042022-04-21 13:46:17 +00005211 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5212 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5213 }
5214#elif defined(MBEDTLS_SSL_PROTO_TLS1_3)
5215 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
5216 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
5217#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
5218 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5219 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5220#else
Gilles Peskine449bd832023-01-11 14:50:10 +01005221 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQian4d3a6042022-04-21 13:46:17 +00005222#endif
5223 }
Glenn Strauss2dfcea22022-03-14 17:26:42 -04005224
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005225 /*
5226 * Preset-specific defaults
5227 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005228 switch (preset) {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005229 /*
5230 * NSA Suite B
5231 */
5232 case MBEDTLS_SSL_PRESET_SUITEB:
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005233
Hanno Beckerd60b6c62021-04-29 12:04:11 +01005234 conf->ciphersuite_list = ssl_preset_suiteb_ciphersuites;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005235
5236#if defined(MBEDTLS_X509_CRT_PARSE_C)
5237 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005238#endif
5239
Ronald Crone68ab4f2022-10-05 12:46:29 +02005240#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08005241#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01005242 if (mbedtls_ssl_conf_is_tls12_only(conf)) {
Jerry Yu909df7b2022-01-22 11:56:27 +08005243 conf->sig_algs = ssl_tls12_preset_suiteb_sig_algs;
Gilles Peskine449bd832023-01-11 14:50:10 +01005244 } else
Jerry Yu909df7b2022-01-22 11:56:27 +08005245#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005246 conf->sig_algs = ssl_preset_suiteb_sig_algs;
Ronald Crone68ab4f2022-10-05 12:46:29 +02005247#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005248
Brett Warrene0edc842021-08-17 09:53:13 +01005249#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
5250 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005251#endif
Brett Warrene0edc842021-08-17 09:53:13 +01005252 conf->group_list = ssl_preset_suiteb_groups;
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02005253 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005254
5255 /*
5256 * Default
5257 */
5258 default:
Ronald Cronf6606552022-03-15 11:23:25 +01005259
Hanno Beckerd60b6c62021-04-29 12:04:11 +01005260 conf->ciphersuite_list = mbedtls_ssl_list_ciphersuites();
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005261
5262#if defined(MBEDTLS_X509_CRT_PARSE_C)
5263 conf->cert_profile = &mbedtls_x509_crt_profile_default;
5264#endif
5265
Ronald Crone68ab4f2022-10-05 12:46:29 +02005266#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08005267#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01005268 if (mbedtls_ssl_conf_is_tls12_only(conf)) {
Jerry Yu909df7b2022-01-22 11:56:27 +08005269 conf->sig_algs = ssl_tls12_preset_default_sig_algs;
Gilles Peskine449bd832023-01-11 14:50:10 +01005270 } else
Jerry Yu909df7b2022-01-22 11:56:27 +08005271#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005272 conf->sig_algs = ssl_preset_default_sig_algs;
Ronald Crone68ab4f2022-10-05 12:46:29 +02005273#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005274
Brett Warrene0edc842021-08-17 09:53:13 +01005275#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
5276 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005277#endif
Brett Warrene0edc842021-08-17 09:53:13 +01005278 conf->group_list = ssl_preset_default_groups;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005279
5280#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
5281 conf->dhm_min_bitlen = 1024;
5282#endif
5283 }
5284
Gilles Peskine449bd832023-01-11 14:50:10 +01005285 return 0;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005286}
5287
5288/*
5289 * Free mbedtls_ssl_config
5290 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005291void mbedtls_ssl_config_free(mbedtls_ssl_config *conf)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005292{
5293#if defined(MBEDTLS_DHM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005294 mbedtls_mpi_free(&conf->dhm_P);
5295 mbedtls_mpi_free(&conf->dhm_G);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005296#endif
5297
Ronald Cron73fe8df2022-10-05 14:31:43 +02005298#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02005299#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01005300 if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) {
Neil Armstrong501c9322022-05-03 09:35:09 +02005301 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
5302 }
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005303#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01005304 if (conf->psk != NULL) {
5305 mbedtls_platform_zeroize(conf->psk, conf->psk_len);
5306 mbedtls_free(conf->psk);
Azim Khan27e8a122018-03-21 14:24:11 +00005307 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005308 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09005309 }
5310
Gilles Peskine449bd832023-01-11 14:50:10 +01005311 if (conf->psk_identity != NULL) {
5312 mbedtls_platform_zeroize(conf->psk_identity, conf->psk_identity_len);
5313 mbedtls_free(conf->psk_identity);
Azim Khan27e8a122018-03-21 14:24:11 +00005314 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005315 conf->psk_identity_len = 0;
5316 }
Ronald Cron73fe8df2022-10-05 14:31:43 +02005317#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005318
5319#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005320 ssl_key_cert_free(conf->key_cert);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005321#endif
5322
Gilles Peskine449bd832023-01-11 14:50:10 +01005323 mbedtls_platform_zeroize(conf, sizeof(mbedtls_ssl_config));
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005324}
5325
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02005326#if defined(MBEDTLS_PK_C) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01005327 (defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C))
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005328/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005329 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005330 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005331unsigned char mbedtls_ssl_sig_from_pk(mbedtls_pk_context *pk)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005332{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005333#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005334 if (mbedtls_pk_can_do(pk, MBEDTLS_PK_RSA)) {
5335 return MBEDTLS_SSL_SIG_RSA;
5336 }
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005337#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005338#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005339 if (mbedtls_pk_can_do(pk, MBEDTLS_PK_ECDSA)) {
5340 return MBEDTLS_SSL_SIG_ECDSA;
5341 }
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005342#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005343 return MBEDTLS_SSL_SIG_ANON;
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005344}
5345
Gilles Peskine449bd832023-01-11 14:50:10 +01005346unsigned char mbedtls_ssl_sig_from_pk_alg(mbedtls_pk_type_t type)
Hanno Becker7e5437a2017-04-28 17:15:26 +01005347{
Gilles Peskine449bd832023-01-11 14:50:10 +01005348 switch (type) {
Hanno Becker7e5437a2017-04-28 17:15:26 +01005349 case MBEDTLS_PK_RSA:
Gilles Peskine449bd832023-01-11 14:50:10 +01005350 return MBEDTLS_SSL_SIG_RSA;
Hanno Becker7e5437a2017-04-28 17:15:26 +01005351 case MBEDTLS_PK_ECDSA:
5352 case MBEDTLS_PK_ECKEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01005353 return MBEDTLS_SSL_SIG_ECDSA;
Hanno Becker7e5437a2017-04-28 17:15:26 +01005354 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005355 return MBEDTLS_SSL_SIG_ANON;
Hanno Becker7e5437a2017-04-28 17:15:26 +01005356 }
5357}
5358
Gilles Peskine449bd832023-01-11 14:50:10 +01005359mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig(unsigned char sig)
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005360{
Gilles Peskine449bd832023-01-11 14:50:10 +01005361 switch (sig) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005362#if defined(MBEDTLS_RSA_C)
5363 case MBEDTLS_SSL_SIG_RSA:
Gilles Peskine449bd832023-01-11 14:50:10 +01005364 return MBEDTLS_PK_RSA;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005365#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005366#if defined(MBEDTLS_ECDSA_C)
5367 case MBEDTLS_SSL_SIG_ECDSA:
Gilles Peskine449bd832023-01-11 14:50:10 +01005368 return MBEDTLS_PK_ECDSA;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005369#endif
5370 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005371 return MBEDTLS_PK_NONE;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005372 }
5373}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02005374#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005375
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02005376/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005377 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02005378 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005379mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash(unsigned char hash)
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005380{
Gilles Peskine449bd832023-01-11 14:50:10 +01005381 switch (hash) {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005382#if defined(MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005383 case MBEDTLS_SSL_HASH_MD5:
Gilles Peskine449bd832023-01-11 14:50:10 +01005384 return MBEDTLS_MD_MD5;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005385#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005386#if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005387 case MBEDTLS_SSL_HASH_SHA1:
Gilles Peskine449bd832023-01-11 14:50:10 +01005388 return MBEDTLS_MD_SHA1;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005389#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005390#if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005391 case MBEDTLS_SSL_HASH_SHA224:
Gilles Peskine449bd832023-01-11 14:50:10 +01005392 return MBEDTLS_MD_SHA224;
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02005393#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005394#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005395 case MBEDTLS_SSL_HASH_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01005396 return MBEDTLS_MD_SHA256;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005397#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005398#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005399 case MBEDTLS_SSL_HASH_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01005400 return MBEDTLS_MD_SHA384;
Mateusz Starzyk3352a532021-04-06 14:28:22 +02005401#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005402#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005403 case MBEDTLS_SSL_HASH_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01005404 return MBEDTLS_MD_SHA512;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005405#endif
5406 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005407 return MBEDTLS_MD_NONE;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005408 }
5409}
5410
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005411/*
5412 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
5413 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005414unsigned char mbedtls_ssl_hash_from_md_alg(int md)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005415{
Gilles Peskine449bd832023-01-11 14:50:10 +01005416 switch (md) {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005417#if defined(MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005418 case MBEDTLS_MD_MD5:
Gilles Peskine449bd832023-01-11 14:50:10 +01005419 return MBEDTLS_SSL_HASH_MD5;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005420#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005421#if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005422 case MBEDTLS_MD_SHA1:
Gilles Peskine449bd832023-01-11 14:50:10 +01005423 return MBEDTLS_SSL_HASH_SHA1;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005424#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005425#if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005426 case MBEDTLS_MD_SHA224:
Gilles Peskine449bd832023-01-11 14:50:10 +01005427 return MBEDTLS_SSL_HASH_SHA224;
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02005428#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005429#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005430 case MBEDTLS_MD_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01005431 return MBEDTLS_SSL_HASH_SHA256;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005432#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005433#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005434 case MBEDTLS_MD_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01005435 return MBEDTLS_SSL_HASH_SHA384;
Mateusz Starzyk3352a532021-04-06 14:28:22 +02005436#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005437#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005438 case MBEDTLS_MD_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01005439 return MBEDTLS_SSL_HASH_SHA512;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005440#endif
5441 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005442 return MBEDTLS_SSL_HASH_NONE;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005443 }
5444}
5445
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005446/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005447 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005448 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005449 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005450int mbedtls_ssl_check_curve_tls_id(const mbedtls_ssl_context *ssl, uint16_t tls_id)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005451{
Gilles Peskine449bd832023-01-11 14:50:10 +01005452 const uint16_t *group_list = mbedtls_ssl_get_groups(ssl);
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005453
Gilles Peskine449bd832023-01-11 14:50:10 +01005454 if (group_list == NULL) {
5455 return -1;
Brett Warrene0edc842021-08-17 09:53:13 +01005456 }
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005457
Gilles Peskine449bd832023-01-11 14:50:10 +01005458 for (; *group_list != 0; group_list++) {
5459 if (*group_list == tls_id) {
5460 return 0;
5461 }
5462 }
5463
5464 return -1;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005465}
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005466
5467#if defined(MBEDTLS_ECP_C)
5468/*
5469 * Same as mbedtls_ssl_check_curve_tls_id() but with a mbedtls_ecp_group_id.
5470 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005471int mbedtls_ssl_check_curve(const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id)
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005472{
Gilles Peskine449bd832023-01-11 14:50:10 +01005473 uint16_t tls_id = mbedtls_ssl_get_tls_id_from_ecp_group_id(grp_id);
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005474
Gilles Peskine449bd832023-01-11 14:50:10 +01005475 if (tls_id == 0) {
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005476 return -1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005477 }
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005478
Gilles Peskine449bd832023-01-11 14:50:10 +01005479 return mbedtls_ssl_check_curve_tls_id(ssl, tls_id);
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005480}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02005481#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005482
Gilles Peskine449bd832023-01-11 14:50:10 +01005483#if defined(MBEDTLS_DEBUG_C)
Valerio Setti67419f02023-01-04 16:12:42 +01005484#define EC_NAME(_name_) _name_
5485#else
5486#define EC_NAME(_name_) NULL
5487#endif
5488
Valerio Setti18c9fed2022-12-30 17:44:24 +01005489static const struct {
5490 uint16_t tls_id;
5491 mbedtls_ecp_group_id ecp_group_id;
5492 psa_ecc_family_t psa_family;
5493 uint16_t bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01005494 const char *name;
Valerio Setti18c9fed2022-12-30 17:44:24 +01005495} tls_id_match_table[] =
5496{
5497#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_521)
Gilles Peskine449bd832023-01-11 14:50:10 +01005498 { 25, MBEDTLS_ECP_DP_SECP521R1, PSA_ECC_FAMILY_SECP_R1, 521, EC_NAME("secp521r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005499#endif
5500#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED) || defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512)
Gilles Peskine449bd832023-01-11 14:50:10 +01005501 { 28, MBEDTLS_ECP_DP_BP512R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 512, EC_NAME("brainpoolP512r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005502#endif
Valerio Setti67419f02023-01-04 16:12:42 +01005503#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_384)
Gilles Peskine449bd832023-01-11 14:50:10 +01005504 { 24, MBEDTLS_ECP_DP_SECP384R1, PSA_ECC_FAMILY_SECP_R1, 384, EC_NAME("secp384r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005505#endif
Valerio Setti67419f02023-01-04 16:12:42 +01005506#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED) || defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384)
Gilles Peskine449bd832023-01-11 14:50:10 +01005507 { 27, MBEDTLS_ECP_DP_BP384R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 384, EC_NAME("brainpoolP384r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005508#endif
Valerio Setti67419f02023-01-04 16:12:42 +01005509#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_256)
Gilles Peskine449bd832023-01-11 14:50:10 +01005510 { 23, MBEDTLS_ECP_DP_SECP256R1, PSA_ECC_FAMILY_SECP_R1, 256, EC_NAME("secp256r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005511#endif
5512#if defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED) || defined(PSA_WANT_ECC_SECP_K1_256)
Gilles Peskine449bd832023-01-11 14:50:10 +01005513 { 22, MBEDTLS_ECP_DP_SECP256K1, PSA_ECC_FAMILY_SECP_K1, 256, EC_NAME("secp256k1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005514#endif
Valerio Setti67419f02023-01-04 16:12:42 +01005515#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED) || defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256)
Gilles Peskine449bd832023-01-11 14:50:10 +01005516 { 26, MBEDTLS_ECP_DP_BP256R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 256, EC_NAME("brainpoolP256r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005517#endif
5518#if defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_224)
Gilles Peskine449bd832023-01-11 14:50:10 +01005519 { 21, MBEDTLS_ECP_DP_SECP224R1, PSA_ECC_FAMILY_SECP_R1, 224, EC_NAME("secp224r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005520#endif
5521#if defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED) || defined(PSA_WANT_ECC_SECP_K1_224)
Gilles Peskine449bd832023-01-11 14:50:10 +01005522 { 20, MBEDTLS_ECP_DP_SECP224K1, PSA_ECC_FAMILY_SECP_K1, 224, EC_NAME("secp224k1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005523#endif
5524#if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_192)
Gilles Peskine449bd832023-01-11 14:50:10 +01005525 { 19, MBEDTLS_ECP_DP_SECP192R1, PSA_ECC_FAMILY_SECP_R1, 192, EC_NAME("secp192r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005526#endif
5527#if defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED) || defined(PSA_WANT_ECC_SECP_K1_192)
Gilles Peskine449bd832023-01-11 14:50:10 +01005528 { 18, MBEDTLS_ECP_DP_SECP192K1, PSA_ECC_FAMILY_SECP_K1, 192, EC_NAME("secp192k1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005529#endif
5530#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED) || defined(PSA_WANT_ECC_MONTGOMERY_255)
Gilles Peskine449bd832023-01-11 14:50:10 +01005531 { 29, MBEDTLS_ECP_DP_CURVE25519, PSA_ECC_FAMILY_MONTGOMERY, 255, EC_NAME("x25519") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005532#endif
5533#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED) || defined(PSA_WANT_ECC_MONTGOMERY_448)
Gilles Peskine449bd832023-01-11 14:50:10 +01005534 { 30, MBEDTLS_ECP_DP_CURVE448, PSA_ECC_FAMILY_MONTGOMERY, 448, EC_NAME("x448") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005535#endif
5536 { 0, MBEDTLS_ECP_DP_NONE, 0, 0, NULL },
5537};
5538
Gilles Peskine449bd832023-01-11 14:50:10 +01005539int mbedtls_ssl_get_psa_curve_info_from_tls_id(uint16_t tls_id,
5540 psa_ecc_family_t *family,
5541 size_t *bits)
Valerio Setti18c9fed2022-12-30 17:44:24 +01005542{
Gilles Peskine449bd832023-01-11 14:50:10 +01005543 for (int i = 0; tls_id_match_table[i].tls_id != 0; i++) {
5544 if (tls_id_match_table[i].tls_id == tls_id) {
5545 if (family != NULL) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01005546 *family = tls_id_match_table[i].psa_family;
Gilles Peskine449bd832023-01-11 14:50:10 +01005547 }
5548 if (bits != NULL) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01005549 *bits = tls_id_match_table[i].bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01005550 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01005551 return PSA_SUCCESS;
5552 }
5553 }
5554
5555 return PSA_ERROR_NOT_SUPPORTED;
5556}
5557
Gilles Peskine449bd832023-01-11 14:50:10 +01005558mbedtls_ecp_group_id mbedtls_ssl_get_ecp_group_id_from_tls_id(uint16_t tls_id)
Valerio Setti18c9fed2022-12-30 17:44:24 +01005559{
Gilles Peskine449bd832023-01-11 14:50:10 +01005560 for (int i = 0; tls_id_match_table[i].tls_id != 0; i++) {
5561 if (tls_id_match_table[i].tls_id == tls_id) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01005562 return tls_id_match_table[i].ecp_group_id;
Gilles Peskine449bd832023-01-11 14:50:10 +01005563 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01005564 }
5565
5566 return MBEDTLS_ECP_DP_NONE;
5567}
5568
Gilles Peskine449bd832023-01-11 14:50:10 +01005569uint16_t mbedtls_ssl_get_tls_id_from_ecp_group_id(mbedtls_ecp_group_id grp_id)
Valerio Setti18c9fed2022-12-30 17:44:24 +01005570{
Gilles Peskine449bd832023-01-11 14:50:10 +01005571 for (int i = 0; tls_id_match_table[i].ecp_group_id != MBEDTLS_ECP_DP_NONE;
5572 i++) {
5573 if (tls_id_match_table[i].ecp_group_id == grp_id) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01005574 return tls_id_match_table[i].tls_id;
Gilles Peskine449bd832023-01-11 14:50:10 +01005575 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01005576 }
5577
5578 return 0;
5579}
5580
Valerio Setti67419f02023-01-04 16:12:42 +01005581#if defined(MBEDTLS_DEBUG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005582const char *mbedtls_ssl_get_curve_name_from_tls_id(uint16_t tls_id)
Valerio Setti18c9fed2022-12-30 17:44:24 +01005583{
Gilles Peskine449bd832023-01-11 14:50:10 +01005584 for (int i = 0; tls_id_match_table[i].tls_id != 0; i++) {
5585 if (tls_id_match_table[i].tls_id == tls_id) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01005586 return tls_id_match_table[i].name;
Gilles Peskine449bd832023-01-11 14:50:10 +01005587 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01005588 }
5589
5590 return NULL;
5591}
Valerio Setti67419f02023-01-04 16:12:42 +01005592#endif
Valerio Setti18c9fed2022-12-30 17:44:24 +01005593
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005594#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005595int mbedtls_ssl_check_cert_usage(const mbedtls_x509_crt *cert,
5596 const mbedtls_ssl_ciphersuite_t *ciphersuite,
5597 int cert_endpoint,
5598 uint32_t *flags)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005599{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005600 int ret = 0;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005601 int usage = 0;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005602 const char *ext_oid;
5603 size_t ext_len;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005604
Gilles Peskine449bd832023-01-11 14:50:10 +01005605 if (cert_endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005606 /* Server part of the key exchange */
Gilles Peskine449bd832023-01-11 14:50:10 +01005607 switch (ciphersuite->key_exchange) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005608 case MBEDTLS_KEY_EXCHANGE_RSA:
5609 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005610 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005611 break;
5612
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005613 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
5614 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
5615 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
5616 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005617 break;
5618
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005619 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
5620 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005621 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005622 break;
5623
5624 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005625 case MBEDTLS_KEY_EXCHANGE_NONE:
5626 case MBEDTLS_KEY_EXCHANGE_PSK:
5627 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
5628 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02005629 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005630 usage = 0;
5631 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005632 } else {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005633 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
5634 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005635 }
5636
Gilles Peskine449bd832023-01-11 14:50:10 +01005637 if (mbedtls_x509_crt_check_key_usage(cert, usage) != 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005638 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005639 ret = -1;
5640 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005641
Gilles Peskine449bd832023-01-11 14:50:10 +01005642 if (cert_endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005643 ext_oid = MBEDTLS_OID_SERVER_AUTH;
Gilles Peskine449bd832023-01-11 14:50:10 +01005644 ext_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_SERVER_AUTH);
5645 } else {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005646 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
Gilles Peskine449bd832023-01-11 14:50:10 +01005647 ext_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_CLIENT_AUTH);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005648 }
5649
Gilles Peskine449bd832023-01-11 14:50:10 +01005650 if (mbedtls_x509_crt_check_extended_key_usage(cert, ext_oid, ext_len) != 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005651 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005652 ret = -1;
5653 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005654
Gilles Peskine449bd832023-01-11 14:50:10 +01005655 return ret;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005656}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005657#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02005658
Jerry Yu148165c2021-09-24 23:20:59 +08005659#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01005660int mbedtls_ssl_get_handshake_transcript(mbedtls_ssl_context *ssl,
5661 const mbedtls_md_type_t md,
5662 unsigned char *dst,
5663 size_t dst_len,
5664 size_t *olen)
Jerry Yu148165c2021-09-24 23:20:59 +08005665{
Ronald Cronf6893e12022-01-07 22:09:01 +01005666 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5667 psa_hash_operation_t *hash_operation_to_clone;
5668 psa_hash_operation_t hash_operation = psa_hash_operation_init();
5669
Jerry Yu148165c2021-09-24 23:20:59 +08005670 *olen = 0;
Ronald Cronf6893e12022-01-07 22:09:01 +01005671
Gilles Peskine449bd832023-01-11 14:50:10 +01005672 switch (md) {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005673#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01005674 case MBEDTLS_MD_SHA384:
5675 hash_operation_to_clone = &ssl->handshake->fin_sha384_psa;
5676 break;
Ronald Cronf6893e12022-01-07 22:09:01 +01005677#endif
5678
Andrzej Kurek25f27152022-08-17 16:09:31 -04005679#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01005680 case MBEDTLS_MD_SHA256:
5681 hash_operation_to_clone = &ssl->handshake->fin_sha256_psa;
5682 break;
Ronald Cronf6893e12022-01-07 22:09:01 +01005683#endif
5684
Gilles Peskine449bd832023-01-11 14:50:10 +01005685 default:
5686 goto exit;
5687 }
5688
5689 status = psa_hash_clone(hash_operation_to_clone, &hash_operation);
5690 if (status != PSA_SUCCESS) {
Ronald Cronf6893e12022-01-07 22:09:01 +01005691 goto exit;
5692 }
5693
Gilles Peskine449bd832023-01-11 14:50:10 +01005694 status = psa_hash_finish(&hash_operation, dst, dst_len, olen);
5695 if (status != PSA_SUCCESS) {
Ronald Cronf6893e12022-01-07 22:09:01 +01005696 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005697 }
Ronald Cronf6893e12022-01-07 22:09:01 +01005698
5699exit:
Andrzej Kurekeabeb302022-10-17 07:52:51 -04005700#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
5701 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
5702 (void) ssl;
5703#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005704 return psa_ssl_status_to_mbedtls(status);
Jerry Yu148165c2021-09-24 23:20:59 +08005705}
5706#else /* MBEDTLS_USE_PSA_CRYPTO */
5707
Andrzej Kurek25f27152022-08-17 16:09:31 -04005708#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005709MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01005710static int ssl_get_handshake_transcript_sha384(mbedtls_ssl_context *ssl,
5711 unsigned char *dst,
5712 size_t dst_len,
5713 size_t *olen)
Jerry Yu24c0ec32021-09-09 14:21:07 +08005714{
Jerry Yu24c0ec32021-09-09 14:21:07 +08005715 int ret;
5716 mbedtls_sha512_context sha512;
5717
Gilles Peskine449bd832023-01-11 14:50:10 +01005718 if (dst_len < 48) {
5719 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
5720 }
Jerry Yu24c0ec32021-09-09 14:21:07 +08005721
Gilles Peskine449bd832023-01-11 14:50:10 +01005722 mbedtls_sha512_init(&sha512);
5723 mbedtls_sha512_clone(&sha512, &ssl->handshake->fin_sha384);
Jerry Yu24c0ec32021-09-09 14:21:07 +08005724
Gilles Peskine449bd832023-01-11 14:50:10 +01005725 if ((ret = mbedtls_sha512_finish(&sha512, dst)) != 0) {
5726 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_sha512_finish", ret);
Jerry Yu24c0ec32021-09-09 14:21:07 +08005727 goto exit;
5728 }
5729
5730 *olen = 48;
5731
5732exit:
5733
Gilles Peskine449bd832023-01-11 14:50:10 +01005734 mbedtls_sha512_free(&sha512);
5735 return ret;
Jerry Yu24c0ec32021-09-09 14:21:07 +08005736}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005737#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005738
Andrzej Kurek25f27152022-08-17 16:09:31 -04005739#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005740MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01005741static int ssl_get_handshake_transcript_sha256(mbedtls_ssl_context *ssl,
5742 unsigned char *dst,
5743 size_t dst_len,
5744 size_t *olen)
Jerry Yu24c0ec32021-09-09 14:21:07 +08005745{
Jerry Yu24c0ec32021-09-09 14:21:07 +08005746 int ret;
5747 mbedtls_sha256_context sha256;
5748
Gilles Peskine449bd832023-01-11 14:50:10 +01005749 if (dst_len < 32) {
5750 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
5751 }
Jerry Yu24c0ec32021-09-09 14:21:07 +08005752
Gilles Peskine449bd832023-01-11 14:50:10 +01005753 mbedtls_sha256_init(&sha256);
5754 mbedtls_sha256_clone(&sha256, &ssl->handshake->fin_sha256);
Jerry Yuc5aef882021-12-23 20:15:02 +08005755
Gilles Peskine449bd832023-01-11 14:50:10 +01005756 if ((ret = mbedtls_sha256_finish(&sha256, dst)) != 0) {
5757 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_sha256_finish", ret);
Jerry Yu24c0ec32021-09-09 14:21:07 +08005758 goto exit;
5759 }
5760
5761 *olen = 32;
5762
5763exit:
5764
Gilles Peskine449bd832023-01-11 14:50:10 +01005765 mbedtls_sha256_free(&sha256);
5766 return ret;
Jerry Yu24c0ec32021-09-09 14:21:07 +08005767}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005768#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005769
Gilles Peskine449bd832023-01-11 14:50:10 +01005770int mbedtls_ssl_get_handshake_transcript(mbedtls_ssl_context *ssl,
5771 const mbedtls_md_type_t md,
5772 unsigned char *dst,
5773 size_t dst_len,
5774 size_t *olen)
Jerry Yu24c0ec32021-09-09 14:21:07 +08005775{
Gilles Peskine449bd832023-01-11 14:50:10 +01005776 switch (md) {
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005777
Andrzej Kurek25f27152022-08-17 16:09:31 -04005778#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01005779 case MBEDTLS_MD_SHA384:
5780 return ssl_get_handshake_transcript_sha384(ssl, dst, dst_len, olen);
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005781#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005782
Andrzej Kurek25f27152022-08-17 16:09:31 -04005783#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01005784 case MBEDTLS_MD_SHA256:
5785 return ssl_get_handshake_transcript_sha256(ssl, dst, dst_len, olen);
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005786#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005787
Gilles Peskine449bd832023-01-11 14:50:10 +01005788 default:
Andrzej Kurek409248a2022-10-24 10:33:21 -04005789#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01005790 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
5791 (void) ssl;
5792 (void) dst;
5793 (void) dst_len;
5794 (void) olen;
Andrzej Kurek409248a2022-10-24 10:33:21 -04005795#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005796 break;
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005797 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005798 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu24c0ec32021-09-09 14:21:07 +08005799}
XiaokangQian647719a2021-12-07 09:16:29 +00005800
Jerry Yu148165c2021-09-24 23:20:59 +08005801#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005802
Ronald Crone68ab4f2022-10-05 12:46:29 +02005803#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Gabor Mezei078e8032022-04-27 21:17:56 +02005804/* mbedtls_ssl_parse_sig_alg_ext()
5805 *
5806 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
5807 * value (TLS 1.3 RFC8446):
5808 * enum {
5809 * ....
5810 * ecdsa_secp256r1_sha256( 0x0403 ),
5811 * ecdsa_secp384r1_sha384( 0x0503 ),
5812 * ecdsa_secp521r1_sha512( 0x0603 ),
5813 * ....
5814 * } SignatureScheme;
5815 *
5816 * struct {
5817 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
5818 * } SignatureSchemeList;
5819 *
5820 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
5821 * value (TLS 1.2 RFC5246):
5822 * enum {
5823 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
5824 * sha512(6), (255)
5825 * } HashAlgorithm;
5826 *
5827 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
5828 * SignatureAlgorithm;
5829 *
5830 * struct {
5831 * HashAlgorithm hash;
5832 * SignatureAlgorithm signature;
5833 * } SignatureAndHashAlgorithm;
5834 *
5835 * SignatureAndHashAlgorithm
5836 * supported_signature_algorithms<2..2^16-2>;
5837 *
5838 * The TLS 1.3 signature algorithm extension was defined to be a compatible
5839 * generalization of the TLS 1.2 signature algorithm extension.
5840 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
5841 * `SignatureScheme` field of TLS 1.3
5842 *
5843 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005844int mbedtls_ssl_parse_sig_alg_ext(mbedtls_ssl_context *ssl,
5845 const unsigned char *buf,
5846 const unsigned char *end)
Gabor Mezei078e8032022-04-27 21:17:56 +02005847{
5848 const unsigned char *p = buf;
5849 size_t supported_sig_algs_len = 0;
5850 const unsigned char *supported_sig_algs_end;
5851 uint16_t sig_alg;
5852 uint32_t common_idx = 0;
5853
Gilles Peskine449bd832023-01-11 14:50:10 +01005854 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
5855 supported_sig_algs_len = MBEDTLS_GET_UINT16_BE(p, 0);
Gabor Mezei078e8032022-04-27 21:17:56 +02005856 p += 2;
5857
Gilles Peskine449bd832023-01-11 14:50:10 +01005858 memset(ssl->handshake->received_sig_algs, 0,
5859 sizeof(ssl->handshake->received_sig_algs));
Gabor Mezei078e8032022-04-27 21:17:56 +02005860
Gilles Peskine449bd832023-01-11 14:50:10 +01005861 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, supported_sig_algs_len);
Gabor Mezei078e8032022-04-27 21:17:56 +02005862 supported_sig_algs_end = p + supported_sig_algs_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01005863 while (p < supported_sig_algs_end) {
5864 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, supported_sig_algs_end, 2);
5865 sig_alg = MBEDTLS_GET_UINT16_BE(p, 0);
Gabor Mezei078e8032022-04-27 21:17:56 +02005866 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01005867 MBEDTLS_SSL_DEBUG_MSG(4, ("received signature algorithm: 0x%x %s",
5868 sig_alg,
5869 mbedtls_ssl_sig_alg_to_str(sig_alg)));
Jerry Yu2fe6c632022-06-29 10:02:38 +08005870#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01005871 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 &&
5872 (!(mbedtls_ssl_sig_alg_is_supported(ssl, sig_alg) &&
5873 mbedtls_ssl_sig_alg_is_offered(ssl, sig_alg)))) {
Gabor Mezei078e8032022-04-27 21:17:56 +02005874 continue;
Jerry Yu2fe6c632022-06-29 10:02:38 +08005875 }
5876#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gabor Mezei078e8032022-04-27 21:17:56 +02005877
Gilles Peskine449bd832023-01-11 14:50:10 +01005878 MBEDTLS_SSL_DEBUG_MSG(4, ("valid signature algorithm: %s",
5879 mbedtls_ssl_sig_alg_to_str(sig_alg)));
Gabor Mezei078e8032022-04-27 21:17:56 +02005880
Gilles Peskine449bd832023-01-11 14:50:10 +01005881 if (common_idx + 1 < MBEDTLS_RECEIVED_SIG_ALGS_SIZE) {
Gabor Mezei078e8032022-04-27 21:17:56 +02005882 ssl->handshake->received_sig_algs[common_idx] = sig_alg;
5883 common_idx += 1;
5884 }
5885 }
5886 /* Check that we consumed all the message. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005887 if (p != end) {
5888 MBEDTLS_SSL_DEBUG_MSG(1,
5889 ("Signature algorithms extension length misaligned"));
5890 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
5891 MBEDTLS_ERR_SSL_DECODE_ERROR);
5892 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Gabor Mezei078e8032022-04-27 21:17:56 +02005893 }
5894
Gilles Peskine449bd832023-01-11 14:50:10 +01005895 if (common_idx == 0) {
5896 MBEDTLS_SSL_DEBUG_MSG(3, ("no signature algorithm in common"));
5897 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
5898 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
5899 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Gabor Mezei078e8032022-04-27 21:17:56 +02005900 }
5901
Gabor Mezei15b95a62022-05-09 16:37:58 +02005902 ssl->handshake->received_sig_algs[common_idx] = MBEDTLS_TLS_SIG_NONE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005903 return 0;
Gabor Mezei078e8032022-04-27 21:17:56 +02005904}
5905
Ronald Crone68ab4f2022-10-05 12:46:29 +02005906#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Gabor Mezei078e8032022-04-27 21:17:56 +02005907
Jerry Yudc7bd172022-02-17 13:44:15 +08005908#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5909
5910#if defined(MBEDTLS_USE_PSA_CRYPTO)
5911
Gilles Peskine449bd832023-01-11 14:50:10 +01005912static psa_status_t setup_psa_key_derivation(psa_key_derivation_operation_t *derivation,
5913 mbedtls_svc_key_id_t key,
5914 psa_algorithm_t alg,
5915 const unsigned char *raw_psk, size_t raw_psk_length,
5916 const unsigned char *seed, size_t seed_length,
5917 const unsigned char *label, size_t label_length,
5918 const unsigned char *other_secret,
5919 size_t other_secret_length,
5920 size_t capacity)
Jerry Yudc7bd172022-02-17 13:44:15 +08005921{
5922 psa_status_t status;
5923
Gilles Peskine449bd832023-01-11 14:50:10 +01005924 status = psa_key_derivation_setup(derivation, alg);
5925 if (status != PSA_SUCCESS) {
5926 return status;
5927 }
Jerry Yudc7bd172022-02-17 13:44:15 +08005928
Gilles Peskine449bd832023-01-11 14:50:10 +01005929 if (PSA_ALG_IS_TLS12_PRF(alg) || PSA_ALG_IS_TLS12_PSK_TO_MS(alg)) {
5930 status = psa_key_derivation_input_bytes(derivation,
5931 PSA_KEY_DERIVATION_INPUT_SEED,
5932 seed, seed_length);
5933 if (status != PSA_SUCCESS) {
5934 return status;
Przemek Stekiel1f027032022-04-05 17:12:11 +02005935 }
5936
Gilles Peskine449bd832023-01-11 14:50:10 +01005937 if (other_secret != NULL) {
5938 status = psa_key_derivation_input_bytes(derivation,
5939 PSA_KEY_DERIVATION_INPUT_OTHER_SECRET,
5940 other_secret, other_secret_length);
5941 if (status != PSA_SUCCESS) {
5942 return status;
5943 }
5944 }
5945
5946 if (mbedtls_svc_key_id_is_null(key)) {
Jerry Yudc7bd172022-02-17 13:44:15 +08005947 status = psa_key_derivation_input_bytes(
5948 derivation, PSA_KEY_DERIVATION_INPUT_SECRET,
Gilles Peskine449bd832023-01-11 14:50:10 +01005949 raw_psk, raw_psk_length);
5950 } else {
Jerry Yudc7bd172022-02-17 13:44:15 +08005951 status = psa_key_derivation_input_key(
Gilles Peskine449bd832023-01-11 14:50:10 +01005952 derivation, PSA_KEY_DERIVATION_INPUT_SECRET, key);
Jerry Yudc7bd172022-02-17 13:44:15 +08005953 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005954 if (status != PSA_SUCCESS) {
5955 return status;
5956 }
Jerry Yudc7bd172022-02-17 13:44:15 +08005957
Gilles Peskine449bd832023-01-11 14:50:10 +01005958 status = psa_key_derivation_input_bytes(derivation,
5959 PSA_KEY_DERIVATION_INPUT_LABEL,
5960 label, label_length);
5961 if (status != PSA_SUCCESS) {
5962 return status;
5963 }
5964 } else {
5965 return PSA_ERROR_NOT_SUPPORTED;
Jerry Yudc7bd172022-02-17 13:44:15 +08005966 }
5967
Gilles Peskine449bd832023-01-11 14:50:10 +01005968 status = psa_key_derivation_set_capacity(derivation, capacity);
5969 if (status != PSA_SUCCESS) {
5970 return status;
5971 }
Jerry Yudc7bd172022-02-17 13:44:15 +08005972
Gilles Peskine449bd832023-01-11 14:50:10 +01005973 return PSA_SUCCESS;
Jerry Yudc7bd172022-02-17 13:44:15 +08005974}
5975
Andrzej Kurek57d10632022-10-24 10:32:01 -04005976#if defined(PSA_WANT_ALG_SHA_384) || \
5977 defined(PSA_WANT_ALG_SHA_256)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005978MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01005979static int tls_prf_generic(mbedtls_md_type_t md_type,
5980 const unsigned char *secret, size_t slen,
5981 const char *label,
5982 const unsigned char *random, size_t rlen,
5983 unsigned char *dstbuf, size_t dlen)
Jerry Yudc7bd172022-02-17 13:44:15 +08005984{
5985 psa_status_t status;
5986 psa_algorithm_t alg;
5987 mbedtls_svc_key_id_t master_key = MBEDTLS_SVC_KEY_ID_INIT;
5988 psa_key_derivation_operation_t derivation =
5989 PSA_KEY_DERIVATION_OPERATION_INIT;
5990
Gilles Peskine449bd832023-01-11 14:50:10 +01005991 if (md_type == MBEDTLS_MD_SHA384) {
Jerry Yudc7bd172022-02-17 13:44:15 +08005992 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
Gilles Peskine449bd832023-01-11 14:50:10 +01005993 } else {
Jerry Yudc7bd172022-02-17 13:44:15 +08005994 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
Gilles Peskine449bd832023-01-11 14:50:10 +01005995 }
Jerry Yudc7bd172022-02-17 13:44:15 +08005996
5997 /* Normally a "secret" should be long enough to be impossible to
5998 * find by brute force, and in particular should not be empty. But
5999 * this PRF is also used to derive an IV, in particular in EAP-TLS,
6000 * and for this use case it makes sense to have a 0-length "secret".
6001 * Since the key API doesn't allow importing a key of length 0,
6002 * keep master_key=0, which setup_psa_key_derivation() understands
6003 * to mean a 0-length "secret" input. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006004 if (slen != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006005 psa_key_attributes_t key_attributes = psa_key_attributes_init();
Gilles Peskine449bd832023-01-11 14:50:10 +01006006 psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE);
6007 psa_set_key_algorithm(&key_attributes, alg);
6008 psa_set_key_type(&key_attributes, PSA_KEY_TYPE_DERIVE);
Jerry Yudc7bd172022-02-17 13:44:15 +08006009
Gilles Peskine449bd832023-01-11 14:50:10 +01006010 status = psa_import_key(&key_attributes, secret, slen, &master_key);
6011 if (status != PSA_SUCCESS) {
6012 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
6013 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006014 }
6015
Gilles Peskine449bd832023-01-11 14:50:10 +01006016 status = setup_psa_key_derivation(&derivation,
6017 master_key, alg,
6018 NULL, 0,
6019 random, rlen,
6020 (unsigned char const *) label,
6021 (size_t) strlen(label),
6022 NULL, 0,
6023 dlen);
6024 if (status != PSA_SUCCESS) {
6025 psa_key_derivation_abort(&derivation);
6026 psa_destroy_key(master_key);
6027 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yudc7bd172022-02-17 13:44:15 +08006028 }
6029
Gilles Peskine449bd832023-01-11 14:50:10 +01006030 status = psa_key_derivation_output_bytes(&derivation, dstbuf, dlen);
6031 if (status != PSA_SUCCESS) {
6032 psa_key_derivation_abort(&derivation);
6033 psa_destroy_key(master_key);
6034 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yudc7bd172022-02-17 13:44:15 +08006035 }
6036
Gilles Peskine449bd832023-01-11 14:50:10 +01006037 status = psa_key_derivation_abort(&derivation);
6038 if (status != PSA_SUCCESS) {
6039 psa_destroy_key(master_key);
6040 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yudc7bd172022-02-17 13:44:15 +08006041 }
6042
Gilles Peskine449bd832023-01-11 14:50:10 +01006043 if (!mbedtls_svc_key_id_is_null(master_key)) {
6044 status = psa_destroy_key(master_key);
6045 }
6046 if (status != PSA_SUCCESS) {
6047 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
6048 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006049
Gilles Peskine449bd832023-01-11 14:50:10 +01006050 return 0;
Jerry Yudc7bd172022-02-17 13:44:15 +08006051}
Andrzej Kurek57d10632022-10-24 10:32:01 -04006052#endif /* PSA_WANT_ALG_SHA_256 || PSA_WANT_ALG_SHA_384 */
Jerry Yudc7bd172022-02-17 13:44:15 +08006053#else /* MBEDTLS_USE_PSA_CRYPTO */
6054
Andrzej Kurek57d10632022-10-24 10:32:01 -04006055#if defined(MBEDTLS_MD_C) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01006056 (defined(MBEDTLS_SHA256_C) || \
6057 defined(MBEDTLS_SHA384_C))
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006058MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006059static int tls_prf_generic(mbedtls_md_type_t md_type,
6060 const unsigned char *secret, size_t slen,
6061 const char *label,
6062 const unsigned char *random, size_t rlen,
6063 unsigned char *dstbuf, size_t dlen)
Jerry Yudc7bd172022-02-17 13:44:15 +08006064{
6065 size_t nb;
6066 size_t i, j, k, md_len;
6067 unsigned char *tmp;
6068 size_t tmp_len = 0;
6069 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
6070 const mbedtls_md_info_t *md_info;
6071 mbedtls_md_context_t md_ctx;
6072 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6073
Gilles Peskine449bd832023-01-11 14:50:10 +01006074 mbedtls_md_init(&md_ctx);
Jerry Yudc7bd172022-02-17 13:44:15 +08006075
Gilles Peskine449bd832023-01-11 14:50:10 +01006076 if ((md_info = mbedtls_md_info_from_type(md_type)) == NULL) {
6077 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
6078 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006079
Gilles Peskine449bd832023-01-11 14:50:10 +01006080 md_len = mbedtls_md_get_size(md_info);
Jerry Yudc7bd172022-02-17 13:44:15 +08006081
Gilles Peskine449bd832023-01-11 14:50:10 +01006082 tmp_len = md_len + strlen(label) + rlen;
6083 tmp = mbedtls_calloc(1, tmp_len);
6084 if (tmp == NULL) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006085 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
6086 goto exit;
6087 }
6088
Gilles Peskine449bd832023-01-11 14:50:10 +01006089 nb = strlen(label);
6090 memcpy(tmp + md_len, label, nb);
6091 memcpy(tmp + md_len + nb, random, rlen);
Jerry Yudc7bd172022-02-17 13:44:15 +08006092 nb += rlen;
6093
6094 /*
6095 * Compute P_<hash>(secret, label + random)[0..dlen]
6096 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006097 if ((ret = mbedtls_md_setup(&md_ctx, md_info, 1)) != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006098 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006099 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006100
Gilles Peskine449bd832023-01-11 14:50:10 +01006101 ret = mbedtls_md_hmac_starts(&md_ctx, secret, slen);
6102 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006103 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006104 }
6105 ret = mbedtls_md_hmac_update(&md_ctx, tmp + md_len, nb);
6106 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006107 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006108 }
6109 ret = mbedtls_md_hmac_finish(&md_ctx, tmp);
6110 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006111 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006112 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006113
Gilles Peskine449bd832023-01-11 14:50:10 +01006114 for (i = 0; i < dlen; i += md_len) {
6115 ret = mbedtls_md_hmac_reset(&md_ctx);
6116 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006117 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006118 }
6119 ret = mbedtls_md_hmac_update(&md_ctx, tmp, md_len + nb);
6120 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006121 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006122 }
6123 ret = mbedtls_md_hmac_finish(&md_ctx, h_i);
6124 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006125 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006126 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006127
Gilles Peskine449bd832023-01-11 14:50:10 +01006128 ret = mbedtls_md_hmac_reset(&md_ctx);
6129 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006130 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006131 }
6132 ret = mbedtls_md_hmac_update(&md_ctx, tmp, md_len);
6133 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006134 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006135 }
6136 ret = mbedtls_md_hmac_finish(&md_ctx, tmp);
6137 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006138 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006139 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006140
Gilles Peskine449bd832023-01-11 14:50:10 +01006141 k = (i + md_len > dlen) ? dlen % md_len : md_len;
Jerry Yudc7bd172022-02-17 13:44:15 +08006142
Gilles Peskine449bd832023-01-11 14:50:10 +01006143 for (j = 0; j < k; j++) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006144 dstbuf[i + j] = h_i[j];
Gilles Peskine449bd832023-01-11 14:50:10 +01006145 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006146 }
6147
6148exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006149 mbedtls_md_free(&md_ctx);
Jerry Yudc7bd172022-02-17 13:44:15 +08006150
Gilles Peskine449bd832023-01-11 14:50:10 +01006151 if (tmp != NULL) {
6152 mbedtls_platform_zeroize(tmp, tmp_len);
6153 }
Dave Rodgman29b9b2b2022-11-01 16:08:14 +00006154
Gilles Peskine449bd832023-01-11 14:50:10 +01006155 mbedtls_platform_zeroize(h_i, sizeof(h_i));
Jerry Yudc7bd172022-02-17 13:44:15 +08006156
Gilles Peskine449bd832023-01-11 14:50:10 +01006157 mbedtls_free(tmp);
Jerry Yudc7bd172022-02-17 13:44:15 +08006158
Gilles Peskine449bd832023-01-11 14:50:10 +01006159 return ret;
Jerry Yudc7bd172022-02-17 13:44:15 +08006160}
Andrzej Kurek57d10632022-10-24 10:32:01 -04006161#endif /* MBEDTLS_MD_C && ( MBEDTLS_SHA256_C || MBEDTLS_SHA384_C ) */
Jerry Yudc7bd172022-02-17 13:44:15 +08006162#endif /* MBEDTLS_USE_PSA_CRYPTO */
6163
Andrzej Kurek25f27152022-08-17 16:09:31 -04006164#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006165MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006166static int tls_prf_sha256(const unsigned char *secret, size_t slen,
6167 const char *label,
6168 const unsigned char *random, size_t rlen,
6169 unsigned char *dstbuf, size_t dlen)
Jerry Yudc7bd172022-02-17 13:44:15 +08006170{
Gilles Peskine449bd832023-01-11 14:50:10 +01006171 return tls_prf_generic(MBEDTLS_MD_SHA256, secret, slen,
6172 label, random, rlen, dstbuf, dlen);
Jerry Yudc7bd172022-02-17 13:44:15 +08006173}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04006174#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudc7bd172022-02-17 13:44:15 +08006175
Andrzej Kurek25f27152022-08-17 16:09:31 -04006176#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006177MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006178static int tls_prf_sha384(const unsigned char *secret, size_t slen,
6179 const char *label,
6180 const unsigned char *random, size_t rlen,
6181 unsigned char *dstbuf, size_t dlen)
Jerry Yudc7bd172022-02-17 13:44:15 +08006182{
Gilles Peskine449bd832023-01-11 14:50:10 +01006183 return tls_prf_generic(MBEDTLS_MD_SHA384, secret, slen,
6184 label, random, rlen, dstbuf, dlen);
Jerry Yudc7bd172022-02-17 13:44:15 +08006185}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04006186#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudc7bd172022-02-17 13:44:15 +08006187
Jerry Yuf009d862022-02-17 14:01:37 +08006188/*
6189 * Set appropriate PRF function and other SSL / TLS1.2 functions
6190 *
6191 * Inputs:
Jerry Yuf009d862022-02-17 14:01:37 +08006192 * - hash associated with the ciphersuite (only used by TLS 1.2)
6193 *
6194 * Outputs:
6195 * - the tls_prf, calc_verify and calc_finished members of handshake structure
6196 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006197MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006198static int ssl_set_handshake_prfs(mbedtls_ssl_handshake_params *handshake,
6199 mbedtls_md_type_t hash)
Jerry Yuf009d862022-02-17 14:01:37 +08006200{
Andrzej Kurek25f27152022-08-17 16:09:31 -04006201#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01006202 if (hash == MBEDTLS_MD_SHA384) {
Jerry Yuf009d862022-02-17 14:01:37 +08006203 handshake->tls_prf = tls_prf_sha384;
6204 handshake->calc_verify = ssl_calc_verify_tls_sha384;
6205 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Gilles Peskine449bd832023-01-11 14:50:10 +01006206 } else
Jerry Yuf009d862022-02-17 14:01:37 +08006207#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04006208#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuf009d862022-02-17 14:01:37 +08006209 {
Ronald Cron81591aa2022-03-07 09:05:51 +01006210 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08006211 handshake->tls_prf = tls_prf_sha256;
6212 handshake->calc_verify = ssl_calc_verify_tls_sha256;
6213 handshake->calc_finished = ssl_calc_finished_tls_sha256;
6214 }
Ronald Cron81591aa2022-03-07 09:05:51 +01006215#else
Jerry Yuf009d862022-02-17 14:01:37 +08006216 {
Jerry Yuf009d862022-02-17 14:01:37 +08006217 (void) handshake;
Ronald Cron81591aa2022-03-07 09:05:51 +01006218 (void) hash;
Gilles Peskine449bd832023-01-11 14:50:10 +01006219 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yuf009d862022-02-17 14:01:37 +08006220 }
Ronald Cron81591aa2022-03-07 09:05:51 +01006221#endif
Jerry Yuf009d862022-02-17 14:01:37 +08006222
Gilles Peskine449bd832023-01-11 14:50:10 +01006223 return 0;
Jerry Yuf009d862022-02-17 14:01:37 +08006224}
Jerry Yud6ab2352022-02-17 14:03:43 +08006225
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006226/*
6227 * Compute master secret if needed
6228 *
6229 * Parameters:
6230 * [in/out] handshake
6231 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
6232 * (PSA-PSK) ciphersuite_info, psk_opaque
6233 * [out] premaster (cleared)
6234 * [out] master
6235 * [in] ssl: optionally used for debugging, EMS and PSA-PSK
6236 * debug: conf->f_dbg, conf->p_dbg
6237 * EMS: passed to calc_verify (debug + session_negotiate)
Ronald Crona25cf582022-03-07 11:10:36 +01006238 * PSA-PSA: conf
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006239 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006240MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006241static int ssl_compute_master(mbedtls_ssl_handshake_params *handshake,
6242 unsigned char *master,
6243 const mbedtls_ssl_context *ssl)
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006244{
6245 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6246
6247 /* cf. RFC 5246, Section 8.1:
6248 * "The master secret is always exactly 48 bytes in length." */
6249 size_t const master_secret_len = 48;
6250
6251#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
6252 unsigned char session_hash[48];
6253#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
6254
6255 /* The label for the KDF used for key expansion.
6256 * This is either "master secret" or "extended master secret"
6257 * depending on whether the Extended Master Secret extension
6258 * is used. */
6259 char const *lbl = "master secret";
6260
Przemek Stekielae4ed302022-04-05 17:15:55 +02006261 /* The seed for the KDF used for key expansion.
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006262 * - If the Extended Master Secret extension is not used,
6263 * this is ClientHello.Random + ServerHello.Random
6264 * (see Sect. 8.1 in RFC 5246).
6265 * - If the Extended Master Secret extension is used,
6266 * this is the transcript of the handshake so far.
6267 * (see Sect. 4 in RFC 7627). */
Przemek Stekielae4ed302022-04-05 17:15:55 +02006268 unsigned char const *seed = handshake->randbytes;
6269 size_t seed_len = 64;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006270
6271#if !defined(MBEDTLS_DEBUG_C) && \
6272 !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \
6273 !(defined(MBEDTLS_USE_PSA_CRYPTO) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01006274 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED))
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006275 ssl = NULL; /* make sure we don't use it except for those cases */
6276 (void) ssl;
6277#endif
6278
Gilles Peskine449bd832023-01-11 14:50:10 +01006279 if (handshake->resume != 0) {
6280 MBEDTLS_SSL_DEBUG_MSG(3, ("no premaster (session resumed)"));
6281 return 0;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006282 }
6283
6284#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Gilles Peskine449bd832023-01-11 14:50:10 +01006285 if (handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED) {
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006286 lbl = "extended master secret";
Przemek Stekielae4ed302022-04-05 17:15:55 +02006287 seed = session_hash;
Gilles Peskine449bd832023-01-11 14:50:10 +01006288 handshake->calc_verify(ssl, session_hash, &seed_len);
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006289
Gilles Peskine449bd832023-01-11 14:50:10 +01006290 MBEDTLS_SSL_DEBUG_BUF(3, "session hash for extended master secret",
6291 session_hash, seed_len);
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006292 }
Przemek Stekiel169bf0b2022-04-29 07:53:29 +02006293#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006294
Przemek Stekiel99114f32022-04-22 11:20:09 +02006295#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Przemek Stekiel8a4b7fd2022-04-28 09:22:22 +02006296 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006297 if (mbedtls_ssl_ciphersuite_uses_psk(handshake->ciphersuite_info) == 1) {
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006298 /* Perform PSK-to-MS expansion in a single step. */
6299 psa_status_t status;
6300 psa_algorithm_t alg;
6301 mbedtls_svc_key_id_t psk;
6302 psa_key_derivation_operation_t derivation =
6303 PSA_KEY_DERIVATION_OPERATION_INIT;
6304 mbedtls_md_type_t hash_alg = handshake->ciphersuite_info->mac;
6305
Gilles Peskine449bd832023-01-11 14:50:10 +01006306 MBEDTLS_SSL_DEBUG_MSG(2, ("perform PSA-based PSK-to-MS expansion"));
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006307
Gilles Peskine449bd832023-01-11 14:50:10 +01006308 psk = mbedtls_ssl_get_opaque_psk(ssl);
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006309
Gilles Peskine449bd832023-01-11 14:50:10 +01006310 if (hash_alg == MBEDTLS_MD_SHA384) {
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006311 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
Gilles Peskine449bd832023-01-11 14:50:10 +01006312 } else {
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006313 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
Gilles Peskine449bd832023-01-11 14:50:10 +01006314 }
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006315
Przemek Stekiel51a1f362022-04-13 08:57:06 +02006316 size_t other_secret_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006317 unsigned char *other_secret = NULL;
Przemek Stekielc2033402022-04-05 17:19:41 +02006318
Gilles Peskine449bd832023-01-11 14:50:10 +01006319 switch (handshake->ciphersuite_info->key_exchange) {
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006320 /* Provide other secret.
Przemek Stekiel51a1f362022-04-13 08:57:06 +02006321 * Other secret is stored in premaster, where first 2 bytes hold the
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006322 * length of the other key.
Przemek Stekielc2033402022-04-05 17:19:41 +02006323 */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006324 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Przemek Stekiel8abcee92022-04-28 09:16:28 +02006325 /* For RSA-PSK other key length is always 48 bytes. */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006326 other_secret_len = 48;
6327 other_secret = handshake->premaster + 2;
6328 break;
6329 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006330 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006331 other_secret_len = MBEDTLS_GET_UINT16_BE(handshake->premaster, 0);
6332 other_secret = handshake->premaster + 2;
6333 break;
6334 default:
6335 break;
Przemek Stekielc2033402022-04-05 17:19:41 +02006336 }
6337
Gilles Peskine449bd832023-01-11 14:50:10 +01006338 status = setup_psa_key_derivation(&derivation, psk, alg,
6339 ssl->conf->psk, ssl->conf->psk_len,
6340 seed, seed_len,
6341 (unsigned char const *) lbl,
6342 (size_t) strlen(lbl),
6343 other_secret, other_secret_len,
6344 master_secret_len);
6345 if (status != PSA_SUCCESS) {
6346 psa_key_derivation_abort(&derivation);
6347 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006348 }
6349
Gilles Peskine449bd832023-01-11 14:50:10 +01006350 status = psa_key_derivation_output_bytes(&derivation,
6351 master,
6352 master_secret_len);
6353 if (status != PSA_SUCCESS) {
6354 psa_key_derivation_abort(&derivation);
6355 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006356 }
6357
Gilles Peskine449bd832023-01-11 14:50:10 +01006358 status = psa_key_derivation_abort(&derivation);
6359 if (status != PSA_SUCCESS) {
6360 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
6361 }
6362 } else
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006363#endif
6364 {
Neil Armstrongca7d5062022-05-31 14:43:23 +02006365#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01006366 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
6367 if (handshake->ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE) {
Neil Armstrongca7d5062022-05-31 14:43:23 +02006368 psa_status_t status;
6369 psa_algorithm_t alg = PSA_ALG_TLS12_ECJPAKE_TO_PMS;
6370 psa_key_derivation_operation_t derivation =
6371 PSA_KEY_DERIVATION_OPERATION_INIT;
6372
Gilles Peskine449bd832023-01-11 14:50:10 +01006373 MBEDTLS_SSL_DEBUG_MSG(2, ("perform PSA-based PMS KDF for ECJPAKE"));
Neil Armstrongca7d5062022-05-31 14:43:23 +02006374
6375 handshake->pmslen = PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE;
6376
Gilles Peskine449bd832023-01-11 14:50:10 +01006377 status = psa_key_derivation_setup(&derivation, alg);
6378 if (status != PSA_SUCCESS) {
6379 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Neil Armstrongca7d5062022-05-31 14:43:23 +02006380 }
6381
Gilles Peskine449bd832023-01-11 14:50:10 +01006382 status = psa_key_derivation_set_capacity(&derivation,
6383 PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE);
6384 if (status != PSA_SUCCESS) {
6385 psa_key_derivation_abort(&derivation);
6386 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Neil Armstrongca7d5062022-05-31 14:43:23 +02006387 }
6388
Gilles Peskine449bd832023-01-11 14:50:10 +01006389 status = psa_pake_get_implicit_key(&handshake->psa_pake_ctx,
6390 &derivation);
6391 if (status != PSA_SUCCESS) {
6392 psa_key_derivation_abort(&derivation);
6393 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Neil Armstrongca7d5062022-05-31 14:43:23 +02006394 }
6395
Gilles Peskine449bd832023-01-11 14:50:10 +01006396 status = psa_key_derivation_output_bytes(&derivation,
6397 handshake->premaster,
6398 handshake->pmslen);
6399 if (status != PSA_SUCCESS) {
6400 psa_key_derivation_abort(&derivation);
6401 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
6402 }
6403
6404 status = psa_key_derivation_abort(&derivation);
6405 if (status != PSA_SUCCESS) {
6406 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Neil Armstrongca7d5062022-05-31 14:43:23 +02006407 }
6408 }
6409#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01006410 ret = handshake->tls_prf(handshake->premaster, handshake->pmslen,
6411 lbl, seed, seed_len,
6412 master,
6413 master_secret_len);
6414 if (ret != 0) {
6415 MBEDTLS_SSL_DEBUG_RET(1, "prf", ret);
6416 return ret;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006417 }
6418
Gilles Peskine449bd832023-01-11 14:50:10 +01006419 MBEDTLS_SSL_DEBUG_BUF(3, "premaster secret",
6420 handshake->premaster,
6421 handshake->pmslen);
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006422
Gilles Peskine449bd832023-01-11 14:50:10 +01006423 mbedtls_platform_zeroize(handshake->premaster,
6424 sizeof(handshake->premaster));
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006425 }
6426
Gilles Peskine449bd832023-01-11 14:50:10 +01006427 return 0;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006428}
6429
Gilles Peskine449bd832023-01-11 14:50:10 +01006430int mbedtls_ssl_derive_keys(mbedtls_ssl_context *ssl)
Jerry Yud62f87e2022-02-17 14:09:02 +08006431{
6432 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6433 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
6434 ssl->handshake->ciphersuite_info;
6435
Gilles Peskine449bd832023-01-11 14:50:10 +01006436 MBEDTLS_SSL_DEBUG_MSG(2, ("=> derive keys"));
Jerry Yud62f87e2022-02-17 14:09:02 +08006437
6438 /* Set PRF, calc_verify and calc_finished function pointers */
Gilles Peskine449bd832023-01-11 14:50:10 +01006439 ret = ssl_set_handshake_prfs(ssl->handshake,
6440 ciphersuite_info->mac);
6441 if (ret != 0) {
6442 MBEDTLS_SSL_DEBUG_RET(1, "ssl_set_handshake_prfs", ret);
6443 return ret;
Jerry Yud62f87e2022-02-17 14:09:02 +08006444 }
6445
6446 /* Compute master secret if needed */
Gilles Peskine449bd832023-01-11 14:50:10 +01006447 ret = ssl_compute_master(ssl->handshake,
6448 ssl->session_negotiate->master,
6449 ssl);
6450 if (ret != 0) {
6451 MBEDTLS_SSL_DEBUG_RET(1, "ssl_compute_master", ret);
6452 return ret;
Jerry Yud62f87e2022-02-17 14:09:02 +08006453 }
6454
6455 /* Swap the client and server random values:
6456 * - MS derivation wanted client+server (RFC 5246 8.1)
6457 * - key derivation wants server+client (RFC 5246 6.3) */
6458 {
6459 unsigned char tmp[64];
Gilles Peskine449bd832023-01-11 14:50:10 +01006460 memcpy(tmp, ssl->handshake->randbytes, 64);
6461 memcpy(ssl->handshake->randbytes, tmp + 32, 32);
6462 memcpy(ssl->handshake->randbytes + 32, tmp, 32);
6463 mbedtls_platform_zeroize(tmp, sizeof(tmp));
Jerry Yud62f87e2022-02-17 14:09:02 +08006464 }
6465
6466 /* Populate transform structure */
Gilles Peskine449bd832023-01-11 14:50:10 +01006467 ret = ssl_tls12_populate_transform(ssl->transform_negotiate,
6468 ssl->session_negotiate->ciphersuite,
6469 ssl->session_negotiate->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02006470#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01006471 ssl->session_negotiate->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02006472#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01006473 ssl->handshake->tls_prf,
6474 ssl->handshake->randbytes,
6475 ssl->tls_version,
6476 ssl->conf->endpoint,
6477 ssl);
6478 if (ret != 0) {
6479 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls12_populate_transform", ret);
6480 return ret;
Jerry Yud62f87e2022-02-17 14:09:02 +08006481 }
6482
6483 /* We no longer need Server/ClientHello.random values */
Gilles Peskine449bd832023-01-11 14:50:10 +01006484 mbedtls_platform_zeroize(ssl->handshake->randbytes,
6485 sizeof(ssl->handshake->randbytes));
Jerry Yud62f87e2022-02-17 14:09:02 +08006486
Gilles Peskine449bd832023-01-11 14:50:10 +01006487 MBEDTLS_SSL_DEBUG_MSG(2, ("<= derive keys"));
Jerry Yud62f87e2022-02-17 14:09:02 +08006488
Gilles Peskine449bd832023-01-11 14:50:10 +01006489 return 0;
Jerry Yud62f87e2022-02-17 14:09:02 +08006490}
Jerry Yu8392e0d2022-02-17 14:10:24 +08006491
Gilles Peskine449bd832023-01-11 14:50:10 +01006492int mbedtls_ssl_set_calc_verify_md(mbedtls_ssl_context *ssl, int md)
Ronald Cron4dcbca92022-03-07 10:21:40 +01006493{
Gilles Peskine449bd832023-01-11 14:50:10 +01006494 switch (md) {
Andrzej Kurek25f27152022-08-17 16:09:31 -04006495#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron4dcbca92022-03-07 10:21:40 +01006496 case MBEDTLS_SSL_HASH_SHA384:
6497 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
6498 break;
6499#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04006500#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron4dcbca92022-03-07 10:21:40 +01006501 case MBEDTLS_SSL_HASH_SHA256:
6502 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
6503 break;
6504#endif
6505 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006506 return -1;
Ronald Cron4dcbca92022-03-07 10:21:40 +01006507 }
Andrzej Kurekeabeb302022-10-17 07:52:51 -04006508#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
6509 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
6510 (void) ssl;
6511#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01006512 return 0;
Ronald Cron4dcbca92022-03-07 10:21:40 +01006513}
6514
Andrzej Kurek25f27152022-08-17 16:09:31 -04006515#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01006516void ssl_calc_verify_tls_sha256(const mbedtls_ssl_context *ssl,
6517 unsigned char *hash,
6518 size_t *hlen)
Jerry Yu8392e0d2022-02-17 14:10:24 +08006519{
6520#if defined(MBEDTLS_USE_PSA_CRYPTO)
6521 size_t hash_size;
6522 psa_status_t status;
6523 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
6524
Gilles Peskine449bd832023-01-11 14:50:10 +01006525 MBEDTLS_SSL_DEBUG_MSG(2, ("=> PSA calc verify sha256"));
6526 status = psa_hash_clone(&ssl->handshake->fin_sha256_psa, &sha256_psa);
6527 if (status != PSA_SUCCESS) {
6528 MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash clone failed"));
Jerry Yu8392e0d2022-02-17 14:10:24 +08006529 return;
6530 }
6531
Gilles Peskine449bd832023-01-11 14:50:10 +01006532 status = psa_hash_finish(&sha256_psa, hash, 32, &hash_size);
6533 if (status != PSA_SUCCESS) {
6534 MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash finish failed"));
Jerry Yu8392e0d2022-02-17 14:10:24 +08006535 return;
6536 }
6537
6538 *hlen = 32;
Gilles Peskine449bd832023-01-11 14:50:10 +01006539 MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated verify result", hash, *hlen);
6540 MBEDTLS_SSL_DEBUG_MSG(2, ("<= PSA calc verify"));
Jerry Yu8392e0d2022-02-17 14:10:24 +08006541#else
6542 mbedtls_sha256_context sha256;
6543
Gilles Peskine449bd832023-01-11 14:50:10 +01006544 mbedtls_sha256_init(&sha256);
Jerry Yu8392e0d2022-02-17 14:10:24 +08006545
Gilles Peskine449bd832023-01-11 14:50:10 +01006546 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc verify sha256"));
Jerry Yu8392e0d2022-02-17 14:10:24 +08006547
Gilles Peskine449bd832023-01-11 14:50:10 +01006548 mbedtls_sha256_clone(&sha256, &ssl->handshake->fin_sha256);
6549 mbedtls_sha256_finish(&sha256, hash);
Jerry Yu8392e0d2022-02-17 14:10:24 +08006550
6551 *hlen = 32;
6552
Gilles Peskine449bd832023-01-11 14:50:10 +01006553 MBEDTLS_SSL_DEBUG_BUF(3, "calculated verify result", hash, *hlen);
6554 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc verify"));
Jerry Yu8392e0d2022-02-17 14:10:24 +08006555
Gilles Peskine449bd832023-01-11 14:50:10 +01006556 mbedtls_sha256_free(&sha256);
Jerry Yu8392e0d2022-02-17 14:10:24 +08006557#endif /* MBEDTLS_USE_PSA_CRYPTO */
6558 return;
6559}
Andrzej Kurek25f27152022-08-17 16:09:31 -04006560#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu8392e0d2022-02-17 14:10:24 +08006561
Andrzej Kurek25f27152022-08-17 16:09:31 -04006562#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01006563void ssl_calc_verify_tls_sha384(const mbedtls_ssl_context *ssl,
6564 unsigned char *hash,
6565 size_t *hlen)
Jerry Yuc1cb3842022-02-17 14:13:48 +08006566{
6567#if defined(MBEDTLS_USE_PSA_CRYPTO)
6568 size_t hash_size;
6569 psa_status_t status;
6570 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
6571
Gilles Peskine449bd832023-01-11 14:50:10 +01006572 MBEDTLS_SSL_DEBUG_MSG(2, ("=> PSA calc verify sha384"));
6573 status = psa_hash_clone(&ssl->handshake->fin_sha384_psa, &sha384_psa);
6574 if (status != PSA_SUCCESS) {
6575 MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash clone failed"));
Jerry Yuc1cb3842022-02-17 14:13:48 +08006576 return;
6577 }
6578
Gilles Peskine449bd832023-01-11 14:50:10 +01006579 status = psa_hash_finish(&sha384_psa, hash, 48, &hash_size);
6580 if (status != PSA_SUCCESS) {
6581 MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash finish failed"));
Jerry Yuc1cb3842022-02-17 14:13:48 +08006582 return;
6583 }
6584
6585 *hlen = 48;
Gilles Peskine449bd832023-01-11 14:50:10 +01006586 MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated verify result", hash, *hlen);
6587 MBEDTLS_SSL_DEBUG_MSG(2, ("<= PSA calc verify"));
Jerry Yuc1cb3842022-02-17 14:13:48 +08006588#else
6589 mbedtls_sha512_context sha512;
6590
Gilles Peskine449bd832023-01-11 14:50:10 +01006591 mbedtls_sha512_init(&sha512);
Jerry Yuc1cb3842022-02-17 14:13:48 +08006592
Gilles Peskine449bd832023-01-11 14:50:10 +01006593 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc verify sha384"));
Jerry Yuc1cb3842022-02-17 14:13:48 +08006594
Gilles Peskine449bd832023-01-11 14:50:10 +01006595 mbedtls_sha512_clone(&sha512, &ssl->handshake->fin_sha384);
6596 mbedtls_sha512_finish(&sha512, hash);
Jerry Yuc1cb3842022-02-17 14:13:48 +08006597
6598 *hlen = 48;
6599
Gilles Peskine449bd832023-01-11 14:50:10 +01006600 MBEDTLS_SSL_DEBUG_BUF(3, "calculated verify result", hash, *hlen);
6601 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc verify"));
Jerry Yuc1cb3842022-02-17 14:13:48 +08006602
Gilles Peskine449bd832023-01-11 14:50:10 +01006603 mbedtls_sha512_free(&sha512);
Jerry Yuc1cb3842022-02-17 14:13:48 +08006604#endif /* MBEDTLS_USE_PSA_CRYPTO */
6605 return;
6606}
Andrzej Kurek25f27152022-08-17 16:09:31 -04006607#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yuc1cb3842022-02-17 14:13:48 +08006608
Neil Armstrong80f6f322022-05-03 17:56:38 +02006609#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
6610 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006611int mbedtls_ssl_psk_derive_premaster(mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex)
Jerry Yuce3dca42022-02-17 14:16:37 +08006612{
6613 unsigned char *p = ssl->handshake->premaster;
Gilles Peskine449bd832023-01-11 14:50:10 +01006614 unsigned char *end = p + sizeof(ssl->handshake->premaster);
Jerry Yuce3dca42022-02-17 14:16:37 +08006615 const unsigned char *psk = NULL;
6616 size_t psk_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006617 int psk_ret = mbedtls_ssl_get_psk(ssl, &psk, &psk_len);
Jerry Yuce3dca42022-02-17 14:16:37 +08006618
Gilles Peskine449bd832023-01-11 14:50:10 +01006619 if (psk_ret == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED) {
Jerry Yuce3dca42022-02-17 14:16:37 +08006620 /*
6621 * This should never happen because the existence of a PSK is always
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006622 * checked before calling this function.
6623 *
6624 * The exception is opaque DHE-PSK. For DHE-PSK fill premaster with
Przemek Stekiel8abcee92022-04-28 09:16:28 +02006625 * the shared secret without PSK.
Jerry Yuce3dca42022-02-17 14:16:37 +08006626 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006627 if (key_ex != MBEDTLS_KEY_EXCHANGE_DHE_PSK) {
6628 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
6629 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006630 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006631 }
6632
6633 /*
6634 * PMS = struct {
6635 * opaque other_secret<0..2^16-1>;
6636 * opaque psk<0..2^16-1>;
6637 * };
6638 * with "other_secret" depending on the particular key exchange
6639 */
6640#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006641 if (key_ex == MBEDTLS_KEY_EXCHANGE_PSK) {
6642 if (end - p < 2) {
6643 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6644 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006645
Gilles Peskine449bd832023-01-11 14:50:10 +01006646 MBEDTLS_PUT_UINT16_BE(psk_len, p, 0);
Jerry Yuce3dca42022-02-17 14:16:37 +08006647 p += 2;
6648
Gilles Peskine449bd832023-01-11 14:50:10 +01006649 if (end < p || (size_t) (end - p) < psk_len) {
6650 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6651 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006652
Gilles Peskine449bd832023-01-11 14:50:10 +01006653 memset(p, 0, psk_len);
Jerry Yuce3dca42022-02-17 14:16:37 +08006654 p += psk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01006655 } else
Jerry Yuce3dca42022-02-17 14:16:37 +08006656#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
6657#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006658 if (key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK) {
Jerry Yuce3dca42022-02-17 14:16:37 +08006659 /*
6660 * other_secret already set by the ClientKeyExchange message,
6661 * and is 48 bytes long
6662 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006663 if (end - p < 2) {
6664 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6665 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006666
6667 *p++ = 0;
6668 *p++ = 48;
6669 p += 48;
Gilles Peskine449bd832023-01-11 14:50:10 +01006670 } else
Jerry Yuce3dca42022-02-17 14:16:37 +08006671#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
6672#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006673 if (key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK) {
Jerry Yuce3dca42022-02-17 14:16:37 +08006674 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6675 size_t len;
6676
6677 /* Write length only when we know the actual value */
Gilles Peskine449bd832023-01-11 14:50:10 +01006678 if ((ret = mbedtls_dhm_calc_secret(&ssl->handshake->dhm_ctx,
6679 p + 2, end - (p + 2), &len,
6680 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
6681 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_calc_secret", ret);
6682 return ret;
Jerry Yuce3dca42022-02-17 14:16:37 +08006683 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006684 MBEDTLS_PUT_UINT16_BE(len, p, 0);
Jerry Yuce3dca42022-02-17 14:16:37 +08006685 p += 2 + len;
6686
Gilles Peskine449bd832023-01-11 14:50:10 +01006687 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: K ", &ssl->handshake->dhm_ctx.K);
6688 } else
Jerry Yuce3dca42022-02-17 14:16:37 +08006689#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Neil Armstrong80f6f322022-05-03 17:56:38 +02006690#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006691 if (key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK) {
Jerry Yuce3dca42022-02-17 14:16:37 +08006692 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6693 size_t zlen;
6694
Gilles Peskine449bd832023-01-11 14:50:10 +01006695 if ((ret = mbedtls_ecdh_calc_secret(&ssl->handshake->ecdh_ctx, &zlen,
6696 p + 2, end - (p + 2),
6697 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
6698 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecdh_calc_secret", ret);
6699 return ret;
Jerry Yuce3dca42022-02-17 14:16:37 +08006700 }
6701
Gilles Peskine449bd832023-01-11 14:50:10 +01006702 MBEDTLS_PUT_UINT16_BE(zlen, p, 0);
Jerry Yuce3dca42022-02-17 14:16:37 +08006703 p += 2 + zlen;
6704
Gilles Peskine449bd832023-01-11 14:50:10 +01006705 MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx,
6706 MBEDTLS_DEBUG_ECDH_Z);
6707 } else
Neil Armstrong80f6f322022-05-03 17:56:38 +02006708#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Jerry Yuce3dca42022-02-17 14:16:37 +08006709 {
Gilles Peskine449bd832023-01-11 14:50:10 +01006710 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
6711 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yuce3dca42022-02-17 14:16:37 +08006712 }
6713
6714 /* opaque psk<0..2^16-1>; */
Gilles Peskine449bd832023-01-11 14:50:10 +01006715 if (end - p < 2) {
6716 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6717 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006718
Gilles Peskine449bd832023-01-11 14:50:10 +01006719 MBEDTLS_PUT_UINT16_BE(psk_len, p, 0);
Jerry Yuce3dca42022-02-17 14:16:37 +08006720 p += 2;
6721
Gilles Peskine449bd832023-01-11 14:50:10 +01006722 if (end < p || (size_t) (end - p) < psk_len) {
6723 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6724 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006725
Gilles Peskine449bd832023-01-11 14:50:10 +01006726 memcpy(p, psk, psk_len);
Jerry Yuce3dca42022-02-17 14:16:37 +08006727 p += psk_len;
6728
6729 ssl->handshake->pmslen = p - ssl->handshake->premaster;
6730
Gilles Peskine449bd832023-01-11 14:50:10 +01006731 return 0;
Jerry Yuce3dca42022-02-17 14:16:37 +08006732}
Neil Armstrong80f6f322022-05-03 17:56:38 +02006733#endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Jerry Yuc2c673d2022-02-17 14:20:39 +08006734
6735#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006736MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006737static int ssl_write_hello_request(mbedtls_ssl_context *ssl);
Jerry Yuc2c673d2022-02-17 14:20:39 +08006738
6739#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006740int mbedtls_ssl_resend_hello_request(mbedtls_ssl_context *ssl)
Jerry Yuc2c673d2022-02-17 14:20:39 +08006741{
6742 /* If renegotiation is not enforced, retransmit until we would reach max
6743 * timeout if we were using the usual handshake doubling scheme */
Gilles Peskine449bd832023-01-11 14:50:10 +01006744 if (ssl->conf->renego_max_records < 0) {
Jerry Yuc2c673d2022-02-17 14:20:39 +08006745 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
6746 unsigned char doublings = 1;
6747
Gilles Peskine449bd832023-01-11 14:50:10 +01006748 while (ratio != 0) {
Jerry Yuc2c673d2022-02-17 14:20:39 +08006749 ++doublings;
6750 ratio >>= 1;
6751 }
6752
Gilles Peskine449bd832023-01-11 14:50:10 +01006753 if (++ssl->renego_records_seen > doublings) {
6754 MBEDTLS_SSL_DEBUG_MSG(2, ("no longer retransmitting hello request"));
6755 return 0;
Jerry Yuc2c673d2022-02-17 14:20:39 +08006756 }
6757 }
6758
Gilles Peskine449bd832023-01-11 14:50:10 +01006759 return ssl_write_hello_request(ssl);
Jerry Yuc2c673d2022-02-17 14:20:39 +08006760}
6761#endif
6762#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Jerry Yud9526692022-02-17 14:23:47 +08006763
Jerry Yud9526692022-02-17 14:23:47 +08006764/*
6765 * Handshake functions
6766 */
6767#if !defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
6768/* No certificate support -> dummy functions */
Gilles Peskine449bd832023-01-11 14:50:10 +01006769int mbedtls_ssl_write_certificate(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08006770{
6771 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6772 ssl->handshake->ciphersuite_info;
6773
Gilles Peskine449bd832023-01-11 14:50:10 +01006774 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006775
Gilles Peskine449bd832023-01-11 14:50:10 +01006776 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
6777 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006778 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01006779 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08006780 }
6781
Gilles Peskine449bd832023-01-11 14:50:10 +01006782 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
6783 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08006784}
6785
Gilles Peskine449bd832023-01-11 14:50:10 +01006786int mbedtls_ssl_parse_certificate(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08006787{
6788 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6789 ssl->handshake->ciphersuite_info;
6790
Gilles Peskine449bd832023-01-11 14:50:10 +01006791 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006792
Gilles Peskine449bd832023-01-11 14:50:10 +01006793 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
6794 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006795 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01006796 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08006797 }
6798
Gilles Peskine449bd832023-01-11 14:50:10 +01006799 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
6800 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08006801}
6802
6803#else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
6804/* Some certificate support -> implement write and parse */
6805
Gilles Peskine449bd832023-01-11 14:50:10 +01006806int mbedtls_ssl_write_certificate(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08006807{
6808 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
6809 size_t i, n;
6810 const mbedtls_x509_crt *crt;
6811 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6812 ssl->handshake->ciphersuite_info;
6813
Gilles Peskine449bd832023-01-11 14:50:10 +01006814 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006815
Gilles Peskine449bd832023-01-11 14:50:10 +01006816 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
6817 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006818 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01006819 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08006820 }
6821
6822#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01006823 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
6824 if (ssl->handshake->client_auth == 0) {
6825 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006826 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01006827 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08006828 }
6829 }
6830#endif /* MBEDTLS_SSL_CLI_C */
6831#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01006832 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
6833 if (mbedtls_ssl_own_cert(ssl) == NULL) {
Jerry Yud9526692022-02-17 14:23:47 +08006834 /* Should never happen because we shouldn't have picked the
6835 * ciphersuite if we don't have a certificate. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006836 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08006837 }
6838 }
6839#endif
6840
Gilles Peskine449bd832023-01-11 14:50:10 +01006841 MBEDTLS_SSL_DEBUG_CRT(3, "own certificate", mbedtls_ssl_own_cert(ssl));
Jerry Yud9526692022-02-17 14:23:47 +08006842
6843 /*
6844 * 0 . 0 handshake type
6845 * 1 . 3 handshake length
6846 * 4 . 6 length of all certs
6847 * 7 . 9 length of cert. 1
6848 * 10 . n-1 peer certificate
6849 * n . n+2 length of cert. 2
6850 * n+3 . ... upper level cert, etc.
6851 */
6852 i = 7;
Gilles Peskine449bd832023-01-11 14:50:10 +01006853 crt = mbedtls_ssl_own_cert(ssl);
Jerry Yud9526692022-02-17 14:23:47 +08006854
Gilles Peskine449bd832023-01-11 14:50:10 +01006855 while (crt != NULL) {
Jerry Yud9526692022-02-17 14:23:47 +08006856 n = crt->raw.len;
Gilles Peskine449bd832023-01-11 14:50:10 +01006857 if (n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i) {
6858 MBEDTLS_SSL_DEBUG_MSG(1, ("certificate too large, %" MBEDTLS_PRINTF_SIZET
6859 " > %" MBEDTLS_PRINTF_SIZET,
6860 i + 3 + n, (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN));
6861 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
Jerry Yud9526692022-02-17 14:23:47 +08006862 }
6863
Gilles Peskine449bd832023-01-11 14:50:10 +01006864 ssl->out_msg[i] = MBEDTLS_BYTE_2(n);
6865 ssl->out_msg[i + 1] = MBEDTLS_BYTE_1(n);
6866 ssl->out_msg[i + 2] = MBEDTLS_BYTE_0(n);
Jerry Yud9526692022-02-17 14:23:47 +08006867
Gilles Peskine449bd832023-01-11 14:50:10 +01006868 i += 3; memcpy(ssl->out_msg + i, crt->raw.p, n);
Jerry Yud9526692022-02-17 14:23:47 +08006869 i += n; crt = crt->next;
6870 }
6871
Gilles Peskine449bd832023-01-11 14:50:10 +01006872 ssl->out_msg[4] = MBEDTLS_BYTE_2(i - 7);
6873 ssl->out_msg[5] = MBEDTLS_BYTE_1(i - 7);
6874 ssl->out_msg[6] = MBEDTLS_BYTE_0(i - 7);
Jerry Yud9526692022-02-17 14:23:47 +08006875
6876 ssl->out_msglen = i;
6877 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6878 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
6879
6880 ssl->state++;
6881
Gilles Peskine449bd832023-01-11 14:50:10 +01006882 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
6883 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
6884 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08006885 }
6886
Gilles Peskine449bd832023-01-11 14:50:10 +01006887 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006888
Gilles Peskine449bd832023-01-11 14:50:10 +01006889 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08006890}
6891
6892#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6893
6894#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006895MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006896static int ssl_check_peer_crt_unchanged(mbedtls_ssl_context *ssl,
6897 unsigned char *crt_buf,
6898 size_t crt_buf_len)
Jerry Yud9526692022-02-17 14:23:47 +08006899{
6900 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6901
Gilles Peskine449bd832023-01-11 14:50:10 +01006902 if (peer_crt == NULL) {
6903 return -1;
6904 }
Jerry Yud9526692022-02-17 14:23:47 +08006905
Gilles Peskine449bd832023-01-11 14:50:10 +01006906 if (peer_crt->raw.len != crt_buf_len) {
6907 return -1;
6908 }
Jerry Yud9526692022-02-17 14:23:47 +08006909
Gilles Peskine449bd832023-01-11 14:50:10 +01006910 return memcmp(peer_crt->raw.p, crt_buf, peer_crt->raw.len);
Jerry Yud9526692022-02-17 14:23:47 +08006911}
6912#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006913MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006914static int ssl_check_peer_crt_unchanged(mbedtls_ssl_context *ssl,
6915 unsigned char *crt_buf,
6916 size_t crt_buf_len)
Jerry Yud9526692022-02-17 14:23:47 +08006917{
6918 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6919 unsigned char const * const peer_cert_digest =
6920 ssl->session->peer_cert_digest;
6921 mbedtls_md_type_t const peer_cert_digest_type =
6922 ssl->session->peer_cert_digest_type;
6923 mbedtls_md_info_t const * const digest_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01006924 mbedtls_md_info_from_type(peer_cert_digest_type);
Jerry Yud9526692022-02-17 14:23:47 +08006925 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6926 size_t digest_len;
6927
Gilles Peskine449bd832023-01-11 14:50:10 +01006928 if (peer_cert_digest == NULL || digest_info == NULL) {
6929 return -1;
6930 }
Jerry Yud9526692022-02-17 14:23:47 +08006931
Gilles Peskine449bd832023-01-11 14:50:10 +01006932 digest_len = mbedtls_md_get_size(digest_info);
6933 if (digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN) {
6934 return -1;
6935 }
Jerry Yud9526692022-02-17 14:23:47 +08006936
Gilles Peskine449bd832023-01-11 14:50:10 +01006937 ret = mbedtls_md(digest_info, crt_buf, crt_buf_len, tmp_digest);
6938 if (ret != 0) {
6939 return -1;
6940 }
Jerry Yud9526692022-02-17 14:23:47 +08006941
Gilles Peskine449bd832023-01-11 14:50:10 +01006942 return memcmp(tmp_digest, peer_cert_digest, digest_len);
Jerry Yud9526692022-02-17 14:23:47 +08006943}
6944#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6945#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6946
6947/*
6948 * Once the certificate message is read, parse it into a cert chain and
6949 * perform basic checks, but leave actual verification to the caller
6950 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006951MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006952static int ssl_parse_certificate_chain(mbedtls_ssl_context *ssl,
6953 mbedtls_x509_crt *chain)
Jerry Yud9526692022-02-17 14:23:47 +08006954{
6955 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6956#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01006957 int crt_cnt = 0;
Jerry Yud9526692022-02-17 14:23:47 +08006958#endif
6959 size_t i, n;
6960 uint8_t alert;
6961
Gilles Peskine449bd832023-01-11 14:50:10 +01006962 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
6963 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
6964 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6965 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
6966 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Jerry Yud9526692022-02-17 14:23:47 +08006967 }
6968
Gilles Peskine449bd832023-01-11 14:50:10 +01006969 if (ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE) {
6970 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6971 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
6972 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Jerry Yud9526692022-02-17 14:23:47 +08006973 }
6974
Gilles Peskine449bd832023-01-11 14:50:10 +01006975 if (ssl->in_hslen < mbedtls_ssl_hs_hdr_len(ssl) + 3 + 3) {
6976 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
6977 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6978 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
6979 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08006980 }
6981
Gilles Peskine449bd832023-01-11 14:50:10 +01006982 i = mbedtls_ssl_hs_hdr_len(ssl);
Jerry Yud9526692022-02-17 14:23:47 +08006983
6984 /*
6985 * Same message structure as in mbedtls_ssl_write_certificate()
6986 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006987 n = (ssl->in_msg[i+1] << 8) | ssl->in_msg[i+2];
Jerry Yud9526692022-02-17 14:23:47 +08006988
Gilles Peskine449bd832023-01-11 14:50:10 +01006989 if (ssl->in_msg[i] != 0 ||
6990 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len(ssl)) {
6991 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
6992 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6993 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
6994 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08006995 }
6996
6997 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6998 i += 3;
6999
7000 /* Iterate through and parse the CRTs in the provided chain. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007001 while (i < ssl->in_hslen) {
Jerry Yud9526692022-02-17 14:23:47 +08007002 /* Check that there's room for the next CRT's length fields. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007003 if (i + 3 > ssl->in_hslen) {
7004 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7005 mbedtls_ssl_send_alert_message(ssl,
7006 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7007 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
7008 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007009 }
7010 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
7011 * anything beyond 2**16 ~ 64K. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007012 if (ssl->in_msg[i] != 0) {
7013 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7014 mbedtls_ssl_send_alert_message(ssl,
7015 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7016 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT);
7017 return MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Jerry Yud9526692022-02-17 14:23:47 +08007018 }
7019
7020 /* Read length of the next CRT in the chain. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007021 n = ((unsigned int) ssl->in_msg[i + 1] << 8)
Jerry Yud9526692022-02-17 14:23:47 +08007022 | (unsigned int) ssl->in_msg[i + 2];
7023 i += 3;
7024
Gilles Peskine449bd832023-01-11 14:50:10 +01007025 if (n < 128 || i + n > ssl->in_hslen) {
7026 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7027 mbedtls_ssl_send_alert_message(ssl,
7028 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7029 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
7030 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007031 }
7032
7033 /* Check if we're handling the first CRT in the chain. */
7034#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007035 if (crt_cnt++ == 0 &&
Jerry Yud9526692022-02-17 14:23:47 +08007036 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Gilles Peskine449bd832023-01-11 14:50:10 +01007037 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
Jerry Yud9526692022-02-17 14:23:47 +08007038 /* During client-side renegotiation, check that the server's
7039 * end-CRTs hasn't changed compared to the initial handshake,
7040 * mitigating the triple handshake attack. On success, reuse
7041 * the original end-CRT instead of parsing it again. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007042 MBEDTLS_SSL_DEBUG_MSG(3, ("Check that peer CRT hasn't changed during renegotiation"));
7043 if (ssl_check_peer_crt_unchanged(ssl,
7044 &ssl->in_msg[i],
7045 n) != 0) {
7046 MBEDTLS_SSL_DEBUG_MSG(1, ("new server cert during renegotiation"));
7047 mbedtls_ssl_send_alert_message(ssl,
7048 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7049 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED);
7050 return MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Jerry Yud9526692022-02-17 14:23:47 +08007051 }
7052
7053 /* Now we can safely free the original chain. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007054 ssl_clear_peer_cert(ssl->session);
Jerry Yud9526692022-02-17 14:23:47 +08007055 }
7056#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
7057
7058 /* Parse the next certificate in the chain. */
7059#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007060 ret = mbedtls_x509_crt_parse_der(chain, ssl->in_msg + i, n);
Jerry Yud9526692022-02-17 14:23:47 +08007061#else
7062 /* If we don't need to store the CRT chain permanently, parse
7063 * it in-place from the input buffer instead of making a copy. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007064 ret = mbedtls_x509_crt_parse_der_nocopy(chain, ssl->in_msg + i, n);
Jerry Yud9526692022-02-17 14:23:47 +08007065#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007066 switch (ret) {
Jerry Yud9526692022-02-17 14:23:47 +08007067 case 0: /*ok*/
7068 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
7069 /* Ignore certificate with an unknown algorithm: maybe a
7070 prior certificate was already trusted. */
7071 break;
7072
7073 case MBEDTLS_ERR_X509_ALLOC_FAILED:
7074 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
7075 goto crt_parse_der_failed;
7076
7077 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
7078 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7079 goto crt_parse_der_failed;
7080
7081 default:
7082 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007083crt_parse_der_failed:
7084 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert);
7085 MBEDTLS_SSL_DEBUG_RET(1, " mbedtls_x509_crt_parse_der", ret);
7086 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007087 }
7088
7089 i += n;
7090 }
7091
Gilles Peskine449bd832023-01-11 14:50:10 +01007092 MBEDTLS_SSL_DEBUG_CRT(3, "peer certificate", chain);
7093 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08007094}
7095
7096#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007097MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007098static int ssl_srv_check_client_no_crt_notification(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08007099{
Gilles Peskine449bd832023-01-11 14:50:10 +01007100 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
7101 return -1;
7102 }
Jerry Yud9526692022-02-17 14:23:47 +08007103
Gilles Peskine449bd832023-01-11 14:50:10 +01007104 if (ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len(ssl) &&
Jerry Yud9526692022-02-17 14:23:47 +08007105 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
7106 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
Gilles Peskine449bd832023-01-11 14:50:10 +01007107 memcmp(ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl), "\0\0\0", 3) == 0) {
7108 MBEDTLS_SSL_DEBUG_MSG(1, ("peer has no certificate"));
7109 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08007110 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007111 return -1;
Jerry Yud9526692022-02-17 14:23:47 +08007112}
7113#endif /* MBEDTLS_SSL_SRV_C */
7114
7115/* Check if a certificate message is expected.
7116 * Return either
7117 * - SSL_CERTIFICATE_EXPECTED, or
7118 * - SSL_CERTIFICATE_SKIP
7119 * indicating whether a Certificate message is expected or not.
7120 */
7121#define SSL_CERTIFICATE_EXPECTED 0
7122#define SSL_CERTIFICATE_SKIP 1
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007123MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007124static int ssl_parse_certificate_coordinate(mbedtls_ssl_context *ssl,
7125 int authmode)
Jerry Yud9526692022-02-17 14:23:47 +08007126{
7127 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
7128 ssl->handshake->ciphersuite_info;
7129
Gilles Peskine449bd832023-01-11 14:50:10 +01007130 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
7131 return SSL_CERTIFICATE_SKIP;
7132 }
Jerry Yud9526692022-02-17 14:23:47 +08007133
7134#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007135 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
7136 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK) {
7137 return SSL_CERTIFICATE_SKIP;
7138 }
Jerry Yud9526692022-02-17 14:23:47 +08007139
Gilles Peskine449bd832023-01-11 14:50:10 +01007140 if (authmode == MBEDTLS_SSL_VERIFY_NONE) {
Jerry Yud9526692022-02-17 14:23:47 +08007141 ssl->session_negotiate->verify_result =
7142 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
Gilles Peskine449bd832023-01-11 14:50:10 +01007143 return SSL_CERTIFICATE_SKIP;
Jerry Yud9526692022-02-17 14:23:47 +08007144 }
7145 }
7146#else
7147 ((void) authmode);
7148#endif /* MBEDTLS_SSL_SRV_C */
7149
Gilles Peskine449bd832023-01-11 14:50:10 +01007150 return SSL_CERTIFICATE_EXPECTED;
Jerry Yud9526692022-02-17 14:23:47 +08007151}
7152
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007153MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007154static int ssl_parse_certificate_verify(mbedtls_ssl_context *ssl,
7155 int authmode,
7156 mbedtls_x509_crt *chain,
7157 void *rs_ctx)
Jerry Yud9526692022-02-17 14:23:47 +08007158{
7159 int ret = 0;
7160 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
7161 ssl->handshake->ciphersuite_info;
7162 int have_ca_chain = 0;
7163
7164 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
7165 void *p_vrfy;
7166
Gilles Peskine449bd832023-01-11 14:50:10 +01007167 if (authmode == MBEDTLS_SSL_VERIFY_NONE) {
7168 return 0;
7169 }
Jerry Yud9526692022-02-17 14:23:47 +08007170
Gilles Peskine449bd832023-01-11 14:50:10 +01007171 if (ssl->f_vrfy != NULL) {
7172 MBEDTLS_SSL_DEBUG_MSG(3, ("Use context-specific verification callback"));
Jerry Yud9526692022-02-17 14:23:47 +08007173 f_vrfy = ssl->f_vrfy;
7174 p_vrfy = ssl->p_vrfy;
Gilles Peskine449bd832023-01-11 14:50:10 +01007175 } else {
7176 MBEDTLS_SSL_DEBUG_MSG(3, ("Use configuration-specific verification callback"));
Jerry Yud9526692022-02-17 14:23:47 +08007177 f_vrfy = ssl->conf->f_vrfy;
7178 p_vrfy = ssl->conf->p_vrfy;
7179 }
7180
7181 /*
7182 * Main check: verify certificate
7183 */
7184#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Gilles Peskine449bd832023-01-11 14:50:10 +01007185 if (ssl->conf->f_ca_cb != NULL) {
Jerry Yud9526692022-02-17 14:23:47 +08007186 ((void) rs_ctx);
7187 have_ca_chain = 1;
7188
Gilles Peskine449bd832023-01-11 14:50:10 +01007189 MBEDTLS_SSL_DEBUG_MSG(3, ("use CA callback for X.509 CRT verification"));
Jerry Yud9526692022-02-17 14:23:47 +08007190 ret = mbedtls_x509_crt_verify_with_ca_cb(
7191 chain,
7192 ssl->conf->f_ca_cb,
7193 ssl->conf->p_ca_cb,
7194 ssl->conf->cert_profile,
7195 ssl->hostname,
7196 &ssl->session_negotiate->verify_result,
Gilles Peskine449bd832023-01-11 14:50:10 +01007197 f_vrfy, p_vrfy);
7198 } else
Jerry Yud9526692022-02-17 14:23:47 +08007199#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
7200 {
7201 mbedtls_x509_crt *ca_chain;
7202 mbedtls_x509_crl *ca_crl;
7203
7204#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01007205 if (ssl->handshake->sni_ca_chain != NULL) {
Jerry Yud9526692022-02-17 14:23:47 +08007206 ca_chain = ssl->handshake->sni_ca_chain;
7207 ca_crl = ssl->handshake->sni_ca_crl;
Gilles Peskine449bd832023-01-11 14:50:10 +01007208 } else
Jerry Yud9526692022-02-17 14:23:47 +08007209#endif
7210 {
7211 ca_chain = ssl->conf->ca_chain;
7212 ca_crl = ssl->conf->ca_crl;
7213 }
7214
Gilles Peskine449bd832023-01-11 14:50:10 +01007215 if (ca_chain != NULL) {
Jerry Yud9526692022-02-17 14:23:47 +08007216 have_ca_chain = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01007217 }
Jerry Yud9526692022-02-17 14:23:47 +08007218
7219 ret = mbedtls_x509_crt_verify_restartable(
7220 chain,
7221 ca_chain, ca_crl,
7222 ssl->conf->cert_profile,
7223 ssl->hostname,
7224 &ssl->session_negotiate->verify_result,
Gilles Peskine449bd832023-01-11 14:50:10 +01007225 f_vrfy, p_vrfy, rs_ctx);
Jerry Yud9526692022-02-17 14:23:47 +08007226 }
7227
Gilles Peskine449bd832023-01-11 14:50:10 +01007228 if (ret != 0) {
7229 MBEDTLS_SSL_DEBUG_RET(1, "x509_verify_cert", ret);
Jerry Yud9526692022-02-17 14:23:47 +08007230 }
7231
7232#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007233 if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
7234 return MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
7235 }
Jerry Yud9526692022-02-17 14:23:47 +08007236#endif
7237
7238 /*
7239 * Secondary checks: always done, but change 'ret' only if it was 0
7240 */
7241
7242#if defined(MBEDTLS_ECP_C)
7243 {
7244 const mbedtls_pk_context *pk = &chain->pk;
7245
Manuel Pégourié-Gonnard66b0d612022-06-17 10:49:29 +02007246 /* If certificate uses an EC key, make sure the curve is OK.
7247 * This is a public key, so it can't be opaque, so can_do() is a good
7248 * enough check to ensure pk_ec() is safe to use here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007249 if (mbedtls_pk_can_do(pk, MBEDTLS_PK_ECKEY)) {
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007250 /* and in the unlikely case the above assumption no longer holds
7251 * we are making sure that pk_ec() here does not return a NULL
7252 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007253 const mbedtls_ecp_keypair *ec = mbedtls_pk_ec(*pk);
7254 if (ec == NULL) {
7255 MBEDTLS_SSL_DEBUG_MSG(1, ("mbedtls_pk_ec() returned NULL"));
7256 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007257 }
Jerry Yud9526692022-02-17 14:23:47 +08007258
Gilles Peskine449bd832023-01-11 14:50:10 +01007259 if (mbedtls_ssl_check_curve(ssl, ec->grp.id) != 0) {
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007260 ssl->session_negotiate->verify_result |=
7261 MBEDTLS_X509_BADCERT_BAD_KEY;
7262
Gilles Peskine449bd832023-01-11 14:50:10 +01007263 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate (EC key curve)"));
7264 if (ret == 0) {
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007265 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Gilles Peskine449bd832023-01-11 14:50:10 +01007266 }
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007267 }
Jerry Yud9526692022-02-17 14:23:47 +08007268 }
7269 }
7270#endif /* MBEDTLS_ECP_C */
7271
Gilles Peskine449bd832023-01-11 14:50:10 +01007272 if (mbedtls_ssl_check_cert_usage(chain,
7273 ciphersuite_info,
7274 !ssl->conf->endpoint,
7275 &ssl->session_negotiate->verify_result) != 0) {
7276 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate (usage extensions)"));
7277 if (ret == 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007278 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Gilles Peskine449bd832023-01-11 14:50:10 +01007279 }
Jerry Yud9526692022-02-17 14:23:47 +08007280 }
7281
7282 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
7283 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
7284 * with details encoded in the verification flags. All other kinds
7285 * of error codes, including those from the user provided f_vrfy
7286 * functions, are treated as fatal and lead to a failure of
7287 * ssl_parse_certificate even if verification was optional. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007288 if (authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
7289 (ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
7290 ret == MBEDTLS_ERR_SSL_BAD_CERTIFICATE)) {
Jerry Yud9526692022-02-17 14:23:47 +08007291 ret = 0;
7292 }
7293
Gilles Peskine449bd832023-01-11 14:50:10 +01007294 if (have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED) {
7295 MBEDTLS_SSL_DEBUG_MSG(1, ("got no CA chain"));
Jerry Yud9526692022-02-17 14:23:47 +08007296 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
7297 }
7298
Gilles Peskine449bd832023-01-11 14:50:10 +01007299 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007300 uint8_t alert;
7301
7302 /* The certificate may have been rejected for several reasons.
7303 Pick one and send the corresponding alert. Which alert to send
7304 may be a subject of debate in some cases. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007305 if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER) {
Jerry Yud9526692022-02-17 14:23:47 +08007306 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
Gilles Peskine449bd832023-01-11 14:50:10 +01007307 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH) {
Jerry Yud9526692022-02-17 14:23:47 +08007308 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007309 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE) {
Jerry Yud9526692022-02-17 14:23:47 +08007310 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007311 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE) {
Jerry Yud9526692022-02-17 14:23:47 +08007312 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007313 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE) {
Jerry Yud9526692022-02-17 14:23:47 +08007314 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007315 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK) {
Jerry Yud9526692022-02-17 14:23:47 +08007316 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007317 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY) {
Jerry Yud9526692022-02-17 14:23:47 +08007318 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007319 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED) {
Jerry Yud9526692022-02-17 14:23:47 +08007320 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
Gilles Peskine449bd832023-01-11 14:50:10 +01007321 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED) {
Jerry Yud9526692022-02-17 14:23:47 +08007322 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
Gilles Peskine449bd832023-01-11 14:50:10 +01007323 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED) {
Jerry Yud9526692022-02-17 14:23:47 +08007324 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
Gilles Peskine449bd832023-01-11 14:50:10 +01007325 } else {
Jerry Yud9526692022-02-17 14:23:47 +08007326 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
Gilles Peskine449bd832023-01-11 14:50:10 +01007327 }
7328 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7329 alert);
Jerry Yud9526692022-02-17 14:23:47 +08007330 }
7331
7332#if defined(MBEDTLS_DEBUG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007333 if (ssl->session_negotiate->verify_result != 0) {
7334 MBEDTLS_SSL_DEBUG_MSG(3, ("! Certificate verification flags %08x",
7335 (unsigned int) ssl->session_negotiate->verify_result));
7336 } else {
7337 MBEDTLS_SSL_DEBUG_MSG(3, ("Certificate verification flags clear"));
Jerry Yud9526692022-02-17 14:23:47 +08007338 }
7339#endif /* MBEDTLS_DEBUG_C */
7340
Gilles Peskine449bd832023-01-11 14:50:10 +01007341 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007342}
7343
7344#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007345MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007346static int ssl_remember_peer_crt_digest(mbedtls_ssl_context *ssl,
7347 unsigned char *start, size_t len)
Jerry Yud9526692022-02-17 14:23:47 +08007348{
7349 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7350 /* Remember digest of the peer's end-CRT. */
7351 ssl->session_negotiate->peer_cert_digest =
Gilles Peskine449bd832023-01-11 14:50:10 +01007352 mbedtls_calloc(1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN);
7353 if (ssl->session_negotiate->peer_cert_digest == NULL) {
7354 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%d bytes) failed",
7355 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN));
7356 mbedtls_ssl_send_alert_message(ssl,
7357 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7358 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
Jerry Yud9526692022-02-17 14:23:47 +08007359
Gilles Peskine449bd832023-01-11 14:50:10 +01007360 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Jerry Yud9526692022-02-17 14:23:47 +08007361 }
7362
Gilles Peskine449bd832023-01-11 14:50:10 +01007363 ret = mbedtls_md(mbedtls_md_info_from_type(
7364 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE),
7365 start, len,
7366 ssl->session_negotiate->peer_cert_digest);
Jerry Yud9526692022-02-17 14:23:47 +08007367
7368 ssl->session_negotiate->peer_cert_digest_type =
7369 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
7370 ssl->session_negotiate->peer_cert_digest_len =
7371 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
7372
Gilles Peskine449bd832023-01-11 14:50:10 +01007373 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007374}
7375
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007376MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007377static int ssl_remember_peer_pubkey(mbedtls_ssl_context *ssl,
7378 unsigned char *start, size_t len)
Jerry Yud9526692022-02-17 14:23:47 +08007379{
7380 unsigned char *end = start + len;
7381 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7382
7383 /* Make a copy of the peer's raw public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007384 mbedtls_pk_init(&ssl->handshake->peer_pubkey);
7385 ret = mbedtls_pk_parse_subpubkey(&start, end,
7386 &ssl->handshake->peer_pubkey);
7387 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007388 /* We should have parsed the public key before. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007389 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007390 }
7391
Gilles Peskine449bd832023-01-11 14:50:10 +01007392 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08007393}
7394#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7395
Gilles Peskine449bd832023-01-11 14:50:10 +01007396int mbedtls_ssl_parse_certificate(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08007397{
7398 int ret = 0;
7399 int crt_expected;
7400#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7401 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
7402 ? ssl->handshake->sni_authmode
7403 : ssl->conf->authmode;
7404#else
7405 const int authmode = ssl->conf->authmode;
7406#endif
7407 void *rs_ctx = NULL;
7408 mbedtls_x509_crt *chain = NULL;
7409
Gilles Peskine449bd832023-01-11 14:50:10 +01007410 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007411
Gilles Peskine449bd832023-01-11 14:50:10 +01007412 crt_expected = ssl_parse_certificate_coordinate(ssl, authmode);
7413 if (crt_expected == SSL_CERTIFICATE_SKIP) {
7414 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007415 goto exit;
7416 }
7417
7418#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007419 if (ssl->handshake->ecrs_enabled &&
7420 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify) {
Jerry Yud9526692022-02-17 14:23:47 +08007421 chain = ssl->handshake->ecrs_peer_cert;
7422 ssl->handshake->ecrs_peer_cert = NULL;
7423 goto crt_verify;
7424 }
7425#endif
7426
Gilles Peskine449bd832023-01-11 14:50:10 +01007427 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007428 /* mbedtls_ssl_read_record may have sent an alert already. We
7429 let it decide whether to alert. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007430 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
Jerry Yud9526692022-02-17 14:23:47 +08007431 goto exit;
7432 }
7433
7434#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007435 if (ssl_srv_check_client_no_crt_notification(ssl) == 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007436 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
7437
Gilles Peskine449bd832023-01-11 14:50:10 +01007438 if (authmode != MBEDTLS_SSL_VERIFY_OPTIONAL) {
Jerry Yud9526692022-02-17 14:23:47 +08007439 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Gilles Peskine449bd832023-01-11 14:50:10 +01007440 }
Jerry Yud9526692022-02-17 14:23:47 +08007441
7442 goto exit;
7443 }
7444#endif /* MBEDTLS_SSL_SRV_C */
7445
7446 /* Clear existing peer CRT structure in case we tried to
7447 * reuse a session but it failed, and allocate a new one. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007448 ssl_clear_peer_cert(ssl->session_negotiate);
Jerry Yud9526692022-02-17 14:23:47 +08007449
Gilles Peskine449bd832023-01-11 14:50:10 +01007450 chain = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
7451 if (chain == NULL) {
7452 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed",
7453 sizeof(mbedtls_x509_crt)));
7454 mbedtls_ssl_send_alert_message(ssl,
7455 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7456 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
Jerry Yud9526692022-02-17 14:23:47 +08007457
7458 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
7459 goto exit;
7460 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007461 mbedtls_x509_crt_init(chain);
Jerry Yud9526692022-02-17 14:23:47 +08007462
Gilles Peskine449bd832023-01-11 14:50:10 +01007463 ret = ssl_parse_certificate_chain(ssl, chain);
7464 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007465 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007466 }
Jerry Yud9526692022-02-17 14:23:47 +08007467
7468#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007469 if (ssl->handshake->ecrs_enabled) {
Jerry Yud9526692022-02-17 14:23:47 +08007470 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Gilles Peskine449bd832023-01-11 14:50:10 +01007471 }
Jerry Yud9526692022-02-17 14:23:47 +08007472
7473crt_verify:
Gilles Peskine449bd832023-01-11 14:50:10 +01007474 if (ssl->handshake->ecrs_enabled) {
Jerry Yud9526692022-02-17 14:23:47 +08007475 rs_ctx = &ssl->handshake->ecrs_ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +01007476 }
Jerry Yud9526692022-02-17 14:23:47 +08007477#endif
7478
Gilles Peskine449bd832023-01-11 14:50:10 +01007479 ret = ssl_parse_certificate_verify(ssl, authmode,
7480 chain, rs_ctx);
7481 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007482 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007483 }
Jerry Yud9526692022-02-17 14:23:47 +08007484
7485#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7486 {
7487 unsigned char *crt_start, *pk_start;
7488 size_t crt_len, pk_len;
7489
7490 /* We parse the CRT chain without copying, so
7491 * these pointers point into the input buffer,
7492 * and are hence still valid after freeing the
7493 * CRT chain. */
7494
7495 crt_start = chain->raw.p;
7496 crt_len = chain->raw.len;
7497
7498 pk_start = chain->pk_raw.p;
7499 pk_len = chain->pk_raw.len;
7500
7501 /* Free the CRT structures before computing
7502 * digest and copying the peer's public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007503 mbedtls_x509_crt_free(chain);
7504 mbedtls_free(chain);
Jerry Yud9526692022-02-17 14:23:47 +08007505 chain = NULL;
7506
Gilles Peskine449bd832023-01-11 14:50:10 +01007507 ret = ssl_remember_peer_crt_digest(ssl, crt_start, crt_len);
7508 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007509 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007510 }
Jerry Yud9526692022-02-17 14:23:47 +08007511
Gilles Peskine449bd832023-01-11 14:50:10 +01007512 ret = ssl_remember_peer_pubkey(ssl, pk_start, pk_len);
7513 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007514 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007515 }
Jerry Yud9526692022-02-17 14:23:47 +08007516 }
7517#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7518 /* Pass ownership to session structure. */
7519 ssl->session_negotiate->peer_cert = chain;
7520 chain = NULL;
7521#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7522
Gilles Peskine449bd832023-01-11 14:50:10 +01007523 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007524
7525exit:
7526
Gilles Peskine449bd832023-01-11 14:50:10 +01007527 if (ret == 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007528 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01007529 }
Jerry Yud9526692022-02-17 14:23:47 +08007530
7531#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007532 if (ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS) {
Jerry Yud9526692022-02-17 14:23:47 +08007533 ssl->handshake->ecrs_peer_cert = chain;
7534 chain = NULL;
7535 }
7536#endif
7537
Gilles Peskine449bd832023-01-11 14:50:10 +01007538 if (chain != NULL) {
7539 mbedtls_x509_crt_free(chain);
7540 mbedtls_free(chain);
Jerry Yud9526692022-02-17 14:23:47 +08007541 }
7542
Gilles Peskine449bd832023-01-11 14:50:10 +01007543 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007544}
7545#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
7546
Andrzej Kurek25f27152022-08-17 16:09:31 -04007547#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu615bd6f2022-02-17 14:25:15 +08007548static void ssl_calc_finished_tls_sha256(
Gilles Peskine449bd832023-01-11 14:50:10 +01007549 mbedtls_ssl_context *ssl, unsigned char *buf, int from)
Jerry Yu615bd6f2022-02-17 14:25:15 +08007550{
7551 int len = 12;
7552 const char *sender;
7553 unsigned char padbuf[32];
7554#if defined(MBEDTLS_USE_PSA_CRYPTO)
7555 size_t hash_size;
7556 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
7557 psa_status_t status;
7558#else
7559 mbedtls_sha256_context sha256;
7560#endif
7561
7562 mbedtls_ssl_session *session = ssl->session_negotiate;
Gilles Peskine449bd832023-01-11 14:50:10 +01007563 if (!session) {
Jerry Yu615bd6f2022-02-17 14:25:15 +08007564 session = ssl->session;
Gilles Peskine449bd832023-01-11 14:50:10 +01007565 }
Jerry Yu615bd6f2022-02-17 14:25:15 +08007566
Gilles Peskine449bd832023-01-11 14:50:10 +01007567 sender = (from == MBEDTLS_SSL_IS_CLIENT)
Jerry Yu615bd6f2022-02-17 14:25:15 +08007568 ? "client finished"
7569 : "server finished";
7570
7571#if defined(MBEDTLS_USE_PSA_CRYPTO)
7572 sha256_psa = psa_hash_operation_init();
7573
Gilles Peskine449bd832023-01-11 14:50:10 +01007574 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc PSA finished tls sha256"));
Jerry Yu615bd6f2022-02-17 14:25:15 +08007575
Gilles Peskine449bd832023-01-11 14:50:10 +01007576 status = psa_hash_clone(&ssl->handshake->fin_sha256_psa, &sha256_psa);
7577 if (status != PSA_SUCCESS) {
7578 MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash clone failed"));
Jerry Yu615bd6f2022-02-17 14:25:15 +08007579 return;
7580 }
7581
Gilles Peskine449bd832023-01-11 14:50:10 +01007582 status = psa_hash_finish(&sha256_psa, padbuf, sizeof(padbuf), &hash_size);
7583 if (status != PSA_SUCCESS) {
7584 MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash finish failed"));
Jerry Yu615bd6f2022-02-17 14:25:15 +08007585 return;
7586 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007587 MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated padbuf", padbuf, 32);
Jerry Yu615bd6f2022-02-17 14:25:15 +08007588#else
7589
Gilles Peskine449bd832023-01-11 14:50:10 +01007590 mbedtls_sha256_init(&sha256);
Jerry Yu615bd6f2022-02-17 14:25:15 +08007591
Gilles Peskine449bd832023-01-11 14:50:10 +01007592 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc finished tls sha256"));
Jerry Yu615bd6f2022-02-17 14:25:15 +08007593
Gilles Peskine449bd832023-01-11 14:50:10 +01007594 mbedtls_sha256_clone(&sha256, &ssl->handshake->fin_sha256);
Jerry Yu615bd6f2022-02-17 14:25:15 +08007595
7596 /*
7597 * TLSv1.2:
7598 * hash = PRF( master, finished_label,
7599 * Hash( handshake ) )[0.11]
7600 */
7601
7602#if !defined(MBEDTLS_SHA256_ALT)
Gilles Peskine449bd832023-01-11 14:50:10 +01007603 MBEDTLS_SSL_DEBUG_BUF(4, "finished sha2 state", (unsigned char *)
7604 sha256.state, sizeof(sha256.state));
Jerry Yu615bd6f2022-02-17 14:25:15 +08007605#endif
7606
Gilles Peskine449bd832023-01-11 14:50:10 +01007607 mbedtls_sha256_finish(&sha256, padbuf);
7608 mbedtls_sha256_free(&sha256);
Jerry Yu615bd6f2022-02-17 14:25:15 +08007609#endif /* MBEDTLS_USE_PSA_CRYPTO */
7610
Gilles Peskine449bd832023-01-11 14:50:10 +01007611 ssl->handshake->tls_prf(session->master, 48, sender,
7612 padbuf, 32, buf, len);
Jerry Yu615bd6f2022-02-17 14:25:15 +08007613
Gilles Peskine449bd832023-01-11 14:50:10 +01007614 MBEDTLS_SSL_DEBUG_BUF(3, "calc finished result", buf, len);
Jerry Yu615bd6f2022-02-17 14:25:15 +08007615
Gilles Peskine449bd832023-01-11 14:50:10 +01007616 mbedtls_platform_zeroize(padbuf, sizeof(padbuf));
Jerry Yu615bd6f2022-02-17 14:25:15 +08007617
Gilles Peskine449bd832023-01-11 14:50:10 +01007618 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc finished"));
Jerry Yu615bd6f2022-02-17 14:25:15 +08007619}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04007620#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yu615bd6f2022-02-17 14:25:15 +08007621
Jerry Yub7ba49e2022-02-17 14:25:53 +08007622
Andrzej Kurek25f27152022-08-17 16:09:31 -04007623#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yub7ba49e2022-02-17 14:25:53 +08007624static void ssl_calc_finished_tls_sha384(
Gilles Peskine449bd832023-01-11 14:50:10 +01007625 mbedtls_ssl_context *ssl, unsigned char *buf, int from)
Jerry Yub7ba49e2022-02-17 14:25:53 +08007626{
7627 int len = 12;
7628 const char *sender;
7629 unsigned char padbuf[48];
7630#if defined(MBEDTLS_USE_PSA_CRYPTO)
7631 size_t hash_size;
7632 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
7633 psa_status_t status;
7634#else
7635 mbedtls_sha512_context sha512;
7636#endif
7637
7638 mbedtls_ssl_session *session = ssl->session_negotiate;
Gilles Peskine449bd832023-01-11 14:50:10 +01007639 if (!session) {
Jerry Yub7ba49e2022-02-17 14:25:53 +08007640 session = ssl->session;
Gilles Peskine449bd832023-01-11 14:50:10 +01007641 }
Jerry Yub7ba49e2022-02-17 14:25:53 +08007642
Gilles Peskine449bd832023-01-11 14:50:10 +01007643 sender = (from == MBEDTLS_SSL_IS_CLIENT)
Jerry Yub7ba49e2022-02-17 14:25:53 +08007644 ? "client finished"
7645 : "server finished";
7646
7647#if defined(MBEDTLS_USE_PSA_CRYPTO)
7648 sha384_psa = psa_hash_operation_init();
7649
Gilles Peskine449bd832023-01-11 14:50:10 +01007650 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc PSA finished tls sha384"));
Jerry Yub7ba49e2022-02-17 14:25:53 +08007651
Gilles Peskine449bd832023-01-11 14:50:10 +01007652 status = psa_hash_clone(&ssl->handshake->fin_sha384_psa, &sha384_psa);
7653 if (status != PSA_SUCCESS) {
7654 MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash clone failed"));
Jerry Yub7ba49e2022-02-17 14:25:53 +08007655 return;
7656 }
7657
Gilles Peskine449bd832023-01-11 14:50:10 +01007658 status = psa_hash_finish(&sha384_psa, padbuf, sizeof(padbuf), &hash_size);
7659 if (status != PSA_SUCCESS) {
7660 MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash finish failed"));
Jerry Yub7ba49e2022-02-17 14:25:53 +08007661 return;
7662 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007663 MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated padbuf", padbuf, 48);
Jerry Yub7ba49e2022-02-17 14:25:53 +08007664#else
Gilles Peskine449bd832023-01-11 14:50:10 +01007665 mbedtls_sha512_init(&sha512);
Jerry Yub7ba49e2022-02-17 14:25:53 +08007666
Gilles Peskine449bd832023-01-11 14:50:10 +01007667 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc finished tls sha384"));
Jerry Yub7ba49e2022-02-17 14:25:53 +08007668
Gilles Peskine449bd832023-01-11 14:50:10 +01007669 mbedtls_sha512_clone(&sha512, &ssl->handshake->fin_sha384);
Jerry Yub7ba49e2022-02-17 14:25:53 +08007670
7671 /*
7672 * TLSv1.2:
7673 * hash = PRF( master, finished_label,
7674 * Hash( handshake ) )[0.11]
7675 */
7676
7677#if !defined(MBEDTLS_SHA512_ALT)
Gilles Peskine449bd832023-01-11 14:50:10 +01007678 MBEDTLS_SSL_DEBUG_BUF(4, "finished sha512 state", (unsigned char *)
7679 sha512.state, sizeof(sha512.state));
Jerry Yub7ba49e2022-02-17 14:25:53 +08007680#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01007681 mbedtls_sha512_finish(&sha512, padbuf);
Jerry Yub7ba49e2022-02-17 14:25:53 +08007682
Gilles Peskine449bd832023-01-11 14:50:10 +01007683 mbedtls_sha512_free(&sha512);
Jerry Yub7ba49e2022-02-17 14:25:53 +08007684#endif
7685
Gilles Peskine449bd832023-01-11 14:50:10 +01007686 ssl->handshake->tls_prf(session->master, 48, sender,
7687 padbuf, 48, buf, len);
Jerry Yub7ba49e2022-02-17 14:25:53 +08007688
Gilles Peskine449bd832023-01-11 14:50:10 +01007689 MBEDTLS_SSL_DEBUG_BUF(3, "calc finished result", buf, len);
Jerry Yub7ba49e2022-02-17 14:25:53 +08007690
Gilles Peskine449bd832023-01-11 14:50:10 +01007691 mbedtls_platform_zeroize(padbuf, sizeof(padbuf));
Jerry Yub7ba49e2022-02-17 14:25:53 +08007692
Gilles Peskine449bd832023-01-11 14:50:10 +01007693 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc finished"));
Jerry Yub7ba49e2022-02-17 14:25:53 +08007694}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04007695#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yub7ba49e2022-02-17 14:25:53 +08007696
Gilles Peskine449bd832023-01-11 14:50:10 +01007697void mbedtls_ssl_handshake_wrapup_free_hs_transform(mbedtls_ssl_context *ssl)
Jerry Yuaef00152022-02-17 14:27:31 +08007698{
Gilles Peskine449bd832023-01-11 14:50:10 +01007699 MBEDTLS_SSL_DEBUG_MSG(3, ("=> handshake wrapup: final free"));
Jerry Yuaef00152022-02-17 14:27:31 +08007700
7701 /*
7702 * Free our handshake params
7703 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007704 mbedtls_ssl_handshake_free(ssl);
7705 mbedtls_free(ssl->handshake);
Jerry Yuaef00152022-02-17 14:27:31 +08007706 ssl->handshake = NULL;
7707
7708 /*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08007709 * Free the previous transform and switch in the current one
Jerry Yuaef00152022-02-17 14:27:31 +08007710 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007711 if (ssl->transform) {
7712 mbedtls_ssl_transform_free(ssl->transform);
7713 mbedtls_free(ssl->transform);
Jerry Yuaef00152022-02-17 14:27:31 +08007714 }
7715 ssl->transform = ssl->transform_negotiate;
7716 ssl->transform_negotiate = NULL;
7717
Gilles Peskine449bd832023-01-11 14:50:10 +01007718 MBEDTLS_SSL_DEBUG_MSG(3, ("<= handshake wrapup: final free"));
Jerry Yuaef00152022-02-17 14:27:31 +08007719}
7720
Gilles Peskine449bd832023-01-11 14:50:10 +01007721void mbedtls_ssl_handshake_wrapup(mbedtls_ssl_context *ssl)
Jerry Yu2a9fff52022-02-17 14:28:51 +08007722{
7723 int resume = ssl->handshake->resume;
7724
Gilles Peskine449bd832023-01-11 14:50:10 +01007725 MBEDTLS_SSL_DEBUG_MSG(3, ("=> handshake wrapup"));
Jerry Yu2a9fff52022-02-17 14:28:51 +08007726
7727#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01007728 if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
Jerry Yu2a9fff52022-02-17 14:28:51 +08007729 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
7730 ssl->renego_records_seen = 0;
7731 }
7732#endif
7733
7734 /*
7735 * Free the previous session and switch in the current one
7736 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007737 if (ssl->session) {
Jerry Yu2a9fff52022-02-17 14:28:51 +08007738#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
7739 /* RFC 7366 3.1: keep the EtM state */
7740 ssl->session_negotiate->encrypt_then_mac =
Gilles Peskine449bd832023-01-11 14:50:10 +01007741 ssl->session->encrypt_then_mac;
Jerry Yu2a9fff52022-02-17 14:28:51 +08007742#endif
7743
Gilles Peskine449bd832023-01-11 14:50:10 +01007744 mbedtls_ssl_session_free(ssl->session);
7745 mbedtls_free(ssl->session);
Jerry Yu2a9fff52022-02-17 14:28:51 +08007746 }
7747 ssl->session = ssl->session_negotiate;
7748 ssl->session_negotiate = NULL;
7749
7750 /*
7751 * Add cache entry
7752 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007753 if (ssl->conf->f_set_cache != NULL &&
Jerry Yu2a9fff52022-02-17 14:28:51 +08007754 ssl->session->id_len != 0 &&
Gilles Peskine449bd832023-01-11 14:50:10 +01007755 resume == 0) {
7756 if (ssl->conf->f_set_cache(ssl->conf->p_cache,
7757 ssl->session->id,
7758 ssl->session->id_len,
7759 ssl->session) != 0) {
7760 MBEDTLS_SSL_DEBUG_MSG(1, ("cache did not store session"));
7761 }
Jerry Yu2a9fff52022-02-17 14:28:51 +08007762 }
7763
7764#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007765 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7766 ssl->handshake->flight != NULL) {
Jerry Yu2a9fff52022-02-17 14:28:51 +08007767 /* Cancel handshake timer */
Gilles Peskine449bd832023-01-11 14:50:10 +01007768 mbedtls_ssl_set_timer(ssl, 0);
Jerry Yu2a9fff52022-02-17 14:28:51 +08007769
7770 /* Keep last flight around in case we need to resend it:
7771 * we need the handshake and transform structures for that */
Gilles Peskine449bd832023-01-11 14:50:10 +01007772 MBEDTLS_SSL_DEBUG_MSG(3, ("skip freeing handshake and transform"));
7773 } else
Jerry Yu2a9fff52022-02-17 14:28:51 +08007774#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01007775 mbedtls_ssl_handshake_wrapup_free_hs_transform(ssl);
Jerry Yu2a9fff52022-02-17 14:28:51 +08007776
Jerry Yu5ed73ff2022-10-27 13:08:42 +08007777 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
Jerry Yu2a9fff52022-02-17 14:28:51 +08007778
Gilles Peskine449bd832023-01-11 14:50:10 +01007779 MBEDTLS_SSL_DEBUG_MSG(3, ("<= handshake wrapup"));
Jerry Yu2a9fff52022-02-17 14:28:51 +08007780}
7781
Gilles Peskine449bd832023-01-11 14:50:10 +01007782int mbedtls_ssl_write_finished(mbedtls_ssl_context *ssl)
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007783{
7784 int ret, hash_len;
7785
Gilles Peskine449bd832023-01-11 14:50:10 +01007786 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write finished"));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007787
Gilles Peskine449bd832023-01-11 14:50:10 +01007788 mbedtls_ssl_update_out_pointers(ssl, ssl->transform_negotiate);
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007789
Gilles Peskine449bd832023-01-11 14:50:10 +01007790 ssl->handshake->calc_finished(ssl, ssl->out_msg + 4, ssl->conf->endpoint);
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007791
7792 /*
7793 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7794 * may define some other value. Currently (early 2016), no defined
7795 * ciphersuite does this (and this is unlikely to change as activity has
7796 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7797 */
7798 hash_len = 12;
7799
7800#if defined(MBEDTLS_SSL_RENEGOTIATION)
7801 ssl->verify_data_len = hash_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01007802 memcpy(ssl->own_verify_data, ssl->out_msg + 4, hash_len);
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007803#endif
7804
7805 ssl->out_msglen = 4 + hash_len;
7806 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7807 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
7808
7809 /*
7810 * In case of session resuming, invert the client and server
7811 * ChangeCipherSpec messages order.
7812 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007813 if (ssl->handshake->resume != 0) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007814#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007815 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007816 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Gilles Peskine449bd832023-01-11 14:50:10 +01007817 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007818#endif
7819#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007820 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007821 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Gilles Peskine449bd832023-01-11 14:50:10 +01007822 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007823#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01007824 } else {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007825 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01007826 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007827
7828 /*
7829 * Switch to our negotiated transform and session parameters for outbound
7830 * data.
7831 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007832 MBEDTLS_SSL_DEBUG_MSG(3, ("switching to new transform spec for outbound data"));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007833
7834#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007835 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007836 unsigned char i;
7837
7838 /* Remember current epoch settings for resending */
7839 ssl->handshake->alt_transform_out = ssl->transform_out;
Gilles Peskine449bd832023-01-11 14:50:10 +01007840 memcpy(ssl->handshake->alt_out_ctr, ssl->cur_out_ctr,
7841 sizeof(ssl->handshake->alt_out_ctr));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007842
7843 /* Set sequence_number to zero */
Gilles Peskine449bd832023-01-11 14:50:10 +01007844 memset(&ssl->cur_out_ctr[2], 0, sizeof(ssl->cur_out_ctr) - 2);
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007845
7846
7847 /* Increment epoch */
Gilles Peskine449bd832023-01-11 14:50:10 +01007848 for (i = 2; i > 0; i--) {
7849 if (++ssl->cur_out_ctr[i - 1] != 0) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007850 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01007851 }
7852 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007853
7854 /* The loop goes to its end iff the counter is wrapping */
Gilles Peskine449bd832023-01-11 14:50:10 +01007855 if (i == 0) {
7856 MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS epoch would wrap"));
7857 return MBEDTLS_ERR_SSL_COUNTER_WRAPPING;
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007858 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007859 } else
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007860#endif /* MBEDTLS_SSL_PROTO_DTLS */
Gilles Peskine449bd832023-01-11 14:50:10 +01007861 memset(ssl->cur_out_ctr, 0, sizeof(ssl->cur_out_ctr));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007862
7863 ssl->transform_out = ssl->transform_negotiate;
7864 ssl->session_out = ssl->session_negotiate;
7865
7866#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007867 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
7868 mbedtls_ssl_send_flight_completed(ssl);
7869 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007870#endif
7871
Gilles Peskine449bd832023-01-11 14:50:10 +01007872 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
7873 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
7874 return ret;
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007875 }
7876
7877#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007878 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7879 (ret = mbedtls_ssl_flight_transmit(ssl)) != 0) {
7880 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_flight_transmit", ret);
7881 return ret;
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007882 }
7883#endif
7884
Gilles Peskine449bd832023-01-11 14:50:10 +01007885 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write finished"));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007886
Gilles Peskine449bd832023-01-11 14:50:10 +01007887 return 0;
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007888}
7889
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007890#define SSL_MAX_HASH_LEN 12
7891
Gilles Peskine449bd832023-01-11 14:50:10 +01007892int mbedtls_ssl_parse_finished(mbedtls_ssl_context *ssl)
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007893{
7894 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7895 unsigned int hash_len = 12;
7896 unsigned char buf[SSL_MAX_HASH_LEN];
7897
Gilles Peskine449bd832023-01-11 14:50:10 +01007898 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse finished"));
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007899
Gilles Peskine449bd832023-01-11 14:50:10 +01007900 ssl->handshake->calc_finished(ssl, buf, ssl->conf->endpoint ^ 1);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007901
Gilles Peskine449bd832023-01-11 14:50:10 +01007902 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
7903 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007904 goto exit;
7905 }
7906
Gilles Peskine449bd832023-01-11 14:50:10 +01007907 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
7908 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
7909 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7910 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007911 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
7912 goto exit;
7913 }
7914
Gilles Peskine449bd832023-01-11 14:50:10 +01007915 if (ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED) {
7916 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7917 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007918 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
7919 goto exit;
7920 }
7921
Gilles Peskine449bd832023-01-11 14:50:10 +01007922 if (ssl->in_hslen != mbedtls_ssl_hs_hdr_len(ssl) + hash_len) {
7923 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
7924 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7925 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007926 ret = MBEDTLS_ERR_SSL_DECODE_ERROR;
7927 goto exit;
7928 }
7929
Gilles Peskine449bd832023-01-11 14:50:10 +01007930 if (mbedtls_ct_memcmp(ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl),
7931 buf, hash_len) != 0) {
7932 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
7933 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7934 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007935 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
7936 goto exit;
7937 }
7938
7939#if defined(MBEDTLS_SSL_RENEGOTIATION)
7940 ssl->verify_data_len = hash_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01007941 memcpy(ssl->peer_verify_data, buf, hash_len);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007942#endif
7943
Gilles Peskine449bd832023-01-11 14:50:10 +01007944 if (ssl->handshake->resume != 0) {
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007945#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007946 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007947 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Gilles Peskine449bd832023-01-11 14:50:10 +01007948 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007949#endif
7950#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007951 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007952 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Gilles Peskine449bd832023-01-11 14:50:10 +01007953 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007954#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01007955 } else {
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007956 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01007957 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007958
7959#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007960 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
7961 mbedtls_ssl_recv_flight_completed(ssl);
7962 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007963#endif
7964
Gilles Peskine449bd832023-01-11 14:50:10 +01007965 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse finished"));
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007966
7967exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007968 mbedtls_platform_zeroize(buf, hash_len);
7969 return ret;
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007970}
7971
Jerry Yu392112c2022-02-17 14:34:10 +08007972#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
7973/*
7974 * Helper to get TLS 1.2 PRF from ciphersuite
7975 * (Duplicates bits of logic from ssl_set_handshake_prfs().)
7976 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007977static tls_prf_fn ssl_tls12prf_from_cs(int ciphersuite_id)
Jerry Yu392112c2022-02-17 14:34:10 +08007978{
Jerry Yu392112c2022-02-17 14:34:10 +08007979 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01007980 mbedtls_ssl_ciphersuite_from_id(ciphersuite_id);
Andrzej Kurek68327742022-10-03 06:18:18 -04007981#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01007982 if (ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA384) {
7983 return tls_prf_sha384;
7984 } else
Jerry Yu392112c2022-02-17 14:34:10 +08007985#endif
Andrzej Kurek894edde2022-09-29 06:31:14 -04007986#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
7987 {
Gilles Peskine449bd832023-01-11 14:50:10 +01007988 if (ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA256) {
7989 return tls_prf_sha256;
7990 }
Andrzej Kurek894edde2022-09-29 06:31:14 -04007991 }
7992#endif
7993#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
7994 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
7995 (void) ciphersuite_info;
7996#endif
Andrzej Kurekeabeb302022-10-17 07:52:51 -04007997
Gilles Peskine449bd832023-01-11 14:50:10 +01007998 return NULL;
Jerry Yu392112c2022-02-17 14:34:10 +08007999}
8000#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Jerry Yue93ffcd2022-02-17 14:37:06 +08008001
Gilles Peskine449bd832023-01-11 14:50:10 +01008002static mbedtls_tls_prf_types tls_prf_get_type(mbedtls_ssl_tls_prf_cb *tls_prf)
Jerry Yue93ffcd2022-02-17 14:37:06 +08008003{
8004 ((void) tls_prf);
Andrzej Kurek25f27152022-08-17 16:09:31 -04008005#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01008006 if (tls_prf == tls_prf_sha384) {
8007 return MBEDTLS_SSL_TLS_PRF_SHA384;
8008 } else
Jerry Yue93ffcd2022-02-17 14:37:06 +08008009#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04008010#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01008011 if (tls_prf == tls_prf_sha256) {
8012 return MBEDTLS_SSL_TLS_PRF_SHA256;
8013 } else
Jerry Yue93ffcd2022-02-17 14:37:06 +08008014#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01008015 return MBEDTLS_SSL_TLS_PRF_NONE;
Jerry Yue93ffcd2022-02-17 14:37:06 +08008016}
8017
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008018/*
8019 * Populate a transform structure with session keys and all the other
8020 * necessary information.
8021 *
8022 * Parameters:
8023 * - [in/out]: transform: structure to populate
8024 * [in] must be just initialised with mbedtls_ssl_transform_init()
8025 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
8026 * - [in] ciphersuite
8027 * - [in] master
8028 * - [in] encrypt_then_mac
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008029 * - [in] tls_prf: pointer to PRF to use for key derivation
8030 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
Glenn Strauss07c64162022-03-14 12:34:51 -04008031 * - [in] tls_version: TLS version
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008032 * - [in] endpoint: client or server
8033 * - [in] ssl: used for:
8034 * - ssl->conf->{f,p}_export_keys
8035 * [in] optionally used for:
8036 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
8037 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02008038MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01008039static int ssl_tls12_populate_transform(mbedtls_ssl_transform *transform,
8040 int ciphersuite,
8041 const unsigned char master[48],
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008042#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01008043 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008044#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01008045 ssl_tls_prf_t tls_prf,
8046 const unsigned char randbytes[64],
8047 mbedtls_ssl_protocol_version tls_version,
8048 unsigned endpoint,
8049 const mbedtls_ssl_context *ssl)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008050{
8051 int ret = 0;
8052 unsigned char keyblk[256];
8053 unsigned char *key1;
8054 unsigned char *key2;
8055 unsigned char *mac_enc;
8056 unsigned char *mac_dec;
8057 size_t mac_key_len = 0;
8058 size_t iv_copy_len;
8059 size_t keylen;
8060 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008061 mbedtls_ssl_mode_t ssl_mode;
Neil Armstronge4512952022-03-08 09:08:22 +01008062#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008063 const mbedtls_cipher_info_t *cipher_info;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008064 const mbedtls_md_info_t *md_info;
Neil Armstronge4512952022-03-08 09:08:22 +01008065#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008066
8067#if defined(MBEDTLS_USE_PSA_CRYPTO)
8068 psa_key_type_t key_type;
8069 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8070 psa_algorithm_t alg;
Neil Armstronge4512952022-03-08 09:08:22 +01008071 psa_algorithm_t mac_alg = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008072 size_t key_bits;
8073 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
8074#endif
8075
8076#if !defined(MBEDTLS_DEBUG_C) && \
8077 !defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Gilles Peskine449bd832023-01-11 14:50:10 +01008078 if (ssl->f_export_keys == NULL) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008079 ssl = NULL; /* make sure we don't use it except for these cases */
8080 (void) ssl;
8081 }
8082#endif
8083
8084 /*
8085 * Some data just needs copying into the structure
8086 */
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008087#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008088 transform->encrypt_then_mac = encrypt_then_mac;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008089#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Glenn Strauss07c64162022-03-14 12:34:51 -04008090 transform->tls_version = tls_version;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008091
8092#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01008093 memcpy(transform->randbytes, randbytes, sizeof(transform->randbytes));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008094#endif
8095
8096#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01008097 if (tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008098 /* At the moment, we keep TLS <= 1.2 and TLS 1.3 transform
8099 * generation separate. This should never happen. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008100 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008101 }
8102#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
8103
8104 /*
8105 * Get various info structures
8106 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008107 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(ciphersuite);
8108 if (ciphersuite_info == NULL) {
8109 MBEDTLS_SSL_DEBUG_MSG(1, ("ciphersuite info for %d not found",
8110 ciphersuite));
8111 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008112 }
8113
Neil Armstrongab555e02022-04-04 11:07:59 +02008114 ssl_mode = mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008115#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01008116 encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008117#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01008118 ciphersuite_info);
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008119
Gilles Peskine449bd832023-01-11 14:50:10 +01008120 if (ssl_mode == MBEDTLS_SSL_MODE_AEAD) {
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008121 transform->taglen =
8122 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Gilles Peskine449bd832023-01-11 14:50:10 +01008123 }
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008124
8125#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008126 if ((status = mbedtls_ssl_cipher_to_psa(ciphersuite_info->cipher,
8127 transform->taglen,
8128 &alg,
8129 &key_type,
8130 &key_bits)) != PSA_SUCCESS) {
8131 ret = psa_ssl_status_to_mbedtls(status);
8132 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_cipher_to_psa", ret);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008133 goto end;
8134 }
8135#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008136 cipher_info = mbedtls_cipher_info_from_type(ciphersuite_info->cipher);
8137 if (cipher_info == NULL) {
8138 MBEDTLS_SSL_DEBUG_MSG(1, ("cipher info for %u not found",
8139 ciphersuite_info->cipher));
8140 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008141 }
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008142#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008143
Neil Armstronge4512952022-03-08 09:08:22 +01008144#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008145 mac_alg = mbedtls_hash_info_psa_from_md(ciphersuite_info->mac);
8146 if (mac_alg == 0) {
8147 MBEDTLS_SSL_DEBUG_MSG(1, ("mbedtls_hash_info_psa_from_md for %u not found",
8148 (unsigned) ciphersuite_info->mac));
8149 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Neil Armstronge4512952022-03-08 09:08:22 +01008150 }
8151#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008152 md_info = mbedtls_md_info_from_type(ciphersuite_info->mac);
8153 if (md_info == NULL) {
8154 MBEDTLS_SSL_DEBUG_MSG(1, ("mbedtls_md info for %u not found",
8155 (unsigned) ciphersuite_info->mac));
8156 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008157 }
Neil Armstronge4512952022-03-08 09:08:22 +01008158#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008159
8160#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
8161 /* Copy own and peer's CID if the use of the CID
8162 * extension has been negotiated. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008163 if (ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED) {
8164 MBEDTLS_SSL_DEBUG_MSG(3, ("Copy CIDs into SSL transform"));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008165
8166 transform->in_cid_len = ssl->own_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008167 memcpy(transform->in_cid, ssl->own_cid, ssl->own_cid_len);
8168 MBEDTLS_SSL_DEBUG_BUF(3, "Incoming CID", transform->in_cid,
8169 transform->in_cid_len);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008170
8171 transform->out_cid_len = ssl->handshake->peer_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008172 memcpy(transform->out_cid, ssl->handshake->peer_cid,
8173 ssl->handshake->peer_cid_len);
8174 MBEDTLS_SSL_DEBUG_BUF(3, "Outgoing CID", transform->out_cid,
8175 transform->out_cid_len);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008176 }
8177#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
8178
8179 /*
8180 * Compute key block using the PRF
8181 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008182 ret = tls_prf(master, 48, "key expansion", randbytes, 64, keyblk, 256);
8183 if (ret != 0) {
8184 MBEDTLS_SSL_DEBUG_RET(1, "prf", ret);
8185 return ret;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008186 }
8187
Gilles Peskine449bd832023-01-11 14:50:10 +01008188 MBEDTLS_SSL_DEBUG_MSG(3, ("ciphersuite = %s",
8189 mbedtls_ssl_get_ciphersuite_name(ciphersuite)));
8190 MBEDTLS_SSL_DEBUG_BUF(3, "master secret", master, 48);
8191 MBEDTLS_SSL_DEBUG_BUF(4, "random bytes", randbytes, 64);
8192 MBEDTLS_SSL_DEBUG_BUF(4, "key block", keyblk, 256);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008193
8194 /*
8195 * Determine the appropriate key, IV and MAC length.
8196 */
8197
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008198#if defined(MBEDTLS_USE_PSA_CRYPTO)
8199 keylen = PSA_BITS_TO_BYTES(key_bits);
8200#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008201 keylen = mbedtls_cipher_info_get_key_bitlen(cipher_info) / 8;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008202#endif
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008203
8204#if defined(MBEDTLS_GCM_C) || \
8205 defined(MBEDTLS_CCM_C) || \
8206 defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008207 if (ssl_mode == MBEDTLS_SSL_MODE_AEAD) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008208 size_t explicit_ivlen;
8209
8210 transform->maclen = 0;
8211 mac_key_len = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008212
8213 /* All modes haves 96-bit IVs, but the length of the static parts vary
8214 * with mode and version:
8215 * - For GCM and CCM in TLS 1.2, there's a static IV of 4 Bytes
8216 * (to be concatenated with a dynamically chosen IV of 8 Bytes)
8217 * - For ChaChaPoly in TLS 1.2, and all modes in TLS 1.3, there's
8218 * a static IV of 12 Bytes (to be XOR'ed with the 8 Byte record
8219 * sequence number).
8220 */
8221 transform->ivlen = 12;
David Horstmann3b2276a2022-10-06 14:49:08 +01008222
8223 int is_chachapoly = 0;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008224#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008225 is_chachapoly = (key_type == PSA_KEY_TYPE_CHACHA20);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008226#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008227 is_chachapoly = (mbedtls_cipher_info_get_mode(cipher_info)
8228 == MBEDTLS_MODE_CHACHAPOLY);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008229#endif /* MBEDTLS_USE_PSA_CRYPTO */
David Horstmann3b2276a2022-10-06 14:49:08 +01008230
Gilles Peskine449bd832023-01-11 14:50:10 +01008231 if (is_chachapoly) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008232 transform->fixed_ivlen = 12;
Gilles Peskine449bd832023-01-11 14:50:10 +01008233 } else {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008234 transform->fixed_ivlen = 4;
Gilles Peskine449bd832023-01-11 14:50:10 +01008235 }
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008236
8237 /* Minimum length of encrypted record */
8238 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
8239 transform->minlen = explicit_ivlen + transform->taglen;
Gilles Peskine449bd832023-01-11 14:50:10 +01008240 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008241#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
8242#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01008243 if (ssl_mode == MBEDTLS_SSL_MODE_STREAM ||
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008244 ssl_mode == MBEDTLS_SSL_MODE_CBC ||
Gilles Peskine449bd832023-01-11 14:50:10 +01008245 ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM) {
Neil Armstronge4512952022-03-08 09:08:22 +01008246#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008247 size_t block_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008248#else
8249 size_t block_size = cipher_info->block_size;
8250#endif /* MBEDTLS_USE_PSA_CRYPTO */
8251
8252#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronge4512952022-03-08 09:08:22 +01008253 /* Get MAC length */
8254 mac_key_len = PSA_HASH_LENGTH(mac_alg);
8255#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008256 /* Initialize HMAC contexts */
Gilles Peskine449bd832023-01-11 14:50:10 +01008257 if ((ret = mbedtls_md_setup(&transform->md_ctx_enc, md_info, 1)) != 0 ||
8258 (ret = mbedtls_md_setup(&transform->md_ctx_dec, md_info, 1)) != 0) {
8259 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_setup", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008260 goto end;
8261 }
8262
8263 /* Get MAC length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008264 mac_key_len = mbedtls_md_get_size(md_info);
Neil Armstronge4512952022-03-08 09:08:22 +01008265#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008266 transform->maclen = mac_key_len;
8267
8268 /* IV length */
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008269#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008270 transform->ivlen = PSA_CIPHER_IV_LENGTH(key_type, alg);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008271#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008272 transform->ivlen = cipher_info->iv_size;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008273#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008274
8275 /* Minimum length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008276 if (ssl_mode == MBEDTLS_SSL_MODE_STREAM) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008277 transform->minlen = transform->maclen;
Gilles Peskine449bd832023-01-11 14:50:10 +01008278 } else {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008279 /*
8280 * GenericBlockCipher:
8281 * 1. if EtM is in use: one block plus MAC
8282 * otherwise: * first multiple of blocklen greater than maclen
8283 * 2. IV
8284 */
8285#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01008286 if (ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008287 transform->minlen = transform->maclen
Gilles Peskine449bd832023-01-11 14:50:10 +01008288 + block_size;
8289 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008290#endif
8291 {
8292 transform->minlen = transform->maclen
Gilles Peskine449bd832023-01-11 14:50:10 +01008293 + block_size
8294 - transform->maclen % block_size;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008295 }
8296
Gilles Peskine449bd832023-01-11 14:50:10 +01008297 if (tls_version == MBEDTLS_SSL_VERSION_TLS1_2) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008298 transform->minlen += transform->ivlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01008299 } else {
8300 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008301 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
8302 goto end;
8303 }
8304 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008305 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008306#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
8307 {
Gilles Peskine449bd832023-01-11 14:50:10 +01008308 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
8309 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008310 }
8311
Gilles Peskine449bd832023-01-11 14:50:10 +01008312 MBEDTLS_SSL_DEBUG_MSG(3, ("keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
8313 (unsigned) keylen,
8314 (unsigned) transform->minlen,
8315 (unsigned) transform->ivlen,
8316 (unsigned) transform->maclen));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008317
8318 /*
8319 * Finally setup the cipher contexts, IVs and MAC secrets.
8320 */
8321#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008322 if (endpoint == MBEDTLS_SSL_IS_CLIENT) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008323 key1 = keyblk + mac_key_len * 2;
8324 key2 = keyblk + mac_key_len * 2 + keylen;
8325
8326 mac_enc = keyblk;
8327 mac_dec = keyblk + mac_key_len;
8328
Gilles Peskine449bd832023-01-11 14:50:10 +01008329 iv_copy_len = (transform->fixed_ivlen) ?
8330 transform->fixed_ivlen : transform->ivlen;
8331 memcpy(transform->iv_enc, key2 + keylen, iv_copy_len);
8332 memcpy(transform->iv_dec, key2 + keylen + iv_copy_len,
8333 iv_copy_len);
8334 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008335#endif /* MBEDTLS_SSL_CLI_C */
8336#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008337 if (endpoint == MBEDTLS_SSL_IS_SERVER) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008338 key1 = keyblk + mac_key_len * 2 + keylen;
8339 key2 = keyblk + mac_key_len * 2;
8340
8341 mac_enc = keyblk + mac_key_len;
8342 mac_dec = keyblk;
8343
Gilles Peskine449bd832023-01-11 14:50:10 +01008344 iv_copy_len = (transform->fixed_ivlen) ?
8345 transform->fixed_ivlen : transform->ivlen;
8346 memcpy(transform->iv_dec, key1 + keylen, iv_copy_len);
8347 memcpy(transform->iv_enc, key1 + keylen + iv_copy_len,
8348 iv_copy_len);
8349 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008350#endif /* MBEDTLS_SSL_SRV_C */
8351 {
Gilles Peskine449bd832023-01-11 14:50:10 +01008352 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008353 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
8354 goto end;
8355 }
8356
Gilles Peskine449bd832023-01-11 14:50:10 +01008357 if (ssl != NULL && ssl->f_export_keys != NULL) {
8358 ssl->f_export_keys(ssl->p_export_keys,
8359 MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET,
8360 master, 48,
8361 randbytes + 32,
8362 randbytes,
8363 tls_prf_get_type(tls_prf));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008364 }
8365
8366#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008367 transform->psa_alg = alg;
8368
Gilles Peskine449bd832023-01-11 14:50:10 +01008369 if (alg != MBEDTLS_SSL_NULL_CIPHER) {
8370 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
8371 psa_set_key_algorithm(&attributes, alg);
8372 psa_set_key_type(&attributes, key_type);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008373
Gilles Peskine449bd832023-01-11 14:50:10 +01008374 if ((status = psa_import_key(&attributes,
8375 key1,
8376 PSA_BITS_TO_BYTES(key_bits),
8377 &transform->psa_key_enc)) != PSA_SUCCESS) {
8378 MBEDTLS_SSL_DEBUG_RET(3, "psa_import_key", (int) status);
8379 ret = psa_ssl_status_to_mbedtls(status);
8380 MBEDTLS_SSL_DEBUG_RET(1, "psa_import_key", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008381 goto end;
8382 }
8383
Gilles Peskine449bd832023-01-11 14:50:10 +01008384 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008385
Gilles Peskine449bd832023-01-11 14:50:10 +01008386 if ((status = psa_import_key(&attributes,
8387 key2,
8388 PSA_BITS_TO_BYTES(key_bits),
8389 &transform->psa_key_dec)) != PSA_SUCCESS) {
8390 ret = psa_ssl_status_to_mbedtls(status);
8391 MBEDTLS_SSL_DEBUG_RET(1, "psa_import_key", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008392 goto end;
8393 }
8394 }
8395#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008396 if ((ret = mbedtls_cipher_setup(&transform->cipher_ctx_enc,
8397 cipher_info)) != 0) {
8398 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008399 goto end;
8400 }
8401
Gilles Peskine449bd832023-01-11 14:50:10 +01008402 if ((ret = mbedtls_cipher_setup(&transform->cipher_ctx_dec,
8403 cipher_info)) != 0) {
8404 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008405 goto end;
8406 }
8407
Gilles Peskine449bd832023-01-11 14:50:10 +01008408 if ((ret = mbedtls_cipher_setkey(&transform->cipher_ctx_enc, key1,
8409 (int) mbedtls_cipher_info_get_key_bitlen(cipher_info),
8410 MBEDTLS_ENCRYPT)) != 0) {
8411 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setkey", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008412 goto end;
8413 }
8414
Gilles Peskine449bd832023-01-11 14:50:10 +01008415 if ((ret = mbedtls_cipher_setkey(&transform->cipher_ctx_dec, key2,
8416 (int) mbedtls_cipher_info_get_key_bitlen(cipher_info),
8417 MBEDTLS_DECRYPT)) != 0) {
8418 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setkey", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008419 goto end;
8420 }
8421
8422#if defined(MBEDTLS_CIPHER_MODE_CBC)
Gilles Peskine449bd832023-01-11 14:50:10 +01008423 if (mbedtls_cipher_info_get_mode(cipher_info) == MBEDTLS_MODE_CBC) {
8424 if ((ret = mbedtls_cipher_set_padding_mode(&transform->cipher_ctx_enc,
8425 MBEDTLS_PADDING_NONE)) != 0) {
8426 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_set_padding_mode", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008427 goto end;
8428 }
8429
Gilles Peskine449bd832023-01-11 14:50:10 +01008430 if ((ret = mbedtls_cipher_set_padding_mode(&transform->cipher_ctx_dec,
8431 MBEDTLS_PADDING_NONE)) != 0) {
8432 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_set_padding_mode", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008433 goto end;
8434 }
8435 }
8436#endif /* MBEDTLS_CIPHER_MODE_CBC */
8437#endif /* MBEDTLS_USE_PSA_CRYPTO */
8438
Neil Armstrong29c0c042022-03-17 17:47:28 +01008439#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
8440 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
8441 For AEAD-based ciphersuites, there is nothing to do here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008442 if (mac_key_len != 0) {
Neil Armstrong29c0c042022-03-17 17:47:28 +01008443#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008444 transform->psa_mac_alg = PSA_ALG_HMAC(mac_alg);
Neil Armstrong29c0c042022-03-17 17:47:28 +01008445
Gilles Peskine449bd832023-01-11 14:50:10 +01008446 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
8447 psa_set_key_algorithm(&attributes, PSA_ALG_HMAC(mac_alg));
8448 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
Neil Armstrong29c0c042022-03-17 17:47:28 +01008449
Gilles Peskine449bd832023-01-11 14:50:10 +01008450 if ((status = psa_import_key(&attributes,
8451 mac_enc, mac_key_len,
8452 &transform->psa_mac_enc)) != PSA_SUCCESS) {
8453 ret = psa_ssl_status_to_mbedtls(status);
8454 MBEDTLS_SSL_DEBUG_RET(1, "psa_import_mac_key", ret);
Neil Armstrong29c0c042022-03-17 17:47:28 +01008455 goto end;
8456 }
8457
Gilles Peskine449bd832023-01-11 14:50:10 +01008458 if ((transform->psa_alg == MBEDTLS_SSL_NULL_CIPHER) ||
8459 ((transform->psa_alg == PSA_ALG_CBC_NO_PADDING)
Andrzej Kurek8c95ac42022-08-17 16:17:00 -04008460#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01008461 && (transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED)
Andrzej Kurek8c95ac42022-08-17 16:17:00 -04008462#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01008463 )) {
Neil Armstrong29c0c042022-03-17 17:47:28 +01008464 /* mbedtls_ct_hmac() requires the key to be exportable */
Gilles Peskine449bd832023-01-11 14:50:10 +01008465 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT |
8466 PSA_KEY_USAGE_VERIFY_HASH);
8467 } else {
8468 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
8469 }
Neil Armstrong29c0c042022-03-17 17:47:28 +01008470
Gilles Peskine449bd832023-01-11 14:50:10 +01008471 if ((status = psa_import_key(&attributes,
8472 mac_dec, mac_key_len,
8473 &transform->psa_mac_dec)) != PSA_SUCCESS) {
8474 ret = psa_ssl_status_to_mbedtls(status);
8475 MBEDTLS_SSL_DEBUG_RET(1, "psa_import_mac_key", ret);
Neil Armstrong29c0c042022-03-17 17:47:28 +01008476 goto end;
8477 }
8478#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008479 ret = mbedtls_md_hmac_starts(&transform->md_ctx_enc, mac_enc, mac_key_len);
8480 if (ret != 0) {
Neil Armstrong29c0c042022-03-17 17:47:28 +01008481 goto end;
Gilles Peskine449bd832023-01-11 14:50:10 +01008482 }
8483 ret = mbedtls_md_hmac_starts(&transform->md_ctx_dec, mac_dec, mac_key_len);
8484 if (ret != 0) {
Neil Armstrong29c0c042022-03-17 17:47:28 +01008485 goto end;
Gilles Peskine449bd832023-01-11 14:50:10 +01008486 }
Neil Armstrong29c0c042022-03-17 17:47:28 +01008487#endif /* MBEDTLS_USE_PSA_CRYPTO */
8488 }
8489#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
8490
8491 ((void) mac_dec);
8492 ((void) mac_enc);
8493
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008494end:
Gilles Peskine449bd832023-01-11 14:50:10 +01008495 mbedtls_platform_zeroize(keyblk, sizeof(keyblk));
8496 return ret;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008497}
8498
Valerio Settia08b1a42022-11-17 15:10:02 +01008499#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) && \
8500 defined(MBEDTLS_USE_PSA_CRYPTO)
Valerio Setti6b3dab02022-11-17 17:14:54 +01008501int mbedtls_psa_ecjpake_read_round(
Gilles Peskine449bd832023-01-11 14:50:10 +01008502 psa_pake_operation_t *pake_ctx,
8503 const unsigned char *buf,
8504 size_t len, mbedtls_ecjpake_rounds_t round)
Valerio Settia08b1a42022-11-17 15:10:02 +01008505{
8506 psa_status_t status;
8507 size_t input_offset = 0;
Valerio Setti819de862022-11-17 18:05:19 +01008508 /*
Valerio Setti6b3dab02022-11-17 17:14:54 +01008509 * At round one repeat the KEY_SHARE, ZK_PUBLIC & ZF_PROOF twice
8510 * At round two perform a single cycle
8511 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008512 unsigned int remaining_steps = (round == MBEDTLS_ECJPAKE_ROUND_ONE) ? 2 : 1;
Valerio Settia08b1a42022-11-17 15:10:02 +01008513
Gilles Peskine449bd832023-01-11 14:50:10 +01008514 for (; remaining_steps > 0; remaining_steps--) {
8515 for (psa_pake_step_t step = PSA_PAKE_STEP_KEY_SHARE;
Valerio Settia08b1a42022-11-17 15:10:02 +01008516 step <= PSA_PAKE_STEP_ZK_PROOF;
Gilles Peskine449bd832023-01-11 14:50:10 +01008517 ++step) {
Valerio Settia08b1a42022-11-17 15:10:02 +01008518 /* Length is stored at the first byte */
8519 size_t length = buf[input_offset];
8520 input_offset += 1;
8521
Gilles Peskine449bd832023-01-11 14:50:10 +01008522 if (input_offset + length > len) {
Valerio Settia08b1a42022-11-17 15:10:02 +01008523 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
8524 }
8525
Gilles Peskine449bd832023-01-11 14:50:10 +01008526 status = psa_pake_input(pake_ctx, step,
8527 buf + input_offset, length);
8528 if (status != PSA_SUCCESS) {
8529 return psa_ssl_status_to_mbedtls(status);
Valerio Settia08b1a42022-11-17 15:10:02 +01008530 }
8531
8532 input_offset += length;
8533 }
8534 }
8535
Gilles Peskine449bd832023-01-11 14:50:10 +01008536 if (input_offset != len) {
Valerio Setti61ea17d2022-11-18 12:11:00 +01008537 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01008538 }
Valerio Setti30ebe112022-11-17 16:23:34 +01008539
Gilles Peskine449bd832023-01-11 14:50:10 +01008540 return 0;
Valerio Settia08b1a42022-11-17 15:10:02 +01008541}
8542
Valerio Setti6b3dab02022-11-17 17:14:54 +01008543int mbedtls_psa_ecjpake_write_round(
Gilles Peskine449bd832023-01-11 14:50:10 +01008544 psa_pake_operation_t *pake_ctx,
8545 unsigned char *buf,
8546 size_t len, size_t *olen,
8547 mbedtls_ecjpake_rounds_t round)
Valerio Settia08b1a42022-11-17 15:10:02 +01008548{
8549 psa_status_t status;
8550 size_t output_offset = 0;
8551 size_t output_len;
Valerio Setti819de862022-11-17 18:05:19 +01008552 /*
Valerio Setti6b3dab02022-11-17 17:14:54 +01008553 * At round one repeat the KEY_SHARE, ZK_PUBLIC & ZF_PROOF twice
8554 * At round two perform a single cycle
8555 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008556 unsigned int remaining_steps = (round == MBEDTLS_ECJPAKE_ROUND_ONE) ? 2 : 1;
Valerio Settia08b1a42022-11-17 15:10:02 +01008557
Gilles Peskine449bd832023-01-11 14:50:10 +01008558 for (; remaining_steps > 0; remaining_steps--) {
8559 for (psa_pake_step_t step = PSA_PAKE_STEP_KEY_SHARE;
8560 step <= PSA_PAKE_STEP_ZK_PROOF;
8561 ++step) {
Valerio Setti79f6b6b2022-11-21 14:17:03 +01008562 /*
Valerio Settid4a9b1a2022-11-22 11:11:10 +01008563 * For each step, prepend 1 byte with the length of the data as
8564 * given by psa_pake_output().
Valerio Setti79f6b6b2022-11-21 14:17:03 +01008565 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008566 status = psa_pake_output(pake_ctx, step,
8567 buf + output_offset + 1,
8568 len - output_offset - 1,
8569 &output_len);
8570 if (status != PSA_SUCCESS) {
8571 return psa_ssl_status_to_mbedtls(status);
Valerio Settia08b1a42022-11-17 15:10:02 +01008572 }
8573
Valerio Setti99d88c12022-11-22 16:03:43 +01008574 *(buf + output_offset) = (uint8_t) output_len;
Valerio Setti79f6b6b2022-11-21 14:17:03 +01008575
8576 output_offset += output_len + 1;
Valerio Settia08b1a42022-11-17 15:10:02 +01008577 }
8578 }
8579
8580 *olen = output_offset;
8581
Gilles Peskine449bd832023-01-11 14:50:10 +01008582 return 0;
Valerio Settia08b1a42022-11-17 15:10:02 +01008583}
Valerio Settia08b1a42022-11-17 15:10:02 +01008584#endif //MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED && MBEDTLS_USE_PSA_CRYPTO
8585
Jerry Yuee40f9d2022-02-17 14:55:16 +08008586#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008587int mbedtls_ssl_get_key_exchange_md_tls1_2(mbedtls_ssl_context *ssl,
8588 unsigned char *hash, size_t *hashlen,
8589 unsigned char *data, size_t data_len,
8590 mbedtls_md_type_t md_alg)
Jerry Yuee40f9d2022-02-17 14:55:16 +08008591{
8592 psa_status_t status;
8593 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01008594 psa_algorithm_t hash_alg = mbedtls_hash_info_psa_from_md(md_alg);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008595
Gilles Peskine449bd832023-01-11 14:50:10 +01008596 MBEDTLS_SSL_DEBUG_MSG(3, ("Perform PSA-based computation of digest of ServerKeyExchange"));
Jerry Yuee40f9d2022-02-17 14:55:16 +08008597
Gilles Peskine449bd832023-01-11 14:50:10 +01008598 if ((status = psa_hash_setup(&hash_operation,
8599 hash_alg)) != PSA_SUCCESS) {
8600 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_setup", status);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008601 goto exit;
8602 }
8603
Gilles Peskine449bd832023-01-11 14:50:10 +01008604 if ((status = psa_hash_update(&hash_operation, ssl->handshake->randbytes,
8605 64)) != PSA_SUCCESS) {
8606 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_update", status);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008607 goto exit;
8608 }
8609
Gilles Peskine449bd832023-01-11 14:50:10 +01008610 if ((status = psa_hash_update(&hash_operation,
8611 data, data_len)) != PSA_SUCCESS) {
8612 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_update", status);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008613 goto exit;
8614 }
8615
Gilles Peskine449bd832023-01-11 14:50:10 +01008616 if ((status = psa_hash_finish(&hash_operation, hash, PSA_HASH_MAX_SIZE,
8617 hashlen)) != PSA_SUCCESS) {
8618 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_finish", status);
8619 goto exit;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008620 }
8621
8622exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008623 if (status != PSA_SUCCESS) {
8624 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8625 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
8626 switch (status) {
Jerry Yuee40f9d2022-02-17 14:55:16 +08008627 case PSA_ERROR_NOT_SUPPORTED:
Gilles Peskine449bd832023-01-11 14:50:10 +01008628 return MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008629 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
8630 case PSA_ERROR_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +01008631 return MBEDTLS_ERR_MD_BAD_INPUT_DATA;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008632 case PSA_ERROR_INSUFFICIENT_MEMORY:
Gilles Peskine449bd832023-01-11 14:50:10 +01008633 return MBEDTLS_ERR_MD_ALLOC_FAILED;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008634 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01008635 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008636 }
8637 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008638 return 0;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008639}
8640
8641#else
8642
Gilles Peskine449bd832023-01-11 14:50:10 +01008643int mbedtls_ssl_get_key_exchange_md_tls1_2(mbedtls_ssl_context *ssl,
8644 unsigned char *hash, size_t *hashlen,
8645 unsigned char *data, size_t data_len,
8646 mbedtls_md_type_t md_alg)
Jerry Yuee40f9d2022-02-17 14:55:16 +08008647{
8648 int ret = 0;
8649 mbedtls_md_context_t ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +01008650 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type(md_alg);
8651 *hashlen = mbedtls_md_get_size(md_info);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008652
Gilles Peskine449bd832023-01-11 14:50:10 +01008653 MBEDTLS_SSL_DEBUG_MSG(3, ("Perform mbedtls-based computation of digest of ServerKeyExchange"));
Jerry Yuee40f9d2022-02-17 14:55:16 +08008654
Gilles Peskine449bd832023-01-11 14:50:10 +01008655 mbedtls_md_init(&ctx);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008656
8657 /*
8658 * digitally-signed struct {
8659 * opaque client_random[32];
8660 * opaque server_random[32];
8661 * ServerDHParams params;
8662 * };
8663 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008664 if ((ret = mbedtls_md_setup(&ctx, md_info, 0)) != 0) {
8665 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_setup", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008666 goto exit;
8667 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008668 if ((ret = mbedtls_md_starts(&ctx)) != 0) {
8669 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_starts", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008670 goto exit;
8671 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008672 if ((ret = mbedtls_md_update(&ctx, ssl->handshake->randbytes, 64)) != 0) {
8673 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_update", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008674 goto exit;
8675 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008676 if ((ret = mbedtls_md_update(&ctx, data, data_len)) != 0) {
8677 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_update", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008678 goto exit;
8679 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008680 if ((ret = mbedtls_md_finish(&ctx, hash)) != 0) {
8681 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_finish", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008682 goto exit;
8683 }
8684
8685exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008686 mbedtls_md_free(&ctx);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008687
Gilles Peskine449bd832023-01-11 14:50:10 +01008688 if (ret != 0) {
8689 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8690 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
8691 }
Jerry Yuee40f9d2022-02-17 14:55:16 +08008692
Gilles Peskine449bd832023-01-11 14:50:10 +01008693 return ret;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008694}
8695#endif /* MBEDTLS_USE_PSA_CRYPTO */
8696
Jerry Yud9d91da2022-02-17 14:57:06 +08008697#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
8698
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008699/* Find the preferred hash for a given signature algorithm. */
8700unsigned int mbedtls_ssl_tls12_get_preferred_hash_for_sig_alg(
Gilles Peskine449bd832023-01-11 14:50:10 +01008701 mbedtls_ssl_context *ssl,
8702 unsigned int sig_alg)
Jerry Yud9d91da2022-02-17 14:57:06 +08008703{
Gabor Mezei078e8032022-04-27 21:17:56 +02008704 unsigned int i;
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008705 uint16_t *received_sig_algs = ssl->handshake->received_sig_algs;
Gabor Mezei078e8032022-04-27 21:17:56 +02008706
Gilles Peskine449bd832023-01-11 14:50:10 +01008707 if (sig_alg == MBEDTLS_SSL_SIG_ANON) {
8708 return MBEDTLS_SSL_HASH_NONE;
8709 }
Gabor Mezei078e8032022-04-27 21:17:56 +02008710
Gilles Peskine449bd832023-01-11 14:50:10 +01008711 for (i = 0; received_sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++) {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008712 unsigned int hash_alg_received =
Gilles Peskine449bd832023-01-11 14:50:10 +01008713 MBEDTLS_SSL_TLS12_HASH_ALG_FROM_SIG_AND_HASH_ALG(
8714 received_sig_algs[i]);
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008715 unsigned int sig_alg_received =
Gilles Peskine449bd832023-01-11 14:50:10 +01008716 MBEDTLS_SSL_TLS12_SIG_ALG_FROM_SIG_AND_HASH_ALG(
8717 received_sig_algs[i]);
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008718
Gilles Peskine449bd832023-01-11 14:50:10 +01008719 if (sig_alg == sig_alg_received) {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008720#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008721 if (ssl->handshake->key_cert && ssl->handshake->key_cert->key) {
Neil Armstrong96eceb82022-06-30 18:05:05 +02008722 psa_algorithm_t psa_hash_alg =
Gilles Peskine449bd832023-01-11 14:50:10 +01008723 mbedtls_hash_info_psa_from_md(hash_alg_received);
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008724
Gilles Peskine449bd832023-01-11 14:50:10 +01008725 if (sig_alg_received == MBEDTLS_SSL_SIG_ECDSA &&
8726 !mbedtls_pk_can_do_ext(ssl->handshake->key_cert->key,
8727 PSA_ALG_ECDSA(psa_hash_alg),
8728 PSA_KEY_USAGE_SIGN_HASH)) {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008729 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01008730 }
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008731
Gilles Peskine449bd832023-01-11 14:50:10 +01008732 if (sig_alg_received == MBEDTLS_SSL_SIG_RSA &&
8733 !mbedtls_pk_can_do_ext(ssl->handshake->key_cert->key,
8734 PSA_ALG_RSA_PKCS1V15_SIGN(
8735 psa_hash_alg),
8736 PSA_KEY_USAGE_SIGN_HASH)) {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008737 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01008738 }
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008739 }
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008740#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong96eceb82022-06-30 18:05:05 +02008741
Gilles Peskine449bd832023-01-11 14:50:10 +01008742 return hash_alg_received;
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008743 }
Jerry Yud9d91da2022-02-17 14:57:06 +08008744 }
Jerry Yud9d91da2022-02-17 14:57:06 +08008745
Gilles Peskine449bd832023-01-11 14:50:10 +01008746 return MBEDTLS_SSL_HASH_NONE;
Jerry Yud9d91da2022-02-17 14:57:06 +08008747}
8748
8749#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
8750
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008751/* Serialization of TLS 1.2 sessions:
8752 *
8753 * struct {
8754 * uint64 start_time;
8755 * uint8 ciphersuite[2]; // defined by the standard
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008756 * uint8 session_id_len; // at most 32
8757 * opaque session_id[32];
8758 * opaque master[48]; // fixed length in the standard
8759 * uint32 verify_result;
8760 * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert
8761 * opaque ticket<0..2^24-1>; // length 0 means no ticket
8762 * uint32 ticket_lifetime;
8763 * uint8 mfl_code; // up to 255 according to standard
8764 * uint8 encrypt_then_mac; // 0 or 1
8765 * } serialized_session_tls12;
8766 *
8767 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008768static size_t ssl_tls12_session_save(const mbedtls_ssl_session *session,
8769 unsigned char *buf,
8770 size_t buf_len)
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008771{
8772 unsigned char *p = buf;
8773 size_t used = 0;
8774
8775#if defined(MBEDTLS_HAVE_TIME)
8776 uint64_t start;
8777#endif
8778#if defined(MBEDTLS_X509_CRT_PARSE_C)
8779#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8780 size_t cert_len;
8781#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8782#endif /* MBEDTLS_X509_CRT_PARSE_C */
8783
8784 /*
8785 * Time
8786 */
8787#if defined(MBEDTLS_HAVE_TIME)
8788 used += 8;
8789
Gilles Peskine449bd832023-01-11 14:50:10 +01008790 if (used <= buf_len) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008791 start = (uint64_t) session->start;
8792
Gilles Peskine449bd832023-01-11 14:50:10 +01008793 MBEDTLS_PUT_UINT64_BE(start, p, 0);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008794 p += 8;
8795 }
8796#endif /* MBEDTLS_HAVE_TIME */
8797
8798 /*
8799 * Basic mandatory fields
8800 */
8801 used += 2 /* ciphersuite */
Gilles Peskine449bd832023-01-11 14:50:10 +01008802 + 1 /* id_len */
8803 + sizeof(session->id)
8804 + sizeof(session->master)
8805 + 4; /* verify_result */
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008806
Gilles Peskine449bd832023-01-11 14:50:10 +01008807 if (used <= buf_len) {
8808 MBEDTLS_PUT_UINT16_BE(session->ciphersuite, p, 0);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008809 p += 2;
8810
Gilles Peskine449bd832023-01-11 14:50:10 +01008811 *p++ = MBEDTLS_BYTE_0(session->id_len);
8812 memcpy(p, session->id, 32);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008813 p += 32;
8814
Gilles Peskine449bd832023-01-11 14:50:10 +01008815 memcpy(p, session->master, 48);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008816 p += 48;
8817
Gilles Peskine449bd832023-01-11 14:50:10 +01008818 MBEDTLS_PUT_UINT32_BE(session->verify_result, p, 0);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008819 p += 4;
8820 }
8821
8822 /*
8823 * Peer's end-entity certificate
8824 */
8825#if defined(MBEDTLS_X509_CRT_PARSE_C)
8826#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01008827 if (session->peer_cert == NULL) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008828 cert_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01008829 } else {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008830 cert_len = session->peer_cert->raw.len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008831 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008832
8833 used += 3 + cert_len;
8834
Gilles Peskine449bd832023-01-11 14:50:10 +01008835 if (used <= buf_len) {
8836 *p++ = MBEDTLS_BYTE_2(cert_len);
8837 *p++ = MBEDTLS_BYTE_1(cert_len);
8838 *p++ = MBEDTLS_BYTE_0(cert_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008839
Gilles Peskine449bd832023-01-11 14:50:10 +01008840 if (session->peer_cert != NULL) {
8841 memcpy(p, session->peer_cert->raw.p, cert_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008842 p += cert_len;
8843 }
8844 }
8845#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008846 if (session->peer_cert_digest != NULL) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008847 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008848 if (used <= buf_len) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008849 *p++ = (unsigned char) session->peer_cert_digest_type;
8850 *p++ = (unsigned char) session->peer_cert_digest_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008851 memcpy(p, session->peer_cert_digest,
8852 session->peer_cert_digest_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008853 p += session->peer_cert_digest_len;
8854 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008855 } else {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008856 used += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01008857 if (used <= buf_len) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008858 *p++ = (unsigned char) MBEDTLS_MD_NONE;
8859 *p++ = 0;
8860 }
8861 }
8862#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8863#endif /* MBEDTLS_X509_CRT_PARSE_C */
8864
8865 /*
8866 * Session ticket if any, plus associated data
8867 */
8868#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8869 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
8870
Gilles Peskine449bd832023-01-11 14:50:10 +01008871 if (used <= buf_len) {
8872 *p++ = MBEDTLS_BYTE_2(session->ticket_len);
8873 *p++ = MBEDTLS_BYTE_1(session->ticket_len);
8874 *p++ = MBEDTLS_BYTE_0(session->ticket_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008875
Gilles Peskine449bd832023-01-11 14:50:10 +01008876 if (session->ticket != NULL) {
8877 memcpy(p, session->ticket, session->ticket_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008878 p += session->ticket_len;
8879 }
8880
Gilles Peskine449bd832023-01-11 14:50:10 +01008881 MBEDTLS_PUT_UINT32_BE(session->ticket_lifetime, p, 0);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008882 p += 4;
8883 }
8884#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8885
8886 /*
8887 * Misc extension-related info
8888 */
8889#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8890 used += 1;
8891
Gilles Peskine449bd832023-01-11 14:50:10 +01008892 if (used <= buf_len) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008893 *p++ = session->mfl_code;
Gilles Peskine449bd832023-01-11 14:50:10 +01008894 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008895#endif
8896
8897#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
8898 used += 1;
8899
Gilles Peskine449bd832023-01-11 14:50:10 +01008900 if (used <= buf_len) {
8901 *p++ = MBEDTLS_BYTE_0(session->encrypt_then_mac);
8902 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008903#endif
8904
Gilles Peskine449bd832023-01-11 14:50:10 +01008905 return used;
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008906}
8907
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02008908MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01008909static int ssl_tls12_session_load(mbedtls_ssl_session *session,
8910 const unsigned char *buf,
8911 size_t len)
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008912{
8913#if defined(MBEDTLS_HAVE_TIME)
8914 uint64_t start;
8915#endif
8916#if defined(MBEDTLS_X509_CRT_PARSE_C)
8917#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8918 size_t cert_len;
8919#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8920#endif /* MBEDTLS_X509_CRT_PARSE_C */
8921
8922 const unsigned char *p = buf;
8923 const unsigned char * const end = buf + len;
8924
8925 /*
8926 * Time
8927 */
8928#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01008929 if (8 > (size_t) (end - p)) {
8930 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
8931 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008932
Gilles Peskine449bd832023-01-11 14:50:10 +01008933 start = ((uint64_t) p[0] << 56) |
8934 ((uint64_t) p[1] << 48) |
8935 ((uint64_t) p[2] << 40) |
8936 ((uint64_t) p[3] << 32) |
8937 ((uint64_t) p[4] << 24) |
8938 ((uint64_t) p[5] << 16) |
8939 ((uint64_t) p[6] << 8) |
8940 ((uint64_t) p[7]);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008941 p += 8;
8942
8943 session->start = (time_t) start;
8944#endif /* MBEDTLS_HAVE_TIME */
8945
8946 /*
8947 * Basic mandatory fields
8948 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008949 if (2 + 1 + 32 + 48 + 4 > (size_t) (end - p)) {
8950 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
8951 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008952
Gilles Peskine449bd832023-01-11 14:50:10 +01008953 session->ciphersuite = (p[0] << 8) | p[1];
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008954 p += 2;
8955
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008956 session->id_len = *p++;
Gilles Peskine449bd832023-01-11 14:50:10 +01008957 memcpy(session->id, p, 32);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008958 p += 32;
8959
Gilles Peskine449bd832023-01-11 14:50:10 +01008960 memcpy(session->master, p, 48);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008961 p += 48;
8962
Gilles Peskine449bd832023-01-11 14:50:10 +01008963 session->verify_result = ((uint32_t) p[0] << 24) |
8964 ((uint32_t) p[1] << 16) |
8965 ((uint32_t) p[2] << 8) |
8966 ((uint32_t) p[3]);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008967 p += 4;
8968
8969 /* Immediately clear invalid pointer values that have been read, in case
8970 * we exit early before we replaced them with valid ones. */
8971#if defined(MBEDTLS_X509_CRT_PARSE_C)
8972#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8973 session->peer_cert = NULL;
8974#else
8975 session->peer_cert_digest = NULL;
8976#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8977#endif /* MBEDTLS_X509_CRT_PARSE_C */
8978#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8979 session->ticket = NULL;
8980#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8981
8982 /*
8983 * Peer certificate
8984 */
8985#if defined(MBEDTLS_X509_CRT_PARSE_C)
8986#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8987 /* Deserialize CRT from the end of the ticket. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008988 if (3 > (size_t) (end - p)) {
8989 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
8990 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008991
Gilles Peskine449bd832023-01-11 14:50:10 +01008992 cert_len = (p[0] << 16) | (p[1] << 8) | p[2];
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008993 p += 3;
8994
Gilles Peskine449bd832023-01-11 14:50:10 +01008995 if (cert_len != 0) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008996 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
8997
Gilles Peskine449bd832023-01-11 14:50:10 +01008998 if (cert_len > (size_t) (end - p)) {
8999 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9000 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009001
Gilles Peskine449bd832023-01-11 14:50:10 +01009002 session->peer_cert = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009003
Gilles Peskine449bd832023-01-11 14:50:10 +01009004 if (session->peer_cert == NULL) {
9005 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
9006 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009007
Gilles Peskine449bd832023-01-11 14:50:10 +01009008 mbedtls_x509_crt_init(session->peer_cert);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009009
Gilles Peskine449bd832023-01-11 14:50:10 +01009010 if ((ret = mbedtls_x509_crt_parse_der(session->peer_cert,
9011 p, cert_len)) != 0) {
9012 mbedtls_x509_crt_free(session->peer_cert);
9013 mbedtls_free(session->peer_cert);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009014 session->peer_cert = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01009015 return ret;
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009016 }
9017
9018 p += cert_len;
9019 }
9020#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9021 /* Deserialize CRT digest from the end of the ticket. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009022 if (2 > (size_t) (end - p)) {
9023 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9024 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009025
9026 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
9027 session->peer_cert_digest_len = (size_t) *p++;
9028
Gilles Peskine449bd832023-01-11 14:50:10 +01009029 if (session->peer_cert_digest_len != 0) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009030 const mbedtls_md_info_t *md_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01009031 mbedtls_md_info_from_type(session->peer_cert_digest_type);
9032 if (md_info == NULL) {
9033 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9034 }
9035 if (session->peer_cert_digest_len != mbedtls_md_get_size(md_info)) {
9036 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9037 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009038
Gilles Peskine449bd832023-01-11 14:50:10 +01009039 if (session->peer_cert_digest_len > (size_t) (end - p)) {
9040 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9041 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009042
9043 session->peer_cert_digest =
Gilles Peskine449bd832023-01-11 14:50:10 +01009044 mbedtls_calloc(1, session->peer_cert_digest_len);
9045 if (session->peer_cert_digest == NULL) {
9046 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
9047 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009048
Gilles Peskine449bd832023-01-11 14:50:10 +01009049 memcpy(session->peer_cert_digest, p,
9050 session->peer_cert_digest_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009051 p += session->peer_cert_digest_len;
9052 }
9053#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9054#endif /* MBEDTLS_X509_CRT_PARSE_C */
9055
9056 /*
9057 * Session ticket and associated data
9058 */
9059#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01009060 if (3 > (size_t) (end - p)) {
9061 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9062 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009063
Gilles Peskine449bd832023-01-11 14:50:10 +01009064 session->ticket_len = (p[0] << 16) | (p[1] << 8) | p[2];
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009065 p += 3;
9066
Gilles Peskine449bd832023-01-11 14:50:10 +01009067 if (session->ticket_len != 0) {
9068 if (session->ticket_len > (size_t) (end - p)) {
9069 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9070 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009071
Gilles Peskine449bd832023-01-11 14:50:10 +01009072 session->ticket = mbedtls_calloc(1, session->ticket_len);
9073 if (session->ticket == NULL) {
9074 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
9075 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009076
Gilles Peskine449bd832023-01-11 14:50:10 +01009077 memcpy(session->ticket, p, session->ticket_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009078 p += session->ticket_len;
9079 }
9080
Gilles Peskine449bd832023-01-11 14:50:10 +01009081 if (4 > (size_t) (end - p)) {
9082 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9083 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009084
Gilles Peskine449bd832023-01-11 14:50:10 +01009085 session->ticket_lifetime = ((uint32_t) p[0] << 24) |
9086 ((uint32_t) p[1] << 16) |
9087 ((uint32_t) p[2] << 8) |
9088 ((uint32_t) p[3]);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009089 p += 4;
9090#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
9091
9092 /*
9093 * Misc extension-related info
9094 */
9095#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01009096 if (1 > (size_t) (end - p)) {
9097 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9098 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009099
9100 session->mfl_code = *p++;
9101#endif
9102
9103#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01009104 if (1 > (size_t) (end - p)) {
9105 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9106 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009107
9108 session->encrypt_then_mac = *p++;
9109#endif
9110
9111 /* Done, should have consumed entire buffer */
Gilles Peskine449bd832023-01-11 14:50:10 +01009112 if (p != end) {
9113 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9114 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009115
Gilles Peskine449bd832023-01-11 14:50:10 +01009116 return 0;
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009117}
Jerry Yudc7bd172022-02-17 13:44:15 +08009118#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9119
XiaokangQian75d40ef2022-04-20 11:05:24 +00009120int mbedtls_ssl_validate_ciphersuite(
9121 const mbedtls_ssl_context *ssl,
9122 const mbedtls_ssl_ciphersuite_t *suite_info,
9123 mbedtls_ssl_protocol_version min_tls_version,
Gilles Peskine449bd832023-01-11 14:50:10 +01009124 mbedtls_ssl_protocol_version max_tls_version)
XiaokangQian75d40ef2022-04-20 11:05:24 +00009125{
9126 (void) ssl;
9127
Gilles Peskine449bd832023-01-11 14:50:10 +01009128 if (suite_info == NULL) {
9129 return -1;
9130 }
XiaokangQian75d40ef2022-04-20 11:05:24 +00009131
Gilles Peskine449bd832023-01-11 14:50:10 +01009132 if ((suite_info->min_tls_version > max_tls_version) ||
9133 (suite_info->max_tls_version < min_tls_version)) {
9134 return -1;
XiaokangQian75d40ef2022-04-20 11:05:24 +00009135 }
9136
XiaokangQian060d8672022-04-21 09:24:56 +00009137#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_CLI_C)
XiaokangQian75d40ef2022-04-20 11:05:24 +00009138#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Neil Armstrongca7d5062022-05-31 14:43:23 +02009139#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01009140 if (suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
9141 ssl->handshake->psa_pake_ctx_is_ok != 1)
Neil Armstrongca7d5062022-05-31 14:43:23 +02009142#else
Gilles Peskine449bd832023-01-11 14:50:10 +01009143 if (suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
9144 mbedtls_ecjpake_check(&ssl->handshake->ecjpake_ctx) != 0)
Neil Armstrongca7d5062022-05-31 14:43:23 +02009145#endif /* MBEDTLS_USE_PSA_CRYPTO */
XiaokangQian75d40ef2022-04-20 11:05:24 +00009146 {
Gilles Peskine449bd832023-01-11 14:50:10 +01009147 return -1;
XiaokangQian75d40ef2022-04-20 11:05:24 +00009148 }
9149#endif
9150
9151 /* Don't suggest PSK-based ciphersuite if no PSK is available. */
9152#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01009153 if (mbedtls_ssl_ciphersuite_uses_psk(suite_info) &&
9154 mbedtls_ssl_conf_has_static_psk(ssl->conf) == 0) {
9155 return -1;
XiaokangQian75d40ef2022-04-20 11:05:24 +00009156 }
9157#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
9158#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9159
Gilles Peskine449bd832023-01-11 14:50:10 +01009160 return 0;
XiaokangQian75d40ef2022-04-20 11:05:24 +00009161}
9162
Ronald Crone68ab4f2022-10-05 12:46:29 +02009163#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
XiaokangQianeaf36512022-04-24 09:07:44 +00009164/*
9165 * Function for writing a signature algorithm extension.
9166 *
9167 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
9168 * value (TLS 1.3 RFC8446):
9169 * enum {
9170 * ....
9171 * ecdsa_secp256r1_sha256( 0x0403 ),
9172 * ecdsa_secp384r1_sha384( 0x0503 ),
9173 * ecdsa_secp521r1_sha512( 0x0603 ),
9174 * ....
9175 * } SignatureScheme;
9176 *
9177 * struct {
9178 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
9179 * } SignatureSchemeList;
9180 *
9181 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
9182 * value (TLS 1.2 RFC5246):
9183 * enum {
9184 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
9185 * sha512(6), (255)
9186 * } HashAlgorithm;
9187 *
9188 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
9189 * SignatureAlgorithm;
9190 *
9191 * struct {
9192 * HashAlgorithm hash;
9193 * SignatureAlgorithm signature;
9194 * } SignatureAndHashAlgorithm;
9195 *
9196 * SignatureAndHashAlgorithm
9197 * supported_signature_algorithms<2..2^16-2>;
9198 *
9199 * The TLS 1.3 signature algorithm extension was defined to be a compatible
9200 * generalization of the TLS 1.2 signature algorithm extension.
9201 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
9202 * `SignatureScheme` field of TLS 1.3
9203 *
9204 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009205int mbedtls_ssl_write_sig_alg_ext(mbedtls_ssl_context *ssl, unsigned char *buf,
9206 const unsigned char *end, size_t *out_len)
XiaokangQianeaf36512022-04-24 09:07:44 +00009207{
9208 unsigned char *p = buf;
9209 unsigned char *supported_sig_alg; /* Start of supported_signature_algorithms */
9210 size_t supported_sig_alg_len = 0; /* Length of supported_signature_algorithms */
9211
9212 *out_len = 0;
9213
Gilles Peskine449bd832023-01-11 14:50:10 +01009214 MBEDTLS_SSL_DEBUG_MSG(3, ("adding signature_algorithms extension"));
XiaokangQianeaf36512022-04-24 09:07:44 +00009215
9216 /* Check if we have space for header and length field:
9217 * - extension_type (2 bytes)
9218 * - extension_data_length (2 bytes)
9219 * - supported_signature_algorithms_length (2 bytes)
9220 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009221 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 6);
XiaokangQianeaf36512022-04-24 09:07:44 +00009222 p += 6;
9223
9224 /*
9225 * Write supported_signature_algorithms
9226 */
9227 supported_sig_alg = p;
Gilles Peskine449bd832023-01-11 14:50:10 +01009228 const uint16_t *sig_alg = mbedtls_ssl_get_sig_algs(ssl);
9229 if (sig_alg == NULL) {
9230 return MBEDTLS_ERR_SSL_BAD_CONFIG;
9231 }
XiaokangQianeaf36512022-04-24 09:07:44 +00009232
Gilles Peskine449bd832023-01-11 14:50:10 +01009233 for (; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++) {
9234 MBEDTLS_SSL_DEBUG_MSG(3, ("got signature scheme [%x] %s",
9235 *sig_alg,
9236 mbedtls_ssl_sig_alg_to_str(*sig_alg)));
9237 if (!mbedtls_ssl_sig_alg_is_supported(ssl, *sig_alg)) {
XiaokangQianeaf36512022-04-24 09:07:44 +00009238 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01009239 }
9240 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
9241 MBEDTLS_PUT_UINT16_BE(*sig_alg, p, 0);
XiaokangQianeaf36512022-04-24 09:07:44 +00009242 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01009243 MBEDTLS_SSL_DEBUG_MSG(3, ("sent signature scheme [%x] %s",
9244 *sig_alg,
9245 mbedtls_ssl_sig_alg_to_str(*sig_alg)));
XiaokangQianeaf36512022-04-24 09:07:44 +00009246 }
9247
9248 /* Length of supported_signature_algorithms */
9249 supported_sig_alg_len = p - supported_sig_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01009250 if (supported_sig_alg_len == 0) {
9251 MBEDTLS_SSL_DEBUG_MSG(1, ("No signature algorithms defined."));
9252 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
XiaokangQianeaf36512022-04-24 09:07:44 +00009253 }
9254
Gilles Peskine449bd832023-01-11 14:50:10 +01009255 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SIG_ALG, buf, 0);
9256 MBEDTLS_PUT_UINT16_BE(supported_sig_alg_len + 2, buf, 2);
9257 MBEDTLS_PUT_UINT16_BE(supported_sig_alg_len, buf, 4);
XiaokangQianeaf36512022-04-24 09:07:44 +00009258
XiaokangQianeaf36512022-04-24 09:07:44 +00009259 *out_len = p - buf;
9260
9261#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01009262 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_SIG_ALG);
XiaokangQianeaf36512022-04-24 09:07:44 +00009263#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu0c354a22022-08-29 15:25:36 +08009264
Gilles Peskine449bd832023-01-11 14:50:10 +01009265 return 0;
XiaokangQianeaf36512022-04-24 09:07:44 +00009266}
Ronald Crone68ab4f2022-10-05 12:46:29 +02009267#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
XiaokangQianeaf36512022-04-24 09:07:44 +00009268
XiaokangQian40a35232022-05-07 09:02:40 +00009269#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
XiaokangQian9b2b7712022-05-17 02:57:00 +00009270/*
9271 * mbedtls_ssl_parse_server_name_ext
9272 *
9273 * Structure of server_name extension:
9274 *
9275 * enum {
9276 * host_name(0), (255)
9277 * } NameType;
9278 * opaque HostName<1..2^16-1>;
9279 *
9280 * struct {
9281 * NameType name_type;
9282 * select (name_type) {
9283 * case host_name: HostName;
9284 * } name;
9285 * } ServerName;
9286 * struct {
9287 * ServerName server_name_list<1..2^16-1>
9288 * } ServerNameList;
9289 */
Ronald Cronce7d76e2022-07-08 18:56:49 +02009290MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01009291int mbedtls_ssl_parse_server_name_ext(mbedtls_ssl_context *ssl,
9292 const unsigned char *buf,
9293 const unsigned char *end)
XiaokangQian40a35232022-05-07 09:02:40 +00009294{
9295 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
9296 const unsigned char *p = buf;
XiaokangQian9b2b7712022-05-17 02:57:00 +00009297 size_t server_name_list_len, hostname_len;
9298 const unsigned char *server_name_list_end;
XiaokangQian40a35232022-05-07 09:02:40 +00009299
Gilles Peskine449bd832023-01-11 14:50:10 +01009300 MBEDTLS_SSL_DEBUG_MSG(3, ("parse ServerName extension"));
XiaokangQian40a35232022-05-07 09:02:40 +00009301
Gilles Peskine449bd832023-01-11 14:50:10 +01009302 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
9303 server_name_list_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian40a35232022-05-07 09:02:40 +00009304 p += 2;
9305
Gilles Peskine449bd832023-01-11 14:50:10 +01009306 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, server_name_list_len);
XiaokangQian9b2b7712022-05-17 02:57:00 +00009307 server_name_list_end = p + server_name_list_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01009308 while (p < server_name_list_end) {
9309 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, server_name_list_end, 3);
9310 hostname_len = MBEDTLS_GET_UINT16_BE(p, 1);
9311 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, server_name_list_end,
9312 hostname_len + 3);
XiaokangQian40a35232022-05-07 09:02:40 +00009313
Gilles Peskine449bd832023-01-11 14:50:10 +01009314 if (p[0] == MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME) {
XiaokangQian75fe8c72022-06-15 09:42:45 +00009315 /* sni_name is intended to be used only during the parsing of the
9316 * ClientHello message (it is reset to NULL before the end of
9317 * the message parsing). Thus it is ok to just point to the
9318 * reception buffer and not make a copy of it.
9319 */
XiaokangQianf2a94202022-05-20 06:44:24 +00009320 ssl->handshake->sni_name = p + 3;
9321 ssl->handshake->sni_name_len = hostname_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01009322 if (ssl->conf->f_sni == NULL) {
9323 return 0;
XiaokangQian40a35232022-05-07 09:02:40 +00009324 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009325 ret = ssl->conf->f_sni(ssl->conf->p_sni,
9326 ssl, p + 3, hostname_len);
9327 if (ret != 0) {
9328 MBEDTLS_SSL_DEBUG_RET(1, "ssl_sni_wrapper", ret);
9329 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_UNRECOGNIZED_NAME,
9330 MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME);
9331 return MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME;
9332 }
9333 return 0;
XiaokangQian40a35232022-05-07 09:02:40 +00009334 }
9335
9336 p += hostname_len + 3;
9337 }
9338
Gilles Peskine449bd832023-01-11 14:50:10 +01009339 return 0;
XiaokangQian40a35232022-05-07 09:02:40 +00009340}
9341#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
9342
XiaokangQianacb39922022-06-17 10:18:48 +00009343#if defined(MBEDTLS_SSL_ALPN)
Ronald Cronce7d76e2022-07-08 18:56:49 +02009344MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01009345int mbedtls_ssl_parse_alpn_ext(mbedtls_ssl_context *ssl,
9346 const unsigned char *buf,
9347 const unsigned char *end)
XiaokangQianacb39922022-06-17 10:18:48 +00009348{
9349 const unsigned char *p = buf;
XiaokangQianc7403452022-06-23 03:24:12 +00009350 size_t protocol_name_list_len;
XiaokangQian95d5f542022-06-24 02:29:26 +00009351 const unsigned char *protocol_name_list;
9352 const unsigned char *protocol_name_list_end;
XiaokangQianc7403452022-06-23 03:24:12 +00009353 size_t protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009354
9355 /* If ALPN not configured, just ignore the extension */
Gilles Peskine449bd832023-01-11 14:50:10 +01009356 if (ssl->conf->alpn_list == NULL) {
9357 return 0;
9358 }
XiaokangQianacb39922022-06-17 10:18:48 +00009359
9360 /*
XiaokangQianc7403452022-06-23 03:24:12 +00009361 * RFC7301, section 3.1
9362 * opaque ProtocolName<1..2^8-1>;
XiaokangQianacb39922022-06-17 10:18:48 +00009363 *
XiaokangQianc7403452022-06-23 03:24:12 +00009364 * struct {
9365 * ProtocolName protocol_name_list<2..2^16-1>
9366 * } ProtocolNameList;
XiaokangQianacb39922022-06-17 10:18:48 +00009367 */
9368
XiaokangQianc7403452022-06-23 03:24:12 +00009369 /*
XiaokangQian0b776e22022-06-24 09:04:59 +00009370 * protocol_name_list_len 2 bytes
9371 * protocol_name_len 1 bytes
9372 * protocol_name >=1 byte
XiaokangQianc7403452022-06-23 03:24:12 +00009373 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009374 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 4);
XiaokangQianacb39922022-06-17 10:18:48 +00009375
Gilles Peskine449bd832023-01-11 14:50:10 +01009376 protocol_name_list_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQianacb39922022-06-17 10:18:48 +00009377 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01009378 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, protocol_name_list_len);
XiaokangQian95d5f542022-06-24 02:29:26 +00009379 protocol_name_list = p;
9380 protocol_name_list_end = p + protocol_name_list_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009381
9382 /* Validate peer's list (lengths) */
Gilles Peskine449bd832023-01-11 14:50:10 +01009383 while (p < protocol_name_list_end) {
XiaokangQian95d5f542022-06-24 02:29:26 +00009384 protocol_name_len = *p++;
Gilles Peskine449bd832023-01-11 14:50:10 +01009385 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, protocol_name_list_end,
9386 protocol_name_len);
9387 if (protocol_name_len == 0) {
XiaokangQian95d5f542022-06-24 02:29:26 +00009388 MBEDTLS_SSL_PEND_FATAL_ALERT(
9389 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
Gilles Peskine449bd832023-01-11 14:50:10 +01009390 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
9391 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
XiaokangQian95d5f542022-06-24 02:29:26 +00009392 }
9393
9394 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009395 }
9396
9397 /* Use our order of preference */
Gilles Peskine449bd832023-01-11 14:50:10 +01009398 for (const char **alpn = ssl->conf->alpn_list; *alpn != NULL; alpn++) {
9399 size_t const alpn_len = strlen(*alpn);
XiaokangQian95d5f542022-06-24 02:29:26 +00009400 p = protocol_name_list;
Gilles Peskine449bd832023-01-11 14:50:10 +01009401 while (p < protocol_name_list_end) {
XiaokangQian95d5f542022-06-24 02:29:26 +00009402 protocol_name_len = *p++;
Gilles Peskine449bd832023-01-11 14:50:10 +01009403 if (protocol_name_len == alpn_len &&
9404 memcmp(p, *alpn, alpn_len) == 0) {
XiaokangQianacb39922022-06-17 10:18:48 +00009405 ssl->alpn_chosen = *alpn;
Gilles Peskine449bd832023-01-11 14:50:10 +01009406 return 0;
XiaokangQianacb39922022-06-17 10:18:48 +00009407 }
XiaokangQian95d5f542022-06-24 02:29:26 +00009408
9409 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009410 }
9411 }
9412
XiaokangQian95d5f542022-06-24 02:29:26 +00009413 /* If we get here, no match was found */
XiaokangQianacb39922022-06-17 10:18:48 +00009414 MBEDTLS_SSL_PEND_FATAL_ALERT(
Gilles Peskine449bd832023-01-11 14:50:10 +01009415 MBEDTLS_SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL,
9416 MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL);
9417 return MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL;
XiaokangQianacb39922022-06-17 10:18:48 +00009418}
9419
Gilles Peskine449bd832023-01-11 14:50:10 +01009420int mbedtls_ssl_write_alpn_ext(mbedtls_ssl_context *ssl,
9421 unsigned char *buf,
9422 unsigned char *end,
9423 size_t *out_len)
XiaokangQianacb39922022-06-17 10:18:48 +00009424{
9425 unsigned char *p = buf;
XiaokangQian95d5f542022-06-24 02:29:26 +00009426 size_t protocol_name_len;
XiaokangQianc7403452022-06-23 03:24:12 +00009427 *out_len = 0;
XiaokangQianacb39922022-06-17 10:18:48 +00009428
Gilles Peskine449bd832023-01-11 14:50:10 +01009429 if (ssl->alpn_chosen == NULL) {
9430 return 0;
XiaokangQianacb39922022-06-17 10:18:48 +00009431 }
9432
Gilles Peskine449bd832023-01-11 14:50:10 +01009433 protocol_name_len = strlen(ssl->alpn_chosen);
9434 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 7 + protocol_name_len);
XiaokangQianacb39922022-06-17 10:18:48 +00009435
Gilles Peskine449bd832023-01-11 14:50:10 +01009436 MBEDTLS_SSL_DEBUG_MSG(3, ("server side, adding alpn extension"));
XiaokangQianacb39922022-06-17 10:18:48 +00009437 /*
9438 * 0 . 1 ext identifier
9439 * 2 . 3 ext length
9440 * 4 . 5 protocol list length
9441 * 6 . 6 protocol name length
9442 * 7 . 7+n protocol name
9443 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009444 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_ALPN, p, 0);
XiaokangQianacb39922022-06-17 10:18:48 +00009445
XiaokangQian95d5f542022-06-24 02:29:26 +00009446 *out_len = 7 + protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009447
Gilles Peskine449bd832023-01-11 14:50:10 +01009448 MBEDTLS_PUT_UINT16_BE(protocol_name_len + 3, p, 2);
9449 MBEDTLS_PUT_UINT16_BE(protocol_name_len + 1, p, 4);
XiaokangQian0b776e22022-06-24 09:04:59 +00009450 /* Note: the length of the chosen protocol has been checked to be less
9451 * than 255 bytes in `mbedtls_ssl_conf_alpn_protocols`.
9452 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009453 p[6] = MBEDTLS_BYTE_0(protocol_name_len);
XiaokangQianacb39922022-06-17 10:18:48 +00009454
Gilles Peskine449bd832023-01-11 14:50:10 +01009455 memcpy(p + 7, ssl->alpn_chosen, protocol_name_len);
Jerry Yub95dd362022-11-08 21:19:34 +08009456
9457#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01009458 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_ALPN);
Jerry Yub95dd362022-11-08 21:19:34 +08009459#endif
9460
Gilles Peskine449bd832023-01-11 14:50:10 +01009461 return 0;
XiaokangQianacb39922022-06-17 10:18:48 +00009462}
9463#endif /* MBEDTLS_SSL_ALPN */
9464
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009465#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
Xiaokang Qian03409292022-10-12 02:49:52 +00009466 defined(MBEDTLS_SSL_SESSION_TICKETS) && \
Xiaokang Qianed0620c2022-10-12 06:58:13 +00009467 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \
Xiaokang Qianed3afcd2022-10-12 08:31:11 +00009468 defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01009469int mbedtls_ssl_session_set_hostname(mbedtls_ssl_session *session,
9470 const char *hostname)
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009471{
9472 /* Initialize to suppress unnecessary compiler warning */
9473 size_t hostname_len = 0;
9474
9475 /* Check if new hostname is valid before
9476 * making any change to current one */
Gilles Peskine449bd832023-01-11 14:50:10 +01009477 if (hostname != NULL) {
9478 hostname_len = strlen(hostname);
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009479
Gilles Peskine449bd832023-01-11 14:50:10 +01009480 if (hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN) {
9481 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9482 }
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009483 }
9484
9485 /* Now it's clear that we will overwrite the old hostname,
9486 * so we can free it safely */
Gilles Peskine449bd832023-01-11 14:50:10 +01009487 if (session->hostname != NULL) {
9488 mbedtls_platform_zeroize(session->hostname,
9489 strlen(session->hostname));
9490 mbedtls_free(session->hostname);
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009491 }
9492
9493 /* Passing NULL as hostname shall clear the old one */
Gilles Peskine449bd832023-01-11 14:50:10 +01009494 if (hostname == NULL) {
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009495 session->hostname = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01009496 } else {
9497 session->hostname = mbedtls_calloc(1, hostname_len + 1);
9498 if (session->hostname == NULL) {
9499 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
9500 }
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009501
Gilles Peskine449bd832023-01-11 14:50:10 +01009502 memcpy(session->hostname, hostname, hostname_len);
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009503 }
9504
Gilles Peskine449bd832023-01-11 14:50:10 +01009505 return 0;
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009506}
Xiaokang Qian03409292022-10-12 02:49:52 +00009507#endif /* MBEDTLS_SSL_PROTO_TLS1_3 &&
9508 MBEDTLS_SSL_SESSION_TICKETS &&
Xiaokang Qianed0620c2022-10-12 06:58:13 +00009509 MBEDTLS_SSL_SERVER_NAME_INDICATION &&
Xiaokang Qianed3afcd2022-10-12 08:31:11 +00009510 MBEDTLS_SSL_CLI_C */
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009511
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009512#endif /* MBEDTLS_SSL_TLS_C */