blob: 55c6c3d3c71fc3e15561a609872743d6ffa5f4fc [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01002 * TLS shared functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakker5121ce52009-01-03 21:22:43 +000018 */
19/*
Paul Bakker5121ce52009-01-03 21:22:43 +000020 * http://www.ietf.org/rfc/rfc2246.txt
21 * http://www.ietf.org/rfc/rfc4346.txt
22 */
23
Gilles Peskinedb09ef62020-06-03 01:43:33 +020024#include "common.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#if defined(MBEDTLS_SSL_TLS_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000027
Jerry Yub476a442022-01-21 18:14:45 +080028#include <assert.h>
29
SimonBd5800b72016-04-26 07:43:27 +010030#include "mbedtls/platform.h"
SimonBd5800b72016-04-26 07:43:27 +010031
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000032#include "mbedtls/ssl.h"
Ronald Cron9f0fba32022-02-10 16:45:15 +010033#include "ssl_client.h"
Ronald Cron27c85e72022-03-08 11:37:55 +010034#include "ssl_debug_helpers.h"
Chris Jones84a773f2021-03-05 18:38:47 +000035#include "ssl_misc.h"
Andrzej Kurek25f27152022-08-17 16:09:31 -040036
Janos Follath73c616b2019-12-18 15:07:04 +000037#include "mbedtls/debug.h"
38#include "mbedtls/error.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050039#include "mbedtls/platform_util.h"
Hanno Beckera835da52019-05-16 12:39:07 +010040#include "mbedtls/version.h"
Gabor Mezei765862c2021-10-19 12:22:25 +020041#include "mbedtls/constant_time.h"
Paul Bakker0be444a2013-08-27 21:55:01 +020042
Rich Evans00ab4702015-02-06 13:43:58 +000043#include <string.h>
44
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050045#if defined(MBEDTLS_USE_PSA_CRYPTO)
46#include "mbedtls/psa_util.h"
47#include "psa/crypto.h"
48#endif
Manuel Pégourié-Gonnard07018f92022-09-15 11:29:35 +020049#include "mbedtls/legacy_or_psa.h"
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050050
Janos Follath23bdca02016-10-07 14:47:14 +010051#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000052#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020053#endif
54
Ronald Cronad8c17b2022-06-10 17:18:09 +020055#if defined(MBEDTLS_TEST_HOOKS)
56static mbedtls_ssl_chk_buf_ptr_args chk_buf_ptr_fail_args;
57
58void mbedtls_ssl_set_chk_buf_ptr_fail_args(
Gilles Peskine449bd832023-01-11 14:50:10 +010059 const uint8_t *cur, const uint8_t *end, size_t need)
Ronald Cronad8c17b2022-06-10 17:18:09 +020060{
61 chk_buf_ptr_fail_args.cur = cur;
62 chk_buf_ptr_fail_args.end = end;
63 chk_buf_ptr_fail_args.need = need;
64}
65
Gilles Peskine449bd832023-01-11 14:50:10 +010066void mbedtls_ssl_reset_chk_buf_ptr_fail_args(void)
Ronald Cronad8c17b2022-06-10 17:18:09 +020067{
Gilles Peskine449bd832023-01-11 14:50:10 +010068 memset(&chk_buf_ptr_fail_args, 0, sizeof(chk_buf_ptr_fail_args));
Ronald Cronad8c17b2022-06-10 17:18:09 +020069}
70
Gilles Peskine449bd832023-01-11 14:50:10 +010071int mbedtls_ssl_cmp_chk_buf_ptr_fail_args(mbedtls_ssl_chk_buf_ptr_args *args)
Ronald Cronad8c17b2022-06-10 17:18:09 +020072{
Gilles Peskine449bd832023-01-11 14:50:10 +010073 return (chk_buf_ptr_fail_args.cur != args->cur) ||
74 (chk_buf_ptr_fail_args.end != args->end) ||
75 (chk_buf_ptr_fail_args.need != args->need);
Ronald Cronad8c17b2022-06-10 17:18:09 +020076}
77#endif /* MBEDTLS_TEST_HOOKS */
78
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020079#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +010080
Hanno Beckera0e20d02019-05-15 14:03:01 +010081#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf8542cf2019-04-09 15:22:03 +010082/* Top-level Connection ID API */
83
Gilles Peskine449bd832023-01-11 14:50:10 +010084int mbedtls_ssl_conf_cid(mbedtls_ssl_config *conf,
85 size_t len,
86 int ignore_other_cid)
Hanno Beckerad4a1372019-05-03 13:06:44 +010087{
Gilles Peskine449bd832023-01-11 14:50:10 +010088 if (len > MBEDTLS_SSL_CID_IN_LEN_MAX) {
89 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
90 }
Hanno Beckerad4a1372019-05-03 13:06:44 +010091
Gilles Peskine449bd832023-01-11 14:50:10 +010092 if (ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_FAIL &&
93 ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE) {
94 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Becker611ac772019-05-14 11:45:26 +010095 }
96
97 conf->ignore_unexpected_cid = ignore_other_cid;
Hanno Beckerad4a1372019-05-03 13:06:44 +010098 conf->cid_len = len;
Gilles Peskine449bd832023-01-11 14:50:10 +010099 return 0;
Hanno Beckerad4a1372019-05-03 13:06:44 +0100100}
101
Gilles Peskine449bd832023-01-11 14:50:10 +0100102int mbedtls_ssl_set_cid(mbedtls_ssl_context *ssl,
103 int enable,
104 unsigned char const *own_cid,
105 size_t own_cid_len)
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100106{
Gilles Peskine449bd832023-01-11 14:50:10 +0100107 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
108 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
109 }
Hanno Becker76a79ab2019-05-03 14:38:32 +0100110
Hanno Beckerca092242019-04-25 16:01:49 +0100111 ssl->negotiate_cid = enable;
Gilles Peskine449bd832023-01-11 14:50:10 +0100112 if (enable == MBEDTLS_SSL_CID_DISABLED) {
113 MBEDTLS_SSL_DEBUG_MSG(3, ("Disable use of CID extension."));
114 return 0;
Hanno Beckerca092242019-04-25 16:01:49 +0100115 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100116 MBEDTLS_SSL_DEBUG_MSG(3, ("Enable use of CID extension."));
117 MBEDTLS_SSL_DEBUG_BUF(3, "Own CID", own_cid, own_cid_len);
Hanno Beckerca092242019-04-25 16:01:49 +0100118
Gilles Peskine449bd832023-01-11 14:50:10 +0100119 if (own_cid_len != ssl->conf->cid_len) {
120 MBEDTLS_SSL_DEBUG_MSG(3, ("CID length %u does not match CID length %u in config",
121 (unsigned) own_cid_len,
122 (unsigned) ssl->conf->cid_len));
123 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Beckerca092242019-04-25 16:01:49 +0100124 }
125
Gilles Peskine449bd832023-01-11 14:50:10 +0100126 memcpy(ssl->own_cid, own_cid, own_cid_len);
Hanno Beckerb7ee0cf2019-04-30 14:07:31 +0100127 /* Truncation is not an issue here because
128 * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */
129 ssl->own_cid_len = (uint8_t) own_cid_len;
Hanno Beckerca092242019-04-25 16:01:49 +0100130
Gilles Peskine449bd832023-01-11 14:50:10 +0100131 return 0;
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100132}
133
Gilles Peskine449bd832023-01-11 14:50:10 +0100134int mbedtls_ssl_get_own_cid(mbedtls_ssl_context *ssl,
135 int *enabled,
136 unsigned char own_cid[MBEDTLS_SSL_CID_OUT_LEN_MAX],
137 size_t *own_cid_len)
Paul Elliott0113cf12022-03-11 20:26:47 +0000138{
139 *enabled = MBEDTLS_SSL_CID_DISABLED;
140
Gilles Peskine449bd832023-01-11 14:50:10 +0100141 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
142 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
143 }
Paul Elliott0113cf12022-03-11 20:26:47 +0000144
145 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID length is
146 * zero as this is indistinguishable from not requesting to use
147 * the CID extension. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100148 if (ssl->own_cid_len == 0 || ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED) {
149 return 0;
150 }
Paul Elliott0113cf12022-03-11 20:26:47 +0000151
Gilles Peskine449bd832023-01-11 14:50:10 +0100152 if (own_cid_len != NULL) {
Paul Elliott0113cf12022-03-11 20:26:47 +0000153 *own_cid_len = ssl->own_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100154 if (own_cid != NULL) {
155 memcpy(own_cid, ssl->own_cid, ssl->own_cid_len);
156 }
Paul Elliott0113cf12022-03-11 20:26:47 +0000157 }
158
159 *enabled = MBEDTLS_SSL_CID_ENABLED;
160
Gilles Peskine449bd832023-01-11 14:50:10 +0100161 return 0;
Paul Elliott0113cf12022-03-11 20:26:47 +0000162}
163
Gilles Peskine449bd832023-01-11 14:50:10 +0100164int mbedtls_ssl_get_peer_cid(mbedtls_ssl_context *ssl,
165 int *enabled,
166 unsigned char peer_cid[MBEDTLS_SSL_CID_OUT_LEN_MAX],
167 size_t *peer_cid_len)
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100168{
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100169 *enabled = MBEDTLS_SSL_CID_DISABLED;
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100170
Gilles Peskine449bd832023-01-11 14:50:10 +0100171 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
172 mbedtls_ssl_is_handshake_over(ssl) == 0) {
173 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Becker76a79ab2019-05-03 14:38:32 +0100174 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100175
Hanno Beckerc5f24222019-05-03 12:54:52 +0100176 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions
177 * were used, but client and server requested the empty CID.
178 * This is indistinguishable from not using the CID extension
179 * in the first place. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100180 if (ssl->transform_in->in_cid_len == 0 &&
181 ssl->transform_in->out_cid_len == 0) {
182 return 0;
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100183 }
184
Gilles Peskine449bd832023-01-11 14:50:10 +0100185 if (peer_cid_len != NULL) {
Hanno Becker615ef172019-05-22 16:50:35 +0100186 *peer_cid_len = ssl->transform_in->out_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100187 if (peer_cid != NULL) {
188 memcpy(peer_cid, ssl->transform_in->out_cid,
189 ssl->transform_in->out_cid_len);
Hanno Becker615ef172019-05-22 16:50:35 +0100190 }
191 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100192
193 *enabled = MBEDTLS_SSL_CID_ENABLED;
194
Gilles Peskine449bd832023-01-11 14:50:10 +0100195 return 0;
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100196}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100197#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100198
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200199#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200200
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200201#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200202/*
203 * Convert max_fragment_length codes to length.
204 * RFC 6066 says:
205 * enum{
206 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
207 * } MaxFragmentLength;
208 * and we add 0 -> extension unused
209 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100210static unsigned int ssl_mfl_code_to_length(int mfl)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200211{
Gilles Peskine449bd832023-01-11 14:50:10 +0100212 switch (mfl) {
213 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
214 return MBEDTLS_TLS_EXT_ADV_CONTENT_LEN;
215 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
216 return 512;
217 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
218 return 1024;
219 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
220 return 2048;
221 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
222 return 4096;
223 default:
224 return MBEDTLS_TLS_EXT_ADV_CONTENT_LEN;
Angus Grattond8213d02016-05-25 20:56:48 +1000225 }
226}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200227#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200228
Gilles Peskine449bd832023-01-11 14:50:10 +0100229int mbedtls_ssl_session_copy(mbedtls_ssl_session *dst,
230 const mbedtls_ssl_session *src)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200231{
Gilles Peskine449bd832023-01-11 14:50:10 +0100232 mbedtls_ssl_session_free(dst);
233 memcpy(dst, src, sizeof(mbedtls_ssl_session));
吴敬辉0b716112021-11-29 10:46:35 +0800234#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
235 dst->ticket = NULL;
Xiaokang Qian87306442022-10-12 09:47:38 +0000236#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
237 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaokang Qian126bf8e2022-10-13 02:22:40 +0000238 dst->hostname = NULL;
吴敬辉0b716112021-11-29 10:46:35 +0800239#endif
Xiaokang Qian87306442022-10-12 09:47:38 +0000240#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
吴敬辉0b716112021-11-29 10:46:35 +0800241
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200242#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker6d1986e2019-02-07 12:27:42 +0000243
244#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100245 if (src->peer_cert != NULL) {
Janos Follath865b3eb2019-12-16 11:46:15 +0000246 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker2292d1f2013-09-15 17:06:49 +0200247
Gilles Peskine449bd832023-01-11 14:50:10 +0100248 dst->peer_cert = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
249 if (dst->peer_cert == NULL) {
250 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
251 }
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200252
Gilles Peskine449bd832023-01-11 14:50:10 +0100253 mbedtls_x509_crt_init(dst->peer_cert);
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200254
Gilles Peskine449bd832023-01-11 14:50:10 +0100255 if ((ret = mbedtls_x509_crt_parse_der(dst->peer_cert, src->peer_cert->raw.p,
256 src->peer_cert->raw.len)) != 0) {
257 mbedtls_free(dst->peer_cert);
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200258 dst->peer_cert = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100259 return ret;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200260 }
261 }
Hanno Becker6d1986e2019-02-07 12:27:42 +0000262#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100263 if (src->peer_cert_digest != NULL) {
Hanno Becker9198ad12019-02-05 17:00:50 +0000264 dst->peer_cert_digest =
Gilles Peskine449bd832023-01-11 14:50:10 +0100265 mbedtls_calloc(1, src->peer_cert_digest_len);
266 if (dst->peer_cert_digest == NULL) {
267 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
268 }
Hanno Becker9198ad12019-02-05 17:00:50 +0000269
Gilles Peskine449bd832023-01-11 14:50:10 +0100270 memcpy(dst->peer_cert_digest, src->peer_cert_digest,
271 src->peer_cert_digest_len);
Hanno Becker9198ad12019-02-05 17:00:50 +0000272 dst->peer_cert_digest_type = src->peer_cert_digest_type;
Hanno Beckeraccc5992019-02-25 10:06:59 +0000273 dst->peer_cert_digest_len = src->peer_cert_digest_len;
Hanno Becker9198ad12019-02-05 17:00:50 +0000274 }
275#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
276
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200277#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200278
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200279#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100280 if (src->ticket != NULL) {
281 dst->ticket = mbedtls_calloc(1, src->ticket_len);
282 if (dst->ticket == NULL) {
283 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
284 }
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200285
Gilles Peskine449bd832023-01-11 14:50:10 +0100286 memcpy(dst->ticket, src->ticket, src->ticket_len);
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200287 }
Xiaokang Qian126bf8e2022-10-13 02:22:40 +0000288
289#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
290 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +0100291 if (src->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Xiaokang Qian126bf8e2022-10-13 02:22:40 +0000292 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100293 ret = mbedtls_ssl_session_set_hostname(dst, src->hostname);
294 if (ret != 0) {
295 return ret;
296 }
Xiaokang Qian126bf8e2022-10-13 02:22:40 +0000297 }
298#endif /* MBEDTLS_SSL_PROTO_TLS1_3 &&
299 MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200300#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200301
Gilles Peskine449bd832023-01-11 14:50:10 +0100302 return 0;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200303}
304
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500305#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200306MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100307static int resize_buffer(unsigned char **buffer, size_t len_new, size_t *len_old)
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500308{
Gilles Peskine449bd832023-01-11 14:50:10 +0100309 unsigned char *resized_buffer = mbedtls_calloc(1, len_new);
310 if (resized_buffer == NULL) {
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500311 return -1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100312 }
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500313
314 /* We want to copy len_new bytes when downsizing the buffer, and
315 * len_old bytes when upsizing, so we choose the smaller of two sizes,
316 * to fit one buffer into another. Size checks, ensuring that no data is
317 * lost, are done outside of this function. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100318 memcpy(resized_buffer, *buffer,
319 (len_new < *len_old) ? len_new : *len_old);
320 mbedtls_platform_zeroize(*buffer, *len_old);
321 mbedtls_free(*buffer);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500322
323 *buffer = resized_buffer;
324 *len_old = len_new;
325
326 return 0;
327}
Andrzej Kurek4a063792020-10-21 15:08:44 +0200328
Gilles Peskine449bd832023-01-11 14:50:10 +0100329static void handle_buffer_resizing(mbedtls_ssl_context *ssl, int downsizing,
330 size_t in_buf_new_len,
331 size_t out_buf_new_len)
Andrzej Kurek4a063792020-10-21 15:08:44 +0200332{
333 int modified = 0;
334 size_t written_in = 0, iv_offset_in = 0, len_offset_in = 0;
335 size_t written_out = 0, iv_offset_out = 0, len_offset_out = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100336 if (ssl->in_buf != NULL) {
Andrzej Kurek4a063792020-10-21 15:08:44 +0200337 written_in = ssl->in_msg - ssl->in_buf;
338 iv_offset_in = ssl->in_iv - ssl->in_buf;
339 len_offset_in = ssl->in_len - ssl->in_buf;
Gilles Peskine449bd832023-01-11 14:50:10 +0100340 if (downsizing ?
Andrzej Kurek4a063792020-10-21 15:08:44 +0200341 ssl->in_buf_len > in_buf_new_len && ssl->in_left < in_buf_new_len :
Gilles Peskine449bd832023-01-11 14:50:10 +0100342 ssl->in_buf_len < in_buf_new_len) {
343 if (resize_buffer(&ssl->in_buf, in_buf_new_len, &ssl->in_buf_len) != 0) {
344 MBEDTLS_SSL_DEBUG_MSG(1, ("input buffer resizing failed - out of memory"));
345 } else {
346 MBEDTLS_SSL_DEBUG_MSG(2, ("Reallocating in_buf to %" MBEDTLS_PRINTF_SIZET,
347 in_buf_new_len));
Andrzej Kurek4a063792020-10-21 15:08:44 +0200348 modified = 1;
349 }
350 }
351 }
352
Gilles Peskine449bd832023-01-11 14:50:10 +0100353 if (ssl->out_buf != NULL) {
Andrzej Kurek4a063792020-10-21 15:08:44 +0200354 written_out = ssl->out_msg - ssl->out_buf;
355 iv_offset_out = ssl->out_iv - ssl->out_buf;
356 len_offset_out = ssl->out_len - ssl->out_buf;
Gilles Peskine449bd832023-01-11 14:50:10 +0100357 if (downsizing ?
Andrzej Kurek4a063792020-10-21 15:08:44 +0200358 ssl->out_buf_len > out_buf_new_len && ssl->out_left < out_buf_new_len :
Gilles Peskine449bd832023-01-11 14:50:10 +0100359 ssl->out_buf_len < out_buf_new_len) {
360 if (resize_buffer(&ssl->out_buf, out_buf_new_len, &ssl->out_buf_len) != 0) {
361 MBEDTLS_SSL_DEBUG_MSG(1, ("output buffer resizing failed - out of memory"));
362 } else {
363 MBEDTLS_SSL_DEBUG_MSG(2, ("Reallocating out_buf to %" MBEDTLS_PRINTF_SIZET,
364 out_buf_new_len));
Andrzej Kurek4a063792020-10-21 15:08:44 +0200365 modified = 1;
366 }
367 }
368 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100369 if (modified) {
Andrzej Kurek4a063792020-10-21 15:08:44 +0200370 /* Update pointers here to avoid doing it twice. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100371 mbedtls_ssl_reset_in_out_pointers(ssl);
Andrzej Kurek4a063792020-10-21 15:08:44 +0200372 /* Fields below might not be properly updated with record
373 * splitting or with CID, so they are manually updated here. */
374 ssl->out_msg = ssl->out_buf + written_out;
375 ssl->out_len = ssl->out_buf + len_offset_out;
376 ssl->out_iv = ssl->out_buf + iv_offset_out;
377
378 ssl->in_msg = ssl->in_buf + written_in;
379 ssl->in_len = ssl->in_buf + len_offset_in;
380 ssl->in_iv = ssl->in_buf + iv_offset_in;
381 }
382}
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500383#endif /* MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH */
384
Jerry Yudb8c48a2022-01-27 14:54:54 +0800385#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yued14c932022-02-17 13:40:45 +0800386
387#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Gilles Peskine449bd832023-01-11 14:50:10 +0100388typedef int (*tls_prf_fn)(const unsigned char *secret, size_t slen,
389 const char *label,
390 const unsigned char *random, size_t rlen,
391 unsigned char *dstbuf, size_t dlen);
Jerry Yued14c932022-02-17 13:40:45 +0800392
Gilles Peskine449bd832023-01-11 14:50:10 +0100393static tls_prf_fn ssl_tls12prf_from_cs(int ciphersuite_id);
Jerry Yued14c932022-02-17 13:40:45 +0800394
395#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
396
397/* Type for the TLS PRF */
398typedef int ssl_tls_prf_t(const unsigned char *, size_t, const char *,
399 const unsigned char *, size_t,
400 unsigned char *, size_t);
401
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200402MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100403static int ssl_tls12_populate_transform(mbedtls_ssl_transform *transform,
404 int ciphersuite,
405 const unsigned char master[48],
Neil Armstrongf2c82f02022-04-05 11:16:53 +0200406#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +0100407 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +0200408#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +0100409 ssl_tls_prf_t tls_prf,
410 const unsigned char randbytes[64],
411 mbedtls_ssl_protocol_version tls_version,
412 unsigned endpoint,
413 const mbedtls_ssl_context *ssl);
Jerry Yued14c932022-02-17 13:40:45 +0800414
Andrzej Kurek25f27152022-08-17 16:09:31 -0400415#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200416MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100417static int tls_prf_sha256(const unsigned char *secret, size_t slen,
Ron Eldor51d3ab52019-05-12 14:54:30 +0300418 const char *label,
419 const unsigned char *random, size_t rlen,
Gilles Peskine449bd832023-01-11 14:50:10 +0100420 unsigned char *dstbuf, size_t dlen);
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100421static int ssl_calc_verify_tls_sha256(const mbedtls_ssl_context *, unsigned char *, size_t *);
422static int ssl_calc_finished_tls_sha256(mbedtls_ssl_context *, unsigned char *, int);
Gilles Peskine449bd832023-01-11 14:50:10 +0100423
424#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
425
426#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
427MBEDTLS_CHECK_RETURN_CRITICAL
428static int tls_prf_sha384(const unsigned char *secret, size_t slen,
429 const char *label,
430 const unsigned char *random, size_t rlen,
431 unsigned char *dstbuf, size_t dlen);
432
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100433static int ssl_calc_verify_tls_sha384(const mbedtls_ssl_context *, unsigned char *, size_t *);
434static int ssl_calc_finished_tls_sha384(mbedtls_ssl_context *, unsigned char *, int);
Gilles Peskine449bd832023-01-11 14:50:10 +0100435#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
436
437static size_t ssl_tls12_session_save(const mbedtls_ssl_session *session,
438 unsigned char *buf,
439 size_t buf_len);
440
441MBEDTLS_CHECK_RETURN_CRITICAL
442static int ssl_tls12_session_load(mbedtls_ssl_session *session,
443 const unsigned char *buf,
444 size_t len);
445#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
446
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100447static int ssl_update_checksum_start(mbedtls_ssl_context *, const unsigned char *, size_t);
Gilles Peskine449bd832023-01-11 14:50:10 +0100448
449#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100450static int ssl_update_checksum_sha256(mbedtls_ssl_context *, const unsigned char *, size_t);
Gilles Peskine449bd832023-01-11 14:50:10 +0100451#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
452
453#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100454static int ssl_update_checksum_sha384(mbedtls_ssl_context *, const unsigned char *, size_t);
Gilles Peskine449bd832023-01-11 14:50:10 +0100455#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
456
457int mbedtls_ssl_tls_prf(const mbedtls_tls_prf_types prf,
458 const unsigned char *secret, size_t slen,
459 const char *label,
460 const unsigned char *random, size_t rlen,
461 unsigned char *dstbuf, size_t dlen)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300462{
463 mbedtls_ssl_tls_prf_cb *tls_prf = NULL;
464
Gilles Peskine449bd832023-01-11 14:50:10 +0100465 switch (prf) {
Ron Eldord2f25f72019-05-15 14:54:22 +0300466#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek25f27152022-08-17 16:09:31 -0400467#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300468 case MBEDTLS_SSL_TLS_PRF_SHA384:
469 tls_prf = tls_prf_sha384;
Gilles Peskine449bd832023-01-11 14:50:10 +0100470 break;
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400471#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -0400472#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300473 case MBEDTLS_SSL_TLS_PRF_SHA256:
474 tls_prf = tls_prf_sha256;
Gilles Peskine449bd832023-01-11 14:50:10 +0100475 break;
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400476#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Ron Eldord2f25f72019-05-15 14:54:22 +0300477#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100478 default:
479 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Ron Eldor51d3ab52019-05-12 14:54:30 +0300480 }
481
Gilles Peskine449bd832023-01-11 14:50:10 +0100482 return tls_prf(secret, slen, label, random, rlen, dstbuf, dlen);
Ron Eldor51d3ab52019-05-12 14:54:30 +0300483}
484
Jerry Yuc73c6182022-02-08 20:29:25 +0800485#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100486static void ssl_clear_peer_cert(mbedtls_ssl_session *session)
Jerry Yuc73c6182022-02-08 20:29:25 +0800487{
488#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100489 if (session->peer_cert != NULL) {
490 mbedtls_x509_crt_free(session->peer_cert);
491 mbedtls_free(session->peer_cert);
Jerry Yuc73c6182022-02-08 20:29:25 +0800492 session->peer_cert = NULL;
493 }
494#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100495 if (session->peer_cert_digest != NULL) {
Jerry Yuc73c6182022-02-08 20:29:25 +0800496 /* Zeroization is not necessary. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100497 mbedtls_free(session->peer_cert_digest);
Jerry Yuc73c6182022-02-08 20:29:25 +0800498 session->peer_cert_digest = NULL;
499 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
500 session->peer_cert_digest_len = 0;
501 }
502#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
503}
504#endif /* MBEDTLS_X509_CRT_PARSE_C */
505
Gilles Peskine449bd832023-01-11 14:50:10 +0100506uint32_t mbedtls_ssl_get_extension_id(unsigned int extension_type)
Jerry Yu7a485c12022-10-31 13:08:18 +0800507{
Gilles Peskine449bd832023-01-11 14:50:10 +0100508 switch (extension_type) {
Jerry Yu7a485c12022-10-31 13:08:18 +0800509 case MBEDTLS_TLS_EXT_SERVERNAME:
Gilles Peskine449bd832023-01-11 14:50:10 +0100510 return MBEDTLS_SSL_EXT_ID_SERVERNAME;
Jerry Yu7a485c12022-10-31 13:08:18 +0800511
512 case MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100513 return MBEDTLS_SSL_EXT_ID_MAX_FRAGMENT_LENGTH;
Jerry Yu7a485c12022-10-31 13:08:18 +0800514
515 case MBEDTLS_TLS_EXT_STATUS_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +0100516 return MBEDTLS_SSL_EXT_ID_STATUS_REQUEST;
Jerry Yu7a485c12022-10-31 13:08:18 +0800517
518 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
Gilles Peskine449bd832023-01-11 14:50:10 +0100519 return MBEDTLS_SSL_EXT_ID_SUPPORTED_GROUPS;
Jerry Yu7a485c12022-10-31 13:08:18 +0800520
521 case MBEDTLS_TLS_EXT_SIG_ALG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100522 return MBEDTLS_SSL_EXT_ID_SIG_ALG;
Jerry Yu7a485c12022-10-31 13:08:18 +0800523
524 case MBEDTLS_TLS_EXT_USE_SRTP:
Gilles Peskine449bd832023-01-11 14:50:10 +0100525 return MBEDTLS_SSL_EXT_ID_USE_SRTP;
Jerry Yu7a485c12022-10-31 13:08:18 +0800526
527 case MBEDTLS_TLS_EXT_HEARTBEAT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100528 return MBEDTLS_SSL_EXT_ID_HEARTBEAT;
Jerry Yu7a485c12022-10-31 13:08:18 +0800529
530 case MBEDTLS_TLS_EXT_ALPN:
Gilles Peskine449bd832023-01-11 14:50:10 +0100531 return MBEDTLS_SSL_EXT_ID_ALPN;
Jerry Yu7a485c12022-10-31 13:08:18 +0800532
533 case MBEDTLS_TLS_EXT_SCT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100534 return MBEDTLS_SSL_EXT_ID_SCT;
Jerry Yu7a485c12022-10-31 13:08:18 +0800535
536 case MBEDTLS_TLS_EXT_CLI_CERT_TYPE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100537 return MBEDTLS_SSL_EXT_ID_CLI_CERT_TYPE;
Jerry Yu7a485c12022-10-31 13:08:18 +0800538
539 case MBEDTLS_TLS_EXT_SERV_CERT_TYPE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100540 return MBEDTLS_SSL_EXT_ID_SERV_CERT_TYPE;
Jerry Yu7a485c12022-10-31 13:08:18 +0800541
542 case MBEDTLS_TLS_EXT_PADDING:
Gilles Peskine449bd832023-01-11 14:50:10 +0100543 return MBEDTLS_SSL_EXT_ID_PADDING;
Jerry Yu7a485c12022-10-31 13:08:18 +0800544
545 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100546 return MBEDTLS_SSL_EXT_ID_PRE_SHARED_KEY;
Jerry Yu7a485c12022-10-31 13:08:18 +0800547
548 case MBEDTLS_TLS_EXT_EARLY_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100549 return MBEDTLS_SSL_EXT_ID_EARLY_DATA;
Jerry Yu7a485c12022-10-31 13:08:18 +0800550
551 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
Gilles Peskine449bd832023-01-11 14:50:10 +0100552 return MBEDTLS_SSL_EXT_ID_SUPPORTED_VERSIONS;
Jerry Yu7a485c12022-10-31 13:08:18 +0800553
554 case MBEDTLS_TLS_EXT_COOKIE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100555 return MBEDTLS_SSL_EXT_ID_COOKIE;
Jerry Yu7a485c12022-10-31 13:08:18 +0800556
557 case MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES:
Gilles Peskine449bd832023-01-11 14:50:10 +0100558 return MBEDTLS_SSL_EXT_ID_PSK_KEY_EXCHANGE_MODES;
Jerry Yu7a485c12022-10-31 13:08:18 +0800559
560 case MBEDTLS_TLS_EXT_CERT_AUTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100561 return MBEDTLS_SSL_EXT_ID_CERT_AUTH;
Jerry Yu7a485c12022-10-31 13:08:18 +0800562
563 case MBEDTLS_TLS_EXT_OID_FILTERS:
Gilles Peskine449bd832023-01-11 14:50:10 +0100564 return MBEDTLS_SSL_EXT_ID_OID_FILTERS;
Jerry Yu7a485c12022-10-31 13:08:18 +0800565
566 case MBEDTLS_TLS_EXT_POST_HANDSHAKE_AUTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100567 return MBEDTLS_SSL_EXT_ID_POST_HANDSHAKE_AUTH;
Jerry Yu7a485c12022-10-31 13:08:18 +0800568
569 case MBEDTLS_TLS_EXT_SIG_ALG_CERT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100570 return MBEDTLS_SSL_EXT_ID_SIG_ALG_CERT;
Jerry Yu7a485c12022-10-31 13:08:18 +0800571
572 case MBEDTLS_TLS_EXT_KEY_SHARE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100573 return MBEDTLS_SSL_EXT_ID_KEY_SHARE;
Jerry Yu7a485c12022-10-31 13:08:18 +0800574
575 case MBEDTLS_TLS_EXT_TRUNCATED_HMAC:
Gilles Peskine449bd832023-01-11 14:50:10 +0100576 return MBEDTLS_SSL_EXT_ID_TRUNCATED_HMAC;
Jerry Yu7a485c12022-10-31 13:08:18 +0800577
578 case MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS:
Gilles Peskine449bd832023-01-11 14:50:10 +0100579 return MBEDTLS_SSL_EXT_ID_SUPPORTED_POINT_FORMATS;
Jerry Yu7a485c12022-10-31 13:08:18 +0800580
581 case MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC:
Gilles Peskine449bd832023-01-11 14:50:10 +0100582 return MBEDTLS_SSL_EXT_ID_ENCRYPT_THEN_MAC;
Jerry Yu7a485c12022-10-31 13:08:18 +0800583
584 case MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +0100585 return MBEDTLS_SSL_EXT_ID_EXTENDED_MASTER_SECRET;
Jerry Yu7a485c12022-10-31 13:08:18 +0800586
587 case MBEDTLS_TLS_EXT_SESSION_TICKET:
Gilles Peskine449bd832023-01-11 14:50:10 +0100588 return MBEDTLS_SSL_EXT_ID_SESSION_TICKET;
Jerry Yu7a485c12022-10-31 13:08:18 +0800589
590 }
591
Gilles Peskine449bd832023-01-11 14:50:10 +0100592 return MBEDTLS_SSL_EXT_ID_UNRECOGNIZED;
Jerry Yu7a485c12022-10-31 13:08:18 +0800593}
594
Gilles Peskine449bd832023-01-11 14:50:10 +0100595uint32_t mbedtls_ssl_get_extension_mask(unsigned int extension_type)
Jerry Yu7a485c12022-10-31 13:08:18 +0800596{
Gilles Peskine449bd832023-01-11 14:50:10 +0100597 return 1 << mbedtls_ssl_get_extension_id(extension_type);
Jerry Yu7a485c12022-10-31 13:08:18 +0800598}
599
Jerry Yud25cab02022-10-31 12:48:30 +0800600#if defined(MBEDTLS_DEBUG_C)
601static const char *extension_name_table[] = {
Jerry Yuea52ed92022-11-08 21:01:17 +0800602 [MBEDTLS_SSL_EXT_ID_UNRECOGNIZED] = "unrecognized",
Jerry Yud25cab02022-10-31 12:48:30 +0800603 [MBEDTLS_SSL_EXT_ID_SERVERNAME] = "server_name",
604 [MBEDTLS_SSL_EXT_ID_MAX_FRAGMENT_LENGTH] = "max_fragment_length",
605 [MBEDTLS_SSL_EXT_ID_STATUS_REQUEST] = "status_request",
606 [MBEDTLS_SSL_EXT_ID_SUPPORTED_GROUPS] = "supported_groups",
607 [MBEDTLS_SSL_EXT_ID_SIG_ALG] = "signature_algorithms",
608 [MBEDTLS_SSL_EXT_ID_USE_SRTP] = "use_srtp",
609 [MBEDTLS_SSL_EXT_ID_HEARTBEAT] = "heartbeat",
610 [MBEDTLS_SSL_EXT_ID_ALPN] = "application_layer_protocol_negotiation",
611 [MBEDTLS_SSL_EXT_ID_SCT] = "signed_certificate_timestamp",
612 [MBEDTLS_SSL_EXT_ID_CLI_CERT_TYPE] = "client_certificate_type",
613 [MBEDTLS_SSL_EXT_ID_SERV_CERT_TYPE] = "server_certificate_type",
614 [MBEDTLS_SSL_EXT_ID_PADDING] = "padding",
615 [MBEDTLS_SSL_EXT_ID_PRE_SHARED_KEY] = "pre_shared_key",
616 [MBEDTLS_SSL_EXT_ID_EARLY_DATA] = "early_data",
617 [MBEDTLS_SSL_EXT_ID_SUPPORTED_VERSIONS] = "supported_versions",
618 [MBEDTLS_SSL_EXT_ID_COOKIE] = "cookie",
619 [MBEDTLS_SSL_EXT_ID_PSK_KEY_EXCHANGE_MODES] = "psk_key_exchange_modes",
620 [MBEDTLS_SSL_EXT_ID_CERT_AUTH] = "certificate_authorities",
621 [MBEDTLS_SSL_EXT_ID_OID_FILTERS] = "oid_filters",
622 [MBEDTLS_SSL_EXT_ID_POST_HANDSHAKE_AUTH] = "post_handshake_auth",
623 [MBEDTLS_SSL_EXT_ID_SIG_ALG_CERT] = "signature_algorithms_cert",
624 [MBEDTLS_SSL_EXT_ID_KEY_SHARE] = "key_share",
625 [MBEDTLS_SSL_EXT_ID_TRUNCATED_HMAC] = "truncated_hmac",
626 [MBEDTLS_SSL_EXT_ID_SUPPORTED_POINT_FORMATS] = "supported_point_formats",
627 [MBEDTLS_SSL_EXT_ID_ENCRYPT_THEN_MAC] = "encrypt_then_mac",
628 [MBEDTLS_SSL_EXT_ID_EXTENDED_MASTER_SECRET] = "extended_master_secret",
629 [MBEDTLS_SSL_EXT_ID_SESSION_TICKET] = "session_ticket"
630};
631
Gilles Peskine449bd832023-01-11 14:50:10 +0100632static unsigned int extension_type_table[] = {
Jerry Yud25cab02022-10-31 12:48:30 +0800633 [MBEDTLS_SSL_EXT_ID_UNRECOGNIZED] = 0xff,
634 [MBEDTLS_SSL_EXT_ID_SERVERNAME] = MBEDTLS_TLS_EXT_SERVERNAME,
635 [MBEDTLS_SSL_EXT_ID_MAX_FRAGMENT_LENGTH] = MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH,
636 [MBEDTLS_SSL_EXT_ID_STATUS_REQUEST] = MBEDTLS_TLS_EXT_STATUS_REQUEST,
637 [MBEDTLS_SSL_EXT_ID_SUPPORTED_GROUPS] = MBEDTLS_TLS_EXT_SUPPORTED_GROUPS,
638 [MBEDTLS_SSL_EXT_ID_SIG_ALG] = MBEDTLS_TLS_EXT_SIG_ALG,
639 [MBEDTLS_SSL_EXT_ID_USE_SRTP] = MBEDTLS_TLS_EXT_USE_SRTP,
640 [MBEDTLS_SSL_EXT_ID_HEARTBEAT] = MBEDTLS_TLS_EXT_HEARTBEAT,
641 [MBEDTLS_SSL_EXT_ID_ALPN] = MBEDTLS_TLS_EXT_ALPN,
642 [MBEDTLS_SSL_EXT_ID_SCT] = MBEDTLS_TLS_EXT_SCT,
643 [MBEDTLS_SSL_EXT_ID_CLI_CERT_TYPE] = MBEDTLS_TLS_EXT_CLI_CERT_TYPE,
644 [MBEDTLS_SSL_EXT_ID_SERV_CERT_TYPE] = MBEDTLS_TLS_EXT_SERV_CERT_TYPE,
645 [MBEDTLS_SSL_EXT_ID_PADDING] = MBEDTLS_TLS_EXT_PADDING,
646 [MBEDTLS_SSL_EXT_ID_PRE_SHARED_KEY] = MBEDTLS_TLS_EXT_PRE_SHARED_KEY,
647 [MBEDTLS_SSL_EXT_ID_EARLY_DATA] = MBEDTLS_TLS_EXT_EARLY_DATA,
648 [MBEDTLS_SSL_EXT_ID_SUPPORTED_VERSIONS] = MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS,
649 [MBEDTLS_SSL_EXT_ID_COOKIE] = MBEDTLS_TLS_EXT_COOKIE,
650 [MBEDTLS_SSL_EXT_ID_PSK_KEY_EXCHANGE_MODES] = MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES,
651 [MBEDTLS_SSL_EXT_ID_CERT_AUTH] = MBEDTLS_TLS_EXT_CERT_AUTH,
652 [MBEDTLS_SSL_EXT_ID_OID_FILTERS] = MBEDTLS_TLS_EXT_OID_FILTERS,
653 [MBEDTLS_SSL_EXT_ID_POST_HANDSHAKE_AUTH] = MBEDTLS_TLS_EXT_POST_HANDSHAKE_AUTH,
654 [MBEDTLS_SSL_EXT_ID_SIG_ALG_CERT] = MBEDTLS_TLS_EXT_SIG_ALG_CERT,
655 [MBEDTLS_SSL_EXT_ID_KEY_SHARE] = MBEDTLS_TLS_EXT_KEY_SHARE,
656 [MBEDTLS_SSL_EXT_ID_TRUNCATED_HMAC] = MBEDTLS_TLS_EXT_TRUNCATED_HMAC,
657 [MBEDTLS_SSL_EXT_ID_SUPPORTED_POINT_FORMATS] = MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS,
658 [MBEDTLS_SSL_EXT_ID_ENCRYPT_THEN_MAC] = MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC,
659 [MBEDTLS_SSL_EXT_ID_EXTENDED_MASTER_SECRET] = MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET,
660 [MBEDTLS_SSL_EXT_ID_SESSION_TICKET] = MBEDTLS_TLS_EXT_SESSION_TICKET
661};
662
Gilles Peskine449bd832023-01-11 14:50:10 +0100663const char *mbedtls_ssl_get_extension_name(unsigned int extension_type)
Jerry Yud25cab02022-10-31 12:48:30 +0800664{
Gilles Peskine449bd832023-01-11 14:50:10 +0100665 return extension_name_table[
666 mbedtls_ssl_get_extension_id(extension_type)];
Jerry Yud25cab02022-10-31 12:48:30 +0800667}
668
Gilles Peskine449bd832023-01-11 14:50:10 +0100669static const char *ssl_tls13_get_hs_msg_name(int hs_msg_type)
Jerry Yud25cab02022-10-31 12:48:30 +0800670{
Gilles Peskine449bd832023-01-11 14:50:10 +0100671 switch (hs_msg_type) {
Jerry Yud25cab02022-10-31 12:48:30 +0800672 case MBEDTLS_SSL_HS_CLIENT_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +0100673 return "ClientHello";
Jerry Yud25cab02022-10-31 12:48:30 +0800674 case MBEDTLS_SSL_HS_SERVER_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +0100675 return "ServerHello";
Jerry Yud25cab02022-10-31 12:48:30 +0800676 case MBEDTLS_SSL_TLS1_3_HS_HELLO_RETRY_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +0100677 return "HelloRetryRequest";
Jerry Yud25cab02022-10-31 12:48:30 +0800678 case MBEDTLS_SSL_HS_NEW_SESSION_TICKET:
Gilles Peskine449bd832023-01-11 14:50:10 +0100679 return "NewSessionTicket";
Jerry Yud25cab02022-10-31 12:48:30 +0800680 case MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS:
Gilles Peskine449bd832023-01-11 14:50:10 +0100681 return "EncryptedExtensions";
Jerry Yud25cab02022-10-31 12:48:30 +0800682 case MBEDTLS_SSL_HS_CERTIFICATE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100683 return "Certificate";
Jerry Yud25cab02022-10-31 12:48:30 +0800684 case MBEDTLS_SSL_HS_CERTIFICATE_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +0100685 return "CertificateRequest";
Jerry Yud25cab02022-10-31 12:48:30 +0800686 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100687 return "Unknown";
Jerry Yud25cab02022-10-31 12:48:30 +0800688}
689
Gilles Peskine449bd832023-01-11 14:50:10 +0100690void mbedtls_ssl_print_extension(const mbedtls_ssl_context *ssl,
691 int level, const char *file, int line,
692 int hs_msg_type, unsigned int extension_type,
693 const char *extra_msg0, const char *extra_msg1)
Jerry Yud25cab02022-10-31 12:48:30 +0800694{
695 const char *extra_msg;
Gilles Peskine449bd832023-01-11 14:50:10 +0100696 if (extra_msg0 && extra_msg1) {
Jerry Yud25cab02022-10-31 12:48:30 +0800697 mbedtls_debug_print_msg(
698 ssl, level, file, line,
699 "%s: %s(%u) extension %s %s.",
Gilles Peskine449bd832023-01-11 14:50:10 +0100700 ssl_tls13_get_hs_msg_name(hs_msg_type),
701 mbedtls_ssl_get_extension_name(extension_type),
Jerry Yud25cab02022-10-31 12:48:30 +0800702 extension_type,
Gilles Peskine449bd832023-01-11 14:50:10 +0100703 extra_msg0, extra_msg1);
Jerry Yud25cab02022-10-31 12:48:30 +0800704 return;
705 }
706
707 extra_msg = extra_msg0 ? extra_msg0 : extra_msg1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100708 if (extra_msg) {
Jerry Yud25cab02022-10-31 12:48:30 +0800709 mbedtls_debug_print_msg(
710 ssl, level, file, line,
Gilles Peskine449bd832023-01-11 14:50:10 +0100711 "%s: %s(%u) extension %s.", ssl_tls13_get_hs_msg_name(hs_msg_type),
712 mbedtls_ssl_get_extension_name(extension_type), extension_type,
713 extra_msg);
Jerry Yud25cab02022-10-31 12:48:30 +0800714 return;
715 }
716
717 mbedtls_debug_print_msg(
718 ssl, level, file, line,
Gilles Peskine449bd832023-01-11 14:50:10 +0100719 "%s: %s(%u) extension.", ssl_tls13_get_hs_msg_name(hs_msg_type),
720 mbedtls_ssl_get_extension_name(extension_type), extension_type);
Jerry Yud25cab02022-10-31 12:48:30 +0800721}
722
Gilles Peskine449bd832023-01-11 14:50:10 +0100723void mbedtls_ssl_print_extensions(const mbedtls_ssl_context *ssl,
724 int level, const char *file, int line,
725 int hs_msg_type, uint32_t extensions_mask,
726 const char *extra)
Jerry Yud25cab02022-10-31 12:48:30 +0800727{
728
Gilles Peskine449bd832023-01-11 14:50:10 +0100729 for (unsigned i = 0;
730 i < sizeof(extension_name_table) / sizeof(extension_name_table[0]);
731 i++) {
Jerry Yu79aa7212022-11-08 21:30:21 +0800732 mbedtls_ssl_print_extension(
Jerry Yuea52ed92022-11-08 21:01:17 +0800733 ssl, level, file, line, hs_msg_type, extension_type_table[i],
Gilles Peskine449bd832023-01-11 14:50:10 +0100734 extensions_mask & (1 << i) ? "exists" : "does not exist", extra);
Jerry Yud25cab02022-10-31 12:48:30 +0800735 }
736}
737
Pengyu Lvee455c02023-01-12 14:37:24 +0800738#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS)
739#define ARRAY_LENGTH(a) (sizeof(a) / sizeof(*(a)))
740
741static const char *ticket_flag_name_table[] =
742{
743 [0] = "ALLOW_PSK_RESUMPTION",
744 [2] = "ALLOW_PSK_EPHEMERAL_RESUMPTION",
745 [3] = "ALLOW_EARLY_DATA",
746};
747
Pengyu Lv4938a562023-01-16 11:28:49 +0800748void mbedtls_ssl_print_ticket_flags(const mbedtls_ssl_context *ssl,
749 int level, const char *file, int line,
750 unsigned int flags)
Pengyu Lvee455c02023-01-12 14:37:24 +0800751{
752 size_t i;
753
754 mbedtls_debug_print_msg(ssl, level, file, line,
Pengyu Lv4938a562023-01-16 11:28:49 +0800755 "print ticket_flags (0x%02x)", flags);
756
757 flags = flags & MBEDTLS_SSL_TLS1_3_TICKET_FLAGS_MASK;
Pengyu Lvee455c02023-01-12 14:37:24 +0800758
759 for (i = 0; i < ARRAY_LENGTH(ticket_flag_name_table); i++) {
Pengyu Lv4938a562023-01-16 11:28:49 +0800760 if ((flags & (1 << i))) {
Pengyu Lvee455c02023-01-12 14:37:24 +0800761 mbedtls_debug_print_msg(ssl, level, file, line, "- %s is set.",
762 ticket_flag_name_table[i]);
763 }
764 }
765}
766#endif /* MBEDTLS_SSL_PROTO_TLS1_3 && MBEDTLS_SSL_SESSION_TICKETS */
767
Jerry Yud25cab02022-10-31 12:48:30 +0800768#endif /* MBEDTLS_DEBUG_C */
769
Gilles Peskine449bd832023-01-11 14:50:10 +0100770void mbedtls_ssl_optimize_checksum(mbedtls_ssl_context *ssl,
771 const mbedtls_ssl_ciphersuite_t *ciphersuite_info)
Jerry Yuc73c6182022-02-08 20:29:25 +0800772{
773 ((void) ciphersuite_info);
774
Andrzej Kurek25f27152022-08-17 16:09:31 -0400775#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +0100776 if (ciphersuite_info->mac == MBEDTLS_MD_SHA384) {
Jerry Yuc73c6182022-02-08 20:29:25 +0800777 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
Gilles Peskine449bd832023-01-11 14:50:10 +0100778 } else
Jerry Yuc73c6182022-02-08 20:29:25 +0800779#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400780#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +0100781 if (ciphersuite_info->mac != MBEDTLS_MD_SHA384) {
Jerry Yuc73c6182022-02-08 20:29:25 +0800782 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Gilles Peskine449bd832023-01-11 14:50:10 +0100783 } else
Jerry Yuc73c6182022-02-08 20:29:25 +0800784#endif
785 {
Gilles Peskine449bd832023-01-11 14:50:10 +0100786 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
Jerry Yuc73c6182022-02-08 20:29:25 +0800787 return;
788 }
789}
790
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100791int mbedtls_ssl_add_hs_hdr_to_checksum(mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100792 unsigned hs_type,
793 size_t total_hs_len)
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100794{
795 unsigned char hs_hdr[4];
796
797 /* Build HS header for checksum update. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100798 hs_hdr[0] = MBEDTLS_BYTE_0(hs_type);
799 hs_hdr[1] = MBEDTLS_BYTE_2(total_hs_len);
800 hs_hdr[2] = MBEDTLS_BYTE_1(total_hs_len);
801 hs_hdr[3] = MBEDTLS_BYTE_0(total_hs_len);
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100802
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100803 return ssl->handshake->update_checksum(ssl, hs_hdr, sizeof(hs_hdr));
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100804}
805
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100806int mbedtls_ssl_add_hs_msg_to_checksum(mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100807 unsigned hs_type,
808 unsigned char const *msg,
809 size_t msg_len)
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100810{
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100811 int ret;
812 ret = mbedtls_ssl_add_hs_hdr_to_checksum(ssl, hs_type, msg_len);
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100813 if (ret != 0) {
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100814 return ret;
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100815 }
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100816 return ssl->handshake->update_checksum(ssl, msg, msg_len);
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100817}
818
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100819int mbedtls_ssl_reset_checksum(mbedtls_ssl_context *ssl)
Jerry Yuc73c6182022-02-08 20:29:25 +0800820{
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100821#if defined(MBEDTLS_USE_PSA_CRYPTO)
822 psa_status_t status;
823#else
824 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
825#endif
Jerry Yuc73c6182022-02-08 20:29:25 +0800826 ((void) ssl);
Andrzej Kurek25f27152022-08-17 16:09:31 -0400827#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800828#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100829 status = psa_hash_abort(&ssl->handshake->fin_sha256_psa);
830 if (status != PSA_SUCCESS) {
831 return mbedtls_md_error_from_psa(status);
832 }
833 status = psa_hash_setup(&ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256);
834 if (status != PSA_SUCCESS) {
835 return mbedtls_md_error_from_psa(status);
836 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800837#else
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100838 ret = mbedtls_sha256_starts(&ssl->handshake->fin_sha256, 0);
839 if (ret != 0) {
840 return ret;
841 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800842#endif
843#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400844#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800845#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100846 status = psa_hash_abort(&ssl->handshake->fin_sha384_psa);
847 if (status != PSA_SUCCESS) {
848 return mbedtls_md_error_from_psa(status);
849 }
850 status = psa_hash_setup(&ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384);
851 if (status != PSA_SUCCESS) {
852 return mbedtls_md_error_from_psa(status);
853 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800854#else
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100855 ret = mbedtls_sha512_starts(&ssl->handshake->fin_sha384, 1);
856 if (ret != 0) {
857 return ret;
858 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800859#endif
860#endif
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100861 return 0;
Jerry Yuc73c6182022-02-08 20:29:25 +0800862}
863
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100864static int ssl_update_checksum_start(mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100865 const unsigned char *buf, size_t len)
Jerry Yuc73c6182022-02-08 20:29:25 +0800866{
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100867#if defined(MBEDTLS_USE_PSA_CRYPTO)
868 psa_status_t status;
869#else
870 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
871#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400872#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800873#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100874 status = psa_hash_update(&ssl->handshake->fin_sha256_psa, buf, len);
875 if (status != PSA_SUCCESS) {
876 return mbedtls_md_error_from_psa(status);
877 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800878#else
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100879 ret = mbedtls_sha256_update(&ssl->handshake->fin_sha256, buf, len);
880 if (ret != 0) {
881 return ret;
882 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800883#endif
884#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400885#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800886#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100887 status = psa_hash_update(&ssl->handshake->fin_sha384_psa, buf, len);
888 if (status != PSA_SUCCESS) {
889 return mbedtls_md_error_from_psa(status);
890 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800891#else
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100892 ret = mbedtls_sha512_update(&ssl->handshake->fin_sha384, buf, len);
893 if (ret != 0) {
894 return ret;
895 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800896#endif
897#endif
Andrzej Kurekeabeb302022-10-17 07:52:51 -0400898#if !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
899 !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
900 (void) ssl;
901 (void) buf;
902 (void) len;
903#endif
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100904 return 0;
Jerry Yuc73c6182022-02-08 20:29:25 +0800905}
906
Andrzej Kurek25f27152022-08-17 16:09:31 -0400907#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100908static int ssl_update_checksum_sha256(mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100909 const unsigned char *buf, size_t len)
Jerry Yuc73c6182022-02-08 20:29:25 +0800910{
911#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100912 return mbedtls_md_error_from_psa(psa_hash_update(
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100913 &ssl->handshake->fin_sha256_psa, buf, len));
Jerry Yuc73c6182022-02-08 20:29:25 +0800914#else
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100915 return mbedtls_sha256_update(&ssl->handshake->fin_sha256, buf, len);
Jerry Yuc73c6182022-02-08 20:29:25 +0800916#endif
917}
918#endif
919
Andrzej Kurek25f27152022-08-17 16:09:31 -0400920#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100921static int ssl_update_checksum_sha384(mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100922 const unsigned char *buf, size_t len)
Jerry Yuc73c6182022-02-08 20:29:25 +0800923{
924#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100925 return mbedtls_md_error_from_psa(psa_hash_update(
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100926 &ssl->handshake->fin_sha384_psa, buf, len));
Jerry Yuc73c6182022-02-08 20:29:25 +0800927#else
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100928 return mbedtls_sha512_update(&ssl->handshake->fin_sha384, buf, len);
Jerry Yuc73c6182022-02-08 20:29:25 +0800929#endif
930}
931#endif
932
Gilles Peskine449bd832023-01-11 14:50:10 +0100933static void ssl_handshake_params_init(mbedtls_ssl_handshake_params *handshake)
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200934{
Gilles Peskine449bd832023-01-11 14:50:10 +0100935 memset(handshake, 0, sizeof(mbedtls_ssl_handshake_params));
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200936
Andrzej Kurek25f27152022-08-17 16:09:31 -0400937#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -0500938#if defined(MBEDTLS_USE_PSA_CRYPTO)
939 handshake->fin_sha256_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -0500940#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100941 mbedtls_sha256_init(&handshake->fin_sha256);
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200942#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -0500943#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400944#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -0500945#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -0500946 handshake->fin_sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -0500947#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100948 mbedtls_sha512_init(&handshake->fin_sha384);
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200949#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -0500950#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200951
952 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +0100953
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200954#if defined(MBEDTLS_DHM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100955 mbedtls_dhm_init(&handshake->dhm_ctx);
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200956#endif
Neil Armstrongf3f46412022-04-12 14:43:39 +0200957#if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100958 mbedtls_ecdh_init(&handshake->ecdh_ctx);
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200959#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +0200960#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Neil Armstrongca7d5062022-05-31 14:43:23 +0200961#if defined(MBEDTLS_USE_PSA_CRYPTO)
962 handshake->psa_pake_ctx = psa_pake_operation_init();
963 handshake->psa_pake_password = MBEDTLS_SVC_KEY_ID_INIT;
964#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100965 mbedtls_ecjpake_init(&handshake->ecjpake_ctx);
Neil Armstrongca7d5062022-05-31 14:43:23 +0200966#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +0200967#if defined(MBEDTLS_SSL_CLI_C)
968 handshake->ecjpake_cache = NULL;
969 handshake->ecjpake_cache_len = 0;
970#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +0200971#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +0200972
Gilles Peskineeccd8882020-03-10 12:19:08 +0100973#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100974 mbedtls_x509_crt_restart_init(&handshake->ecrs_ctx);
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +0200975#endif
976
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +0200977#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
978 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
979#endif
Hanno Becker75173122019-02-06 16:18:31 +0000980
981#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
982 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100983 mbedtls_pk_init(&handshake->peer_pubkey);
Hanno Becker75173122019-02-06 16:18:31 +0000984#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200985}
986
Gilles Peskine449bd832023-01-11 14:50:10 +0100987void mbedtls_ssl_transform_init(mbedtls_ssl_transform *transform)
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200988{
Gilles Peskine449bd832023-01-11 14:50:10 +0100989 memset(transform, 0, sizeof(mbedtls_ssl_transform));
Paul Bakker84bbeb52014-07-01 14:53:22 +0200990
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +0100991#if defined(MBEDTLS_USE_PSA_CRYPTO)
992 transform->psa_key_enc = MBEDTLS_SVC_KEY_ID_INIT;
993 transform->psa_key_dec = MBEDTLS_SVC_KEY_ID_INIT;
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +0100994#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100995 mbedtls_cipher_init(&transform->cipher_ctx_enc);
996 mbedtls_cipher_init(&transform->cipher_ctx_dec);
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +0100997#endif
998
Hanno Beckerfd86ca82020-11-30 08:54:23 +0000999#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong39b8e7d2022-02-23 09:24:45 +01001000#if defined(MBEDTLS_USE_PSA_CRYPTO)
1001 transform->psa_mac_enc = MBEDTLS_SVC_KEY_ID_INIT;
1002 transform->psa_mac_dec = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrongcf8841a2022-02-24 11:17:45 +01001003#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001004 mbedtls_md_init(&transform->md_ctx_enc);
1005 mbedtls_md_init(&transform->md_ctx_dec);
Hanno Beckerd56ed242018-01-03 15:32:51 +00001006#endif
Neil Armstrongcf8841a2022-02-24 11:17:45 +01001007#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001008}
1009
Gilles Peskine449bd832023-01-11 14:50:10 +01001010void mbedtls_ssl_session_init(mbedtls_ssl_session *session)
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001011{
Gilles Peskine449bd832023-01-11 14:50:10 +01001012 memset(session, 0, sizeof(mbedtls_ssl_session));
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001013}
1014
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001015MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001016static int ssl_handshake_init(mbedtls_ssl_context *ssl)
Paul Bakker48916f92012-09-16 19:57:18 +00001017{
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001018 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1019
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001020 /* Clear old handshake information if present */
Jerry Yu2e199812022-12-01 18:57:19 +08001021#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01001022 if (ssl->transform_negotiate) {
1023 mbedtls_ssl_transform_free(ssl->transform_negotiate);
1024 }
Jerry Yu2e199812022-12-01 18:57:19 +08001025#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001026 if (ssl->session_negotiate) {
1027 mbedtls_ssl_session_free(ssl->session_negotiate);
1028 }
1029 if (ssl->handshake) {
1030 mbedtls_ssl_handshake_free(ssl);
1031 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001032
Jerry Yu2e199812022-12-01 18:57:19 +08001033#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001034 /*
1035 * Either the pointers are now NULL or cleared properly and can be freed.
1036 * Now allocate missing structures.
1037 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001038 if (ssl->transform_negotiate == NULL) {
1039 ssl->transform_negotiate = mbedtls_calloc(1, sizeof(mbedtls_ssl_transform));
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001040 }
Jerry Yu2e199812022-12-01 18:57:19 +08001041#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker48916f92012-09-16 19:57:18 +00001042
Gilles Peskine449bd832023-01-11 14:50:10 +01001043 if (ssl->session_negotiate == NULL) {
1044 ssl->session_negotiate = mbedtls_calloc(1, sizeof(mbedtls_ssl_session));
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001045 }
Paul Bakker48916f92012-09-16 19:57:18 +00001046
Gilles Peskine449bd832023-01-11 14:50:10 +01001047 if (ssl->handshake == NULL) {
1048 ssl->handshake = mbedtls_calloc(1, sizeof(mbedtls_ssl_handshake_params));
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001049 }
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05001050#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1051 /* If the buffers are too small - reallocate */
Andrzej Kurek8ea68722020-04-03 06:40:47 -04001052
Gilles Peskine449bd832023-01-11 14:50:10 +01001053 handle_buffer_resizing(ssl, 0, MBEDTLS_SSL_IN_BUFFER_LEN,
1054 MBEDTLS_SSL_OUT_BUFFER_LEN);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05001055#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001056
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001057 /* All pointers should exist and can be directly freed without issue */
Gilles Peskine449bd832023-01-11 14:50:10 +01001058 if (ssl->handshake == NULL ||
Jerry Yu2e199812022-12-01 18:57:19 +08001059#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker48916f92012-09-16 19:57:18 +00001060 ssl->transform_negotiate == NULL ||
Jerry Yu2e199812022-12-01 18:57:19 +08001061#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001062 ssl->session_negotiate == NULL) {
1063 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc() of ssl sub-contexts failed"));
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001064
Gilles Peskine449bd832023-01-11 14:50:10 +01001065 mbedtls_free(ssl->handshake);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001066 ssl->handshake = NULL;
Jerry Yu2e199812022-12-01 18:57:19 +08001067
1068#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01001069 mbedtls_free(ssl->transform_negotiate);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001070 ssl->transform_negotiate = NULL;
Jerry Yu2e199812022-12-01 18:57:19 +08001071#endif
1072
Gilles Peskine449bd832023-01-11 14:50:10 +01001073 mbedtls_free(ssl->session_negotiate);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001074 ssl->session_negotiate = NULL;
1075
Gilles Peskine449bd832023-01-11 14:50:10 +01001076 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Paul Bakker48916f92012-09-16 19:57:18 +00001077 }
1078
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001079 /* Initialize structures */
Gilles Peskine449bd832023-01-11 14:50:10 +01001080 mbedtls_ssl_session_init(ssl->session_negotiate);
1081 ssl_handshake_params_init(ssl->handshake);
Paul Bakker968afaa2014-07-09 11:09:24 +02001082
Jerry Yu2e199812022-12-01 18:57:19 +08001083#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01001084 mbedtls_ssl_transform_init(ssl->transform_negotiate);
Jerry Yu2e199812022-12-01 18:57:19 +08001085#endif
1086
Manuel Pégourié-Gonnard537f2312023-02-05 10:17:45 +01001087 /* Setup handshake checksums */
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001088 ret = mbedtls_ssl_reset_checksum(ssl);
1089 if (ret != 0) {
1090 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_reset_checksum", ret);
1091 return ret;
1092 }
Manuel Pégourié-Gonnard537f2312023-02-05 10:17:45 +01001093
Jerry Yud0766ec2022-09-22 10:46:57 +08001094#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
1095 defined(MBEDTLS_SSL_SRV_C) && \
1096 defined(MBEDTLS_SSL_SESSION_TICKETS)
1097 ssl->handshake->new_session_tickets_count =
Gilles Peskine449bd832023-01-11 14:50:10 +01001098 ssl->conf->new_session_tickets_count;
Jerry Yud0766ec2022-09-22 10:46:57 +08001099#endif
1100
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001101#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001102 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001103 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001104
Gilles Peskine449bd832023-01-11 14:50:10 +01001105 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001106 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Gilles Peskine449bd832023-01-11 14:50:10 +01001107 } else {
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001108 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Gilles Peskine449bd832023-01-11 14:50:10 +01001109 }
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001110
Gilles Peskine449bd832023-01-11 14:50:10 +01001111 mbedtls_ssl_set_timer(ssl, 0);
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001112 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001113#endif
1114
Brett Warrene0edc842021-08-17 09:53:13 +01001115/*
1116 * curve_list is translated to IANA TLS group identifiers here because
1117 * mbedtls_ssl_conf_curves returns void and so can't return
1118 * any error codes.
1119 */
1120#if defined(MBEDTLS_ECP_C)
1121#if !defined(MBEDTLS_DEPRECATED_REMOVED)
1122 /* Heap allocate and translate curve_list from internal to IANA group ids */
Gilles Peskine449bd832023-01-11 14:50:10 +01001123 if (ssl->conf->curve_list != NULL) {
Brett Warrene0edc842021-08-17 09:53:13 +01001124 size_t length;
1125 const mbedtls_ecp_group_id *curve_list = ssl->conf->curve_list;
1126
Gilles Peskine449bd832023-01-11 14:50:10 +01001127 for (length = 0; (curve_list[length] != MBEDTLS_ECP_DP_NONE) &&
1128 (length < MBEDTLS_ECP_DP_MAX); length++) {
1129 }
Brett Warrene0edc842021-08-17 09:53:13 +01001130
1131 /* Leave room for zero termination */
Gilles Peskine449bd832023-01-11 14:50:10 +01001132 uint16_t *group_list = mbedtls_calloc(length + 1, sizeof(uint16_t));
1133 if (group_list == NULL) {
1134 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
1135 }
Brett Warrene0edc842021-08-17 09:53:13 +01001136
Gilles Peskine449bd832023-01-11 14:50:10 +01001137 for (size_t i = 0; i < length; i++) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01001138 uint16_t tls_id = mbedtls_ssl_get_tls_id_from_ecp_group_id(
Gilles Peskine449bd832023-01-11 14:50:10 +01001139 curve_list[i]);
1140 if (tls_id == 0) {
1141 mbedtls_free(group_list);
1142 return MBEDTLS_ERR_SSL_BAD_CONFIG;
Brett Warrene0edc842021-08-17 09:53:13 +01001143 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01001144 group_list[i] = tls_id;
Brett Warrene0edc842021-08-17 09:53:13 +01001145 }
1146
1147 group_list[length] = 0;
1148
1149 ssl->handshake->group_list = group_list;
1150 ssl->handshake->group_list_heap_allocated = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001151 } else {
Brett Warrene0edc842021-08-17 09:53:13 +01001152 ssl->handshake->group_list = ssl->conf->group_list;
1153 ssl->handshake->group_list_heap_allocated = 0;
1154 }
1155#endif /* MBEDTLS_DEPRECATED_REMOVED */
1156#endif /* MBEDTLS_ECP_C */
1157
Ronald Crone68ab4f2022-10-05 12:46:29 +02001158#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +08001159#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Jerry Yua69269a2022-01-17 21:06:01 +08001160#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yu713013f2022-01-17 18:16:35 +08001161 /* Heap allocate and translate sig_hashes from internal hash identifiers to
1162 signature algorithms IANA identifiers. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001163 if (mbedtls_ssl_conf_is_tls12_only(ssl->conf) &&
1164 ssl->conf->sig_hashes != NULL) {
Jerry Yuf017ee42022-01-12 15:49:48 +08001165 const int *md;
1166 const int *sig_hashes = ssl->conf->sig_hashes;
Jerry Yub476a442022-01-21 18:14:45 +08001167 size_t sig_algs_len = 0;
Jerry Yuf017ee42022-01-12 15:49:48 +08001168 uint16_t *p;
1169
Jerry Yub476a442022-01-21 18:14:45 +08001170#if defined(static_assert)
Gilles Peskine449bd832023-01-11 14:50:10 +01001171 static_assert(MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN
1172 <= (SIZE_MAX - (2 * sizeof(uint16_t))),
1173 "MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN too big");
Jerry Yua68dca22022-01-20 16:28:27 +08001174#endif
1175
Gilles Peskine449bd832023-01-11 14:50:10 +01001176 for (md = sig_hashes; *md != MBEDTLS_MD_NONE; md++) {
1177 if (mbedtls_ssl_hash_from_md_alg(*md) == MBEDTLS_SSL_HASH_NONE) {
Jerry Yuf017ee42022-01-12 15:49:48 +08001178 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001179 }
Jerry Yub476a442022-01-21 18:14:45 +08001180#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001181 sig_algs_len += sizeof(uint16_t);
Jerry Yub476a442022-01-21 18:14:45 +08001182#endif
Jerry Yua68dca22022-01-20 16:28:27 +08001183
Jerry Yub476a442022-01-21 18:14:45 +08001184#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001185 sig_algs_len += sizeof(uint16_t);
Jerry Yub476a442022-01-21 18:14:45 +08001186#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001187 if (sig_algs_len > MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN) {
1188 return MBEDTLS_ERR_SSL_BAD_CONFIG;
1189 }
Jerry Yuf017ee42022-01-12 15:49:48 +08001190 }
1191
Gilles Peskine449bd832023-01-11 14:50:10 +01001192 if (sig_algs_len < MBEDTLS_SSL_MIN_SIG_ALG_LIST_LEN) {
1193 return MBEDTLS_ERR_SSL_BAD_CONFIG;
1194 }
Jerry Yuf017ee42022-01-12 15:49:48 +08001195
Gilles Peskine449bd832023-01-11 14:50:10 +01001196 ssl->handshake->sig_algs = mbedtls_calloc(1, sig_algs_len +
1197 sizeof(uint16_t));
1198 if (ssl->handshake->sig_algs == NULL) {
1199 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
1200 }
Jerry Yuf017ee42022-01-12 15:49:48 +08001201
Gilles Peskine449bd832023-01-11 14:50:10 +01001202 p = (uint16_t *) ssl->handshake->sig_algs;
1203 for (md = sig_hashes; *md != MBEDTLS_MD_NONE; md++) {
1204 unsigned char hash = mbedtls_ssl_hash_from_md_alg(*md);
1205 if (hash == MBEDTLS_SSL_HASH_NONE) {
Jerry Yuf017ee42022-01-12 15:49:48 +08001206 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001207 }
Jerry Yu6106fdc2022-01-12 16:36:14 +08001208#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001209 *p = ((hash << 8) | MBEDTLS_SSL_SIG_ECDSA);
Jerry Yuf017ee42022-01-12 15:49:48 +08001210 p++;
Jerry Yu6106fdc2022-01-12 16:36:14 +08001211#endif
1212#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001213 *p = ((hash << 8) | MBEDTLS_SSL_SIG_RSA);
Jerry Yuf017ee42022-01-12 15:49:48 +08001214 p++;
Jerry Yu6106fdc2022-01-12 16:36:14 +08001215#endif
Jerry Yuf017ee42022-01-12 15:49:48 +08001216 }
Gabor Mezei15b95a62022-05-09 16:37:58 +02001217 *p = MBEDTLS_TLS_SIG_NONE;
Jerry Yuf017ee42022-01-12 15:49:48 +08001218 ssl->handshake->sig_algs_heap_allocated = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001219 } else
Jerry Yua69269a2022-01-17 21:06:01 +08001220#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Jerry Yuf017ee42022-01-12 15:49:48 +08001221 {
Jerry Yuf017ee42022-01-12 15:49:48 +08001222 ssl->handshake->sig_algs_heap_allocated = 0;
1223 }
Jerry Yucc539102022-06-27 16:27:35 +08001224#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Ronald Crone68ab4f2022-10-05 12:46:29 +02001225#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Gilles Peskine449bd832023-01-11 14:50:10 +01001226 return 0;
Paul Bakker48916f92012-09-16 19:57:18 +00001227}
1228
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001229#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001230/* Dummy cookie callbacks for defaults */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001231MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001232static int ssl_cookie_write_dummy(void *ctx,
1233 unsigned char **p, unsigned char *end,
1234 const unsigned char *cli_id, size_t cli_id_len)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001235{
1236 ((void) ctx);
1237 ((void) p);
1238 ((void) end);
1239 ((void) cli_id);
1240 ((void) cli_id_len);
1241
Gilles Peskine449bd832023-01-11 14:50:10 +01001242 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001243}
1244
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001245MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001246static int ssl_cookie_check_dummy(void *ctx,
1247 const unsigned char *cookie, size_t cookie_len,
1248 const unsigned char *cli_id, size_t cli_id_len)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001249{
1250 ((void) ctx);
1251 ((void) cookie);
1252 ((void) cookie_len);
1253 ((void) cli_id);
1254 ((void) cli_id_len);
1255
Gilles Peskine449bd832023-01-11 14:50:10 +01001256 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001257}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001258#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001259
Paul Bakker5121ce52009-01-03 21:22:43 +00001260/*
1261 * Initialize an SSL context
1262 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001263void mbedtls_ssl_init(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001264{
Gilles Peskine449bd832023-01-11 14:50:10 +01001265 memset(ssl, 0, sizeof(mbedtls_ssl_context));
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001266}
1267
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001268MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001269static int ssl_conf_version_check(const mbedtls_ssl_context *ssl)
Jerry Yu60835a82021-08-04 10:13:52 +08001270{
Ronald Cron086ee0b2022-03-15 15:18:51 +01001271 const mbedtls_ssl_config *conf = ssl->conf;
1272
Ronald Cron6f135e12021-12-08 16:57:54 +01001273#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001274 if (mbedtls_ssl_conf_is_tls13_only(conf)) {
1275 if (conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
1276 MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS 1.3 is not yet supported."));
1277 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQianed582dd2022-04-13 08:21:05 +00001278 }
1279
Gilles Peskine449bd832023-01-11 14:50:10 +01001280 MBEDTLS_SSL_DEBUG_MSG(4, ("The SSL configuration is tls13 only."));
1281 return 0;
Jerry Yu60835a82021-08-04 10:13:52 +08001282 }
1283#endif
1284
1285#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01001286 if (mbedtls_ssl_conf_is_tls12_only(conf)) {
1287 MBEDTLS_SSL_DEBUG_MSG(4, ("The SSL configuration is tls12 only."));
1288 return 0;
Jerry Yu60835a82021-08-04 10:13:52 +08001289 }
1290#endif
1291
Ronald Cron6f135e12021-12-08 16:57:54 +01001292#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001293 if (mbedtls_ssl_conf_is_hybrid_tls12_tls13(conf)) {
1294 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
1295 MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS not yet supported in Hybrid TLS 1.3 + TLS 1.2"));
1296 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQianed582dd2022-04-13 08:21:05 +00001297 }
1298
Gilles Peskine449bd832023-01-11 14:50:10 +01001299 if (conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
1300 MBEDTLS_SSL_DEBUG_MSG(1, ("TLS 1.3 server is not supported yet."));
1301 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQian8f9dfe42022-04-15 02:52:39 +00001302 }
1303
1304
Gilles Peskine449bd832023-01-11 14:50:10 +01001305 MBEDTLS_SSL_DEBUG_MSG(4, ("The SSL configuration is TLS 1.3 or TLS 1.2."));
1306 return 0;
Jerry Yu60835a82021-08-04 10:13:52 +08001307 }
1308#endif
1309
Gilles Peskine449bd832023-01-11 14:50:10 +01001310 MBEDTLS_SSL_DEBUG_MSG(1, ("The SSL configuration is invalid."));
1311 return MBEDTLS_ERR_SSL_BAD_CONFIG;
Jerry Yu60835a82021-08-04 10:13:52 +08001312}
1313
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001314MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu60835a82021-08-04 10:13:52 +08001315static int ssl_conf_check(const mbedtls_ssl_context *ssl)
1316{
1317 int ret;
Gilles Peskine449bd832023-01-11 14:50:10 +01001318 ret = ssl_conf_version_check(ssl);
1319 if (ret != 0) {
1320 return ret;
1321 }
Jerry Yu60835a82021-08-04 10:13:52 +08001322
Jerry Yudef7ae42022-10-30 14:13:19 +08001323#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1324 /* RFC 8446 section 4.4.3
1325 *
1326 * If the verification fails, the receiver MUST terminate the handshake with
1327 * a "decrypt_error" alert.
1328 *
1329 * If the client is configured as TLS 1.3 only with optional verify, return
1330 * bad config.
1331 *
1332 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001333 if (mbedtls_ssl_conf_tls13_ephemeral_enabled(
1334 (mbedtls_ssl_context *) ssl) &&
Jerry Yudef7ae42022-10-30 14:13:19 +08001335 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
1336 ssl->conf->max_tls_version == MBEDTLS_SSL_VERSION_TLS1_3 &&
1337 ssl->conf->min_tls_version == MBEDTLS_SSL_VERSION_TLS1_3 &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001338 ssl->conf->authmode == MBEDTLS_SSL_VERIFY_OPTIONAL) {
Jerry Yudef7ae42022-10-30 14:13:19 +08001339 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +01001340 1, ("Optional verify auth mode "
1341 "is not available for TLS 1.3 client"));
1342 return MBEDTLS_ERR_SSL_BAD_CONFIG;
Jerry Yudef7ae42022-10-30 14:13:19 +08001343 }
1344#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
1345
Jerry Yu60835a82021-08-04 10:13:52 +08001346 /* Space for further checks */
1347
Gilles Peskine449bd832023-01-11 14:50:10 +01001348 return 0;
Jerry Yu60835a82021-08-04 10:13:52 +08001349}
1350
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001351/*
1352 * Setup an SSL context
1353 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01001354
Gilles Peskine449bd832023-01-11 14:50:10 +01001355int mbedtls_ssl_setup(mbedtls_ssl_context *ssl,
1356 const mbedtls_ssl_config *conf)
Paul Bakker5121ce52009-01-03 21:22:43 +00001357{
Janos Follath865b3eb2019-12-16 11:46:15 +00001358 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Darryl Greenb33cc762019-11-28 14:29:44 +00001359 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1360 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00001361
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001362 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00001363
Gilles Peskine449bd832023-01-11 14:50:10 +01001364 if ((ret = ssl_conf_check(ssl)) != 0) {
1365 return ret;
1366 }
Jerry Yu60835a82021-08-04 10:13:52 +08001367
Paul Bakker62f2dee2012-09-28 07:31:51 +00001368 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001369 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00001370 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02001371
1372 /* Set to NULL in case of an error condition */
1373 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02001374
Darryl Greenb33cc762019-11-28 14:29:44 +00001375#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1376 ssl->in_buf_len = in_buf_len;
1377#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001378 ssl->in_buf = mbedtls_calloc(1, in_buf_len);
1379 if (ssl->in_buf == NULL) {
1380 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", in_buf_len));
k-stachowiak9f7798e2018-07-31 16:52:32 +02001381 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02001382 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10001383 }
1384
Darryl Greenb33cc762019-11-28 14:29:44 +00001385#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1386 ssl->out_buf_len = out_buf_len;
1387#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001388 ssl->out_buf = mbedtls_calloc(1, out_buf_len);
1389 if (ssl->out_buf == NULL) {
1390 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", out_buf_len));
k-stachowiak9f7798e2018-07-31 16:52:32 +02001391 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02001392 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00001393 }
1394
Gilles Peskine449bd832023-01-11 14:50:10 +01001395 mbedtls_ssl_reset_in_out_pointers(ssl);
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02001396
Johan Pascalb62bb512015-12-03 21:56:45 +01001397#if defined(MBEDTLS_SSL_DTLS_SRTP)
Gilles Peskine449bd832023-01-11 14:50:10 +01001398 memset(&ssl->dtls_srtp_info, 0, sizeof(ssl->dtls_srtp_info));
Johan Pascalb62bb512015-12-03 21:56:45 +01001399#endif
1400
Gilles Peskine449bd832023-01-11 14:50:10 +01001401 if ((ret = ssl_handshake_init(ssl)) != 0) {
k-stachowiaka47911c2018-07-04 17:41:58 +02001402 goto error;
Gilles Peskine449bd832023-01-11 14:50:10 +01001403 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001404
Gilles Peskine449bd832023-01-11 14:50:10 +01001405 return 0;
k-stachowiaka47911c2018-07-04 17:41:58 +02001406
1407error:
Gilles Peskine449bd832023-01-11 14:50:10 +01001408 mbedtls_free(ssl->in_buf);
1409 mbedtls_free(ssl->out_buf);
k-stachowiaka47911c2018-07-04 17:41:58 +02001410
1411 ssl->conf = NULL;
1412
Darryl Greenb33cc762019-11-28 14:29:44 +00001413#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1414 ssl->in_buf_len = 0;
1415 ssl->out_buf_len = 0;
1416#endif
k-stachowiaka47911c2018-07-04 17:41:58 +02001417 ssl->in_buf = NULL;
1418 ssl->out_buf = NULL;
1419
1420 ssl->in_hdr = NULL;
1421 ssl->in_ctr = NULL;
1422 ssl->in_len = NULL;
1423 ssl->in_iv = NULL;
1424 ssl->in_msg = NULL;
1425
1426 ssl->out_hdr = NULL;
1427 ssl->out_ctr = NULL;
1428 ssl->out_len = NULL;
1429 ssl->out_iv = NULL;
1430 ssl->out_msg = NULL;
1431
Gilles Peskine449bd832023-01-11 14:50:10 +01001432 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001433}
1434
1435/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00001436 * Reset an initialized and used SSL context for re-use while retaining
1437 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001438 *
1439 * If partial is non-zero, keep data in the input buffer and client ID.
1440 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00001441 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001442void mbedtls_ssl_session_reset_msg_layer(mbedtls_ssl_context *ssl,
1443 int partial)
Paul Bakker7eb013f2011-10-06 12:37:39 +00001444{
Darryl Greenb33cc762019-11-28 14:29:44 +00001445#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1446 size_t in_buf_len = ssl->in_buf_len;
1447 size_t out_buf_len = ssl->out_buf_len;
1448#else
1449 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1450 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
1451#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001452
Hanno Beckerb0302c42021-08-03 09:39:42 +01001453#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || !defined(MBEDTLS_SSL_SRV_C)
1454 partial = 0;
Hanno Becker7e772132018-08-10 12:38:21 +01001455#endif
1456
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001457 /* Cancel any possibly running timer */
Gilles Peskine449bd832023-01-11 14:50:10 +01001458 mbedtls_ssl_set_timer(ssl, 0);
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001459
Gilles Peskine449bd832023-01-11 14:50:10 +01001460 mbedtls_ssl_reset_in_out_pointers(ssl);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001461
1462 /* Reset incoming message parsing */
1463 ssl->in_offt = NULL;
1464 ssl->nb_zero = 0;
1465 ssl->in_msgtype = 0;
1466 ssl->in_msglen = 0;
1467 ssl->in_hslen = 0;
1468 ssl->keep_current_message = 0;
1469 ssl->transform_in = NULL;
1470
1471#if defined(MBEDTLS_SSL_PROTO_DTLS)
1472 ssl->next_record_offset = 0;
1473 ssl->in_epoch = 0;
1474#endif
1475
1476 /* Keep current datagram if partial == 1 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001477 if (partial == 0) {
Hanno Beckerb0302c42021-08-03 09:39:42 +01001478 ssl->in_left = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001479 memset(ssl->in_buf, 0, in_buf_len);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001480 }
1481
Ronald Cronad8c17b2022-06-10 17:18:09 +02001482 ssl->send_alert = 0;
1483
Hanno Beckerb0302c42021-08-03 09:39:42 +01001484 /* Reset outgoing message writing */
1485 ssl->out_msgtype = 0;
1486 ssl->out_msglen = 0;
1487 ssl->out_left = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001488 memset(ssl->out_buf, 0, out_buf_len);
1489 memset(ssl->cur_out_ctr, 0, sizeof(ssl->cur_out_ctr));
Hanno Beckerb0302c42021-08-03 09:39:42 +01001490 ssl->transform_out = NULL;
1491
1492#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine449bd832023-01-11 14:50:10 +01001493 mbedtls_ssl_dtls_replay_reset(ssl);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001494#endif
1495
XiaokangQian2b01dc32022-01-21 02:53:13 +00001496#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01001497 if (ssl->transform) {
1498 mbedtls_ssl_transform_free(ssl->transform);
1499 mbedtls_free(ssl->transform);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001500 ssl->transform = NULL;
1501 }
XiaokangQian2b01dc32022-01-21 02:53:13 +00001502#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1503
XiaokangQian2b01dc32022-01-21 02:53:13 +00001504#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001505 mbedtls_ssl_transform_free(ssl->transform_application);
1506 mbedtls_free(ssl->transform_application);
XiaokangQian2b01dc32022-01-21 02:53:13 +00001507 ssl->transform_application = NULL;
1508
Gilles Peskine449bd832023-01-11 14:50:10 +01001509 if (ssl->handshake != NULL) {
Jerry Yu3d9b5902022-11-04 14:07:25 +08001510#if defined(MBEDTLS_SSL_EARLY_DATA)
Gilles Peskine449bd832023-01-11 14:50:10 +01001511 mbedtls_ssl_transform_free(ssl->handshake->transform_earlydata);
1512 mbedtls_free(ssl->handshake->transform_earlydata);
XiaokangQian2b01dc32022-01-21 02:53:13 +00001513 ssl->handshake->transform_earlydata = NULL;
Jerry Yu3d9b5902022-11-04 14:07:25 +08001514#endif
XiaokangQian2b01dc32022-01-21 02:53:13 +00001515
Gilles Peskine449bd832023-01-11 14:50:10 +01001516 mbedtls_ssl_transform_free(ssl->handshake->transform_handshake);
1517 mbedtls_free(ssl->handshake->transform_handshake);
XiaokangQian2b01dc32022-01-21 02:53:13 +00001518 ssl->handshake->transform_handshake = NULL;
1519 }
1520
XiaokangQian2b01dc32022-01-21 02:53:13 +00001521#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerb0302c42021-08-03 09:39:42 +01001522}
1523
Gilles Peskine449bd832023-01-11 14:50:10 +01001524int mbedtls_ssl_session_reset_int(mbedtls_ssl_context *ssl, int partial)
Hanno Beckerb0302c42021-08-03 09:39:42 +01001525{
1526 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1527
1528 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
1529
Gilles Peskine449bd832023-01-11 14:50:10 +01001530 mbedtls_ssl_session_reset_msg_layer(ssl, partial);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001531
1532 /* Reset renegotiation state */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001533#if defined(MBEDTLS_SSL_RENEGOTIATION)
1534 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001535 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00001536
1537 ssl->verify_data_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001538 memset(ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN);
1539 memset(ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN);
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001540#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001541 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00001542
Hanno Beckerb0302c42021-08-03 09:39:42 +01001543 ssl->session_in = NULL;
Hanno Becker78640902018-08-13 16:35:15 +01001544 ssl->session_out = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001545 if (ssl->session) {
1546 mbedtls_ssl_session_free(ssl->session);
1547 mbedtls_free(ssl->session);
Paul Bakkerc0463502013-02-14 11:19:38 +01001548 ssl->session = NULL;
1549 }
1550
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001551#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02001552 ssl->alpn_chosen = NULL;
1553#endif
1554
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001555#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
David Horstmann3b2276a2022-10-06 14:49:08 +01001556 int free_cli_id = 1;
Hanno Becker4ccbf062018-08-10 11:20:38 +01001557#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Gilles Peskine449bd832023-01-11 14:50:10 +01001558 free_cli_id = (partial == 0);
Hanno Becker4ccbf062018-08-10 11:20:38 +01001559#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001560 if (free_cli_id) {
1561 mbedtls_free(ssl->cli_id);
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001562 ssl->cli_id = NULL;
1563 ssl->cli_id_len = 0;
1564 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02001565#endif
1566
Gilles Peskine449bd832023-01-11 14:50:10 +01001567 if ((ret = ssl_handshake_init(ssl)) != 0) {
1568 return ret;
1569 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00001570
Gilles Peskine449bd832023-01-11 14:50:10 +01001571 return 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00001572}
1573
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02001574/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001575 * Reset an initialized and used SSL context for re-use while retaining
1576 * all application-set variables, function pointers and data.
1577 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001578int mbedtls_ssl_session_reset(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001579{
Gilles Peskine449bd832023-01-11 14:50:10 +01001580 return mbedtls_ssl_session_reset_int(ssl, 0);
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001581}
1582
1583/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001584 * SSL set accessors
1585 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001586void mbedtls_ssl_conf_endpoint(mbedtls_ssl_config *conf, int endpoint)
Paul Bakker5121ce52009-01-03 21:22:43 +00001587{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001588 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00001589}
1590
Gilles Peskine449bd832023-01-11 14:50:10 +01001591void mbedtls_ssl_conf_transport(mbedtls_ssl_config *conf, int transport)
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001592{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001593 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001594}
1595
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001596#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine449bd832023-01-11 14:50:10 +01001597void mbedtls_ssl_conf_dtls_anti_replay(mbedtls_ssl_config *conf, char mode)
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001598{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001599 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001600}
1601#endif
1602
Gilles Peskine449bd832023-01-11 14:50:10 +01001603void mbedtls_ssl_conf_dtls_badmac_limit(mbedtls_ssl_config *conf, unsigned limit)
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001604{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001605 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001606}
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001607
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001608#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01001609
Gilles Peskine449bd832023-01-11 14:50:10 +01001610void mbedtls_ssl_set_datagram_packing(mbedtls_ssl_context *ssl,
1611 unsigned allow_packing)
Hanno Becker04da1892018-08-14 13:22:10 +01001612{
1613 ssl->disable_datagram_packing = !allow_packing;
1614}
1615
Gilles Peskine449bd832023-01-11 14:50:10 +01001616void mbedtls_ssl_conf_handshake_timeout(mbedtls_ssl_config *conf,
1617 uint32_t min, uint32_t max)
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001618{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001619 conf->hs_timeout_min = min;
1620 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001621}
1622#endif
1623
Gilles Peskine449bd832023-01-11 14:50:10 +01001624void mbedtls_ssl_conf_authmode(mbedtls_ssl_config *conf, int authmode)
Paul Bakker5121ce52009-01-03 21:22:43 +00001625{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001626 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00001627}
1628
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001629#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001630void mbedtls_ssl_conf_verify(mbedtls_ssl_config *conf,
1631 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
1632 void *p_vrfy)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001633{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001634 conf->f_vrfy = f_vrfy;
1635 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001636}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001637#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001638
Gilles Peskine449bd832023-01-11 14:50:10 +01001639void mbedtls_ssl_conf_rng(mbedtls_ssl_config *conf,
1640 int (*f_rng)(void *, unsigned char *, size_t),
1641 void *p_rng)
Paul Bakker5121ce52009-01-03 21:22:43 +00001642{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001643 conf->f_rng = f_rng;
1644 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00001645}
1646
Gilles Peskine449bd832023-01-11 14:50:10 +01001647void mbedtls_ssl_conf_dbg(mbedtls_ssl_config *conf,
1648 void (*f_dbg)(void *, int, const char *, int, const char *),
1649 void *p_dbg)
Paul Bakker5121ce52009-01-03 21:22:43 +00001650{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001651 conf->f_dbg = f_dbg;
1652 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00001653}
1654
Gilles Peskine449bd832023-01-11 14:50:10 +01001655void mbedtls_ssl_set_bio(mbedtls_ssl_context *ssl,
1656 void *p_bio,
1657 mbedtls_ssl_send_t *f_send,
1658 mbedtls_ssl_recv_t *f_recv,
1659 mbedtls_ssl_recv_timeout_t *f_recv_timeout)
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001660{
1661 ssl->p_bio = p_bio;
1662 ssl->f_send = f_send;
1663 ssl->f_recv = f_recv;
1664 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001665}
1666
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02001667#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001668void mbedtls_ssl_set_mtu(mbedtls_ssl_context *ssl, uint16_t mtu)
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02001669{
1670 ssl->mtu = mtu;
1671}
1672#endif
1673
Gilles Peskine449bd832023-01-11 14:50:10 +01001674void mbedtls_ssl_conf_read_timeout(mbedtls_ssl_config *conf, uint32_t timeout)
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001675{
1676 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001677}
1678
Gilles Peskine449bd832023-01-11 14:50:10 +01001679void mbedtls_ssl_set_timer_cb(mbedtls_ssl_context *ssl,
1680 void *p_timer,
1681 mbedtls_ssl_set_timer_t *f_set_timer,
1682 mbedtls_ssl_get_timer_t *f_get_timer)
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001683{
1684 ssl->p_timer = p_timer;
1685 ssl->f_set_timer = f_set_timer;
1686 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001687
1688 /* Make sure we start with no timer running */
Gilles Peskine449bd832023-01-11 14:50:10 +01001689 mbedtls_ssl_set_timer(ssl, 0);
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001690}
1691
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001692#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001693void mbedtls_ssl_conf_session_cache(mbedtls_ssl_config *conf,
1694 void *p_cache,
1695 mbedtls_ssl_cache_get_t *f_get_cache,
1696 mbedtls_ssl_cache_set_t *f_set_cache)
Paul Bakker5121ce52009-01-03 21:22:43 +00001697{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01001698 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001699 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001700 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00001701}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001702#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001703
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001704#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001705int mbedtls_ssl_set_session(mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session)
Paul Bakker5121ce52009-01-03 21:22:43 +00001706{
Janos Follath865b3eb2019-12-16 11:46:15 +00001707 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001708
Gilles Peskine449bd832023-01-11 14:50:10 +01001709 if (ssl == NULL ||
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001710 session == NULL ||
1711 ssl->session_negotiate == NULL ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001712 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT) {
1713 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001714 }
1715
Gilles Peskine449bd832023-01-11 14:50:10 +01001716 if (ssl->handshake->resume == 1) {
1717 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
1718 }
Hanno Beckere810bbc2021-05-14 16:01:05 +01001719
Jerry Yu21092062022-10-10 21:21:31 +08001720#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001721 if (session->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
Jerry Yu21092062022-10-10 21:21:31 +08001722 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01001723 mbedtls_ssl_ciphersuite_from_id(session->ciphersuite);
Jerry Yu21092062022-10-10 21:21:31 +08001724
Gilles Peskine449bd832023-01-11 14:50:10 +01001725 if (mbedtls_ssl_validate_ciphersuite(
Jerry Yu21092062022-10-10 21:21:31 +08001726 ssl, ciphersuite_info, MBEDTLS_SSL_VERSION_TLS1_3,
Gilles Peskine449bd832023-01-11 14:50:10 +01001727 MBEDTLS_SSL_VERSION_TLS1_3) != 0) {
1728 MBEDTLS_SSL_DEBUG_MSG(4, ("%d is not a valid TLS 1.3 ciphersuite.",
1729 session->ciphersuite));
1730 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jerry Yu21092062022-10-10 21:21:31 +08001731 }
Jerry Yu40afab62022-10-08 10:42:13 +08001732 }
Jerry Yu21092062022-10-10 21:21:31 +08001733#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu40afab62022-10-08 10:42:13 +08001734
Gilles Peskine449bd832023-01-11 14:50:10 +01001735 if ((ret = mbedtls_ssl_session_copy(ssl->session_negotiate,
1736 session)) != 0) {
1737 return ret;
1738 }
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001739
Paul Bakker0a597072012-09-25 21:55:46 +00001740 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001741
Gilles Peskine449bd832023-01-11 14:50:10 +01001742 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001743}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001744#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001745
Gilles Peskine449bd832023-01-11 14:50:10 +01001746void mbedtls_ssl_conf_ciphersuites(mbedtls_ssl_config *conf,
1747 const int *ciphersuites)
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001748{
Hanno Beckerd60b6c62021-04-29 12:04:11 +01001749 conf->ciphersuite_list = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00001750}
1751
Ronald Cron6f135e12021-12-08 16:57:54 +01001752#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001753void mbedtls_ssl_conf_tls13_key_exchange_modes(mbedtls_ssl_config *conf,
1754 const int kex_modes)
Hanno Becker71f1ed62021-07-24 06:01:47 +01001755{
Xiaofei Bai746f9482021-11-12 08:53:56 +00001756 conf->tls13_kex_modes = kex_modes & MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Hanno Becker71f1ed62021-07-24 06:01:47 +01001757}
Xiaokang Qian72de95d2022-10-25 02:54:33 +00001758
1759#if defined(MBEDTLS_SSL_EARLY_DATA)
Gilles Peskine449bd832023-01-11 14:50:10 +01001760void mbedtls_ssl_tls13_conf_early_data(mbedtls_ssl_config *conf,
1761 int early_data_enabled)
Xiaokang Qian72de95d2022-10-25 02:54:33 +00001762{
1763 conf->early_data_enabled = early_data_enabled;
1764}
Jerry Yucc4e0072022-11-22 17:22:22 +08001765
1766#if defined(MBEDTLS_SSL_SRV_C)
1767void mbedtls_ssl_tls13_conf_max_early_data_size(
Gilles Peskine449bd832023-01-11 14:50:10 +01001768 mbedtls_ssl_config *conf, uint32_t max_early_data_size)
Jerry Yucc4e0072022-11-22 17:22:22 +08001769{
Jerry Yu39da9852022-12-06 16:58:36 +08001770 conf->max_early_data_size = max_early_data_size;
Jerry Yucc4e0072022-11-22 17:22:22 +08001771}
1772#endif /* MBEDTLS_SSL_SRV_C */
1773
Xiaokang Qian72de95d2022-10-25 02:54:33 +00001774#endif /* MBEDTLS_SSL_EARLY_DATA */
Ronald Cron6f135e12021-12-08 16:57:54 +01001775#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01001776
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001777#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001778void mbedtls_ssl_conf_cert_profile(mbedtls_ssl_config *conf,
1779 const mbedtls_x509_crt_profile *profile)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02001780{
1781 conf->cert_profile = profile;
1782}
1783
Gilles Peskine449bd832023-01-11 14:50:10 +01001784static void ssl_key_cert_free(mbedtls_ssl_key_cert *key_cert)
Glenn Strauss36872db2022-01-22 05:06:31 -05001785{
1786 mbedtls_ssl_key_cert *cur = key_cert, *next;
1787
Gilles Peskine449bd832023-01-11 14:50:10 +01001788 while (cur != NULL) {
Glenn Strauss36872db2022-01-22 05:06:31 -05001789 next = cur->next;
Gilles Peskine449bd832023-01-11 14:50:10 +01001790 mbedtls_free(cur);
Glenn Strauss36872db2022-01-22 05:06:31 -05001791 cur = next;
1792 }
1793}
1794
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001795/* Append a new keycert entry to a (possibly empty) list */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001796MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001797static int ssl_append_key_cert(mbedtls_ssl_key_cert **head,
1798 mbedtls_x509_crt *cert,
1799 mbedtls_pk_context *key)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001800{
niisato8ee24222018-06-25 19:05:48 +09001801 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001802
Gilles Peskine449bd832023-01-11 14:50:10 +01001803 if (cert == NULL) {
Glenn Strauss36872db2022-01-22 05:06:31 -05001804 /* Free list if cert is null */
Gilles Peskine449bd832023-01-11 14:50:10 +01001805 ssl_key_cert_free(*head);
Glenn Strauss36872db2022-01-22 05:06:31 -05001806 *head = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001807 return 0;
Glenn Strauss36872db2022-01-22 05:06:31 -05001808 }
1809
Gilles Peskine449bd832023-01-11 14:50:10 +01001810 new_cert = mbedtls_calloc(1, sizeof(mbedtls_ssl_key_cert));
1811 if (new_cert == NULL) {
1812 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
1813 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001814
niisato8ee24222018-06-25 19:05:48 +09001815 new_cert->cert = cert;
1816 new_cert->key = key;
1817 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001818
Glenn Strauss36872db2022-01-22 05:06:31 -05001819 /* Update head if the list was null, else add to the end */
Gilles Peskine449bd832023-01-11 14:50:10 +01001820 if (*head == NULL) {
niisato8ee24222018-06-25 19:05:48 +09001821 *head = new_cert;
Gilles Peskine449bd832023-01-11 14:50:10 +01001822 } else {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001823 mbedtls_ssl_key_cert *cur = *head;
Gilles Peskine449bd832023-01-11 14:50:10 +01001824 while (cur->next != NULL) {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001825 cur = cur->next;
Gilles Peskine449bd832023-01-11 14:50:10 +01001826 }
niisato8ee24222018-06-25 19:05:48 +09001827 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001828 }
1829
Gilles Peskine449bd832023-01-11 14:50:10 +01001830 return 0;
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001831}
1832
Gilles Peskine449bd832023-01-11 14:50:10 +01001833int mbedtls_ssl_conf_own_cert(mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001834 mbedtls_x509_crt *own_cert,
Gilles Peskine449bd832023-01-11 14:50:10 +01001835 mbedtls_pk_context *pk_key)
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001836{
Gilles Peskine449bd832023-01-11 14:50:10 +01001837 return ssl_append_key_cert(&conf->key_cert, own_cert, pk_key);
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001838}
1839
Gilles Peskine449bd832023-01-11 14:50:10 +01001840void mbedtls_ssl_conf_ca_chain(mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001841 mbedtls_x509_crt *ca_chain,
Gilles Peskine449bd832023-01-11 14:50:10 +01001842 mbedtls_x509_crl *ca_crl)
Paul Bakker5121ce52009-01-03 21:22:43 +00001843{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001844 conf->ca_chain = ca_chain;
1845 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00001846
1847#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1848 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1849 * cannot be used together. */
1850 conf->f_ca_cb = NULL;
1851 conf->p_ca_cb = NULL;
1852#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00001853}
Hanno Becker5adaad92019-03-27 16:54:37 +00001854
1855#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Gilles Peskine449bd832023-01-11 14:50:10 +01001856void mbedtls_ssl_conf_ca_cb(mbedtls_ssl_config *conf,
1857 mbedtls_x509_crt_ca_cb_t f_ca_cb,
1858 void *p_ca_cb)
Hanno Becker5adaad92019-03-27 16:54:37 +00001859{
1860 conf->f_ca_cb = f_ca_cb;
1861 conf->p_ca_cb = p_ca_cb;
1862
1863 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1864 * cannot be used together. */
1865 conf->ca_chain = NULL;
1866 conf->ca_crl = NULL;
1867}
1868#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001869#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00001870
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001871#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01001872const unsigned char *mbedtls_ssl_get_hs_sni(mbedtls_ssl_context *ssl,
1873 size_t *name_len)
Glenn Strauss69894072022-01-24 12:58:00 -05001874{
1875 *name_len = ssl->handshake->sni_name_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001876 return ssl->handshake->sni_name;
Glenn Strauss69894072022-01-24 12:58:00 -05001877}
1878
Gilles Peskine449bd832023-01-11 14:50:10 +01001879int mbedtls_ssl_set_hs_own_cert(mbedtls_ssl_context *ssl,
1880 mbedtls_x509_crt *own_cert,
1881 mbedtls_pk_context *pk_key)
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001882{
Gilles Peskine449bd832023-01-11 14:50:10 +01001883 return ssl_append_key_cert(&ssl->handshake->sni_key_cert,
1884 own_cert, pk_key);
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001885}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02001886
Gilles Peskine449bd832023-01-11 14:50:10 +01001887void mbedtls_ssl_set_hs_ca_chain(mbedtls_ssl_context *ssl,
1888 mbedtls_x509_crt *ca_chain,
1889 mbedtls_x509_crl *ca_crl)
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02001890{
1891 ssl->handshake->sni_ca_chain = ca_chain;
1892 ssl->handshake->sni_ca_crl = ca_crl;
1893}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001894
Glenn Strauss999ef702022-03-11 01:37:23 -05001895#if defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001896void mbedtls_ssl_set_hs_dn_hints(mbedtls_ssl_context *ssl,
1897 const mbedtls_x509_crt *crt)
Glenn Strauss999ef702022-03-11 01:37:23 -05001898{
1899 ssl->handshake->dn_hints = crt;
1900}
1901#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
1902
Gilles Peskine449bd832023-01-11 14:50:10 +01001903void mbedtls_ssl_set_hs_authmode(mbedtls_ssl_context *ssl,
1904 int authmode)
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001905{
1906 ssl->handshake->sni_authmode = authmode;
1907}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001908#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
1909
Hanno Becker8927c832019-04-03 12:52:50 +01001910#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001911void mbedtls_ssl_set_verify(mbedtls_ssl_context *ssl,
1912 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
1913 void *p_vrfy)
Hanno Becker8927c832019-04-03 12:52:50 +01001914{
1915 ssl->f_vrfy = f_vrfy;
1916 ssl->p_vrfy = p_vrfy;
1917}
1918#endif
1919
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02001920#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001921
Valerio Setti016f6822022-12-09 14:17:50 +01001922#if defined(MBEDTLS_USE_PSA_CRYPTO)
1923static psa_status_t mbedtls_ssl_set_hs_ecjpake_password_common(
Gilles Peskine449bd832023-01-11 14:50:10 +01001924 mbedtls_ssl_context *ssl,
1925 mbedtls_svc_key_id_t pwd)
Valerio Setti016f6822022-12-09 14:17:50 +01001926{
1927 psa_status_t status;
1928 psa_pake_role_t psa_role;
1929 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
1930
Gilles Peskine449bd832023-01-11 14:50:10 +01001931 psa_pake_cs_set_algorithm(&cipher_suite, PSA_ALG_JPAKE);
1932 psa_pake_cs_set_primitive(&cipher_suite,
1933 PSA_PAKE_PRIMITIVE(PSA_PAKE_PRIMITIVE_TYPE_ECC,
1934 PSA_ECC_FAMILY_SECP_R1,
1935 256));
1936 psa_pake_cs_set_hash(&cipher_suite, PSA_ALG_SHA_256);
Valerio Setti016f6822022-12-09 14:17:50 +01001937
Gilles Peskine449bd832023-01-11 14:50:10 +01001938 status = psa_pake_setup(&ssl->handshake->psa_pake_ctx, &cipher_suite);
1939 if (status != PSA_SUCCESS) {
Valerio Setti016f6822022-12-09 14:17:50 +01001940 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001941 }
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001942
Gilles Peskine449bd832023-01-11 14:50:10 +01001943 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Neil Armstrongca7d5062022-05-31 14:43:23 +02001944 psa_role = PSA_PAKE_ROLE_SERVER;
Gilles Peskine449bd832023-01-11 14:50:10 +01001945 } else {
Neil Armstrongca7d5062022-05-31 14:43:23 +02001946 psa_role = PSA_PAKE_ROLE_CLIENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001947 }
Neil Armstrongca7d5062022-05-31 14:43:23 +02001948
Gilles Peskine449bd832023-01-11 14:50:10 +01001949 status = psa_pake_set_role(&ssl->handshake->psa_pake_ctx, psa_role);
1950 if (status != PSA_SUCCESS) {
Valerio Setti016f6822022-12-09 14:17:50 +01001951 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001952 }
Valerio Setti016f6822022-12-09 14:17:50 +01001953
Gilles Peskine449bd832023-01-11 14:50:10 +01001954 status = psa_pake_set_password_key(&ssl->handshake->psa_pake_ctx, pwd);
1955 if (status != PSA_SUCCESS) {
Valerio Setti016f6822022-12-09 14:17:50 +01001956 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001957 }
Valerio Setti016f6822022-12-09 14:17:50 +01001958
1959 ssl->handshake->psa_pake_ctx_is_ok = 1;
1960
Gilles Peskine449bd832023-01-11 14:50:10 +01001961 return PSA_SUCCESS;
Valerio Setti016f6822022-12-09 14:17:50 +01001962}
1963
Gilles Peskine449bd832023-01-11 14:50:10 +01001964int mbedtls_ssl_set_hs_ecjpake_password(mbedtls_ssl_context *ssl,
1965 const unsigned char *pw,
1966 size_t pw_len)
Valerio Setti016f6822022-12-09 14:17:50 +01001967{
1968 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1969 psa_status_t status;
1970
Gilles Peskine449bd832023-01-11 14:50:10 +01001971 if (ssl->handshake == NULL || ssl->conf == NULL) {
1972 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Neil Armstrongca7d5062022-05-31 14:43:23 +02001973 }
1974
Gilles Peskine449bd832023-01-11 14:50:10 +01001975 /* Empty password is not valid */
1976 if ((pw == NULL) || (pw_len == 0)) {
1977 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
1978 }
1979
1980 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
1981 psa_set_key_algorithm(&attributes, PSA_ALG_JPAKE);
1982 psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD);
1983
1984 status = psa_import_key(&attributes, pw, pw_len,
1985 &ssl->handshake->psa_pake_password);
1986 if (status != PSA_SUCCESS) {
1987 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
1988 }
1989
1990 status = mbedtls_ssl_set_hs_ecjpake_password_common(ssl,
1991 ssl->handshake->psa_pake_password);
1992 if (status != PSA_SUCCESS) {
1993 psa_destroy_key(ssl->handshake->psa_pake_password);
1994 psa_pake_abort(&ssl->handshake->psa_pake_ctx);
1995 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
1996 }
1997
1998 return 0;
Valerio Setti02c25b52022-11-15 14:08:42 +01001999}
Valerio Settia9a97dc2022-11-28 18:26:16 +01002000
Gilles Peskine449bd832023-01-11 14:50:10 +01002001int mbedtls_ssl_set_hs_ecjpake_password_opaque(mbedtls_ssl_context *ssl,
2002 mbedtls_svc_key_id_t pwd)
Valerio Settia9a97dc2022-11-28 18:26:16 +01002003{
Valerio Settia9a97dc2022-11-28 18:26:16 +01002004 psa_status_t status;
2005
Gilles Peskine449bd832023-01-11 14:50:10 +01002006 if (ssl->handshake == NULL || ssl->conf == NULL) {
2007 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Neil Armstrongca7d5062022-05-31 14:43:23 +02002008 }
2009
Gilles Peskine449bd832023-01-11 14:50:10 +01002010 if (mbedtls_svc_key_id_is_null(pwd)) {
2011 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2012 }
2013
2014 status = mbedtls_ssl_set_hs_ecjpake_password_common(ssl, pwd);
2015 if (status != PSA_SUCCESS) {
2016 psa_pake_abort(&ssl->handshake->psa_pake_ctx);
2017 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2018 }
2019
2020 return 0;
Valerio Setti02c25b52022-11-15 14:08:42 +01002021}
2022#else /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01002023int mbedtls_ssl_set_hs_ecjpake_password(mbedtls_ssl_context *ssl,
2024 const unsigned char *pw,
2025 size_t pw_len)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002026{
2027 mbedtls_ecjpake_role role;
2028
Gilles Peskine449bd832023-01-11 14:50:10 +01002029 if (ssl->handshake == NULL || ssl->conf == NULL) {
2030 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2031 }
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002032
Valerio Settic689ed82022-12-07 14:40:38 +01002033 /* Empty password is not valid */
Gilles Peskine449bd832023-01-11 14:50:10 +01002034 if ((pw == NULL) || (pw_len == 0)) {
2035 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2036 }
Valerio Settic689ed82022-12-07 14:40:38 +01002037
Gilles Peskine449bd832023-01-11 14:50:10 +01002038 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002039 role = MBEDTLS_ECJPAKE_SERVER;
Gilles Peskine449bd832023-01-11 14:50:10 +01002040 } else {
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002041 role = MBEDTLS_ECJPAKE_CLIENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01002042 }
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002043
Gilles Peskine449bd832023-01-11 14:50:10 +01002044 return mbedtls_ecjpake_setup(&ssl->handshake->ecjpake_ctx,
2045 role,
2046 MBEDTLS_MD_SHA256,
2047 MBEDTLS_ECP_DP_SECP256R1,
2048 pw, pw_len);
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002049}
Valerio Setti02c25b52022-11-15 14:08:42 +01002050#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02002051#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002052
Ronald Cron73fe8df2022-10-05 14:31:43 +02002053#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002054int mbedtls_ssl_conf_has_static_psk(mbedtls_ssl_config const *conf)
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002055{
Gilles Peskine449bd832023-01-11 14:50:10 +01002056 if (conf->psk_identity == NULL ||
2057 conf->psk_identity_len == 0) {
2058 return 0;
Ronald Crond29e13e2022-10-19 10:33:48 +02002059 }
2060
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002061#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002062 if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) {
2063 return 1;
2064 }
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002065#endif /* MBEDTLS_USE_PSA_CRYPTO */
Ronald Crond29e13e2022-10-19 10:33:48 +02002066
Gilles Peskine449bd832023-01-11 14:50:10 +01002067 if (conf->psk != NULL && conf->psk_len != 0) {
2068 return 1;
2069 }
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002070
Gilles Peskine449bd832023-01-11 14:50:10 +01002071 return 0;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002072}
2073
Gilles Peskine449bd832023-01-11 14:50:10 +01002074static void ssl_conf_remove_psk(mbedtls_ssl_config *conf)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002075{
2076 /* Remove reference to existing PSK, if any. */
2077#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002078 if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) {
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002079 /* The maintenance of the PSK key slot is the
2080 * user's responsibility. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002081 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002082 }
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002083#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01002084 if (conf->psk != NULL) {
2085 mbedtls_platform_zeroize(conf->psk, conf->psk_len);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002086
Gilles Peskine449bd832023-01-11 14:50:10 +01002087 mbedtls_free(conf->psk);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002088 conf->psk = NULL;
2089 conf->psk_len = 0;
2090 }
2091
2092 /* Remove reference to PSK identity, if any. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002093 if (conf->psk_identity != NULL) {
2094 mbedtls_free(conf->psk_identity);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002095 conf->psk_identity = NULL;
2096 conf->psk_identity_len = 0;
2097 }
2098}
2099
Hanno Becker7390c712018-11-15 13:33:04 +00002100/* This function assumes that PSK identity in the SSL config is unset.
2101 * It checks that the provided identity is well-formed and attempts
2102 * to make a copy of it in the SSL config.
2103 * On failure, the PSK identity in the config remains unset. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002104MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002105static int ssl_conf_set_psk_identity(mbedtls_ssl_config *conf,
2106 unsigned char const *psk_identity,
2107 size_t psk_identity_len)
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002108{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02002109 /* Identity len will be encoded on two bytes */
Gilles Peskine449bd832023-01-11 14:50:10 +01002110 if (psk_identity == NULL ||
Ronald Cron2a87e9b2022-10-19 10:55:26 +02002111 psk_identity_len == 0 ||
Gilles Peskine449bd832023-01-11 14:50:10 +01002112 (psk_identity_len >> 16) != 0 ||
2113 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN) {
2114 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02002115 }
2116
Gilles Peskine449bd832023-01-11 14:50:10 +01002117 conf->psk_identity = mbedtls_calloc(1, psk_identity_len);
2118 if (conf->psk_identity == NULL) {
2119 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2120 }
Paul Bakker6db455e2013-09-18 17:29:31 +02002121
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01002122 conf->psk_identity_len = psk_identity_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002123 memcpy(conf->psk_identity, psk_identity, conf->psk_identity_len);
Paul Bakker5ad403f2013-09-18 21:21:30 +02002124
Gilles Peskine449bd832023-01-11 14:50:10 +01002125 return 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02002126}
2127
Gilles Peskine449bd832023-01-11 14:50:10 +01002128int mbedtls_ssl_conf_psk(mbedtls_ssl_config *conf,
2129 const unsigned char *psk, size_t psk_len,
2130 const unsigned char *psk_identity, size_t psk_identity_len)
Hanno Becker7390c712018-11-15 13:33:04 +00002131{
Janos Follath865b3eb2019-12-16 11:46:15 +00002132 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002133
2134 /* We currently only support one PSK, raw or opaque. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002135 if (mbedtls_ssl_conf_has_static_psk(conf)) {
2136 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
2137 }
Hanno Becker7390c712018-11-15 13:33:04 +00002138
2139 /* Check and set raw PSK */
Gilles Peskine449bd832023-01-11 14:50:10 +01002140 if (psk == NULL) {
2141 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2142 }
2143 if (psk_len == 0) {
2144 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2145 }
2146 if (psk_len > MBEDTLS_PSK_MAX_LEN) {
2147 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2148 }
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01002149
Gilles Peskine449bd832023-01-11 14:50:10 +01002150 if ((conf->psk = mbedtls_calloc(1, psk_len)) == NULL) {
2151 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2152 }
Hanno Becker7390c712018-11-15 13:33:04 +00002153 conf->psk_len = psk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002154 memcpy(conf->psk, psk, conf->psk_len);
Hanno Becker7390c712018-11-15 13:33:04 +00002155
2156 /* Check and set PSK Identity */
Gilles Peskine449bd832023-01-11 14:50:10 +01002157 ret = ssl_conf_set_psk_identity(conf, psk_identity, psk_identity_len);
2158 if (ret != 0) {
2159 ssl_conf_remove_psk(conf);
2160 }
Hanno Becker7390c712018-11-15 13:33:04 +00002161
Gilles Peskine449bd832023-01-11 14:50:10 +01002162 return ret;
Hanno Becker7390c712018-11-15 13:33:04 +00002163}
2164
Gilles Peskine449bd832023-01-11 14:50:10 +01002165static void ssl_remove_psk(mbedtls_ssl_context *ssl)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002166{
2167#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002168 if (!mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque)) {
Neil Armstrong501c9322022-05-03 09:35:09 +02002169 /* The maintenance of the external PSK key slot is the
2170 * user's responsibility. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002171 if (ssl->handshake->psk_opaque_is_internal) {
2172 psa_destroy_key(ssl->handshake->psk_opaque);
Neil Armstrong501c9322022-05-03 09:35:09 +02002173 ssl->handshake->psk_opaque_is_internal = 0;
2174 }
Ronald Croncf56a0a2020-08-04 09:51:30 +02002175 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002176 }
Neil Armstronge952a302022-05-03 10:22:14 +02002177#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002178 if (ssl->handshake->psk != NULL) {
2179 mbedtls_platform_zeroize(ssl->handshake->psk,
2180 ssl->handshake->psk_len);
2181 mbedtls_free(ssl->handshake->psk);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002182 ssl->handshake->psk_len = 0;
2183 }
Neil Armstronge952a302022-05-03 10:22:14 +02002184#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002185}
2186
Gilles Peskine449bd832023-01-11 14:50:10 +01002187int mbedtls_ssl_set_hs_psk(mbedtls_ssl_context *ssl,
2188 const unsigned char *psk, size_t psk_len)
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002189{
Neil Armstrong501c9322022-05-03 09:35:09 +02002190#if defined(MBEDTLS_USE_PSA_CRYPTO)
2191 psa_key_attributes_t key_attributes = psa_key_attributes_init();
Jerry Yu5d01c052022-08-17 10:18:10 +08002192 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jerry Yu24b8c812022-08-20 19:06:56 +08002193 psa_algorithm_t alg = PSA_ALG_NONE;
Jerry Yu5d01c052022-08-17 10:18:10 +08002194 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrong501c9322022-05-03 09:35:09 +02002195#endif /* MBEDTLS_USE_PSA_CRYPTO */
2196
Gilles Peskine449bd832023-01-11 14:50:10 +01002197 if (psk == NULL || ssl->handshake == NULL) {
2198 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2199 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002200
Gilles Peskine449bd832023-01-11 14:50:10 +01002201 if (psk_len > MBEDTLS_PSK_MAX_LEN) {
2202 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2203 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002204
Gilles Peskine449bd832023-01-11 14:50:10 +01002205 ssl_remove_psk(ssl);
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002206
Neil Armstrong501c9322022-05-03 09:35:09 +02002207#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yuccc68a42022-07-26 16:39:20 +08002208#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01002209 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2) {
2210 if (ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384) {
2211 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
2212 } else {
2213 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
2214 }
2215 psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE);
Jerry Yuccc68a42022-07-26 16:39:20 +08002216 }
2217#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Neil Armstrong501c9322022-05-03 09:35:09 +02002218
Jerry Yu568ec252022-07-22 21:27:34 +08002219#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01002220 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
2221 alg = PSA_ALG_HKDF_EXTRACT(PSA_ALG_ANY_HASH);
2222 psa_set_key_usage_flags(&key_attributes,
2223 PSA_KEY_USAGE_DERIVE | PSA_KEY_USAGE_EXPORT);
Jerry Yuccc68a42022-07-26 16:39:20 +08002224 }
2225#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
2226
Gilles Peskine449bd832023-01-11 14:50:10 +01002227 psa_set_key_algorithm(&key_attributes, alg);
2228 psa_set_key_type(&key_attributes, PSA_KEY_TYPE_DERIVE);
Neil Armstrong501c9322022-05-03 09:35:09 +02002229
Gilles Peskine449bd832023-01-11 14:50:10 +01002230 status = psa_import_key(&key_attributes, psk, psk_len, &key);
2231 if (status != PSA_SUCCESS) {
2232 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2233 }
Neil Armstrong501c9322022-05-03 09:35:09 +02002234
2235 /* Allow calling psa_destroy_key() on psk remove */
2236 ssl->handshake->psk_opaque_is_internal = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01002237 return mbedtls_ssl_set_hs_psk_opaque(ssl, key);
Neil Armstrong501c9322022-05-03 09:35:09 +02002238#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002239 if ((ssl->handshake->psk = mbedtls_calloc(1, psk_len)) == NULL) {
2240 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2241 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002242
2243 ssl->handshake->psk_len = psk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002244 memcpy(ssl->handshake->psk, psk, ssl->handshake->psk_len);
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002245
Gilles Peskine449bd832023-01-11 14:50:10 +01002246 return 0;
Neil Armstrong501c9322022-05-03 09:35:09 +02002247#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002248}
2249
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002250#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002251int mbedtls_ssl_conf_psk_opaque(mbedtls_ssl_config *conf,
2252 mbedtls_svc_key_id_t psk,
2253 const unsigned char *psk_identity,
2254 size_t psk_identity_len)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002255{
Janos Follath865b3eb2019-12-16 11:46:15 +00002256 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002257
2258 /* We currently only support one PSK, raw or opaque. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002259 if (mbedtls_ssl_conf_has_static_psk(conf)) {
2260 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
2261 }
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002262
Hanno Becker7390c712018-11-15 13:33:04 +00002263 /* Check and set opaque PSK */
Gilles Peskine449bd832023-01-11 14:50:10 +01002264 if (mbedtls_svc_key_id_is_null(psk)) {
2265 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2266 }
Ronald Croncf56a0a2020-08-04 09:51:30 +02002267 conf->psk_opaque = psk;
Hanno Becker7390c712018-11-15 13:33:04 +00002268
2269 /* Check and set PSK Identity */
Gilles Peskine449bd832023-01-11 14:50:10 +01002270 ret = ssl_conf_set_psk_identity(conf, psk_identity,
2271 psk_identity_len);
2272 if (ret != 0) {
2273 ssl_conf_remove_psk(conf);
2274 }
Hanno Becker7390c712018-11-15 13:33:04 +00002275
Gilles Peskine449bd832023-01-11 14:50:10 +01002276 return ret;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002277}
2278
Gilles Peskine449bd832023-01-11 14:50:10 +01002279int mbedtls_ssl_set_hs_psk_opaque(mbedtls_ssl_context *ssl,
2280 mbedtls_svc_key_id_t psk)
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002281{
Gilles Peskine449bd832023-01-11 14:50:10 +01002282 if ((mbedtls_svc_key_id_is_null(psk)) ||
2283 (ssl->handshake == NULL)) {
2284 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2285 }
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002286
Gilles Peskine449bd832023-01-11 14:50:10 +01002287 ssl_remove_psk(ssl);
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002288 ssl->handshake->psk_opaque = psk;
Gilles Peskine449bd832023-01-11 14:50:10 +01002289 return 0;
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002290}
2291#endif /* MBEDTLS_USE_PSA_CRYPTO */
2292
Jerry Yu8897c072022-08-12 13:56:53 +08002293#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002294void mbedtls_ssl_conf_psk_cb(mbedtls_ssl_config *conf,
2295 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
2296 size_t),
2297 void *p_psk)
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002298{
2299 conf->f_psk = f_psk;
2300 conf->p_psk = p_psk;
2301}
Jerry Yu8897c072022-08-12 13:56:53 +08002302#endif /* MBEDTLS_SSL_SRV_C */
2303
Ronald Cron73fe8df2022-10-05 14:31:43 +02002304#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002305
2306#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine301711e2022-04-26 16:57:05 +02002307static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
Gilles Peskine449bd832023-01-11 14:50:10 +01002308 psa_algorithm_t alg)
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002309{
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002310#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01002311 if (alg == PSA_ALG_CBC_NO_PADDING) {
2312 return MBEDTLS_SSL_MODE_CBC;
2313 }
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002314#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
Gilles Peskine449bd832023-01-11 14:50:10 +01002315 if (PSA_ALG_IS_AEAD(alg)) {
2316 return MBEDTLS_SSL_MODE_AEAD;
2317 }
2318 return MBEDTLS_SSL_MODE_STREAM;
Gilles Peskine301711e2022-04-26 16:57:05 +02002319}
2320
2321#else /* MBEDTLS_USE_PSA_CRYPTO */
2322
2323static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
Gilles Peskine449bd832023-01-11 14:50:10 +01002324 mbedtls_cipher_mode_t mode)
Gilles Peskine301711e2022-04-26 16:57:05 +02002325{
2326#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01002327 if (mode == MBEDTLS_MODE_CBC) {
2328 return MBEDTLS_SSL_MODE_CBC;
2329 }
Gilles Peskine301711e2022-04-26 16:57:05 +02002330#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
2331
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002332#if defined(MBEDTLS_GCM_C) || \
2333 defined(MBEDTLS_CCM_C) || \
2334 defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002335 if (mode == MBEDTLS_MODE_GCM ||
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002336 mode == MBEDTLS_MODE_CCM ||
Gilles Peskine449bd832023-01-11 14:50:10 +01002337 mode == MBEDTLS_MODE_CHACHAPOLY) {
2338 return MBEDTLS_SSL_MODE_AEAD;
2339 }
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002340#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002341
Gilles Peskine449bd832023-01-11 14:50:10 +01002342 return MBEDTLS_SSL_MODE_STREAM;
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002343}
Gilles Peskine301711e2022-04-26 16:57:05 +02002344#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002345
Gilles Peskinee108d982022-04-26 16:50:40 +02002346static mbedtls_ssl_mode_t mbedtls_ssl_get_actual_mode(
2347 mbedtls_ssl_mode_t base_mode,
Gilles Peskine449bd832023-01-11 14:50:10 +01002348 int encrypt_then_mac)
Gilles Peskinee108d982022-04-26 16:50:40 +02002349{
2350#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01002351 if (encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED &&
2352 base_mode == MBEDTLS_SSL_MODE_CBC) {
2353 return MBEDTLS_SSL_MODE_CBC_ETM;
Gilles Peskinee108d982022-04-26 16:50:40 +02002354 }
2355#else
2356 (void) encrypt_then_mac;
2357#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002358 return base_mode;
Gilles Peskinee108d982022-04-26 16:50:40 +02002359}
2360
Neil Armstrongab555e02022-04-04 11:07:59 +02002361mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_transform(
Gilles Peskine449bd832023-01-11 14:50:10 +01002362 const mbedtls_ssl_transform *transform)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002363{
Gilles Peskinee108d982022-04-26 16:50:40 +02002364 mbedtls_ssl_mode_t base_mode = mbedtls_ssl_get_base_mode(
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002365#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002366 transform->psa_alg
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002367#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002368 mbedtls_cipher_get_cipher_mode(&transform->cipher_ctx_enc)
Gilles Peskinee108d982022-04-26 16:50:40 +02002369#endif
2370 );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002371
Gilles Peskinee108d982022-04-26 16:50:40 +02002372 int encrypt_then_mac = 0;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002373#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskinee108d982022-04-26 16:50:40 +02002374 encrypt_then_mac = transform->encrypt_then_mac;
2375#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002376 return mbedtls_ssl_get_actual_mode(base_mode, encrypt_then_mac);
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002377}
2378
Neil Armstrongab555e02022-04-04 11:07:59 +02002379mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002380#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01002381 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002382#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01002383 const mbedtls_ssl_ciphersuite_t *suite)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002384{
Gilles Peskinee108d982022-04-26 16:50:40 +02002385 mbedtls_ssl_mode_t base_mode = MBEDTLS_SSL_MODE_STREAM;
2386
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002387#if defined(MBEDTLS_USE_PSA_CRYPTO)
2388 psa_status_t status;
2389 psa_algorithm_t alg;
2390 psa_key_type_t type;
2391 size_t size;
Gilles Peskine449bd832023-01-11 14:50:10 +01002392 status = mbedtls_ssl_cipher_to_psa(suite->cipher, 0, &alg, &type, &size);
2393 if (status == PSA_SUCCESS) {
2394 base_mode = mbedtls_ssl_get_base_mode(alg);
2395 }
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002396#else
2397 const mbedtls_cipher_info_t *cipher =
Gilles Peskine449bd832023-01-11 14:50:10 +01002398 mbedtls_cipher_info_from_type(suite->cipher);
2399 if (cipher != NULL) {
Gilles Peskinee108d982022-04-26 16:50:40 +02002400 base_mode =
2401 mbedtls_ssl_get_base_mode(
Gilles Peskine449bd832023-01-11 14:50:10 +01002402 mbedtls_cipher_info_get_mode(cipher));
Gilles Peskinee108d982022-04-26 16:50:40 +02002403 }
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002404#endif /* MBEDTLS_USE_PSA_CRYPTO */
2405
Gilles Peskinee108d982022-04-26 16:50:40 +02002406#if !defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
2407 int encrypt_then_mac = 0;
2408#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002409 return mbedtls_ssl_get_actual_mode(base_mode, encrypt_then_mac);
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002410}
2411
Neil Armstrong8395d7a2022-05-18 11:44:56 +02002412#if defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu251a12e2022-07-13 15:15:48 +08002413
2414#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu438ddd82022-07-07 06:55:50 +00002415/* Serialization of TLS 1.3 sessions:
2416 *
2417 * struct {
Xiaokang Qian126bf8e2022-10-13 02:22:40 +00002418 * opaque hostname<0..2^16-1>;
Jerry Yu438ddd82022-07-07 06:55:50 +00002419 * uint64 ticket_received;
2420 * uint32 ticket_lifetime;
Jerry Yu34191072022-08-18 10:32:09 +08002421 * opaque ticket<1..2^16-1>;
Jerry Yu438ddd82022-07-07 06:55:50 +00002422 * } ClientOnlyData;
2423 *
2424 * struct {
2425 * uint8 endpoint;
2426 * uint8 ciphersuite[2];
2427 * uint32 ticket_age_add;
2428 * uint8 ticket_flags;
2429 * opaque resumption_key<0..255>;
2430 * select ( endpoint ) {
2431 * case client: ClientOnlyData;
2432 * case server: uint64 start_time;
2433 * };
2434 * } serialized_session_tls13;
2435 *
2436 */
2437#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yue36fdd62022-08-17 21:31:36 +08002438MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002439static int ssl_tls13_session_save(const mbedtls_ssl_session *session,
2440 unsigned char *buf,
2441 size_t buf_len,
2442 size_t *olen)
Jerry Yu438ddd82022-07-07 06:55:50 +00002443{
2444 unsigned char *p = buf;
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00002445#if defined(MBEDTLS_SSL_CLI_C) && \
2446 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01002447 size_t hostname_len = (session->hostname == NULL) ?
2448 0 : strlen(session->hostname) + 1;
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00002449#endif
Jerry Yubc7c1a42022-07-21 22:57:37 +08002450 size_t needed = 1 /* endpoint */
2451 + 2 /* ciphersuite */
2452 + 4 /* ticket_age_add */
Jerry Yu34191072022-08-18 10:32:09 +08002453 + 1 /* ticket_flags */
2454 + 1; /* resumption_key length */
Jerry Yue36fdd62022-08-17 21:31:36 +08002455 *olen = 0;
Jerry Yu34191072022-08-18 10:32:09 +08002456
Gilles Peskine449bd832023-01-11 14:50:10 +01002457 if (session->resumption_key_len > MBEDTLS_SSL_TLS1_3_TICKET_RESUMPTION_KEY_LEN) {
2458 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2459 }
Jerry Yu34191072022-08-18 10:32:09 +08002460 needed += session->resumption_key_len; /* resumption_key */
2461
Jerry Yu438ddd82022-07-07 06:55:50 +00002462#if defined(MBEDTLS_HAVE_TIME)
2463 needed += 8; /* start_time or ticket_received */
2464#endif
2465
2466#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002467 if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Xiaokang Qian126bf8e2022-10-13 02:22:40 +00002468#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaokang Qianbc663a02022-10-09 11:14:39 +00002469 needed += 2 /* hostname_len */
Gilles Peskine449bd832023-01-11 14:50:10 +01002470 + hostname_len; /* hostname */
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00002471#endif
2472
Jerry Yu438ddd82022-07-07 06:55:50 +00002473 needed += 4 /* ticket_lifetime */
Jerry Yue36fdd62022-08-17 21:31:36 +08002474 + 2; /* ticket_len */
Jerry Yu34191072022-08-18 10:32:09 +08002475
2476 /* Check size_t overflow */
Gilles Peskine449bd832023-01-11 14:50:10 +01002477 if (session->ticket_len > SIZE_MAX - needed) {
2478 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2479 }
Jerry Yu34191072022-08-18 10:32:09 +08002480
Jerry Yue28d9742022-08-18 15:44:03 +08002481 needed += session->ticket_len; /* ticket */
Jerry Yu438ddd82022-07-07 06:55:50 +00002482 }
2483#endif /* MBEDTLS_SSL_CLI_C */
2484
Jerry Yue36fdd62022-08-17 21:31:36 +08002485 *olen = needed;
Gilles Peskine449bd832023-01-11 14:50:10 +01002486 if (needed > buf_len) {
2487 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
2488 }
Jerry Yu438ddd82022-07-07 06:55:50 +00002489
2490 p[0] = session->endpoint;
Gilles Peskine449bd832023-01-11 14:50:10 +01002491 MBEDTLS_PUT_UINT16_BE(session->ciphersuite, p, 1);
2492 MBEDTLS_PUT_UINT32_BE(session->ticket_age_add, p, 3);
Jerry Yu438ddd82022-07-07 06:55:50 +00002493 p[7] = session->ticket_flags;
2494
2495 /* save resumption_key */
Jerry Yubc7c1a42022-07-21 22:57:37 +08002496 p[8] = session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002497 p += 9;
Gilles Peskine449bd832023-01-11 14:50:10 +01002498 memcpy(p, session->resumption_key, session->resumption_key_len);
Jerry Yubc7c1a42022-07-21 22:57:37 +08002499 p += session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002500
2501#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002502 if (session->endpoint == MBEDTLS_SSL_IS_SERVER) {
2503 MBEDTLS_PUT_UINT64_BE((uint64_t) session->start, p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002504 p += 8;
2505 }
2506#endif /* MBEDTLS_HAVE_TIME */
2507
2508#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002509 if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002510#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01002511 MBEDTLS_PUT_UINT16_BE(hostname_len, p, 0);
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002512 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002513 if (hostname_len > 0) {
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002514 /* save host name */
Gilles Peskine449bd832023-01-11 14:50:10 +01002515 memcpy(p, session->hostname, hostname_len);
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002516 p += hostname_len;
2517 }
2518#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
2519
Jerry Yu438ddd82022-07-07 06:55:50 +00002520#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01002521 MBEDTLS_PUT_UINT64_BE((uint64_t) session->ticket_received, p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002522 p += 8;
2523#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002524 MBEDTLS_PUT_UINT32_BE(session->ticket_lifetime, p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002525 p += 4;
2526
Gilles Peskine449bd832023-01-11 14:50:10 +01002527 MBEDTLS_PUT_UINT16_BE(session->ticket_len, p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002528 p += 2;
Jerry Yu34191072022-08-18 10:32:09 +08002529
Gilles Peskine449bd832023-01-11 14:50:10 +01002530 if (session->ticket != NULL && session->ticket_len > 0) {
2531 memcpy(p, session->ticket, session->ticket_len);
Jerry Yu438ddd82022-07-07 06:55:50 +00002532 p += session->ticket_len;
2533 }
2534 }
2535#endif /* MBEDTLS_SSL_CLI_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01002536 return 0;
Jerry Yu438ddd82022-07-07 06:55:50 +00002537}
2538
2539MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002540static int ssl_tls13_session_load(mbedtls_ssl_session *session,
2541 const unsigned char *buf,
2542 size_t len)
Jerry Yu438ddd82022-07-07 06:55:50 +00002543{
2544 const unsigned char *p = buf;
2545 const unsigned char *end = buf + len;
2546
Gilles Peskine449bd832023-01-11 14:50:10 +01002547 if (end - p < 9) {
2548 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2549 }
Jerry Yu438ddd82022-07-07 06:55:50 +00002550 session->endpoint = p[0];
Gilles Peskine449bd832023-01-11 14:50:10 +01002551 session->ciphersuite = MBEDTLS_GET_UINT16_BE(p, 1);
2552 session->ticket_age_add = MBEDTLS_GET_UINT32_BE(p, 3);
Jerry Yu438ddd82022-07-07 06:55:50 +00002553 session->ticket_flags = p[7];
2554
2555 /* load resumption_key */
Jerry Yubc7c1a42022-07-21 22:57:37 +08002556 session->resumption_key_len = p[8];
Jerry Yu438ddd82022-07-07 06:55:50 +00002557 p += 9;
2558
Gilles Peskine449bd832023-01-11 14:50:10 +01002559 if (end - p < session->resumption_key_len) {
2560 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2561 }
Jerry Yu438ddd82022-07-07 06:55:50 +00002562
Gilles Peskine449bd832023-01-11 14:50:10 +01002563 if (sizeof(session->resumption_key) < session->resumption_key_len) {
2564 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2565 }
2566 memcpy(session->resumption_key, p, session->resumption_key_len);
Jerry Yubc7c1a42022-07-21 22:57:37 +08002567 p += session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002568
2569#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002570 if (session->endpoint == MBEDTLS_SSL_IS_SERVER) {
2571 if (end - p < 8) {
2572 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2573 }
2574 session->start = MBEDTLS_GET_UINT64_BE(p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002575 p += 8;
2576 }
2577#endif /* MBEDTLS_HAVE_TIME */
2578
2579#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002580 if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002581#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01002582 defined(MBEDTLS_SSL_SESSION_TICKETS)
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002583 size_t hostname_len;
2584 /* load host name */
Gilles Peskine449bd832023-01-11 14:50:10 +01002585 if (end - p < 2) {
2586 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2587 }
2588 hostname_len = MBEDTLS_GET_UINT16_BE(p, 0);
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002589 p += 2;
2590
Gilles Peskine449bd832023-01-11 14:50:10 +01002591 if (end - p < (long int) hostname_len) {
2592 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2593 }
2594 if (hostname_len > 0) {
2595 session->hostname = mbedtls_calloc(1, hostname_len);
2596 if (session->hostname == NULL) {
2597 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2598 }
2599 memcpy(session->hostname, p, hostname_len);
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002600 p += hostname_len;
2601 }
2602#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION &&
2603 MBEDTLS_SSL_SESSION_TICKETS */
2604
Jerry Yu438ddd82022-07-07 06:55:50 +00002605#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01002606 if (end - p < 8) {
2607 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2608 }
2609 session->ticket_received = MBEDTLS_GET_UINT64_BE(p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002610 p += 8;
2611#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002612 if (end - p < 4) {
2613 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2614 }
2615 session->ticket_lifetime = MBEDTLS_GET_UINT32_BE(p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002616 p += 4;
2617
Gilles Peskine449bd832023-01-11 14:50:10 +01002618 if (end - p < 2) {
2619 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2620 }
2621 session->ticket_len = MBEDTLS_GET_UINT16_BE(p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002622 p += 2;
2623
Gilles Peskine449bd832023-01-11 14:50:10 +01002624 if (end - p < (long int) session->ticket_len) {
2625 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2626 }
2627 if (session->ticket_len > 0) {
2628 session->ticket = mbedtls_calloc(1, session->ticket_len);
2629 if (session->ticket == NULL) {
2630 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2631 }
2632 memcpy(session->ticket, p, session->ticket_len);
Jerry Yu438ddd82022-07-07 06:55:50 +00002633 p += session->ticket_len;
2634 }
2635 }
2636#endif /* MBEDTLS_SSL_CLI_C */
2637
Gilles Peskine449bd832023-01-11 14:50:10 +01002638 return 0;
Jerry Yu438ddd82022-07-07 06:55:50 +00002639
2640}
2641#else /* MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yue36fdd62022-08-17 21:31:36 +08002642MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002643static int ssl_tls13_session_save(const mbedtls_ssl_session *session,
2644 unsigned char *buf,
2645 size_t buf_len,
2646 size_t *olen)
Jerry Yu251a12e2022-07-13 15:15:48 +08002647{
2648 ((void) session);
2649 ((void) buf);
2650 ((void) buf_len);
Jerry Yue36fdd62022-08-17 21:31:36 +08002651 *olen = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002652 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Jerry Yu251a12e2022-07-13 15:15:48 +08002653}
Jerry Yu438ddd82022-07-07 06:55:50 +00002654
Gilles Peskine449bd832023-01-11 14:50:10 +01002655static int ssl_tls13_session_load(const mbedtls_ssl_session *session,
2656 unsigned char *buf,
2657 size_t buf_len)
Jerry Yu438ddd82022-07-07 06:55:50 +00002658{
2659 ((void) session);
2660 ((void) buf);
2661 ((void) buf_len);
Gilles Peskine449bd832023-01-11 14:50:10 +01002662 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Jerry Yu438ddd82022-07-07 06:55:50 +00002663}
2664#endif /* !MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yu251a12e2022-07-13 15:15:48 +08002665#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
2666
Gilles Peskine449bd832023-01-11 14:50:10 +01002667psa_status_t mbedtls_ssl_cipher_to_psa(mbedtls_cipher_type_t mbedtls_cipher_type,
2668 size_t taglen,
2669 psa_algorithm_t *alg,
2670 psa_key_type_t *key_type,
2671 size_t *key_size)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002672{
Gilles Peskine449bd832023-01-11 14:50:10 +01002673 switch (mbedtls_cipher_type) {
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002674 case MBEDTLS_CIPHER_AES_128_CBC:
2675 *alg = PSA_ALG_CBC_NO_PADDING;
2676 *key_type = PSA_KEY_TYPE_AES;
2677 *key_size = 128;
2678 break;
2679 case MBEDTLS_CIPHER_AES_128_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002680 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002681 *key_type = PSA_KEY_TYPE_AES;
2682 *key_size = 128;
2683 break;
2684 case MBEDTLS_CIPHER_AES_128_GCM:
2685 *alg = PSA_ALG_GCM;
2686 *key_type = PSA_KEY_TYPE_AES;
2687 *key_size = 128;
2688 break;
2689 case MBEDTLS_CIPHER_AES_192_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002690 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002691 *key_type = PSA_KEY_TYPE_AES;
2692 *key_size = 192;
2693 break;
2694 case MBEDTLS_CIPHER_AES_192_GCM:
2695 *alg = PSA_ALG_GCM;
2696 *key_type = PSA_KEY_TYPE_AES;
2697 *key_size = 192;
2698 break;
2699 case MBEDTLS_CIPHER_AES_256_CBC:
2700 *alg = PSA_ALG_CBC_NO_PADDING;
2701 *key_type = PSA_KEY_TYPE_AES;
2702 *key_size = 256;
2703 break;
2704 case MBEDTLS_CIPHER_AES_256_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002705 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002706 *key_type = PSA_KEY_TYPE_AES;
2707 *key_size = 256;
2708 break;
2709 case MBEDTLS_CIPHER_AES_256_GCM:
2710 *alg = PSA_ALG_GCM;
2711 *key_type = PSA_KEY_TYPE_AES;
2712 *key_size = 256;
2713 break;
2714 case MBEDTLS_CIPHER_ARIA_128_CBC:
2715 *alg = PSA_ALG_CBC_NO_PADDING;
2716 *key_type = PSA_KEY_TYPE_ARIA;
2717 *key_size = 128;
2718 break;
2719 case MBEDTLS_CIPHER_ARIA_128_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002720 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002721 *key_type = PSA_KEY_TYPE_ARIA;
2722 *key_size = 128;
2723 break;
2724 case MBEDTLS_CIPHER_ARIA_128_GCM:
2725 *alg = PSA_ALG_GCM;
2726 *key_type = PSA_KEY_TYPE_ARIA;
2727 *key_size = 128;
2728 break;
2729 case MBEDTLS_CIPHER_ARIA_192_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002730 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002731 *key_type = PSA_KEY_TYPE_ARIA;
2732 *key_size = 192;
2733 break;
2734 case MBEDTLS_CIPHER_ARIA_192_GCM:
2735 *alg = PSA_ALG_GCM;
2736 *key_type = PSA_KEY_TYPE_ARIA;
2737 *key_size = 192;
2738 break;
2739 case MBEDTLS_CIPHER_ARIA_256_CBC:
2740 *alg = PSA_ALG_CBC_NO_PADDING;
2741 *key_type = PSA_KEY_TYPE_ARIA;
2742 *key_size = 256;
2743 break;
2744 case MBEDTLS_CIPHER_ARIA_256_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002745 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002746 *key_type = PSA_KEY_TYPE_ARIA;
2747 *key_size = 256;
2748 break;
2749 case MBEDTLS_CIPHER_ARIA_256_GCM:
2750 *alg = PSA_ALG_GCM;
2751 *key_type = PSA_KEY_TYPE_ARIA;
2752 *key_size = 256;
2753 break;
2754 case MBEDTLS_CIPHER_CAMELLIA_128_CBC:
2755 *alg = PSA_ALG_CBC_NO_PADDING;
2756 *key_type = PSA_KEY_TYPE_CAMELLIA;
2757 *key_size = 128;
2758 break;
2759 case MBEDTLS_CIPHER_CAMELLIA_128_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002760 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002761 *key_type = PSA_KEY_TYPE_CAMELLIA;
2762 *key_size = 128;
2763 break;
2764 case MBEDTLS_CIPHER_CAMELLIA_128_GCM:
2765 *alg = PSA_ALG_GCM;
2766 *key_type = PSA_KEY_TYPE_CAMELLIA;
2767 *key_size = 128;
2768 break;
2769 case MBEDTLS_CIPHER_CAMELLIA_192_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002770 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002771 *key_type = PSA_KEY_TYPE_CAMELLIA;
2772 *key_size = 192;
2773 break;
2774 case MBEDTLS_CIPHER_CAMELLIA_192_GCM:
2775 *alg = PSA_ALG_GCM;
2776 *key_type = PSA_KEY_TYPE_CAMELLIA;
2777 *key_size = 192;
2778 break;
2779 case MBEDTLS_CIPHER_CAMELLIA_256_CBC:
2780 *alg = PSA_ALG_CBC_NO_PADDING;
2781 *key_type = PSA_KEY_TYPE_CAMELLIA;
2782 *key_size = 256;
2783 break;
2784 case MBEDTLS_CIPHER_CAMELLIA_256_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002785 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002786 *key_type = PSA_KEY_TYPE_CAMELLIA;
2787 *key_size = 256;
2788 break;
2789 case MBEDTLS_CIPHER_CAMELLIA_256_GCM:
2790 *alg = PSA_ALG_GCM;
2791 *key_type = PSA_KEY_TYPE_CAMELLIA;
2792 *key_size = 256;
2793 break;
2794 case MBEDTLS_CIPHER_CHACHA20_POLY1305:
2795 *alg = PSA_ALG_CHACHA20_POLY1305;
2796 *key_type = PSA_KEY_TYPE_CHACHA20;
2797 *key_size = 256;
2798 break;
2799 case MBEDTLS_CIPHER_NULL:
2800 *alg = MBEDTLS_SSL_NULL_CIPHER;
2801 *key_type = 0;
2802 *key_size = 0;
2803 break;
2804 default:
2805 return PSA_ERROR_NOT_SUPPORTED;
2806 }
2807
2808 return PSA_SUCCESS;
2809}
Neil Armstrong8395d7a2022-05-18 11:44:56 +02002810#endif /* MBEDTLS_USE_PSA_CRYPTO || MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002811
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002812#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002813int mbedtls_ssl_conf_dh_param_bin(mbedtls_ssl_config *conf,
2814 const unsigned char *dhm_P, size_t P_len,
2815 const unsigned char *dhm_G, size_t G_len)
Hanno Beckera90658f2017-10-04 15:29:08 +01002816{
Janos Follath865b3eb2019-12-16 11:46:15 +00002817 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckera90658f2017-10-04 15:29:08 +01002818
Gilles Peskine449bd832023-01-11 14:50:10 +01002819 mbedtls_mpi_free(&conf->dhm_P);
2820 mbedtls_mpi_free(&conf->dhm_G);
Glenn Strausscee11292021-12-20 01:43:17 -05002821
Gilles Peskine449bd832023-01-11 14:50:10 +01002822 if ((ret = mbedtls_mpi_read_binary(&conf->dhm_P, dhm_P, P_len)) != 0 ||
2823 (ret = mbedtls_mpi_read_binary(&conf->dhm_G, dhm_G, G_len)) != 0) {
2824 mbedtls_mpi_free(&conf->dhm_P);
2825 mbedtls_mpi_free(&conf->dhm_G);
2826 return ret;
Hanno Beckera90658f2017-10-04 15:29:08 +01002827 }
2828
Gilles Peskine449bd832023-01-11 14:50:10 +01002829 return 0;
Hanno Beckera90658f2017-10-04 15:29:08 +01002830}
Paul Bakker5121ce52009-01-03 21:22:43 +00002831
Gilles Peskine449bd832023-01-11 14:50:10 +01002832int mbedtls_ssl_conf_dh_param_ctx(mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx)
Paul Bakker1b57b062011-01-06 15:48:19 +00002833{
Janos Follath865b3eb2019-12-16 11:46:15 +00002834 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1b57b062011-01-06 15:48:19 +00002835
Gilles Peskine449bd832023-01-11 14:50:10 +01002836 mbedtls_mpi_free(&conf->dhm_P);
2837 mbedtls_mpi_free(&conf->dhm_G);
Glenn Strausscee11292021-12-20 01:43:17 -05002838
Gilles Peskine449bd832023-01-11 14:50:10 +01002839 if ((ret = mbedtls_dhm_get_value(dhm_ctx, MBEDTLS_DHM_PARAM_P,
2840 &conf->dhm_P)) != 0 ||
2841 (ret = mbedtls_dhm_get_value(dhm_ctx, MBEDTLS_DHM_PARAM_G,
2842 &conf->dhm_G)) != 0) {
2843 mbedtls_mpi_free(&conf->dhm_P);
2844 mbedtls_mpi_free(&conf->dhm_G);
2845 return ret;
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01002846 }
Paul Bakker1b57b062011-01-06 15:48:19 +00002847
Gilles Peskine449bd832023-01-11 14:50:10 +01002848 return 0;
Paul Bakker1b57b062011-01-06 15:48:19 +00002849}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002850#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00002851
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02002852#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
2853/*
2854 * Set the minimum length for Diffie-Hellman parameters
2855 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002856void mbedtls_ssl_conf_dhm_min_bitlen(mbedtls_ssl_config *conf,
2857 unsigned int bitlen)
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02002858{
2859 conf->dhm_min_bitlen = bitlen;
2860}
2861#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
2862
Ronald Crone68ab4f2022-10-05 12:46:29 +02002863#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002864#if !defined(MBEDTLS_DEPRECATED_REMOVED) && defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002865/*
2866 * Set allowed/preferred hashes for handshake signatures
2867 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002868void mbedtls_ssl_conf_sig_hashes(mbedtls_ssl_config *conf,
2869 const int *hashes)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002870{
2871 conf->sig_hashes = hashes;
2872}
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002873#endif /* !MBEDTLS_DEPRECATED_REMOVED && MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker1cd6e002021-08-10 13:27:10 +01002874
Jerry Yuf017ee42022-01-12 15:49:48 +08002875/* Configure allowed signature algorithms for handshake */
Gilles Peskine449bd832023-01-11 14:50:10 +01002876void mbedtls_ssl_conf_sig_algs(mbedtls_ssl_config *conf,
2877 const uint16_t *sig_algs)
Hanno Becker1cd6e002021-08-10 13:27:10 +01002878{
Jerry Yuf017ee42022-01-12 15:49:48 +08002879#if !defined(MBEDTLS_DEPRECATED_REMOVED)
2880 conf->sig_hashes = NULL;
2881#endif /* !MBEDTLS_DEPRECATED_REMOVED */
2882 conf->sig_algs = sig_algs;
Hanno Becker1cd6e002021-08-10 13:27:10 +01002883}
Ronald Crone68ab4f2022-10-05 12:46:29 +02002884#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002885
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02002886#if defined(MBEDTLS_ECP_C)
Brett Warrene0edc842021-08-17 09:53:13 +01002887#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002888/*
2889 * Set the allowed elliptic curves
Brett Warrene0edc842021-08-17 09:53:13 +01002890 *
2891 * mbedtls_ssl_setup() takes the provided list
2892 * and translates it to a list of IANA TLS group identifiers,
2893 * stored in ssl->handshake->group_list.
2894 *
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002895 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002896void mbedtls_ssl_conf_curves(mbedtls_ssl_config *conf,
2897 const mbedtls_ecp_group_id *curve_list)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002898{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002899 conf->curve_list = curve_list;
Brett Warrene0edc842021-08-17 09:53:13 +01002900 conf->group_list = NULL;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002901}
Brett Warrene0edc842021-08-17 09:53:13 +01002902#endif /* MBEDTLS_DEPRECATED_REMOVED */
Hanno Becker947194e2017-04-07 13:25:49 +01002903#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002904
Brett Warrene0edc842021-08-17 09:53:13 +01002905/*
2906 * Set the allowed groups
2907 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002908void mbedtls_ssl_conf_groups(mbedtls_ssl_config *conf,
2909 const uint16_t *group_list)
Brett Warrene0edc842021-08-17 09:53:13 +01002910{
2911#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
2912 conf->curve_list = NULL;
2913#endif
2914 conf->group_list = group_list;
2915}
2916
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002917#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002918int mbedtls_ssl_set_hostname(mbedtls_ssl_context *ssl, const char *hostname)
Paul Bakker5121ce52009-01-03 21:22:43 +00002919{
Hanno Becker947194e2017-04-07 13:25:49 +01002920 /* Initialize to suppress unnecessary compiler warning */
2921 size_t hostname_len = 0;
2922
2923 /* Check if new hostname is valid before
2924 * making any change to current one */
Gilles Peskine449bd832023-01-11 14:50:10 +01002925 if (hostname != NULL) {
2926 hostname_len = strlen(hostname);
Hanno Becker947194e2017-04-07 13:25:49 +01002927
Gilles Peskine449bd832023-01-11 14:50:10 +01002928 if (hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN) {
2929 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2930 }
Hanno Becker947194e2017-04-07 13:25:49 +01002931 }
2932
2933 /* Now it's clear that we will overwrite the old hostname,
2934 * so we can free it safely */
2935
Gilles Peskine449bd832023-01-11 14:50:10 +01002936 if (ssl->hostname != NULL) {
2937 mbedtls_platform_zeroize(ssl->hostname, strlen(ssl->hostname));
2938 mbedtls_free(ssl->hostname);
Hanno Becker947194e2017-04-07 13:25:49 +01002939 }
2940
2941 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01002942
Gilles Peskine449bd832023-01-11 14:50:10 +01002943 if (hostname == NULL) {
Hanno Becker947194e2017-04-07 13:25:49 +01002944 ssl->hostname = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01002945 } else {
2946 ssl->hostname = mbedtls_calloc(1, hostname_len + 1);
2947 if (ssl->hostname == NULL) {
2948 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2949 }
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002950
Gilles Peskine449bd832023-01-11 14:50:10 +01002951 memcpy(ssl->hostname, hostname, hostname_len);
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002952
Hanno Becker947194e2017-04-07 13:25:49 +01002953 ssl->hostname[hostname_len] = '\0';
2954 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002955
Gilles Peskine449bd832023-01-11 14:50:10 +01002956 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002957}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01002958#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002959
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002960#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01002961void mbedtls_ssl_conf_sni(mbedtls_ssl_config *conf,
2962 int (*f_sni)(void *, mbedtls_ssl_context *,
2963 const unsigned char *, size_t),
2964 void *p_sni)
Paul Bakker5701cdc2012-09-27 21:49:42 +00002965{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002966 conf->f_sni = f_sni;
2967 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00002968}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002969#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00002970
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002971#if defined(MBEDTLS_SSL_ALPN)
Gilles Peskine449bd832023-01-11 14:50:10 +01002972int mbedtls_ssl_conf_alpn_protocols(mbedtls_ssl_config *conf, const char **protos)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002973{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002974 size_t cur_len, tot_len;
2975 const char **p;
2976
2977 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08002978 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
2979 * MUST NOT be truncated."
2980 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002981 */
2982 tot_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002983 for (p = protos; *p != NULL; p++) {
2984 cur_len = strlen(*p);
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002985 tot_len += cur_len;
2986
Gilles Peskine449bd832023-01-11 14:50:10 +01002987 if ((cur_len == 0) ||
2988 (cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN) ||
2989 (tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN)) {
2990 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2991 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002992 }
2993
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002994 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002995
Gilles Peskine449bd832023-01-11 14:50:10 +01002996 return 0;
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002997}
2998
Gilles Peskine449bd832023-01-11 14:50:10 +01002999const char *mbedtls_ssl_get_alpn_protocol(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003000{
Gilles Peskine449bd832023-01-11 14:50:10 +01003001 return ssl->alpn_chosen;
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003002}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003003#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003004
Johan Pascalb62bb512015-12-03 21:56:45 +01003005#if defined(MBEDTLS_SSL_DTLS_SRTP)
Gilles Peskine449bd832023-01-11 14:50:10 +01003006void mbedtls_ssl_conf_srtp_mki_value_supported(mbedtls_ssl_config *conf,
3007 int support_mki_value)
Ron Eldor591f1622018-01-22 12:30:04 +02003008{
3009 conf->dtls_srtp_mki_support = support_mki_value;
3010}
3011
Gilles Peskine449bd832023-01-11 14:50:10 +01003012int mbedtls_ssl_dtls_srtp_set_mki_value(mbedtls_ssl_context *ssl,
3013 unsigned char *mki_value,
3014 uint16_t mki_len)
Ron Eldor591f1622018-01-22 12:30:04 +02003015{
Gilles Peskine449bd832023-01-11 14:50:10 +01003016 if (mki_len > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH) {
3017 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Ron Eldora9788042018-12-05 11:04:31 +02003018 }
Ron Eldor591f1622018-01-22 12:30:04 +02003019
Gilles Peskine449bd832023-01-11 14:50:10 +01003020 if (ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_UNSUPPORTED) {
3021 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Ron Eldora9788042018-12-05 11:04:31 +02003022 }
Ron Eldor591f1622018-01-22 12:30:04 +02003023
Gilles Peskine449bd832023-01-11 14:50:10 +01003024 memcpy(ssl->dtls_srtp_info.mki_value, mki_value, mki_len);
Ron Eldor591f1622018-01-22 12:30:04 +02003025 ssl->dtls_srtp_info.mki_len = mki_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01003026 return 0;
Ron Eldor591f1622018-01-22 12:30:04 +02003027}
3028
Gilles Peskine449bd832023-01-11 14:50:10 +01003029int mbedtls_ssl_conf_dtls_srtp_protection_profiles(mbedtls_ssl_config *conf,
3030 const mbedtls_ssl_srtp_profile *profiles)
Johan Pascalb62bb512015-12-03 21:56:45 +01003031{
Johan Pascal253d0262020-09-22 13:04:45 +02003032 const mbedtls_ssl_srtp_profile *p;
3033 size_t list_size = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01003034
Johan Pascal253d0262020-09-22 13:04:45 +02003035 /* check the profiles list: all entry must be valid,
3036 * its size cannot be more than the total number of supported profiles, currently 4 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003037 for (p = profiles; *p != MBEDTLS_TLS_SRTP_UNSET &&
3038 list_size <= MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH;
3039 p++) {
3040 if (mbedtls_ssl_check_srtp_profile_value(*p) != MBEDTLS_TLS_SRTP_UNSET) {
Johan Pascal76fdf1d2020-10-22 23:31:00 +02003041 list_size++;
Gilles Peskine449bd832023-01-11 14:50:10 +01003042 } else {
Johan Pascal76fdf1d2020-10-22 23:31:00 +02003043 /* unsupported value, stop parsing and set the size to an error value */
3044 list_size = MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH + 1;
Johan Pascalb62bb512015-12-03 21:56:45 +01003045 }
3046 }
3047
Gilles Peskine449bd832023-01-11 14:50:10 +01003048 if (list_size > MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH) {
3049 conf->dtls_srtp_profile_list = NULL;
3050 conf->dtls_srtp_profile_list_len = 0;
3051 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Johan Pascal253d0262020-09-22 13:04:45 +02003052 }
3053
Johan Pascal9bc97ca2020-09-21 23:44:45 +02003054 conf->dtls_srtp_profile_list = profiles;
Johan Pascal253d0262020-09-22 13:04:45 +02003055 conf->dtls_srtp_profile_list_len = list_size;
Johan Pascalb62bb512015-12-03 21:56:45 +01003056
Gilles Peskine449bd832023-01-11 14:50:10 +01003057 return 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01003058}
3059
Gilles Peskine449bd832023-01-11 14:50:10 +01003060void mbedtls_ssl_get_dtls_srtp_negotiation_result(const mbedtls_ssl_context *ssl,
3061 mbedtls_dtls_srtp_info *dtls_srtp_info)
Johan Pascalb62bb512015-12-03 21:56:45 +01003062{
Johan Pascal2258a4f2020-10-28 13:53:09 +01003063 dtls_srtp_info->chosen_dtls_srtp_profile = ssl->dtls_srtp_info.chosen_dtls_srtp_profile;
3064 /* do not copy the mki value if there is no chosen profile */
Gilles Peskine449bd832023-01-11 14:50:10 +01003065 if (dtls_srtp_info->chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET) {
Johan Pascal2258a4f2020-10-28 13:53:09 +01003066 dtls_srtp_info->mki_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003067 } else {
Johan Pascal2258a4f2020-10-28 13:53:09 +01003068 dtls_srtp_info->mki_len = ssl->dtls_srtp_info.mki_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01003069 memcpy(dtls_srtp_info->mki_value, ssl->dtls_srtp_info.mki_value,
3070 ssl->dtls_srtp_info.mki_len);
Johan Pascal2258a4f2020-10-28 13:53:09 +01003071 }
Johan Pascalb62bb512015-12-03 21:56:45 +01003072}
Johan Pascalb62bb512015-12-03 21:56:45 +01003073#endif /* MBEDTLS_SSL_DTLS_SRTP */
3074
Aditya Patwardhan3096f332022-07-26 14:31:46 +05303075#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003076void mbedtls_ssl_conf_max_version(mbedtls_ssl_config *conf, int major, int minor)
Paul Bakker490ecc82011-10-06 13:04:09 +00003077{
Glenn Strauss2dfcea22022-03-14 17:26:42 -04003078 conf->max_tls_version = (major << 8) | minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00003079}
3080
Gilles Peskine449bd832023-01-11 14:50:10 +01003081void mbedtls_ssl_conf_min_version(mbedtls_ssl_config *conf, int major, int minor)
Paul Bakker1d29fb52012-09-28 13:28:45 +00003082{
Glenn Strauss2dfcea22022-03-14 17:26:42 -04003083 conf->min_tls_version = (major << 8) | minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00003084}
Aditya Patwardhan3096f332022-07-26 14:31:46 +05303085#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker1d29fb52012-09-28 13:28:45 +00003086
Janos Follath088ce432017-04-10 12:42:31 +01003087#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003088void mbedtls_ssl_conf_cert_req_ca_list(mbedtls_ssl_config *conf,
3089 char cert_req_ca_list)
Janos Follath088ce432017-04-10 12:42:31 +01003090{
3091 conf->cert_req_ca_list = cert_req_ca_list;
3092}
3093#endif
3094
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003095#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01003096void mbedtls_ssl_conf_encrypt_then_mac(mbedtls_ssl_config *conf, char etm)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01003097{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003098 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01003099}
3100#endif
3101
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003102#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Gilles Peskine449bd832023-01-11 14:50:10 +01003103void mbedtls_ssl_conf_extended_master_secret(mbedtls_ssl_config *conf, char ems)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02003104{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003105 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02003106}
3107#endif
3108
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003109#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01003110int mbedtls_ssl_conf_max_frag_len(mbedtls_ssl_config *conf, unsigned char mfl_code)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003111{
Gilles Peskine449bd832023-01-11 14:50:10 +01003112 if (mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
3113 ssl_mfl_code_to_length(mfl_code) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN) {
3114 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003115 }
3116
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01003117 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003118
Gilles Peskine449bd832023-01-11 14:50:10 +01003119 return 0;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003120}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003121#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003122
Gilles Peskine449bd832023-01-11 14:50:10 +01003123void mbedtls_ssl_conf_legacy_renegotiation(mbedtls_ssl_config *conf, int allow_legacy)
Paul Bakker48916f92012-09-16 19:57:18 +00003124{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003125 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00003126}
3127
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003128#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01003129void mbedtls_ssl_conf_renegotiation(mbedtls_ssl_config *conf, int renegotiation)
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003130{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003131 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003132}
3133
Gilles Peskine449bd832023-01-11 14:50:10 +01003134void mbedtls_ssl_conf_renegotiation_enforced(mbedtls_ssl_config *conf, int max_records)
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003135{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003136 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003137}
3138
Gilles Peskine449bd832023-01-11 14:50:10 +01003139void mbedtls_ssl_conf_renegotiation_period(mbedtls_ssl_config *conf,
3140 const unsigned char period[8])
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01003141{
Gilles Peskine449bd832023-01-11 14:50:10 +01003142 memcpy(conf->renego_period, period, 8);
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01003143}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003144#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00003145
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003146#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003147#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003148void mbedtls_ssl_conf_session_tickets(mbedtls_ssl_config *conf, int use_tickets)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003149{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01003150 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003151}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003152#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02003153
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003154#if defined(MBEDTLS_SSL_SRV_C)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08003155
Jerry Yud0766ec2022-09-22 10:46:57 +08003156#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003157void mbedtls_ssl_conf_new_session_tickets(mbedtls_ssl_config *conf,
3158 uint16_t num_tickets)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08003159{
Jerry Yud0766ec2022-09-22 10:46:57 +08003160 conf->new_session_tickets_count = num_tickets;
Jerry Yu1ad7ace2022-08-09 13:28:39 +08003161}
3162#endif
3163
Gilles Peskine449bd832023-01-11 14:50:10 +01003164void mbedtls_ssl_conf_session_tickets_cb(mbedtls_ssl_config *conf,
3165 mbedtls_ssl_ticket_write_t *f_ticket_write,
3166 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
3167 void *p_ticket)
Paul Bakker606b4ba2013-08-14 16:52:14 +02003168{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02003169 conf->f_ticket_write = f_ticket_write;
3170 conf->f_ticket_parse = f_ticket_parse;
3171 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02003172}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003173#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003174#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003175
Gilles Peskine449bd832023-01-11 14:50:10 +01003176void mbedtls_ssl_set_export_keys_cb(mbedtls_ssl_context *ssl,
3177 mbedtls_ssl_export_keys_t *f_export_keys,
3178 void *p_export_keys)
Robert Cragie4feb7ae2015-10-02 13:33:37 +01003179{
Hanno Becker7e6c1782021-06-08 09:24:55 +01003180 ssl->f_export_keys = f_export_keys;
3181 ssl->p_export_keys = p_export_keys;
Ron Eldorf5cc10d2019-05-07 18:33:40 +03003182}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01003183
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003184#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003185void mbedtls_ssl_conf_async_private_cb(
3186 mbedtls_ssl_config *conf,
3187 mbedtls_ssl_async_sign_t *f_async_sign,
3188 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
3189 mbedtls_ssl_async_resume_t *f_async_resume,
3190 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskine449bd832023-01-11 14:50:10 +01003191 void *async_config_data)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003192{
3193 conf->f_async_sign_start = f_async_sign;
3194 conf->f_async_decrypt_start = f_async_decrypt;
3195 conf->f_async_resume = f_async_resume;
3196 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003197 conf->p_async_config_data = async_config_data;
3198}
3199
Gilles Peskine449bd832023-01-11 14:50:10 +01003200void *mbedtls_ssl_conf_get_async_config_data(const mbedtls_ssl_config *conf)
Gilles Peskine8f97af72018-04-26 11:46:10 +02003201{
Gilles Peskine449bd832023-01-11 14:50:10 +01003202 return conf->p_async_config_data;
Gilles Peskine8f97af72018-04-26 11:46:10 +02003203}
3204
Gilles Peskine449bd832023-01-11 14:50:10 +01003205void *mbedtls_ssl_get_async_operation_data(const mbedtls_ssl_context *ssl)
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003206{
Gilles Peskine449bd832023-01-11 14:50:10 +01003207 if (ssl->handshake == NULL) {
3208 return NULL;
3209 } else {
3210 return ssl->handshake->user_async_ctx;
3211 }
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003212}
3213
Gilles Peskine449bd832023-01-11 14:50:10 +01003214void mbedtls_ssl_set_async_operation_data(mbedtls_ssl_context *ssl,
3215 void *ctx)
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003216{
Gilles Peskine449bd832023-01-11 14:50:10 +01003217 if (ssl->handshake != NULL) {
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003218 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +01003219 }
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003220}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003221#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003222
Paul Bakker5121ce52009-01-03 21:22:43 +00003223/*
3224 * SSL get accessors
3225 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003226uint32_t mbedtls_ssl_get_verify_result(const mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00003227{
Gilles Peskine449bd832023-01-11 14:50:10 +01003228 if (ssl->session != NULL) {
3229 return ssl->session->verify_result;
3230 }
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00003231
Gilles Peskine449bd832023-01-11 14:50:10 +01003232 if (ssl->session_negotiate != NULL) {
3233 return ssl->session_negotiate->verify_result;
3234 }
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00003235
Gilles Peskine449bd832023-01-11 14:50:10 +01003236 return 0xFFFFFFFF;
Paul Bakker5121ce52009-01-03 21:22:43 +00003237}
3238
Gilles Peskine449bd832023-01-11 14:50:10 +01003239int mbedtls_ssl_get_ciphersuite_id_from_ssl(const mbedtls_ssl_context *ssl)
Glenn Strauss8f526902022-01-13 00:04:49 -05003240{
Gilles Peskine449bd832023-01-11 14:50:10 +01003241 if (ssl == NULL || ssl->session == NULL) {
3242 return 0;
3243 }
Glenn Strauss8f526902022-01-13 00:04:49 -05003244
Gilles Peskine449bd832023-01-11 14:50:10 +01003245 return ssl->session->ciphersuite;
Glenn Strauss8f526902022-01-13 00:04:49 -05003246}
3247
Gilles Peskine449bd832023-01-11 14:50:10 +01003248const char *mbedtls_ssl_get_ciphersuite(const mbedtls_ssl_context *ssl)
Paul Bakker72f62662011-01-16 21:27:44 +00003249{
Gilles Peskine449bd832023-01-11 14:50:10 +01003250 if (ssl == NULL || ssl->session == NULL) {
3251 return NULL;
3252 }
Paul Bakker926c8e42013-03-06 10:23:34 +01003253
Gilles Peskine449bd832023-01-11 14:50:10 +01003254 return mbedtls_ssl_get_ciphersuite_name(ssl->session->ciphersuite);
Paul Bakker72f62662011-01-16 21:27:44 +00003255}
3256
Gilles Peskine449bd832023-01-11 14:50:10 +01003257const char *mbedtls_ssl_get_version(const mbedtls_ssl_context *ssl)
Paul Bakker43ca69c2011-01-15 17:35:19 +00003258{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003259#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003260 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
3261 switch (ssl->tls_version) {
Glenn Strauss60bfe602022-03-14 19:04:24 -04003262 case MBEDTLS_SSL_VERSION_TLS1_2:
Gilles Peskine449bd832023-01-11 14:50:10 +01003263 return "DTLSv1.2";
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003264 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01003265 return "unknown (DTLS)";
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003266 }
3267 }
3268#endif
3269
Gilles Peskine449bd832023-01-11 14:50:10 +01003270 switch (ssl->tls_version) {
Glenn Strauss60bfe602022-03-14 19:04:24 -04003271 case MBEDTLS_SSL_VERSION_TLS1_2:
Gilles Peskine449bd832023-01-11 14:50:10 +01003272 return "TLSv1.2";
Glenn Strauss60bfe602022-03-14 19:04:24 -04003273 case MBEDTLS_SSL_VERSION_TLS1_3:
Gilles Peskine449bd832023-01-11 14:50:10 +01003274 return "TLSv1.3";
Paul Bakker43ca69c2011-01-15 17:35:19 +00003275 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01003276 return "unknown";
Paul Bakker43ca69c2011-01-15 17:35:19 +00003277 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00003278}
3279
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003280#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01003281size_t mbedtls_ssl_get_input_max_frag_len(const mbedtls_ssl_context *ssl)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003282{
David Horstmann95d516f2021-05-04 18:36:56 +01003283 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003284 size_t read_mfl;
3285
Jerry Yuddda0502022-12-01 19:43:12 +08003286#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003287 /* Use the configured MFL for the client if we're past SERVER_HELLO_DONE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003288 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
3289 ssl->state >= MBEDTLS_SSL_SERVER_HELLO_DONE) {
3290 return ssl_mfl_code_to_length(ssl->conf->mfl_code);
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003291 }
Jerry Yuddda0502022-12-01 19:43:12 +08003292#endif
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003293
3294 /* Check if a smaller max length was negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01003295 if (ssl->session_out != NULL) {
3296 read_mfl = ssl_mfl_code_to_length(ssl->session_out->mfl_code);
3297 if (read_mfl < max_len) {
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003298 max_len = read_mfl;
3299 }
3300 }
3301
Jerry Yuddda0502022-12-01 19:43:12 +08003302 /* During a handshake, use the value being negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01003303 if (ssl->session_negotiate != NULL) {
3304 read_mfl = ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code);
3305 if (read_mfl < max_len) {
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003306 max_len = read_mfl;
3307 }
3308 }
3309
Gilles Peskine449bd832023-01-11 14:50:10 +01003310 return max_len;
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003311}
3312
Gilles Peskine449bd832023-01-11 14:50:10 +01003313size_t mbedtls_ssl_get_output_max_frag_len(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003314{
3315 size_t max_len;
3316
3317 /*
3318 * Assume mfl_code is correct since it was checked when set
3319 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003320 max_len = ssl_mfl_code_to_length(ssl->conf->mfl_code);
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003321
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02003322 /* Check if a smaller max length was negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01003323 if (ssl->session_out != NULL &&
3324 ssl_mfl_code_to_length(ssl->session_out->mfl_code) < max_len) {
3325 max_len = ssl_mfl_code_to_length(ssl->session_out->mfl_code);
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003326 }
3327
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02003328 /* During a handshake, use the value being negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01003329 if (ssl->session_negotiate != NULL &&
3330 ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code) < max_len) {
3331 max_len = ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code);
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02003332 }
3333
Gilles Peskine449bd832023-01-11 14:50:10 +01003334 return max_len;
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003335}
3336#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
3337
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003338#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003339size_t mbedtls_ssl_get_current_mtu(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003340{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04003341 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003342 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
3343 (ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
3344 ssl->state == MBEDTLS_SSL_SERVER_HELLO)) {
3345 return 0;
3346 }
Andrzej Kurekef43ce62018-10-09 08:24:12 -04003347
Gilles Peskine449bd832023-01-11 14:50:10 +01003348 if (ssl->handshake == NULL || ssl->handshake->mtu == 0) {
3349 return ssl->mtu;
3350 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003351
Gilles Peskine449bd832023-01-11 14:50:10 +01003352 if (ssl->mtu == 0) {
3353 return ssl->handshake->mtu;
3354 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003355
Gilles Peskine449bd832023-01-11 14:50:10 +01003356 return ssl->mtu < ssl->handshake->mtu ?
3357 ssl->mtu : ssl->handshake->mtu;
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003358}
3359#endif /* MBEDTLS_SSL_PROTO_DTLS */
3360
Gilles Peskine449bd832023-01-11 14:50:10 +01003361int mbedtls_ssl_get_max_out_record_payload(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003362{
3363 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
3364
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02003365#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
3366 !defined(MBEDTLS_SSL_PROTO_DTLS)
3367 (void) ssl;
3368#endif
3369
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003370#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01003371 const size_t mfl = mbedtls_ssl_get_output_max_frag_len(ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003372
Gilles Peskine449bd832023-01-11 14:50:10 +01003373 if (max_len > mfl) {
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003374 max_len = mfl;
Gilles Peskine449bd832023-01-11 14:50:10 +01003375 }
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003376#endif
3377
3378#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003379 if (mbedtls_ssl_get_current_mtu(ssl) != 0) {
3380 const size_t mtu = mbedtls_ssl_get_current_mtu(ssl);
3381 const int ret = mbedtls_ssl_get_record_expansion(ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003382 const size_t overhead = (size_t) ret;
3383
Gilles Peskine449bd832023-01-11 14:50:10 +01003384 if (ret < 0) {
3385 return ret;
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003386 }
3387
Gilles Peskine449bd832023-01-11 14:50:10 +01003388 if (mtu <= overhead) {
3389 MBEDTLS_SSL_DEBUG_MSG(1, ("MTU too low for record expansion"));
3390 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
3391 }
3392
3393 if (max_len > mtu - overhead) {
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003394 max_len = mtu - overhead;
Gilles Peskine449bd832023-01-11 14:50:10 +01003395 }
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003396 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003397#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003398
Hanno Becker0defedb2018-08-10 12:35:02 +01003399#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
3400 !defined(MBEDTLS_SSL_PROTO_DTLS)
3401 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003402#endif
3403
Gilles Peskine449bd832023-01-11 14:50:10 +01003404 return (int) max_len;
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003405}
3406
Gilles Peskine449bd832023-01-11 14:50:10 +01003407int mbedtls_ssl_get_max_in_record_payload(const mbedtls_ssl_context *ssl)
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003408{
3409 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
3410
3411#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
3412 (void) ssl;
3413#endif
3414
3415#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01003416 const size_t mfl = mbedtls_ssl_get_input_max_frag_len(ssl);
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003417
Gilles Peskine449bd832023-01-11 14:50:10 +01003418 if (max_len > mfl) {
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003419 max_len = mfl;
Gilles Peskine449bd832023-01-11 14:50:10 +01003420 }
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003421#endif
3422
Gilles Peskine449bd832023-01-11 14:50:10 +01003423 return (int) max_len;
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003424}
3425
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003426#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003427const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert(const mbedtls_ssl_context *ssl)
Paul Bakkerb0550d92012-10-30 07:51:03 +00003428{
Gilles Peskine449bd832023-01-11 14:50:10 +01003429 if (ssl == NULL || ssl->session == NULL) {
3430 return NULL;
3431 }
Paul Bakkerb0550d92012-10-30 07:51:03 +00003432
Hanno Beckere6824572019-02-07 13:18:46 +00003433#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01003434 return ssl->session->peer_cert;
Hanno Beckere6824572019-02-07 13:18:46 +00003435#else
Gilles Peskine449bd832023-01-11 14:50:10 +01003436 return NULL;
Hanno Beckere6824572019-02-07 13:18:46 +00003437#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00003438}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003439#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00003440
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003441#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003442int mbedtls_ssl_get_session(const mbedtls_ssl_context *ssl,
3443 mbedtls_ssl_session *dst)
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003444{
Hanno Beckere810bbc2021-05-14 16:01:05 +01003445 int ret;
3446
Gilles Peskine449bd832023-01-11 14:50:10 +01003447 if (ssl == NULL ||
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003448 dst == NULL ||
3449 ssl->session == NULL ||
Gilles Peskine449bd832023-01-11 14:50:10 +01003450 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT) {
3451 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003452 }
3453
Hanno Beckere810bbc2021-05-14 16:01:05 +01003454 /* Since Mbed TLS 3.0, mbedtls_ssl_get_session() is no longer
3455 * idempotent: Each session can only be exported once.
3456 *
3457 * (This is in preparation for TLS 1.3 support where we will
3458 * need the ability to export multiple sessions (aka tickets),
3459 * which will be achieved by calling mbedtls_ssl_get_session()
3460 * multiple times until it fails.)
3461 *
3462 * Check whether we have already exported the current session,
3463 * and fail if so.
3464 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003465 if (ssl->session->exported == 1) {
3466 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
3467 }
Hanno Beckere810bbc2021-05-14 16:01:05 +01003468
Gilles Peskine449bd832023-01-11 14:50:10 +01003469 ret = mbedtls_ssl_session_copy(dst, ssl->session);
3470 if (ret != 0) {
3471 return ret;
3472 }
Hanno Beckere810bbc2021-05-14 16:01:05 +01003473
3474 /* Remember that we've exported the session. */
3475 ssl->session->exported = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003476 return 0;
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003477}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003478#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003479
Paul Bakker5121ce52009-01-03 21:22:43 +00003480/*
Hanno Beckera835da52019-05-16 12:39:07 +01003481 * Define ticket header determining Mbed TLS version
3482 * and structure of the ticket.
3483 */
3484
Hanno Becker94ef3b32019-05-16 12:50:45 +01003485/*
Hanno Becker50b59662019-05-28 14:30:45 +01003486 * Define bitflag determining compile-time settings influencing
3487 * structure of serialized SSL sessions.
Hanno Becker94ef3b32019-05-16 12:50:45 +01003488 */
3489
Hanno Becker50b59662019-05-28 14:30:45 +01003490#if defined(MBEDTLS_HAVE_TIME)
Hanno Becker3e088662019-05-29 11:10:18 +01003491#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker50b59662019-05-28 14:30:45 +01003492#else
Hanno Becker3e088662019-05-29 11:10:18 +01003493#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003494#endif /* MBEDTLS_HAVE_TIME */
3495
3496#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker3e088662019-05-29 11:10:18 +01003497#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003498#else
Hanno Becker3e088662019-05-29 11:10:18 +01003499#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003500#endif /* MBEDTLS_X509_CRT_PARSE_C */
3501
3502#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Becker3e088662019-05-29 11:10:18 +01003503#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003504#else
Hanno Becker3e088662019-05-29 11:10:18 +01003505#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003506#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
3507
3508#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Becker3e088662019-05-29 11:10:18 +01003509#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003510#else
Hanno Becker3e088662019-05-29 11:10:18 +01003511#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003512#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
3513
Hanno Becker94ef3b32019-05-16 12:50:45 +01003514#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3e088662019-05-29 11:10:18 +01003515#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003516#else
Hanno Becker3e088662019-05-29 11:10:18 +01003517#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003518#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
3519
Hanno Becker94ef3b32019-05-16 12:50:45 +01003520#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3521#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
3522#else
3523#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
3524#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3525
Hanno Becker3e088662019-05-29 11:10:18 +01003526#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
3527#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
3528#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
3529#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
Hanno Becker37bdbe62021-08-01 05:38:58 +01003530#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 4
3531#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 5
Hanno Becker3e088662019-05-29 11:10:18 +01003532
Hanno Becker50b59662019-05-28 14:30:45 +01003533#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Gilles Peskine449bd832023-01-11 14:50:10 +01003534 ((uint16_t) ( \
3535 (SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT) | \
3536 (SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT) | \
3537 (SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << \
3538 SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT) | \
3539 (SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT) | \
3540 (SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT) | \
3541 (SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT)))
Hanno Becker94ef3b32019-05-16 12:50:45 +01003542
Hanno Beckerf8787072019-05-16 12:41:07 +01003543static unsigned char ssl_serialized_session_header[] = {
Hanno Becker94ef3b32019-05-16 12:50:45 +01003544 MBEDTLS_VERSION_MAJOR,
3545 MBEDTLS_VERSION_MINOR,
3546 MBEDTLS_VERSION_PATCH,
Gilles Peskine449bd832023-01-11 14:50:10 +01003547 MBEDTLS_BYTE_1(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
3548 MBEDTLS_BYTE_0(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
Hanno Beckerf8787072019-05-16 12:41:07 +01003549};
Hanno Beckera835da52019-05-16 12:39:07 +01003550
3551/*
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003552 * Serialize a session in the following format:
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02003553 * (in the presentation language of TLS, RFC 8446 section 3)
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003554 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003555 * struct {
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01003556 *
Hanno Beckerdce50972021-08-01 05:39:23 +01003557 * opaque mbedtls_version[3]; // library version: major, minor, patch
3558 * opaque session_format[2]; // library-version specific 16-bit field
3559 * // determining the format of the remaining
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003560 * // serialized data.
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01003561 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003562 * Note: When updating the format, remember to keep
3563 * these version+format bytes.
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003564 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003565 * // In this version, `session_format` determines
3566 * // the setting of those compile-time
3567 * // configuration options which influence
3568 * // the structure of mbedtls_ssl_session.
3569 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003570 * uint8_t minor_ver; // Protocol minor version. Possible values:
Jerry Yu0c2a7382022-12-06 13:27:25 +08003571 * // - TLS 1.2 (0x0303)
3572 * // - TLS 1.3 (0x0304)
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003573 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003574 * select (serialized_session.tls_version) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003575 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003576 * case MBEDTLS_SSL_VERSION_TLS1_2:
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003577 * serialized_session_tls12 data;
Jerry Yu0c2a7382022-12-06 13:27:25 +08003578 * case MBEDTLS_SSL_VERSION_TLS1_3:
Jerry Yuddda0502022-12-01 19:43:12 +08003579 * serialized_session_tls13 data;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003580 *
3581 * };
3582 *
3583 * } serialized_session;
3584 *
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003585 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003586
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003587MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003588static int ssl_session_save(const mbedtls_ssl_session *session,
3589 unsigned char omit_header,
3590 unsigned char *buf,
3591 size_t buf_len,
3592 size_t *olen)
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003593{
3594 unsigned char *p = buf;
3595 size_t used = 0;
Jerry Yu251a12e2022-07-13 15:15:48 +08003596 size_t remaining_len;
Jerry Yue36fdd62022-08-17 21:31:36 +08003597#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3598 size_t out_len;
3599 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3600#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01003601 if (session == NULL) {
3602 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
3603 }
Jerry Yu438ddd82022-07-07 06:55:50 +00003604
Gilles Peskine449bd832023-01-11 14:50:10 +01003605 if (!omit_header) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003606 /*
3607 * Add Mbed TLS version identifier
3608 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003609 used += sizeof(ssl_serialized_session_header);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003610
Gilles Peskine449bd832023-01-11 14:50:10 +01003611 if (used <= buf_len) {
3612 memcpy(p, ssl_serialized_session_header,
3613 sizeof(ssl_serialized_session_header));
3614 p += sizeof(ssl_serialized_session_header);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003615 }
3616 }
3617
3618 /*
3619 * TLS version identifier
3620 */
3621 used += 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003622 if (used <= buf_len) {
3623 *p++ = MBEDTLS_BYTE_0(session->tls_version);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003624 }
3625
3626 /* Forward to version-specific serialization routine. */
Jerry Yufca4d572022-07-21 10:37:48 +08003627 remaining_len = (buf_len >= used) ? buf_len - used : 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003628 switch (session->tls_version) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003629#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01003630 case MBEDTLS_SSL_VERSION_TLS1_2:
3631 used += ssl_tls12_session_save(session, p, remaining_len);
3632 break;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003633#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3634
Jerry Yu251a12e2022-07-13 15:15:48 +08003635#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01003636 case MBEDTLS_SSL_VERSION_TLS1_3:
3637 ret = ssl_tls13_session_save(session, p, remaining_len, &out_len);
3638 if (ret != 0 && ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL) {
3639 return ret;
3640 }
3641 used += out_len;
3642 break;
Jerry Yu251a12e2022-07-13 15:15:48 +08003643#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3644
Gilles Peskine449bd832023-01-11 14:50:10 +01003645 default:
3646 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003647 }
3648
3649 *olen = used;
Gilles Peskine449bd832023-01-11 14:50:10 +01003650 if (used > buf_len) {
3651 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
3652 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003653
Gilles Peskine449bd832023-01-11 14:50:10 +01003654 return 0;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003655}
3656
3657/*
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003658 * Public wrapper for ssl_session_save()
3659 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003660int mbedtls_ssl_session_save(const mbedtls_ssl_session *session,
3661 unsigned char *buf,
3662 size_t buf_len,
3663 size_t *olen)
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003664{
Gilles Peskine449bd832023-01-11 14:50:10 +01003665 return ssl_session_save(session, 0, buf, buf_len, olen);
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003666}
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003667
Jerry Yuf1b23ca2022-02-18 11:48:47 +08003668/*
3669 * Deserialize session, see mbedtls_ssl_session_save() for format.
3670 *
3671 * This internal version is wrapped by a public function that cleans up in
3672 * case of error, and has an extra option omit_header.
3673 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003674MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003675static int ssl_session_load(mbedtls_ssl_session *session,
3676 unsigned char omit_header,
3677 const unsigned char *buf,
3678 size_t len)
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003679{
3680 const unsigned char *p = buf;
3681 const unsigned char * const end = buf + len;
Jerry Yu438ddd82022-07-07 06:55:50 +00003682 size_t remaining_len;
3683
3684
Gilles Peskine449bd832023-01-11 14:50:10 +01003685 if (session == NULL) {
3686 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
3687 }
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003688
Gilles Peskine449bd832023-01-11 14:50:10 +01003689 if (!omit_header) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003690 /*
3691 * Check Mbed TLS version identifier
3692 */
3693
Gilles Peskine449bd832023-01-11 14:50:10 +01003694 if ((size_t) (end - p) < sizeof(ssl_serialized_session_header)) {
3695 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003696 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003697
3698 if (memcmp(p, ssl_serialized_session_header,
3699 sizeof(ssl_serialized_session_header)) != 0) {
3700 return MBEDTLS_ERR_SSL_VERSION_MISMATCH;
3701 }
3702 p += sizeof(ssl_serialized_session_header);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003703 }
3704
3705 /*
3706 * TLS version identifier
3707 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003708 if (1 > (size_t) (end - p)) {
3709 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3710 }
Glenn Straussda7851c2022-03-14 13:29:48 -04003711 session->tls_version = 0x0300 | *p++;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003712
3713 /* Dispatch according to TLS version. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003714 remaining_len = (end - p);
3715 switch (session->tls_version) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003716#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01003717 case MBEDTLS_SSL_VERSION_TLS1_2:
3718 return ssl_tls12_session_load(session, p, remaining_len);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003719#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3720
Jerry Yu438ddd82022-07-07 06:55:50 +00003721#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01003722 case MBEDTLS_SSL_VERSION_TLS1_3:
3723 return ssl_tls13_session_load(session, p, remaining_len);
Jerry Yu438ddd82022-07-07 06:55:50 +00003724#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3725
Gilles Peskine449bd832023-01-11 14:50:10 +01003726 default:
3727 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003728 }
3729}
3730
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003731/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02003732 * Deserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003733 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003734int mbedtls_ssl_session_load(mbedtls_ssl_session *session,
3735 const unsigned char *buf,
3736 size_t len)
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003737{
Gilles Peskine449bd832023-01-11 14:50:10 +01003738 int ret = ssl_session_load(session, 0, buf, len);
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003739
Gilles Peskine449bd832023-01-11 14:50:10 +01003740 if (ret != 0) {
3741 mbedtls_ssl_session_free(session);
3742 }
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003743
Gilles Peskine449bd832023-01-11 14:50:10 +01003744 return ret;
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003745}
3746
3747/*
Paul Bakker1961b702013-01-25 14:49:24 +01003748 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00003749 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003750MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003751static int ssl_prepare_handshake_step(mbedtls_ssl_context *ssl)
Hanno Becker41934dd2021-08-07 19:13:43 +01003752{
3753 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3754
Ronald Cron66dbf912022-02-02 15:33:46 +01003755 /*
3756 * We may have not been able to send to the peer all the handshake data
Ronald Cron3f20b772022-03-08 16:00:02 +01003757 * that were written into the output buffer by the previous handshake step,
3758 * if the write to the network callback returned with the
Ronald Cron66dbf912022-02-02 15:33:46 +01003759 * #MBEDTLS_ERR_SSL_WANT_WRITE error code.
3760 * We proceed to the next handshake step only when all data from the
3761 * previous one have been sent to the peer, thus we make sure that this is
3762 * the case here by calling `mbedtls_ssl_flush_output()`. The function may
3763 * return with the #MBEDTLS_ERR_SSL_WANT_WRITE error code in which case
3764 * we have to wait before to go ahead.
3765 * In the case of TLS 1.3, handshake step handlers do not send data to the
3766 * peer. Data are only sent here and through
3767 * `mbedtls_ssl_handle_pending_alert` in case an error that triggered an
Andrzej Kurek5c65c572022-04-13 14:28:52 -04003768 * alert occurred.
Ronald Cron66dbf912022-02-02 15:33:46 +01003769 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003770 if ((ret = mbedtls_ssl_flush_output(ssl)) != 0) {
3771 return ret;
3772 }
Hanno Becker41934dd2021-08-07 19:13:43 +01003773
3774#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003775 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3776 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING) {
3777 if ((ret = mbedtls_ssl_flight_transmit(ssl)) != 0) {
3778 return ret;
3779 }
Hanno Becker41934dd2021-08-07 19:13:43 +01003780 }
3781#endif /* MBEDTLS_SSL_PROTO_DTLS */
3782
Gilles Peskine449bd832023-01-11 14:50:10 +01003783 return ret;
Hanno Becker41934dd2021-08-07 19:13:43 +01003784}
3785
Gilles Peskine449bd832023-01-11 14:50:10 +01003786int mbedtls_ssl_handshake_step(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00003787{
Hanno Becker41934dd2021-08-07 19:13:43 +01003788 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003789
Gilles Peskine449bd832023-01-11 14:50:10 +01003790 if (ssl == NULL ||
Hanno Becker41934dd2021-08-07 19:13:43 +01003791 ssl->conf == NULL ||
3792 ssl->handshake == NULL ||
Gilles Peskine449bd832023-01-11 14:50:10 +01003793 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER) {
3794 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Becker41934dd2021-08-07 19:13:43 +01003795 }
3796
Gilles Peskine449bd832023-01-11 14:50:10 +01003797 ret = ssl_prepare_handshake_step(ssl);
3798 if (ret != 0) {
3799 return ret;
3800 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003801
Gilles Peskine449bd832023-01-11 14:50:10 +01003802 ret = mbedtls_ssl_handle_pending_alert(ssl);
3803 if (ret != 0) {
Jerry Yue7047812021-09-13 19:26:39 +08003804 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01003805 }
Jerry Yue7047812021-09-13 19:26:39 +08003806
Tom Cosgrove2fdc7b32022-09-21 12:33:17 +01003807 /* If ssl->conf->endpoint is not one of MBEDTLS_SSL_IS_CLIENT or
3808 * MBEDTLS_SSL_IS_SERVER, this is the return code we give */
3809 ret = MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3810
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003811#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003812 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
3813 MBEDTLS_SSL_DEBUG_MSG(2, ("client state: %s",
3814 mbedtls_ssl_states_str(ssl->state)));
Jerry Yub9930e72021-08-06 17:11:51 +08003815
Gilles Peskine449bd832023-01-11 14:50:10 +01003816 switch (ssl->state) {
Ronald Cron9f0fba32022-02-10 16:45:15 +01003817 case MBEDTLS_SSL_HELLO_REQUEST:
3818 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
Tom Cosgrove87d9c6c2022-09-22 09:27:56 +01003819 ret = 0;
Ronald Cron9f0fba32022-02-10 16:45:15 +01003820 break;
Jerry Yub9930e72021-08-06 17:11:51 +08003821
Ronald Cron9f0fba32022-02-10 16:45:15 +01003822 case MBEDTLS_SSL_CLIENT_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01003823 ret = mbedtls_ssl_write_client_hello(ssl);
Ronald Cron9f0fba32022-02-10 16:45:15 +01003824 break;
3825
3826 default:
3827#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01003828 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
3829 ret = mbedtls_ssl_tls13_handshake_client_step(ssl);
3830 } else {
3831 ret = mbedtls_ssl_handshake_client_step(ssl);
3832 }
Ronald Cron9f0fba32022-02-10 16:45:15 +01003833#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01003834 ret = mbedtls_ssl_handshake_client_step(ssl);
Ronald Cron9f0fba32022-02-10 16:45:15 +01003835#else
Gilles Peskine449bd832023-01-11 14:50:10 +01003836 ret = mbedtls_ssl_tls13_handshake_client_step(ssl);
Ronald Cron9f0fba32022-02-10 16:45:15 +01003837#endif
3838 }
Jerry Yub9930e72021-08-06 17:11:51 +08003839 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003840#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003841#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003842 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Ronald Cron6f135e12021-12-08 16:57:54 +01003843#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01003844 if (mbedtls_ssl_conf_is_tls13_only(ssl->conf)) {
3845 ret = mbedtls_ssl_tls13_handshake_server_step(ssl);
3846 }
Ronald Cron6f135e12021-12-08 16:57:54 +01003847#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yub9930e72021-08-06 17:11:51 +08003848
3849#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01003850 if (mbedtls_ssl_conf_is_tls12_only(ssl->conf)) {
3851 ret = mbedtls_ssl_handshake_server_step(ssl);
3852 }
Jerry Yub9930e72021-08-06 17:11:51 +08003853#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3854 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003855#endif
3856
Gilles Peskine449bd832023-01-11 14:50:10 +01003857 if (ret != 0) {
Jerry Yubbd5a3f2021-09-18 20:50:22 +08003858 /* handshake_step return error. And it is same
3859 * with alert_reason.
3860 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003861 if (ssl->send_alert) {
3862 ret = mbedtls_ssl_handle_pending_alert(ssl);
Jerry Yue7047812021-09-13 19:26:39 +08003863 goto cleanup;
3864 }
3865 }
3866
3867cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01003868 return ret;
Paul Bakker1961b702013-01-25 14:49:24 +01003869}
3870
3871/*
3872 * Perform the SSL handshake
3873 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003874int mbedtls_ssl_handshake(mbedtls_ssl_context *ssl)
Paul Bakker1961b702013-01-25 14:49:24 +01003875{
3876 int ret = 0;
3877
Hanno Beckera817ea42020-10-20 15:20:23 +01003878 /* Sanity checks */
3879
Gilles Peskine449bd832023-01-11 14:50:10 +01003880 if (ssl == NULL || ssl->conf == NULL) {
3881 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3882 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003883
Hanno Beckera817ea42020-10-20 15:20:23 +01003884#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003885 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3886 (ssl->f_set_timer == NULL || ssl->f_get_timer == NULL)) {
3887 MBEDTLS_SSL_DEBUG_MSG(1, ("You must use "
3888 "mbedtls_ssl_set_timer_cb() for DTLS"));
3889 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Beckera817ea42020-10-20 15:20:23 +01003890 }
3891#endif /* MBEDTLS_SSL_PROTO_DTLS */
3892
Gilles Peskine449bd832023-01-11 14:50:10 +01003893 MBEDTLS_SSL_DEBUG_MSG(2, ("=> handshake"));
Paul Bakker1961b702013-01-25 14:49:24 +01003894
Hanno Beckera817ea42020-10-20 15:20:23 +01003895 /* Main handshake loop */
Gilles Peskine449bd832023-01-11 14:50:10 +01003896 while (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER) {
3897 ret = mbedtls_ssl_handshake_step(ssl);
Paul Bakker1961b702013-01-25 14:49:24 +01003898
Gilles Peskine449bd832023-01-11 14:50:10 +01003899 if (ret != 0) {
Paul Bakker1961b702013-01-25 14:49:24 +01003900 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01003901 }
Paul Bakker1961b702013-01-25 14:49:24 +01003902 }
3903
Gilles Peskine449bd832023-01-11 14:50:10 +01003904 MBEDTLS_SSL_DEBUG_MSG(2, ("<= handshake"));
Paul Bakker5121ce52009-01-03 21:22:43 +00003905
Gilles Peskine449bd832023-01-11 14:50:10 +01003906 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003907}
3908
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003909#if defined(MBEDTLS_SSL_RENEGOTIATION)
3910#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003911/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003912 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00003913 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003914MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003915static int ssl_write_hello_request(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003916{
Janos Follath865b3eb2019-12-16 11:46:15 +00003917 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003918
Gilles Peskine449bd832023-01-11 14:50:10 +01003919 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write hello request"));
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003920
3921 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003922 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3923 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003924
Gilles Peskine449bd832023-01-11 14:50:10 +01003925 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
3926 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
3927 return ret;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003928 }
3929
Gilles Peskine449bd832023-01-11 14:50:10 +01003930 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write hello request"));
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003931
Gilles Peskine449bd832023-01-11 14:50:10 +01003932 return 0;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003933}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003934#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003935
3936/*
3937 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003938 * - any side: calling mbedtls_ssl_renegotiate(),
3939 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
3940 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02003941 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003942 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003943 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003944 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003945int mbedtls_ssl_start_renegotiation(mbedtls_ssl_context *ssl)
Paul Bakker48916f92012-09-16 19:57:18 +00003946{
Janos Follath865b3eb2019-12-16 11:46:15 +00003947 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker48916f92012-09-16 19:57:18 +00003948
Gilles Peskine449bd832023-01-11 14:50:10 +01003949 MBEDTLS_SSL_DEBUG_MSG(2, ("=> renegotiate"));
Paul Bakker48916f92012-09-16 19:57:18 +00003950
Gilles Peskine449bd832023-01-11 14:50:10 +01003951 if ((ret = ssl_handshake_init(ssl)) != 0) {
3952 return ret;
3953 }
Paul Bakker48916f92012-09-16 19:57:18 +00003954
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003955 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
3956 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003957#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003958 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3959 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING) {
3960 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003961 ssl->handshake->out_msg_seq = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003962 } else {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003963 ssl->handshake->in_msg_seq = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003964 }
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003965 }
3966#endif
3967
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003968 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
3969 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00003970
Gilles Peskine449bd832023-01-11 14:50:10 +01003971 if ((ret = mbedtls_ssl_handshake(ssl)) != 0) {
3972 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_handshake", ret);
3973 return ret;
Paul Bakker48916f92012-09-16 19:57:18 +00003974 }
3975
Gilles Peskine449bd832023-01-11 14:50:10 +01003976 MBEDTLS_SSL_DEBUG_MSG(2, ("<= renegotiate"));
Paul Bakker48916f92012-09-16 19:57:18 +00003977
Gilles Peskine449bd832023-01-11 14:50:10 +01003978 return 0;
Paul Bakker48916f92012-09-16 19:57:18 +00003979}
3980
3981/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003982 * Renegotiate current connection on client,
3983 * or request renegotiation on server
3984 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003985int mbedtls_ssl_renegotiate(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003986{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003987 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003988
Gilles Peskine449bd832023-01-11 14:50:10 +01003989 if (ssl == NULL || ssl->conf == NULL) {
3990 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3991 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003992
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003993#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003994 /* On server, just send the request */
Gilles Peskine449bd832023-01-11 14:50:10 +01003995 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
3996 if (mbedtls_ssl_is_handshake_over(ssl) == 0) {
3997 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3998 }
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003999
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004000 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02004001
4002 /* Did we already try/start sending HelloRequest? */
Gilles Peskine449bd832023-01-11 14:50:10 +01004003 if (ssl->out_left != 0) {
4004 return mbedtls_ssl_flush_output(ssl);
4005 }
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02004006
Gilles Peskine449bd832023-01-11 14:50:10 +01004007 return ssl_write_hello_request(ssl);
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004008 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004009#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004010
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004011#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004012 /*
4013 * On client, either start the renegotiation process or,
4014 * if already in progress, continue the handshake
4015 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004016 if (ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
4017 if (mbedtls_ssl_is_handshake_over(ssl) == 0) {
4018 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004019 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004020
4021 if ((ret = mbedtls_ssl_start_renegotiation(ssl)) != 0) {
4022 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_start_renegotiation", ret);
4023 return ret;
4024 }
4025 } else {
4026 if ((ret = mbedtls_ssl_handshake(ssl)) != 0) {
4027 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_handshake", ret);
4028 return ret;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004029 }
4030 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004031#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004032
Gilles Peskine449bd832023-01-11 14:50:10 +01004033 return ret;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004034}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004035#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004036
Gilles Peskine449bd832023-01-11 14:50:10 +01004037void mbedtls_ssl_handshake_free(mbedtls_ssl_context *ssl)
Paul Bakker48916f92012-09-16 19:57:18 +00004038{
Gilles Peskine9b562d52018-04-25 20:32:43 +02004039 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
4040
Gilles Peskine449bd832023-01-11 14:50:10 +01004041 if (handshake == NULL) {
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004042 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01004043 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004044
Brett Warrene0edc842021-08-17 09:53:13 +01004045#if defined(MBEDTLS_ECP_C)
4046#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01004047 if (ssl->handshake->group_list_heap_allocated) {
4048 mbedtls_free((void *) handshake->group_list);
4049 }
Brett Warrene0edc842021-08-17 09:53:13 +01004050 handshake->group_list = NULL;
4051#endif /* MBEDTLS_DEPRECATED_REMOVED */
4052#endif /* MBEDTLS_ECP_C */
4053
Ronald Crone68ab4f2022-10-05 12:46:29 +02004054#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +08004055#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01004056 if (ssl->handshake->sig_algs_heap_allocated) {
4057 mbedtls_free((void *) handshake->sig_algs);
4058 }
Jerry Yuf017ee42022-01-12 15:49:48 +08004059 handshake->sig_algs = NULL;
4060#endif /* MBEDTLS_DEPRECATED_REMOVED */
Xiaofei Baic234ecf2022-02-08 09:59:23 +00004061#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01004062 if (ssl->handshake->certificate_request_context) {
4063 mbedtls_free((void *) handshake->certificate_request_context);
Xiaofei Baic234ecf2022-02-08 09:59:23 +00004064 }
4065#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Ronald Crone68ab4f2022-10-05 12:46:29 +02004066#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yuf017ee42022-01-12 15:49:48 +08004067
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02004068#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01004069 if (ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0) {
4070 ssl->conf->f_async_cancel(ssl);
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02004071 handshake->async_in_progress = 0;
4072 }
4073#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
4074
Andrzej Kurek25f27152022-08-17 16:09:31 -04004075#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -05004076#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01004077 psa_hash_abort(&handshake->fin_sha256_psa);
Andrzej Kurekeb342242019-01-29 09:14:33 -05004078#else
Gilles Peskine449bd832023-01-11 14:50:10 +01004079 mbedtls_sha256_free(&handshake->fin_sha256);
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02004080#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05004081#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004082#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -05004083#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01004084 psa_hash_abort(&handshake->fin_sha384_psa);
Andrzej Kurekeb342242019-01-29 09:14:33 -05004085#else
Gilles Peskine449bd832023-01-11 14:50:10 +01004086 mbedtls_sha512_free(&handshake->fin_sha384);
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02004087#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05004088#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02004089
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004090#if defined(MBEDTLS_DHM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004091 mbedtls_dhm_free(&handshake->dhm_ctx);
Paul Bakker48916f92012-09-16 19:57:18 +00004092#endif
Neil Armstrongf3f46412022-04-12 14:43:39 +02004093#if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004094 mbedtls_ecdh_free(&handshake->ecdh_ctx);
Paul Bakker61d113b2013-07-04 11:51:43 +02004095#endif
Valerio Setti02c25b52022-11-15 14:08:42 +01004096
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02004097#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Neil Armstrongca7d5062022-05-31 14:43:23 +02004098#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01004099 psa_pake_abort(&handshake->psa_pake_ctx);
Valerio Settieb3f7882022-12-08 18:42:58 +01004100 /*
4101 * Opaque keys are not stored in the handshake's data and it's the user
4102 * responsibility to destroy them. Clear ones, instead, are created by
4103 * the TLS library and should be destroyed at the same level
4104 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004105 if (!mbedtls_svc_key_id_is_null(handshake->psa_pake_password)) {
4106 psa_destroy_key(handshake->psa_pake_password);
Valerio Settieb3f7882022-12-08 18:42:58 +01004107 }
Neil Armstrongca7d5062022-05-31 14:43:23 +02004108 handshake->psa_pake_password = MBEDTLS_SVC_KEY_ID_INIT;
4109#else
Gilles Peskine449bd832023-01-11 14:50:10 +01004110 mbedtls_ecjpake_free(&handshake->ecjpake_ctx);
Neil Armstrongca7d5062022-05-31 14:43:23 +02004111#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02004112#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004113 mbedtls_free(handshake->ecjpake_cache);
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02004114 handshake->ecjpake_cache = NULL;
4115 handshake->ecjpake_cache_len = 0;
4116#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02004117#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02004118
Janos Follath4ae5c292016-02-10 11:27:43 +00004119#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
4120 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02004121 /* explicit void pointer cast for buggy MS compiler */
Gilles Peskine449bd832023-01-11 14:50:10 +01004122 mbedtls_free((void *) handshake->curves_tls_id);
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004123#endif
4124
Ronald Cron73fe8df2022-10-05 14:31:43 +02004125#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02004126#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01004127 if (!mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque)) {
Neil Armstrong501c9322022-05-03 09:35:09 +02004128 /* The maintenance of the external PSK key slot is the
4129 * user's responsibility. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004130 if (ssl->handshake->psk_opaque_is_internal) {
4131 psa_destroy_key(ssl->handshake->psk_opaque);
Neil Armstrong501c9322022-05-03 09:35:09 +02004132 ssl->handshake->psk_opaque_is_internal = 0;
4133 }
4134 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
4135 }
Neil Armstronge952a302022-05-03 10:22:14 +02004136#else
Gilles Peskine449bd832023-01-11 14:50:10 +01004137 if (handshake->psk != NULL) {
4138 mbedtls_platform_zeroize(handshake->psk, handshake->psk_len);
4139 mbedtls_free(handshake->psk);
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004140 }
Neil Armstronge952a302022-05-03 10:22:14 +02004141#endif /* MBEDTLS_USE_PSA_CRYPTO */
Ronald Cron73fe8df2022-10-05 14:31:43 +02004142#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004143
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004144#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
4145 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02004146 /*
4147 * Free only the linked list wrapper, not the keys themselves
4148 * since the belong to the SNI callback
4149 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004150 ssl_key_cert_free(handshake->sni_key_cert);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004151#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004152
Gilles Peskineeccd8882020-03-10 12:19:08 +01004153#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01004154 mbedtls_x509_crt_restart_free(&handshake->ecrs_ctx);
4155 if (handshake->ecrs_peer_cert != NULL) {
4156 mbedtls_x509_crt_free(handshake->ecrs_peer_cert);
4157 mbedtls_free(handshake->ecrs_peer_cert);
Hanno Becker3dad3112019-02-05 17:19:52 +00004158 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02004159#endif
4160
Hanno Becker75173122019-02-06 16:18:31 +00004161#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
4162 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01004163 mbedtls_pk_free(&handshake->peer_pubkey);
Hanno Becker75173122019-02-06 16:18:31 +00004164#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
4165
XiaokangQian9b93c0d2022-02-09 06:02:25 +00004166#if defined(MBEDTLS_SSL_CLI_C) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01004167 (defined(MBEDTLS_SSL_PROTO_DTLS) || defined(MBEDTLS_SSL_PROTO_TLS1_3))
4168 mbedtls_free(handshake->cookie);
XiaokangQian9b93c0d2022-02-09 06:02:25 +00004169#endif /* MBEDTLS_SSL_CLI_C &&
4170 ( MBEDTLS_SSL_PROTO_DTLS || MBEDTLS_SSL_PROTO_TLS1_3 ) */
XiaokangQian8499b6c2022-01-27 09:00:11 +00004171
4172#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004173 mbedtls_ssl_flight_free(handshake->flight);
4174 mbedtls_ssl_buffering_free(ssl);
XiaokangQian8499b6c2022-01-27 09:00:11 +00004175#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02004176
Ronald Cronf12b81d2022-03-15 10:42:41 +01004177#if defined(MBEDTLS_ECDH_C) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01004178 (defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3))
4179 if (handshake->ecdh_psa_privkey_is_external == 0) {
4180 psa_destroy_key(handshake->ecdh_psa_privkey);
4181 }
Hanno Becker4a63ed42019-01-08 11:39:35 +00004182#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
4183
Ronald Cron6f135e12021-12-08 16:57:54 +01004184#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01004185 mbedtls_ssl_transform_free(handshake->transform_handshake);
4186 mbedtls_free(handshake->transform_handshake);
Jerry Yu3d9b5902022-11-04 14:07:25 +08004187#if defined(MBEDTLS_SSL_EARLY_DATA)
Gilles Peskine449bd832023-01-11 14:50:10 +01004188 mbedtls_ssl_transform_free(handshake->transform_earlydata);
4189 mbedtls_free(handshake->transform_earlydata);
Jerry Yu3d9b5902022-11-04 14:07:25 +08004190#endif
Ronald Cron6f135e12021-12-08 16:57:54 +01004191#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yuba9c7272021-10-30 11:54:10 +08004192
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004193
4194#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4195 /* If the buffers are too big - reallocate. Because of the way Mbed TLS
4196 * processes datagrams and the fact that a datagram is allowed to have
4197 * several records in it, it is possible that the I/O buffers are not
4198 * empty at this stage */
Gilles Peskine449bd832023-01-11 14:50:10 +01004199 handle_buffer_resizing(ssl, 1, mbedtls_ssl_get_input_buflen(ssl),
4200 mbedtls_ssl_get_output_buflen(ssl));
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004201#endif
Hanno Becker3aa186f2021-08-10 09:24:19 +01004202
Jerry Yuba9c7272021-10-30 11:54:10 +08004203 /* mbedtls_platform_zeroize MUST be last one in this function */
Gilles Peskine449bd832023-01-11 14:50:10 +01004204 mbedtls_platform_zeroize(handshake,
4205 sizeof(mbedtls_ssl_handshake_params));
Paul Bakker48916f92012-09-16 19:57:18 +00004206}
4207
Gilles Peskine449bd832023-01-11 14:50:10 +01004208void mbedtls_ssl_session_free(mbedtls_ssl_session *session)
Paul Bakker48916f92012-09-16 19:57:18 +00004209{
Gilles Peskine449bd832023-01-11 14:50:10 +01004210 if (session == NULL) {
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004211 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01004212 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004213
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004214#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004215 ssl_clear_peer_cert(session);
Paul Bakkered27a042013-04-18 22:46:23 +02004216#endif
Paul Bakker0a597072012-09-25 21:55:46 +00004217
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02004218#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Xiaokang Qianbc663a02022-10-09 11:14:39 +00004219#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
4220 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01004221 mbedtls_free(session->hostname);
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00004222#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01004223 mbedtls_free(session->ticket);
Paul Bakkera503a632013-08-14 13:48:06 +02004224#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004225
Gilles Peskine449bd832023-01-11 14:50:10 +01004226 mbedtls_platform_zeroize(session, sizeof(mbedtls_ssl_session));
Paul Bakker48916f92012-09-16 19:57:18 +00004227}
4228
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02004229#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004230
4231#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4232#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u
4233#else
4234#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u
4235#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4236
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004237#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004238
4239#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4240#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u
4241#else
4242#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u
4243#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4244
4245#if defined(MBEDTLS_SSL_ALPN)
4246#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u
4247#else
4248#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u
4249#endif /* MBEDTLS_SSL_ALPN */
4250
4251#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0
4252#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1
4253#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2
4254#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3
4255
4256#define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \
Gilles Peskine449bd832023-01-11 14:50:10 +01004257 ((uint32_t) ( \
4258 (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << \
4259 SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT) | \
4260 (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << \
4261 SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT) | \
4262 (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << \
4263 SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT) | \
4264 (SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT) | \
4265 0u))
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004266
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004267static unsigned char ssl_serialized_context_header[] = {
4268 MBEDTLS_VERSION_MAJOR,
4269 MBEDTLS_VERSION_MINOR,
4270 MBEDTLS_VERSION_PATCH,
Gilles Peskine449bd832023-01-11 14:50:10 +01004271 MBEDTLS_BYTE_1(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
4272 MBEDTLS_BYTE_0(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
4273 MBEDTLS_BYTE_2(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG),
4274 MBEDTLS_BYTE_1(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG),
4275 MBEDTLS_BYTE_0(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG),
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004276};
4277
Paul Bakker5121ce52009-01-03 21:22:43 +00004278/*
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004279 * Serialize a full SSL context
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02004280 *
4281 * The format of the serialized data is:
4282 * (in the presentation language of TLS, RFC 8446 section 3)
4283 *
4284 * // header
4285 * opaque mbedtls_version[3]; // major, minor, patch
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004286 * opaque context_format[5]; // version-specific field determining
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02004287 * // the format of the remaining
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004288 * // serialized data.
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004289 * Note: When updating the format, remember to keep these
4290 * version+format bytes. (We may make their size part of the API.)
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02004291 *
4292 * // session sub-structure
4293 * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save()
4294 * // transform sub-structure
4295 * uint8 random[64]; // ServerHello.random+ClientHello.random
4296 * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value
4297 * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use
4298 * // fields from ssl_context
4299 * uint32 badmac_seen; // DTLS: number of records with failing MAC
4300 * uint64 in_window_top; // DTLS: last validated record seq_num
4301 * uint64 in_window; // DTLS: bitmask for replay protection
4302 * uint8 disable_datagram_packing; // DTLS: only one record per datagram
4303 * uint64 cur_out_ctr; // Record layer: outgoing sequence number
4304 * uint16 mtu; // DTLS: path mtu (max outgoing fragment size)
4305 * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol
4306 *
4307 * Note that many fields of the ssl_context or sub-structures are not
4308 * serialized, as they fall in one of the following categories:
4309 *
4310 * 1. forced value (eg in_left must be 0)
4311 * 2. pointer to dynamically-allocated memory (eg session, transform)
4312 * 3. value can be re-derived from other data (eg session keys from MS)
4313 * 4. value was temporary (eg content of input buffer)
4314 * 5. value will be provided by the user again (eg I/O callbacks and context)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004315 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004316int mbedtls_ssl_context_save(mbedtls_ssl_context *ssl,
4317 unsigned char *buf,
4318 size_t buf_len,
4319 size_t *olen)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004320{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004321 unsigned char *p = buf;
4322 size_t used = 0;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004323 size_t session_len;
4324 int ret = 0;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004325
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02004326 /*
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004327 * Enforce usage restrictions, see "return BAD_INPUT_DATA" in
4328 * this function's documentation.
4329 *
4330 * These are due to assumptions/limitations in the implementation. Some of
4331 * them are likely to stay (no handshake in progress) some might go away
4332 * (only DTLS) but are currently used to simplify the implementation.
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02004333 */
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004334 /* The initial handshake must be over */
Gilles Peskine449bd832023-01-11 14:50:10 +01004335 if (mbedtls_ssl_is_handshake_over(ssl) == 0) {
4336 MBEDTLS_SSL_DEBUG_MSG(1, ("Initial handshake isn't over"));
4337 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004338 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004339 if (ssl->handshake != NULL) {
4340 MBEDTLS_SSL_DEBUG_MSG(1, ("Handshake isn't completed"));
4341 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004342 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004343 /* Double-check that sub-structures are indeed ready */
Gilles Peskine449bd832023-01-11 14:50:10 +01004344 if (ssl->transform == NULL || ssl->session == NULL) {
4345 MBEDTLS_SSL_DEBUG_MSG(1, ("Serialised structures aren't ready"));
4346 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004347 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004348 /* There must be no pending incoming or outgoing data */
Gilles Peskine449bd832023-01-11 14:50:10 +01004349 if (mbedtls_ssl_check_pending(ssl) != 0) {
4350 MBEDTLS_SSL_DEBUG_MSG(1, ("There is pending incoming data"));
4351 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004352 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004353 if (ssl->out_left != 0) {
4354 MBEDTLS_SSL_DEBUG_MSG(1, ("There is pending outgoing data"));
4355 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004356 }
Dave Rodgman556e8a32022-12-06 16:31:25 +00004357 /* Protocol must be DTLS, not TLS */
Gilles Peskine449bd832023-01-11 14:50:10 +01004358 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
4359 MBEDTLS_SSL_DEBUG_MSG(1, ("Only DTLS is supported"));
4360 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004361 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004362 /* Version must be 1.2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004363 if (ssl->tls_version != MBEDTLS_SSL_VERSION_TLS1_2) {
4364 MBEDTLS_SSL_DEBUG_MSG(1, ("Only version 1.2 supported"));
4365 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004366 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004367 /* We must be using an AEAD ciphersuite */
Gilles Peskine449bd832023-01-11 14:50:10 +01004368 if (mbedtls_ssl_transform_uses_aead(ssl->transform) != 1) {
4369 MBEDTLS_SSL_DEBUG_MSG(1, ("Only AEAD ciphersuites supported"));
4370 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004371 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004372 /* Renegotiation must not be enabled */
4373#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01004374 if (ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED) {
4375 MBEDTLS_SSL_DEBUG_MSG(1, ("Renegotiation must not be enabled"));
4376 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004377 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004378#endif
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004379
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004380 /*
4381 * Version and format identifier
4382 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004383 used += sizeof(ssl_serialized_context_header);
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004384
Gilles Peskine449bd832023-01-11 14:50:10 +01004385 if (used <= buf_len) {
4386 memcpy(p, ssl_serialized_context_header,
4387 sizeof(ssl_serialized_context_header));
4388 p += sizeof(ssl_serialized_context_header);
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004389 }
4390
4391 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004392 * Session (length + data)
4393 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004394 ret = ssl_session_save(ssl->session, 1, NULL, 0, &session_len);
4395 if (ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL) {
4396 return ret;
4397 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004398
4399 used += 4 + session_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004400 if (used <= buf_len) {
4401 MBEDTLS_PUT_UINT32_BE(session_len, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004402 p += 4;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004403
Gilles Peskine449bd832023-01-11 14:50:10 +01004404 ret = ssl_session_save(ssl->session, 1,
4405 p, session_len, &session_len);
4406 if (ret != 0) {
4407 return ret;
4408 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004409
4410 p += session_len;
4411 }
4412
4413 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004414 * Transform
4415 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004416 used += sizeof(ssl->transform->randbytes);
4417 if (used <= buf_len) {
4418 memcpy(p, ssl->transform->randbytes,
4419 sizeof(ssl->transform->randbytes));
4420 p += sizeof(ssl->transform->randbytes);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004421 }
4422
4423#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4424 used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004425 if (used <= buf_len) {
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004426 *p++ = ssl->transform->in_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004427 memcpy(p, ssl->transform->in_cid, ssl->transform->in_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004428 p += ssl->transform->in_cid_len;
4429
4430 *p++ = ssl->transform->out_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004431 memcpy(p, ssl->transform->out_cid, ssl->transform->out_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004432 p += ssl->transform->out_cid_len;
4433 }
4434#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4435
4436 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004437 * Saved fields from top-level ssl_context structure
4438 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004439 used += 4;
Gilles Peskine449bd832023-01-11 14:50:10 +01004440 if (used <= buf_len) {
4441 MBEDTLS_PUT_UINT32_BE(ssl->badmac_seen, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004442 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004443 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004444
4445#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4446 used += 16;
Gilles Peskine449bd832023-01-11 14:50:10 +01004447 if (used <= buf_len) {
4448 MBEDTLS_PUT_UINT64_BE(ssl->in_window_top, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004449 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004450
Gilles Peskine449bd832023-01-11 14:50:10 +01004451 MBEDTLS_PUT_UINT64_BE(ssl->in_window, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004452 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004453 }
4454#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4455
4456#if defined(MBEDTLS_SSL_PROTO_DTLS)
4457 used += 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004458 if (used <= buf_len) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004459 *p++ = ssl->disable_datagram_packing;
4460 }
4461#endif /* MBEDTLS_SSL_PROTO_DTLS */
4462
Jerry Yuae0b2e22021-10-08 15:21:19 +08004463 used += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Gilles Peskine449bd832023-01-11 14:50:10 +01004464 if (used <= buf_len) {
4465 memcpy(p, ssl->cur_out_ctr, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN);
Jerry Yuae0b2e22021-10-08 15:21:19 +08004466 p += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004467 }
4468
4469#if defined(MBEDTLS_SSL_PROTO_DTLS)
4470 used += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01004471 if (used <= buf_len) {
4472 MBEDTLS_PUT_UINT16_BE(ssl->mtu, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004473 p += 2;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004474 }
4475#endif /* MBEDTLS_SSL_PROTO_DTLS */
4476
4477#if defined(MBEDTLS_SSL_ALPN)
4478 {
4479 const uint8_t alpn_len = ssl->alpn_chosen
Gilles Peskine449bd832023-01-11 14:50:10 +01004480 ? (uint8_t) strlen(ssl->alpn_chosen)
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004481 : 0;
4482
4483 used += 1 + alpn_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004484 if (used <= buf_len) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004485 *p++ = alpn_len;
4486
Gilles Peskine449bd832023-01-11 14:50:10 +01004487 if (ssl->alpn_chosen != NULL) {
4488 memcpy(p, ssl->alpn_chosen, alpn_len);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004489 p += alpn_len;
4490 }
4491 }
4492 }
4493#endif /* MBEDTLS_SSL_ALPN */
4494
4495 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004496 * Done
4497 */
4498 *olen = used;
4499
Gilles Peskine449bd832023-01-11 14:50:10 +01004500 if (used > buf_len) {
4501 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
4502 }
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004503
Gilles Peskine449bd832023-01-11 14:50:10 +01004504 MBEDTLS_SSL_DEBUG_BUF(4, "saved context", buf, used);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004505
Gilles Peskine449bd832023-01-11 14:50:10 +01004506 return mbedtls_ssl_session_reset_int(ssl, 0);
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004507}
4508
4509/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02004510 * Deserialize context, see mbedtls_ssl_context_save() for format.
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004511 *
4512 * This internal version is wrapped by a public function that cleans up in
4513 * case of error.
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004514 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004515MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01004516static int ssl_context_load(mbedtls_ssl_context *ssl,
4517 const unsigned char *buf,
4518 size_t len)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004519{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004520 const unsigned char *p = buf;
4521 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004522 size_t session_len;
Janos Follath865b3eb2019-12-16 11:46:15 +00004523 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004524#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek894edde2022-09-29 06:31:14 -04004525 tls_prf_fn prf_func = NULL;
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004526#endif
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004527
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004528 /*
4529 * The context should have been freshly setup or reset.
4530 * Give the user an error in case of obvious misuse.
Manuel Pégourié-Gonnard4ca930f2019-07-26 16:31:53 +02004531 * (Checking session is useful because it won't be NULL if we're
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004532 * renegotiating, or if the user mistakenly loaded a session first.)
4533 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004534 if (ssl->state != MBEDTLS_SSL_HELLO_REQUEST ||
4535 ssl->session != NULL) {
4536 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004537 }
4538
4539 /*
4540 * We can't check that the config matches the initial one, but we can at
4541 * least check it matches the requirements for serializing.
4542 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004543 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
Glenn Strauss2dfcea22022-03-14 17:26:42 -04004544 ssl->conf->max_tls_version < MBEDTLS_SSL_VERSION_TLS1_2 ||
4545 ssl->conf->min_tls_version > MBEDTLS_SSL_VERSION_TLS1_2 ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004546#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02004547 ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004548#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01004549 0) {
4550 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004551 }
4552
Gilles Peskine449bd832023-01-11 14:50:10 +01004553 MBEDTLS_SSL_DEBUG_BUF(4, "context to load", buf, len);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004554
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004555 /*
4556 * Check version identifier
4557 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004558 if ((size_t) (end - p) < sizeof(ssl_serialized_context_header)) {
4559 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004560 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004561
4562 if (memcmp(p, ssl_serialized_context_header,
4563 sizeof(ssl_serialized_context_header)) != 0) {
4564 return MBEDTLS_ERR_SSL_VERSION_MISMATCH;
4565 }
4566 p += sizeof(ssl_serialized_context_header);
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004567
4568 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004569 * Session
4570 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004571 if ((size_t) (end - p) < 4) {
4572 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4573 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004574
Gilles Peskine449bd832023-01-11 14:50:10 +01004575 session_len = ((size_t) p[0] << 24) |
4576 ((size_t) p[1] << 16) |
4577 ((size_t) p[2] << 8) |
4578 ((size_t) p[3]);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004579 p += 4;
4580
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004581 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00004582 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004583 ssl->session = ssl->session_negotiate;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004584 ssl->session_in = ssl->session;
4585 ssl->session_out = ssl->session;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004586 ssl->session_negotiate = NULL;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004587
Gilles Peskine449bd832023-01-11 14:50:10 +01004588 if ((size_t) (end - p) < session_len) {
4589 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4590 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004591
Gilles Peskine449bd832023-01-11 14:50:10 +01004592 ret = ssl_session_load(ssl->session, 1, p, session_len);
4593 if (ret != 0) {
4594 mbedtls_ssl_session_free(ssl->session);
4595 return ret;
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004596 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004597
4598 p += session_len;
4599
4600 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004601 * Transform
4602 */
4603
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004604 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00004605 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Jerry Yu2e199812022-12-01 18:57:19 +08004606#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004607 ssl->transform = ssl->transform_negotiate;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004608 ssl->transform_in = ssl->transform;
4609 ssl->transform_out = ssl->transform;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004610 ssl->transform_negotiate = NULL;
Jerry Yu2e199812022-12-01 18:57:19 +08004611#endif
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004612
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004613#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01004614 prf_func = ssl_tls12prf_from_cs(ssl->session->ciphersuite);
4615 if (prf_func == NULL) {
4616 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4617 }
Andrzej Kurek894edde2022-09-29 06:31:14 -04004618
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004619 /* Read random bytes and populate structure */
Gilles Peskine449bd832023-01-11 14:50:10 +01004620 if ((size_t) (end - p) < sizeof(ssl->transform->randbytes)) {
4621 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4622 }
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004623
Gilles Peskine449bd832023-01-11 14:50:10 +01004624 ret = ssl_tls12_populate_transform(ssl->transform,
4625 ssl->session->ciphersuite,
4626 ssl->session->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02004627#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004628 ssl->session->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02004629#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01004630 prf_func,
4631 p, /* currently pointing to randbytes */
4632 MBEDTLS_SSL_VERSION_TLS1_2, /* (D)TLS 1.2 is forced */
4633 ssl->conf->endpoint,
4634 ssl);
4635 if (ret != 0) {
4636 return ret;
4637 }
Jerry Yu840fbb22022-02-17 14:59:29 +08004638#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004639 p += sizeof(ssl->transform->randbytes);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004640
4641#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4642 /* Read connection IDs and store them */
Gilles Peskine449bd832023-01-11 14:50:10 +01004643 if ((size_t) (end - p) < 1) {
4644 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4645 }
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004646
4647 ssl->transform->in_cid_len = *p++;
4648
Gilles Peskine449bd832023-01-11 14:50:10 +01004649 if ((size_t) (end - p) < ssl->transform->in_cid_len + 1u) {
4650 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4651 }
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004652
Gilles Peskine449bd832023-01-11 14:50:10 +01004653 memcpy(ssl->transform->in_cid, p, ssl->transform->in_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004654 p += ssl->transform->in_cid_len;
4655
4656 ssl->transform->out_cid_len = *p++;
4657
Gilles Peskine449bd832023-01-11 14:50:10 +01004658 if ((size_t) (end - p) < ssl->transform->out_cid_len) {
4659 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4660 }
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004661
Gilles Peskine449bd832023-01-11 14:50:10 +01004662 memcpy(ssl->transform->out_cid, p, ssl->transform->out_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004663 p += ssl->transform->out_cid_len;
4664#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4665
4666 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004667 * Saved fields from top-level ssl_context structure
4668 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004669 if ((size_t) (end - p) < 4) {
4670 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4671 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004672
Gilles Peskine449bd832023-01-11 14:50:10 +01004673 ssl->badmac_seen = ((uint32_t) p[0] << 24) |
4674 ((uint32_t) p[1] << 16) |
4675 ((uint32_t) p[2] << 8) |
4676 ((uint32_t) p[3]);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004677 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004678
4679#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine449bd832023-01-11 14:50:10 +01004680 if ((size_t) (end - p) < 16) {
4681 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4682 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004683
Gilles Peskine449bd832023-01-11 14:50:10 +01004684 ssl->in_window_top = ((uint64_t) p[0] << 56) |
4685 ((uint64_t) p[1] << 48) |
4686 ((uint64_t) p[2] << 40) |
4687 ((uint64_t) p[3] << 32) |
4688 ((uint64_t) p[4] << 24) |
4689 ((uint64_t) p[5] << 16) |
4690 ((uint64_t) p[6] << 8) |
4691 ((uint64_t) p[7]);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004692 p += 8;
4693
Gilles Peskine449bd832023-01-11 14:50:10 +01004694 ssl->in_window = ((uint64_t) p[0] << 56) |
4695 ((uint64_t) p[1] << 48) |
4696 ((uint64_t) p[2] << 40) |
4697 ((uint64_t) p[3] << 32) |
4698 ((uint64_t) p[4] << 24) |
4699 ((uint64_t) p[5] << 16) |
4700 ((uint64_t) p[6] << 8) |
4701 ((uint64_t) p[7]);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004702 p += 8;
4703#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4704
4705#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004706 if ((size_t) (end - p) < 1) {
4707 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4708 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004709
4710 ssl->disable_datagram_packing = *p++;
4711#endif /* MBEDTLS_SSL_PROTO_DTLS */
4712
Gilles Peskine449bd832023-01-11 14:50:10 +01004713 if ((size_t) (end - p) < sizeof(ssl->cur_out_ctr)) {
4714 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4715 }
4716 memcpy(ssl->cur_out_ctr, p, sizeof(ssl->cur_out_ctr));
4717 p += sizeof(ssl->cur_out_ctr);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004718
4719#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004720 if ((size_t) (end - p) < 2) {
4721 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4722 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004723
Gilles Peskine449bd832023-01-11 14:50:10 +01004724 ssl->mtu = (p[0] << 8) | p[1];
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004725 p += 2;
4726#endif /* MBEDTLS_SSL_PROTO_DTLS */
4727
4728#if defined(MBEDTLS_SSL_ALPN)
4729 {
4730 uint8_t alpn_len;
4731 const char **cur;
4732
Gilles Peskine449bd832023-01-11 14:50:10 +01004733 if ((size_t) (end - p) < 1) {
4734 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4735 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004736
4737 alpn_len = *p++;
4738
Gilles Peskine449bd832023-01-11 14:50:10 +01004739 if (alpn_len != 0 && ssl->conf->alpn_list != NULL) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004740 /* alpn_chosen should point to an item in the configured list */
Gilles Peskine449bd832023-01-11 14:50:10 +01004741 for (cur = ssl->conf->alpn_list; *cur != NULL; cur++) {
4742 if (strlen(*cur) == alpn_len &&
4743 memcmp(p, cur, alpn_len) == 0) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004744 ssl->alpn_chosen = *cur;
4745 break;
4746 }
4747 }
4748 }
4749
4750 /* can only happen on conf mismatch */
Gilles Peskine449bd832023-01-11 14:50:10 +01004751 if (alpn_len != 0 && ssl->alpn_chosen == NULL) {
4752 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4753 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004754
4755 p += alpn_len;
4756 }
4757#endif /* MBEDTLS_SSL_ALPN */
4758
4759 /*
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004760 * Forced fields from top-level ssl_context structure
4761 *
4762 * Most of them already set to the correct value by mbedtls_ssl_init() and
4763 * mbedtls_ssl_reset(), so we only need to set the remaining ones.
4764 */
4765 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
Glenn Strauss60bfe602022-03-14 19:04:24 -04004766 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004767
Hanno Becker361b10d2019-08-30 10:42:49 +01004768 /* Adjust pointers for header fields of outgoing records to
4769 * the given transform, accounting for explicit IV and CID. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004770 mbedtls_ssl_update_out_pointers(ssl, ssl->transform);
Hanno Becker361b10d2019-08-30 10:42:49 +01004771
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004772#if defined(MBEDTLS_SSL_PROTO_DTLS)
4773 ssl->in_epoch = 1;
4774#endif
4775
4776 /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated,
4777 * which we don't want - otherwise we'd end up freeing the wrong transform
Hanno Beckerce5f5fd2020-02-05 10:47:44 +00004778 * by calling mbedtls_ssl_handshake_wrapup_free_hs_transform()
4779 * inappropriately. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004780 if (ssl->handshake != NULL) {
4781 mbedtls_ssl_handshake_free(ssl);
4782 mbedtls_free(ssl->handshake);
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004783 ssl->handshake = NULL;
4784 }
4785
4786 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004787 * Done - should have consumed entire buffer
4788 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004789 if (p != end) {
4790 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4791 }
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004792
Gilles Peskine449bd832023-01-11 14:50:10 +01004793 return 0;
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004794}
4795
4796/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02004797 * Deserialize context: public wrapper for error cleaning
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004798 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004799int mbedtls_ssl_context_load(mbedtls_ssl_context *context,
4800 const unsigned char *buf,
4801 size_t len)
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004802{
Gilles Peskine449bd832023-01-11 14:50:10 +01004803 int ret = ssl_context_load(context, buf, len);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004804
Gilles Peskine449bd832023-01-11 14:50:10 +01004805 if (ret != 0) {
4806 mbedtls_ssl_free(context);
4807 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004808
Gilles Peskine449bd832023-01-11 14:50:10 +01004809 return ret;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004810}
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02004811#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004812
4813/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004814 * Free an SSL context
4815 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004816void mbedtls_ssl_free(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00004817{
Gilles Peskine449bd832023-01-11 14:50:10 +01004818 if (ssl == NULL) {
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004819 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01004820 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004821
Gilles Peskine449bd832023-01-11 14:50:10 +01004822 MBEDTLS_SSL_DEBUG_MSG(2, ("=> free"));
Paul Bakker5121ce52009-01-03 21:22:43 +00004823
Gilles Peskine449bd832023-01-11 14:50:10 +01004824 if (ssl->out_buf != NULL) {
sander-visserb8aa2072020-05-06 22:05:13 +02004825#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4826 size_t out_buf_len = ssl->out_buf_len;
4827#else
4828 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
4829#endif
4830
Gilles Peskine449bd832023-01-11 14:50:10 +01004831 mbedtls_platform_zeroize(ssl->out_buf, out_buf_len);
4832 mbedtls_free(ssl->out_buf);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004833 ssl->out_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004834 }
4835
Gilles Peskine449bd832023-01-11 14:50:10 +01004836 if (ssl->in_buf != NULL) {
sander-visserb8aa2072020-05-06 22:05:13 +02004837#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4838 size_t in_buf_len = ssl->in_buf_len;
4839#else
4840 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
4841#endif
4842
Gilles Peskine449bd832023-01-11 14:50:10 +01004843 mbedtls_platform_zeroize(ssl->in_buf, in_buf_len);
4844 mbedtls_free(ssl->in_buf);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004845 ssl->in_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004846 }
4847
Gilles Peskine449bd832023-01-11 14:50:10 +01004848 if (ssl->transform) {
4849 mbedtls_ssl_transform_free(ssl->transform);
4850 mbedtls_free(ssl->transform);
Paul Bakker48916f92012-09-16 19:57:18 +00004851 }
4852
Gilles Peskine449bd832023-01-11 14:50:10 +01004853 if (ssl->handshake) {
4854 mbedtls_ssl_handshake_free(ssl);
4855 mbedtls_free(ssl->handshake);
Jerry Yu2e199812022-12-01 18:57:19 +08004856
4857#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01004858 mbedtls_ssl_transform_free(ssl->transform_negotiate);
4859 mbedtls_free(ssl->transform_negotiate);
Jerry Yu2e199812022-12-01 18:57:19 +08004860#endif
4861
Gilles Peskine449bd832023-01-11 14:50:10 +01004862 mbedtls_ssl_session_free(ssl->session_negotiate);
4863 mbedtls_free(ssl->session_negotiate);
Paul Bakker48916f92012-09-16 19:57:18 +00004864 }
4865
Ronald Cron6f135e12021-12-08 16:57:54 +01004866#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01004867 mbedtls_ssl_transform_free(ssl->transform_application);
4868 mbedtls_free(ssl->transform_application);
Ronald Cron6f135e12021-12-08 16:57:54 +01004869#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker3aa186f2021-08-10 09:24:19 +01004870
Gilles Peskine449bd832023-01-11 14:50:10 +01004871 if (ssl->session) {
4872 mbedtls_ssl_session_free(ssl->session);
4873 mbedtls_free(ssl->session);
Paul Bakkerc0463502013-02-14 11:19:38 +01004874 }
4875
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02004876#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004877 if (ssl->hostname != NULL) {
4878 mbedtls_platform_zeroize(ssl->hostname, strlen(ssl->hostname));
4879 mbedtls_free(ssl->hostname);
Paul Bakker5121ce52009-01-03 21:22:43 +00004880 }
Paul Bakker0be444a2013-08-27 21:55:01 +02004881#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004882
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02004883#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004884 mbedtls_free(ssl->cli_id);
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02004885#endif
4886
Gilles Peskine449bd832023-01-11 14:50:10 +01004887 MBEDTLS_SSL_DEBUG_MSG(2, ("<= free"));
Paul Bakker2da561c2009-02-05 18:00:28 +00004888
Paul Bakker86f04f42013-02-14 11:20:09 +01004889 /* Actually clear after last debug message */
Gilles Peskine449bd832023-01-11 14:50:10 +01004890 mbedtls_platform_zeroize(ssl, sizeof(mbedtls_ssl_context));
Paul Bakker5121ce52009-01-03 21:22:43 +00004891}
4892
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004893/*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08004894 * Initialize mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004895 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004896void mbedtls_ssl_config_init(mbedtls_ssl_config *conf)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004897{
Gilles Peskine449bd832023-01-11 14:50:10 +01004898 memset(conf, 0, sizeof(mbedtls_ssl_config));
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004899}
4900
Gilles Peskineae270bf2021-06-02 00:05:29 +02004901/* The selection should be the same as mbedtls_x509_crt_profile_default in
4902 * x509_crt.c, plus Montgomery curves for ECDHE. Here, the order matters:
Gilles Peskineb1940a72021-06-02 15:18:12 +02004903 * curves with a lower resource usage come first.
Gilles Peskineae270bf2021-06-02 00:05:29 +02004904 * See the documentation of mbedtls_ssl_conf_curves() for what we promise
Gilles Peskineb1940a72021-06-02 15:18:12 +02004905 * about this list.
4906 */
Brett Warrene0edc842021-08-17 09:53:13 +01004907static uint16_t ssl_preset_default_groups[] = {
Gilles Peskineae270bf2021-06-02 00:05:29 +02004908#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004909 MBEDTLS_SSL_IANA_TLS_GROUP_X25519,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004910#endif
4911#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004912 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004913#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02004914#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004915 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004916#endif
4917#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004918 MBEDTLS_SSL_IANA_TLS_GROUP_X448,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004919#endif
4920#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004921 MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004922#endif
Gilles Peskineae270bf2021-06-02 00:05:29 +02004923#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004924 MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004925#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02004926#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004927 MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004928#endif
4929#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004930 MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004931#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004932 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Gilles Peskineae270bf2021-06-02 00:05:29 +02004933};
Gilles Peskineae270bf2021-06-02 00:05:29 +02004934
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004935static int ssl_preset_suiteb_ciphersuites[] = {
4936 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
4937 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
4938 0
4939};
4940
Ronald Crone68ab4f2022-10-05 12:46:29 +02004941#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004942
Jerry Yu909df7b2022-01-22 11:56:27 +08004943/* NOTICE:
Jerry Yu0b994b82022-01-25 17:22:12 +08004944 * For ssl_preset_*_sig_algs and ssl_tls12_preset_*_sig_algs, the following
Jerry Yu370e1462022-01-25 10:36:53 +08004945 * rules SHOULD be upheld.
4946 * - No duplicate entries.
4947 * - But if there is a good reason, do not change the order of the algorithms.
Jerry Yu09a99fc2022-07-28 14:22:17 +08004948 * - ssl_tls12_preset* is for TLS 1.2 use only.
Jerry Yu370e1462022-01-25 10:36:53 +08004949 * - ssl_preset_* is for TLS 1.3 only or hybrid TLS 1.3/1.2 handshakes.
Jerry Yu1a8b4812022-01-20 17:56:50 +08004950 */
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004951static uint16_t ssl_preset_default_sig_algs[] = {
Jerry Yu1a8b4812022-01-20 17:56:50 +08004952
Andrzej Kurek25f27152022-08-17 16:09:31 -04004953#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 +08004954 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
4955 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004956#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA &&
Jerry Yued5e9f42022-01-26 11:21:34 +08004957 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004958
Andrzej Kurek25f27152022-08-17 16:09:31 -04004959#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 +08004960 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
4961 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004962#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu909df7b2022-01-22 11:56:27 +08004963 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
4964
Andrzej Kurek25f27152022-08-17 16:09:31 -04004965#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 +08004966 defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
4967 MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004968#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yued5e9f42022-01-26 11:21:34 +08004969 MBEDTLS_ECP_DP_SECP521R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004970
Gilles Peskine449bd832023-01-11 14:50:10 +01004971#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && \
4972 defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu9bb3ee42022-06-23 10:16:33 +08004973 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
Gilles Peskine449bd832023-01-11 14:50:10 +01004974#endif \
4975 /* 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 +08004976
Gilles Peskine449bd832023-01-11 14:50:10 +01004977#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && \
4978 defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu9bb3ee42022-06-23 10:16:33 +08004979 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
Gilles Peskine449bd832023-01-11 14:50:10 +01004980#endif \
4981 /* 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 +08004982
Gilles Peskine449bd832023-01-11 14:50:10 +01004983#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && \
4984 defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu9bb3ee42022-06-23 10:16:33 +08004985 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Gilles Peskine449bd832023-01-11 14:50:10 +01004986#endif \
4987 /* 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 +08004988
Andrzej Kurek25f27152022-08-17 16:09:31 -04004989#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 +08004990 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512,
4991#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA512_C */
4992
Andrzej Kurek25f27152022-08-17 16:09:31 -04004993#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 +08004994 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384,
4995#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA384_C */
4996
Andrzej Kurek25f27152022-08-17 16:09:31 -04004997#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 +08004998 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
4999#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA256_C */
5000
Gabor Mezei15b95a62022-05-09 16:37:58 +02005001 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08005002};
5003
5004/* NOTICE: see above */
5005#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5006static uint16_t ssl_tls12_preset_default_sig_algs[] = {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005007#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02005008#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005009 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA512),
Jerry Yu713013f2022-01-17 18:16:35 +08005010#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08005011#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
5012 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
5013#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02005014#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005015 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA512),
Gabor Mezeic1051b62022-05-10 13:13:58 +02005016#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005017#endif /* MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04005018#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02005019#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005020 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08005021#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08005022#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
5023 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
5024#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
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_SHA384),
Gabor Mezeic1051b62022-05-10 13:13:58 +02005027#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005028#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04005029#if defined(MBEDTLS_HAS_ALG_SHA_256_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_SHA256),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08005032#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08005033#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
5034 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
5035#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02005036#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005037 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256),
Gabor Mezeic1051b62022-05-10 13:13:58 +02005038#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005039#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Gabor Mezei15b95a62022-05-09 16:37:58 +02005040 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08005041};
5042#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5043/* NOTICE: see above */
5044static uint16_t ssl_preset_suiteb_sig_algs[] = {
5045
Andrzej Kurek25f27152022-08-17 16:09:31 -04005046#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 +08005047 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
5048 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005049#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu909df7b2022-01-22 11:56:27 +08005050 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
5051
Andrzej Kurek25f27152022-08-17 16:09:31 -04005052#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 +08005053 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
5054 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005055#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu53037892022-01-25 11:02:06 +08005056 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08005057
Gilles Peskine449bd832023-01-11 14:50:10 +01005058#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && \
5059 defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu909df7b2022-01-22 11:56:27 +08005060 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Gilles Peskine449bd832023-01-11 14:50:10 +01005061#endif \
5062 /* 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 +08005063
Andrzej Kurek25f27152022-08-17 16:09:31 -04005064#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 +08005065 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005066#endif /* MBEDTLS_RSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yu53037892022-01-25 11:02:06 +08005067
Gabor Mezei15b95a62022-05-09 16:37:58 +02005068 MBEDTLS_TLS_SIG_NONE
Jerry Yu713013f2022-01-17 18:16:35 +08005069};
Jerry Yu6106fdc2022-01-12 16:36:14 +08005070
Jerry Yu909df7b2022-01-22 11:56:27 +08005071/* NOTICE: see above */
5072#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5073static uint16_t ssl_tls12_preset_suiteb_sig_algs[] = {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005074#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02005075#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005076 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256),
Jerry Yu713013f2022-01-17 18:16:35 +08005077#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02005078#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005079 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256),
Gabor Mezeic1051b62022-05-10 13:13:58 +02005080#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005081#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04005082#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02005083#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005084 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384),
Jerry Yu18c833e2022-01-25 10:55:47 +08005085#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02005086#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005087 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384),
Gabor Mezeic1051b62022-05-10 13:13:58 +02005088#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005089#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Gabor Mezei15b95a62022-05-09 16:37:58 +02005090 MBEDTLS_TLS_SIG_NONE
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01005091};
Jerry Yu909df7b2022-01-22 11:56:27 +08005092#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5093
Ronald Crone68ab4f2022-10-05 12:46:29 +02005094#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005095
Brett Warrene0edc842021-08-17 09:53:13 +01005096static uint16_t ssl_preset_suiteb_groups[] = {
Jaeden Amerod4311042019-06-03 08:27:16 +01005097#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01005098 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01005099#endif
5100#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01005101 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01005102#endif
Brett Warrene0edc842021-08-17 09:53:13 +01005103 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005104};
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005105
Ronald Crone68ab4f2022-10-05 12:46:29 +02005106#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08005107/* Function for checking `ssl_preset_*_sig_algs` and `ssl_tls12_preset_*_sig_algs`
Jerry Yu370e1462022-01-25 10:36:53 +08005108 * to make sure there are no duplicated signature algorithm entries. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005109MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01005110static int ssl_check_no_sig_alg_duplication(uint16_t *sig_algs)
Jerry Yu1a8b4812022-01-20 17:56:50 +08005111{
5112 size_t i, j;
5113 int ret = 0;
Jerry Yu909df7b2022-01-22 11:56:27 +08005114
Gilles Peskine449bd832023-01-11 14:50:10 +01005115 for (i = 0; sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++) {
5116 for (j = 0; j < i; j++) {
5117 if (sig_algs[i] != sig_algs[j]) {
Jerry Yuf377d642022-01-25 10:43:59 +08005118 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01005119 }
5120 mbedtls_printf(" entry(%04x,%" MBEDTLS_PRINTF_SIZET
5121 ") is duplicated at %" MBEDTLS_PRINTF_SIZET "\n",
5122 sig_algs[i], j, i);
Jerry Yuf377d642022-01-25 10:43:59 +08005123 ret = -1;
Jerry Yu1a8b4812022-01-20 17:56:50 +08005124 }
5125 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005126 return ret;
Jerry Yu1a8b4812022-01-20 17:56:50 +08005127}
5128
Ronald Crone68ab4f2022-10-05 12:46:29 +02005129#endif /* MBEDTLS_DEBUG_C && MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yu1a8b4812022-01-20 17:56:50 +08005130
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005131/*
Tillmann Karras588ad502015-09-25 04:27:22 +02005132 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005133 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005134int mbedtls_ssl_config_defaults(mbedtls_ssl_config *conf,
5135 int endpoint, int transport, int preset)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005136{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005137#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Janos Follath865b3eb2019-12-16 11:46:15 +00005138 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005139#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005140
Ronald Crone68ab4f2022-10-05 12:46:29 +02005141#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01005142 if (ssl_check_no_sig_alg_duplication(ssl_preset_suiteb_sig_algs)) {
5143 mbedtls_printf("ssl_preset_suiteb_sig_algs has duplicated entries\n");
5144 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu1a8b4812022-01-20 17:56:50 +08005145 }
5146
Gilles Peskine449bd832023-01-11 14:50:10 +01005147 if (ssl_check_no_sig_alg_duplication(ssl_preset_default_sig_algs)) {
5148 mbedtls_printf("ssl_preset_default_sig_algs has duplicated entries\n");
5149 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu1a8b4812022-01-20 17:56:50 +08005150 }
Jerry Yu909df7b2022-01-22 11:56:27 +08005151
5152#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01005153 if (ssl_check_no_sig_alg_duplication(ssl_tls12_preset_suiteb_sig_algs)) {
5154 mbedtls_printf("ssl_tls12_preset_suiteb_sig_algs has duplicated entries\n");
5155 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu909df7b2022-01-22 11:56:27 +08005156 }
5157
Gilles Peskine449bd832023-01-11 14:50:10 +01005158 if (ssl_check_no_sig_alg_duplication(ssl_tls12_preset_default_sig_algs)) {
5159 mbedtls_printf("ssl_tls12_preset_default_sig_algs has duplicated entries\n");
5160 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu909df7b2022-01-22 11:56:27 +08005161 }
5162#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Ronald Crone68ab4f2022-10-05 12:46:29 +02005163#endif /* MBEDTLS_DEBUG_C && MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yu1a8b4812022-01-20 17:56:50 +08005164
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02005165 /* Use the functions here so that they are covered in tests,
5166 * but otherwise access member directly for efficiency */
Gilles Peskine449bd832023-01-11 14:50:10 +01005167 mbedtls_ssl_conf_endpoint(conf, endpoint);
5168 mbedtls_ssl_conf_transport(conf, transport);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005169
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005170 /*
5171 * Things that are common to all presets
5172 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02005173#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005174 if (endpoint == MBEDTLS_SSL_IS_CLIENT) {
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02005175 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
5176#if defined(MBEDTLS_SSL_SESSION_TICKETS)
5177 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
5178#endif
5179 }
5180#endif
5181
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005182#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
5183 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
5184#endif
5185
5186#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
5187 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
5188#endif
5189
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02005190#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005191 conf->f_cookie_write = ssl_cookie_write_dummy;
5192 conf->f_cookie_check = ssl_cookie_check_dummy;
5193#endif
5194
5195#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
5196 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
5197#endif
5198
Janos Follath088ce432017-04-10 12:42:31 +01005199#if defined(MBEDTLS_SSL_SRV_C)
5200 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
TRodziewicz3946f792021-06-14 12:11:18 +02005201 conf->respect_cli_pref = MBEDTLS_SSL_SRV_CIPHERSUITE_ORDER_SERVER;
Janos Follath088ce432017-04-10 12:42:31 +01005202#endif
5203
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005204#if defined(MBEDTLS_SSL_PROTO_DTLS)
5205 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
5206 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
5207#endif
5208
5209#if defined(MBEDTLS_SSL_RENEGOTIATION)
5210 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005211 memset(conf->renego_period, 0x00, 2);
5212 memset(conf->renego_period + 2, 0xFF, 6);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005213#endif
5214
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005215#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005216 if (endpoint == MBEDTLS_SSL_IS_SERVER) {
Hanno Beckere2defad2021-07-24 05:59:17 +01005217 const unsigned char dhm_p[] =
5218 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
5219 const unsigned char dhm_g[] =
5220 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
Hanno Becker00d0a682017-10-04 13:14:29 +01005221
Gilles Peskine449bd832023-01-11 14:50:10 +01005222 if ((ret = mbedtls_ssl_conf_dh_param_bin(conf,
5223 dhm_p, sizeof(dhm_p),
5224 dhm_g, sizeof(dhm_g))) != 0) {
5225 return ret;
Hanno Beckere2defad2021-07-24 05:59:17 +01005226 }
5227 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02005228#endif
5229
Ronald Cron6f135e12021-12-08 16:57:54 +01005230#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu6ee56aa2022-12-06 17:47:22 +08005231
5232#if defined(MBEDTLS_SSL_EARLY_DATA)
Gilles Peskine449bd832023-01-11 14:50:10 +01005233 mbedtls_ssl_tls13_conf_early_data(conf, MBEDTLS_SSL_EARLY_DATA_DISABLED);
Jerry Yu6ee56aa2022-12-06 17:47:22 +08005234#if defined(MBEDTLS_SSL_SRV_C)
5235 mbedtls_ssl_tls13_conf_max_early_data_size(
Gilles Peskine449bd832023-01-11 14:50:10 +01005236 conf, MBEDTLS_SSL_MAX_EARLY_DATA_SIZE);
Jerry Yu6ee56aa2022-12-06 17:47:22 +08005237#endif
5238#endif /* MBEDTLS_SSL_EARLY_DATA */
5239
Jerry Yud0766ec2022-09-22 10:46:57 +08005240#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08005241 mbedtls_ssl_conf_new_session_tickets(
Gilles Peskine449bd832023-01-11 14:50:10 +01005242 conf, MBEDTLS_SSL_TLS1_3_DEFAULT_NEW_SESSION_TICKETS);
Jerry Yu1ad7ace2022-08-09 13:28:39 +08005243#endif
Hanno Becker71f1ed62021-07-24 06:01:47 +01005244 /*
5245 * Allow all TLS 1.3 key exchange modes by default.
5246 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00005247 conf->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Ronald Cron6f135e12021-12-08 16:57:54 +01005248#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01005249
Gilles Peskine449bd832023-01-11 14:50:10 +01005250 if (transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Glenn Strauss2dfcea22022-03-14 17:26:42 -04005251#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
XiaokangQian4d3a6042022-04-21 13:46:17 +00005252 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian060d8672022-04-21 09:24:56 +00005253 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian4d3a6042022-04-21 13:46:17 +00005254#else
Gilles Peskine449bd832023-01-11 14:50:10 +01005255 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQian060d8672022-04-21 09:24:56 +00005256#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005257 } else {
XiaokangQian4d3a6042022-04-21 13:46:17 +00005258#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01005259 if (endpoint == MBEDTLS_SSL_IS_CLIENT) {
XiaokangQian4d3a6042022-04-21 13:46:17 +00005260 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5261 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
Gilles Peskine449bd832023-01-11 14:50:10 +01005262 } else {
5263 /* Hybrid TLS 1.2 / 1.3 is not supported on server side yet */
XiaokangQian4d3a6042022-04-21 13:46:17 +00005264 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5265 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5266 }
5267#elif defined(MBEDTLS_SSL_PROTO_TLS1_3)
5268 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
5269 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
5270#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
5271 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5272 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5273#else
Gilles Peskine449bd832023-01-11 14:50:10 +01005274 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQian4d3a6042022-04-21 13:46:17 +00005275#endif
5276 }
Glenn Strauss2dfcea22022-03-14 17:26:42 -04005277
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005278 /*
5279 * Preset-specific defaults
5280 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005281 switch (preset) {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005282 /*
5283 * NSA Suite B
5284 */
5285 case MBEDTLS_SSL_PRESET_SUITEB:
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005286
Hanno Beckerd60b6c62021-04-29 12:04:11 +01005287 conf->ciphersuite_list = ssl_preset_suiteb_ciphersuites;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005288
5289#if defined(MBEDTLS_X509_CRT_PARSE_C)
5290 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005291#endif
5292
Ronald Crone68ab4f2022-10-05 12:46:29 +02005293#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08005294#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01005295 if (mbedtls_ssl_conf_is_tls12_only(conf)) {
Jerry Yu909df7b2022-01-22 11:56:27 +08005296 conf->sig_algs = ssl_tls12_preset_suiteb_sig_algs;
Gilles Peskine449bd832023-01-11 14:50:10 +01005297 } else
Jerry Yu909df7b2022-01-22 11:56:27 +08005298#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005299 conf->sig_algs = ssl_preset_suiteb_sig_algs;
Ronald Crone68ab4f2022-10-05 12:46:29 +02005300#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005301
Brett Warrene0edc842021-08-17 09:53:13 +01005302#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
5303 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005304#endif
Brett Warrene0edc842021-08-17 09:53:13 +01005305 conf->group_list = ssl_preset_suiteb_groups;
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02005306 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005307
5308 /*
5309 * Default
5310 */
5311 default:
Ronald Cronf6606552022-03-15 11:23:25 +01005312
Hanno Beckerd60b6c62021-04-29 12:04:11 +01005313 conf->ciphersuite_list = mbedtls_ssl_list_ciphersuites();
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005314
5315#if defined(MBEDTLS_X509_CRT_PARSE_C)
5316 conf->cert_profile = &mbedtls_x509_crt_profile_default;
5317#endif
5318
Ronald Crone68ab4f2022-10-05 12:46:29 +02005319#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08005320#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01005321 if (mbedtls_ssl_conf_is_tls12_only(conf)) {
Jerry Yu909df7b2022-01-22 11:56:27 +08005322 conf->sig_algs = ssl_tls12_preset_default_sig_algs;
Gilles Peskine449bd832023-01-11 14:50:10 +01005323 } else
Jerry Yu909df7b2022-01-22 11:56:27 +08005324#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005325 conf->sig_algs = ssl_preset_default_sig_algs;
Ronald Crone68ab4f2022-10-05 12:46:29 +02005326#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005327
Brett Warrene0edc842021-08-17 09:53:13 +01005328#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
5329 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005330#endif
Brett Warrene0edc842021-08-17 09:53:13 +01005331 conf->group_list = ssl_preset_default_groups;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005332
5333#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
5334 conf->dhm_min_bitlen = 1024;
5335#endif
5336 }
5337
Gilles Peskine449bd832023-01-11 14:50:10 +01005338 return 0;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005339}
5340
5341/*
5342 * Free mbedtls_ssl_config
5343 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005344void mbedtls_ssl_config_free(mbedtls_ssl_config *conf)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005345{
5346#if defined(MBEDTLS_DHM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005347 mbedtls_mpi_free(&conf->dhm_P);
5348 mbedtls_mpi_free(&conf->dhm_G);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005349#endif
5350
Ronald Cron73fe8df2022-10-05 14:31:43 +02005351#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02005352#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01005353 if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) {
Neil Armstrong501c9322022-05-03 09:35:09 +02005354 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
5355 }
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005356#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01005357 if (conf->psk != NULL) {
5358 mbedtls_platform_zeroize(conf->psk, conf->psk_len);
5359 mbedtls_free(conf->psk);
Azim Khan27e8a122018-03-21 14:24:11 +00005360 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005361 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09005362 }
5363
Gilles Peskine449bd832023-01-11 14:50:10 +01005364 if (conf->psk_identity != NULL) {
5365 mbedtls_platform_zeroize(conf->psk_identity, conf->psk_identity_len);
5366 mbedtls_free(conf->psk_identity);
Azim Khan27e8a122018-03-21 14:24:11 +00005367 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005368 conf->psk_identity_len = 0;
5369 }
Ronald Cron73fe8df2022-10-05 14:31:43 +02005370#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005371
5372#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005373 ssl_key_cert_free(conf->key_cert);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005374#endif
5375
Gilles Peskine449bd832023-01-11 14:50:10 +01005376 mbedtls_platform_zeroize(conf, sizeof(mbedtls_ssl_config));
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005377}
5378
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02005379#if defined(MBEDTLS_PK_C) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01005380 (defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C))
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005381/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005382 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005383 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005384unsigned char mbedtls_ssl_sig_from_pk(mbedtls_pk_context *pk)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005385{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005386#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005387 if (mbedtls_pk_can_do(pk, MBEDTLS_PK_RSA)) {
5388 return MBEDTLS_SSL_SIG_RSA;
5389 }
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005390#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005391#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005392 if (mbedtls_pk_can_do(pk, MBEDTLS_PK_ECDSA)) {
5393 return MBEDTLS_SSL_SIG_ECDSA;
5394 }
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005395#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005396 return MBEDTLS_SSL_SIG_ANON;
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005397}
5398
Gilles Peskine449bd832023-01-11 14:50:10 +01005399unsigned char mbedtls_ssl_sig_from_pk_alg(mbedtls_pk_type_t type)
Hanno Becker7e5437a2017-04-28 17:15:26 +01005400{
Gilles Peskine449bd832023-01-11 14:50:10 +01005401 switch (type) {
Hanno Becker7e5437a2017-04-28 17:15:26 +01005402 case MBEDTLS_PK_RSA:
Gilles Peskine449bd832023-01-11 14:50:10 +01005403 return MBEDTLS_SSL_SIG_RSA;
Hanno Becker7e5437a2017-04-28 17:15:26 +01005404 case MBEDTLS_PK_ECDSA:
5405 case MBEDTLS_PK_ECKEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01005406 return MBEDTLS_SSL_SIG_ECDSA;
Hanno Becker7e5437a2017-04-28 17:15:26 +01005407 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005408 return MBEDTLS_SSL_SIG_ANON;
Hanno Becker7e5437a2017-04-28 17:15:26 +01005409 }
5410}
5411
Gilles Peskine449bd832023-01-11 14:50:10 +01005412mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig(unsigned char sig)
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005413{
Gilles Peskine449bd832023-01-11 14:50:10 +01005414 switch (sig) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005415#if defined(MBEDTLS_RSA_C)
5416 case MBEDTLS_SSL_SIG_RSA:
Gilles Peskine449bd832023-01-11 14:50:10 +01005417 return MBEDTLS_PK_RSA;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005418#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005419#if defined(MBEDTLS_ECDSA_C)
5420 case MBEDTLS_SSL_SIG_ECDSA:
Gilles Peskine449bd832023-01-11 14:50:10 +01005421 return MBEDTLS_PK_ECDSA;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005422#endif
5423 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005424 return MBEDTLS_PK_NONE;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005425 }
5426}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02005427#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005428
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02005429/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005430 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02005431 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005432mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash(unsigned char hash)
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005433{
Gilles Peskine449bd832023-01-11 14:50:10 +01005434 switch (hash) {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005435#if defined(MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005436 case MBEDTLS_SSL_HASH_MD5:
Gilles Peskine449bd832023-01-11 14:50:10 +01005437 return MBEDTLS_MD_MD5;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005438#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005439#if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005440 case MBEDTLS_SSL_HASH_SHA1:
Gilles Peskine449bd832023-01-11 14:50:10 +01005441 return MBEDTLS_MD_SHA1;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005442#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005443#if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005444 case MBEDTLS_SSL_HASH_SHA224:
Gilles Peskine449bd832023-01-11 14:50:10 +01005445 return MBEDTLS_MD_SHA224;
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02005446#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005447#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005448 case MBEDTLS_SSL_HASH_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01005449 return MBEDTLS_MD_SHA256;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005450#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005451#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005452 case MBEDTLS_SSL_HASH_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01005453 return MBEDTLS_MD_SHA384;
Mateusz Starzyk3352a532021-04-06 14:28:22 +02005454#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005455#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005456 case MBEDTLS_SSL_HASH_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01005457 return MBEDTLS_MD_SHA512;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005458#endif
5459 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005460 return MBEDTLS_MD_NONE;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005461 }
5462}
5463
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005464/*
5465 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
5466 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005467unsigned char mbedtls_ssl_hash_from_md_alg(int md)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005468{
Gilles Peskine449bd832023-01-11 14:50:10 +01005469 switch (md) {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005470#if defined(MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005471 case MBEDTLS_MD_MD5:
Gilles Peskine449bd832023-01-11 14:50:10 +01005472 return MBEDTLS_SSL_HASH_MD5;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005473#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005474#if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005475 case MBEDTLS_MD_SHA1:
Gilles Peskine449bd832023-01-11 14:50:10 +01005476 return MBEDTLS_SSL_HASH_SHA1;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005477#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005478#if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005479 case MBEDTLS_MD_SHA224:
Gilles Peskine449bd832023-01-11 14:50:10 +01005480 return MBEDTLS_SSL_HASH_SHA224;
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02005481#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005482#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005483 case MBEDTLS_MD_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01005484 return MBEDTLS_SSL_HASH_SHA256;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005485#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005486#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005487 case MBEDTLS_MD_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01005488 return MBEDTLS_SSL_HASH_SHA384;
Mateusz Starzyk3352a532021-04-06 14:28:22 +02005489#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005490#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005491 case MBEDTLS_MD_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01005492 return MBEDTLS_SSL_HASH_SHA512;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005493#endif
5494 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005495 return MBEDTLS_SSL_HASH_NONE;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005496 }
5497}
5498
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005499/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005500 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005501 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005502 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005503int mbedtls_ssl_check_curve_tls_id(const mbedtls_ssl_context *ssl, uint16_t tls_id)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005504{
Gilles Peskine449bd832023-01-11 14:50:10 +01005505 const uint16_t *group_list = mbedtls_ssl_get_groups(ssl);
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005506
Gilles Peskine449bd832023-01-11 14:50:10 +01005507 if (group_list == NULL) {
5508 return -1;
Brett Warrene0edc842021-08-17 09:53:13 +01005509 }
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005510
Gilles Peskine449bd832023-01-11 14:50:10 +01005511 for (; *group_list != 0; group_list++) {
5512 if (*group_list == tls_id) {
5513 return 0;
5514 }
5515 }
5516
5517 return -1;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005518}
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005519
5520#if defined(MBEDTLS_ECP_C)
5521/*
5522 * Same as mbedtls_ssl_check_curve_tls_id() but with a mbedtls_ecp_group_id.
5523 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005524int mbedtls_ssl_check_curve(const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id)
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005525{
Gilles Peskine449bd832023-01-11 14:50:10 +01005526 uint16_t tls_id = mbedtls_ssl_get_tls_id_from_ecp_group_id(grp_id);
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005527
Gilles Peskine449bd832023-01-11 14:50:10 +01005528 if (tls_id == 0) {
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005529 return -1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005530 }
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005531
Gilles Peskine449bd832023-01-11 14:50:10 +01005532 return mbedtls_ssl_check_curve_tls_id(ssl, tls_id);
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005533}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02005534#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005535
Gilles Peskine449bd832023-01-11 14:50:10 +01005536#if defined(MBEDTLS_DEBUG_C)
Valerio Setti67419f02023-01-04 16:12:42 +01005537#define EC_NAME(_name_) _name_
5538#else
5539#define EC_NAME(_name_) NULL
5540#endif
5541
Valerio Setti18c9fed2022-12-30 17:44:24 +01005542static const struct {
5543 uint16_t tls_id;
5544 mbedtls_ecp_group_id ecp_group_id;
5545 psa_ecc_family_t psa_family;
5546 uint16_t bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01005547 const char *name;
Valerio Setti18c9fed2022-12-30 17:44:24 +01005548} tls_id_match_table[] =
5549{
5550#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_521)
Gilles Peskine449bd832023-01-11 14:50:10 +01005551 { 25, MBEDTLS_ECP_DP_SECP521R1, PSA_ECC_FAMILY_SECP_R1, 521, EC_NAME("secp521r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005552#endif
5553#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED) || defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512)
Gilles Peskine449bd832023-01-11 14:50:10 +01005554 { 28, MBEDTLS_ECP_DP_BP512R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 512, EC_NAME("brainpoolP512r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005555#endif
Valerio Setti67419f02023-01-04 16:12:42 +01005556#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_384)
Gilles Peskine449bd832023-01-11 14:50:10 +01005557 { 24, MBEDTLS_ECP_DP_SECP384R1, PSA_ECC_FAMILY_SECP_R1, 384, EC_NAME("secp384r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005558#endif
Valerio Setti67419f02023-01-04 16:12:42 +01005559#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED) || defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384)
Gilles Peskine449bd832023-01-11 14:50:10 +01005560 { 27, MBEDTLS_ECP_DP_BP384R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 384, EC_NAME("brainpoolP384r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005561#endif
Valerio Setti67419f02023-01-04 16:12:42 +01005562#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_256)
Gilles Peskine449bd832023-01-11 14:50:10 +01005563 { 23, MBEDTLS_ECP_DP_SECP256R1, PSA_ECC_FAMILY_SECP_R1, 256, EC_NAME("secp256r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005564#endif
5565#if defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED) || defined(PSA_WANT_ECC_SECP_K1_256)
Gilles Peskine449bd832023-01-11 14:50:10 +01005566 { 22, MBEDTLS_ECP_DP_SECP256K1, PSA_ECC_FAMILY_SECP_K1, 256, EC_NAME("secp256k1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005567#endif
Valerio Setti67419f02023-01-04 16:12:42 +01005568#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED) || defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256)
Gilles Peskine449bd832023-01-11 14:50:10 +01005569 { 26, MBEDTLS_ECP_DP_BP256R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 256, EC_NAME("brainpoolP256r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005570#endif
5571#if defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_224)
Gilles Peskine449bd832023-01-11 14:50:10 +01005572 { 21, MBEDTLS_ECP_DP_SECP224R1, PSA_ECC_FAMILY_SECP_R1, 224, EC_NAME("secp224r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005573#endif
5574#if defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED) || defined(PSA_WANT_ECC_SECP_K1_224)
Gilles Peskine449bd832023-01-11 14:50:10 +01005575 { 20, MBEDTLS_ECP_DP_SECP224K1, PSA_ECC_FAMILY_SECP_K1, 224, EC_NAME("secp224k1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005576#endif
5577#if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_192)
Gilles Peskine449bd832023-01-11 14:50:10 +01005578 { 19, MBEDTLS_ECP_DP_SECP192R1, PSA_ECC_FAMILY_SECP_R1, 192, EC_NAME("secp192r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005579#endif
5580#if defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED) || defined(PSA_WANT_ECC_SECP_K1_192)
Gilles Peskine449bd832023-01-11 14:50:10 +01005581 { 18, MBEDTLS_ECP_DP_SECP192K1, PSA_ECC_FAMILY_SECP_K1, 192, EC_NAME("secp192k1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005582#endif
5583#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED) || defined(PSA_WANT_ECC_MONTGOMERY_255)
Gilles Peskine449bd832023-01-11 14:50:10 +01005584 { 29, MBEDTLS_ECP_DP_CURVE25519, PSA_ECC_FAMILY_MONTGOMERY, 255, EC_NAME("x25519") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005585#endif
5586#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED) || defined(PSA_WANT_ECC_MONTGOMERY_448)
Gilles Peskine449bd832023-01-11 14:50:10 +01005587 { 30, MBEDTLS_ECP_DP_CURVE448, PSA_ECC_FAMILY_MONTGOMERY, 448, EC_NAME("x448") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005588#endif
5589 { 0, MBEDTLS_ECP_DP_NONE, 0, 0, NULL },
5590};
5591
Gilles Peskine449bd832023-01-11 14:50:10 +01005592int mbedtls_ssl_get_psa_curve_info_from_tls_id(uint16_t tls_id,
5593 psa_ecc_family_t *family,
5594 size_t *bits)
Valerio Setti18c9fed2022-12-30 17:44:24 +01005595{
Gilles Peskine449bd832023-01-11 14:50:10 +01005596 for (int i = 0; tls_id_match_table[i].tls_id != 0; i++) {
5597 if (tls_id_match_table[i].tls_id == tls_id) {
5598 if (family != NULL) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01005599 *family = tls_id_match_table[i].psa_family;
Gilles Peskine449bd832023-01-11 14:50:10 +01005600 }
5601 if (bits != NULL) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01005602 *bits = tls_id_match_table[i].bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01005603 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01005604 return PSA_SUCCESS;
5605 }
5606 }
5607
5608 return PSA_ERROR_NOT_SUPPORTED;
5609}
5610
Gilles Peskine449bd832023-01-11 14:50:10 +01005611mbedtls_ecp_group_id mbedtls_ssl_get_ecp_group_id_from_tls_id(uint16_t tls_id)
Valerio Setti18c9fed2022-12-30 17:44:24 +01005612{
Gilles Peskine449bd832023-01-11 14:50:10 +01005613 for (int i = 0; tls_id_match_table[i].tls_id != 0; i++) {
5614 if (tls_id_match_table[i].tls_id == tls_id) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01005615 return tls_id_match_table[i].ecp_group_id;
Gilles Peskine449bd832023-01-11 14:50:10 +01005616 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01005617 }
5618
5619 return MBEDTLS_ECP_DP_NONE;
5620}
5621
Gilles Peskine449bd832023-01-11 14:50:10 +01005622uint16_t mbedtls_ssl_get_tls_id_from_ecp_group_id(mbedtls_ecp_group_id grp_id)
Valerio Setti18c9fed2022-12-30 17:44:24 +01005623{
Gilles Peskine449bd832023-01-11 14:50:10 +01005624 for (int i = 0; tls_id_match_table[i].ecp_group_id != MBEDTLS_ECP_DP_NONE;
5625 i++) {
5626 if (tls_id_match_table[i].ecp_group_id == grp_id) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01005627 return tls_id_match_table[i].tls_id;
Gilles Peskine449bd832023-01-11 14:50:10 +01005628 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01005629 }
5630
5631 return 0;
5632}
5633
Valerio Setti67419f02023-01-04 16:12:42 +01005634#if defined(MBEDTLS_DEBUG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005635const char *mbedtls_ssl_get_curve_name_from_tls_id(uint16_t tls_id)
Valerio Setti18c9fed2022-12-30 17:44:24 +01005636{
Gilles Peskine449bd832023-01-11 14:50:10 +01005637 for (int i = 0; tls_id_match_table[i].tls_id != 0; i++) {
5638 if (tls_id_match_table[i].tls_id == tls_id) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01005639 return tls_id_match_table[i].name;
Gilles Peskine449bd832023-01-11 14:50:10 +01005640 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01005641 }
5642
5643 return NULL;
5644}
Valerio Setti67419f02023-01-04 16:12:42 +01005645#endif
Valerio Setti18c9fed2022-12-30 17:44:24 +01005646
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005647#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005648int mbedtls_ssl_check_cert_usage(const mbedtls_x509_crt *cert,
5649 const mbedtls_ssl_ciphersuite_t *ciphersuite,
5650 int cert_endpoint,
5651 uint32_t *flags)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005652{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005653 int ret = 0;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005654 int usage = 0;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005655 const char *ext_oid;
5656 size_t ext_len;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005657
Gilles Peskine449bd832023-01-11 14:50:10 +01005658 if (cert_endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005659 /* Server part of the key exchange */
Gilles Peskine449bd832023-01-11 14:50:10 +01005660 switch (ciphersuite->key_exchange) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005661 case MBEDTLS_KEY_EXCHANGE_RSA:
5662 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005663 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005664 break;
5665
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005666 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
5667 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
5668 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
5669 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005670 break;
5671
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005672 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
5673 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005674 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005675 break;
5676
5677 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005678 case MBEDTLS_KEY_EXCHANGE_NONE:
5679 case MBEDTLS_KEY_EXCHANGE_PSK:
5680 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
5681 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02005682 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005683 usage = 0;
5684 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005685 } else {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005686 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
5687 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005688 }
5689
Gilles Peskine449bd832023-01-11 14:50:10 +01005690 if (mbedtls_x509_crt_check_key_usage(cert, usage) != 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005691 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005692 ret = -1;
5693 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005694
Gilles Peskine449bd832023-01-11 14:50:10 +01005695 if (cert_endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005696 ext_oid = MBEDTLS_OID_SERVER_AUTH;
Gilles Peskine449bd832023-01-11 14:50:10 +01005697 ext_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_SERVER_AUTH);
5698 } else {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005699 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
Gilles Peskine449bd832023-01-11 14:50:10 +01005700 ext_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_CLIENT_AUTH);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005701 }
5702
Gilles Peskine449bd832023-01-11 14:50:10 +01005703 if (mbedtls_x509_crt_check_extended_key_usage(cert, ext_oid, ext_len) != 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005704 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005705 ret = -1;
5706 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005707
Gilles Peskine449bd832023-01-11 14:50:10 +01005708 return ret;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005709}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005710#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02005711
Jerry Yu148165c2021-09-24 23:20:59 +08005712#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01005713int mbedtls_ssl_get_handshake_transcript(mbedtls_ssl_context *ssl,
5714 const mbedtls_md_type_t md,
5715 unsigned char *dst,
5716 size_t dst_len,
5717 size_t *olen)
Jerry Yu148165c2021-09-24 23:20:59 +08005718{
Ronald Cronf6893e12022-01-07 22:09:01 +01005719 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5720 psa_hash_operation_t *hash_operation_to_clone;
5721 psa_hash_operation_t hash_operation = psa_hash_operation_init();
5722
Jerry Yu148165c2021-09-24 23:20:59 +08005723 *olen = 0;
Ronald Cronf6893e12022-01-07 22:09:01 +01005724
Gilles Peskine449bd832023-01-11 14:50:10 +01005725 switch (md) {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005726#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01005727 case MBEDTLS_MD_SHA384:
5728 hash_operation_to_clone = &ssl->handshake->fin_sha384_psa;
5729 break;
Ronald Cronf6893e12022-01-07 22:09:01 +01005730#endif
5731
Andrzej Kurek25f27152022-08-17 16:09:31 -04005732#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01005733 case MBEDTLS_MD_SHA256:
5734 hash_operation_to_clone = &ssl->handshake->fin_sha256_psa;
5735 break;
Ronald Cronf6893e12022-01-07 22:09:01 +01005736#endif
5737
Gilles Peskine449bd832023-01-11 14:50:10 +01005738 default:
5739 goto exit;
5740 }
5741
5742 status = psa_hash_clone(hash_operation_to_clone, &hash_operation);
5743 if (status != PSA_SUCCESS) {
Ronald Cronf6893e12022-01-07 22:09:01 +01005744 goto exit;
5745 }
5746
Gilles Peskine449bd832023-01-11 14:50:10 +01005747 status = psa_hash_finish(&hash_operation, dst, dst_len, olen);
5748 if (status != PSA_SUCCESS) {
Ronald Cronf6893e12022-01-07 22:09:01 +01005749 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005750 }
Ronald Cronf6893e12022-01-07 22:09:01 +01005751
5752exit:
Andrzej Kurekeabeb302022-10-17 07:52:51 -04005753#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
5754 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
5755 (void) ssl;
5756#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005757 return psa_ssl_status_to_mbedtls(status);
Jerry Yu148165c2021-09-24 23:20:59 +08005758}
5759#else /* MBEDTLS_USE_PSA_CRYPTO */
5760
Andrzej Kurek25f27152022-08-17 16:09:31 -04005761#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005762MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01005763static int ssl_get_handshake_transcript_sha384(mbedtls_ssl_context *ssl,
5764 unsigned char *dst,
5765 size_t dst_len,
5766 size_t *olen)
Jerry Yu24c0ec32021-09-09 14:21:07 +08005767{
Jerry Yu24c0ec32021-09-09 14:21:07 +08005768 int ret;
5769 mbedtls_sha512_context sha512;
5770
Gilles Peskine449bd832023-01-11 14:50:10 +01005771 if (dst_len < 48) {
5772 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
5773 }
Jerry Yu24c0ec32021-09-09 14:21:07 +08005774
Gilles Peskine449bd832023-01-11 14:50:10 +01005775 mbedtls_sha512_init(&sha512);
5776 mbedtls_sha512_clone(&sha512, &ssl->handshake->fin_sha384);
Jerry Yu24c0ec32021-09-09 14:21:07 +08005777
Gilles Peskine449bd832023-01-11 14:50:10 +01005778 if ((ret = mbedtls_sha512_finish(&sha512, dst)) != 0) {
5779 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_sha512_finish", ret);
Jerry Yu24c0ec32021-09-09 14:21:07 +08005780 goto exit;
5781 }
5782
5783 *olen = 48;
5784
5785exit:
5786
Gilles Peskine449bd832023-01-11 14:50:10 +01005787 mbedtls_sha512_free(&sha512);
5788 return ret;
Jerry Yu24c0ec32021-09-09 14:21:07 +08005789}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005790#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005791
Andrzej Kurek25f27152022-08-17 16:09:31 -04005792#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005793MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01005794static int ssl_get_handshake_transcript_sha256(mbedtls_ssl_context *ssl,
5795 unsigned char *dst,
5796 size_t dst_len,
5797 size_t *olen)
Jerry Yu24c0ec32021-09-09 14:21:07 +08005798{
Jerry Yu24c0ec32021-09-09 14:21:07 +08005799 int ret;
5800 mbedtls_sha256_context sha256;
5801
Gilles Peskine449bd832023-01-11 14:50:10 +01005802 if (dst_len < 32) {
5803 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
5804 }
Jerry Yu24c0ec32021-09-09 14:21:07 +08005805
Gilles Peskine449bd832023-01-11 14:50:10 +01005806 mbedtls_sha256_init(&sha256);
5807 mbedtls_sha256_clone(&sha256, &ssl->handshake->fin_sha256);
Jerry Yuc5aef882021-12-23 20:15:02 +08005808
Gilles Peskine449bd832023-01-11 14:50:10 +01005809 if ((ret = mbedtls_sha256_finish(&sha256, dst)) != 0) {
5810 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_sha256_finish", ret);
Jerry Yu24c0ec32021-09-09 14:21:07 +08005811 goto exit;
5812 }
5813
5814 *olen = 32;
5815
5816exit:
5817
Gilles Peskine449bd832023-01-11 14:50:10 +01005818 mbedtls_sha256_free(&sha256);
5819 return ret;
Jerry Yu24c0ec32021-09-09 14:21:07 +08005820}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005821#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005822
Gilles Peskine449bd832023-01-11 14:50:10 +01005823int mbedtls_ssl_get_handshake_transcript(mbedtls_ssl_context *ssl,
5824 const mbedtls_md_type_t md,
5825 unsigned char *dst,
5826 size_t dst_len,
5827 size_t *olen)
Jerry Yu24c0ec32021-09-09 14:21:07 +08005828{
Gilles Peskine449bd832023-01-11 14:50:10 +01005829 switch (md) {
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005830
Andrzej Kurek25f27152022-08-17 16:09:31 -04005831#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01005832 case MBEDTLS_MD_SHA384:
5833 return ssl_get_handshake_transcript_sha384(ssl, dst, dst_len, olen);
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005834#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005835
Andrzej Kurek25f27152022-08-17 16:09:31 -04005836#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01005837 case MBEDTLS_MD_SHA256:
5838 return ssl_get_handshake_transcript_sha256(ssl, dst, dst_len, olen);
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005839#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005840
Gilles Peskine449bd832023-01-11 14:50:10 +01005841 default:
Andrzej Kurek409248a2022-10-24 10:33:21 -04005842#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01005843 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
5844 (void) ssl;
5845 (void) dst;
5846 (void) dst_len;
5847 (void) olen;
Andrzej Kurek409248a2022-10-24 10:33:21 -04005848#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005849 break;
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005850 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005851 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu24c0ec32021-09-09 14:21:07 +08005852}
XiaokangQian647719a2021-12-07 09:16:29 +00005853
Jerry Yu148165c2021-09-24 23:20:59 +08005854#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005855
Ronald Crone68ab4f2022-10-05 12:46:29 +02005856#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Gabor Mezei078e8032022-04-27 21:17:56 +02005857/* mbedtls_ssl_parse_sig_alg_ext()
5858 *
5859 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
5860 * value (TLS 1.3 RFC8446):
5861 * enum {
5862 * ....
5863 * ecdsa_secp256r1_sha256( 0x0403 ),
5864 * ecdsa_secp384r1_sha384( 0x0503 ),
5865 * ecdsa_secp521r1_sha512( 0x0603 ),
5866 * ....
5867 * } SignatureScheme;
5868 *
5869 * struct {
5870 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
5871 * } SignatureSchemeList;
5872 *
5873 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
5874 * value (TLS 1.2 RFC5246):
5875 * enum {
5876 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
5877 * sha512(6), (255)
5878 * } HashAlgorithm;
5879 *
5880 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
5881 * SignatureAlgorithm;
5882 *
5883 * struct {
5884 * HashAlgorithm hash;
5885 * SignatureAlgorithm signature;
5886 * } SignatureAndHashAlgorithm;
5887 *
5888 * SignatureAndHashAlgorithm
5889 * supported_signature_algorithms<2..2^16-2>;
5890 *
5891 * The TLS 1.3 signature algorithm extension was defined to be a compatible
5892 * generalization of the TLS 1.2 signature algorithm extension.
5893 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
5894 * `SignatureScheme` field of TLS 1.3
5895 *
5896 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005897int mbedtls_ssl_parse_sig_alg_ext(mbedtls_ssl_context *ssl,
5898 const unsigned char *buf,
5899 const unsigned char *end)
Gabor Mezei078e8032022-04-27 21:17:56 +02005900{
5901 const unsigned char *p = buf;
5902 size_t supported_sig_algs_len = 0;
5903 const unsigned char *supported_sig_algs_end;
5904 uint16_t sig_alg;
5905 uint32_t common_idx = 0;
5906
Gilles Peskine449bd832023-01-11 14:50:10 +01005907 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
5908 supported_sig_algs_len = MBEDTLS_GET_UINT16_BE(p, 0);
Gabor Mezei078e8032022-04-27 21:17:56 +02005909 p += 2;
5910
Gilles Peskine449bd832023-01-11 14:50:10 +01005911 memset(ssl->handshake->received_sig_algs, 0,
5912 sizeof(ssl->handshake->received_sig_algs));
Gabor Mezei078e8032022-04-27 21:17:56 +02005913
Gilles Peskine449bd832023-01-11 14:50:10 +01005914 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, supported_sig_algs_len);
Gabor Mezei078e8032022-04-27 21:17:56 +02005915 supported_sig_algs_end = p + supported_sig_algs_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01005916 while (p < supported_sig_algs_end) {
5917 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, supported_sig_algs_end, 2);
5918 sig_alg = MBEDTLS_GET_UINT16_BE(p, 0);
Gabor Mezei078e8032022-04-27 21:17:56 +02005919 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01005920 MBEDTLS_SSL_DEBUG_MSG(4, ("received signature algorithm: 0x%x %s",
5921 sig_alg,
5922 mbedtls_ssl_sig_alg_to_str(sig_alg)));
Jerry Yu2fe6c632022-06-29 10:02:38 +08005923#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01005924 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 &&
5925 (!(mbedtls_ssl_sig_alg_is_supported(ssl, sig_alg) &&
5926 mbedtls_ssl_sig_alg_is_offered(ssl, sig_alg)))) {
Gabor Mezei078e8032022-04-27 21:17:56 +02005927 continue;
Jerry Yu2fe6c632022-06-29 10:02:38 +08005928 }
5929#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gabor Mezei078e8032022-04-27 21:17:56 +02005930
Gilles Peskine449bd832023-01-11 14:50:10 +01005931 MBEDTLS_SSL_DEBUG_MSG(4, ("valid signature algorithm: %s",
5932 mbedtls_ssl_sig_alg_to_str(sig_alg)));
Gabor Mezei078e8032022-04-27 21:17:56 +02005933
Gilles Peskine449bd832023-01-11 14:50:10 +01005934 if (common_idx + 1 < MBEDTLS_RECEIVED_SIG_ALGS_SIZE) {
Gabor Mezei078e8032022-04-27 21:17:56 +02005935 ssl->handshake->received_sig_algs[common_idx] = sig_alg;
5936 common_idx += 1;
5937 }
5938 }
5939 /* Check that we consumed all the message. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005940 if (p != end) {
5941 MBEDTLS_SSL_DEBUG_MSG(1,
5942 ("Signature algorithms extension length misaligned"));
5943 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
5944 MBEDTLS_ERR_SSL_DECODE_ERROR);
5945 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Gabor Mezei078e8032022-04-27 21:17:56 +02005946 }
5947
Gilles Peskine449bd832023-01-11 14:50:10 +01005948 if (common_idx == 0) {
5949 MBEDTLS_SSL_DEBUG_MSG(3, ("no signature algorithm in common"));
5950 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
5951 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
5952 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Gabor Mezei078e8032022-04-27 21:17:56 +02005953 }
5954
Gabor Mezei15b95a62022-05-09 16:37:58 +02005955 ssl->handshake->received_sig_algs[common_idx] = MBEDTLS_TLS_SIG_NONE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005956 return 0;
Gabor Mezei078e8032022-04-27 21:17:56 +02005957}
5958
Ronald Crone68ab4f2022-10-05 12:46:29 +02005959#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Gabor Mezei078e8032022-04-27 21:17:56 +02005960
Jerry Yudc7bd172022-02-17 13:44:15 +08005961#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5962
5963#if defined(MBEDTLS_USE_PSA_CRYPTO)
5964
Gilles Peskine449bd832023-01-11 14:50:10 +01005965static psa_status_t setup_psa_key_derivation(psa_key_derivation_operation_t *derivation,
5966 mbedtls_svc_key_id_t key,
5967 psa_algorithm_t alg,
5968 const unsigned char *raw_psk, size_t raw_psk_length,
5969 const unsigned char *seed, size_t seed_length,
5970 const unsigned char *label, size_t label_length,
5971 const unsigned char *other_secret,
5972 size_t other_secret_length,
5973 size_t capacity)
Jerry Yudc7bd172022-02-17 13:44:15 +08005974{
5975 psa_status_t status;
5976
Gilles Peskine449bd832023-01-11 14:50:10 +01005977 status = psa_key_derivation_setup(derivation, alg);
5978 if (status != PSA_SUCCESS) {
5979 return status;
5980 }
Jerry Yudc7bd172022-02-17 13:44:15 +08005981
Gilles Peskine449bd832023-01-11 14:50:10 +01005982 if (PSA_ALG_IS_TLS12_PRF(alg) || PSA_ALG_IS_TLS12_PSK_TO_MS(alg)) {
5983 status = psa_key_derivation_input_bytes(derivation,
5984 PSA_KEY_DERIVATION_INPUT_SEED,
5985 seed, seed_length);
5986 if (status != PSA_SUCCESS) {
5987 return status;
Przemek Stekiel1f027032022-04-05 17:12:11 +02005988 }
5989
Gilles Peskine449bd832023-01-11 14:50:10 +01005990 if (other_secret != NULL) {
5991 status = psa_key_derivation_input_bytes(derivation,
5992 PSA_KEY_DERIVATION_INPUT_OTHER_SECRET,
5993 other_secret, other_secret_length);
5994 if (status != PSA_SUCCESS) {
5995 return status;
5996 }
5997 }
5998
5999 if (mbedtls_svc_key_id_is_null(key)) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006000 status = psa_key_derivation_input_bytes(
6001 derivation, PSA_KEY_DERIVATION_INPUT_SECRET,
Gilles Peskine449bd832023-01-11 14:50:10 +01006002 raw_psk, raw_psk_length);
6003 } else {
Jerry Yudc7bd172022-02-17 13:44:15 +08006004 status = psa_key_derivation_input_key(
Gilles Peskine449bd832023-01-11 14:50:10 +01006005 derivation, PSA_KEY_DERIVATION_INPUT_SECRET, key);
Jerry Yudc7bd172022-02-17 13:44:15 +08006006 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006007 if (status != PSA_SUCCESS) {
6008 return status;
6009 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006010
Gilles Peskine449bd832023-01-11 14:50:10 +01006011 status = psa_key_derivation_input_bytes(derivation,
6012 PSA_KEY_DERIVATION_INPUT_LABEL,
6013 label, label_length);
6014 if (status != PSA_SUCCESS) {
6015 return status;
6016 }
6017 } else {
6018 return PSA_ERROR_NOT_SUPPORTED;
Jerry Yudc7bd172022-02-17 13:44:15 +08006019 }
6020
Gilles Peskine449bd832023-01-11 14:50:10 +01006021 status = psa_key_derivation_set_capacity(derivation, capacity);
6022 if (status != PSA_SUCCESS) {
6023 return status;
6024 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006025
Gilles Peskine449bd832023-01-11 14:50:10 +01006026 return PSA_SUCCESS;
Jerry Yudc7bd172022-02-17 13:44:15 +08006027}
6028
Andrzej Kurek57d10632022-10-24 10:32:01 -04006029#if defined(PSA_WANT_ALG_SHA_384) || \
6030 defined(PSA_WANT_ALG_SHA_256)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006031MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006032static int tls_prf_generic(mbedtls_md_type_t md_type,
6033 const unsigned char *secret, size_t slen,
6034 const char *label,
6035 const unsigned char *random, size_t rlen,
6036 unsigned char *dstbuf, size_t dlen)
Jerry Yudc7bd172022-02-17 13:44:15 +08006037{
6038 psa_status_t status;
6039 psa_algorithm_t alg;
6040 mbedtls_svc_key_id_t master_key = MBEDTLS_SVC_KEY_ID_INIT;
6041 psa_key_derivation_operation_t derivation =
6042 PSA_KEY_DERIVATION_OPERATION_INIT;
6043
Gilles Peskine449bd832023-01-11 14:50:10 +01006044 if (md_type == MBEDTLS_MD_SHA384) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006045 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
Gilles Peskine449bd832023-01-11 14:50:10 +01006046 } else {
Jerry Yudc7bd172022-02-17 13:44:15 +08006047 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
Gilles Peskine449bd832023-01-11 14:50:10 +01006048 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006049
6050 /* Normally a "secret" should be long enough to be impossible to
6051 * find by brute force, and in particular should not be empty. But
6052 * this PRF is also used to derive an IV, in particular in EAP-TLS,
6053 * and for this use case it makes sense to have a 0-length "secret".
6054 * Since the key API doesn't allow importing a key of length 0,
6055 * keep master_key=0, which setup_psa_key_derivation() understands
6056 * to mean a 0-length "secret" input. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006057 if (slen != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006058 psa_key_attributes_t key_attributes = psa_key_attributes_init();
Gilles Peskine449bd832023-01-11 14:50:10 +01006059 psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE);
6060 psa_set_key_algorithm(&key_attributes, alg);
6061 psa_set_key_type(&key_attributes, PSA_KEY_TYPE_DERIVE);
Jerry Yudc7bd172022-02-17 13:44:15 +08006062
Gilles Peskine449bd832023-01-11 14:50:10 +01006063 status = psa_import_key(&key_attributes, secret, slen, &master_key);
6064 if (status != PSA_SUCCESS) {
6065 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
6066 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006067 }
6068
Gilles Peskine449bd832023-01-11 14:50:10 +01006069 status = setup_psa_key_derivation(&derivation,
6070 master_key, alg,
6071 NULL, 0,
6072 random, rlen,
6073 (unsigned char const *) label,
6074 (size_t) strlen(label),
6075 NULL, 0,
6076 dlen);
6077 if (status != PSA_SUCCESS) {
6078 psa_key_derivation_abort(&derivation);
6079 psa_destroy_key(master_key);
6080 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yudc7bd172022-02-17 13:44:15 +08006081 }
6082
Gilles Peskine449bd832023-01-11 14:50:10 +01006083 status = psa_key_derivation_output_bytes(&derivation, dstbuf, dlen);
6084 if (status != PSA_SUCCESS) {
6085 psa_key_derivation_abort(&derivation);
6086 psa_destroy_key(master_key);
6087 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yudc7bd172022-02-17 13:44:15 +08006088 }
6089
Gilles Peskine449bd832023-01-11 14:50:10 +01006090 status = psa_key_derivation_abort(&derivation);
6091 if (status != PSA_SUCCESS) {
6092 psa_destroy_key(master_key);
6093 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yudc7bd172022-02-17 13:44:15 +08006094 }
6095
Gilles Peskine449bd832023-01-11 14:50:10 +01006096 if (!mbedtls_svc_key_id_is_null(master_key)) {
6097 status = psa_destroy_key(master_key);
6098 }
6099 if (status != PSA_SUCCESS) {
6100 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
6101 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006102
Gilles Peskine449bd832023-01-11 14:50:10 +01006103 return 0;
Jerry Yudc7bd172022-02-17 13:44:15 +08006104}
Andrzej Kurek57d10632022-10-24 10:32:01 -04006105#endif /* PSA_WANT_ALG_SHA_256 || PSA_WANT_ALG_SHA_384 */
Jerry Yudc7bd172022-02-17 13:44:15 +08006106#else /* MBEDTLS_USE_PSA_CRYPTO */
6107
Andrzej Kurek57d10632022-10-24 10:32:01 -04006108#if defined(MBEDTLS_MD_C) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01006109 (defined(MBEDTLS_SHA256_C) || \
6110 defined(MBEDTLS_SHA384_C))
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006111MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006112static int tls_prf_generic(mbedtls_md_type_t md_type,
6113 const unsigned char *secret, size_t slen,
6114 const char *label,
6115 const unsigned char *random, size_t rlen,
6116 unsigned char *dstbuf, size_t dlen)
Jerry Yudc7bd172022-02-17 13:44:15 +08006117{
6118 size_t nb;
6119 size_t i, j, k, md_len;
6120 unsigned char *tmp;
6121 size_t tmp_len = 0;
6122 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
6123 const mbedtls_md_info_t *md_info;
6124 mbedtls_md_context_t md_ctx;
6125 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6126
Gilles Peskine449bd832023-01-11 14:50:10 +01006127 mbedtls_md_init(&md_ctx);
Jerry Yudc7bd172022-02-17 13:44:15 +08006128
Gilles Peskine449bd832023-01-11 14:50:10 +01006129 if ((md_info = mbedtls_md_info_from_type(md_type)) == NULL) {
6130 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
6131 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006132
Gilles Peskine449bd832023-01-11 14:50:10 +01006133 md_len = mbedtls_md_get_size(md_info);
Jerry Yudc7bd172022-02-17 13:44:15 +08006134
Gilles Peskine449bd832023-01-11 14:50:10 +01006135 tmp_len = md_len + strlen(label) + rlen;
6136 tmp = mbedtls_calloc(1, tmp_len);
6137 if (tmp == NULL) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006138 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
6139 goto exit;
6140 }
6141
Gilles Peskine449bd832023-01-11 14:50:10 +01006142 nb = strlen(label);
6143 memcpy(tmp + md_len, label, nb);
6144 memcpy(tmp + md_len + nb, random, rlen);
Jerry Yudc7bd172022-02-17 13:44:15 +08006145 nb += rlen;
6146
6147 /*
6148 * Compute P_<hash>(secret, label + random)[0..dlen]
6149 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006150 if ((ret = mbedtls_md_setup(&md_ctx, md_info, 1)) != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006151 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006152 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006153
Gilles Peskine449bd832023-01-11 14:50:10 +01006154 ret = mbedtls_md_hmac_starts(&md_ctx, secret, slen);
6155 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006156 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006157 }
6158 ret = mbedtls_md_hmac_update(&md_ctx, tmp + md_len, nb);
6159 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006160 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006161 }
6162 ret = mbedtls_md_hmac_finish(&md_ctx, tmp);
6163 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006164 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006165 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006166
Gilles Peskine449bd832023-01-11 14:50:10 +01006167 for (i = 0; i < dlen; i += md_len) {
6168 ret = mbedtls_md_hmac_reset(&md_ctx);
6169 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006170 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006171 }
6172 ret = mbedtls_md_hmac_update(&md_ctx, tmp, md_len + nb);
6173 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006174 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006175 }
6176 ret = mbedtls_md_hmac_finish(&md_ctx, h_i);
6177 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006178 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006179 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006180
Gilles Peskine449bd832023-01-11 14:50:10 +01006181 ret = mbedtls_md_hmac_reset(&md_ctx);
6182 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006183 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006184 }
6185 ret = mbedtls_md_hmac_update(&md_ctx, tmp, md_len);
6186 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006187 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006188 }
6189 ret = mbedtls_md_hmac_finish(&md_ctx, tmp);
6190 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006191 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006192 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006193
Gilles Peskine449bd832023-01-11 14:50:10 +01006194 k = (i + md_len > dlen) ? dlen % md_len : md_len;
Jerry Yudc7bd172022-02-17 13:44:15 +08006195
Gilles Peskine449bd832023-01-11 14:50:10 +01006196 for (j = 0; j < k; j++) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006197 dstbuf[i + j] = h_i[j];
Gilles Peskine449bd832023-01-11 14:50:10 +01006198 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006199 }
6200
6201exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006202 mbedtls_md_free(&md_ctx);
Jerry Yudc7bd172022-02-17 13:44:15 +08006203
Gilles Peskine449bd832023-01-11 14:50:10 +01006204 if (tmp != NULL) {
6205 mbedtls_platform_zeroize(tmp, tmp_len);
6206 }
Dave Rodgman29b9b2b2022-11-01 16:08:14 +00006207
Gilles Peskine449bd832023-01-11 14:50:10 +01006208 mbedtls_platform_zeroize(h_i, sizeof(h_i));
Jerry Yudc7bd172022-02-17 13:44:15 +08006209
Gilles Peskine449bd832023-01-11 14:50:10 +01006210 mbedtls_free(tmp);
Jerry Yudc7bd172022-02-17 13:44:15 +08006211
Gilles Peskine449bd832023-01-11 14:50:10 +01006212 return ret;
Jerry Yudc7bd172022-02-17 13:44:15 +08006213}
Andrzej Kurek57d10632022-10-24 10:32:01 -04006214#endif /* MBEDTLS_MD_C && ( MBEDTLS_SHA256_C || MBEDTLS_SHA384_C ) */
Jerry Yudc7bd172022-02-17 13:44:15 +08006215#endif /* MBEDTLS_USE_PSA_CRYPTO */
6216
Andrzej Kurek25f27152022-08-17 16:09:31 -04006217#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006218MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006219static int tls_prf_sha256(const unsigned char *secret, size_t slen,
6220 const char *label,
6221 const unsigned char *random, size_t rlen,
6222 unsigned char *dstbuf, size_t dlen)
Jerry Yudc7bd172022-02-17 13:44:15 +08006223{
Gilles Peskine449bd832023-01-11 14:50:10 +01006224 return tls_prf_generic(MBEDTLS_MD_SHA256, secret, slen,
6225 label, random, rlen, dstbuf, dlen);
Jerry Yudc7bd172022-02-17 13:44:15 +08006226}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04006227#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudc7bd172022-02-17 13:44:15 +08006228
Andrzej Kurek25f27152022-08-17 16:09:31 -04006229#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006230MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006231static int tls_prf_sha384(const unsigned char *secret, size_t slen,
6232 const char *label,
6233 const unsigned char *random, size_t rlen,
6234 unsigned char *dstbuf, size_t dlen)
Jerry Yudc7bd172022-02-17 13:44:15 +08006235{
Gilles Peskine449bd832023-01-11 14:50:10 +01006236 return tls_prf_generic(MBEDTLS_MD_SHA384, secret, slen,
6237 label, random, rlen, dstbuf, dlen);
Jerry Yudc7bd172022-02-17 13:44:15 +08006238}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04006239#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudc7bd172022-02-17 13:44:15 +08006240
Jerry Yuf009d862022-02-17 14:01:37 +08006241/*
6242 * Set appropriate PRF function and other SSL / TLS1.2 functions
6243 *
6244 * Inputs:
Jerry Yuf009d862022-02-17 14:01:37 +08006245 * - hash associated with the ciphersuite (only used by TLS 1.2)
6246 *
6247 * Outputs:
6248 * - the tls_prf, calc_verify and calc_finished members of handshake structure
6249 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006250MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006251static int ssl_set_handshake_prfs(mbedtls_ssl_handshake_params *handshake,
6252 mbedtls_md_type_t hash)
Jerry Yuf009d862022-02-17 14:01:37 +08006253{
Andrzej Kurek25f27152022-08-17 16:09:31 -04006254#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01006255 if (hash == MBEDTLS_MD_SHA384) {
Jerry Yuf009d862022-02-17 14:01:37 +08006256 handshake->tls_prf = tls_prf_sha384;
6257 handshake->calc_verify = ssl_calc_verify_tls_sha384;
6258 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Gilles Peskine449bd832023-01-11 14:50:10 +01006259 } else
Jerry Yuf009d862022-02-17 14:01:37 +08006260#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04006261#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuf009d862022-02-17 14:01:37 +08006262 {
Ronald Cron81591aa2022-03-07 09:05:51 +01006263 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08006264 handshake->tls_prf = tls_prf_sha256;
6265 handshake->calc_verify = ssl_calc_verify_tls_sha256;
6266 handshake->calc_finished = ssl_calc_finished_tls_sha256;
6267 }
Ronald Cron81591aa2022-03-07 09:05:51 +01006268#else
Jerry Yuf009d862022-02-17 14:01:37 +08006269 {
Jerry Yuf009d862022-02-17 14:01:37 +08006270 (void) handshake;
Ronald Cron81591aa2022-03-07 09:05:51 +01006271 (void) hash;
Gilles Peskine449bd832023-01-11 14:50:10 +01006272 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yuf009d862022-02-17 14:01:37 +08006273 }
Ronald Cron81591aa2022-03-07 09:05:51 +01006274#endif
Jerry Yuf009d862022-02-17 14:01:37 +08006275
Gilles Peskine449bd832023-01-11 14:50:10 +01006276 return 0;
Jerry Yuf009d862022-02-17 14:01:37 +08006277}
Jerry Yud6ab2352022-02-17 14:03:43 +08006278
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006279/*
6280 * Compute master secret if needed
6281 *
6282 * Parameters:
6283 * [in/out] handshake
6284 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
6285 * (PSA-PSK) ciphersuite_info, psk_opaque
6286 * [out] premaster (cleared)
6287 * [out] master
6288 * [in] ssl: optionally used for debugging, EMS and PSA-PSK
6289 * debug: conf->f_dbg, conf->p_dbg
6290 * EMS: passed to calc_verify (debug + session_negotiate)
Ronald Crona25cf582022-03-07 11:10:36 +01006291 * PSA-PSA: conf
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006292 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006293MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006294static int ssl_compute_master(mbedtls_ssl_handshake_params *handshake,
6295 unsigned char *master,
6296 const mbedtls_ssl_context *ssl)
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006297{
6298 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6299
6300 /* cf. RFC 5246, Section 8.1:
6301 * "The master secret is always exactly 48 bytes in length." */
6302 size_t const master_secret_len = 48;
6303
6304#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
6305 unsigned char session_hash[48];
6306#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
6307
6308 /* The label for the KDF used for key expansion.
6309 * This is either "master secret" or "extended master secret"
6310 * depending on whether the Extended Master Secret extension
6311 * is used. */
6312 char const *lbl = "master secret";
6313
Przemek Stekielae4ed302022-04-05 17:15:55 +02006314 /* The seed for the KDF used for key expansion.
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006315 * - If the Extended Master Secret extension is not used,
6316 * this is ClientHello.Random + ServerHello.Random
6317 * (see Sect. 8.1 in RFC 5246).
6318 * - If the Extended Master Secret extension is used,
6319 * this is the transcript of the handshake so far.
6320 * (see Sect. 4 in RFC 7627). */
Przemek Stekielae4ed302022-04-05 17:15:55 +02006321 unsigned char const *seed = handshake->randbytes;
6322 size_t seed_len = 64;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006323
6324#if !defined(MBEDTLS_DEBUG_C) && \
6325 !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \
6326 !(defined(MBEDTLS_USE_PSA_CRYPTO) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01006327 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED))
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006328 ssl = NULL; /* make sure we don't use it except for those cases */
6329 (void) ssl;
6330#endif
6331
Gilles Peskine449bd832023-01-11 14:50:10 +01006332 if (handshake->resume != 0) {
6333 MBEDTLS_SSL_DEBUG_MSG(3, ("no premaster (session resumed)"));
6334 return 0;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006335 }
6336
6337#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Gilles Peskine449bd832023-01-11 14:50:10 +01006338 if (handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED) {
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006339 lbl = "extended master secret";
Przemek Stekielae4ed302022-04-05 17:15:55 +02006340 seed = session_hash;
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01006341 ret = handshake->calc_verify(ssl, session_hash, &seed_len);
6342 if (ret != 0) {
6343 MBEDTLS_SSL_DEBUG_RET(1, "calc_verify", ret);
6344 }
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006345
Gilles Peskine449bd832023-01-11 14:50:10 +01006346 MBEDTLS_SSL_DEBUG_BUF(3, "session hash for extended master secret",
6347 session_hash, seed_len);
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006348 }
Przemek Stekiel169bf0b2022-04-29 07:53:29 +02006349#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006350
Przemek Stekiel99114f32022-04-22 11:20:09 +02006351#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Przemek Stekiel8a4b7fd2022-04-28 09:22:22 +02006352 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006353 if (mbedtls_ssl_ciphersuite_uses_psk(handshake->ciphersuite_info) == 1) {
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006354 /* Perform PSK-to-MS expansion in a single step. */
6355 psa_status_t status;
6356 psa_algorithm_t alg;
6357 mbedtls_svc_key_id_t psk;
6358 psa_key_derivation_operation_t derivation =
6359 PSA_KEY_DERIVATION_OPERATION_INIT;
6360 mbedtls_md_type_t hash_alg = handshake->ciphersuite_info->mac;
6361
Gilles Peskine449bd832023-01-11 14:50:10 +01006362 MBEDTLS_SSL_DEBUG_MSG(2, ("perform PSA-based PSK-to-MS expansion"));
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006363
Gilles Peskine449bd832023-01-11 14:50:10 +01006364 psk = mbedtls_ssl_get_opaque_psk(ssl);
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006365
Gilles Peskine449bd832023-01-11 14:50:10 +01006366 if (hash_alg == MBEDTLS_MD_SHA384) {
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006367 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
Gilles Peskine449bd832023-01-11 14:50:10 +01006368 } else {
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006369 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
Gilles Peskine449bd832023-01-11 14:50:10 +01006370 }
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006371
Przemek Stekiel51a1f362022-04-13 08:57:06 +02006372 size_t other_secret_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006373 unsigned char *other_secret = NULL;
Przemek Stekielc2033402022-04-05 17:19:41 +02006374
Gilles Peskine449bd832023-01-11 14:50:10 +01006375 switch (handshake->ciphersuite_info->key_exchange) {
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006376 /* Provide other secret.
Przemek Stekiel51a1f362022-04-13 08:57:06 +02006377 * Other secret is stored in premaster, where first 2 bytes hold the
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006378 * length of the other key.
Przemek Stekielc2033402022-04-05 17:19:41 +02006379 */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006380 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Przemek Stekiel8abcee92022-04-28 09:16:28 +02006381 /* For RSA-PSK other key length is always 48 bytes. */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006382 other_secret_len = 48;
6383 other_secret = handshake->premaster + 2;
6384 break;
6385 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006386 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006387 other_secret_len = MBEDTLS_GET_UINT16_BE(handshake->premaster, 0);
6388 other_secret = handshake->premaster + 2;
6389 break;
6390 default:
6391 break;
Przemek Stekielc2033402022-04-05 17:19:41 +02006392 }
6393
Gilles Peskine449bd832023-01-11 14:50:10 +01006394 status = setup_psa_key_derivation(&derivation, psk, alg,
6395 ssl->conf->psk, ssl->conf->psk_len,
6396 seed, seed_len,
6397 (unsigned char const *) lbl,
6398 (size_t) strlen(lbl),
6399 other_secret, other_secret_len,
6400 master_secret_len);
6401 if (status != PSA_SUCCESS) {
6402 psa_key_derivation_abort(&derivation);
6403 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006404 }
6405
Gilles Peskine449bd832023-01-11 14:50:10 +01006406 status = psa_key_derivation_output_bytes(&derivation,
6407 master,
6408 master_secret_len);
6409 if (status != PSA_SUCCESS) {
6410 psa_key_derivation_abort(&derivation);
6411 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006412 }
6413
Gilles Peskine449bd832023-01-11 14:50:10 +01006414 status = psa_key_derivation_abort(&derivation);
6415 if (status != PSA_SUCCESS) {
6416 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
6417 }
6418 } else
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006419#endif
6420 {
Neil Armstrongca7d5062022-05-31 14:43:23 +02006421#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01006422 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
6423 if (handshake->ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE) {
Neil Armstrongca7d5062022-05-31 14:43:23 +02006424 psa_status_t status;
6425 psa_algorithm_t alg = PSA_ALG_TLS12_ECJPAKE_TO_PMS;
6426 psa_key_derivation_operation_t derivation =
6427 PSA_KEY_DERIVATION_OPERATION_INIT;
6428
Gilles Peskine449bd832023-01-11 14:50:10 +01006429 MBEDTLS_SSL_DEBUG_MSG(2, ("perform PSA-based PMS KDF for ECJPAKE"));
Neil Armstrongca7d5062022-05-31 14:43:23 +02006430
6431 handshake->pmslen = PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE;
6432
Gilles Peskine449bd832023-01-11 14:50:10 +01006433 status = psa_key_derivation_setup(&derivation, alg);
6434 if (status != PSA_SUCCESS) {
6435 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Neil Armstrongca7d5062022-05-31 14:43:23 +02006436 }
6437
Gilles Peskine449bd832023-01-11 14:50:10 +01006438 status = psa_key_derivation_set_capacity(&derivation,
6439 PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE);
6440 if (status != PSA_SUCCESS) {
6441 psa_key_derivation_abort(&derivation);
6442 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Neil Armstrongca7d5062022-05-31 14:43:23 +02006443 }
6444
Gilles Peskine449bd832023-01-11 14:50:10 +01006445 status = psa_pake_get_implicit_key(&handshake->psa_pake_ctx,
6446 &derivation);
6447 if (status != PSA_SUCCESS) {
6448 psa_key_derivation_abort(&derivation);
6449 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Neil Armstrongca7d5062022-05-31 14:43:23 +02006450 }
6451
Gilles Peskine449bd832023-01-11 14:50:10 +01006452 status = psa_key_derivation_output_bytes(&derivation,
6453 handshake->premaster,
6454 handshake->pmslen);
6455 if (status != PSA_SUCCESS) {
6456 psa_key_derivation_abort(&derivation);
6457 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
6458 }
6459
6460 status = psa_key_derivation_abort(&derivation);
6461 if (status != PSA_SUCCESS) {
6462 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Neil Armstrongca7d5062022-05-31 14:43:23 +02006463 }
6464 }
6465#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01006466 ret = handshake->tls_prf(handshake->premaster, handshake->pmslen,
6467 lbl, seed, seed_len,
6468 master,
6469 master_secret_len);
6470 if (ret != 0) {
6471 MBEDTLS_SSL_DEBUG_RET(1, "prf", ret);
6472 return ret;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006473 }
6474
Gilles Peskine449bd832023-01-11 14:50:10 +01006475 MBEDTLS_SSL_DEBUG_BUF(3, "premaster secret",
6476 handshake->premaster,
6477 handshake->pmslen);
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006478
Gilles Peskine449bd832023-01-11 14:50:10 +01006479 mbedtls_platform_zeroize(handshake->premaster,
6480 sizeof(handshake->premaster));
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006481 }
6482
Gilles Peskine449bd832023-01-11 14:50:10 +01006483 return 0;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006484}
6485
Gilles Peskine449bd832023-01-11 14:50:10 +01006486int mbedtls_ssl_derive_keys(mbedtls_ssl_context *ssl)
Jerry Yud62f87e2022-02-17 14:09:02 +08006487{
6488 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6489 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
6490 ssl->handshake->ciphersuite_info;
6491
Gilles Peskine449bd832023-01-11 14:50:10 +01006492 MBEDTLS_SSL_DEBUG_MSG(2, ("=> derive keys"));
Jerry Yud62f87e2022-02-17 14:09:02 +08006493
6494 /* Set PRF, calc_verify and calc_finished function pointers */
Gilles Peskine449bd832023-01-11 14:50:10 +01006495 ret = ssl_set_handshake_prfs(ssl->handshake,
6496 ciphersuite_info->mac);
6497 if (ret != 0) {
6498 MBEDTLS_SSL_DEBUG_RET(1, "ssl_set_handshake_prfs", ret);
6499 return ret;
Jerry Yud62f87e2022-02-17 14:09:02 +08006500 }
6501
6502 /* Compute master secret if needed */
Gilles Peskine449bd832023-01-11 14:50:10 +01006503 ret = ssl_compute_master(ssl->handshake,
6504 ssl->session_negotiate->master,
6505 ssl);
6506 if (ret != 0) {
6507 MBEDTLS_SSL_DEBUG_RET(1, "ssl_compute_master", ret);
6508 return ret;
Jerry Yud62f87e2022-02-17 14:09:02 +08006509 }
6510
6511 /* Swap the client and server random values:
6512 * - MS derivation wanted client+server (RFC 5246 8.1)
6513 * - key derivation wants server+client (RFC 5246 6.3) */
6514 {
6515 unsigned char tmp[64];
Gilles Peskine449bd832023-01-11 14:50:10 +01006516 memcpy(tmp, ssl->handshake->randbytes, 64);
6517 memcpy(ssl->handshake->randbytes, tmp + 32, 32);
6518 memcpy(ssl->handshake->randbytes + 32, tmp, 32);
6519 mbedtls_platform_zeroize(tmp, sizeof(tmp));
Jerry Yud62f87e2022-02-17 14:09:02 +08006520 }
6521
6522 /* Populate transform structure */
Gilles Peskine449bd832023-01-11 14:50:10 +01006523 ret = ssl_tls12_populate_transform(ssl->transform_negotiate,
6524 ssl->session_negotiate->ciphersuite,
6525 ssl->session_negotiate->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02006526#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01006527 ssl->session_negotiate->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02006528#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01006529 ssl->handshake->tls_prf,
6530 ssl->handshake->randbytes,
6531 ssl->tls_version,
6532 ssl->conf->endpoint,
6533 ssl);
6534 if (ret != 0) {
6535 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls12_populate_transform", ret);
6536 return ret;
Jerry Yud62f87e2022-02-17 14:09:02 +08006537 }
6538
6539 /* We no longer need Server/ClientHello.random values */
Gilles Peskine449bd832023-01-11 14:50:10 +01006540 mbedtls_platform_zeroize(ssl->handshake->randbytes,
6541 sizeof(ssl->handshake->randbytes));
Jerry Yud62f87e2022-02-17 14:09:02 +08006542
Gilles Peskine449bd832023-01-11 14:50:10 +01006543 MBEDTLS_SSL_DEBUG_MSG(2, ("<= derive keys"));
Jerry Yud62f87e2022-02-17 14:09:02 +08006544
Gilles Peskine449bd832023-01-11 14:50:10 +01006545 return 0;
Jerry Yud62f87e2022-02-17 14:09:02 +08006546}
Jerry Yu8392e0d2022-02-17 14:10:24 +08006547
Gilles Peskine449bd832023-01-11 14:50:10 +01006548int mbedtls_ssl_set_calc_verify_md(mbedtls_ssl_context *ssl, int md)
Ronald Cron4dcbca92022-03-07 10:21:40 +01006549{
Gilles Peskine449bd832023-01-11 14:50:10 +01006550 switch (md) {
Andrzej Kurek25f27152022-08-17 16:09:31 -04006551#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron4dcbca92022-03-07 10:21:40 +01006552 case MBEDTLS_SSL_HASH_SHA384:
6553 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
6554 break;
6555#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04006556#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron4dcbca92022-03-07 10:21:40 +01006557 case MBEDTLS_SSL_HASH_SHA256:
6558 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
6559 break;
6560#endif
6561 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006562 return -1;
Ronald Cron4dcbca92022-03-07 10:21:40 +01006563 }
Andrzej Kurekeabeb302022-10-17 07:52:51 -04006564#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
6565 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
6566 (void) ssl;
6567#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01006568 return 0;
Ronald Cron4dcbca92022-03-07 10:21:40 +01006569}
6570
Andrzej Kurek25f27152022-08-17 16:09:31 -04006571#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01006572int ssl_calc_verify_tls_sha256(const mbedtls_ssl_context *ssl,
6573 unsigned char *hash,
6574 size_t *hlen)
Jerry Yu8392e0d2022-02-17 14:10:24 +08006575{
6576#if defined(MBEDTLS_USE_PSA_CRYPTO)
6577 size_t hash_size;
6578 psa_status_t status;
6579 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
6580
Gilles Peskine449bd832023-01-11 14:50:10 +01006581 MBEDTLS_SSL_DEBUG_MSG(2, ("=> PSA calc verify sha256"));
6582 status = psa_hash_clone(&ssl->handshake->fin_sha256_psa, &sha256_psa);
6583 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006584 goto exit;
Jerry Yu8392e0d2022-02-17 14:10:24 +08006585 }
6586
Gilles Peskine449bd832023-01-11 14:50:10 +01006587 status = psa_hash_finish(&sha256_psa, hash, 32, &hash_size);
6588 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006589 goto exit;
Jerry Yu8392e0d2022-02-17 14:10:24 +08006590 }
6591
6592 *hlen = 32;
Gilles Peskine449bd832023-01-11 14:50:10 +01006593 MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated verify result", hash, *hlen);
6594 MBEDTLS_SSL_DEBUG_MSG(2, ("<= PSA calc verify"));
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006595
6596exit:
6597 psa_hash_abort(&sha256_psa);
6598 return mbedtls_md_error_from_psa(status);
Jerry Yu8392e0d2022-02-17 14:10:24 +08006599#else
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006600 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu8392e0d2022-02-17 14:10:24 +08006601 mbedtls_sha256_context sha256;
6602
Gilles Peskine449bd832023-01-11 14:50:10 +01006603 mbedtls_sha256_init(&sha256);
Jerry Yu8392e0d2022-02-17 14:10:24 +08006604
Gilles Peskine449bd832023-01-11 14:50:10 +01006605 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc verify sha256"));
Jerry Yu8392e0d2022-02-17 14:10:24 +08006606
Gilles Peskine449bd832023-01-11 14:50:10 +01006607 mbedtls_sha256_clone(&sha256, &ssl->handshake->fin_sha256);
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006608
6609 ret = mbedtls_sha256_finish(&sha256, hash);
6610 if (ret != 0) {
6611 goto exit;
6612 }
Jerry Yu8392e0d2022-02-17 14:10:24 +08006613
6614 *hlen = 32;
6615
Gilles Peskine449bd832023-01-11 14:50:10 +01006616 MBEDTLS_SSL_DEBUG_BUF(3, "calculated verify result", hash, *hlen);
6617 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc verify"));
Jerry Yu8392e0d2022-02-17 14:10:24 +08006618
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006619exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006620 mbedtls_sha256_free(&sha256);
Jerry Yu8392e0d2022-02-17 14:10:24 +08006621#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01006622 return 0;
Jerry Yu8392e0d2022-02-17 14:10:24 +08006623}
Andrzej Kurek25f27152022-08-17 16:09:31 -04006624#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu8392e0d2022-02-17 14:10:24 +08006625
Andrzej Kurek25f27152022-08-17 16:09:31 -04006626#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01006627int ssl_calc_verify_tls_sha384(const mbedtls_ssl_context *ssl,
6628 unsigned char *hash,
6629 size_t *hlen)
Jerry Yuc1cb3842022-02-17 14:13:48 +08006630{
6631#if defined(MBEDTLS_USE_PSA_CRYPTO)
6632 size_t hash_size;
6633 psa_status_t status;
6634 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
6635
Gilles Peskine449bd832023-01-11 14:50:10 +01006636 MBEDTLS_SSL_DEBUG_MSG(2, ("=> PSA calc verify sha384"));
6637 status = psa_hash_clone(&ssl->handshake->fin_sha384_psa, &sha384_psa);
6638 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006639 goto exit;
Jerry Yuc1cb3842022-02-17 14:13:48 +08006640 }
6641
Gilles Peskine449bd832023-01-11 14:50:10 +01006642 status = psa_hash_finish(&sha384_psa, hash, 48, &hash_size);
6643 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006644 goto exit;
Jerry Yuc1cb3842022-02-17 14:13:48 +08006645 }
6646
6647 *hlen = 48;
Gilles Peskine449bd832023-01-11 14:50:10 +01006648 MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated verify result", hash, *hlen);
6649 MBEDTLS_SSL_DEBUG_MSG(2, ("<= PSA calc verify"));
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006650
6651exit:
6652 psa_hash_abort(&sha384_psa);
6653 return mbedtls_md_error_from_psa(status);
Jerry Yuc1cb3842022-02-17 14:13:48 +08006654#else
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006655 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yuc1cb3842022-02-17 14:13:48 +08006656 mbedtls_sha512_context sha512;
6657
Gilles Peskine449bd832023-01-11 14:50:10 +01006658 mbedtls_sha512_init(&sha512);
Jerry Yuc1cb3842022-02-17 14:13:48 +08006659
Gilles Peskine449bd832023-01-11 14:50:10 +01006660 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc verify sha384"));
Jerry Yuc1cb3842022-02-17 14:13:48 +08006661
Gilles Peskine449bd832023-01-11 14:50:10 +01006662 mbedtls_sha512_clone(&sha512, &ssl->handshake->fin_sha384);
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006663
6664 ret = mbedtls_sha512_finish(&sha512, hash);
6665 if (ret != 0) {
6666 goto exit;
6667 }
Jerry Yuc1cb3842022-02-17 14:13:48 +08006668
6669 *hlen = 48;
6670
Gilles Peskine449bd832023-01-11 14:50:10 +01006671 MBEDTLS_SSL_DEBUG_BUF(3, "calculated verify result", hash, *hlen);
6672 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc verify"));
Jerry Yuc1cb3842022-02-17 14:13:48 +08006673
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006674exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006675 mbedtls_sha512_free(&sha512);
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006676 return ret;
Jerry Yuc1cb3842022-02-17 14:13:48 +08006677#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yuc1cb3842022-02-17 14:13:48 +08006678}
Andrzej Kurek25f27152022-08-17 16:09:31 -04006679#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yuc1cb3842022-02-17 14:13:48 +08006680
Neil Armstrong80f6f322022-05-03 17:56:38 +02006681#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
6682 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006683int mbedtls_ssl_psk_derive_premaster(mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex)
Jerry Yuce3dca42022-02-17 14:16:37 +08006684{
6685 unsigned char *p = ssl->handshake->premaster;
Gilles Peskine449bd832023-01-11 14:50:10 +01006686 unsigned char *end = p + sizeof(ssl->handshake->premaster);
Jerry Yuce3dca42022-02-17 14:16:37 +08006687 const unsigned char *psk = NULL;
6688 size_t psk_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006689 int psk_ret = mbedtls_ssl_get_psk(ssl, &psk, &psk_len);
Jerry Yuce3dca42022-02-17 14:16:37 +08006690
Gilles Peskine449bd832023-01-11 14:50:10 +01006691 if (psk_ret == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED) {
Jerry Yuce3dca42022-02-17 14:16:37 +08006692 /*
6693 * This should never happen because the existence of a PSK is always
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006694 * checked before calling this function.
6695 *
6696 * The exception is opaque DHE-PSK. For DHE-PSK fill premaster with
Przemek Stekiel8abcee92022-04-28 09:16:28 +02006697 * the shared secret without PSK.
Jerry Yuce3dca42022-02-17 14:16:37 +08006698 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006699 if (key_ex != MBEDTLS_KEY_EXCHANGE_DHE_PSK) {
6700 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
6701 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006702 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006703 }
6704
6705 /*
6706 * PMS = struct {
6707 * opaque other_secret<0..2^16-1>;
6708 * opaque psk<0..2^16-1>;
6709 * };
6710 * with "other_secret" depending on the particular key exchange
6711 */
6712#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006713 if (key_ex == MBEDTLS_KEY_EXCHANGE_PSK) {
6714 if (end - p < 2) {
6715 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6716 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006717
Gilles Peskine449bd832023-01-11 14:50:10 +01006718 MBEDTLS_PUT_UINT16_BE(psk_len, p, 0);
Jerry Yuce3dca42022-02-17 14:16:37 +08006719 p += 2;
6720
Gilles Peskine449bd832023-01-11 14:50:10 +01006721 if (end < p || (size_t) (end - p) < psk_len) {
6722 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6723 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006724
Gilles Peskine449bd832023-01-11 14:50:10 +01006725 memset(p, 0, psk_len);
Jerry Yuce3dca42022-02-17 14:16:37 +08006726 p += psk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01006727 } else
Jerry Yuce3dca42022-02-17 14:16:37 +08006728#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
6729#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006730 if (key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK) {
Jerry Yuce3dca42022-02-17 14:16:37 +08006731 /*
6732 * other_secret already set by the ClientKeyExchange message,
6733 * and is 48 bytes long
6734 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006735 if (end - p < 2) {
6736 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6737 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006738
6739 *p++ = 0;
6740 *p++ = 48;
6741 p += 48;
Gilles Peskine449bd832023-01-11 14:50:10 +01006742 } else
Jerry Yuce3dca42022-02-17 14:16:37 +08006743#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
6744#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006745 if (key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK) {
Jerry Yuce3dca42022-02-17 14:16:37 +08006746 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6747 size_t len;
6748
6749 /* Write length only when we know the actual value */
Gilles Peskine449bd832023-01-11 14:50:10 +01006750 if ((ret = mbedtls_dhm_calc_secret(&ssl->handshake->dhm_ctx,
6751 p + 2, end - (p + 2), &len,
6752 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
6753 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_calc_secret", ret);
6754 return ret;
Jerry Yuce3dca42022-02-17 14:16:37 +08006755 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006756 MBEDTLS_PUT_UINT16_BE(len, p, 0);
Jerry Yuce3dca42022-02-17 14:16:37 +08006757 p += 2 + len;
6758
Gilles Peskine449bd832023-01-11 14:50:10 +01006759 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: K ", &ssl->handshake->dhm_ctx.K);
6760 } else
Jerry Yuce3dca42022-02-17 14:16:37 +08006761#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Neil Armstrong80f6f322022-05-03 17:56:38 +02006762#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006763 if (key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK) {
Jerry Yuce3dca42022-02-17 14:16:37 +08006764 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6765 size_t zlen;
6766
Gilles Peskine449bd832023-01-11 14:50:10 +01006767 if ((ret = mbedtls_ecdh_calc_secret(&ssl->handshake->ecdh_ctx, &zlen,
6768 p + 2, end - (p + 2),
6769 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
6770 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecdh_calc_secret", ret);
6771 return ret;
Jerry Yuce3dca42022-02-17 14:16:37 +08006772 }
6773
Gilles Peskine449bd832023-01-11 14:50:10 +01006774 MBEDTLS_PUT_UINT16_BE(zlen, p, 0);
Jerry Yuce3dca42022-02-17 14:16:37 +08006775 p += 2 + zlen;
6776
Gilles Peskine449bd832023-01-11 14:50:10 +01006777 MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx,
6778 MBEDTLS_DEBUG_ECDH_Z);
6779 } else
Neil Armstrong80f6f322022-05-03 17:56:38 +02006780#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Jerry Yuce3dca42022-02-17 14:16:37 +08006781 {
Gilles Peskine449bd832023-01-11 14:50:10 +01006782 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
6783 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yuce3dca42022-02-17 14:16:37 +08006784 }
6785
6786 /* opaque psk<0..2^16-1>; */
Gilles Peskine449bd832023-01-11 14:50:10 +01006787 if (end - p < 2) {
6788 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6789 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006790
Gilles Peskine449bd832023-01-11 14:50:10 +01006791 MBEDTLS_PUT_UINT16_BE(psk_len, p, 0);
Jerry Yuce3dca42022-02-17 14:16:37 +08006792 p += 2;
6793
Gilles Peskine449bd832023-01-11 14:50:10 +01006794 if (end < p || (size_t) (end - p) < psk_len) {
6795 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6796 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006797
Gilles Peskine449bd832023-01-11 14:50:10 +01006798 memcpy(p, psk, psk_len);
Jerry Yuce3dca42022-02-17 14:16:37 +08006799 p += psk_len;
6800
6801 ssl->handshake->pmslen = p - ssl->handshake->premaster;
6802
Gilles Peskine449bd832023-01-11 14:50:10 +01006803 return 0;
Jerry Yuce3dca42022-02-17 14:16:37 +08006804}
Neil Armstrong80f6f322022-05-03 17:56:38 +02006805#endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Jerry Yuc2c673d2022-02-17 14:20:39 +08006806
6807#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006808MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006809static int ssl_write_hello_request(mbedtls_ssl_context *ssl);
Jerry Yuc2c673d2022-02-17 14:20:39 +08006810
6811#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006812int mbedtls_ssl_resend_hello_request(mbedtls_ssl_context *ssl)
Jerry Yuc2c673d2022-02-17 14:20:39 +08006813{
6814 /* If renegotiation is not enforced, retransmit until we would reach max
6815 * timeout if we were using the usual handshake doubling scheme */
Gilles Peskine449bd832023-01-11 14:50:10 +01006816 if (ssl->conf->renego_max_records < 0) {
Jerry Yuc2c673d2022-02-17 14:20:39 +08006817 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
6818 unsigned char doublings = 1;
6819
Gilles Peskine449bd832023-01-11 14:50:10 +01006820 while (ratio != 0) {
Jerry Yuc2c673d2022-02-17 14:20:39 +08006821 ++doublings;
6822 ratio >>= 1;
6823 }
6824
Gilles Peskine449bd832023-01-11 14:50:10 +01006825 if (++ssl->renego_records_seen > doublings) {
6826 MBEDTLS_SSL_DEBUG_MSG(2, ("no longer retransmitting hello request"));
6827 return 0;
Jerry Yuc2c673d2022-02-17 14:20:39 +08006828 }
6829 }
6830
Gilles Peskine449bd832023-01-11 14:50:10 +01006831 return ssl_write_hello_request(ssl);
Jerry Yuc2c673d2022-02-17 14:20:39 +08006832}
6833#endif
6834#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Jerry Yud9526692022-02-17 14:23:47 +08006835
Jerry Yud9526692022-02-17 14:23:47 +08006836/*
6837 * Handshake functions
6838 */
6839#if !defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
6840/* No certificate support -> dummy functions */
Gilles Peskine449bd832023-01-11 14:50:10 +01006841int mbedtls_ssl_write_certificate(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08006842{
6843 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6844 ssl->handshake->ciphersuite_info;
6845
Gilles Peskine449bd832023-01-11 14:50:10 +01006846 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006847
Gilles Peskine449bd832023-01-11 14:50:10 +01006848 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
6849 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006850 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01006851 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08006852 }
6853
Gilles Peskine449bd832023-01-11 14:50:10 +01006854 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
6855 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08006856}
6857
Gilles Peskine449bd832023-01-11 14:50:10 +01006858int mbedtls_ssl_parse_certificate(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08006859{
6860 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6861 ssl->handshake->ciphersuite_info;
6862
Gilles Peskine449bd832023-01-11 14:50:10 +01006863 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006864
Gilles Peskine449bd832023-01-11 14:50:10 +01006865 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
6866 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006867 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01006868 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08006869 }
6870
Gilles Peskine449bd832023-01-11 14:50:10 +01006871 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
6872 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08006873}
6874
6875#else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
6876/* Some certificate support -> implement write and parse */
6877
Gilles Peskine449bd832023-01-11 14:50:10 +01006878int mbedtls_ssl_write_certificate(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08006879{
6880 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
6881 size_t i, n;
6882 const mbedtls_x509_crt *crt;
6883 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6884 ssl->handshake->ciphersuite_info;
6885
Gilles Peskine449bd832023-01-11 14:50:10 +01006886 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006887
Gilles Peskine449bd832023-01-11 14:50:10 +01006888 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
6889 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006890 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01006891 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08006892 }
6893
6894#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01006895 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
6896 if (ssl->handshake->client_auth == 0) {
6897 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006898 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01006899 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08006900 }
6901 }
6902#endif /* MBEDTLS_SSL_CLI_C */
6903#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01006904 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
6905 if (mbedtls_ssl_own_cert(ssl) == NULL) {
Jerry Yud9526692022-02-17 14:23:47 +08006906 /* Should never happen because we shouldn't have picked the
6907 * ciphersuite if we don't have a certificate. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006908 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08006909 }
6910 }
6911#endif
6912
Gilles Peskine449bd832023-01-11 14:50:10 +01006913 MBEDTLS_SSL_DEBUG_CRT(3, "own certificate", mbedtls_ssl_own_cert(ssl));
Jerry Yud9526692022-02-17 14:23:47 +08006914
6915 /*
6916 * 0 . 0 handshake type
6917 * 1 . 3 handshake length
6918 * 4 . 6 length of all certs
6919 * 7 . 9 length of cert. 1
6920 * 10 . n-1 peer certificate
6921 * n . n+2 length of cert. 2
6922 * n+3 . ... upper level cert, etc.
6923 */
6924 i = 7;
Gilles Peskine449bd832023-01-11 14:50:10 +01006925 crt = mbedtls_ssl_own_cert(ssl);
Jerry Yud9526692022-02-17 14:23:47 +08006926
Gilles Peskine449bd832023-01-11 14:50:10 +01006927 while (crt != NULL) {
Jerry Yud9526692022-02-17 14:23:47 +08006928 n = crt->raw.len;
Gilles Peskine449bd832023-01-11 14:50:10 +01006929 if (n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i) {
6930 MBEDTLS_SSL_DEBUG_MSG(1, ("certificate too large, %" MBEDTLS_PRINTF_SIZET
6931 " > %" MBEDTLS_PRINTF_SIZET,
6932 i + 3 + n, (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN));
6933 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
Jerry Yud9526692022-02-17 14:23:47 +08006934 }
6935
Gilles Peskine449bd832023-01-11 14:50:10 +01006936 ssl->out_msg[i] = MBEDTLS_BYTE_2(n);
6937 ssl->out_msg[i + 1] = MBEDTLS_BYTE_1(n);
6938 ssl->out_msg[i + 2] = MBEDTLS_BYTE_0(n);
Jerry Yud9526692022-02-17 14:23:47 +08006939
Gilles Peskine449bd832023-01-11 14:50:10 +01006940 i += 3; memcpy(ssl->out_msg + i, crt->raw.p, n);
Jerry Yud9526692022-02-17 14:23:47 +08006941 i += n; crt = crt->next;
6942 }
6943
Gilles Peskine449bd832023-01-11 14:50:10 +01006944 ssl->out_msg[4] = MBEDTLS_BYTE_2(i - 7);
6945 ssl->out_msg[5] = MBEDTLS_BYTE_1(i - 7);
6946 ssl->out_msg[6] = MBEDTLS_BYTE_0(i - 7);
Jerry Yud9526692022-02-17 14:23:47 +08006947
6948 ssl->out_msglen = i;
6949 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6950 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
6951
6952 ssl->state++;
6953
Gilles Peskine449bd832023-01-11 14:50:10 +01006954 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
6955 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
6956 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08006957 }
6958
Gilles Peskine449bd832023-01-11 14:50:10 +01006959 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006960
Gilles Peskine449bd832023-01-11 14:50:10 +01006961 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08006962}
6963
6964#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6965
6966#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006967MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006968static int ssl_check_peer_crt_unchanged(mbedtls_ssl_context *ssl,
6969 unsigned char *crt_buf,
6970 size_t crt_buf_len)
Jerry Yud9526692022-02-17 14:23:47 +08006971{
6972 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6973
Gilles Peskine449bd832023-01-11 14:50:10 +01006974 if (peer_crt == NULL) {
6975 return -1;
6976 }
Jerry Yud9526692022-02-17 14:23:47 +08006977
Gilles Peskine449bd832023-01-11 14:50:10 +01006978 if (peer_crt->raw.len != crt_buf_len) {
6979 return -1;
6980 }
Jerry Yud9526692022-02-17 14:23:47 +08006981
Gilles Peskine449bd832023-01-11 14:50:10 +01006982 return memcmp(peer_crt->raw.p, crt_buf, peer_crt->raw.len);
Jerry Yud9526692022-02-17 14:23:47 +08006983}
6984#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006985MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006986static int ssl_check_peer_crt_unchanged(mbedtls_ssl_context *ssl,
6987 unsigned char *crt_buf,
6988 size_t crt_buf_len)
Jerry Yud9526692022-02-17 14:23:47 +08006989{
6990 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6991 unsigned char const * const peer_cert_digest =
6992 ssl->session->peer_cert_digest;
6993 mbedtls_md_type_t const peer_cert_digest_type =
6994 ssl->session->peer_cert_digest_type;
6995 mbedtls_md_info_t const * const digest_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01006996 mbedtls_md_info_from_type(peer_cert_digest_type);
Jerry Yud9526692022-02-17 14:23:47 +08006997 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6998 size_t digest_len;
6999
Gilles Peskine449bd832023-01-11 14:50:10 +01007000 if (peer_cert_digest == NULL || digest_info == NULL) {
7001 return -1;
7002 }
Jerry Yud9526692022-02-17 14:23:47 +08007003
Gilles Peskine449bd832023-01-11 14:50:10 +01007004 digest_len = mbedtls_md_get_size(digest_info);
7005 if (digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN) {
7006 return -1;
7007 }
Jerry Yud9526692022-02-17 14:23:47 +08007008
Gilles Peskine449bd832023-01-11 14:50:10 +01007009 ret = mbedtls_md(digest_info, crt_buf, crt_buf_len, tmp_digest);
7010 if (ret != 0) {
7011 return -1;
7012 }
Jerry Yud9526692022-02-17 14:23:47 +08007013
Gilles Peskine449bd832023-01-11 14:50:10 +01007014 return memcmp(tmp_digest, peer_cert_digest, digest_len);
Jerry Yud9526692022-02-17 14:23:47 +08007015}
7016#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7017#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
7018
7019/*
7020 * Once the certificate message is read, parse it into a cert chain and
7021 * perform basic checks, but leave actual verification to the caller
7022 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007023MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007024static int ssl_parse_certificate_chain(mbedtls_ssl_context *ssl,
7025 mbedtls_x509_crt *chain)
Jerry Yud9526692022-02-17 14:23:47 +08007026{
7027 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7028#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007029 int crt_cnt = 0;
Jerry Yud9526692022-02-17 14:23:47 +08007030#endif
7031 size_t i, n;
7032 uint8_t alert;
7033
Gilles Peskine449bd832023-01-11 14:50:10 +01007034 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
7035 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7036 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7037 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
7038 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Jerry Yud9526692022-02-17 14:23:47 +08007039 }
7040
Gilles Peskine449bd832023-01-11 14:50:10 +01007041 if (ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE) {
7042 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7043 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
7044 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Jerry Yud9526692022-02-17 14:23:47 +08007045 }
7046
Gilles Peskine449bd832023-01-11 14:50:10 +01007047 if (ssl->in_hslen < mbedtls_ssl_hs_hdr_len(ssl) + 3 + 3) {
7048 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7049 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7050 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
7051 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007052 }
7053
Gilles Peskine449bd832023-01-11 14:50:10 +01007054 i = mbedtls_ssl_hs_hdr_len(ssl);
Jerry Yud9526692022-02-17 14:23:47 +08007055
7056 /*
7057 * Same message structure as in mbedtls_ssl_write_certificate()
7058 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007059 n = (ssl->in_msg[i+1] << 8) | ssl->in_msg[i+2];
Jerry Yud9526692022-02-17 14:23:47 +08007060
Gilles Peskine449bd832023-01-11 14:50:10 +01007061 if (ssl->in_msg[i] != 0 ||
7062 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len(ssl)) {
7063 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7064 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7065 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
7066 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007067 }
7068
7069 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
7070 i += 3;
7071
7072 /* Iterate through and parse the CRTs in the provided chain. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007073 while (i < ssl->in_hslen) {
Jerry Yud9526692022-02-17 14:23:47 +08007074 /* Check that there's room for the next CRT's length fields. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007075 if (i + 3 > ssl->in_hslen) {
7076 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7077 mbedtls_ssl_send_alert_message(ssl,
7078 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7079 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
7080 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007081 }
7082 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
7083 * anything beyond 2**16 ~ 64K. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007084 if (ssl->in_msg[i] != 0) {
7085 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7086 mbedtls_ssl_send_alert_message(ssl,
7087 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7088 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT);
7089 return MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Jerry Yud9526692022-02-17 14:23:47 +08007090 }
7091
7092 /* Read length of the next CRT in the chain. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007093 n = ((unsigned int) ssl->in_msg[i + 1] << 8)
Jerry Yud9526692022-02-17 14:23:47 +08007094 | (unsigned int) ssl->in_msg[i + 2];
7095 i += 3;
7096
Gilles Peskine449bd832023-01-11 14:50:10 +01007097 if (n < 128 || i + n > ssl->in_hslen) {
7098 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7099 mbedtls_ssl_send_alert_message(ssl,
7100 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7101 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
7102 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007103 }
7104
7105 /* Check if we're handling the first CRT in the chain. */
7106#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007107 if (crt_cnt++ == 0 &&
Jerry Yud9526692022-02-17 14:23:47 +08007108 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Gilles Peskine449bd832023-01-11 14:50:10 +01007109 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
Jerry Yud9526692022-02-17 14:23:47 +08007110 /* During client-side renegotiation, check that the server's
7111 * end-CRTs hasn't changed compared to the initial handshake,
7112 * mitigating the triple handshake attack. On success, reuse
7113 * the original end-CRT instead of parsing it again. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007114 MBEDTLS_SSL_DEBUG_MSG(3, ("Check that peer CRT hasn't changed during renegotiation"));
7115 if (ssl_check_peer_crt_unchanged(ssl,
7116 &ssl->in_msg[i],
7117 n) != 0) {
7118 MBEDTLS_SSL_DEBUG_MSG(1, ("new server cert during renegotiation"));
7119 mbedtls_ssl_send_alert_message(ssl,
7120 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7121 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED);
7122 return MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Jerry Yud9526692022-02-17 14:23:47 +08007123 }
7124
7125 /* Now we can safely free the original chain. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007126 ssl_clear_peer_cert(ssl->session);
Jerry Yud9526692022-02-17 14:23:47 +08007127 }
7128#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
7129
7130 /* Parse the next certificate in the chain. */
7131#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007132 ret = mbedtls_x509_crt_parse_der(chain, ssl->in_msg + i, n);
Jerry Yud9526692022-02-17 14:23:47 +08007133#else
7134 /* If we don't need to store the CRT chain permanently, parse
7135 * it in-place from the input buffer instead of making a copy. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007136 ret = mbedtls_x509_crt_parse_der_nocopy(chain, ssl->in_msg + i, n);
Jerry Yud9526692022-02-17 14:23:47 +08007137#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007138 switch (ret) {
Jerry Yud9526692022-02-17 14:23:47 +08007139 case 0: /*ok*/
7140 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
7141 /* Ignore certificate with an unknown algorithm: maybe a
7142 prior certificate was already trusted. */
7143 break;
7144
7145 case MBEDTLS_ERR_X509_ALLOC_FAILED:
7146 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
7147 goto crt_parse_der_failed;
7148
7149 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
7150 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7151 goto crt_parse_der_failed;
7152
7153 default:
7154 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007155crt_parse_der_failed:
7156 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert);
7157 MBEDTLS_SSL_DEBUG_RET(1, " mbedtls_x509_crt_parse_der", ret);
7158 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007159 }
7160
7161 i += n;
7162 }
7163
Gilles Peskine449bd832023-01-11 14:50:10 +01007164 MBEDTLS_SSL_DEBUG_CRT(3, "peer certificate", chain);
7165 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08007166}
7167
7168#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007169MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007170static int ssl_srv_check_client_no_crt_notification(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08007171{
Gilles Peskine449bd832023-01-11 14:50:10 +01007172 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
7173 return -1;
7174 }
Jerry Yud9526692022-02-17 14:23:47 +08007175
Gilles Peskine449bd832023-01-11 14:50:10 +01007176 if (ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len(ssl) &&
Jerry Yud9526692022-02-17 14:23:47 +08007177 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
7178 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
Gilles Peskine449bd832023-01-11 14:50:10 +01007179 memcmp(ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl), "\0\0\0", 3) == 0) {
7180 MBEDTLS_SSL_DEBUG_MSG(1, ("peer has no certificate"));
7181 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08007182 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007183 return -1;
Jerry Yud9526692022-02-17 14:23:47 +08007184}
7185#endif /* MBEDTLS_SSL_SRV_C */
7186
7187/* Check if a certificate message is expected.
7188 * Return either
7189 * - SSL_CERTIFICATE_EXPECTED, or
7190 * - SSL_CERTIFICATE_SKIP
7191 * indicating whether a Certificate message is expected or not.
7192 */
7193#define SSL_CERTIFICATE_EXPECTED 0
7194#define SSL_CERTIFICATE_SKIP 1
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007195MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007196static int ssl_parse_certificate_coordinate(mbedtls_ssl_context *ssl,
7197 int authmode)
Jerry Yud9526692022-02-17 14:23:47 +08007198{
7199 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
7200 ssl->handshake->ciphersuite_info;
7201
Gilles Peskine449bd832023-01-11 14:50:10 +01007202 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
7203 return SSL_CERTIFICATE_SKIP;
7204 }
Jerry Yud9526692022-02-17 14:23:47 +08007205
7206#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007207 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
7208 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK) {
7209 return SSL_CERTIFICATE_SKIP;
7210 }
Jerry Yud9526692022-02-17 14:23:47 +08007211
Gilles Peskine449bd832023-01-11 14:50:10 +01007212 if (authmode == MBEDTLS_SSL_VERIFY_NONE) {
Jerry Yud9526692022-02-17 14:23:47 +08007213 ssl->session_negotiate->verify_result =
7214 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
Gilles Peskine449bd832023-01-11 14:50:10 +01007215 return SSL_CERTIFICATE_SKIP;
Jerry Yud9526692022-02-17 14:23:47 +08007216 }
7217 }
7218#else
7219 ((void) authmode);
7220#endif /* MBEDTLS_SSL_SRV_C */
7221
Gilles Peskine449bd832023-01-11 14:50:10 +01007222 return SSL_CERTIFICATE_EXPECTED;
Jerry Yud9526692022-02-17 14:23:47 +08007223}
7224
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007225MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007226static int ssl_parse_certificate_verify(mbedtls_ssl_context *ssl,
7227 int authmode,
7228 mbedtls_x509_crt *chain,
7229 void *rs_ctx)
Jerry Yud9526692022-02-17 14:23:47 +08007230{
7231 int ret = 0;
7232 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
7233 ssl->handshake->ciphersuite_info;
7234 int have_ca_chain = 0;
7235
7236 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
7237 void *p_vrfy;
7238
Gilles Peskine449bd832023-01-11 14:50:10 +01007239 if (authmode == MBEDTLS_SSL_VERIFY_NONE) {
7240 return 0;
7241 }
Jerry Yud9526692022-02-17 14:23:47 +08007242
Gilles Peskine449bd832023-01-11 14:50:10 +01007243 if (ssl->f_vrfy != NULL) {
7244 MBEDTLS_SSL_DEBUG_MSG(3, ("Use context-specific verification callback"));
Jerry Yud9526692022-02-17 14:23:47 +08007245 f_vrfy = ssl->f_vrfy;
7246 p_vrfy = ssl->p_vrfy;
Gilles Peskine449bd832023-01-11 14:50:10 +01007247 } else {
7248 MBEDTLS_SSL_DEBUG_MSG(3, ("Use configuration-specific verification callback"));
Jerry Yud9526692022-02-17 14:23:47 +08007249 f_vrfy = ssl->conf->f_vrfy;
7250 p_vrfy = ssl->conf->p_vrfy;
7251 }
7252
7253 /*
7254 * Main check: verify certificate
7255 */
7256#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Gilles Peskine449bd832023-01-11 14:50:10 +01007257 if (ssl->conf->f_ca_cb != NULL) {
Jerry Yud9526692022-02-17 14:23:47 +08007258 ((void) rs_ctx);
7259 have_ca_chain = 1;
7260
Gilles Peskine449bd832023-01-11 14:50:10 +01007261 MBEDTLS_SSL_DEBUG_MSG(3, ("use CA callback for X.509 CRT verification"));
Jerry Yud9526692022-02-17 14:23:47 +08007262 ret = mbedtls_x509_crt_verify_with_ca_cb(
7263 chain,
7264 ssl->conf->f_ca_cb,
7265 ssl->conf->p_ca_cb,
7266 ssl->conf->cert_profile,
7267 ssl->hostname,
7268 &ssl->session_negotiate->verify_result,
Gilles Peskine449bd832023-01-11 14:50:10 +01007269 f_vrfy, p_vrfy);
7270 } else
Jerry Yud9526692022-02-17 14:23:47 +08007271#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
7272 {
7273 mbedtls_x509_crt *ca_chain;
7274 mbedtls_x509_crl *ca_crl;
7275
7276#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01007277 if (ssl->handshake->sni_ca_chain != NULL) {
Jerry Yud9526692022-02-17 14:23:47 +08007278 ca_chain = ssl->handshake->sni_ca_chain;
7279 ca_crl = ssl->handshake->sni_ca_crl;
Gilles Peskine449bd832023-01-11 14:50:10 +01007280 } else
Jerry Yud9526692022-02-17 14:23:47 +08007281#endif
7282 {
7283 ca_chain = ssl->conf->ca_chain;
7284 ca_crl = ssl->conf->ca_crl;
7285 }
7286
Gilles Peskine449bd832023-01-11 14:50:10 +01007287 if (ca_chain != NULL) {
Jerry Yud9526692022-02-17 14:23:47 +08007288 have_ca_chain = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01007289 }
Jerry Yud9526692022-02-17 14:23:47 +08007290
7291 ret = mbedtls_x509_crt_verify_restartable(
7292 chain,
7293 ca_chain, ca_crl,
7294 ssl->conf->cert_profile,
7295 ssl->hostname,
7296 &ssl->session_negotiate->verify_result,
Gilles Peskine449bd832023-01-11 14:50:10 +01007297 f_vrfy, p_vrfy, rs_ctx);
Jerry Yud9526692022-02-17 14:23:47 +08007298 }
7299
Gilles Peskine449bd832023-01-11 14:50:10 +01007300 if (ret != 0) {
7301 MBEDTLS_SSL_DEBUG_RET(1, "x509_verify_cert", ret);
Jerry Yud9526692022-02-17 14:23:47 +08007302 }
7303
7304#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007305 if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
7306 return MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
7307 }
Jerry Yud9526692022-02-17 14:23:47 +08007308#endif
7309
7310 /*
7311 * Secondary checks: always done, but change 'ret' only if it was 0
7312 */
7313
7314#if defined(MBEDTLS_ECP_C)
7315 {
7316 const mbedtls_pk_context *pk = &chain->pk;
7317
Manuel Pégourié-Gonnard66b0d612022-06-17 10:49:29 +02007318 /* If certificate uses an EC key, make sure the curve is OK.
7319 * This is a public key, so it can't be opaque, so can_do() is a good
7320 * enough check to ensure pk_ec() is safe to use here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007321 if (mbedtls_pk_can_do(pk, MBEDTLS_PK_ECKEY)) {
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007322 /* and in the unlikely case the above assumption no longer holds
7323 * we are making sure that pk_ec() here does not return a NULL
7324 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007325 const mbedtls_ecp_keypair *ec = mbedtls_pk_ec(*pk);
7326 if (ec == NULL) {
7327 MBEDTLS_SSL_DEBUG_MSG(1, ("mbedtls_pk_ec() returned NULL"));
7328 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007329 }
Jerry Yud9526692022-02-17 14:23:47 +08007330
Gilles Peskine449bd832023-01-11 14:50:10 +01007331 if (mbedtls_ssl_check_curve(ssl, ec->grp.id) != 0) {
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007332 ssl->session_negotiate->verify_result |=
7333 MBEDTLS_X509_BADCERT_BAD_KEY;
7334
Gilles Peskine449bd832023-01-11 14:50:10 +01007335 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate (EC key curve)"));
7336 if (ret == 0) {
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007337 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Gilles Peskine449bd832023-01-11 14:50:10 +01007338 }
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007339 }
Jerry Yud9526692022-02-17 14:23:47 +08007340 }
7341 }
7342#endif /* MBEDTLS_ECP_C */
7343
Gilles Peskine449bd832023-01-11 14:50:10 +01007344 if (mbedtls_ssl_check_cert_usage(chain,
7345 ciphersuite_info,
7346 !ssl->conf->endpoint,
7347 &ssl->session_negotiate->verify_result) != 0) {
7348 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate (usage extensions)"));
7349 if (ret == 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007350 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Gilles Peskine449bd832023-01-11 14:50:10 +01007351 }
Jerry Yud9526692022-02-17 14:23:47 +08007352 }
7353
7354 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
7355 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
7356 * with details encoded in the verification flags. All other kinds
7357 * of error codes, including those from the user provided f_vrfy
7358 * functions, are treated as fatal and lead to a failure of
7359 * ssl_parse_certificate even if verification was optional. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007360 if (authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
7361 (ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
7362 ret == MBEDTLS_ERR_SSL_BAD_CERTIFICATE)) {
Jerry Yud9526692022-02-17 14:23:47 +08007363 ret = 0;
7364 }
7365
Gilles Peskine449bd832023-01-11 14:50:10 +01007366 if (have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED) {
7367 MBEDTLS_SSL_DEBUG_MSG(1, ("got no CA chain"));
Jerry Yud9526692022-02-17 14:23:47 +08007368 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
7369 }
7370
Gilles Peskine449bd832023-01-11 14:50:10 +01007371 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007372 uint8_t alert;
7373
7374 /* The certificate may have been rejected for several reasons.
7375 Pick one and send the corresponding alert. Which alert to send
7376 may be a subject of debate in some cases. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007377 if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER) {
Jerry Yud9526692022-02-17 14:23:47 +08007378 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
Gilles Peskine449bd832023-01-11 14:50:10 +01007379 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH) {
Jerry Yud9526692022-02-17 14:23:47 +08007380 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007381 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE) {
Jerry Yud9526692022-02-17 14:23:47 +08007382 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007383 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE) {
Jerry Yud9526692022-02-17 14:23:47 +08007384 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007385 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE) {
Jerry Yud9526692022-02-17 14:23:47 +08007386 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007387 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK) {
Jerry Yud9526692022-02-17 14:23:47 +08007388 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007389 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY) {
Jerry Yud9526692022-02-17 14:23:47 +08007390 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007391 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED) {
Jerry Yud9526692022-02-17 14:23:47 +08007392 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
Gilles Peskine449bd832023-01-11 14:50:10 +01007393 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED) {
Jerry Yud9526692022-02-17 14:23:47 +08007394 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
Gilles Peskine449bd832023-01-11 14:50:10 +01007395 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED) {
Jerry Yud9526692022-02-17 14:23:47 +08007396 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
Gilles Peskine449bd832023-01-11 14:50:10 +01007397 } else {
Jerry Yud9526692022-02-17 14:23:47 +08007398 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
Gilles Peskine449bd832023-01-11 14:50:10 +01007399 }
7400 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7401 alert);
Jerry Yud9526692022-02-17 14:23:47 +08007402 }
7403
7404#if defined(MBEDTLS_DEBUG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007405 if (ssl->session_negotiate->verify_result != 0) {
7406 MBEDTLS_SSL_DEBUG_MSG(3, ("! Certificate verification flags %08x",
7407 (unsigned int) ssl->session_negotiate->verify_result));
7408 } else {
7409 MBEDTLS_SSL_DEBUG_MSG(3, ("Certificate verification flags clear"));
Jerry Yud9526692022-02-17 14:23:47 +08007410 }
7411#endif /* MBEDTLS_DEBUG_C */
7412
Gilles Peskine449bd832023-01-11 14:50:10 +01007413 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007414}
7415
7416#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007417MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007418static int ssl_remember_peer_crt_digest(mbedtls_ssl_context *ssl,
7419 unsigned char *start, size_t len)
Jerry Yud9526692022-02-17 14:23:47 +08007420{
7421 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7422 /* Remember digest of the peer's end-CRT. */
7423 ssl->session_negotiate->peer_cert_digest =
Gilles Peskine449bd832023-01-11 14:50:10 +01007424 mbedtls_calloc(1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN);
7425 if (ssl->session_negotiate->peer_cert_digest == NULL) {
7426 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%d bytes) failed",
7427 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN));
7428 mbedtls_ssl_send_alert_message(ssl,
7429 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7430 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
Jerry Yud9526692022-02-17 14:23:47 +08007431
Gilles Peskine449bd832023-01-11 14:50:10 +01007432 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Jerry Yud9526692022-02-17 14:23:47 +08007433 }
7434
Gilles Peskine449bd832023-01-11 14:50:10 +01007435 ret = mbedtls_md(mbedtls_md_info_from_type(
7436 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE),
7437 start, len,
7438 ssl->session_negotiate->peer_cert_digest);
Jerry Yud9526692022-02-17 14:23:47 +08007439
7440 ssl->session_negotiate->peer_cert_digest_type =
7441 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
7442 ssl->session_negotiate->peer_cert_digest_len =
7443 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
7444
Gilles Peskine449bd832023-01-11 14:50:10 +01007445 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007446}
7447
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007448MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007449static int ssl_remember_peer_pubkey(mbedtls_ssl_context *ssl,
7450 unsigned char *start, size_t len)
Jerry Yud9526692022-02-17 14:23:47 +08007451{
7452 unsigned char *end = start + len;
7453 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7454
7455 /* Make a copy of the peer's raw public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007456 mbedtls_pk_init(&ssl->handshake->peer_pubkey);
7457 ret = mbedtls_pk_parse_subpubkey(&start, end,
7458 &ssl->handshake->peer_pubkey);
7459 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007460 /* We should have parsed the public key before. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007461 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007462 }
7463
Gilles Peskine449bd832023-01-11 14:50:10 +01007464 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08007465}
7466#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7467
Gilles Peskine449bd832023-01-11 14:50:10 +01007468int mbedtls_ssl_parse_certificate(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08007469{
7470 int ret = 0;
7471 int crt_expected;
7472#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7473 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
7474 ? ssl->handshake->sni_authmode
7475 : ssl->conf->authmode;
7476#else
7477 const int authmode = ssl->conf->authmode;
7478#endif
7479 void *rs_ctx = NULL;
7480 mbedtls_x509_crt *chain = NULL;
7481
Gilles Peskine449bd832023-01-11 14:50:10 +01007482 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007483
Gilles Peskine449bd832023-01-11 14:50:10 +01007484 crt_expected = ssl_parse_certificate_coordinate(ssl, authmode);
7485 if (crt_expected == SSL_CERTIFICATE_SKIP) {
7486 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007487 goto exit;
7488 }
7489
7490#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007491 if (ssl->handshake->ecrs_enabled &&
7492 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify) {
Jerry Yud9526692022-02-17 14:23:47 +08007493 chain = ssl->handshake->ecrs_peer_cert;
7494 ssl->handshake->ecrs_peer_cert = NULL;
7495 goto crt_verify;
7496 }
7497#endif
7498
Gilles Peskine449bd832023-01-11 14:50:10 +01007499 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007500 /* mbedtls_ssl_read_record may have sent an alert already. We
7501 let it decide whether to alert. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007502 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
Jerry Yud9526692022-02-17 14:23:47 +08007503 goto exit;
7504 }
7505
7506#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007507 if (ssl_srv_check_client_no_crt_notification(ssl) == 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007508 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
7509
Gilles Peskine449bd832023-01-11 14:50:10 +01007510 if (authmode != MBEDTLS_SSL_VERIFY_OPTIONAL) {
Jerry Yud9526692022-02-17 14:23:47 +08007511 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Gilles Peskine449bd832023-01-11 14:50:10 +01007512 }
Jerry Yud9526692022-02-17 14:23:47 +08007513
7514 goto exit;
7515 }
7516#endif /* MBEDTLS_SSL_SRV_C */
7517
7518 /* Clear existing peer CRT structure in case we tried to
7519 * reuse a session but it failed, and allocate a new one. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007520 ssl_clear_peer_cert(ssl->session_negotiate);
Jerry Yud9526692022-02-17 14:23:47 +08007521
Gilles Peskine449bd832023-01-11 14:50:10 +01007522 chain = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
7523 if (chain == NULL) {
7524 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed",
7525 sizeof(mbedtls_x509_crt)));
7526 mbedtls_ssl_send_alert_message(ssl,
7527 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7528 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
Jerry Yud9526692022-02-17 14:23:47 +08007529
7530 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
7531 goto exit;
7532 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007533 mbedtls_x509_crt_init(chain);
Jerry Yud9526692022-02-17 14:23:47 +08007534
Gilles Peskine449bd832023-01-11 14:50:10 +01007535 ret = ssl_parse_certificate_chain(ssl, chain);
7536 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007537 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007538 }
Jerry Yud9526692022-02-17 14:23:47 +08007539
7540#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007541 if (ssl->handshake->ecrs_enabled) {
Jerry Yud9526692022-02-17 14:23:47 +08007542 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Gilles Peskine449bd832023-01-11 14:50:10 +01007543 }
Jerry Yud9526692022-02-17 14:23:47 +08007544
7545crt_verify:
Gilles Peskine449bd832023-01-11 14:50:10 +01007546 if (ssl->handshake->ecrs_enabled) {
Jerry Yud9526692022-02-17 14:23:47 +08007547 rs_ctx = &ssl->handshake->ecrs_ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +01007548 }
Jerry Yud9526692022-02-17 14:23:47 +08007549#endif
7550
Gilles Peskine449bd832023-01-11 14:50:10 +01007551 ret = ssl_parse_certificate_verify(ssl, authmode,
7552 chain, rs_ctx);
7553 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007554 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007555 }
Jerry Yud9526692022-02-17 14:23:47 +08007556
7557#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7558 {
7559 unsigned char *crt_start, *pk_start;
7560 size_t crt_len, pk_len;
7561
7562 /* We parse the CRT chain without copying, so
7563 * these pointers point into the input buffer,
7564 * and are hence still valid after freeing the
7565 * CRT chain. */
7566
7567 crt_start = chain->raw.p;
7568 crt_len = chain->raw.len;
7569
7570 pk_start = chain->pk_raw.p;
7571 pk_len = chain->pk_raw.len;
7572
7573 /* Free the CRT structures before computing
7574 * digest and copying the peer's public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007575 mbedtls_x509_crt_free(chain);
7576 mbedtls_free(chain);
Jerry Yud9526692022-02-17 14:23:47 +08007577 chain = NULL;
7578
Gilles Peskine449bd832023-01-11 14:50:10 +01007579 ret = ssl_remember_peer_crt_digest(ssl, crt_start, crt_len);
7580 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007581 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007582 }
Jerry Yud9526692022-02-17 14:23:47 +08007583
Gilles Peskine449bd832023-01-11 14:50:10 +01007584 ret = ssl_remember_peer_pubkey(ssl, pk_start, pk_len);
7585 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007586 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007587 }
Jerry Yud9526692022-02-17 14:23:47 +08007588 }
7589#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7590 /* Pass ownership to session structure. */
7591 ssl->session_negotiate->peer_cert = chain;
7592 chain = NULL;
7593#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7594
Gilles Peskine449bd832023-01-11 14:50:10 +01007595 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007596
7597exit:
7598
Gilles Peskine449bd832023-01-11 14:50:10 +01007599 if (ret == 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007600 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01007601 }
Jerry Yud9526692022-02-17 14:23:47 +08007602
7603#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007604 if (ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS) {
Jerry Yud9526692022-02-17 14:23:47 +08007605 ssl->handshake->ecrs_peer_cert = chain;
7606 chain = NULL;
7607 }
7608#endif
7609
Gilles Peskine449bd832023-01-11 14:50:10 +01007610 if (chain != NULL) {
7611 mbedtls_x509_crt_free(chain);
7612 mbedtls_free(chain);
Jerry Yud9526692022-02-17 14:23:47 +08007613 }
7614
Gilles Peskine449bd832023-01-11 14:50:10 +01007615 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007616}
7617#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
7618
Andrzej Kurek25f27152022-08-17 16:09:31 -04007619#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01007620static int ssl_calc_finished_tls_sha256(
Gilles Peskine449bd832023-01-11 14:50:10 +01007621 mbedtls_ssl_context *ssl, unsigned char *buf, int from)
Jerry Yu615bd6f2022-02-17 14:25:15 +08007622{
7623 int len = 12;
7624 const char *sender;
7625 unsigned char padbuf[32];
7626#if defined(MBEDTLS_USE_PSA_CRYPTO)
7627 size_t hash_size;
7628 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
7629 psa_status_t status;
7630#else
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007631 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu615bd6f2022-02-17 14:25:15 +08007632 mbedtls_sha256_context sha256;
7633#endif
7634
7635 mbedtls_ssl_session *session = ssl->session_negotiate;
Gilles Peskine449bd832023-01-11 14:50:10 +01007636 if (!session) {
Jerry Yu615bd6f2022-02-17 14:25:15 +08007637 session = ssl->session;
Gilles Peskine449bd832023-01-11 14:50:10 +01007638 }
Jerry Yu615bd6f2022-02-17 14:25:15 +08007639
Gilles Peskine449bd832023-01-11 14:50:10 +01007640 sender = (from == MBEDTLS_SSL_IS_CLIENT)
Jerry Yu615bd6f2022-02-17 14:25:15 +08007641 ? "client finished"
7642 : "server finished";
7643
7644#if defined(MBEDTLS_USE_PSA_CRYPTO)
7645 sha256_psa = psa_hash_operation_init();
7646
Gilles Peskine449bd832023-01-11 14:50:10 +01007647 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc PSA finished tls sha256"));
Jerry Yu615bd6f2022-02-17 14:25:15 +08007648
Gilles Peskine449bd832023-01-11 14:50:10 +01007649 status = psa_hash_clone(&ssl->handshake->fin_sha256_psa, &sha256_psa);
7650 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007651 goto exit;
Jerry Yu615bd6f2022-02-17 14:25:15 +08007652 }
7653
Gilles Peskine449bd832023-01-11 14:50:10 +01007654 status = psa_hash_finish(&sha256_psa, padbuf, sizeof(padbuf), &hash_size);
7655 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007656 goto exit;
Jerry Yu615bd6f2022-02-17 14:25:15 +08007657 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007658 MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated padbuf", padbuf, 32);
Jerry Yu615bd6f2022-02-17 14:25:15 +08007659#else
7660
Gilles Peskine449bd832023-01-11 14:50:10 +01007661 mbedtls_sha256_init(&sha256);
Jerry Yu615bd6f2022-02-17 14:25:15 +08007662
Gilles Peskine449bd832023-01-11 14:50:10 +01007663 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc finished tls sha256"));
Jerry Yu615bd6f2022-02-17 14:25:15 +08007664
Gilles Peskine449bd832023-01-11 14:50:10 +01007665 mbedtls_sha256_clone(&sha256, &ssl->handshake->fin_sha256);
Jerry Yu615bd6f2022-02-17 14:25:15 +08007666
7667 /*
7668 * TLSv1.2:
7669 * hash = PRF( master, finished_label,
7670 * Hash( handshake ) )[0.11]
7671 */
7672
7673#if !defined(MBEDTLS_SHA256_ALT)
Gilles Peskine449bd832023-01-11 14:50:10 +01007674 MBEDTLS_SSL_DEBUG_BUF(4, "finished sha2 state", (unsigned char *)
7675 sha256.state, sizeof(sha256.state));
Jerry Yu615bd6f2022-02-17 14:25:15 +08007676#endif
7677
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007678 ret = mbedtls_sha256_finish(&sha256, padbuf);
7679 if (ret != 0) {
7680 goto exit;
7681 }
Jerry Yu615bd6f2022-02-17 14:25:15 +08007682#endif /* MBEDTLS_USE_PSA_CRYPTO */
7683
Gilles Peskine449bd832023-01-11 14:50:10 +01007684 ssl->handshake->tls_prf(session->master, 48, sender,
7685 padbuf, 32, buf, len);
Jerry Yu615bd6f2022-02-17 14:25:15 +08007686
Gilles Peskine449bd832023-01-11 14:50:10 +01007687 MBEDTLS_SSL_DEBUG_BUF(3, "calc finished result", buf, len);
Jerry Yu615bd6f2022-02-17 14:25:15 +08007688
Gilles Peskine449bd832023-01-11 14:50:10 +01007689 mbedtls_platform_zeroize(padbuf, sizeof(padbuf));
Jerry Yu615bd6f2022-02-17 14:25:15 +08007690
Gilles Peskine449bd832023-01-11 14:50:10 +01007691 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc finished"));
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007692
7693exit:
7694#if defined(MBEDTLS_USE_PSA_CRYPTO)
7695 psa_hash_abort(&sha256_psa);
7696 return mbedtls_md_error_from_psa(status);
7697#else
7698 mbedtls_sha256_free(&sha256);
7699 return ret;
7700#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu615bd6f2022-02-17 14:25:15 +08007701}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04007702#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yu615bd6f2022-02-17 14:25:15 +08007703
Jerry Yub7ba49e2022-02-17 14:25:53 +08007704
Andrzej Kurek25f27152022-08-17 16:09:31 -04007705#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01007706static int ssl_calc_finished_tls_sha384(
Gilles Peskine449bd832023-01-11 14:50:10 +01007707 mbedtls_ssl_context *ssl, unsigned char *buf, int from)
Jerry Yub7ba49e2022-02-17 14:25:53 +08007708{
7709 int len = 12;
7710 const char *sender;
7711 unsigned char padbuf[48];
7712#if defined(MBEDTLS_USE_PSA_CRYPTO)
7713 size_t hash_size;
7714 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
7715 psa_status_t status;
7716#else
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007717 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yub7ba49e2022-02-17 14:25:53 +08007718 mbedtls_sha512_context sha512;
7719#endif
7720
7721 mbedtls_ssl_session *session = ssl->session_negotiate;
Gilles Peskine449bd832023-01-11 14:50:10 +01007722 if (!session) {
Jerry Yub7ba49e2022-02-17 14:25:53 +08007723 session = ssl->session;
Gilles Peskine449bd832023-01-11 14:50:10 +01007724 }
Jerry Yub7ba49e2022-02-17 14:25:53 +08007725
Gilles Peskine449bd832023-01-11 14:50:10 +01007726 sender = (from == MBEDTLS_SSL_IS_CLIENT)
Jerry Yub7ba49e2022-02-17 14:25:53 +08007727 ? "client finished"
7728 : "server finished";
7729
7730#if defined(MBEDTLS_USE_PSA_CRYPTO)
7731 sha384_psa = psa_hash_operation_init();
7732
Gilles Peskine449bd832023-01-11 14:50:10 +01007733 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc PSA finished tls sha384"));
Jerry Yub7ba49e2022-02-17 14:25:53 +08007734
Gilles Peskine449bd832023-01-11 14:50:10 +01007735 status = psa_hash_clone(&ssl->handshake->fin_sha384_psa, &sha384_psa);
7736 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007737 goto exit;
Jerry Yub7ba49e2022-02-17 14:25:53 +08007738 }
7739
Gilles Peskine449bd832023-01-11 14:50:10 +01007740 status = psa_hash_finish(&sha384_psa, padbuf, sizeof(padbuf), &hash_size);
7741 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007742 goto exit;
Jerry Yub7ba49e2022-02-17 14:25:53 +08007743 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007744 MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated padbuf", padbuf, 48);
Jerry Yub7ba49e2022-02-17 14:25:53 +08007745#else
Gilles Peskine449bd832023-01-11 14:50:10 +01007746 mbedtls_sha512_init(&sha512);
Jerry Yub7ba49e2022-02-17 14:25:53 +08007747
Gilles Peskine449bd832023-01-11 14:50:10 +01007748 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc finished tls sha384"));
Jerry Yub7ba49e2022-02-17 14:25:53 +08007749
Gilles Peskine449bd832023-01-11 14:50:10 +01007750 mbedtls_sha512_clone(&sha512, &ssl->handshake->fin_sha384);
Jerry Yub7ba49e2022-02-17 14:25:53 +08007751
7752 /*
7753 * TLSv1.2:
7754 * hash = PRF( master, finished_label,
7755 * Hash( handshake ) )[0.11]
7756 */
7757
7758#if !defined(MBEDTLS_SHA512_ALT)
Gilles Peskine449bd832023-01-11 14:50:10 +01007759 MBEDTLS_SSL_DEBUG_BUF(4, "finished sha512 state", (unsigned char *)
7760 sha512.state, sizeof(sha512.state));
Jerry Yub7ba49e2022-02-17 14:25:53 +08007761#endif
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007762 ret = mbedtls_sha512_finish(&sha512, padbuf);
7763 if (ret != 0) {
7764 goto exit;
7765 }
Jerry Yub7ba49e2022-02-17 14:25:53 +08007766#endif
7767
Gilles Peskine449bd832023-01-11 14:50:10 +01007768 ssl->handshake->tls_prf(session->master, 48, sender,
7769 padbuf, 48, buf, len);
Jerry Yub7ba49e2022-02-17 14:25:53 +08007770
Gilles Peskine449bd832023-01-11 14:50:10 +01007771 MBEDTLS_SSL_DEBUG_BUF(3, "calc finished result", buf, len);
Jerry Yub7ba49e2022-02-17 14:25:53 +08007772
Gilles Peskine449bd832023-01-11 14:50:10 +01007773 mbedtls_platform_zeroize(padbuf, sizeof(padbuf));
Jerry Yub7ba49e2022-02-17 14:25:53 +08007774
Gilles Peskine449bd832023-01-11 14:50:10 +01007775 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc finished"));
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007776
7777exit:
7778#if defined(MBEDTLS_USE_PSA_CRYPTO)
7779 psa_hash_abort(&sha384_psa);
7780 return mbedtls_md_error_from_psa(status);
7781#else
7782 mbedtls_sha512_free(&sha512);
7783 return ret;
7784#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yub7ba49e2022-02-17 14:25:53 +08007785}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04007786#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yub7ba49e2022-02-17 14:25:53 +08007787
Gilles Peskine449bd832023-01-11 14:50:10 +01007788void mbedtls_ssl_handshake_wrapup_free_hs_transform(mbedtls_ssl_context *ssl)
Jerry Yuaef00152022-02-17 14:27:31 +08007789{
Gilles Peskine449bd832023-01-11 14:50:10 +01007790 MBEDTLS_SSL_DEBUG_MSG(3, ("=> handshake wrapup: final free"));
Jerry Yuaef00152022-02-17 14:27:31 +08007791
7792 /*
7793 * Free our handshake params
7794 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007795 mbedtls_ssl_handshake_free(ssl);
7796 mbedtls_free(ssl->handshake);
Jerry Yuaef00152022-02-17 14:27:31 +08007797 ssl->handshake = NULL;
7798
7799 /*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08007800 * Free the previous transform and switch in the current one
Jerry Yuaef00152022-02-17 14:27:31 +08007801 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007802 if (ssl->transform) {
7803 mbedtls_ssl_transform_free(ssl->transform);
7804 mbedtls_free(ssl->transform);
Jerry Yuaef00152022-02-17 14:27:31 +08007805 }
7806 ssl->transform = ssl->transform_negotiate;
7807 ssl->transform_negotiate = NULL;
7808
Gilles Peskine449bd832023-01-11 14:50:10 +01007809 MBEDTLS_SSL_DEBUG_MSG(3, ("<= handshake wrapup: final free"));
Jerry Yuaef00152022-02-17 14:27:31 +08007810}
7811
Gilles Peskine449bd832023-01-11 14:50:10 +01007812void mbedtls_ssl_handshake_wrapup(mbedtls_ssl_context *ssl)
Jerry Yu2a9fff52022-02-17 14:28:51 +08007813{
7814 int resume = ssl->handshake->resume;
7815
Gilles Peskine449bd832023-01-11 14:50:10 +01007816 MBEDTLS_SSL_DEBUG_MSG(3, ("=> handshake wrapup"));
Jerry Yu2a9fff52022-02-17 14:28:51 +08007817
7818#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01007819 if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
Jerry Yu2a9fff52022-02-17 14:28:51 +08007820 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
7821 ssl->renego_records_seen = 0;
7822 }
7823#endif
7824
7825 /*
7826 * Free the previous session and switch in the current one
7827 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007828 if (ssl->session) {
Jerry Yu2a9fff52022-02-17 14:28:51 +08007829#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
7830 /* RFC 7366 3.1: keep the EtM state */
7831 ssl->session_negotiate->encrypt_then_mac =
Gilles Peskine449bd832023-01-11 14:50:10 +01007832 ssl->session->encrypt_then_mac;
Jerry Yu2a9fff52022-02-17 14:28:51 +08007833#endif
7834
Gilles Peskine449bd832023-01-11 14:50:10 +01007835 mbedtls_ssl_session_free(ssl->session);
7836 mbedtls_free(ssl->session);
Jerry Yu2a9fff52022-02-17 14:28:51 +08007837 }
7838 ssl->session = ssl->session_negotiate;
7839 ssl->session_negotiate = NULL;
7840
7841 /*
7842 * Add cache entry
7843 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007844 if (ssl->conf->f_set_cache != NULL &&
Jerry Yu2a9fff52022-02-17 14:28:51 +08007845 ssl->session->id_len != 0 &&
Gilles Peskine449bd832023-01-11 14:50:10 +01007846 resume == 0) {
7847 if (ssl->conf->f_set_cache(ssl->conf->p_cache,
7848 ssl->session->id,
7849 ssl->session->id_len,
7850 ssl->session) != 0) {
7851 MBEDTLS_SSL_DEBUG_MSG(1, ("cache did not store session"));
7852 }
Jerry Yu2a9fff52022-02-17 14:28:51 +08007853 }
7854
7855#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007856 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7857 ssl->handshake->flight != NULL) {
Jerry Yu2a9fff52022-02-17 14:28:51 +08007858 /* Cancel handshake timer */
Gilles Peskine449bd832023-01-11 14:50:10 +01007859 mbedtls_ssl_set_timer(ssl, 0);
Jerry Yu2a9fff52022-02-17 14:28:51 +08007860
7861 /* Keep last flight around in case we need to resend it:
7862 * we need the handshake and transform structures for that */
Gilles Peskine449bd832023-01-11 14:50:10 +01007863 MBEDTLS_SSL_DEBUG_MSG(3, ("skip freeing handshake and transform"));
7864 } else
Jerry Yu2a9fff52022-02-17 14:28:51 +08007865#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01007866 mbedtls_ssl_handshake_wrapup_free_hs_transform(ssl);
Jerry Yu2a9fff52022-02-17 14:28:51 +08007867
Jerry Yu5ed73ff2022-10-27 13:08:42 +08007868 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
Jerry Yu2a9fff52022-02-17 14:28:51 +08007869
Gilles Peskine449bd832023-01-11 14:50:10 +01007870 MBEDTLS_SSL_DEBUG_MSG(3, ("<= handshake wrapup"));
Jerry Yu2a9fff52022-02-17 14:28:51 +08007871}
7872
Gilles Peskine449bd832023-01-11 14:50:10 +01007873int mbedtls_ssl_write_finished(mbedtls_ssl_context *ssl)
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007874{
7875 int ret, hash_len;
7876
Gilles Peskine449bd832023-01-11 14:50:10 +01007877 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write finished"));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007878
Gilles Peskine449bd832023-01-11 14:50:10 +01007879 mbedtls_ssl_update_out_pointers(ssl, ssl->transform_negotiate);
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007880
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01007881 ret = ssl->handshake->calc_finished(ssl, ssl->out_msg + 4, ssl->conf->endpoint);
7882 if (ret != 0) {
7883 MBEDTLS_SSL_DEBUG_RET(1, "calc_finished", ret);
7884 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007885
7886 /*
7887 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7888 * may define some other value. Currently (early 2016), no defined
7889 * ciphersuite does this (and this is unlikely to change as activity has
7890 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7891 */
7892 hash_len = 12;
7893
7894#if defined(MBEDTLS_SSL_RENEGOTIATION)
7895 ssl->verify_data_len = hash_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01007896 memcpy(ssl->own_verify_data, ssl->out_msg + 4, hash_len);
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007897#endif
7898
7899 ssl->out_msglen = 4 + hash_len;
7900 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7901 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
7902
7903 /*
7904 * In case of session resuming, invert the client and server
7905 * ChangeCipherSpec messages order.
7906 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007907 if (ssl->handshake->resume != 0) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007908#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007909 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007910 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Gilles Peskine449bd832023-01-11 14:50:10 +01007911 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007912#endif
7913#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007914 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007915 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Gilles Peskine449bd832023-01-11 14:50:10 +01007916 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007917#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01007918 } else {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007919 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01007920 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007921
7922 /*
7923 * Switch to our negotiated transform and session parameters for outbound
7924 * data.
7925 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007926 MBEDTLS_SSL_DEBUG_MSG(3, ("switching to new transform spec for outbound data"));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007927
7928#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007929 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007930 unsigned char i;
7931
7932 /* Remember current epoch settings for resending */
7933 ssl->handshake->alt_transform_out = ssl->transform_out;
Gilles Peskine449bd832023-01-11 14:50:10 +01007934 memcpy(ssl->handshake->alt_out_ctr, ssl->cur_out_ctr,
7935 sizeof(ssl->handshake->alt_out_ctr));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007936
7937 /* Set sequence_number to zero */
Gilles Peskine449bd832023-01-11 14:50:10 +01007938 memset(&ssl->cur_out_ctr[2], 0, sizeof(ssl->cur_out_ctr) - 2);
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007939
7940
7941 /* Increment epoch */
Gilles Peskine449bd832023-01-11 14:50:10 +01007942 for (i = 2; i > 0; i--) {
7943 if (++ssl->cur_out_ctr[i - 1] != 0) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007944 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01007945 }
7946 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007947
7948 /* The loop goes to its end iff the counter is wrapping */
Gilles Peskine449bd832023-01-11 14:50:10 +01007949 if (i == 0) {
7950 MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS epoch would wrap"));
7951 return MBEDTLS_ERR_SSL_COUNTER_WRAPPING;
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007952 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007953 } else
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007954#endif /* MBEDTLS_SSL_PROTO_DTLS */
Gilles Peskine449bd832023-01-11 14:50:10 +01007955 memset(ssl->cur_out_ctr, 0, sizeof(ssl->cur_out_ctr));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007956
7957 ssl->transform_out = ssl->transform_negotiate;
7958 ssl->session_out = ssl->session_negotiate;
7959
7960#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007961 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
7962 mbedtls_ssl_send_flight_completed(ssl);
7963 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007964#endif
7965
Gilles Peskine449bd832023-01-11 14:50:10 +01007966 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
7967 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
7968 return ret;
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007969 }
7970
7971#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007972 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7973 (ret = mbedtls_ssl_flight_transmit(ssl)) != 0) {
7974 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_flight_transmit", ret);
7975 return ret;
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007976 }
7977#endif
7978
Gilles Peskine449bd832023-01-11 14:50:10 +01007979 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write finished"));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007980
Gilles Peskine449bd832023-01-11 14:50:10 +01007981 return 0;
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007982}
7983
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007984#define SSL_MAX_HASH_LEN 12
7985
Gilles Peskine449bd832023-01-11 14:50:10 +01007986int mbedtls_ssl_parse_finished(mbedtls_ssl_context *ssl)
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007987{
7988 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7989 unsigned int hash_len = 12;
7990 unsigned char buf[SSL_MAX_HASH_LEN];
7991
Gilles Peskine449bd832023-01-11 14:50:10 +01007992 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse finished"));
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007993
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01007994 ret = ssl->handshake->calc_finished(ssl, buf, ssl->conf->endpoint ^ 1);
7995 if (ret != 0) {
7996 MBEDTLS_SSL_DEBUG_RET(1, "calc_finished", ret);
7997 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007998
Gilles Peskine449bd832023-01-11 14:50:10 +01007999 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
8000 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008001 goto exit;
8002 }
8003
Gilles Peskine449bd832023-01-11 14:50:10 +01008004 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
8005 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
8006 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8007 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008008 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
8009 goto exit;
8010 }
8011
Gilles Peskine449bd832023-01-11 14:50:10 +01008012 if (ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED) {
8013 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8014 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008015 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
8016 goto exit;
8017 }
8018
Gilles Peskine449bd832023-01-11 14:50:10 +01008019 if (ssl->in_hslen != mbedtls_ssl_hs_hdr_len(ssl) + hash_len) {
8020 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
8021 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8022 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008023 ret = MBEDTLS_ERR_SSL_DECODE_ERROR;
8024 goto exit;
8025 }
8026
Gilles Peskine449bd832023-01-11 14:50:10 +01008027 if (mbedtls_ct_memcmp(ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl),
8028 buf, hash_len) != 0) {
8029 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
8030 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8031 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008032 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
8033 goto exit;
8034 }
8035
8036#if defined(MBEDTLS_SSL_RENEGOTIATION)
8037 ssl->verify_data_len = hash_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008038 memcpy(ssl->peer_verify_data, buf, hash_len);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008039#endif
8040
Gilles Peskine449bd832023-01-11 14:50:10 +01008041 if (ssl->handshake->resume != 0) {
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008042#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008043 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008044 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Gilles Peskine449bd832023-01-11 14:50:10 +01008045 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008046#endif
8047#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008048 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008049 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Gilles Peskine449bd832023-01-11 14:50:10 +01008050 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008051#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01008052 } else {
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008053 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01008054 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008055
8056#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01008057 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
8058 mbedtls_ssl_recv_flight_completed(ssl);
8059 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008060#endif
8061
Gilles Peskine449bd832023-01-11 14:50:10 +01008062 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse finished"));
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008063
8064exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008065 mbedtls_platform_zeroize(buf, hash_len);
8066 return ret;
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008067}
8068
Jerry Yu392112c2022-02-17 14:34:10 +08008069#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
8070/*
8071 * Helper to get TLS 1.2 PRF from ciphersuite
8072 * (Duplicates bits of logic from ssl_set_handshake_prfs().)
8073 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008074static tls_prf_fn ssl_tls12prf_from_cs(int ciphersuite_id)
Jerry Yu392112c2022-02-17 14:34:10 +08008075{
Jerry Yu392112c2022-02-17 14:34:10 +08008076 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01008077 mbedtls_ssl_ciphersuite_from_id(ciphersuite_id);
Andrzej Kurek68327742022-10-03 06:18:18 -04008078#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01008079 if (ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA384) {
8080 return tls_prf_sha384;
8081 } else
Jerry Yu392112c2022-02-17 14:34:10 +08008082#endif
Andrzej Kurek894edde2022-09-29 06:31:14 -04008083#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
8084 {
Gilles Peskine449bd832023-01-11 14:50:10 +01008085 if (ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA256) {
8086 return tls_prf_sha256;
8087 }
Andrzej Kurek894edde2022-09-29 06:31:14 -04008088 }
8089#endif
8090#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
8091 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
8092 (void) ciphersuite_info;
8093#endif
Andrzej Kurekeabeb302022-10-17 07:52:51 -04008094
Gilles Peskine449bd832023-01-11 14:50:10 +01008095 return NULL;
Jerry Yu392112c2022-02-17 14:34:10 +08008096}
8097#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Jerry Yue93ffcd2022-02-17 14:37:06 +08008098
Gilles Peskine449bd832023-01-11 14:50:10 +01008099static mbedtls_tls_prf_types tls_prf_get_type(mbedtls_ssl_tls_prf_cb *tls_prf)
Jerry Yue93ffcd2022-02-17 14:37:06 +08008100{
8101 ((void) tls_prf);
Andrzej Kurek25f27152022-08-17 16:09:31 -04008102#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01008103 if (tls_prf == tls_prf_sha384) {
8104 return MBEDTLS_SSL_TLS_PRF_SHA384;
8105 } else
Jerry Yue93ffcd2022-02-17 14:37:06 +08008106#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04008107#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01008108 if (tls_prf == tls_prf_sha256) {
8109 return MBEDTLS_SSL_TLS_PRF_SHA256;
8110 } else
Jerry Yue93ffcd2022-02-17 14:37:06 +08008111#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01008112 return MBEDTLS_SSL_TLS_PRF_NONE;
Jerry Yue93ffcd2022-02-17 14:37:06 +08008113}
8114
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008115/*
8116 * Populate a transform structure with session keys and all the other
8117 * necessary information.
8118 *
8119 * Parameters:
8120 * - [in/out]: transform: structure to populate
8121 * [in] must be just initialised with mbedtls_ssl_transform_init()
8122 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
8123 * - [in] ciphersuite
8124 * - [in] master
8125 * - [in] encrypt_then_mac
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008126 * - [in] tls_prf: pointer to PRF to use for key derivation
8127 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
Glenn Strauss07c64162022-03-14 12:34:51 -04008128 * - [in] tls_version: TLS version
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008129 * - [in] endpoint: client or server
8130 * - [in] ssl: used for:
8131 * - ssl->conf->{f,p}_export_keys
8132 * [in] optionally used for:
8133 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
8134 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02008135MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01008136static int ssl_tls12_populate_transform(mbedtls_ssl_transform *transform,
8137 int ciphersuite,
8138 const unsigned char master[48],
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008139#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01008140 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008141#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01008142 ssl_tls_prf_t tls_prf,
8143 const unsigned char randbytes[64],
8144 mbedtls_ssl_protocol_version tls_version,
8145 unsigned endpoint,
8146 const mbedtls_ssl_context *ssl)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008147{
8148 int ret = 0;
8149 unsigned char keyblk[256];
8150 unsigned char *key1;
8151 unsigned char *key2;
8152 unsigned char *mac_enc;
8153 unsigned char *mac_dec;
8154 size_t mac_key_len = 0;
8155 size_t iv_copy_len;
8156 size_t keylen;
8157 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008158 mbedtls_ssl_mode_t ssl_mode;
Neil Armstronge4512952022-03-08 09:08:22 +01008159#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008160 const mbedtls_cipher_info_t *cipher_info;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008161 const mbedtls_md_info_t *md_info;
Neil Armstronge4512952022-03-08 09:08:22 +01008162#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008163
8164#if defined(MBEDTLS_USE_PSA_CRYPTO)
8165 psa_key_type_t key_type;
8166 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8167 psa_algorithm_t alg;
Neil Armstronge4512952022-03-08 09:08:22 +01008168 psa_algorithm_t mac_alg = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008169 size_t key_bits;
8170 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
8171#endif
8172
8173#if !defined(MBEDTLS_DEBUG_C) && \
8174 !defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Gilles Peskine449bd832023-01-11 14:50:10 +01008175 if (ssl->f_export_keys == NULL) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008176 ssl = NULL; /* make sure we don't use it except for these cases */
8177 (void) ssl;
8178 }
8179#endif
8180
8181 /*
8182 * Some data just needs copying into the structure
8183 */
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008184#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008185 transform->encrypt_then_mac = encrypt_then_mac;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008186#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Glenn Strauss07c64162022-03-14 12:34:51 -04008187 transform->tls_version = tls_version;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008188
8189#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01008190 memcpy(transform->randbytes, randbytes, sizeof(transform->randbytes));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008191#endif
8192
8193#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01008194 if (tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008195 /* At the moment, we keep TLS <= 1.2 and TLS 1.3 transform
8196 * generation separate. This should never happen. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008197 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008198 }
8199#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
8200
8201 /*
8202 * Get various info structures
8203 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008204 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(ciphersuite);
8205 if (ciphersuite_info == NULL) {
8206 MBEDTLS_SSL_DEBUG_MSG(1, ("ciphersuite info for %d not found",
8207 ciphersuite));
8208 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008209 }
8210
Neil Armstrongab555e02022-04-04 11:07:59 +02008211 ssl_mode = mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008212#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01008213 encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008214#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01008215 ciphersuite_info);
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008216
Gilles Peskine449bd832023-01-11 14:50:10 +01008217 if (ssl_mode == MBEDTLS_SSL_MODE_AEAD) {
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008218 transform->taglen =
8219 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Gilles Peskine449bd832023-01-11 14:50:10 +01008220 }
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008221
8222#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008223 if ((status = mbedtls_ssl_cipher_to_psa(ciphersuite_info->cipher,
8224 transform->taglen,
8225 &alg,
8226 &key_type,
8227 &key_bits)) != PSA_SUCCESS) {
8228 ret = psa_ssl_status_to_mbedtls(status);
8229 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_cipher_to_psa", ret);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008230 goto end;
8231 }
8232#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008233 cipher_info = mbedtls_cipher_info_from_type(ciphersuite_info->cipher);
8234 if (cipher_info == NULL) {
8235 MBEDTLS_SSL_DEBUG_MSG(1, ("cipher info for %u not found",
8236 ciphersuite_info->cipher));
8237 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008238 }
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008239#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008240
Neil Armstronge4512952022-03-08 09:08:22 +01008241#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008242 mac_alg = mbedtls_hash_info_psa_from_md(ciphersuite_info->mac);
8243 if (mac_alg == 0) {
8244 MBEDTLS_SSL_DEBUG_MSG(1, ("mbedtls_hash_info_psa_from_md for %u not found",
8245 (unsigned) ciphersuite_info->mac));
8246 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Neil Armstronge4512952022-03-08 09:08:22 +01008247 }
8248#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008249 md_info = mbedtls_md_info_from_type(ciphersuite_info->mac);
8250 if (md_info == NULL) {
8251 MBEDTLS_SSL_DEBUG_MSG(1, ("mbedtls_md info for %u not found",
8252 (unsigned) ciphersuite_info->mac));
8253 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008254 }
Neil Armstronge4512952022-03-08 09:08:22 +01008255#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008256
8257#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
8258 /* Copy own and peer's CID if the use of the CID
8259 * extension has been negotiated. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008260 if (ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED) {
8261 MBEDTLS_SSL_DEBUG_MSG(3, ("Copy CIDs into SSL transform"));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008262
8263 transform->in_cid_len = ssl->own_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008264 memcpy(transform->in_cid, ssl->own_cid, ssl->own_cid_len);
8265 MBEDTLS_SSL_DEBUG_BUF(3, "Incoming CID", transform->in_cid,
8266 transform->in_cid_len);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008267
8268 transform->out_cid_len = ssl->handshake->peer_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008269 memcpy(transform->out_cid, ssl->handshake->peer_cid,
8270 ssl->handshake->peer_cid_len);
8271 MBEDTLS_SSL_DEBUG_BUF(3, "Outgoing CID", transform->out_cid,
8272 transform->out_cid_len);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008273 }
8274#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
8275
8276 /*
8277 * Compute key block using the PRF
8278 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008279 ret = tls_prf(master, 48, "key expansion", randbytes, 64, keyblk, 256);
8280 if (ret != 0) {
8281 MBEDTLS_SSL_DEBUG_RET(1, "prf", ret);
8282 return ret;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008283 }
8284
Gilles Peskine449bd832023-01-11 14:50:10 +01008285 MBEDTLS_SSL_DEBUG_MSG(3, ("ciphersuite = %s",
8286 mbedtls_ssl_get_ciphersuite_name(ciphersuite)));
8287 MBEDTLS_SSL_DEBUG_BUF(3, "master secret", master, 48);
8288 MBEDTLS_SSL_DEBUG_BUF(4, "random bytes", randbytes, 64);
8289 MBEDTLS_SSL_DEBUG_BUF(4, "key block", keyblk, 256);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008290
8291 /*
8292 * Determine the appropriate key, IV and MAC length.
8293 */
8294
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008295#if defined(MBEDTLS_USE_PSA_CRYPTO)
8296 keylen = PSA_BITS_TO_BYTES(key_bits);
8297#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008298 keylen = mbedtls_cipher_info_get_key_bitlen(cipher_info) / 8;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008299#endif
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008300
8301#if defined(MBEDTLS_GCM_C) || \
8302 defined(MBEDTLS_CCM_C) || \
8303 defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008304 if (ssl_mode == MBEDTLS_SSL_MODE_AEAD) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008305 size_t explicit_ivlen;
8306
8307 transform->maclen = 0;
8308 mac_key_len = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008309
8310 /* All modes haves 96-bit IVs, but the length of the static parts vary
8311 * with mode and version:
8312 * - For GCM and CCM in TLS 1.2, there's a static IV of 4 Bytes
8313 * (to be concatenated with a dynamically chosen IV of 8 Bytes)
8314 * - For ChaChaPoly in TLS 1.2, and all modes in TLS 1.3, there's
8315 * a static IV of 12 Bytes (to be XOR'ed with the 8 Byte record
8316 * sequence number).
8317 */
8318 transform->ivlen = 12;
David Horstmann3b2276a2022-10-06 14:49:08 +01008319
8320 int is_chachapoly = 0;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008321#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008322 is_chachapoly = (key_type == PSA_KEY_TYPE_CHACHA20);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008323#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008324 is_chachapoly = (mbedtls_cipher_info_get_mode(cipher_info)
8325 == MBEDTLS_MODE_CHACHAPOLY);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008326#endif /* MBEDTLS_USE_PSA_CRYPTO */
David Horstmann3b2276a2022-10-06 14:49:08 +01008327
Gilles Peskine449bd832023-01-11 14:50:10 +01008328 if (is_chachapoly) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008329 transform->fixed_ivlen = 12;
Gilles Peskine449bd832023-01-11 14:50:10 +01008330 } else {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008331 transform->fixed_ivlen = 4;
Gilles Peskine449bd832023-01-11 14:50:10 +01008332 }
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008333
8334 /* Minimum length of encrypted record */
8335 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
8336 transform->minlen = explicit_ivlen + transform->taglen;
Gilles Peskine449bd832023-01-11 14:50:10 +01008337 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008338#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
8339#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01008340 if (ssl_mode == MBEDTLS_SSL_MODE_STREAM ||
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008341 ssl_mode == MBEDTLS_SSL_MODE_CBC ||
Gilles Peskine449bd832023-01-11 14:50:10 +01008342 ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM) {
Neil Armstronge4512952022-03-08 09:08:22 +01008343#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008344 size_t block_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008345#else
8346 size_t block_size = cipher_info->block_size;
8347#endif /* MBEDTLS_USE_PSA_CRYPTO */
8348
8349#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronge4512952022-03-08 09:08:22 +01008350 /* Get MAC length */
8351 mac_key_len = PSA_HASH_LENGTH(mac_alg);
8352#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008353 /* Initialize HMAC contexts */
Gilles Peskine449bd832023-01-11 14:50:10 +01008354 if ((ret = mbedtls_md_setup(&transform->md_ctx_enc, md_info, 1)) != 0 ||
8355 (ret = mbedtls_md_setup(&transform->md_ctx_dec, md_info, 1)) != 0) {
8356 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_setup", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008357 goto end;
8358 }
8359
8360 /* Get MAC length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008361 mac_key_len = mbedtls_md_get_size(md_info);
Neil Armstronge4512952022-03-08 09:08:22 +01008362#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008363 transform->maclen = mac_key_len;
8364
8365 /* IV length */
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008366#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008367 transform->ivlen = PSA_CIPHER_IV_LENGTH(key_type, alg);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008368#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008369 transform->ivlen = cipher_info->iv_size;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008370#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008371
8372 /* Minimum length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008373 if (ssl_mode == MBEDTLS_SSL_MODE_STREAM) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008374 transform->minlen = transform->maclen;
Gilles Peskine449bd832023-01-11 14:50:10 +01008375 } else {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008376 /*
8377 * GenericBlockCipher:
8378 * 1. if EtM is in use: one block plus MAC
8379 * otherwise: * first multiple of blocklen greater than maclen
8380 * 2. IV
8381 */
8382#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01008383 if (ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008384 transform->minlen = transform->maclen
Gilles Peskine449bd832023-01-11 14:50:10 +01008385 + block_size;
8386 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008387#endif
8388 {
8389 transform->minlen = transform->maclen
Gilles Peskine449bd832023-01-11 14:50:10 +01008390 + block_size
8391 - transform->maclen % block_size;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008392 }
8393
Gilles Peskine449bd832023-01-11 14:50:10 +01008394 if (tls_version == MBEDTLS_SSL_VERSION_TLS1_2) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008395 transform->minlen += transform->ivlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01008396 } else {
8397 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008398 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
8399 goto end;
8400 }
8401 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008402 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008403#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
8404 {
Gilles Peskine449bd832023-01-11 14:50:10 +01008405 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
8406 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008407 }
8408
Gilles Peskine449bd832023-01-11 14:50:10 +01008409 MBEDTLS_SSL_DEBUG_MSG(3, ("keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
8410 (unsigned) keylen,
8411 (unsigned) transform->minlen,
8412 (unsigned) transform->ivlen,
8413 (unsigned) transform->maclen));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008414
8415 /*
8416 * Finally setup the cipher contexts, IVs and MAC secrets.
8417 */
8418#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008419 if (endpoint == MBEDTLS_SSL_IS_CLIENT) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008420 key1 = keyblk + mac_key_len * 2;
8421 key2 = keyblk + mac_key_len * 2 + keylen;
8422
8423 mac_enc = keyblk;
8424 mac_dec = keyblk + mac_key_len;
8425
Gilles Peskine449bd832023-01-11 14:50:10 +01008426 iv_copy_len = (transform->fixed_ivlen) ?
8427 transform->fixed_ivlen : transform->ivlen;
8428 memcpy(transform->iv_enc, key2 + keylen, iv_copy_len);
8429 memcpy(transform->iv_dec, key2 + keylen + iv_copy_len,
8430 iv_copy_len);
8431 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008432#endif /* MBEDTLS_SSL_CLI_C */
8433#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008434 if (endpoint == MBEDTLS_SSL_IS_SERVER) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008435 key1 = keyblk + mac_key_len * 2 + keylen;
8436 key2 = keyblk + mac_key_len * 2;
8437
8438 mac_enc = keyblk + mac_key_len;
8439 mac_dec = keyblk;
8440
Gilles Peskine449bd832023-01-11 14:50:10 +01008441 iv_copy_len = (transform->fixed_ivlen) ?
8442 transform->fixed_ivlen : transform->ivlen;
8443 memcpy(transform->iv_dec, key1 + keylen, iv_copy_len);
8444 memcpy(transform->iv_enc, key1 + keylen + iv_copy_len,
8445 iv_copy_len);
8446 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008447#endif /* MBEDTLS_SSL_SRV_C */
8448 {
Gilles Peskine449bd832023-01-11 14:50:10 +01008449 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008450 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
8451 goto end;
8452 }
8453
Gilles Peskine449bd832023-01-11 14:50:10 +01008454 if (ssl != NULL && ssl->f_export_keys != NULL) {
8455 ssl->f_export_keys(ssl->p_export_keys,
8456 MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET,
8457 master, 48,
8458 randbytes + 32,
8459 randbytes,
8460 tls_prf_get_type(tls_prf));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008461 }
8462
8463#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008464 transform->psa_alg = alg;
8465
Gilles Peskine449bd832023-01-11 14:50:10 +01008466 if (alg != MBEDTLS_SSL_NULL_CIPHER) {
8467 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
8468 psa_set_key_algorithm(&attributes, alg);
8469 psa_set_key_type(&attributes, key_type);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008470
Gilles Peskine449bd832023-01-11 14:50:10 +01008471 if ((status = psa_import_key(&attributes,
8472 key1,
8473 PSA_BITS_TO_BYTES(key_bits),
8474 &transform->psa_key_enc)) != PSA_SUCCESS) {
8475 MBEDTLS_SSL_DEBUG_RET(3, "psa_import_key", (int) status);
8476 ret = psa_ssl_status_to_mbedtls(status);
8477 MBEDTLS_SSL_DEBUG_RET(1, "psa_import_key", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008478 goto end;
8479 }
8480
Gilles Peskine449bd832023-01-11 14:50:10 +01008481 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008482
Gilles Peskine449bd832023-01-11 14:50:10 +01008483 if ((status = psa_import_key(&attributes,
8484 key2,
8485 PSA_BITS_TO_BYTES(key_bits),
8486 &transform->psa_key_dec)) != PSA_SUCCESS) {
8487 ret = psa_ssl_status_to_mbedtls(status);
8488 MBEDTLS_SSL_DEBUG_RET(1, "psa_import_key", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008489 goto end;
8490 }
8491 }
8492#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008493 if ((ret = mbedtls_cipher_setup(&transform->cipher_ctx_enc,
8494 cipher_info)) != 0) {
8495 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008496 goto end;
8497 }
8498
Gilles Peskine449bd832023-01-11 14:50:10 +01008499 if ((ret = mbedtls_cipher_setup(&transform->cipher_ctx_dec,
8500 cipher_info)) != 0) {
8501 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008502 goto end;
8503 }
8504
Gilles Peskine449bd832023-01-11 14:50:10 +01008505 if ((ret = mbedtls_cipher_setkey(&transform->cipher_ctx_enc, key1,
8506 (int) mbedtls_cipher_info_get_key_bitlen(cipher_info),
8507 MBEDTLS_ENCRYPT)) != 0) {
8508 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setkey", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008509 goto end;
8510 }
8511
Gilles Peskine449bd832023-01-11 14:50:10 +01008512 if ((ret = mbedtls_cipher_setkey(&transform->cipher_ctx_dec, key2,
8513 (int) mbedtls_cipher_info_get_key_bitlen(cipher_info),
8514 MBEDTLS_DECRYPT)) != 0) {
8515 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setkey", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008516 goto end;
8517 }
8518
8519#if defined(MBEDTLS_CIPHER_MODE_CBC)
Gilles Peskine449bd832023-01-11 14:50:10 +01008520 if (mbedtls_cipher_info_get_mode(cipher_info) == MBEDTLS_MODE_CBC) {
8521 if ((ret = mbedtls_cipher_set_padding_mode(&transform->cipher_ctx_enc,
8522 MBEDTLS_PADDING_NONE)) != 0) {
8523 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_set_padding_mode", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008524 goto end;
8525 }
8526
Gilles Peskine449bd832023-01-11 14:50:10 +01008527 if ((ret = mbedtls_cipher_set_padding_mode(&transform->cipher_ctx_dec,
8528 MBEDTLS_PADDING_NONE)) != 0) {
8529 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_set_padding_mode", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008530 goto end;
8531 }
8532 }
8533#endif /* MBEDTLS_CIPHER_MODE_CBC */
8534#endif /* MBEDTLS_USE_PSA_CRYPTO */
8535
Neil Armstrong29c0c042022-03-17 17:47:28 +01008536#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
8537 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
8538 For AEAD-based ciphersuites, there is nothing to do here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008539 if (mac_key_len != 0) {
Neil Armstrong29c0c042022-03-17 17:47:28 +01008540#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008541 transform->psa_mac_alg = PSA_ALG_HMAC(mac_alg);
Neil Armstrong29c0c042022-03-17 17:47:28 +01008542
Gilles Peskine449bd832023-01-11 14:50:10 +01008543 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
8544 psa_set_key_algorithm(&attributes, PSA_ALG_HMAC(mac_alg));
8545 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
Neil Armstrong29c0c042022-03-17 17:47:28 +01008546
Gilles Peskine449bd832023-01-11 14:50:10 +01008547 if ((status = psa_import_key(&attributes,
8548 mac_enc, mac_key_len,
8549 &transform->psa_mac_enc)) != PSA_SUCCESS) {
8550 ret = psa_ssl_status_to_mbedtls(status);
8551 MBEDTLS_SSL_DEBUG_RET(1, "psa_import_mac_key", ret);
Neil Armstrong29c0c042022-03-17 17:47:28 +01008552 goto end;
8553 }
8554
Gilles Peskine449bd832023-01-11 14:50:10 +01008555 if ((transform->psa_alg == MBEDTLS_SSL_NULL_CIPHER) ||
8556 ((transform->psa_alg == PSA_ALG_CBC_NO_PADDING)
Andrzej Kurek8c95ac42022-08-17 16:17:00 -04008557#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01008558 && (transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED)
Andrzej Kurek8c95ac42022-08-17 16:17:00 -04008559#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01008560 )) {
Neil Armstrong29c0c042022-03-17 17:47:28 +01008561 /* mbedtls_ct_hmac() requires the key to be exportable */
Gilles Peskine449bd832023-01-11 14:50:10 +01008562 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT |
8563 PSA_KEY_USAGE_VERIFY_HASH);
8564 } else {
8565 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
8566 }
Neil Armstrong29c0c042022-03-17 17:47:28 +01008567
Gilles Peskine449bd832023-01-11 14:50:10 +01008568 if ((status = psa_import_key(&attributes,
8569 mac_dec, mac_key_len,
8570 &transform->psa_mac_dec)) != PSA_SUCCESS) {
8571 ret = psa_ssl_status_to_mbedtls(status);
8572 MBEDTLS_SSL_DEBUG_RET(1, "psa_import_mac_key", ret);
Neil Armstrong29c0c042022-03-17 17:47:28 +01008573 goto end;
8574 }
8575#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008576 ret = mbedtls_md_hmac_starts(&transform->md_ctx_enc, mac_enc, mac_key_len);
8577 if (ret != 0) {
Neil Armstrong29c0c042022-03-17 17:47:28 +01008578 goto end;
Gilles Peskine449bd832023-01-11 14:50:10 +01008579 }
8580 ret = mbedtls_md_hmac_starts(&transform->md_ctx_dec, mac_dec, mac_key_len);
8581 if (ret != 0) {
Neil Armstrong29c0c042022-03-17 17:47:28 +01008582 goto end;
Gilles Peskine449bd832023-01-11 14:50:10 +01008583 }
Neil Armstrong29c0c042022-03-17 17:47:28 +01008584#endif /* MBEDTLS_USE_PSA_CRYPTO */
8585 }
8586#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
8587
8588 ((void) mac_dec);
8589 ((void) mac_enc);
8590
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008591end:
Gilles Peskine449bd832023-01-11 14:50:10 +01008592 mbedtls_platform_zeroize(keyblk, sizeof(keyblk));
8593 return ret;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008594}
8595
Valerio Settia08b1a42022-11-17 15:10:02 +01008596#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) && \
8597 defined(MBEDTLS_USE_PSA_CRYPTO)
Valerio Setti6b3dab02022-11-17 17:14:54 +01008598int mbedtls_psa_ecjpake_read_round(
Gilles Peskine449bd832023-01-11 14:50:10 +01008599 psa_pake_operation_t *pake_ctx,
8600 const unsigned char *buf,
8601 size_t len, mbedtls_ecjpake_rounds_t round)
Valerio Settia08b1a42022-11-17 15:10:02 +01008602{
8603 psa_status_t status;
8604 size_t input_offset = 0;
Valerio Setti819de862022-11-17 18:05:19 +01008605 /*
Valerio Setti6b3dab02022-11-17 17:14:54 +01008606 * At round one repeat the KEY_SHARE, ZK_PUBLIC & ZF_PROOF twice
8607 * At round two perform a single cycle
8608 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008609 unsigned int remaining_steps = (round == MBEDTLS_ECJPAKE_ROUND_ONE) ? 2 : 1;
Valerio Settia08b1a42022-11-17 15:10:02 +01008610
Gilles Peskine449bd832023-01-11 14:50:10 +01008611 for (; remaining_steps > 0; remaining_steps--) {
8612 for (psa_pake_step_t step = PSA_PAKE_STEP_KEY_SHARE;
Valerio Settia08b1a42022-11-17 15:10:02 +01008613 step <= PSA_PAKE_STEP_ZK_PROOF;
Gilles Peskine449bd832023-01-11 14:50:10 +01008614 ++step) {
Valerio Settia08b1a42022-11-17 15:10:02 +01008615 /* Length is stored at the first byte */
8616 size_t length = buf[input_offset];
8617 input_offset += 1;
8618
Gilles Peskine449bd832023-01-11 14:50:10 +01008619 if (input_offset + length > len) {
Valerio Settia08b1a42022-11-17 15:10:02 +01008620 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
8621 }
8622
Gilles Peskine449bd832023-01-11 14:50:10 +01008623 status = psa_pake_input(pake_ctx, step,
8624 buf + input_offset, length);
8625 if (status != PSA_SUCCESS) {
8626 return psa_ssl_status_to_mbedtls(status);
Valerio Settia08b1a42022-11-17 15:10:02 +01008627 }
8628
8629 input_offset += length;
8630 }
8631 }
8632
Gilles Peskine449bd832023-01-11 14:50:10 +01008633 if (input_offset != len) {
Valerio Setti61ea17d2022-11-18 12:11:00 +01008634 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01008635 }
Valerio Setti30ebe112022-11-17 16:23:34 +01008636
Gilles Peskine449bd832023-01-11 14:50:10 +01008637 return 0;
Valerio Settia08b1a42022-11-17 15:10:02 +01008638}
8639
Valerio Setti6b3dab02022-11-17 17:14:54 +01008640int mbedtls_psa_ecjpake_write_round(
Gilles Peskine449bd832023-01-11 14:50:10 +01008641 psa_pake_operation_t *pake_ctx,
8642 unsigned char *buf,
8643 size_t len, size_t *olen,
8644 mbedtls_ecjpake_rounds_t round)
Valerio Settia08b1a42022-11-17 15:10:02 +01008645{
8646 psa_status_t status;
8647 size_t output_offset = 0;
8648 size_t output_len;
Valerio Setti819de862022-11-17 18:05:19 +01008649 /*
Valerio Setti6b3dab02022-11-17 17:14:54 +01008650 * At round one repeat the KEY_SHARE, ZK_PUBLIC & ZF_PROOF twice
8651 * At round two perform a single cycle
8652 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008653 unsigned int remaining_steps = (round == MBEDTLS_ECJPAKE_ROUND_ONE) ? 2 : 1;
Valerio Settia08b1a42022-11-17 15:10:02 +01008654
Gilles Peskine449bd832023-01-11 14:50:10 +01008655 for (; remaining_steps > 0; remaining_steps--) {
8656 for (psa_pake_step_t step = PSA_PAKE_STEP_KEY_SHARE;
8657 step <= PSA_PAKE_STEP_ZK_PROOF;
8658 ++step) {
Valerio Setti79f6b6b2022-11-21 14:17:03 +01008659 /*
Valerio Settid4a9b1a2022-11-22 11:11:10 +01008660 * For each step, prepend 1 byte with the length of the data as
8661 * given by psa_pake_output().
Valerio Setti79f6b6b2022-11-21 14:17:03 +01008662 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008663 status = psa_pake_output(pake_ctx, step,
8664 buf + output_offset + 1,
8665 len - output_offset - 1,
8666 &output_len);
8667 if (status != PSA_SUCCESS) {
8668 return psa_ssl_status_to_mbedtls(status);
Valerio Settia08b1a42022-11-17 15:10:02 +01008669 }
8670
Valerio Setti99d88c12022-11-22 16:03:43 +01008671 *(buf + output_offset) = (uint8_t) output_len;
Valerio Setti79f6b6b2022-11-21 14:17:03 +01008672
8673 output_offset += output_len + 1;
Valerio Settia08b1a42022-11-17 15:10:02 +01008674 }
8675 }
8676
8677 *olen = output_offset;
8678
Gilles Peskine449bd832023-01-11 14:50:10 +01008679 return 0;
Valerio Settia08b1a42022-11-17 15:10:02 +01008680}
Valerio Settia08b1a42022-11-17 15:10:02 +01008681#endif //MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED && MBEDTLS_USE_PSA_CRYPTO
8682
Jerry Yuee40f9d2022-02-17 14:55:16 +08008683#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008684int mbedtls_ssl_get_key_exchange_md_tls1_2(mbedtls_ssl_context *ssl,
8685 unsigned char *hash, size_t *hashlen,
8686 unsigned char *data, size_t data_len,
8687 mbedtls_md_type_t md_alg)
Jerry Yuee40f9d2022-02-17 14:55:16 +08008688{
8689 psa_status_t status;
8690 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01008691 psa_algorithm_t hash_alg = mbedtls_hash_info_psa_from_md(md_alg);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008692
Gilles Peskine449bd832023-01-11 14:50:10 +01008693 MBEDTLS_SSL_DEBUG_MSG(3, ("Perform PSA-based computation of digest of ServerKeyExchange"));
Jerry Yuee40f9d2022-02-17 14:55:16 +08008694
Gilles Peskine449bd832023-01-11 14:50:10 +01008695 if ((status = psa_hash_setup(&hash_operation,
8696 hash_alg)) != PSA_SUCCESS) {
8697 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_setup", status);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008698 goto exit;
8699 }
8700
Gilles Peskine449bd832023-01-11 14:50:10 +01008701 if ((status = psa_hash_update(&hash_operation, ssl->handshake->randbytes,
8702 64)) != PSA_SUCCESS) {
8703 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_update", status);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008704 goto exit;
8705 }
8706
Gilles Peskine449bd832023-01-11 14:50:10 +01008707 if ((status = psa_hash_update(&hash_operation,
8708 data, data_len)) != PSA_SUCCESS) {
8709 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_update", status);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008710 goto exit;
8711 }
8712
Gilles Peskine449bd832023-01-11 14:50:10 +01008713 if ((status = psa_hash_finish(&hash_operation, hash, PSA_HASH_MAX_SIZE,
8714 hashlen)) != PSA_SUCCESS) {
8715 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_finish", status);
8716 goto exit;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008717 }
8718
8719exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008720 if (status != PSA_SUCCESS) {
8721 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8722 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
8723 switch (status) {
Jerry Yuee40f9d2022-02-17 14:55:16 +08008724 case PSA_ERROR_NOT_SUPPORTED:
Gilles Peskine449bd832023-01-11 14:50:10 +01008725 return MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008726 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
8727 case PSA_ERROR_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +01008728 return MBEDTLS_ERR_MD_BAD_INPUT_DATA;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008729 case PSA_ERROR_INSUFFICIENT_MEMORY:
Gilles Peskine449bd832023-01-11 14:50:10 +01008730 return MBEDTLS_ERR_MD_ALLOC_FAILED;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008731 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01008732 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008733 }
8734 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008735 return 0;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008736}
8737
8738#else
8739
Gilles Peskine449bd832023-01-11 14:50:10 +01008740int mbedtls_ssl_get_key_exchange_md_tls1_2(mbedtls_ssl_context *ssl,
8741 unsigned char *hash, size_t *hashlen,
8742 unsigned char *data, size_t data_len,
8743 mbedtls_md_type_t md_alg)
Jerry Yuee40f9d2022-02-17 14:55:16 +08008744{
8745 int ret = 0;
8746 mbedtls_md_context_t ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +01008747 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type(md_alg);
8748 *hashlen = mbedtls_md_get_size(md_info);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008749
Gilles Peskine449bd832023-01-11 14:50:10 +01008750 MBEDTLS_SSL_DEBUG_MSG(3, ("Perform mbedtls-based computation of digest of ServerKeyExchange"));
Jerry Yuee40f9d2022-02-17 14:55:16 +08008751
Gilles Peskine449bd832023-01-11 14:50:10 +01008752 mbedtls_md_init(&ctx);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008753
8754 /*
8755 * digitally-signed struct {
8756 * opaque client_random[32];
8757 * opaque server_random[32];
8758 * ServerDHParams params;
8759 * };
8760 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008761 if ((ret = mbedtls_md_setup(&ctx, md_info, 0)) != 0) {
8762 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_setup", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008763 goto exit;
8764 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008765 if ((ret = mbedtls_md_starts(&ctx)) != 0) {
8766 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_starts", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008767 goto exit;
8768 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008769 if ((ret = mbedtls_md_update(&ctx, ssl->handshake->randbytes, 64)) != 0) {
8770 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_update", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008771 goto exit;
8772 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008773 if ((ret = mbedtls_md_update(&ctx, data, data_len)) != 0) {
8774 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_update", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008775 goto exit;
8776 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008777 if ((ret = mbedtls_md_finish(&ctx, hash)) != 0) {
8778 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_finish", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008779 goto exit;
8780 }
8781
8782exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008783 mbedtls_md_free(&ctx);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008784
Gilles Peskine449bd832023-01-11 14:50:10 +01008785 if (ret != 0) {
8786 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8787 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
8788 }
Jerry Yuee40f9d2022-02-17 14:55:16 +08008789
Gilles Peskine449bd832023-01-11 14:50:10 +01008790 return ret;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008791}
8792#endif /* MBEDTLS_USE_PSA_CRYPTO */
8793
Jerry Yud9d91da2022-02-17 14:57:06 +08008794#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
8795
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008796/* Find the preferred hash for a given signature algorithm. */
8797unsigned int mbedtls_ssl_tls12_get_preferred_hash_for_sig_alg(
Gilles Peskine449bd832023-01-11 14:50:10 +01008798 mbedtls_ssl_context *ssl,
8799 unsigned int sig_alg)
Jerry Yud9d91da2022-02-17 14:57:06 +08008800{
Gabor Mezei078e8032022-04-27 21:17:56 +02008801 unsigned int i;
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008802 uint16_t *received_sig_algs = ssl->handshake->received_sig_algs;
Gabor Mezei078e8032022-04-27 21:17:56 +02008803
Gilles Peskine449bd832023-01-11 14:50:10 +01008804 if (sig_alg == MBEDTLS_SSL_SIG_ANON) {
8805 return MBEDTLS_SSL_HASH_NONE;
8806 }
Gabor Mezei078e8032022-04-27 21:17:56 +02008807
Gilles Peskine449bd832023-01-11 14:50:10 +01008808 for (i = 0; received_sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++) {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008809 unsigned int hash_alg_received =
Gilles Peskine449bd832023-01-11 14:50:10 +01008810 MBEDTLS_SSL_TLS12_HASH_ALG_FROM_SIG_AND_HASH_ALG(
8811 received_sig_algs[i]);
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008812 unsigned int sig_alg_received =
Gilles Peskine449bd832023-01-11 14:50:10 +01008813 MBEDTLS_SSL_TLS12_SIG_ALG_FROM_SIG_AND_HASH_ALG(
8814 received_sig_algs[i]);
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008815
Gilles Peskine449bd832023-01-11 14:50:10 +01008816 if (sig_alg == sig_alg_received) {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008817#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008818 if (ssl->handshake->key_cert && ssl->handshake->key_cert->key) {
Neil Armstrong96eceb82022-06-30 18:05:05 +02008819 psa_algorithm_t psa_hash_alg =
Gilles Peskine449bd832023-01-11 14:50:10 +01008820 mbedtls_hash_info_psa_from_md(hash_alg_received);
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008821
Gilles Peskine449bd832023-01-11 14:50:10 +01008822 if (sig_alg_received == MBEDTLS_SSL_SIG_ECDSA &&
8823 !mbedtls_pk_can_do_ext(ssl->handshake->key_cert->key,
8824 PSA_ALG_ECDSA(psa_hash_alg),
8825 PSA_KEY_USAGE_SIGN_HASH)) {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008826 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01008827 }
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008828
Gilles Peskine449bd832023-01-11 14:50:10 +01008829 if (sig_alg_received == MBEDTLS_SSL_SIG_RSA &&
8830 !mbedtls_pk_can_do_ext(ssl->handshake->key_cert->key,
8831 PSA_ALG_RSA_PKCS1V15_SIGN(
8832 psa_hash_alg),
8833 PSA_KEY_USAGE_SIGN_HASH)) {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008834 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01008835 }
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008836 }
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008837#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong96eceb82022-06-30 18:05:05 +02008838
Gilles Peskine449bd832023-01-11 14:50:10 +01008839 return hash_alg_received;
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008840 }
Jerry Yud9d91da2022-02-17 14:57:06 +08008841 }
Jerry Yud9d91da2022-02-17 14:57:06 +08008842
Gilles Peskine449bd832023-01-11 14:50:10 +01008843 return MBEDTLS_SSL_HASH_NONE;
Jerry Yud9d91da2022-02-17 14:57:06 +08008844}
8845
8846#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
8847
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008848/* Serialization of TLS 1.2 sessions:
8849 *
8850 * struct {
8851 * uint64 start_time;
8852 * uint8 ciphersuite[2]; // defined by the standard
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008853 * uint8 session_id_len; // at most 32
8854 * opaque session_id[32];
8855 * opaque master[48]; // fixed length in the standard
8856 * uint32 verify_result;
8857 * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert
8858 * opaque ticket<0..2^24-1>; // length 0 means no ticket
8859 * uint32 ticket_lifetime;
8860 * uint8 mfl_code; // up to 255 according to standard
8861 * uint8 encrypt_then_mac; // 0 or 1
8862 * } serialized_session_tls12;
8863 *
8864 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008865static size_t ssl_tls12_session_save(const mbedtls_ssl_session *session,
8866 unsigned char *buf,
8867 size_t buf_len)
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008868{
8869 unsigned char *p = buf;
8870 size_t used = 0;
8871
8872#if defined(MBEDTLS_HAVE_TIME)
8873 uint64_t start;
8874#endif
8875#if defined(MBEDTLS_X509_CRT_PARSE_C)
8876#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8877 size_t cert_len;
8878#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8879#endif /* MBEDTLS_X509_CRT_PARSE_C */
8880
8881 /*
8882 * Time
8883 */
8884#if defined(MBEDTLS_HAVE_TIME)
8885 used += 8;
8886
Gilles Peskine449bd832023-01-11 14:50:10 +01008887 if (used <= buf_len) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008888 start = (uint64_t) session->start;
8889
Gilles Peskine449bd832023-01-11 14:50:10 +01008890 MBEDTLS_PUT_UINT64_BE(start, p, 0);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008891 p += 8;
8892 }
8893#endif /* MBEDTLS_HAVE_TIME */
8894
8895 /*
8896 * Basic mandatory fields
8897 */
8898 used += 2 /* ciphersuite */
Gilles Peskine449bd832023-01-11 14:50:10 +01008899 + 1 /* id_len */
8900 + sizeof(session->id)
8901 + sizeof(session->master)
8902 + 4; /* verify_result */
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008903
Gilles Peskine449bd832023-01-11 14:50:10 +01008904 if (used <= buf_len) {
8905 MBEDTLS_PUT_UINT16_BE(session->ciphersuite, p, 0);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008906 p += 2;
8907
Gilles Peskine449bd832023-01-11 14:50:10 +01008908 *p++ = MBEDTLS_BYTE_0(session->id_len);
8909 memcpy(p, session->id, 32);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008910 p += 32;
8911
Gilles Peskine449bd832023-01-11 14:50:10 +01008912 memcpy(p, session->master, 48);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008913 p += 48;
8914
Gilles Peskine449bd832023-01-11 14:50:10 +01008915 MBEDTLS_PUT_UINT32_BE(session->verify_result, p, 0);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008916 p += 4;
8917 }
8918
8919 /*
8920 * Peer's end-entity certificate
8921 */
8922#if defined(MBEDTLS_X509_CRT_PARSE_C)
8923#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01008924 if (session->peer_cert == NULL) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008925 cert_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01008926 } else {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008927 cert_len = session->peer_cert->raw.len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008928 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008929
8930 used += 3 + cert_len;
8931
Gilles Peskine449bd832023-01-11 14:50:10 +01008932 if (used <= buf_len) {
8933 *p++ = MBEDTLS_BYTE_2(cert_len);
8934 *p++ = MBEDTLS_BYTE_1(cert_len);
8935 *p++ = MBEDTLS_BYTE_0(cert_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008936
Gilles Peskine449bd832023-01-11 14:50:10 +01008937 if (session->peer_cert != NULL) {
8938 memcpy(p, session->peer_cert->raw.p, cert_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008939 p += cert_len;
8940 }
8941 }
8942#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008943 if (session->peer_cert_digest != NULL) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008944 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008945 if (used <= buf_len) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008946 *p++ = (unsigned char) session->peer_cert_digest_type;
8947 *p++ = (unsigned char) session->peer_cert_digest_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008948 memcpy(p, session->peer_cert_digest,
8949 session->peer_cert_digest_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008950 p += session->peer_cert_digest_len;
8951 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008952 } else {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008953 used += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01008954 if (used <= buf_len) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008955 *p++ = (unsigned char) MBEDTLS_MD_NONE;
8956 *p++ = 0;
8957 }
8958 }
8959#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8960#endif /* MBEDTLS_X509_CRT_PARSE_C */
8961
8962 /*
8963 * Session ticket if any, plus associated data
8964 */
8965#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8966 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
8967
Gilles Peskine449bd832023-01-11 14:50:10 +01008968 if (used <= buf_len) {
8969 *p++ = MBEDTLS_BYTE_2(session->ticket_len);
8970 *p++ = MBEDTLS_BYTE_1(session->ticket_len);
8971 *p++ = MBEDTLS_BYTE_0(session->ticket_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008972
Gilles Peskine449bd832023-01-11 14:50:10 +01008973 if (session->ticket != NULL) {
8974 memcpy(p, session->ticket, session->ticket_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008975 p += session->ticket_len;
8976 }
8977
Gilles Peskine449bd832023-01-11 14:50:10 +01008978 MBEDTLS_PUT_UINT32_BE(session->ticket_lifetime, p, 0);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008979 p += 4;
8980 }
8981#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8982
8983 /*
8984 * Misc extension-related info
8985 */
8986#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8987 used += 1;
8988
Gilles Peskine449bd832023-01-11 14:50:10 +01008989 if (used <= buf_len) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008990 *p++ = session->mfl_code;
Gilles Peskine449bd832023-01-11 14:50:10 +01008991 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008992#endif
8993
8994#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
8995 used += 1;
8996
Gilles Peskine449bd832023-01-11 14:50:10 +01008997 if (used <= buf_len) {
8998 *p++ = MBEDTLS_BYTE_0(session->encrypt_then_mac);
8999 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009000#endif
9001
Gilles Peskine449bd832023-01-11 14:50:10 +01009002 return used;
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009003}
9004
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02009005MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01009006static int ssl_tls12_session_load(mbedtls_ssl_session *session,
9007 const unsigned char *buf,
9008 size_t len)
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009009{
9010#if defined(MBEDTLS_HAVE_TIME)
9011 uint64_t start;
9012#endif
9013#if defined(MBEDTLS_X509_CRT_PARSE_C)
9014#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
9015 size_t cert_len;
9016#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9017#endif /* MBEDTLS_X509_CRT_PARSE_C */
9018
9019 const unsigned char *p = buf;
9020 const unsigned char * const end = buf + len;
9021
9022 /*
9023 * Time
9024 */
9025#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01009026 if (8 > (size_t) (end - p)) {
9027 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9028 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009029
Gilles Peskine449bd832023-01-11 14:50:10 +01009030 start = ((uint64_t) p[0] << 56) |
9031 ((uint64_t) p[1] << 48) |
9032 ((uint64_t) p[2] << 40) |
9033 ((uint64_t) p[3] << 32) |
9034 ((uint64_t) p[4] << 24) |
9035 ((uint64_t) p[5] << 16) |
9036 ((uint64_t) p[6] << 8) |
9037 ((uint64_t) p[7]);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009038 p += 8;
9039
9040 session->start = (time_t) start;
9041#endif /* MBEDTLS_HAVE_TIME */
9042
9043 /*
9044 * Basic mandatory fields
9045 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009046 if (2 + 1 + 32 + 48 + 4 > (size_t) (end - p)) {
9047 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9048 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009049
Gilles Peskine449bd832023-01-11 14:50:10 +01009050 session->ciphersuite = (p[0] << 8) | p[1];
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009051 p += 2;
9052
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009053 session->id_len = *p++;
Gilles Peskine449bd832023-01-11 14:50:10 +01009054 memcpy(session->id, p, 32);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009055 p += 32;
9056
Gilles Peskine449bd832023-01-11 14:50:10 +01009057 memcpy(session->master, p, 48);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009058 p += 48;
9059
Gilles Peskine449bd832023-01-11 14:50:10 +01009060 session->verify_result = ((uint32_t) p[0] << 24) |
9061 ((uint32_t) p[1] << 16) |
9062 ((uint32_t) p[2] << 8) |
9063 ((uint32_t) p[3]);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009064 p += 4;
9065
9066 /* Immediately clear invalid pointer values that have been read, in case
9067 * we exit early before we replaced them with valid ones. */
9068#if defined(MBEDTLS_X509_CRT_PARSE_C)
9069#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
9070 session->peer_cert = NULL;
9071#else
9072 session->peer_cert_digest = NULL;
9073#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9074#endif /* MBEDTLS_X509_CRT_PARSE_C */
9075#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
9076 session->ticket = NULL;
9077#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
9078
9079 /*
9080 * Peer certificate
9081 */
9082#if defined(MBEDTLS_X509_CRT_PARSE_C)
9083#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
9084 /* Deserialize CRT from the end of the ticket. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009085 if (3 > (size_t) (end - p)) {
9086 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9087 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009088
Gilles Peskine449bd832023-01-11 14:50:10 +01009089 cert_len = (p[0] << 16) | (p[1] << 8) | p[2];
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009090 p += 3;
9091
Gilles Peskine449bd832023-01-11 14:50:10 +01009092 if (cert_len != 0) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009093 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
9094
Gilles Peskine449bd832023-01-11 14:50:10 +01009095 if (cert_len > (size_t) (end - p)) {
9096 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9097 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009098
Gilles Peskine449bd832023-01-11 14:50:10 +01009099 session->peer_cert = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009100
Gilles Peskine449bd832023-01-11 14:50:10 +01009101 if (session->peer_cert == NULL) {
9102 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
9103 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009104
Gilles Peskine449bd832023-01-11 14:50:10 +01009105 mbedtls_x509_crt_init(session->peer_cert);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009106
Gilles Peskine449bd832023-01-11 14:50:10 +01009107 if ((ret = mbedtls_x509_crt_parse_der(session->peer_cert,
9108 p, cert_len)) != 0) {
9109 mbedtls_x509_crt_free(session->peer_cert);
9110 mbedtls_free(session->peer_cert);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009111 session->peer_cert = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01009112 return ret;
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009113 }
9114
9115 p += cert_len;
9116 }
9117#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9118 /* Deserialize CRT digest from the end of the ticket. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009119 if (2 > (size_t) (end - p)) {
9120 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9121 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009122
9123 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
9124 session->peer_cert_digest_len = (size_t) *p++;
9125
Gilles Peskine449bd832023-01-11 14:50:10 +01009126 if (session->peer_cert_digest_len != 0) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009127 const mbedtls_md_info_t *md_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01009128 mbedtls_md_info_from_type(session->peer_cert_digest_type);
9129 if (md_info == NULL) {
9130 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9131 }
9132 if (session->peer_cert_digest_len != mbedtls_md_get_size(md_info)) {
9133 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9134 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009135
Gilles Peskine449bd832023-01-11 14:50:10 +01009136 if (session->peer_cert_digest_len > (size_t) (end - p)) {
9137 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9138 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009139
9140 session->peer_cert_digest =
Gilles Peskine449bd832023-01-11 14:50:10 +01009141 mbedtls_calloc(1, session->peer_cert_digest_len);
9142 if (session->peer_cert_digest == NULL) {
9143 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
9144 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009145
Gilles Peskine449bd832023-01-11 14:50:10 +01009146 memcpy(session->peer_cert_digest, p,
9147 session->peer_cert_digest_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009148 p += session->peer_cert_digest_len;
9149 }
9150#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9151#endif /* MBEDTLS_X509_CRT_PARSE_C */
9152
9153 /*
9154 * Session ticket and associated data
9155 */
9156#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01009157 if (3 > (size_t) (end - p)) {
9158 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9159 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009160
Gilles Peskine449bd832023-01-11 14:50:10 +01009161 session->ticket_len = (p[0] << 16) | (p[1] << 8) | p[2];
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009162 p += 3;
9163
Gilles Peskine449bd832023-01-11 14:50:10 +01009164 if (session->ticket_len != 0) {
9165 if (session->ticket_len > (size_t) (end - p)) {
9166 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9167 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009168
Gilles Peskine449bd832023-01-11 14:50:10 +01009169 session->ticket = mbedtls_calloc(1, session->ticket_len);
9170 if (session->ticket == NULL) {
9171 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
9172 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009173
Gilles Peskine449bd832023-01-11 14:50:10 +01009174 memcpy(session->ticket, p, session->ticket_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009175 p += session->ticket_len;
9176 }
9177
Gilles Peskine449bd832023-01-11 14:50:10 +01009178 if (4 > (size_t) (end - p)) {
9179 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9180 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009181
Gilles Peskine449bd832023-01-11 14:50:10 +01009182 session->ticket_lifetime = ((uint32_t) p[0] << 24) |
9183 ((uint32_t) p[1] << 16) |
9184 ((uint32_t) p[2] << 8) |
9185 ((uint32_t) p[3]);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009186 p += 4;
9187#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
9188
9189 /*
9190 * Misc extension-related info
9191 */
9192#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01009193 if (1 > (size_t) (end - p)) {
9194 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9195 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009196
9197 session->mfl_code = *p++;
9198#endif
9199
9200#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01009201 if (1 > (size_t) (end - p)) {
9202 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9203 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009204
9205 session->encrypt_then_mac = *p++;
9206#endif
9207
9208 /* Done, should have consumed entire buffer */
Gilles Peskine449bd832023-01-11 14:50:10 +01009209 if (p != end) {
9210 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9211 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009212
Gilles Peskine449bd832023-01-11 14:50:10 +01009213 return 0;
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009214}
Jerry Yudc7bd172022-02-17 13:44:15 +08009215#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9216
XiaokangQian75d40ef2022-04-20 11:05:24 +00009217int mbedtls_ssl_validate_ciphersuite(
9218 const mbedtls_ssl_context *ssl,
9219 const mbedtls_ssl_ciphersuite_t *suite_info,
9220 mbedtls_ssl_protocol_version min_tls_version,
Gilles Peskine449bd832023-01-11 14:50:10 +01009221 mbedtls_ssl_protocol_version max_tls_version)
XiaokangQian75d40ef2022-04-20 11:05:24 +00009222{
9223 (void) ssl;
9224
Gilles Peskine449bd832023-01-11 14:50:10 +01009225 if (suite_info == NULL) {
9226 return -1;
9227 }
XiaokangQian75d40ef2022-04-20 11:05:24 +00009228
Gilles Peskine449bd832023-01-11 14:50:10 +01009229 if ((suite_info->min_tls_version > max_tls_version) ||
9230 (suite_info->max_tls_version < min_tls_version)) {
9231 return -1;
XiaokangQian75d40ef2022-04-20 11:05:24 +00009232 }
9233
XiaokangQian060d8672022-04-21 09:24:56 +00009234#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_CLI_C)
XiaokangQian75d40ef2022-04-20 11:05:24 +00009235#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Neil Armstrongca7d5062022-05-31 14:43:23 +02009236#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01009237 if (suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
9238 ssl->handshake->psa_pake_ctx_is_ok != 1)
Neil Armstrongca7d5062022-05-31 14:43:23 +02009239#else
Gilles Peskine449bd832023-01-11 14:50:10 +01009240 if (suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
9241 mbedtls_ecjpake_check(&ssl->handshake->ecjpake_ctx) != 0)
Neil Armstrongca7d5062022-05-31 14:43:23 +02009242#endif /* MBEDTLS_USE_PSA_CRYPTO */
XiaokangQian75d40ef2022-04-20 11:05:24 +00009243 {
Gilles Peskine449bd832023-01-11 14:50:10 +01009244 return -1;
XiaokangQian75d40ef2022-04-20 11:05:24 +00009245 }
9246#endif
9247
9248 /* Don't suggest PSK-based ciphersuite if no PSK is available. */
9249#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01009250 if (mbedtls_ssl_ciphersuite_uses_psk(suite_info) &&
9251 mbedtls_ssl_conf_has_static_psk(ssl->conf) == 0) {
9252 return -1;
XiaokangQian75d40ef2022-04-20 11:05:24 +00009253 }
9254#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
9255#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9256
Gilles Peskine449bd832023-01-11 14:50:10 +01009257 return 0;
XiaokangQian75d40ef2022-04-20 11:05:24 +00009258}
9259
Ronald Crone68ab4f2022-10-05 12:46:29 +02009260#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
XiaokangQianeaf36512022-04-24 09:07:44 +00009261/*
9262 * Function for writing a signature algorithm extension.
9263 *
9264 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
9265 * value (TLS 1.3 RFC8446):
9266 * enum {
9267 * ....
9268 * ecdsa_secp256r1_sha256( 0x0403 ),
9269 * ecdsa_secp384r1_sha384( 0x0503 ),
9270 * ecdsa_secp521r1_sha512( 0x0603 ),
9271 * ....
9272 * } SignatureScheme;
9273 *
9274 * struct {
9275 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
9276 * } SignatureSchemeList;
9277 *
9278 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
9279 * value (TLS 1.2 RFC5246):
9280 * enum {
9281 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
9282 * sha512(6), (255)
9283 * } HashAlgorithm;
9284 *
9285 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
9286 * SignatureAlgorithm;
9287 *
9288 * struct {
9289 * HashAlgorithm hash;
9290 * SignatureAlgorithm signature;
9291 * } SignatureAndHashAlgorithm;
9292 *
9293 * SignatureAndHashAlgorithm
9294 * supported_signature_algorithms<2..2^16-2>;
9295 *
9296 * The TLS 1.3 signature algorithm extension was defined to be a compatible
9297 * generalization of the TLS 1.2 signature algorithm extension.
9298 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
9299 * `SignatureScheme` field of TLS 1.3
9300 *
9301 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009302int mbedtls_ssl_write_sig_alg_ext(mbedtls_ssl_context *ssl, unsigned char *buf,
9303 const unsigned char *end, size_t *out_len)
XiaokangQianeaf36512022-04-24 09:07:44 +00009304{
9305 unsigned char *p = buf;
9306 unsigned char *supported_sig_alg; /* Start of supported_signature_algorithms */
9307 size_t supported_sig_alg_len = 0; /* Length of supported_signature_algorithms */
9308
9309 *out_len = 0;
9310
Gilles Peskine449bd832023-01-11 14:50:10 +01009311 MBEDTLS_SSL_DEBUG_MSG(3, ("adding signature_algorithms extension"));
XiaokangQianeaf36512022-04-24 09:07:44 +00009312
9313 /* Check if we have space for header and length field:
9314 * - extension_type (2 bytes)
9315 * - extension_data_length (2 bytes)
9316 * - supported_signature_algorithms_length (2 bytes)
9317 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009318 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 6);
XiaokangQianeaf36512022-04-24 09:07:44 +00009319 p += 6;
9320
9321 /*
9322 * Write supported_signature_algorithms
9323 */
9324 supported_sig_alg = p;
Gilles Peskine449bd832023-01-11 14:50:10 +01009325 const uint16_t *sig_alg = mbedtls_ssl_get_sig_algs(ssl);
9326 if (sig_alg == NULL) {
9327 return MBEDTLS_ERR_SSL_BAD_CONFIG;
9328 }
XiaokangQianeaf36512022-04-24 09:07:44 +00009329
Gilles Peskine449bd832023-01-11 14:50:10 +01009330 for (; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++) {
9331 MBEDTLS_SSL_DEBUG_MSG(3, ("got signature scheme [%x] %s",
9332 *sig_alg,
9333 mbedtls_ssl_sig_alg_to_str(*sig_alg)));
9334 if (!mbedtls_ssl_sig_alg_is_supported(ssl, *sig_alg)) {
XiaokangQianeaf36512022-04-24 09:07:44 +00009335 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01009336 }
9337 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
9338 MBEDTLS_PUT_UINT16_BE(*sig_alg, p, 0);
XiaokangQianeaf36512022-04-24 09:07:44 +00009339 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01009340 MBEDTLS_SSL_DEBUG_MSG(3, ("sent signature scheme [%x] %s",
9341 *sig_alg,
9342 mbedtls_ssl_sig_alg_to_str(*sig_alg)));
XiaokangQianeaf36512022-04-24 09:07:44 +00009343 }
9344
9345 /* Length of supported_signature_algorithms */
9346 supported_sig_alg_len = p - supported_sig_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01009347 if (supported_sig_alg_len == 0) {
9348 MBEDTLS_SSL_DEBUG_MSG(1, ("No signature algorithms defined."));
9349 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
XiaokangQianeaf36512022-04-24 09:07:44 +00009350 }
9351
Gilles Peskine449bd832023-01-11 14:50:10 +01009352 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SIG_ALG, buf, 0);
9353 MBEDTLS_PUT_UINT16_BE(supported_sig_alg_len + 2, buf, 2);
9354 MBEDTLS_PUT_UINT16_BE(supported_sig_alg_len, buf, 4);
XiaokangQianeaf36512022-04-24 09:07:44 +00009355
XiaokangQianeaf36512022-04-24 09:07:44 +00009356 *out_len = p - buf;
9357
9358#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01009359 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_SIG_ALG);
XiaokangQianeaf36512022-04-24 09:07:44 +00009360#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu0c354a22022-08-29 15:25:36 +08009361
Gilles Peskine449bd832023-01-11 14:50:10 +01009362 return 0;
XiaokangQianeaf36512022-04-24 09:07:44 +00009363}
Ronald Crone68ab4f2022-10-05 12:46:29 +02009364#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
XiaokangQianeaf36512022-04-24 09:07:44 +00009365
XiaokangQian40a35232022-05-07 09:02:40 +00009366#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
XiaokangQian9b2b7712022-05-17 02:57:00 +00009367/*
9368 * mbedtls_ssl_parse_server_name_ext
9369 *
9370 * Structure of server_name extension:
9371 *
9372 * enum {
9373 * host_name(0), (255)
9374 * } NameType;
9375 * opaque HostName<1..2^16-1>;
9376 *
9377 * struct {
9378 * NameType name_type;
9379 * select (name_type) {
9380 * case host_name: HostName;
9381 * } name;
9382 * } ServerName;
9383 * struct {
9384 * ServerName server_name_list<1..2^16-1>
9385 * } ServerNameList;
9386 */
Ronald Cronce7d76e2022-07-08 18:56:49 +02009387MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01009388int mbedtls_ssl_parse_server_name_ext(mbedtls_ssl_context *ssl,
9389 const unsigned char *buf,
9390 const unsigned char *end)
XiaokangQian40a35232022-05-07 09:02:40 +00009391{
9392 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
9393 const unsigned char *p = buf;
XiaokangQian9b2b7712022-05-17 02:57:00 +00009394 size_t server_name_list_len, hostname_len;
9395 const unsigned char *server_name_list_end;
XiaokangQian40a35232022-05-07 09:02:40 +00009396
Gilles Peskine449bd832023-01-11 14:50:10 +01009397 MBEDTLS_SSL_DEBUG_MSG(3, ("parse ServerName extension"));
XiaokangQian40a35232022-05-07 09:02:40 +00009398
Gilles Peskine449bd832023-01-11 14:50:10 +01009399 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
9400 server_name_list_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian40a35232022-05-07 09:02:40 +00009401 p += 2;
9402
Gilles Peskine449bd832023-01-11 14:50:10 +01009403 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, server_name_list_len);
XiaokangQian9b2b7712022-05-17 02:57:00 +00009404 server_name_list_end = p + server_name_list_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01009405 while (p < server_name_list_end) {
9406 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, server_name_list_end, 3);
9407 hostname_len = MBEDTLS_GET_UINT16_BE(p, 1);
9408 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, server_name_list_end,
9409 hostname_len + 3);
XiaokangQian40a35232022-05-07 09:02:40 +00009410
Gilles Peskine449bd832023-01-11 14:50:10 +01009411 if (p[0] == MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME) {
XiaokangQian75fe8c72022-06-15 09:42:45 +00009412 /* sni_name is intended to be used only during the parsing of the
9413 * ClientHello message (it is reset to NULL before the end of
9414 * the message parsing). Thus it is ok to just point to the
9415 * reception buffer and not make a copy of it.
9416 */
XiaokangQianf2a94202022-05-20 06:44:24 +00009417 ssl->handshake->sni_name = p + 3;
9418 ssl->handshake->sni_name_len = hostname_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01009419 if (ssl->conf->f_sni == NULL) {
9420 return 0;
XiaokangQian40a35232022-05-07 09:02:40 +00009421 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009422 ret = ssl->conf->f_sni(ssl->conf->p_sni,
9423 ssl, p + 3, hostname_len);
9424 if (ret != 0) {
9425 MBEDTLS_SSL_DEBUG_RET(1, "ssl_sni_wrapper", ret);
9426 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_UNRECOGNIZED_NAME,
9427 MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME);
9428 return MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME;
9429 }
9430 return 0;
XiaokangQian40a35232022-05-07 09:02:40 +00009431 }
9432
9433 p += hostname_len + 3;
9434 }
9435
Gilles Peskine449bd832023-01-11 14:50:10 +01009436 return 0;
XiaokangQian40a35232022-05-07 09:02:40 +00009437}
9438#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
9439
XiaokangQianacb39922022-06-17 10:18:48 +00009440#if defined(MBEDTLS_SSL_ALPN)
Ronald Cronce7d76e2022-07-08 18:56:49 +02009441MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01009442int mbedtls_ssl_parse_alpn_ext(mbedtls_ssl_context *ssl,
9443 const unsigned char *buf,
9444 const unsigned char *end)
XiaokangQianacb39922022-06-17 10:18:48 +00009445{
9446 const unsigned char *p = buf;
XiaokangQianc7403452022-06-23 03:24:12 +00009447 size_t protocol_name_list_len;
XiaokangQian95d5f542022-06-24 02:29:26 +00009448 const unsigned char *protocol_name_list;
9449 const unsigned char *protocol_name_list_end;
XiaokangQianc7403452022-06-23 03:24:12 +00009450 size_t protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009451
9452 /* If ALPN not configured, just ignore the extension */
Gilles Peskine449bd832023-01-11 14:50:10 +01009453 if (ssl->conf->alpn_list == NULL) {
9454 return 0;
9455 }
XiaokangQianacb39922022-06-17 10:18:48 +00009456
9457 /*
XiaokangQianc7403452022-06-23 03:24:12 +00009458 * RFC7301, section 3.1
9459 * opaque ProtocolName<1..2^8-1>;
XiaokangQianacb39922022-06-17 10:18:48 +00009460 *
XiaokangQianc7403452022-06-23 03:24:12 +00009461 * struct {
9462 * ProtocolName protocol_name_list<2..2^16-1>
9463 * } ProtocolNameList;
XiaokangQianacb39922022-06-17 10:18:48 +00009464 */
9465
XiaokangQianc7403452022-06-23 03:24:12 +00009466 /*
XiaokangQian0b776e22022-06-24 09:04:59 +00009467 * protocol_name_list_len 2 bytes
9468 * protocol_name_len 1 bytes
9469 * protocol_name >=1 byte
XiaokangQianc7403452022-06-23 03:24:12 +00009470 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009471 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 4);
XiaokangQianacb39922022-06-17 10:18:48 +00009472
Gilles Peskine449bd832023-01-11 14:50:10 +01009473 protocol_name_list_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQianacb39922022-06-17 10:18:48 +00009474 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01009475 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, protocol_name_list_len);
XiaokangQian95d5f542022-06-24 02:29:26 +00009476 protocol_name_list = p;
9477 protocol_name_list_end = p + protocol_name_list_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009478
9479 /* Validate peer's list (lengths) */
Gilles Peskine449bd832023-01-11 14:50:10 +01009480 while (p < protocol_name_list_end) {
XiaokangQian95d5f542022-06-24 02:29:26 +00009481 protocol_name_len = *p++;
Gilles Peskine449bd832023-01-11 14:50:10 +01009482 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, protocol_name_list_end,
9483 protocol_name_len);
9484 if (protocol_name_len == 0) {
XiaokangQian95d5f542022-06-24 02:29:26 +00009485 MBEDTLS_SSL_PEND_FATAL_ALERT(
9486 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
Gilles Peskine449bd832023-01-11 14:50:10 +01009487 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
9488 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
XiaokangQian95d5f542022-06-24 02:29:26 +00009489 }
9490
9491 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009492 }
9493
9494 /* Use our order of preference */
Gilles Peskine449bd832023-01-11 14:50:10 +01009495 for (const char **alpn = ssl->conf->alpn_list; *alpn != NULL; alpn++) {
9496 size_t const alpn_len = strlen(*alpn);
XiaokangQian95d5f542022-06-24 02:29:26 +00009497 p = protocol_name_list;
Gilles Peskine449bd832023-01-11 14:50:10 +01009498 while (p < protocol_name_list_end) {
XiaokangQian95d5f542022-06-24 02:29:26 +00009499 protocol_name_len = *p++;
Gilles Peskine449bd832023-01-11 14:50:10 +01009500 if (protocol_name_len == alpn_len &&
9501 memcmp(p, *alpn, alpn_len) == 0) {
XiaokangQianacb39922022-06-17 10:18:48 +00009502 ssl->alpn_chosen = *alpn;
Gilles Peskine449bd832023-01-11 14:50:10 +01009503 return 0;
XiaokangQianacb39922022-06-17 10:18:48 +00009504 }
XiaokangQian95d5f542022-06-24 02:29:26 +00009505
9506 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009507 }
9508 }
9509
XiaokangQian95d5f542022-06-24 02:29:26 +00009510 /* If we get here, no match was found */
XiaokangQianacb39922022-06-17 10:18:48 +00009511 MBEDTLS_SSL_PEND_FATAL_ALERT(
Gilles Peskine449bd832023-01-11 14:50:10 +01009512 MBEDTLS_SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL,
9513 MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL);
9514 return MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL;
XiaokangQianacb39922022-06-17 10:18:48 +00009515}
9516
Gilles Peskine449bd832023-01-11 14:50:10 +01009517int mbedtls_ssl_write_alpn_ext(mbedtls_ssl_context *ssl,
9518 unsigned char *buf,
9519 unsigned char *end,
9520 size_t *out_len)
XiaokangQianacb39922022-06-17 10:18:48 +00009521{
9522 unsigned char *p = buf;
XiaokangQian95d5f542022-06-24 02:29:26 +00009523 size_t protocol_name_len;
XiaokangQianc7403452022-06-23 03:24:12 +00009524 *out_len = 0;
XiaokangQianacb39922022-06-17 10:18:48 +00009525
Gilles Peskine449bd832023-01-11 14:50:10 +01009526 if (ssl->alpn_chosen == NULL) {
9527 return 0;
XiaokangQianacb39922022-06-17 10:18:48 +00009528 }
9529
Gilles Peskine449bd832023-01-11 14:50:10 +01009530 protocol_name_len = strlen(ssl->alpn_chosen);
9531 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 7 + protocol_name_len);
XiaokangQianacb39922022-06-17 10:18:48 +00009532
Gilles Peskine449bd832023-01-11 14:50:10 +01009533 MBEDTLS_SSL_DEBUG_MSG(3, ("server side, adding alpn extension"));
XiaokangQianacb39922022-06-17 10:18:48 +00009534 /*
9535 * 0 . 1 ext identifier
9536 * 2 . 3 ext length
9537 * 4 . 5 protocol list length
9538 * 6 . 6 protocol name length
9539 * 7 . 7+n protocol name
9540 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009541 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_ALPN, p, 0);
XiaokangQianacb39922022-06-17 10:18:48 +00009542
XiaokangQian95d5f542022-06-24 02:29:26 +00009543 *out_len = 7 + protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009544
Gilles Peskine449bd832023-01-11 14:50:10 +01009545 MBEDTLS_PUT_UINT16_BE(protocol_name_len + 3, p, 2);
9546 MBEDTLS_PUT_UINT16_BE(protocol_name_len + 1, p, 4);
XiaokangQian0b776e22022-06-24 09:04:59 +00009547 /* Note: the length of the chosen protocol has been checked to be less
9548 * than 255 bytes in `mbedtls_ssl_conf_alpn_protocols`.
9549 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009550 p[6] = MBEDTLS_BYTE_0(protocol_name_len);
XiaokangQianacb39922022-06-17 10:18:48 +00009551
Gilles Peskine449bd832023-01-11 14:50:10 +01009552 memcpy(p + 7, ssl->alpn_chosen, protocol_name_len);
Jerry Yub95dd362022-11-08 21:19:34 +08009553
9554#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01009555 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_ALPN);
Jerry Yub95dd362022-11-08 21:19:34 +08009556#endif
9557
Gilles Peskine449bd832023-01-11 14:50:10 +01009558 return 0;
XiaokangQianacb39922022-06-17 10:18:48 +00009559}
9560#endif /* MBEDTLS_SSL_ALPN */
9561
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009562#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
Xiaokang Qian03409292022-10-12 02:49:52 +00009563 defined(MBEDTLS_SSL_SESSION_TICKETS) && \
Xiaokang Qianed0620c2022-10-12 06:58:13 +00009564 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \
Xiaokang Qianed3afcd2022-10-12 08:31:11 +00009565 defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01009566int mbedtls_ssl_session_set_hostname(mbedtls_ssl_session *session,
9567 const char *hostname)
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009568{
9569 /* Initialize to suppress unnecessary compiler warning */
9570 size_t hostname_len = 0;
9571
9572 /* Check if new hostname is valid before
9573 * making any change to current one */
Gilles Peskine449bd832023-01-11 14:50:10 +01009574 if (hostname != NULL) {
9575 hostname_len = strlen(hostname);
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009576
Gilles Peskine449bd832023-01-11 14:50:10 +01009577 if (hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN) {
9578 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9579 }
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009580 }
9581
9582 /* Now it's clear that we will overwrite the old hostname,
9583 * so we can free it safely */
Gilles Peskine449bd832023-01-11 14:50:10 +01009584 if (session->hostname != NULL) {
9585 mbedtls_platform_zeroize(session->hostname,
9586 strlen(session->hostname));
9587 mbedtls_free(session->hostname);
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009588 }
9589
9590 /* Passing NULL as hostname shall clear the old one */
Gilles Peskine449bd832023-01-11 14:50:10 +01009591 if (hostname == NULL) {
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009592 session->hostname = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01009593 } else {
9594 session->hostname = mbedtls_calloc(1, hostname_len + 1);
9595 if (session->hostname == NULL) {
9596 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
9597 }
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009598
Gilles Peskine449bd832023-01-11 14:50:10 +01009599 memcpy(session->hostname, hostname, hostname_len);
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009600 }
9601
Gilles Peskine449bd832023-01-11 14:50:10 +01009602 return 0;
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009603}
Xiaokang Qian03409292022-10-12 02:49:52 +00009604#endif /* MBEDTLS_SSL_PROTO_TLS1_3 &&
9605 MBEDTLS_SSL_SESSION_TICKETS &&
Xiaokang Qianed0620c2022-10-12 06:58:13 +00009606 MBEDTLS_SSL_SERVER_NAME_INDICATION &&
Xiaokang Qianed3afcd2022-10-12 08:31:11 +00009607 MBEDTLS_SSL_CLI_C */
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009608
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009609#endif /* MBEDTLS_SSL_TLS_C */