blob: 375233d5710d360e4a7ee5fca164ce0c25a00547 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01002 * TLS shared functions
Paul Bakker5121ce52009-01-03 21:22:43 +00003 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakker5121ce52009-01-03 21:22:43 +000018 */
19/*
Paul Bakker5121ce52009-01-03 21:22:43 +000020 * http://www.ietf.org/rfc/rfc2246.txt
21 * http://www.ietf.org/rfc/rfc4346.txt
22 */
23
Gilles Peskinedb09ef62020-06-03 01:43:33 +020024#include "common.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#if defined(MBEDTLS_SSL_TLS_C)
Paul Bakker5121ce52009-01-03 21:22:43 +000027
Jerry Yub476a442022-01-21 18:14:45 +080028#include <assert.h>
29
SimonBd5800b72016-04-26 07:43:27 +010030#include "mbedtls/platform.h"
SimonBd5800b72016-04-26 07:43:27 +010031
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000032#include "mbedtls/ssl.h"
Ronald Cron9f0fba32022-02-10 16:45:15 +010033#include "ssl_client.h"
Ronald Cron27c85e72022-03-08 11:37:55 +010034#include "ssl_debug_helpers.h"
Chris Jones84a773f2021-03-05 18:38:47 +000035#include "ssl_misc.h"
Andrzej Kurek25f27152022-08-17 16:09:31 -040036
Janos Follath73c616b2019-12-18 15:07:04 +000037#include "mbedtls/debug.h"
38#include "mbedtls/error.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050039#include "mbedtls/platform_util.h"
Hanno Beckera835da52019-05-16 12:39:07 +010040#include "mbedtls/version.h"
Gabor Mezei765862c2021-10-19 12:22:25 +020041#include "mbedtls/constant_time.h"
Paul Bakker0be444a2013-08-27 21:55:01 +020042
Rich Evans00ab4702015-02-06 13:43:58 +000043#include <string.h>
44
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050045#if defined(MBEDTLS_USE_PSA_CRYPTO)
46#include "mbedtls/psa_util.h"
47#include "psa/crypto.h"
48#endif
Manuel Pégourié-Gonnard07018f92022-09-15 11:29:35 +020049#include "mbedtls/legacy_or_psa.h"
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050050
Janos Follath23bdca02016-10-07 14:47:14 +010051#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000052#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020053#endif
54
Ronald Cronad8c17b2022-06-10 17:18:09 +020055#if defined(MBEDTLS_TEST_HOOKS)
56static mbedtls_ssl_chk_buf_ptr_args chk_buf_ptr_fail_args;
57
58void mbedtls_ssl_set_chk_buf_ptr_fail_args(
Gilles Peskine449bd832023-01-11 14:50:10 +010059 const uint8_t *cur, const uint8_t *end, size_t need)
Ronald Cronad8c17b2022-06-10 17:18:09 +020060{
61 chk_buf_ptr_fail_args.cur = cur;
62 chk_buf_ptr_fail_args.end = end;
63 chk_buf_ptr_fail_args.need = need;
64}
65
Gilles Peskine449bd832023-01-11 14:50:10 +010066void mbedtls_ssl_reset_chk_buf_ptr_fail_args(void)
Ronald Cronad8c17b2022-06-10 17:18:09 +020067{
Gilles Peskine449bd832023-01-11 14:50:10 +010068 memset(&chk_buf_ptr_fail_args, 0, sizeof(chk_buf_ptr_fail_args));
Ronald Cronad8c17b2022-06-10 17:18:09 +020069}
70
Gilles Peskine449bd832023-01-11 14:50:10 +010071int mbedtls_ssl_cmp_chk_buf_ptr_fail_args(mbedtls_ssl_chk_buf_ptr_args *args)
Ronald Cronad8c17b2022-06-10 17:18:09 +020072{
Gilles Peskine449bd832023-01-11 14:50:10 +010073 return (chk_buf_ptr_fail_args.cur != args->cur) ||
74 (chk_buf_ptr_fail_args.end != args->end) ||
75 (chk_buf_ptr_fail_args.need != args->need);
Ronald Cronad8c17b2022-06-10 17:18:09 +020076}
77#endif /* MBEDTLS_TEST_HOOKS */
78
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020079#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +010080
Hanno Beckera0e20d02019-05-15 14:03:01 +010081#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf8542cf2019-04-09 15:22:03 +010082/* Top-level Connection ID API */
83
Gilles Peskine449bd832023-01-11 14:50:10 +010084int mbedtls_ssl_conf_cid(mbedtls_ssl_config *conf,
85 size_t len,
86 int ignore_other_cid)
Hanno Beckerad4a1372019-05-03 13:06:44 +010087{
Gilles Peskine449bd832023-01-11 14:50:10 +010088 if (len > MBEDTLS_SSL_CID_IN_LEN_MAX) {
89 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
90 }
Hanno Beckerad4a1372019-05-03 13:06:44 +010091
Gilles Peskine449bd832023-01-11 14:50:10 +010092 if (ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_FAIL &&
93 ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE) {
94 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Becker611ac772019-05-14 11:45:26 +010095 }
96
97 conf->ignore_unexpected_cid = ignore_other_cid;
Hanno Beckerad4a1372019-05-03 13:06:44 +010098 conf->cid_len = len;
Gilles Peskine449bd832023-01-11 14:50:10 +010099 return 0;
Hanno Beckerad4a1372019-05-03 13:06:44 +0100100}
101
Gilles Peskine449bd832023-01-11 14:50:10 +0100102int mbedtls_ssl_set_cid(mbedtls_ssl_context *ssl,
103 int enable,
104 unsigned char const *own_cid,
105 size_t own_cid_len)
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100106{
Gilles Peskine449bd832023-01-11 14:50:10 +0100107 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
108 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
109 }
Hanno Becker76a79ab2019-05-03 14:38:32 +0100110
Hanno Beckerca092242019-04-25 16:01:49 +0100111 ssl->negotiate_cid = enable;
Gilles Peskine449bd832023-01-11 14:50:10 +0100112 if (enable == MBEDTLS_SSL_CID_DISABLED) {
113 MBEDTLS_SSL_DEBUG_MSG(3, ("Disable use of CID extension."));
114 return 0;
Hanno Beckerca092242019-04-25 16:01:49 +0100115 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100116 MBEDTLS_SSL_DEBUG_MSG(3, ("Enable use of CID extension."));
117 MBEDTLS_SSL_DEBUG_BUF(3, "Own CID", own_cid, own_cid_len);
Hanno Beckerca092242019-04-25 16:01:49 +0100118
Gilles Peskine449bd832023-01-11 14:50:10 +0100119 if (own_cid_len != ssl->conf->cid_len) {
120 MBEDTLS_SSL_DEBUG_MSG(3, ("CID length %u does not match CID length %u in config",
121 (unsigned) own_cid_len,
122 (unsigned) ssl->conf->cid_len));
123 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Beckerca092242019-04-25 16:01:49 +0100124 }
125
Gilles Peskine449bd832023-01-11 14:50:10 +0100126 memcpy(ssl->own_cid, own_cid, own_cid_len);
Hanno Beckerb7ee0cf2019-04-30 14:07:31 +0100127 /* Truncation is not an issue here because
128 * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */
129 ssl->own_cid_len = (uint8_t) own_cid_len;
Hanno Beckerca092242019-04-25 16:01:49 +0100130
Gilles Peskine449bd832023-01-11 14:50:10 +0100131 return 0;
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100132}
133
Gilles Peskine449bd832023-01-11 14:50:10 +0100134int mbedtls_ssl_get_own_cid(mbedtls_ssl_context *ssl,
135 int *enabled,
136 unsigned char own_cid[MBEDTLS_SSL_CID_OUT_LEN_MAX],
137 size_t *own_cid_len)
Paul Elliott0113cf12022-03-11 20:26:47 +0000138{
139 *enabled = MBEDTLS_SSL_CID_DISABLED;
140
Gilles Peskine449bd832023-01-11 14:50:10 +0100141 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
142 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
143 }
Paul Elliott0113cf12022-03-11 20:26:47 +0000144
145 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID length is
146 * zero as this is indistinguishable from not requesting to use
147 * the CID extension. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100148 if (ssl->own_cid_len == 0 || ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED) {
149 return 0;
150 }
Paul Elliott0113cf12022-03-11 20:26:47 +0000151
Gilles Peskine449bd832023-01-11 14:50:10 +0100152 if (own_cid_len != NULL) {
Paul Elliott0113cf12022-03-11 20:26:47 +0000153 *own_cid_len = ssl->own_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100154 if (own_cid != NULL) {
155 memcpy(own_cid, ssl->own_cid, ssl->own_cid_len);
156 }
Paul Elliott0113cf12022-03-11 20:26:47 +0000157 }
158
159 *enabled = MBEDTLS_SSL_CID_ENABLED;
160
Gilles Peskine449bd832023-01-11 14:50:10 +0100161 return 0;
Paul Elliott0113cf12022-03-11 20:26:47 +0000162}
163
Gilles Peskine449bd832023-01-11 14:50:10 +0100164int mbedtls_ssl_get_peer_cid(mbedtls_ssl_context *ssl,
165 int *enabled,
166 unsigned char peer_cid[MBEDTLS_SSL_CID_OUT_LEN_MAX],
167 size_t *peer_cid_len)
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100168{
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100169 *enabled = MBEDTLS_SSL_CID_DISABLED;
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100170
Gilles Peskine449bd832023-01-11 14:50:10 +0100171 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
172 mbedtls_ssl_is_handshake_over(ssl) == 0) {
173 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Becker76a79ab2019-05-03 14:38:32 +0100174 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100175
Hanno Beckerc5f24222019-05-03 12:54:52 +0100176 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions
177 * were used, but client and server requested the empty CID.
178 * This is indistinguishable from not using the CID extension
179 * in the first place. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100180 if (ssl->transform_in->in_cid_len == 0 &&
181 ssl->transform_in->out_cid_len == 0) {
182 return 0;
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100183 }
184
Gilles Peskine449bd832023-01-11 14:50:10 +0100185 if (peer_cid_len != NULL) {
Hanno Becker615ef172019-05-22 16:50:35 +0100186 *peer_cid_len = ssl->transform_in->out_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100187 if (peer_cid != NULL) {
188 memcpy(peer_cid, ssl->transform_in->out_cid,
189 ssl->transform_in->out_cid_len);
Hanno Becker615ef172019-05-22 16:50:35 +0100190 }
191 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100192
193 *enabled = MBEDTLS_SSL_CID_ENABLED;
194
Gilles Peskine449bd832023-01-11 14:50:10 +0100195 return 0;
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100196}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100197#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100198
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200199#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200200
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200201#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200202/*
203 * Convert max_fragment_length codes to length.
204 * RFC 6066 says:
205 * enum{
206 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
207 * } MaxFragmentLength;
208 * and we add 0 -> extension unused
209 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100210static unsigned int ssl_mfl_code_to_length(int mfl)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200211{
Gilles Peskine449bd832023-01-11 14:50:10 +0100212 switch (mfl) {
213 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
214 return MBEDTLS_TLS_EXT_ADV_CONTENT_LEN;
215 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
216 return 512;
217 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
218 return 1024;
219 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
220 return 2048;
221 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
222 return 4096;
223 default:
224 return MBEDTLS_TLS_EXT_ADV_CONTENT_LEN;
Angus Grattond8213d02016-05-25 20:56:48 +1000225 }
226}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200227#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200228
Gilles Peskine449bd832023-01-11 14:50:10 +0100229int mbedtls_ssl_session_copy(mbedtls_ssl_session *dst,
230 const mbedtls_ssl_session *src)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200231{
Gilles Peskine449bd832023-01-11 14:50:10 +0100232 mbedtls_ssl_session_free(dst);
233 memcpy(dst, src, sizeof(mbedtls_ssl_session));
吴敬辉0b716112021-11-29 10:46:35 +0800234#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
235 dst->ticket = NULL;
Xiaokang Qian87306442022-10-12 09:47:38 +0000236#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
237 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaokang Qian126bf8e2022-10-13 02:22:40 +0000238 dst->hostname = NULL;
吴敬辉0b716112021-11-29 10:46:35 +0800239#endif
Xiaokang Qian87306442022-10-12 09:47:38 +0000240#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
吴敬辉0b716112021-11-29 10:46:35 +0800241
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200242#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker6d1986e2019-02-07 12:27:42 +0000243
244#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100245 if (src->peer_cert != NULL) {
Janos Follath865b3eb2019-12-16 11:46:15 +0000246 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker2292d1f2013-09-15 17:06:49 +0200247
Gilles Peskine449bd832023-01-11 14:50:10 +0100248 dst->peer_cert = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
249 if (dst->peer_cert == NULL) {
250 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
251 }
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200252
Gilles Peskine449bd832023-01-11 14:50:10 +0100253 mbedtls_x509_crt_init(dst->peer_cert);
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200254
Gilles Peskine449bd832023-01-11 14:50:10 +0100255 if ((ret = mbedtls_x509_crt_parse_der(dst->peer_cert, src->peer_cert->raw.p,
256 src->peer_cert->raw.len)) != 0) {
257 mbedtls_free(dst->peer_cert);
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200258 dst->peer_cert = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100259 return ret;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200260 }
261 }
Hanno Becker6d1986e2019-02-07 12:27:42 +0000262#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100263 if (src->peer_cert_digest != NULL) {
Hanno Becker9198ad12019-02-05 17:00:50 +0000264 dst->peer_cert_digest =
Gilles Peskine449bd832023-01-11 14:50:10 +0100265 mbedtls_calloc(1, src->peer_cert_digest_len);
266 if (dst->peer_cert_digest == NULL) {
267 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
268 }
Hanno Becker9198ad12019-02-05 17:00:50 +0000269
Gilles Peskine449bd832023-01-11 14:50:10 +0100270 memcpy(dst->peer_cert_digest, src->peer_cert_digest,
271 src->peer_cert_digest_len);
Hanno Becker9198ad12019-02-05 17:00:50 +0000272 dst->peer_cert_digest_type = src->peer_cert_digest_type;
Hanno Beckeraccc5992019-02-25 10:06:59 +0000273 dst->peer_cert_digest_len = src->peer_cert_digest_len;
Hanno Becker9198ad12019-02-05 17:00:50 +0000274 }
275#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
276
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200277#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200278
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200279#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100280 if (src->ticket != NULL) {
281 dst->ticket = mbedtls_calloc(1, src->ticket_len);
282 if (dst->ticket == NULL) {
283 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
284 }
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200285
Gilles Peskine449bd832023-01-11 14:50:10 +0100286 memcpy(dst->ticket, src->ticket, src->ticket_len);
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200287 }
Xiaokang Qian126bf8e2022-10-13 02:22:40 +0000288
289#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
290 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +0100291 if (src->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Xiaokang Qian126bf8e2022-10-13 02:22:40 +0000292 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100293 ret = mbedtls_ssl_session_set_hostname(dst, src->hostname);
294 if (ret != 0) {
295 return ret;
296 }
Xiaokang Qian126bf8e2022-10-13 02:22:40 +0000297 }
298#endif /* MBEDTLS_SSL_PROTO_TLS1_3 &&
299 MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200300#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200301
Gilles Peskine449bd832023-01-11 14:50:10 +0100302 return 0;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200303}
304
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500305#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200306MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100307static int resize_buffer(unsigned char **buffer, size_t len_new, size_t *len_old)
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500308{
Gilles Peskine449bd832023-01-11 14:50:10 +0100309 unsigned char *resized_buffer = mbedtls_calloc(1, len_new);
310 if (resized_buffer == NULL) {
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500311 return -1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100312 }
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500313
314 /* We want to copy len_new bytes when downsizing the buffer, and
315 * len_old bytes when upsizing, so we choose the smaller of two sizes,
316 * to fit one buffer into another. Size checks, ensuring that no data is
317 * lost, are done outside of this function. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100318 memcpy(resized_buffer, *buffer,
319 (len_new < *len_old) ? len_new : *len_old);
320 mbedtls_platform_zeroize(*buffer, *len_old);
321 mbedtls_free(*buffer);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500322
323 *buffer = resized_buffer;
324 *len_old = len_new;
325
326 return 0;
327}
Andrzej Kurek4a063792020-10-21 15:08:44 +0200328
Gilles Peskine449bd832023-01-11 14:50:10 +0100329static void handle_buffer_resizing(mbedtls_ssl_context *ssl, int downsizing,
330 size_t in_buf_new_len,
331 size_t out_buf_new_len)
Andrzej Kurek4a063792020-10-21 15:08:44 +0200332{
333 int modified = 0;
334 size_t written_in = 0, iv_offset_in = 0, len_offset_in = 0;
335 size_t written_out = 0, iv_offset_out = 0, len_offset_out = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100336 if (ssl->in_buf != NULL) {
Andrzej Kurek4a063792020-10-21 15:08:44 +0200337 written_in = ssl->in_msg - ssl->in_buf;
338 iv_offset_in = ssl->in_iv - ssl->in_buf;
339 len_offset_in = ssl->in_len - ssl->in_buf;
Gilles Peskine449bd832023-01-11 14:50:10 +0100340 if (downsizing ?
Andrzej Kurek4a063792020-10-21 15:08:44 +0200341 ssl->in_buf_len > in_buf_new_len && ssl->in_left < in_buf_new_len :
Gilles Peskine449bd832023-01-11 14:50:10 +0100342 ssl->in_buf_len < in_buf_new_len) {
343 if (resize_buffer(&ssl->in_buf, in_buf_new_len, &ssl->in_buf_len) != 0) {
344 MBEDTLS_SSL_DEBUG_MSG(1, ("input buffer resizing failed - out of memory"));
345 } else {
346 MBEDTLS_SSL_DEBUG_MSG(2, ("Reallocating in_buf to %" MBEDTLS_PRINTF_SIZET,
347 in_buf_new_len));
Andrzej Kurek4a063792020-10-21 15:08:44 +0200348 modified = 1;
349 }
350 }
351 }
352
Gilles Peskine449bd832023-01-11 14:50:10 +0100353 if (ssl->out_buf != NULL) {
Andrzej Kurek4a063792020-10-21 15:08:44 +0200354 written_out = ssl->out_msg - ssl->out_buf;
355 iv_offset_out = ssl->out_iv - ssl->out_buf;
356 len_offset_out = ssl->out_len - ssl->out_buf;
Gilles Peskine449bd832023-01-11 14:50:10 +0100357 if (downsizing ?
Andrzej Kurek4a063792020-10-21 15:08:44 +0200358 ssl->out_buf_len > out_buf_new_len && ssl->out_left < out_buf_new_len :
Gilles Peskine449bd832023-01-11 14:50:10 +0100359 ssl->out_buf_len < out_buf_new_len) {
360 if (resize_buffer(&ssl->out_buf, out_buf_new_len, &ssl->out_buf_len) != 0) {
361 MBEDTLS_SSL_DEBUG_MSG(1, ("output buffer resizing failed - out of memory"));
362 } else {
363 MBEDTLS_SSL_DEBUG_MSG(2, ("Reallocating out_buf to %" MBEDTLS_PRINTF_SIZET,
364 out_buf_new_len));
Andrzej Kurek4a063792020-10-21 15:08:44 +0200365 modified = 1;
366 }
367 }
368 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100369 if (modified) {
Andrzej Kurek4a063792020-10-21 15:08:44 +0200370 /* Update pointers here to avoid doing it twice. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100371 mbedtls_ssl_reset_in_out_pointers(ssl);
Andrzej Kurek4a063792020-10-21 15:08:44 +0200372 /* Fields below might not be properly updated with record
373 * splitting or with CID, so they are manually updated here. */
374 ssl->out_msg = ssl->out_buf + written_out;
375 ssl->out_len = ssl->out_buf + len_offset_out;
376 ssl->out_iv = ssl->out_buf + iv_offset_out;
377
378 ssl->in_msg = ssl->in_buf + written_in;
379 ssl->in_len = ssl->in_buf + len_offset_in;
380 ssl->in_iv = ssl->in_buf + iv_offset_in;
381 }
382}
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500383#endif /* MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH */
384
Jerry Yudb8c48a2022-01-27 14:54:54 +0800385#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yued14c932022-02-17 13:40:45 +0800386
387#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Gilles Peskine449bd832023-01-11 14:50:10 +0100388typedef int (*tls_prf_fn)(const unsigned char *secret, size_t slen,
389 const char *label,
390 const unsigned char *random, size_t rlen,
391 unsigned char *dstbuf, size_t dlen);
Jerry Yued14c932022-02-17 13:40:45 +0800392
Gilles Peskine449bd832023-01-11 14:50:10 +0100393static tls_prf_fn ssl_tls12prf_from_cs(int ciphersuite_id);
Jerry Yued14c932022-02-17 13:40:45 +0800394
395#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
396
397/* Type for the TLS PRF */
398typedef int ssl_tls_prf_t(const unsigned char *, size_t, const char *,
399 const unsigned char *, size_t,
400 unsigned char *, size_t);
401
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200402MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100403static int ssl_tls12_populate_transform(mbedtls_ssl_transform *transform,
404 int ciphersuite,
405 const unsigned char master[48],
Neil Armstrongf2c82f02022-04-05 11:16:53 +0200406#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +0100407 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +0200408#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +0100409 ssl_tls_prf_t tls_prf,
410 const unsigned char randbytes[64],
411 mbedtls_ssl_protocol_version tls_version,
412 unsigned endpoint,
413 const mbedtls_ssl_context *ssl);
Jerry Yued14c932022-02-17 13:40:45 +0800414
Andrzej Kurek25f27152022-08-17 16:09:31 -0400415#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200416MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100417static int tls_prf_sha256(const unsigned char *secret, size_t slen,
Ron Eldor51d3ab52019-05-12 14:54:30 +0300418 const char *label,
419 const unsigned char *random, size_t rlen,
Gilles Peskine449bd832023-01-11 14:50:10 +0100420 unsigned char *dstbuf, size_t dlen);
421static void ssl_calc_verify_tls_sha256(const mbedtls_ssl_context *, unsigned char *, size_t *);
422static void ssl_calc_finished_tls_sha256(mbedtls_ssl_context *, unsigned char *, int);
423
424#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
425
426#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
427MBEDTLS_CHECK_RETURN_CRITICAL
428static int tls_prf_sha384(const unsigned char *secret, size_t slen,
429 const char *label,
430 const unsigned char *random, size_t rlen,
431 unsigned char *dstbuf, size_t dlen);
432
433static void ssl_calc_verify_tls_sha384(const mbedtls_ssl_context *, unsigned char *, size_t *);
434static void ssl_calc_finished_tls_sha384(mbedtls_ssl_context *, unsigned char *, int);
435#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
436
437static size_t ssl_tls12_session_save(const mbedtls_ssl_session *session,
438 unsigned char *buf,
439 size_t buf_len);
440
441MBEDTLS_CHECK_RETURN_CRITICAL
442static int ssl_tls12_session_load(mbedtls_ssl_session *session,
443 const unsigned char *buf,
444 size_t len);
445#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
446
447static void ssl_update_checksum_start(mbedtls_ssl_context *, const unsigned char *, size_t);
448
449#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
450static void ssl_update_checksum_sha256(mbedtls_ssl_context *, const unsigned char *, size_t);
451#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
452
453#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
454static void ssl_update_checksum_sha384(mbedtls_ssl_context *, const unsigned char *, size_t);
455#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
456
457int mbedtls_ssl_tls_prf(const mbedtls_tls_prf_types prf,
458 const unsigned char *secret, size_t slen,
459 const char *label,
460 const unsigned char *random, size_t rlen,
461 unsigned char *dstbuf, size_t dlen)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300462{
463 mbedtls_ssl_tls_prf_cb *tls_prf = NULL;
464
Gilles Peskine449bd832023-01-11 14:50:10 +0100465 switch (prf) {
Ron Eldord2f25f72019-05-15 14:54:22 +0300466#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek25f27152022-08-17 16:09:31 -0400467#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300468 case MBEDTLS_SSL_TLS_PRF_SHA384:
469 tls_prf = tls_prf_sha384;
Gilles Peskine449bd832023-01-11 14:50:10 +0100470 break;
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400471#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -0400472#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300473 case MBEDTLS_SSL_TLS_PRF_SHA256:
474 tls_prf = tls_prf_sha256;
Gilles Peskine449bd832023-01-11 14:50:10 +0100475 break;
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400476#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Ron Eldord2f25f72019-05-15 14:54:22 +0300477#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100478 default:
479 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Ron Eldor51d3ab52019-05-12 14:54:30 +0300480 }
481
Gilles Peskine449bd832023-01-11 14:50:10 +0100482 return tls_prf(secret, slen, label, random, rlen, dstbuf, dlen);
Ron Eldor51d3ab52019-05-12 14:54:30 +0300483}
484
Jerry Yuc73c6182022-02-08 20:29:25 +0800485#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100486static void ssl_clear_peer_cert(mbedtls_ssl_session *session)
Jerry Yuc73c6182022-02-08 20:29:25 +0800487{
488#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100489 if (session->peer_cert != NULL) {
490 mbedtls_x509_crt_free(session->peer_cert);
491 mbedtls_free(session->peer_cert);
Jerry Yuc73c6182022-02-08 20:29:25 +0800492 session->peer_cert = NULL;
493 }
494#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100495 if (session->peer_cert_digest != NULL) {
Jerry Yuc73c6182022-02-08 20:29:25 +0800496 /* Zeroization is not necessary. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100497 mbedtls_free(session->peer_cert_digest);
Jerry Yuc73c6182022-02-08 20:29:25 +0800498 session->peer_cert_digest = NULL;
499 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
500 session->peer_cert_digest_len = 0;
501 }
502#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
503}
504#endif /* MBEDTLS_X509_CRT_PARSE_C */
505
Gilles Peskine449bd832023-01-11 14:50:10 +0100506uint32_t mbedtls_ssl_get_extension_id(unsigned int extension_type)
Jerry Yu7a485c12022-10-31 13:08:18 +0800507{
Gilles Peskine449bd832023-01-11 14:50:10 +0100508 switch (extension_type) {
Jerry Yu7a485c12022-10-31 13:08:18 +0800509 case MBEDTLS_TLS_EXT_SERVERNAME:
Gilles Peskine449bd832023-01-11 14:50:10 +0100510 return MBEDTLS_SSL_EXT_ID_SERVERNAME;
Jerry Yu7a485c12022-10-31 13:08:18 +0800511
512 case MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100513 return MBEDTLS_SSL_EXT_ID_MAX_FRAGMENT_LENGTH;
Jerry Yu7a485c12022-10-31 13:08:18 +0800514
515 case MBEDTLS_TLS_EXT_STATUS_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +0100516 return MBEDTLS_SSL_EXT_ID_STATUS_REQUEST;
Jerry Yu7a485c12022-10-31 13:08:18 +0800517
518 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
Gilles Peskine449bd832023-01-11 14:50:10 +0100519 return MBEDTLS_SSL_EXT_ID_SUPPORTED_GROUPS;
Jerry Yu7a485c12022-10-31 13:08:18 +0800520
521 case MBEDTLS_TLS_EXT_SIG_ALG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100522 return MBEDTLS_SSL_EXT_ID_SIG_ALG;
Jerry Yu7a485c12022-10-31 13:08:18 +0800523
524 case MBEDTLS_TLS_EXT_USE_SRTP:
Gilles Peskine449bd832023-01-11 14:50:10 +0100525 return MBEDTLS_SSL_EXT_ID_USE_SRTP;
Jerry Yu7a485c12022-10-31 13:08:18 +0800526
527 case MBEDTLS_TLS_EXT_HEARTBEAT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100528 return MBEDTLS_SSL_EXT_ID_HEARTBEAT;
Jerry Yu7a485c12022-10-31 13:08:18 +0800529
530 case MBEDTLS_TLS_EXT_ALPN:
Gilles Peskine449bd832023-01-11 14:50:10 +0100531 return MBEDTLS_SSL_EXT_ID_ALPN;
Jerry Yu7a485c12022-10-31 13:08:18 +0800532
533 case MBEDTLS_TLS_EXT_SCT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100534 return MBEDTLS_SSL_EXT_ID_SCT;
Jerry Yu7a485c12022-10-31 13:08:18 +0800535
536 case MBEDTLS_TLS_EXT_CLI_CERT_TYPE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100537 return MBEDTLS_SSL_EXT_ID_CLI_CERT_TYPE;
Jerry Yu7a485c12022-10-31 13:08:18 +0800538
539 case MBEDTLS_TLS_EXT_SERV_CERT_TYPE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100540 return MBEDTLS_SSL_EXT_ID_SERV_CERT_TYPE;
Jerry Yu7a485c12022-10-31 13:08:18 +0800541
542 case MBEDTLS_TLS_EXT_PADDING:
Gilles Peskine449bd832023-01-11 14:50:10 +0100543 return MBEDTLS_SSL_EXT_ID_PADDING;
Jerry Yu7a485c12022-10-31 13:08:18 +0800544
545 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100546 return MBEDTLS_SSL_EXT_ID_PRE_SHARED_KEY;
Jerry Yu7a485c12022-10-31 13:08:18 +0800547
548 case MBEDTLS_TLS_EXT_EARLY_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100549 return MBEDTLS_SSL_EXT_ID_EARLY_DATA;
Jerry Yu7a485c12022-10-31 13:08:18 +0800550
551 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
Gilles Peskine449bd832023-01-11 14:50:10 +0100552 return MBEDTLS_SSL_EXT_ID_SUPPORTED_VERSIONS;
Jerry Yu7a485c12022-10-31 13:08:18 +0800553
554 case MBEDTLS_TLS_EXT_COOKIE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100555 return MBEDTLS_SSL_EXT_ID_COOKIE;
Jerry Yu7a485c12022-10-31 13:08:18 +0800556
557 case MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES:
Gilles Peskine449bd832023-01-11 14:50:10 +0100558 return MBEDTLS_SSL_EXT_ID_PSK_KEY_EXCHANGE_MODES;
Jerry Yu7a485c12022-10-31 13:08:18 +0800559
560 case MBEDTLS_TLS_EXT_CERT_AUTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100561 return MBEDTLS_SSL_EXT_ID_CERT_AUTH;
Jerry Yu7a485c12022-10-31 13:08:18 +0800562
563 case MBEDTLS_TLS_EXT_OID_FILTERS:
Gilles Peskine449bd832023-01-11 14:50:10 +0100564 return MBEDTLS_SSL_EXT_ID_OID_FILTERS;
Jerry Yu7a485c12022-10-31 13:08:18 +0800565
566 case MBEDTLS_TLS_EXT_POST_HANDSHAKE_AUTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100567 return MBEDTLS_SSL_EXT_ID_POST_HANDSHAKE_AUTH;
Jerry Yu7a485c12022-10-31 13:08:18 +0800568
569 case MBEDTLS_TLS_EXT_SIG_ALG_CERT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100570 return MBEDTLS_SSL_EXT_ID_SIG_ALG_CERT;
Jerry Yu7a485c12022-10-31 13:08:18 +0800571
572 case MBEDTLS_TLS_EXT_KEY_SHARE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100573 return MBEDTLS_SSL_EXT_ID_KEY_SHARE;
Jerry Yu7a485c12022-10-31 13:08:18 +0800574
575 case MBEDTLS_TLS_EXT_TRUNCATED_HMAC:
Gilles Peskine449bd832023-01-11 14:50:10 +0100576 return MBEDTLS_SSL_EXT_ID_TRUNCATED_HMAC;
Jerry Yu7a485c12022-10-31 13:08:18 +0800577
578 case MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS:
Gilles Peskine449bd832023-01-11 14:50:10 +0100579 return MBEDTLS_SSL_EXT_ID_SUPPORTED_POINT_FORMATS;
Jerry Yu7a485c12022-10-31 13:08:18 +0800580
581 case MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC:
Gilles Peskine449bd832023-01-11 14:50:10 +0100582 return MBEDTLS_SSL_EXT_ID_ENCRYPT_THEN_MAC;
Jerry Yu7a485c12022-10-31 13:08:18 +0800583
584 case MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +0100585 return MBEDTLS_SSL_EXT_ID_EXTENDED_MASTER_SECRET;
Jerry Yu7a485c12022-10-31 13:08:18 +0800586
587 case MBEDTLS_TLS_EXT_SESSION_TICKET:
Gilles Peskine449bd832023-01-11 14:50:10 +0100588 return MBEDTLS_SSL_EXT_ID_SESSION_TICKET;
Jerry Yu7a485c12022-10-31 13:08:18 +0800589
590 }
591
Gilles Peskine449bd832023-01-11 14:50:10 +0100592 return MBEDTLS_SSL_EXT_ID_UNRECOGNIZED;
Jerry Yu7a485c12022-10-31 13:08:18 +0800593}
594
Gilles Peskine449bd832023-01-11 14:50:10 +0100595uint32_t mbedtls_ssl_get_extension_mask(unsigned int extension_type)
Jerry Yu7a485c12022-10-31 13:08:18 +0800596{
Gilles Peskine449bd832023-01-11 14:50:10 +0100597 return 1 << mbedtls_ssl_get_extension_id(extension_type);
Jerry Yu7a485c12022-10-31 13:08:18 +0800598}
599
Jerry Yud25cab02022-10-31 12:48:30 +0800600#if defined(MBEDTLS_DEBUG_C)
601static const char *extension_name_table[] = {
Jerry Yuea52ed92022-11-08 21:01:17 +0800602 [MBEDTLS_SSL_EXT_ID_UNRECOGNIZED] = "unrecognized",
Jerry Yud25cab02022-10-31 12:48:30 +0800603 [MBEDTLS_SSL_EXT_ID_SERVERNAME] = "server_name",
604 [MBEDTLS_SSL_EXT_ID_MAX_FRAGMENT_LENGTH] = "max_fragment_length",
605 [MBEDTLS_SSL_EXT_ID_STATUS_REQUEST] = "status_request",
606 [MBEDTLS_SSL_EXT_ID_SUPPORTED_GROUPS] = "supported_groups",
607 [MBEDTLS_SSL_EXT_ID_SIG_ALG] = "signature_algorithms",
608 [MBEDTLS_SSL_EXT_ID_USE_SRTP] = "use_srtp",
609 [MBEDTLS_SSL_EXT_ID_HEARTBEAT] = "heartbeat",
610 [MBEDTLS_SSL_EXT_ID_ALPN] = "application_layer_protocol_negotiation",
611 [MBEDTLS_SSL_EXT_ID_SCT] = "signed_certificate_timestamp",
612 [MBEDTLS_SSL_EXT_ID_CLI_CERT_TYPE] = "client_certificate_type",
613 [MBEDTLS_SSL_EXT_ID_SERV_CERT_TYPE] = "server_certificate_type",
614 [MBEDTLS_SSL_EXT_ID_PADDING] = "padding",
615 [MBEDTLS_SSL_EXT_ID_PRE_SHARED_KEY] = "pre_shared_key",
616 [MBEDTLS_SSL_EXT_ID_EARLY_DATA] = "early_data",
617 [MBEDTLS_SSL_EXT_ID_SUPPORTED_VERSIONS] = "supported_versions",
618 [MBEDTLS_SSL_EXT_ID_COOKIE] = "cookie",
619 [MBEDTLS_SSL_EXT_ID_PSK_KEY_EXCHANGE_MODES] = "psk_key_exchange_modes",
620 [MBEDTLS_SSL_EXT_ID_CERT_AUTH] = "certificate_authorities",
621 [MBEDTLS_SSL_EXT_ID_OID_FILTERS] = "oid_filters",
622 [MBEDTLS_SSL_EXT_ID_POST_HANDSHAKE_AUTH] = "post_handshake_auth",
623 [MBEDTLS_SSL_EXT_ID_SIG_ALG_CERT] = "signature_algorithms_cert",
624 [MBEDTLS_SSL_EXT_ID_KEY_SHARE] = "key_share",
625 [MBEDTLS_SSL_EXT_ID_TRUNCATED_HMAC] = "truncated_hmac",
626 [MBEDTLS_SSL_EXT_ID_SUPPORTED_POINT_FORMATS] = "supported_point_formats",
627 [MBEDTLS_SSL_EXT_ID_ENCRYPT_THEN_MAC] = "encrypt_then_mac",
628 [MBEDTLS_SSL_EXT_ID_EXTENDED_MASTER_SECRET] = "extended_master_secret",
629 [MBEDTLS_SSL_EXT_ID_SESSION_TICKET] = "session_ticket"
630};
631
Gilles Peskine449bd832023-01-11 14:50:10 +0100632static unsigned int extension_type_table[] = {
Jerry Yud25cab02022-10-31 12:48:30 +0800633 [MBEDTLS_SSL_EXT_ID_UNRECOGNIZED] = 0xff,
634 [MBEDTLS_SSL_EXT_ID_SERVERNAME] = MBEDTLS_TLS_EXT_SERVERNAME,
635 [MBEDTLS_SSL_EXT_ID_MAX_FRAGMENT_LENGTH] = MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH,
636 [MBEDTLS_SSL_EXT_ID_STATUS_REQUEST] = MBEDTLS_TLS_EXT_STATUS_REQUEST,
637 [MBEDTLS_SSL_EXT_ID_SUPPORTED_GROUPS] = MBEDTLS_TLS_EXT_SUPPORTED_GROUPS,
638 [MBEDTLS_SSL_EXT_ID_SIG_ALG] = MBEDTLS_TLS_EXT_SIG_ALG,
639 [MBEDTLS_SSL_EXT_ID_USE_SRTP] = MBEDTLS_TLS_EXT_USE_SRTP,
640 [MBEDTLS_SSL_EXT_ID_HEARTBEAT] = MBEDTLS_TLS_EXT_HEARTBEAT,
641 [MBEDTLS_SSL_EXT_ID_ALPN] = MBEDTLS_TLS_EXT_ALPN,
642 [MBEDTLS_SSL_EXT_ID_SCT] = MBEDTLS_TLS_EXT_SCT,
643 [MBEDTLS_SSL_EXT_ID_CLI_CERT_TYPE] = MBEDTLS_TLS_EXT_CLI_CERT_TYPE,
644 [MBEDTLS_SSL_EXT_ID_SERV_CERT_TYPE] = MBEDTLS_TLS_EXT_SERV_CERT_TYPE,
645 [MBEDTLS_SSL_EXT_ID_PADDING] = MBEDTLS_TLS_EXT_PADDING,
646 [MBEDTLS_SSL_EXT_ID_PRE_SHARED_KEY] = MBEDTLS_TLS_EXT_PRE_SHARED_KEY,
647 [MBEDTLS_SSL_EXT_ID_EARLY_DATA] = MBEDTLS_TLS_EXT_EARLY_DATA,
648 [MBEDTLS_SSL_EXT_ID_SUPPORTED_VERSIONS] = MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS,
649 [MBEDTLS_SSL_EXT_ID_COOKIE] = MBEDTLS_TLS_EXT_COOKIE,
650 [MBEDTLS_SSL_EXT_ID_PSK_KEY_EXCHANGE_MODES] = MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES,
651 [MBEDTLS_SSL_EXT_ID_CERT_AUTH] = MBEDTLS_TLS_EXT_CERT_AUTH,
652 [MBEDTLS_SSL_EXT_ID_OID_FILTERS] = MBEDTLS_TLS_EXT_OID_FILTERS,
653 [MBEDTLS_SSL_EXT_ID_POST_HANDSHAKE_AUTH] = MBEDTLS_TLS_EXT_POST_HANDSHAKE_AUTH,
654 [MBEDTLS_SSL_EXT_ID_SIG_ALG_CERT] = MBEDTLS_TLS_EXT_SIG_ALG_CERT,
655 [MBEDTLS_SSL_EXT_ID_KEY_SHARE] = MBEDTLS_TLS_EXT_KEY_SHARE,
656 [MBEDTLS_SSL_EXT_ID_TRUNCATED_HMAC] = MBEDTLS_TLS_EXT_TRUNCATED_HMAC,
657 [MBEDTLS_SSL_EXT_ID_SUPPORTED_POINT_FORMATS] = MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS,
658 [MBEDTLS_SSL_EXT_ID_ENCRYPT_THEN_MAC] = MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC,
659 [MBEDTLS_SSL_EXT_ID_EXTENDED_MASTER_SECRET] = MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET,
660 [MBEDTLS_SSL_EXT_ID_SESSION_TICKET] = MBEDTLS_TLS_EXT_SESSION_TICKET
661};
662
Gilles Peskine449bd832023-01-11 14:50:10 +0100663const char *mbedtls_ssl_get_extension_name(unsigned int extension_type)
Jerry Yud25cab02022-10-31 12:48:30 +0800664{
Gilles Peskine449bd832023-01-11 14:50:10 +0100665 return extension_name_table[
666 mbedtls_ssl_get_extension_id(extension_type)];
Jerry Yud25cab02022-10-31 12:48:30 +0800667}
668
Gilles Peskine449bd832023-01-11 14:50:10 +0100669static const char *ssl_tls13_get_hs_msg_name(int hs_msg_type)
Jerry Yud25cab02022-10-31 12:48:30 +0800670{
Gilles Peskine449bd832023-01-11 14:50:10 +0100671 switch (hs_msg_type) {
Jerry Yud25cab02022-10-31 12:48:30 +0800672 case MBEDTLS_SSL_HS_CLIENT_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +0100673 return "ClientHello";
Jerry Yud25cab02022-10-31 12:48:30 +0800674 case MBEDTLS_SSL_HS_SERVER_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +0100675 return "ServerHello";
Jerry Yud25cab02022-10-31 12:48:30 +0800676 case MBEDTLS_SSL_TLS1_3_HS_HELLO_RETRY_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +0100677 return "HelloRetryRequest";
Jerry Yud25cab02022-10-31 12:48:30 +0800678 case MBEDTLS_SSL_HS_NEW_SESSION_TICKET:
Gilles Peskine449bd832023-01-11 14:50:10 +0100679 return "NewSessionTicket";
Jerry Yud25cab02022-10-31 12:48:30 +0800680 case MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS:
Gilles Peskine449bd832023-01-11 14:50:10 +0100681 return "EncryptedExtensions";
Jerry Yud25cab02022-10-31 12:48:30 +0800682 case MBEDTLS_SSL_HS_CERTIFICATE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100683 return "Certificate";
Jerry Yud25cab02022-10-31 12:48:30 +0800684 case MBEDTLS_SSL_HS_CERTIFICATE_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +0100685 return "CertificateRequest";
Jerry Yud25cab02022-10-31 12:48:30 +0800686 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100687 return "Unknown";
Jerry Yud25cab02022-10-31 12:48:30 +0800688}
689
Gilles Peskine449bd832023-01-11 14:50:10 +0100690void mbedtls_ssl_print_extension(const mbedtls_ssl_context *ssl,
691 int level, const char *file, int line,
692 int hs_msg_type, unsigned int extension_type,
693 const char *extra_msg0, const char *extra_msg1)
Jerry Yud25cab02022-10-31 12:48:30 +0800694{
695 const char *extra_msg;
Gilles Peskine449bd832023-01-11 14:50:10 +0100696 if (extra_msg0 && extra_msg1) {
Jerry Yud25cab02022-10-31 12:48:30 +0800697 mbedtls_debug_print_msg(
698 ssl, level, file, line,
699 "%s: %s(%u) extension %s %s.",
Gilles Peskine449bd832023-01-11 14:50:10 +0100700 ssl_tls13_get_hs_msg_name(hs_msg_type),
701 mbedtls_ssl_get_extension_name(extension_type),
Jerry Yud25cab02022-10-31 12:48:30 +0800702 extension_type,
Gilles Peskine449bd832023-01-11 14:50:10 +0100703 extra_msg0, extra_msg1);
Jerry Yud25cab02022-10-31 12:48:30 +0800704 return;
705 }
706
707 extra_msg = extra_msg0 ? extra_msg0 : extra_msg1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100708 if (extra_msg) {
Jerry Yud25cab02022-10-31 12:48:30 +0800709 mbedtls_debug_print_msg(
710 ssl, level, file, line,
Gilles Peskine449bd832023-01-11 14:50:10 +0100711 "%s: %s(%u) extension %s.", ssl_tls13_get_hs_msg_name(hs_msg_type),
712 mbedtls_ssl_get_extension_name(extension_type), extension_type,
713 extra_msg);
Jerry Yud25cab02022-10-31 12:48:30 +0800714 return;
715 }
716
717 mbedtls_debug_print_msg(
718 ssl, level, file, line,
Gilles Peskine449bd832023-01-11 14:50:10 +0100719 "%s: %s(%u) extension.", ssl_tls13_get_hs_msg_name(hs_msg_type),
720 mbedtls_ssl_get_extension_name(extension_type), extension_type);
Jerry Yud25cab02022-10-31 12:48:30 +0800721}
722
Gilles Peskine449bd832023-01-11 14:50:10 +0100723void mbedtls_ssl_print_extensions(const mbedtls_ssl_context *ssl,
724 int level, const char *file, int line,
725 int hs_msg_type, uint32_t extensions_mask,
726 const char *extra)
Jerry Yud25cab02022-10-31 12:48:30 +0800727{
728
Gilles Peskine449bd832023-01-11 14:50:10 +0100729 for (unsigned i = 0;
730 i < sizeof(extension_name_table) / sizeof(extension_name_table[0]);
731 i++) {
Jerry Yu79aa7212022-11-08 21:30:21 +0800732 mbedtls_ssl_print_extension(
Jerry Yuea52ed92022-11-08 21:01:17 +0800733 ssl, level, file, line, hs_msg_type, extension_type_table[i],
Gilles Peskine449bd832023-01-11 14:50:10 +0100734 extensions_mask & (1 << i) ? "exists" : "does not exist", extra);
Jerry Yud25cab02022-10-31 12:48:30 +0800735 }
736}
737
Pengyu Lvee455c02023-01-12 14:37:24 +0800738#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS)
739#define ARRAY_LENGTH(a) (sizeof(a) / sizeof(*(a)))
740
741static const char *ticket_flag_name_table[] =
742{
743 [0] = "ALLOW_PSK_RESUMPTION",
744 [2] = "ALLOW_PSK_EPHEMERAL_RESUMPTION",
745 [3] = "ALLOW_EARLY_DATA",
746};
747
748void mbedtls_debug_print_ticket_flags(
749 const mbedtls_ssl_context *ssl, int level,
750 const char *file, int line,
751 mbedtls_ssl_tls13_ticket_flags flag)
752{
753 size_t i;
754
755 mbedtls_debug_print_msg(ssl, level, file, line,
756 "print ticket_flags (0x%02x)", flag);
757
758 for (i = 0; i < ARRAY_LENGTH(ticket_flag_name_table); i++) {
759 if ((flag & (1 << i)) & MBEDTLS_SSL_TLS1_3_TICKET_FLAGS_MASK) {
760 mbedtls_debug_print_msg(ssl, level, file, line, "- %s is set.",
761 ticket_flag_name_table[i]);
762 }
763 }
764}
765#endif /* MBEDTLS_SSL_PROTO_TLS1_3 && MBEDTLS_SSL_SESSION_TICKETS */
766
Jerry Yud25cab02022-10-31 12:48:30 +0800767#endif /* MBEDTLS_DEBUG_C */
768
Gilles Peskine449bd832023-01-11 14:50:10 +0100769void mbedtls_ssl_optimize_checksum(mbedtls_ssl_context *ssl,
770 const mbedtls_ssl_ciphersuite_t *ciphersuite_info)
Jerry Yuc73c6182022-02-08 20:29:25 +0800771{
772 ((void) ciphersuite_info);
773
Andrzej Kurek25f27152022-08-17 16:09:31 -0400774#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +0100775 if (ciphersuite_info->mac == MBEDTLS_MD_SHA384) {
Jerry Yuc73c6182022-02-08 20:29:25 +0800776 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
Gilles Peskine449bd832023-01-11 14:50:10 +0100777 } else
Jerry Yuc73c6182022-02-08 20:29:25 +0800778#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400779#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +0100780 if (ciphersuite_info->mac != MBEDTLS_MD_SHA384) {
Jerry Yuc73c6182022-02-08 20:29:25 +0800781 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Gilles Peskine449bd832023-01-11 14:50:10 +0100782 } else
Jerry Yuc73c6182022-02-08 20:29:25 +0800783#endif
784 {
Gilles Peskine449bd832023-01-11 14:50:10 +0100785 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
Jerry Yuc73c6182022-02-08 20:29:25 +0800786 return;
787 }
788}
789
Gilles Peskine449bd832023-01-11 14:50:10 +0100790void mbedtls_ssl_add_hs_hdr_to_checksum(mbedtls_ssl_context *ssl,
791 unsigned hs_type,
792 size_t total_hs_len)
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100793{
794 unsigned char hs_hdr[4];
795
796 /* Build HS header for checksum update. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100797 hs_hdr[0] = MBEDTLS_BYTE_0(hs_type);
798 hs_hdr[1] = MBEDTLS_BYTE_2(total_hs_len);
799 hs_hdr[2] = MBEDTLS_BYTE_1(total_hs_len);
800 hs_hdr[3] = MBEDTLS_BYTE_0(total_hs_len);
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100801
Gilles Peskine449bd832023-01-11 14:50:10 +0100802 ssl->handshake->update_checksum(ssl, hs_hdr, sizeof(hs_hdr));
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100803}
804
Gilles Peskine449bd832023-01-11 14:50:10 +0100805void mbedtls_ssl_add_hs_msg_to_checksum(mbedtls_ssl_context *ssl,
806 unsigned hs_type,
807 unsigned char const *msg,
808 size_t msg_len)
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100809{
Gilles Peskine449bd832023-01-11 14:50:10 +0100810 mbedtls_ssl_add_hs_hdr_to_checksum(ssl, hs_type, msg_len);
811 ssl->handshake->update_checksum(ssl, msg, msg_len);
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100812}
813
Gilles Peskine449bd832023-01-11 14:50:10 +0100814void mbedtls_ssl_reset_checksum(mbedtls_ssl_context *ssl)
Jerry Yuc73c6182022-02-08 20:29:25 +0800815{
816 ((void) ssl);
Andrzej Kurek25f27152022-08-17 16:09:31 -0400817#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800818#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100819 psa_hash_abort(&ssl->handshake->fin_sha256_psa);
820 psa_hash_setup(&ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256);
Jerry Yuc73c6182022-02-08 20:29:25 +0800821#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100822 mbedtls_sha256_starts(&ssl->handshake->fin_sha256, 0);
Jerry Yuc73c6182022-02-08 20:29:25 +0800823#endif
824#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400825#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800826#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100827 psa_hash_abort(&ssl->handshake->fin_sha384_psa);
828 psa_hash_setup(&ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384);
Jerry Yuc73c6182022-02-08 20:29:25 +0800829#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100830 mbedtls_sha512_starts(&ssl->handshake->fin_sha384, 1);
Jerry Yuc73c6182022-02-08 20:29:25 +0800831#endif
832#endif
833}
834
Gilles Peskine449bd832023-01-11 14:50:10 +0100835static void ssl_update_checksum_start(mbedtls_ssl_context *ssl,
836 const unsigned char *buf, size_t len)
Jerry Yuc73c6182022-02-08 20:29:25 +0800837{
Andrzej Kurek25f27152022-08-17 16:09:31 -0400838#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800839#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100840 psa_hash_update(&ssl->handshake->fin_sha256_psa, buf, len);
Jerry Yuc73c6182022-02-08 20:29:25 +0800841#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100842 mbedtls_sha256_update(&ssl->handshake->fin_sha256, buf, len);
Jerry Yuc73c6182022-02-08 20:29:25 +0800843#endif
844#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400845#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800846#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100847 psa_hash_update(&ssl->handshake->fin_sha384_psa, buf, len);
Jerry Yuc73c6182022-02-08 20:29:25 +0800848#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100849 mbedtls_sha512_update(&ssl->handshake->fin_sha384, buf, len);
Jerry Yuc73c6182022-02-08 20:29:25 +0800850#endif
851#endif
Andrzej Kurekeabeb302022-10-17 07:52:51 -0400852#if !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
853 !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
854 (void) ssl;
855 (void) buf;
856 (void) len;
857#endif
Jerry Yuc73c6182022-02-08 20:29:25 +0800858}
859
Andrzej Kurek25f27152022-08-17 16:09:31 -0400860#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +0100861static void ssl_update_checksum_sha256(mbedtls_ssl_context *ssl,
862 const unsigned char *buf, size_t len)
Jerry Yuc73c6182022-02-08 20:29:25 +0800863{
864#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100865 psa_hash_update(&ssl->handshake->fin_sha256_psa, buf, len);
Jerry Yuc73c6182022-02-08 20:29:25 +0800866#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100867 mbedtls_sha256_update(&ssl->handshake->fin_sha256, buf, len);
Jerry Yuc73c6182022-02-08 20:29:25 +0800868#endif
869}
870#endif
871
Andrzej Kurek25f27152022-08-17 16:09:31 -0400872#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +0100873static void ssl_update_checksum_sha384(mbedtls_ssl_context *ssl,
874 const unsigned char *buf, size_t len)
Jerry Yuc73c6182022-02-08 20:29:25 +0800875{
876#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100877 psa_hash_update(&ssl->handshake->fin_sha384_psa, buf, len);
Jerry Yuc73c6182022-02-08 20:29:25 +0800878#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100879 mbedtls_sha512_update(&ssl->handshake->fin_sha384, buf, len);
Jerry Yuc73c6182022-02-08 20:29:25 +0800880#endif
881}
882#endif
883
Gilles Peskine449bd832023-01-11 14:50:10 +0100884static void ssl_handshake_params_init(mbedtls_ssl_handshake_params *handshake)
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200885{
Gilles Peskine449bd832023-01-11 14:50:10 +0100886 memset(handshake, 0, sizeof(mbedtls_ssl_handshake_params));
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200887
Andrzej Kurek25f27152022-08-17 16:09:31 -0400888#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -0500889#if defined(MBEDTLS_USE_PSA_CRYPTO)
890 handshake->fin_sha256_psa = psa_hash_operation_init();
Gilles Peskine449bd832023-01-11 14:50:10 +0100891 psa_hash_setup(&handshake->fin_sha256_psa, PSA_ALG_SHA_256);
Andrzej Kurekeb342242019-01-29 09:14:33 -0500892#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100893 mbedtls_sha256_init(&handshake->fin_sha256);
894 mbedtls_sha256_starts(&handshake->fin_sha256, 0);
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200895#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -0500896#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400897#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -0500898#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -0500899 handshake->fin_sha384_psa = psa_hash_operation_init();
Gilles Peskine449bd832023-01-11 14:50:10 +0100900 psa_hash_setup(&handshake->fin_sha384_psa, PSA_ALG_SHA_384);
Andrzej Kurekeb342242019-01-29 09:14:33 -0500901#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100902 mbedtls_sha512_init(&handshake->fin_sha384);
903 mbedtls_sha512_starts(&handshake->fin_sha384, 1);
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200904#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -0500905#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200906
907 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +0100908
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200909#if defined(MBEDTLS_DHM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100910 mbedtls_dhm_init(&handshake->dhm_ctx);
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200911#endif
Neil Armstrongf3f46412022-04-12 14:43:39 +0200912#if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100913 mbedtls_ecdh_init(&handshake->ecdh_ctx);
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200914#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +0200915#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Neil Armstrongca7d5062022-05-31 14:43:23 +0200916#if defined(MBEDTLS_USE_PSA_CRYPTO)
917 handshake->psa_pake_ctx = psa_pake_operation_init();
918 handshake->psa_pake_password = MBEDTLS_SVC_KEY_ID_INIT;
919#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100920 mbedtls_ecjpake_init(&handshake->ecjpake_ctx);
Neil Armstrongca7d5062022-05-31 14:43:23 +0200921#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +0200922#if defined(MBEDTLS_SSL_CLI_C)
923 handshake->ecjpake_cache = NULL;
924 handshake->ecjpake_cache_len = 0;
925#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +0200926#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +0200927
Gilles Peskineeccd8882020-03-10 12:19:08 +0100928#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100929 mbedtls_x509_crt_restart_init(&handshake->ecrs_ctx);
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +0200930#endif
931
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +0200932#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
933 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
934#endif
Hanno Becker75173122019-02-06 16:18:31 +0000935
936#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
937 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100938 mbedtls_pk_init(&handshake->peer_pubkey);
Hanno Becker75173122019-02-06 16:18:31 +0000939#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200940}
941
Gilles Peskine449bd832023-01-11 14:50:10 +0100942void mbedtls_ssl_transform_init(mbedtls_ssl_transform *transform)
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200943{
Gilles Peskine449bd832023-01-11 14:50:10 +0100944 memset(transform, 0, sizeof(mbedtls_ssl_transform));
Paul Bakker84bbeb52014-07-01 14:53:22 +0200945
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +0100946#if defined(MBEDTLS_USE_PSA_CRYPTO)
947 transform->psa_key_enc = MBEDTLS_SVC_KEY_ID_INIT;
948 transform->psa_key_dec = MBEDTLS_SVC_KEY_ID_INIT;
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +0100949#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100950 mbedtls_cipher_init(&transform->cipher_ctx_enc);
951 mbedtls_cipher_init(&transform->cipher_ctx_dec);
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +0100952#endif
953
Hanno Beckerfd86ca82020-11-30 08:54:23 +0000954#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong39b8e7d2022-02-23 09:24:45 +0100955#if defined(MBEDTLS_USE_PSA_CRYPTO)
956 transform->psa_mac_enc = MBEDTLS_SVC_KEY_ID_INIT;
957 transform->psa_mac_dec = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrongcf8841a2022-02-24 11:17:45 +0100958#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100959 mbedtls_md_init(&transform->md_ctx_enc);
960 mbedtls_md_init(&transform->md_ctx_dec);
Hanno Beckerd56ed242018-01-03 15:32:51 +0000961#endif
Neil Armstrongcf8841a2022-02-24 11:17:45 +0100962#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200963}
964
Gilles Peskine449bd832023-01-11 14:50:10 +0100965void mbedtls_ssl_session_init(mbedtls_ssl_session *session)
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200966{
Gilles Peskine449bd832023-01-11 14:50:10 +0100967 memset(session, 0, sizeof(mbedtls_ssl_session));
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200968}
969
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200970MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100971static int ssl_handshake_init(mbedtls_ssl_context *ssl)
Paul Bakker48916f92012-09-16 19:57:18 +0000972{
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200973 /* Clear old handshake information if present */
Jerry Yu2e199812022-12-01 18:57:19 +0800974#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +0100975 if (ssl->transform_negotiate) {
976 mbedtls_ssl_transform_free(ssl->transform_negotiate);
977 }
Jerry Yu2e199812022-12-01 18:57:19 +0800978#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100979 if (ssl->session_negotiate) {
980 mbedtls_ssl_session_free(ssl->session_negotiate);
981 }
982 if (ssl->handshake) {
983 mbedtls_ssl_handshake_free(ssl);
984 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200985
Jerry Yu2e199812022-12-01 18:57:19 +0800986#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200987 /*
988 * Either the pointers are now NULL or cleared properly and can be freed.
989 * Now allocate missing structures.
990 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100991 if (ssl->transform_negotiate == NULL) {
992 ssl->transform_negotiate = mbedtls_calloc(1, sizeof(mbedtls_ssl_transform));
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200993 }
Jerry Yu2e199812022-12-01 18:57:19 +0800994#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker48916f92012-09-16 19:57:18 +0000995
Gilles Peskine449bd832023-01-11 14:50:10 +0100996 if (ssl->session_negotiate == NULL) {
997 ssl->session_negotiate = mbedtls_calloc(1, sizeof(mbedtls_ssl_session));
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200998 }
Paul Bakker48916f92012-09-16 19:57:18 +0000999
Gilles Peskine449bd832023-01-11 14:50:10 +01001000 if (ssl->handshake == NULL) {
1001 ssl->handshake = mbedtls_calloc(1, sizeof(mbedtls_ssl_handshake_params));
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001002 }
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05001003#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1004 /* If the buffers are too small - reallocate */
Andrzej Kurek8ea68722020-04-03 06:40:47 -04001005
Gilles Peskine449bd832023-01-11 14:50:10 +01001006 handle_buffer_resizing(ssl, 0, MBEDTLS_SSL_IN_BUFFER_LEN,
1007 MBEDTLS_SSL_OUT_BUFFER_LEN);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05001008#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001009
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001010 /* All pointers should exist and can be directly freed without issue */
Gilles Peskine449bd832023-01-11 14:50:10 +01001011 if (ssl->handshake == NULL ||
Jerry Yu2e199812022-12-01 18:57:19 +08001012#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker48916f92012-09-16 19:57:18 +00001013 ssl->transform_negotiate == NULL ||
Jerry Yu2e199812022-12-01 18:57:19 +08001014#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001015 ssl->session_negotiate == NULL) {
1016 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc() of ssl sub-contexts failed"));
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001017
Gilles Peskine449bd832023-01-11 14:50:10 +01001018 mbedtls_free(ssl->handshake);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001019 ssl->handshake = NULL;
Jerry Yu2e199812022-12-01 18:57:19 +08001020
1021#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01001022 mbedtls_free(ssl->transform_negotiate);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001023 ssl->transform_negotiate = NULL;
Jerry Yu2e199812022-12-01 18:57:19 +08001024#endif
1025
Gilles Peskine449bd832023-01-11 14:50:10 +01001026 mbedtls_free(ssl->session_negotiate);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001027 ssl->session_negotiate = NULL;
1028
Gilles Peskine449bd832023-01-11 14:50:10 +01001029 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Paul Bakker48916f92012-09-16 19:57:18 +00001030 }
1031
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001032 /* Initialize structures */
Gilles Peskine449bd832023-01-11 14:50:10 +01001033 mbedtls_ssl_session_init(ssl->session_negotiate);
1034 ssl_handshake_params_init(ssl->handshake);
Paul Bakker968afaa2014-07-09 11:09:24 +02001035
Jerry Yu2e199812022-12-01 18:57:19 +08001036#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01001037 mbedtls_ssl_transform_init(ssl->transform_negotiate);
Jerry Yu2e199812022-12-01 18:57:19 +08001038#endif
1039
Jerry Yud0766ec2022-09-22 10:46:57 +08001040#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
1041 defined(MBEDTLS_SSL_SRV_C) && \
1042 defined(MBEDTLS_SSL_SESSION_TICKETS)
1043 ssl->handshake->new_session_tickets_count =
Gilles Peskine449bd832023-01-11 14:50:10 +01001044 ssl->conf->new_session_tickets_count;
Jerry Yud0766ec2022-09-22 10:46:57 +08001045#endif
1046
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001047#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001048 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001049 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001050
Gilles Peskine449bd832023-01-11 14:50:10 +01001051 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001052 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Gilles Peskine449bd832023-01-11 14:50:10 +01001053 } else {
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001054 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Gilles Peskine449bd832023-01-11 14:50:10 +01001055 }
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001056
Gilles Peskine449bd832023-01-11 14:50:10 +01001057 mbedtls_ssl_set_timer(ssl, 0);
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001058 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001059#endif
1060
Brett Warrene0edc842021-08-17 09:53:13 +01001061/*
1062 * curve_list is translated to IANA TLS group identifiers here because
1063 * mbedtls_ssl_conf_curves returns void and so can't return
1064 * any error codes.
1065 */
1066#if defined(MBEDTLS_ECP_C)
1067#if !defined(MBEDTLS_DEPRECATED_REMOVED)
1068 /* Heap allocate and translate curve_list from internal to IANA group ids */
Gilles Peskine449bd832023-01-11 14:50:10 +01001069 if (ssl->conf->curve_list != NULL) {
Brett Warrene0edc842021-08-17 09:53:13 +01001070 size_t length;
1071 const mbedtls_ecp_group_id *curve_list = ssl->conf->curve_list;
1072
Gilles Peskine449bd832023-01-11 14:50:10 +01001073 for (length = 0; (curve_list[length] != MBEDTLS_ECP_DP_NONE) &&
1074 (length < MBEDTLS_ECP_DP_MAX); length++) {
1075 }
Brett Warrene0edc842021-08-17 09:53:13 +01001076
1077 /* Leave room for zero termination */
Gilles Peskine449bd832023-01-11 14:50:10 +01001078 uint16_t *group_list = mbedtls_calloc(length + 1, sizeof(uint16_t));
1079 if (group_list == NULL) {
1080 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
1081 }
Brett Warrene0edc842021-08-17 09:53:13 +01001082
Gilles Peskine449bd832023-01-11 14:50:10 +01001083 for (size_t i = 0; i < length; i++) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01001084 uint16_t tls_id = mbedtls_ssl_get_tls_id_from_ecp_group_id(
Gilles Peskine449bd832023-01-11 14:50:10 +01001085 curve_list[i]);
1086 if (tls_id == 0) {
1087 mbedtls_free(group_list);
1088 return MBEDTLS_ERR_SSL_BAD_CONFIG;
Brett Warrene0edc842021-08-17 09:53:13 +01001089 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01001090 group_list[i] = tls_id;
Brett Warrene0edc842021-08-17 09:53:13 +01001091 }
1092
1093 group_list[length] = 0;
1094
1095 ssl->handshake->group_list = group_list;
1096 ssl->handshake->group_list_heap_allocated = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001097 } else {
Brett Warrene0edc842021-08-17 09:53:13 +01001098 ssl->handshake->group_list = ssl->conf->group_list;
1099 ssl->handshake->group_list_heap_allocated = 0;
1100 }
1101#endif /* MBEDTLS_DEPRECATED_REMOVED */
1102#endif /* MBEDTLS_ECP_C */
1103
Ronald Crone68ab4f2022-10-05 12:46:29 +02001104#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +08001105#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Jerry Yua69269a2022-01-17 21:06:01 +08001106#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yu713013f2022-01-17 18:16:35 +08001107 /* Heap allocate and translate sig_hashes from internal hash identifiers to
1108 signature algorithms IANA identifiers. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001109 if (mbedtls_ssl_conf_is_tls12_only(ssl->conf) &&
1110 ssl->conf->sig_hashes != NULL) {
Jerry Yuf017ee42022-01-12 15:49:48 +08001111 const int *md;
1112 const int *sig_hashes = ssl->conf->sig_hashes;
Jerry Yub476a442022-01-21 18:14:45 +08001113 size_t sig_algs_len = 0;
Jerry Yuf017ee42022-01-12 15:49:48 +08001114 uint16_t *p;
1115
Jerry Yub476a442022-01-21 18:14:45 +08001116#if defined(static_assert)
Gilles Peskine449bd832023-01-11 14:50:10 +01001117 static_assert(MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN
1118 <= (SIZE_MAX - (2 * sizeof(uint16_t))),
1119 "MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN too big");
Jerry Yua68dca22022-01-20 16:28:27 +08001120#endif
1121
Gilles Peskine449bd832023-01-11 14:50:10 +01001122 for (md = sig_hashes; *md != MBEDTLS_MD_NONE; md++) {
1123 if (mbedtls_ssl_hash_from_md_alg(*md) == MBEDTLS_SSL_HASH_NONE) {
Jerry Yuf017ee42022-01-12 15:49:48 +08001124 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001125 }
Jerry Yub476a442022-01-21 18:14:45 +08001126#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001127 sig_algs_len += sizeof(uint16_t);
Jerry Yub476a442022-01-21 18:14:45 +08001128#endif
Jerry Yua68dca22022-01-20 16:28:27 +08001129
Jerry Yub476a442022-01-21 18:14:45 +08001130#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001131 sig_algs_len += sizeof(uint16_t);
Jerry Yub476a442022-01-21 18:14:45 +08001132#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001133 if (sig_algs_len > MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN) {
1134 return MBEDTLS_ERR_SSL_BAD_CONFIG;
1135 }
Jerry Yuf017ee42022-01-12 15:49:48 +08001136 }
1137
Gilles Peskine449bd832023-01-11 14:50:10 +01001138 if (sig_algs_len < MBEDTLS_SSL_MIN_SIG_ALG_LIST_LEN) {
1139 return MBEDTLS_ERR_SSL_BAD_CONFIG;
1140 }
Jerry Yuf017ee42022-01-12 15:49:48 +08001141
Gilles Peskine449bd832023-01-11 14:50:10 +01001142 ssl->handshake->sig_algs = mbedtls_calloc(1, sig_algs_len +
1143 sizeof(uint16_t));
1144 if (ssl->handshake->sig_algs == NULL) {
1145 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
1146 }
Jerry Yuf017ee42022-01-12 15:49:48 +08001147
Gilles Peskine449bd832023-01-11 14:50:10 +01001148 p = (uint16_t *) ssl->handshake->sig_algs;
1149 for (md = sig_hashes; *md != MBEDTLS_MD_NONE; md++) {
1150 unsigned char hash = mbedtls_ssl_hash_from_md_alg(*md);
1151 if (hash == MBEDTLS_SSL_HASH_NONE) {
Jerry Yuf017ee42022-01-12 15:49:48 +08001152 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001153 }
Jerry Yu6106fdc2022-01-12 16:36:14 +08001154#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001155 *p = ((hash << 8) | MBEDTLS_SSL_SIG_ECDSA);
Jerry Yuf017ee42022-01-12 15:49:48 +08001156 p++;
Jerry Yu6106fdc2022-01-12 16:36:14 +08001157#endif
1158#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001159 *p = ((hash << 8) | MBEDTLS_SSL_SIG_RSA);
Jerry Yuf017ee42022-01-12 15:49:48 +08001160 p++;
Jerry Yu6106fdc2022-01-12 16:36:14 +08001161#endif
Jerry Yuf017ee42022-01-12 15:49:48 +08001162 }
Gabor Mezei15b95a62022-05-09 16:37:58 +02001163 *p = MBEDTLS_TLS_SIG_NONE;
Jerry Yuf017ee42022-01-12 15:49:48 +08001164 ssl->handshake->sig_algs_heap_allocated = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001165 } else
Jerry Yua69269a2022-01-17 21:06:01 +08001166#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Jerry Yuf017ee42022-01-12 15:49:48 +08001167 {
Jerry Yuf017ee42022-01-12 15:49:48 +08001168 ssl->handshake->sig_algs_heap_allocated = 0;
1169 }
Jerry Yucc539102022-06-27 16:27:35 +08001170#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Ronald Crone68ab4f2022-10-05 12:46:29 +02001171#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Gilles Peskine449bd832023-01-11 14:50:10 +01001172 return 0;
Paul Bakker48916f92012-09-16 19:57:18 +00001173}
1174
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001175#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001176/* Dummy cookie callbacks for defaults */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001177MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001178static int ssl_cookie_write_dummy(void *ctx,
1179 unsigned char **p, unsigned char *end,
1180 const unsigned char *cli_id, size_t cli_id_len)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001181{
1182 ((void) ctx);
1183 ((void) p);
1184 ((void) end);
1185 ((void) cli_id);
1186 ((void) cli_id_len);
1187
Gilles Peskine449bd832023-01-11 14:50:10 +01001188 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001189}
1190
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001191MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001192static int ssl_cookie_check_dummy(void *ctx,
1193 const unsigned char *cookie, size_t cookie_len,
1194 const unsigned char *cli_id, size_t cli_id_len)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001195{
1196 ((void) ctx);
1197 ((void) cookie);
1198 ((void) cookie_len);
1199 ((void) cli_id);
1200 ((void) cli_id_len);
1201
Gilles Peskine449bd832023-01-11 14:50:10 +01001202 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001203}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001204#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001205
Paul Bakker5121ce52009-01-03 21:22:43 +00001206/*
1207 * Initialize an SSL context
1208 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001209void mbedtls_ssl_init(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001210{
Gilles Peskine449bd832023-01-11 14:50:10 +01001211 memset(ssl, 0, sizeof(mbedtls_ssl_context));
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001212}
1213
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001214MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001215static int ssl_conf_version_check(const mbedtls_ssl_context *ssl)
Jerry Yu60835a82021-08-04 10:13:52 +08001216{
Ronald Cron086ee0b2022-03-15 15:18:51 +01001217 const mbedtls_ssl_config *conf = ssl->conf;
1218
Ronald Cron6f135e12021-12-08 16:57:54 +01001219#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001220 if (mbedtls_ssl_conf_is_tls13_only(conf)) {
1221 if (conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
1222 MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS 1.3 is not yet supported."));
1223 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQianed582dd2022-04-13 08:21:05 +00001224 }
1225
Gilles Peskine449bd832023-01-11 14:50:10 +01001226 MBEDTLS_SSL_DEBUG_MSG(4, ("The SSL configuration is tls13 only."));
1227 return 0;
Jerry Yu60835a82021-08-04 10:13:52 +08001228 }
1229#endif
1230
1231#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01001232 if (mbedtls_ssl_conf_is_tls12_only(conf)) {
1233 MBEDTLS_SSL_DEBUG_MSG(4, ("The SSL configuration is tls12 only."));
1234 return 0;
Jerry Yu60835a82021-08-04 10:13:52 +08001235 }
1236#endif
1237
Ronald Cron6f135e12021-12-08 16:57:54 +01001238#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001239 if (mbedtls_ssl_conf_is_hybrid_tls12_tls13(conf)) {
1240 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
1241 MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS not yet supported in Hybrid TLS 1.3 + TLS 1.2"));
1242 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQianed582dd2022-04-13 08:21:05 +00001243 }
1244
Gilles Peskine449bd832023-01-11 14:50:10 +01001245 if (conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
1246 MBEDTLS_SSL_DEBUG_MSG(1, ("TLS 1.3 server is not supported yet."));
1247 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQian8f9dfe42022-04-15 02:52:39 +00001248 }
1249
1250
Gilles Peskine449bd832023-01-11 14:50:10 +01001251 MBEDTLS_SSL_DEBUG_MSG(4, ("The SSL configuration is TLS 1.3 or TLS 1.2."));
1252 return 0;
Jerry Yu60835a82021-08-04 10:13:52 +08001253 }
1254#endif
1255
Gilles Peskine449bd832023-01-11 14:50:10 +01001256 MBEDTLS_SSL_DEBUG_MSG(1, ("The SSL configuration is invalid."));
1257 return MBEDTLS_ERR_SSL_BAD_CONFIG;
Jerry Yu60835a82021-08-04 10:13:52 +08001258}
1259
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001260MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu60835a82021-08-04 10:13:52 +08001261static int ssl_conf_check(const mbedtls_ssl_context *ssl)
1262{
1263 int ret;
Gilles Peskine449bd832023-01-11 14:50:10 +01001264 ret = ssl_conf_version_check(ssl);
1265 if (ret != 0) {
1266 return ret;
1267 }
Jerry Yu60835a82021-08-04 10:13:52 +08001268
Jerry Yudef7ae42022-10-30 14:13:19 +08001269#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1270 /* RFC 8446 section 4.4.3
1271 *
1272 * If the verification fails, the receiver MUST terminate the handshake with
1273 * a "decrypt_error" alert.
1274 *
1275 * If the client is configured as TLS 1.3 only with optional verify, return
1276 * bad config.
1277 *
1278 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001279 if (mbedtls_ssl_conf_tls13_ephemeral_enabled(
1280 (mbedtls_ssl_context *) ssl) &&
Jerry Yudef7ae42022-10-30 14:13:19 +08001281 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
1282 ssl->conf->max_tls_version == MBEDTLS_SSL_VERSION_TLS1_3 &&
1283 ssl->conf->min_tls_version == MBEDTLS_SSL_VERSION_TLS1_3 &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001284 ssl->conf->authmode == MBEDTLS_SSL_VERIFY_OPTIONAL) {
Jerry Yudef7ae42022-10-30 14:13:19 +08001285 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +01001286 1, ("Optional verify auth mode "
1287 "is not available for TLS 1.3 client"));
1288 return MBEDTLS_ERR_SSL_BAD_CONFIG;
Jerry Yudef7ae42022-10-30 14:13:19 +08001289 }
1290#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
1291
Jerry Yu60835a82021-08-04 10:13:52 +08001292 /* Space for further checks */
1293
Gilles Peskine449bd832023-01-11 14:50:10 +01001294 return 0;
Jerry Yu60835a82021-08-04 10:13:52 +08001295}
1296
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001297/*
1298 * Setup an SSL context
1299 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01001300
Gilles Peskine449bd832023-01-11 14:50:10 +01001301int mbedtls_ssl_setup(mbedtls_ssl_context *ssl,
1302 const mbedtls_ssl_config *conf)
Paul Bakker5121ce52009-01-03 21:22:43 +00001303{
Janos Follath865b3eb2019-12-16 11:46:15 +00001304 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Darryl Greenb33cc762019-11-28 14:29:44 +00001305 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1306 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00001307
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001308 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00001309
Gilles Peskine449bd832023-01-11 14:50:10 +01001310 if ((ret = ssl_conf_check(ssl)) != 0) {
1311 return ret;
1312 }
Jerry Yu60835a82021-08-04 10:13:52 +08001313
Paul Bakker62f2dee2012-09-28 07:31:51 +00001314 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001315 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00001316 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02001317
1318 /* Set to NULL in case of an error condition */
1319 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02001320
Darryl Greenb33cc762019-11-28 14:29:44 +00001321#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1322 ssl->in_buf_len = in_buf_len;
1323#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001324 ssl->in_buf = mbedtls_calloc(1, in_buf_len);
1325 if (ssl->in_buf == NULL) {
1326 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", in_buf_len));
k-stachowiak9f7798e2018-07-31 16:52:32 +02001327 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02001328 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10001329 }
1330
Darryl Greenb33cc762019-11-28 14:29:44 +00001331#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1332 ssl->out_buf_len = out_buf_len;
1333#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001334 ssl->out_buf = mbedtls_calloc(1, out_buf_len);
1335 if (ssl->out_buf == NULL) {
1336 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", out_buf_len));
k-stachowiak9f7798e2018-07-31 16:52:32 +02001337 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02001338 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00001339 }
1340
Gilles Peskine449bd832023-01-11 14:50:10 +01001341 mbedtls_ssl_reset_in_out_pointers(ssl);
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02001342
Johan Pascalb62bb512015-12-03 21:56:45 +01001343#if defined(MBEDTLS_SSL_DTLS_SRTP)
Gilles Peskine449bd832023-01-11 14:50:10 +01001344 memset(&ssl->dtls_srtp_info, 0, sizeof(ssl->dtls_srtp_info));
Johan Pascalb62bb512015-12-03 21:56:45 +01001345#endif
1346
Gilles Peskine449bd832023-01-11 14:50:10 +01001347 if ((ret = ssl_handshake_init(ssl)) != 0) {
k-stachowiaka47911c2018-07-04 17:41:58 +02001348 goto error;
Gilles Peskine449bd832023-01-11 14:50:10 +01001349 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001350
Gilles Peskine449bd832023-01-11 14:50:10 +01001351 return 0;
k-stachowiaka47911c2018-07-04 17:41:58 +02001352
1353error:
Gilles Peskine449bd832023-01-11 14:50:10 +01001354 mbedtls_free(ssl->in_buf);
1355 mbedtls_free(ssl->out_buf);
k-stachowiaka47911c2018-07-04 17:41:58 +02001356
1357 ssl->conf = NULL;
1358
Darryl Greenb33cc762019-11-28 14:29:44 +00001359#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1360 ssl->in_buf_len = 0;
1361 ssl->out_buf_len = 0;
1362#endif
k-stachowiaka47911c2018-07-04 17:41:58 +02001363 ssl->in_buf = NULL;
1364 ssl->out_buf = NULL;
1365
1366 ssl->in_hdr = NULL;
1367 ssl->in_ctr = NULL;
1368 ssl->in_len = NULL;
1369 ssl->in_iv = NULL;
1370 ssl->in_msg = NULL;
1371
1372 ssl->out_hdr = NULL;
1373 ssl->out_ctr = NULL;
1374 ssl->out_len = NULL;
1375 ssl->out_iv = NULL;
1376 ssl->out_msg = NULL;
1377
Gilles Peskine449bd832023-01-11 14:50:10 +01001378 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001379}
1380
1381/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00001382 * Reset an initialized and used SSL context for re-use while retaining
1383 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001384 *
1385 * If partial is non-zero, keep data in the input buffer and client ID.
1386 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00001387 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001388void mbedtls_ssl_session_reset_msg_layer(mbedtls_ssl_context *ssl,
1389 int partial)
Paul Bakker7eb013f2011-10-06 12:37:39 +00001390{
Darryl Greenb33cc762019-11-28 14:29:44 +00001391#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1392 size_t in_buf_len = ssl->in_buf_len;
1393 size_t out_buf_len = ssl->out_buf_len;
1394#else
1395 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1396 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
1397#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001398
Hanno Beckerb0302c42021-08-03 09:39:42 +01001399#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || !defined(MBEDTLS_SSL_SRV_C)
1400 partial = 0;
Hanno Becker7e772132018-08-10 12:38:21 +01001401#endif
1402
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001403 /* Cancel any possibly running timer */
Gilles Peskine449bd832023-01-11 14:50:10 +01001404 mbedtls_ssl_set_timer(ssl, 0);
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001405
Gilles Peskine449bd832023-01-11 14:50:10 +01001406 mbedtls_ssl_reset_in_out_pointers(ssl);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001407
1408 /* Reset incoming message parsing */
1409 ssl->in_offt = NULL;
1410 ssl->nb_zero = 0;
1411 ssl->in_msgtype = 0;
1412 ssl->in_msglen = 0;
1413 ssl->in_hslen = 0;
1414 ssl->keep_current_message = 0;
1415 ssl->transform_in = NULL;
1416
1417#if defined(MBEDTLS_SSL_PROTO_DTLS)
1418 ssl->next_record_offset = 0;
1419 ssl->in_epoch = 0;
1420#endif
1421
1422 /* Keep current datagram if partial == 1 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001423 if (partial == 0) {
Hanno Beckerb0302c42021-08-03 09:39:42 +01001424 ssl->in_left = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001425 memset(ssl->in_buf, 0, in_buf_len);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001426 }
1427
Ronald Cronad8c17b2022-06-10 17:18:09 +02001428 ssl->send_alert = 0;
1429
Hanno Beckerb0302c42021-08-03 09:39:42 +01001430 /* Reset outgoing message writing */
1431 ssl->out_msgtype = 0;
1432 ssl->out_msglen = 0;
1433 ssl->out_left = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001434 memset(ssl->out_buf, 0, out_buf_len);
1435 memset(ssl->cur_out_ctr, 0, sizeof(ssl->cur_out_ctr));
Hanno Beckerb0302c42021-08-03 09:39:42 +01001436 ssl->transform_out = NULL;
1437
1438#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine449bd832023-01-11 14:50:10 +01001439 mbedtls_ssl_dtls_replay_reset(ssl);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001440#endif
1441
XiaokangQian2b01dc32022-01-21 02:53:13 +00001442#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01001443 if (ssl->transform) {
1444 mbedtls_ssl_transform_free(ssl->transform);
1445 mbedtls_free(ssl->transform);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001446 ssl->transform = NULL;
1447 }
XiaokangQian2b01dc32022-01-21 02:53:13 +00001448#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1449
XiaokangQian2b01dc32022-01-21 02:53:13 +00001450#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001451 mbedtls_ssl_transform_free(ssl->transform_application);
1452 mbedtls_free(ssl->transform_application);
XiaokangQian2b01dc32022-01-21 02:53:13 +00001453 ssl->transform_application = NULL;
1454
Gilles Peskine449bd832023-01-11 14:50:10 +01001455 if (ssl->handshake != NULL) {
Jerry Yu3d9b5902022-11-04 14:07:25 +08001456#if defined(MBEDTLS_SSL_EARLY_DATA)
Gilles Peskine449bd832023-01-11 14:50:10 +01001457 mbedtls_ssl_transform_free(ssl->handshake->transform_earlydata);
1458 mbedtls_free(ssl->handshake->transform_earlydata);
XiaokangQian2b01dc32022-01-21 02:53:13 +00001459 ssl->handshake->transform_earlydata = NULL;
Jerry Yu3d9b5902022-11-04 14:07:25 +08001460#endif
XiaokangQian2b01dc32022-01-21 02:53:13 +00001461
Gilles Peskine449bd832023-01-11 14:50:10 +01001462 mbedtls_ssl_transform_free(ssl->handshake->transform_handshake);
1463 mbedtls_free(ssl->handshake->transform_handshake);
XiaokangQian2b01dc32022-01-21 02:53:13 +00001464 ssl->handshake->transform_handshake = NULL;
1465 }
1466
XiaokangQian2b01dc32022-01-21 02:53:13 +00001467#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerb0302c42021-08-03 09:39:42 +01001468}
1469
Gilles Peskine449bd832023-01-11 14:50:10 +01001470int mbedtls_ssl_session_reset_int(mbedtls_ssl_context *ssl, int partial)
Hanno Beckerb0302c42021-08-03 09:39:42 +01001471{
1472 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1473
1474 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
1475
Gilles Peskine449bd832023-01-11 14:50:10 +01001476 mbedtls_ssl_session_reset_msg_layer(ssl, partial);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001477
1478 /* Reset renegotiation state */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001479#if defined(MBEDTLS_SSL_RENEGOTIATION)
1480 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001481 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00001482
1483 ssl->verify_data_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001484 memset(ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN);
1485 memset(ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN);
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001486#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001487 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00001488
Hanno Beckerb0302c42021-08-03 09:39:42 +01001489 ssl->session_in = NULL;
Hanno Becker78640902018-08-13 16:35:15 +01001490 ssl->session_out = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001491 if (ssl->session) {
1492 mbedtls_ssl_session_free(ssl->session);
1493 mbedtls_free(ssl->session);
Paul Bakkerc0463502013-02-14 11:19:38 +01001494 ssl->session = NULL;
1495 }
1496
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001497#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02001498 ssl->alpn_chosen = NULL;
1499#endif
1500
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001501#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
David Horstmann3b2276a2022-10-06 14:49:08 +01001502 int free_cli_id = 1;
Hanno Becker4ccbf062018-08-10 11:20:38 +01001503#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Gilles Peskine449bd832023-01-11 14:50:10 +01001504 free_cli_id = (partial == 0);
Hanno Becker4ccbf062018-08-10 11:20:38 +01001505#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001506 if (free_cli_id) {
1507 mbedtls_free(ssl->cli_id);
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001508 ssl->cli_id = NULL;
1509 ssl->cli_id_len = 0;
1510 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02001511#endif
1512
Gilles Peskine449bd832023-01-11 14:50:10 +01001513 if ((ret = ssl_handshake_init(ssl)) != 0) {
1514 return ret;
1515 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00001516
Gilles Peskine449bd832023-01-11 14:50:10 +01001517 return 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00001518}
1519
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02001520/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001521 * Reset an initialized and used SSL context for re-use while retaining
1522 * all application-set variables, function pointers and data.
1523 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001524int mbedtls_ssl_session_reset(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001525{
Gilles Peskine449bd832023-01-11 14:50:10 +01001526 return mbedtls_ssl_session_reset_int(ssl, 0);
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001527}
1528
1529/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001530 * SSL set accessors
1531 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001532void mbedtls_ssl_conf_endpoint(mbedtls_ssl_config *conf, int endpoint)
Paul Bakker5121ce52009-01-03 21:22:43 +00001533{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001534 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00001535}
1536
Gilles Peskine449bd832023-01-11 14:50:10 +01001537void mbedtls_ssl_conf_transport(mbedtls_ssl_config *conf, int transport)
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001538{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001539 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001540}
1541
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001542#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine449bd832023-01-11 14:50:10 +01001543void mbedtls_ssl_conf_dtls_anti_replay(mbedtls_ssl_config *conf, char mode)
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001544{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001545 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001546}
1547#endif
1548
Gilles Peskine449bd832023-01-11 14:50:10 +01001549void mbedtls_ssl_conf_dtls_badmac_limit(mbedtls_ssl_config *conf, unsigned limit)
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001550{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001551 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001552}
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001553
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001554#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01001555
Gilles Peskine449bd832023-01-11 14:50:10 +01001556void mbedtls_ssl_set_datagram_packing(mbedtls_ssl_context *ssl,
1557 unsigned allow_packing)
Hanno Becker04da1892018-08-14 13:22:10 +01001558{
1559 ssl->disable_datagram_packing = !allow_packing;
1560}
1561
Gilles Peskine449bd832023-01-11 14:50:10 +01001562void mbedtls_ssl_conf_handshake_timeout(mbedtls_ssl_config *conf,
1563 uint32_t min, uint32_t max)
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001564{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001565 conf->hs_timeout_min = min;
1566 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001567}
1568#endif
1569
Gilles Peskine449bd832023-01-11 14:50:10 +01001570void mbedtls_ssl_conf_authmode(mbedtls_ssl_config *conf, int authmode)
Paul Bakker5121ce52009-01-03 21:22:43 +00001571{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001572 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00001573}
1574
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001575#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001576void mbedtls_ssl_conf_verify(mbedtls_ssl_config *conf,
1577 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
1578 void *p_vrfy)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001579{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001580 conf->f_vrfy = f_vrfy;
1581 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001582}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001583#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001584
Gilles Peskine449bd832023-01-11 14:50:10 +01001585void mbedtls_ssl_conf_rng(mbedtls_ssl_config *conf,
1586 int (*f_rng)(void *, unsigned char *, size_t),
1587 void *p_rng)
Paul Bakker5121ce52009-01-03 21:22:43 +00001588{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001589 conf->f_rng = f_rng;
1590 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00001591}
1592
Gilles Peskine449bd832023-01-11 14:50:10 +01001593void mbedtls_ssl_conf_dbg(mbedtls_ssl_config *conf,
1594 void (*f_dbg)(void *, int, const char *, int, const char *),
1595 void *p_dbg)
Paul Bakker5121ce52009-01-03 21:22:43 +00001596{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001597 conf->f_dbg = f_dbg;
1598 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00001599}
1600
Gilles Peskine449bd832023-01-11 14:50:10 +01001601void mbedtls_ssl_set_bio(mbedtls_ssl_context *ssl,
1602 void *p_bio,
1603 mbedtls_ssl_send_t *f_send,
1604 mbedtls_ssl_recv_t *f_recv,
1605 mbedtls_ssl_recv_timeout_t *f_recv_timeout)
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001606{
1607 ssl->p_bio = p_bio;
1608 ssl->f_send = f_send;
1609 ssl->f_recv = f_recv;
1610 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001611}
1612
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02001613#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001614void mbedtls_ssl_set_mtu(mbedtls_ssl_context *ssl, uint16_t mtu)
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02001615{
1616 ssl->mtu = mtu;
1617}
1618#endif
1619
Gilles Peskine449bd832023-01-11 14:50:10 +01001620void mbedtls_ssl_conf_read_timeout(mbedtls_ssl_config *conf, uint32_t timeout)
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001621{
1622 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001623}
1624
Gilles Peskine449bd832023-01-11 14:50:10 +01001625void mbedtls_ssl_set_timer_cb(mbedtls_ssl_context *ssl,
1626 void *p_timer,
1627 mbedtls_ssl_set_timer_t *f_set_timer,
1628 mbedtls_ssl_get_timer_t *f_get_timer)
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001629{
1630 ssl->p_timer = p_timer;
1631 ssl->f_set_timer = f_set_timer;
1632 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001633
1634 /* Make sure we start with no timer running */
Gilles Peskine449bd832023-01-11 14:50:10 +01001635 mbedtls_ssl_set_timer(ssl, 0);
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001636}
1637
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001638#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001639void mbedtls_ssl_conf_session_cache(mbedtls_ssl_config *conf,
1640 void *p_cache,
1641 mbedtls_ssl_cache_get_t *f_get_cache,
1642 mbedtls_ssl_cache_set_t *f_set_cache)
Paul Bakker5121ce52009-01-03 21:22:43 +00001643{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01001644 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001645 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001646 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00001647}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001648#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001649
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001650#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001651int mbedtls_ssl_set_session(mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session)
Paul Bakker5121ce52009-01-03 21:22:43 +00001652{
Janos Follath865b3eb2019-12-16 11:46:15 +00001653 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001654
Gilles Peskine449bd832023-01-11 14:50:10 +01001655 if (ssl == NULL ||
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001656 session == NULL ||
1657 ssl->session_negotiate == NULL ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001658 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT) {
1659 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001660 }
1661
Gilles Peskine449bd832023-01-11 14:50:10 +01001662 if (ssl->handshake->resume == 1) {
1663 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
1664 }
Hanno Beckere810bbc2021-05-14 16:01:05 +01001665
Jerry Yu21092062022-10-10 21:21:31 +08001666#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001667 if (session->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
Jerry Yu21092062022-10-10 21:21:31 +08001668 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01001669 mbedtls_ssl_ciphersuite_from_id(session->ciphersuite);
Jerry Yu21092062022-10-10 21:21:31 +08001670
Gilles Peskine449bd832023-01-11 14:50:10 +01001671 if (mbedtls_ssl_validate_ciphersuite(
Jerry Yu21092062022-10-10 21:21:31 +08001672 ssl, ciphersuite_info, MBEDTLS_SSL_VERSION_TLS1_3,
Gilles Peskine449bd832023-01-11 14:50:10 +01001673 MBEDTLS_SSL_VERSION_TLS1_3) != 0) {
1674 MBEDTLS_SSL_DEBUG_MSG(4, ("%d is not a valid TLS 1.3 ciphersuite.",
1675 session->ciphersuite));
1676 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jerry Yu21092062022-10-10 21:21:31 +08001677 }
Jerry Yu40afab62022-10-08 10:42:13 +08001678 }
Jerry Yu21092062022-10-10 21:21:31 +08001679#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu40afab62022-10-08 10:42:13 +08001680
Gilles Peskine449bd832023-01-11 14:50:10 +01001681 if ((ret = mbedtls_ssl_session_copy(ssl->session_negotiate,
1682 session)) != 0) {
1683 return ret;
1684 }
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001685
Paul Bakker0a597072012-09-25 21:55:46 +00001686 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001687
Gilles Peskine449bd832023-01-11 14:50:10 +01001688 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001689}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001690#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001691
Gilles Peskine449bd832023-01-11 14:50:10 +01001692void mbedtls_ssl_conf_ciphersuites(mbedtls_ssl_config *conf,
1693 const int *ciphersuites)
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001694{
Hanno Beckerd60b6c62021-04-29 12:04:11 +01001695 conf->ciphersuite_list = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00001696}
1697
Ronald Cron6f135e12021-12-08 16:57:54 +01001698#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001699void mbedtls_ssl_conf_tls13_key_exchange_modes(mbedtls_ssl_config *conf,
1700 const int kex_modes)
Hanno Becker71f1ed62021-07-24 06:01:47 +01001701{
Xiaofei Bai746f9482021-11-12 08:53:56 +00001702 conf->tls13_kex_modes = kex_modes & MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Hanno Becker71f1ed62021-07-24 06:01:47 +01001703}
Xiaokang Qian72de95d2022-10-25 02:54:33 +00001704
1705#if defined(MBEDTLS_SSL_EARLY_DATA)
Gilles Peskine449bd832023-01-11 14:50:10 +01001706void mbedtls_ssl_tls13_conf_early_data(mbedtls_ssl_config *conf,
1707 int early_data_enabled)
Xiaokang Qian72de95d2022-10-25 02:54:33 +00001708{
1709 conf->early_data_enabled = early_data_enabled;
1710}
Jerry Yucc4e0072022-11-22 17:22:22 +08001711
1712#if defined(MBEDTLS_SSL_SRV_C)
1713void mbedtls_ssl_tls13_conf_max_early_data_size(
Gilles Peskine449bd832023-01-11 14:50:10 +01001714 mbedtls_ssl_config *conf, uint32_t max_early_data_size)
Jerry Yucc4e0072022-11-22 17:22:22 +08001715{
Jerry Yu39da9852022-12-06 16:58:36 +08001716 conf->max_early_data_size = max_early_data_size;
Jerry Yucc4e0072022-11-22 17:22:22 +08001717}
1718#endif /* MBEDTLS_SSL_SRV_C */
1719
Xiaokang Qian72de95d2022-10-25 02:54:33 +00001720#endif /* MBEDTLS_SSL_EARLY_DATA */
Ronald Cron6f135e12021-12-08 16:57:54 +01001721#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01001722
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001723#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001724void mbedtls_ssl_conf_cert_profile(mbedtls_ssl_config *conf,
1725 const mbedtls_x509_crt_profile *profile)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02001726{
1727 conf->cert_profile = profile;
1728}
1729
Gilles Peskine449bd832023-01-11 14:50:10 +01001730static void ssl_key_cert_free(mbedtls_ssl_key_cert *key_cert)
Glenn Strauss36872db2022-01-22 05:06:31 -05001731{
1732 mbedtls_ssl_key_cert *cur = key_cert, *next;
1733
Gilles Peskine449bd832023-01-11 14:50:10 +01001734 while (cur != NULL) {
Glenn Strauss36872db2022-01-22 05:06:31 -05001735 next = cur->next;
Gilles Peskine449bd832023-01-11 14:50:10 +01001736 mbedtls_free(cur);
Glenn Strauss36872db2022-01-22 05:06:31 -05001737 cur = next;
1738 }
1739}
1740
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001741/* Append a new keycert entry to a (possibly empty) list */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001742MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001743static int ssl_append_key_cert(mbedtls_ssl_key_cert **head,
1744 mbedtls_x509_crt *cert,
1745 mbedtls_pk_context *key)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001746{
niisato8ee24222018-06-25 19:05:48 +09001747 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001748
Gilles Peskine449bd832023-01-11 14:50:10 +01001749 if (cert == NULL) {
Glenn Strauss36872db2022-01-22 05:06:31 -05001750 /* Free list if cert is null */
Gilles Peskine449bd832023-01-11 14:50:10 +01001751 ssl_key_cert_free(*head);
Glenn Strauss36872db2022-01-22 05:06:31 -05001752 *head = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001753 return 0;
Glenn Strauss36872db2022-01-22 05:06:31 -05001754 }
1755
Gilles Peskine449bd832023-01-11 14:50:10 +01001756 new_cert = mbedtls_calloc(1, sizeof(mbedtls_ssl_key_cert));
1757 if (new_cert == NULL) {
1758 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
1759 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001760
niisato8ee24222018-06-25 19:05:48 +09001761 new_cert->cert = cert;
1762 new_cert->key = key;
1763 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001764
Glenn Strauss36872db2022-01-22 05:06:31 -05001765 /* Update head if the list was null, else add to the end */
Gilles Peskine449bd832023-01-11 14:50:10 +01001766 if (*head == NULL) {
niisato8ee24222018-06-25 19:05:48 +09001767 *head = new_cert;
Gilles Peskine449bd832023-01-11 14:50:10 +01001768 } else {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001769 mbedtls_ssl_key_cert *cur = *head;
Gilles Peskine449bd832023-01-11 14:50:10 +01001770 while (cur->next != NULL) {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001771 cur = cur->next;
Gilles Peskine449bd832023-01-11 14:50:10 +01001772 }
niisato8ee24222018-06-25 19:05:48 +09001773 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001774 }
1775
Gilles Peskine449bd832023-01-11 14:50:10 +01001776 return 0;
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001777}
1778
Gilles Peskine449bd832023-01-11 14:50:10 +01001779int mbedtls_ssl_conf_own_cert(mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001780 mbedtls_x509_crt *own_cert,
Gilles Peskine449bd832023-01-11 14:50:10 +01001781 mbedtls_pk_context *pk_key)
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001782{
Gilles Peskine449bd832023-01-11 14:50:10 +01001783 return ssl_append_key_cert(&conf->key_cert, own_cert, pk_key);
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001784}
1785
Gilles Peskine449bd832023-01-11 14:50:10 +01001786void mbedtls_ssl_conf_ca_chain(mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001787 mbedtls_x509_crt *ca_chain,
Gilles Peskine449bd832023-01-11 14:50:10 +01001788 mbedtls_x509_crl *ca_crl)
Paul Bakker5121ce52009-01-03 21:22:43 +00001789{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001790 conf->ca_chain = ca_chain;
1791 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00001792
1793#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1794 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1795 * cannot be used together. */
1796 conf->f_ca_cb = NULL;
1797 conf->p_ca_cb = NULL;
1798#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00001799}
Hanno Becker5adaad92019-03-27 16:54:37 +00001800
1801#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Gilles Peskine449bd832023-01-11 14:50:10 +01001802void mbedtls_ssl_conf_ca_cb(mbedtls_ssl_config *conf,
1803 mbedtls_x509_crt_ca_cb_t f_ca_cb,
1804 void *p_ca_cb)
Hanno Becker5adaad92019-03-27 16:54:37 +00001805{
1806 conf->f_ca_cb = f_ca_cb;
1807 conf->p_ca_cb = p_ca_cb;
1808
1809 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1810 * cannot be used together. */
1811 conf->ca_chain = NULL;
1812 conf->ca_crl = NULL;
1813}
1814#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001815#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00001816
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001817#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01001818const unsigned char *mbedtls_ssl_get_hs_sni(mbedtls_ssl_context *ssl,
1819 size_t *name_len)
Glenn Strauss69894072022-01-24 12:58:00 -05001820{
1821 *name_len = ssl->handshake->sni_name_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001822 return ssl->handshake->sni_name;
Glenn Strauss69894072022-01-24 12:58:00 -05001823}
1824
Gilles Peskine449bd832023-01-11 14:50:10 +01001825int mbedtls_ssl_set_hs_own_cert(mbedtls_ssl_context *ssl,
1826 mbedtls_x509_crt *own_cert,
1827 mbedtls_pk_context *pk_key)
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001828{
Gilles Peskine449bd832023-01-11 14:50:10 +01001829 return ssl_append_key_cert(&ssl->handshake->sni_key_cert,
1830 own_cert, pk_key);
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001831}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02001832
Gilles Peskine449bd832023-01-11 14:50:10 +01001833void mbedtls_ssl_set_hs_ca_chain(mbedtls_ssl_context *ssl,
1834 mbedtls_x509_crt *ca_chain,
1835 mbedtls_x509_crl *ca_crl)
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02001836{
1837 ssl->handshake->sni_ca_chain = ca_chain;
1838 ssl->handshake->sni_ca_crl = ca_crl;
1839}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001840
Glenn Strauss999ef702022-03-11 01:37:23 -05001841#if defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001842void mbedtls_ssl_set_hs_dn_hints(mbedtls_ssl_context *ssl,
1843 const mbedtls_x509_crt *crt)
Glenn Strauss999ef702022-03-11 01:37:23 -05001844{
1845 ssl->handshake->dn_hints = crt;
1846}
1847#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
1848
Gilles Peskine449bd832023-01-11 14:50:10 +01001849void mbedtls_ssl_set_hs_authmode(mbedtls_ssl_context *ssl,
1850 int authmode)
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001851{
1852 ssl->handshake->sni_authmode = authmode;
1853}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001854#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
1855
Hanno Becker8927c832019-04-03 12:52:50 +01001856#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001857void mbedtls_ssl_set_verify(mbedtls_ssl_context *ssl,
1858 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
1859 void *p_vrfy)
Hanno Becker8927c832019-04-03 12:52:50 +01001860{
1861 ssl->f_vrfy = f_vrfy;
1862 ssl->p_vrfy = p_vrfy;
1863}
1864#endif
1865
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02001866#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001867
Valerio Setti016f6822022-12-09 14:17:50 +01001868#if defined(MBEDTLS_USE_PSA_CRYPTO)
1869static psa_status_t mbedtls_ssl_set_hs_ecjpake_password_common(
Gilles Peskine449bd832023-01-11 14:50:10 +01001870 mbedtls_ssl_context *ssl,
1871 mbedtls_svc_key_id_t pwd)
Valerio Setti016f6822022-12-09 14:17:50 +01001872{
1873 psa_status_t status;
1874 psa_pake_role_t psa_role;
1875 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
1876
Gilles Peskine449bd832023-01-11 14:50:10 +01001877 psa_pake_cs_set_algorithm(&cipher_suite, PSA_ALG_JPAKE);
1878 psa_pake_cs_set_primitive(&cipher_suite,
1879 PSA_PAKE_PRIMITIVE(PSA_PAKE_PRIMITIVE_TYPE_ECC,
1880 PSA_ECC_FAMILY_SECP_R1,
1881 256));
1882 psa_pake_cs_set_hash(&cipher_suite, PSA_ALG_SHA_256);
Valerio Setti016f6822022-12-09 14:17:50 +01001883
Gilles Peskine449bd832023-01-11 14:50:10 +01001884 status = psa_pake_setup(&ssl->handshake->psa_pake_ctx, &cipher_suite);
1885 if (status != PSA_SUCCESS) {
Valerio Setti016f6822022-12-09 14:17:50 +01001886 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001887 }
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001888
Gilles Peskine449bd832023-01-11 14:50:10 +01001889 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Neil Armstrongca7d5062022-05-31 14:43:23 +02001890 psa_role = PSA_PAKE_ROLE_SERVER;
Gilles Peskine449bd832023-01-11 14:50:10 +01001891 } else {
Neil Armstrongca7d5062022-05-31 14:43:23 +02001892 psa_role = PSA_PAKE_ROLE_CLIENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001893 }
Neil Armstrongca7d5062022-05-31 14:43:23 +02001894
Gilles Peskine449bd832023-01-11 14:50:10 +01001895 status = psa_pake_set_role(&ssl->handshake->psa_pake_ctx, psa_role);
1896 if (status != PSA_SUCCESS) {
Valerio Setti016f6822022-12-09 14:17:50 +01001897 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001898 }
Valerio Setti016f6822022-12-09 14:17:50 +01001899
Gilles Peskine449bd832023-01-11 14:50:10 +01001900 status = psa_pake_set_password_key(&ssl->handshake->psa_pake_ctx, pwd);
1901 if (status != PSA_SUCCESS) {
Valerio Setti016f6822022-12-09 14:17:50 +01001902 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001903 }
Valerio Setti016f6822022-12-09 14:17:50 +01001904
1905 ssl->handshake->psa_pake_ctx_is_ok = 1;
1906
Gilles Peskine449bd832023-01-11 14:50:10 +01001907 return PSA_SUCCESS;
Valerio Setti016f6822022-12-09 14:17:50 +01001908}
1909
Gilles Peskine449bd832023-01-11 14:50:10 +01001910int mbedtls_ssl_set_hs_ecjpake_password(mbedtls_ssl_context *ssl,
1911 const unsigned char *pw,
1912 size_t pw_len)
Valerio Setti016f6822022-12-09 14:17:50 +01001913{
1914 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1915 psa_status_t status;
1916
Gilles Peskine449bd832023-01-11 14:50:10 +01001917 if (ssl->handshake == NULL || ssl->conf == NULL) {
1918 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Neil Armstrongca7d5062022-05-31 14:43:23 +02001919 }
1920
Gilles Peskine449bd832023-01-11 14:50:10 +01001921 /* Empty password is not valid */
1922 if ((pw == NULL) || (pw_len == 0)) {
1923 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
1924 }
1925
1926 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
1927 psa_set_key_algorithm(&attributes, PSA_ALG_JPAKE);
1928 psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD);
1929
1930 status = psa_import_key(&attributes, pw, pw_len,
1931 &ssl->handshake->psa_pake_password);
1932 if (status != PSA_SUCCESS) {
1933 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
1934 }
1935
1936 status = mbedtls_ssl_set_hs_ecjpake_password_common(ssl,
1937 ssl->handshake->psa_pake_password);
1938 if (status != PSA_SUCCESS) {
1939 psa_destroy_key(ssl->handshake->psa_pake_password);
1940 psa_pake_abort(&ssl->handshake->psa_pake_ctx);
1941 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
1942 }
1943
1944 return 0;
Valerio Setti02c25b52022-11-15 14:08:42 +01001945}
Valerio Settia9a97dc2022-11-28 18:26:16 +01001946
Gilles Peskine449bd832023-01-11 14:50:10 +01001947int mbedtls_ssl_set_hs_ecjpake_password_opaque(mbedtls_ssl_context *ssl,
1948 mbedtls_svc_key_id_t pwd)
Valerio Settia9a97dc2022-11-28 18:26:16 +01001949{
Valerio Settia9a97dc2022-11-28 18:26:16 +01001950 psa_status_t status;
1951
Gilles Peskine449bd832023-01-11 14:50:10 +01001952 if (ssl->handshake == NULL || ssl->conf == NULL) {
1953 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Neil Armstrongca7d5062022-05-31 14:43:23 +02001954 }
1955
Gilles Peskine449bd832023-01-11 14:50:10 +01001956 if (mbedtls_svc_key_id_is_null(pwd)) {
1957 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
1958 }
1959
1960 status = mbedtls_ssl_set_hs_ecjpake_password_common(ssl, pwd);
1961 if (status != PSA_SUCCESS) {
1962 psa_pake_abort(&ssl->handshake->psa_pake_ctx);
1963 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
1964 }
1965
1966 return 0;
Valerio Setti02c25b52022-11-15 14:08:42 +01001967}
1968#else /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01001969int mbedtls_ssl_set_hs_ecjpake_password(mbedtls_ssl_context *ssl,
1970 const unsigned char *pw,
1971 size_t pw_len)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001972{
1973 mbedtls_ecjpake_role role;
1974
Gilles Peskine449bd832023-01-11 14:50:10 +01001975 if (ssl->handshake == NULL || ssl->conf == NULL) {
1976 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
1977 }
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001978
Valerio Settic689ed82022-12-07 14:40:38 +01001979 /* Empty password is not valid */
Gilles Peskine449bd832023-01-11 14:50:10 +01001980 if ((pw == NULL) || (pw_len == 0)) {
1981 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
1982 }
Valerio Settic689ed82022-12-07 14:40:38 +01001983
Gilles Peskine449bd832023-01-11 14:50:10 +01001984 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001985 role = MBEDTLS_ECJPAKE_SERVER;
Gilles Peskine449bd832023-01-11 14:50:10 +01001986 } else {
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001987 role = MBEDTLS_ECJPAKE_CLIENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001988 }
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001989
Gilles Peskine449bd832023-01-11 14:50:10 +01001990 return mbedtls_ecjpake_setup(&ssl->handshake->ecjpake_ctx,
1991 role,
1992 MBEDTLS_MD_SHA256,
1993 MBEDTLS_ECP_DP_SECP256R1,
1994 pw, pw_len);
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001995}
Valerio Setti02c25b52022-11-15 14:08:42 +01001996#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02001997#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001998
Ronald Cron73fe8df2022-10-05 14:31:43 +02001999#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002000int mbedtls_ssl_conf_has_static_psk(mbedtls_ssl_config const *conf)
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002001{
Gilles Peskine449bd832023-01-11 14:50:10 +01002002 if (conf->psk_identity == NULL ||
2003 conf->psk_identity_len == 0) {
2004 return 0;
Ronald Crond29e13e2022-10-19 10:33:48 +02002005 }
2006
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002007#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002008 if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) {
2009 return 1;
2010 }
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002011#endif /* MBEDTLS_USE_PSA_CRYPTO */
Ronald Crond29e13e2022-10-19 10:33:48 +02002012
Gilles Peskine449bd832023-01-11 14:50:10 +01002013 if (conf->psk != NULL && conf->psk_len != 0) {
2014 return 1;
2015 }
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002016
Gilles Peskine449bd832023-01-11 14:50:10 +01002017 return 0;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002018}
2019
Gilles Peskine449bd832023-01-11 14:50:10 +01002020static void ssl_conf_remove_psk(mbedtls_ssl_config *conf)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002021{
2022 /* Remove reference to existing PSK, if any. */
2023#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002024 if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) {
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002025 /* The maintenance of the PSK key slot is the
2026 * user's responsibility. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002027 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002028 }
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002029#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01002030 if (conf->psk != NULL) {
2031 mbedtls_platform_zeroize(conf->psk, conf->psk_len);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002032
Gilles Peskine449bd832023-01-11 14:50:10 +01002033 mbedtls_free(conf->psk);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002034 conf->psk = NULL;
2035 conf->psk_len = 0;
2036 }
2037
2038 /* Remove reference to PSK identity, if any. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002039 if (conf->psk_identity != NULL) {
2040 mbedtls_free(conf->psk_identity);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002041 conf->psk_identity = NULL;
2042 conf->psk_identity_len = 0;
2043 }
2044}
2045
Hanno Becker7390c712018-11-15 13:33:04 +00002046/* This function assumes that PSK identity in the SSL config is unset.
2047 * It checks that the provided identity is well-formed and attempts
2048 * to make a copy of it in the SSL config.
2049 * On failure, the PSK identity in the config remains unset. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002050MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002051static int ssl_conf_set_psk_identity(mbedtls_ssl_config *conf,
2052 unsigned char const *psk_identity,
2053 size_t psk_identity_len)
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002054{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02002055 /* Identity len will be encoded on two bytes */
Gilles Peskine449bd832023-01-11 14:50:10 +01002056 if (psk_identity == NULL ||
Ronald Cron2a87e9b2022-10-19 10:55:26 +02002057 psk_identity_len == 0 ||
Gilles Peskine449bd832023-01-11 14:50:10 +01002058 (psk_identity_len >> 16) != 0 ||
2059 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN) {
2060 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02002061 }
2062
Gilles Peskine449bd832023-01-11 14:50:10 +01002063 conf->psk_identity = mbedtls_calloc(1, psk_identity_len);
2064 if (conf->psk_identity == NULL) {
2065 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2066 }
Paul Bakker6db455e2013-09-18 17:29:31 +02002067
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01002068 conf->psk_identity_len = psk_identity_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002069 memcpy(conf->psk_identity, psk_identity, conf->psk_identity_len);
Paul Bakker5ad403f2013-09-18 21:21:30 +02002070
Gilles Peskine449bd832023-01-11 14:50:10 +01002071 return 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02002072}
2073
Gilles Peskine449bd832023-01-11 14:50:10 +01002074int mbedtls_ssl_conf_psk(mbedtls_ssl_config *conf,
2075 const unsigned char *psk, size_t psk_len,
2076 const unsigned char *psk_identity, size_t psk_identity_len)
Hanno Becker7390c712018-11-15 13:33:04 +00002077{
Janos Follath865b3eb2019-12-16 11:46:15 +00002078 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002079
2080 /* We currently only support one PSK, raw or opaque. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002081 if (mbedtls_ssl_conf_has_static_psk(conf)) {
2082 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
2083 }
Hanno Becker7390c712018-11-15 13:33:04 +00002084
2085 /* Check and set raw PSK */
Gilles Peskine449bd832023-01-11 14:50:10 +01002086 if (psk == NULL) {
2087 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2088 }
2089 if (psk_len == 0) {
2090 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2091 }
2092 if (psk_len > MBEDTLS_PSK_MAX_LEN) {
2093 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2094 }
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01002095
Gilles Peskine449bd832023-01-11 14:50:10 +01002096 if ((conf->psk = mbedtls_calloc(1, psk_len)) == NULL) {
2097 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2098 }
Hanno Becker7390c712018-11-15 13:33:04 +00002099 conf->psk_len = psk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002100 memcpy(conf->psk, psk, conf->psk_len);
Hanno Becker7390c712018-11-15 13:33:04 +00002101
2102 /* Check and set PSK Identity */
Gilles Peskine449bd832023-01-11 14:50:10 +01002103 ret = ssl_conf_set_psk_identity(conf, psk_identity, psk_identity_len);
2104 if (ret != 0) {
2105 ssl_conf_remove_psk(conf);
2106 }
Hanno Becker7390c712018-11-15 13:33:04 +00002107
Gilles Peskine449bd832023-01-11 14:50:10 +01002108 return ret;
Hanno Becker7390c712018-11-15 13:33:04 +00002109}
2110
Gilles Peskine449bd832023-01-11 14:50:10 +01002111static void ssl_remove_psk(mbedtls_ssl_context *ssl)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002112{
2113#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002114 if (!mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque)) {
Neil Armstrong501c9322022-05-03 09:35:09 +02002115 /* The maintenance of the external PSK key slot is the
2116 * user's responsibility. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002117 if (ssl->handshake->psk_opaque_is_internal) {
2118 psa_destroy_key(ssl->handshake->psk_opaque);
Neil Armstrong501c9322022-05-03 09:35:09 +02002119 ssl->handshake->psk_opaque_is_internal = 0;
2120 }
Ronald Croncf56a0a2020-08-04 09:51:30 +02002121 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002122 }
Neil Armstronge952a302022-05-03 10:22:14 +02002123#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002124 if (ssl->handshake->psk != NULL) {
2125 mbedtls_platform_zeroize(ssl->handshake->psk,
2126 ssl->handshake->psk_len);
2127 mbedtls_free(ssl->handshake->psk);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002128 ssl->handshake->psk_len = 0;
2129 }
Neil Armstronge952a302022-05-03 10:22:14 +02002130#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002131}
2132
Gilles Peskine449bd832023-01-11 14:50:10 +01002133int mbedtls_ssl_set_hs_psk(mbedtls_ssl_context *ssl,
2134 const unsigned char *psk, size_t psk_len)
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002135{
Neil Armstrong501c9322022-05-03 09:35:09 +02002136#if defined(MBEDTLS_USE_PSA_CRYPTO)
2137 psa_key_attributes_t key_attributes = psa_key_attributes_init();
Jerry Yu5d01c052022-08-17 10:18:10 +08002138 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jerry Yu24b8c812022-08-20 19:06:56 +08002139 psa_algorithm_t alg = PSA_ALG_NONE;
Jerry Yu5d01c052022-08-17 10:18:10 +08002140 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrong501c9322022-05-03 09:35:09 +02002141#endif /* MBEDTLS_USE_PSA_CRYPTO */
2142
Gilles Peskine449bd832023-01-11 14:50:10 +01002143 if (psk == NULL || ssl->handshake == NULL) {
2144 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2145 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002146
Gilles Peskine449bd832023-01-11 14:50:10 +01002147 if (psk_len > MBEDTLS_PSK_MAX_LEN) {
2148 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2149 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002150
Gilles Peskine449bd832023-01-11 14:50:10 +01002151 ssl_remove_psk(ssl);
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002152
Neil Armstrong501c9322022-05-03 09:35:09 +02002153#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yuccc68a42022-07-26 16:39:20 +08002154#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01002155 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2) {
2156 if (ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384) {
2157 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
2158 } else {
2159 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
2160 }
2161 psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE);
Jerry Yuccc68a42022-07-26 16:39:20 +08002162 }
2163#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Neil Armstrong501c9322022-05-03 09:35:09 +02002164
Jerry Yu568ec252022-07-22 21:27:34 +08002165#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01002166 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
2167 alg = PSA_ALG_HKDF_EXTRACT(PSA_ALG_ANY_HASH);
2168 psa_set_key_usage_flags(&key_attributes,
2169 PSA_KEY_USAGE_DERIVE | PSA_KEY_USAGE_EXPORT);
Jerry Yuccc68a42022-07-26 16:39:20 +08002170 }
2171#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
2172
Gilles Peskine449bd832023-01-11 14:50:10 +01002173 psa_set_key_algorithm(&key_attributes, alg);
2174 psa_set_key_type(&key_attributes, PSA_KEY_TYPE_DERIVE);
Neil Armstrong501c9322022-05-03 09:35:09 +02002175
Gilles Peskine449bd832023-01-11 14:50:10 +01002176 status = psa_import_key(&key_attributes, psk, psk_len, &key);
2177 if (status != PSA_SUCCESS) {
2178 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2179 }
Neil Armstrong501c9322022-05-03 09:35:09 +02002180
2181 /* Allow calling psa_destroy_key() on psk remove */
2182 ssl->handshake->psk_opaque_is_internal = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01002183 return mbedtls_ssl_set_hs_psk_opaque(ssl, key);
Neil Armstrong501c9322022-05-03 09:35:09 +02002184#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002185 if ((ssl->handshake->psk = mbedtls_calloc(1, psk_len)) == NULL) {
2186 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2187 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002188
2189 ssl->handshake->psk_len = psk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002190 memcpy(ssl->handshake->psk, psk, ssl->handshake->psk_len);
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002191
Gilles Peskine449bd832023-01-11 14:50:10 +01002192 return 0;
Neil Armstrong501c9322022-05-03 09:35:09 +02002193#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002194}
2195
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002196#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002197int mbedtls_ssl_conf_psk_opaque(mbedtls_ssl_config *conf,
2198 mbedtls_svc_key_id_t psk,
2199 const unsigned char *psk_identity,
2200 size_t psk_identity_len)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002201{
Janos Follath865b3eb2019-12-16 11:46:15 +00002202 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002203
2204 /* We currently only support one PSK, raw or opaque. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002205 if (mbedtls_ssl_conf_has_static_psk(conf)) {
2206 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
2207 }
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002208
Hanno Becker7390c712018-11-15 13:33:04 +00002209 /* Check and set opaque PSK */
Gilles Peskine449bd832023-01-11 14:50:10 +01002210 if (mbedtls_svc_key_id_is_null(psk)) {
2211 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2212 }
Ronald Croncf56a0a2020-08-04 09:51:30 +02002213 conf->psk_opaque = psk;
Hanno Becker7390c712018-11-15 13:33:04 +00002214
2215 /* Check and set PSK Identity */
Gilles Peskine449bd832023-01-11 14:50:10 +01002216 ret = ssl_conf_set_psk_identity(conf, psk_identity,
2217 psk_identity_len);
2218 if (ret != 0) {
2219 ssl_conf_remove_psk(conf);
2220 }
Hanno Becker7390c712018-11-15 13:33:04 +00002221
Gilles Peskine449bd832023-01-11 14:50:10 +01002222 return ret;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002223}
2224
Gilles Peskine449bd832023-01-11 14:50:10 +01002225int mbedtls_ssl_set_hs_psk_opaque(mbedtls_ssl_context *ssl,
2226 mbedtls_svc_key_id_t psk)
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002227{
Gilles Peskine449bd832023-01-11 14:50:10 +01002228 if ((mbedtls_svc_key_id_is_null(psk)) ||
2229 (ssl->handshake == NULL)) {
2230 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2231 }
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002232
Gilles Peskine449bd832023-01-11 14:50:10 +01002233 ssl_remove_psk(ssl);
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002234 ssl->handshake->psk_opaque = psk;
Gilles Peskine449bd832023-01-11 14:50:10 +01002235 return 0;
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002236}
2237#endif /* MBEDTLS_USE_PSA_CRYPTO */
2238
Jerry Yu8897c072022-08-12 13:56:53 +08002239#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002240void mbedtls_ssl_conf_psk_cb(mbedtls_ssl_config *conf,
2241 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
2242 size_t),
2243 void *p_psk)
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002244{
2245 conf->f_psk = f_psk;
2246 conf->p_psk = p_psk;
2247}
Jerry Yu8897c072022-08-12 13:56:53 +08002248#endif /* MBEDTLS_SSL_SRV_C */
2249
Ronald Cron73fe8df2022-10-05 14:31:43 +02002250#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002251
2252#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine301711e2022-04-26 16:57:05 +02002253static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
Gilles Peskine449bd832023-01-11 14:50:10 +01002254 psa_algorithm_t alg)
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002255{
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002256#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01002257 if (alg == PSA_ALG_CBC_NO_PADDING) {
2258 return MBEDTLS_SSL_MODE_CBC;
2259 }
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002260#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
Gilles Peskine449bd832023-01-11 14:50:10 +01002261 if (PSA_ALG_IS_AEAD(alg)) {
2262 return MBEDTLS_SSL_MODE_AEAD;
2263 }
2264 return MBEDTLS_SSL_MODE_STREAM;
Gilles Peskine301711e2022-04-26 16:57:05 +02002265}
2266
2267#else /* MBEDTLS_USE_PSA_CRYPTO */
2268
2269static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
Gilles Peskine449bd832023-01-11 14:50:10 +01002270 mbedtls_cipher_mode_t mode)
Gilles Peskine301711e2022-04-26 16:57:05 +02002271{
2272#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01002273 if (mode == MBEDTLS_MODE_CBC) {
2274 return MBEDTLS_SSL_MODE_CBC;
2275 }
Gilles Peskine301711e2022-04-26 16:57:05 +02002276#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
2277
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002278#if defined(MBEDTLS_GCM_C) || \
2279 defined(MBEDTLS_CCM_C) || \
2280 defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002281 if (mode == MBEDTLS_MODE_GCM ||
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002282 mode == MBEDTLS_MODE_CCM ||
Gilles Peskine449bd832023-01-11 14:50:10 +01002283 mode == MBEDTLS_MODE_CHACHAPOLY) {
2284 return MBEDTLS_SSL_MODE_AEAD;
2285 }
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002286#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002287
Gilles Peskine449bd832023-01-11 14:50:10 +01002288 return MBEDTLS_SSL_MODE_STREAM;
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002289}
Gilles Peskine301711e2022-04-26 16:57:05 +02002290#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002291
Gilles Peskinee108d982022-04-26 16:50:40 +02002292static mbedtls_ssl_mode_t mbedtls_ssl_get_actual_mode(
2293 mbedtls_ssl_mode_t base_mode,
Gilles Peskine449bd832023-01-11 14:50:10 +01002294 int encrypt_then_mac)
Gilles Peskinee108d982022-04-26 16:50:40 +02002295{
2296#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01002297 if (encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED &&
2298 base_mode == MBEDTLS_SSL_MODE_CBC) {
2299 return MBEDTLS_SSL_MODE_CBC_ETM;
Gilles Peskinee108d982022-04-26 16:50:40 +02002300 }
2301#else
2302 (void) encrypt_then_mac;
2303#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002304 return base_mode;
Gilles Peskinee108d982022-04-26 16:50:40 +02002305}
2306
Neil Armstrongab555e02022-04-04 11:07:59 +02002307mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_transform(
Gilles Peskine449bd832023-01-11 14:50:10 +01002308 const mbedtls_ssl_transform *transform)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002309{
Gilles Peskinee108d982022-04-26 16:50:40 +02002310 mbedtls_ssl_mode_t base_mode = mbedtls_ssl_get_base_mode(
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002311#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002312 transform->psa_alg
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002313#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002314 mbedtls_cipher_get_cipher_mode(&transform->cipher_ctx_enc)
Gilles Peskinee108d982022-04-26 16:50:40 +02002315#endif
2316 );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002317
Gilles Peskinee108d982022-04-26 16:50:40 +02002318 int encrypt_then_mac = 0;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002319#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskinee108d982022-04-26 16:50:40 +02002320 encrypt_then_mac = transform->encrypt_then_mac;
2321#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002322 return mbedtls_ssl_get_actual_mode(base_mode, encrypt_then_mac);
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002323}
2324
Neil Armstrongab555e02022-04-04 11:07:59 +02002325mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002326#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01002327 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002328#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01002329 const mbedtls_ssl_ciphersuite_t *suite)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002330{
Gilles Peskinee108d982022-04-26 16:50:40 +02002331 mbedtls_ssl_mode_t base_mode = MBEDTLS_SSL_MODE_STREAM;
2332
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002333#if defined(MBEDTLS_USE_PSA_CRYPTO)
2334 psa_status_t status;
2335 psa_algorithm_t alg;
2336 psa_key_type_t type;
2337 size_t size;
Gilles Peskine449bd832023-01-11 14:50:10 +01002338 status = mbedtls_ssl_cipher_to_psa(suite->cipher, 0, &alg, &type, &size);
2339 if (status == PSA_SUCCESS) {
2340 base_mode = mbedtls_ssl_get_base_mode(alg);
2341 }
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002342#else
2343 const mbedtls_cipher_info_t *cipher =
Gilles Peskine449bd832023-01-11 14:50:10 +01002344 mbedtls_cipher_info_from_type(suite->cipher);
2345 if (cipher != NULL) {
Gilles Peskinee108d982022-04-26 16:50:40 +02002346 base_mode =
2347 mbedtls_ssl_get_base_mode(
Gilles Peskine449bd832023-01-11 14:50:10 +01002348 mbedtls_cipher_info_get_mode(cipher));
Gilles Peskinee108d982022-04-26 16:50:40 +02002349 }
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002350#endif /* MBEDTLS_USE_PSA_CRYPTO */
2351
Gilles Peskinee108d982022-04-26 16:50:40 +02002352#if !defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
2353 int encrypt_then_mac = 0;
2354#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002355 return mbedtls_ssl_get_actual_mode(base_mode, encrypt_then_mac);
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002356}
2357
Neil Armstrong8395d7a2022-05-18 11:44:56 +02002358#if defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu251a12e2022-07-13 15:15:48 +08002359
2360#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu438ddd82022-07-07 06:55:50 +00002361/* Serialization of TLS 1.3 sessions:
2362 *
2363 * struct {
Xiaokang Qian126bf8e2022-10-13 02:22:40 +00002364 * opaque hostname<0..2^16-1>;
Jerry Yu438ddd82022-07-07 06:55:50 +00002365 * uint64 ticket_received;
2366 * uint32 ticket_lifetime;
Jerry Yu34191072022-08-18 10:32:09 +08002367 * opaque ticket<1..2^16-1>;
Jerry Yu438ddd82022-07-07 06:55:50 +00002368 * } ClientOnlyData;
2369 *
2370 * struct {
2371 * uint8 endpoint;
2372 * uint8 ciphersuite[2];
2373 * uint32 ticket_age_add;
2374 * uint8 ticket_flags;
2375 * opaque resumption_key<0..255>;
2376 * select ( endpoint ) {
2377 * case client: ClientOnlyData;
2378 * case server: uint64 start_time;
2379 * };
2380 * } serialized_session_tls13;
2381 *
2382 */
2383#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yue36fdd62022-08-17 21:31:36 +08002384MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002385static int ssl_tls13_session_save(const mbedtls_ssl_session *session,
2386 unsigned char *buf,
2387 size_t buf_len,
2388 size_t *olen)
Jerry Yu438ddd82022-07-07 06:55:50 +00002389{
2390 unsigned char *p = buf;
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00002391#if defined(MBEDTLS_SSL_CLI_C) && \
2392 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01002393 size_t hostname_len = (session->hostname == NULL) ?
2394 0 : strlen(session->hostname) + 1;
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00002395#endif
Jerry Yubc7c1a42022-07-21 22:57:37 +08002396 size_t needed = 1 /* endpoint */
2397 + 2 /* ciphersuite */
2398 + 4 /* ticket_age_add */
Jerry Yu34191072022-08-18 10:32:09 +08002399 + 1 /* ticket_flags */
2400 + 1; /* resumption_key length */
Jerry Yue36fdd62022-08-17 21:31:36 +08002401 *olen = 0;
Jerry Yu34191072022-08-18 10:32:09 +08002402
Gilles Peskine449bd832023-01-11 14:50:10 +01002403 if (session->resumption_key_len > MBEDTLS_SSL_TLS1_3_TICKET_RESUMPTION_KEY_LEN) {
2404 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2405 }
Jerry Yu34191072022-08-18 10:32:09 +08002406 needed += session->resumption_key_len; /* resumption_key */
2407
Jerry Yu438ddd82022-07-07 06:55:50 +00002408#if defined(MBEDTLS_HAVE_TIME)
2409 needed += 8; /* start_time or ticket_received */
2410#endif
2411
2412#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002413 if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Xiaokang Qian126bf8e2022-10-13 02:22:40 +00002414#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaokang Qianbc663a02022-10-09 11:14:39 +00002415 needed += 2 /* hostname_len */
Gilles Peskine449bd832023-01-11 14:50:10 +01002416 + hostname_len; /* hostname */
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00002417#endif
2418
Jerry Yu438ddd82022-07-07 06:55:50 +00002419 needed += 4 /* ticket_lifetime */
Jerry Yue36fdd62022-08-17 21:31:36 +08002420 + 2; /* ticket_len */
Jerry Yu34191072022-08-18 10:32:09 +08002421
2422 /* Check size_t overflow */
Gilles Peskine449bd832023-01-11 14:50:10 +01002423 if (session->ticket_len > SIZE_MAX - needed) {
2424 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2425 }
Jerry Yu34191072022-08-18 10:32:09 +08002426
Jerry Yue28d9742022-08-18 15:44:03 +08002427 needed += session->ticket_len; /* ticket */
Jerry Yu438ddd82022-07-07 06:55:50 +00002428 }
2429#endif /* MBEDTLS_SSL_CLI_C */
2430
Jerry Yue36fdd62022-08-17 21:31:36 +08002431 *olen = needed;
Gilles Peskine449bd832023-01-11 14:50:10 +01002432 if (needed > buf_len) {
2433 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
2434 }
Jerry Yu438ddd82022-07-07 06:55:50 +00002435
2436 p[0] = session->endpoint;
Gilles Peskine449bd832023-01-11 14:50:10 +01002437 MBEDTLS_PUT_UINT16_BE(session->ciphersuite, p, 1);
2438 MBEDTLS_PUT_UINT32_BE(session->ticket_age_add, p, 3);
Jerry Yu438ddd82022-07-07 06:55:50 +00002439 p[7] = session->ticket_flags;
2440
2441 /* save resumption_key */
Jerry Yubc7c1a42022-07-21 22:57:37 +08002442 p[8] = session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002443 p += 9;
Gilles Peskine449bd832023-01-11 14:50:10 +01002444 memcpy(p, session->resumption_key, session->resumption_key_len);
Jerry Yubc7c1a42022-07-21 22:57:37 +08002445 p += session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002446
2447#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002448 if (session->endpoint == MBEDTLS_SSL_IS_SERVER) {
2449 MBEDTLS_PUT_UINT64_BE((uint64_t) session->start, p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002450 p += 8;
2451 }
2452#endif /* MBEDTLS_HAVE_TIME */
2453
2454#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002455 if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002456#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01002457 MBEDTLS_PUT_UINT16_BE(hostname_len, p, 0);
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002458 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002459 if (hostname_len > 0) {
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002460 /* save host name */
Gilles Peskine449bd832023-01-11 14:50:10 +01002461 memcpy(p, session->hostname, hostname_len);
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002462 p += hostname_len;
2463 }
2464#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
2465
Jerry Yu438ddd82022-07-07 06:55:50 +00002466#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01002467 MBEDTLS_PUT_UINT64_BE((uint64_t) session->ticket_received, p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002468 p += 8;
2469#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002470 MBEDTLS_PUT_UINT32_BE(session->ticket_lifetime, p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002471 p += 4;
2472
Gilles Peskine449bd832023-01-11 14:50:10 +01002473 MBEDTLS_PUT_UINT16_BE(session->ticket_len, p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002474 p += 2;
Jerry Yu34191072022-08-18 10:32:09 +08002475
Gilles Peskine449bd832023-01-11 14:50:10 +01002476 if (session->ticket != NULL && session->ticket_len > 0) {
2477 memcpy(p, session->ticket, session->ticket_len);
Jerry Yu438ddd82022-07-07 06:55:50 +00002478 p += session->ticket_len;
2479 }
2480 }
2481#endif /* MBEDTLS_SSL_CLI_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01002482 return 0;
Jerry Yu438ddd82022-07-07 06:55:50 +00002483}
2484
2485MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002486static int ssl_tls13_session_load(mbedtls_ssl_session *session,
2487 const unsigned char *buf,
2488 size_t len)
Jerry Yu438ddd82022-07-07 06:55:50 +00002489{
2490 const unsigned char *p = buf;
2491 const unsigned char *end = buf + len;
2492
Gilles Peskine449bd832023-01-11 14:50:10 +01002493 if (end - p < 9) {
2494 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2495 }
Jerry Yu438ddd82022-07-07 06:55:50 +00002496 session->endpoint = p[0];
Gilles Peskine449bd832023-01-11 14:50:10 +01002497 session->ciphersuite = MBEDTLS_GET_UINT16_BE(p, 1);
2498 session->ticket_age_add = MBEDTLS_GET_UINT32_BE(p, 3);
Jerry Yu438ddd82022-07-07 06:55:50 +00002499 session->ticket_flags = p[7];
2500
2501 /* load resumption_key */
Jerry Yubc7c1a42022-07-21 22:57:37 +08002502 session->resumption_key_len = p[8];
Jerry Yu438ddd82022-07-07 06:55:50 +00002503 p += 9;
2504
Gilles Peskine449bd832023-01-11 14:50:10 +01002505 if (end - p < session->resumption_key_len) {
2506 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2507 }
Jerry Yu438ddd82022-07-07 06:55:50 +00002508
Gilles Peskine449bd832023-01-11 14:50:10 +01002509 if (sizeof(session->resumption_key) < session->resumption_key_len) {
2510 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2511 }
2512 memcpy(session->resumption_key, p, session->resumption_key_len);
Jerry Yubc7c1a42022-07-21 22:57:37 +08002513 p += session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002514
2515#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002516 if (session->endpoint == MBEDTLS_SSL_IS_SERVER) {
2517 if (end - p < 8) {
2518 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2519 }
2520 session->start = MBEDTLS_GET_UINT64_BE(p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002521 p += 8;
2522 }
2523#endif /* MBEDTLS_HAVE_TIME */
2524
2525#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002526 if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002527#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01002528 defined(MBEDTLS_SSL_SESSION_TICKETS)
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002529 size_t hostname_len;
2530 /* load host name */
Gilles Peskine449bd832023-01-11 14:50:10 +01002531 if (end - p < 2) {
2532 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2533 }
2534 hostname_len = MBEDTLS_GET_UINT16_BE(p, 0);
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002535 p += 2;
2536
Gilles Peskine449bd832023-01-11 14:50:10 +01002537 if (end - p < (long int) hostname_len) {
2538 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2539 }
2540 if (hostname_len > 0) {
2541 session->hostname = mbedtls_calloc(1, hostname_len);
2542 if (session->hostname == NULL) {
2543 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2544 }
2545 memcpy(session->hostname, p, hostname_len);
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002546 p += hostname_len;
2547 }
2548#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION &&
2549 MBEDTLS_SSL_SESSION_TICKETS */
2550
Jerry Yu438ddd82022-07-07 06:55:50 +00002551#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01002552 if (end - p < 8) {
2553 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2554 }
2555 session->ticket_received = MBEDTLS_GET_UINT64_BE(p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002556 p += 8;
2557#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002558 if (end - p < 4) {
2559 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2560 }
2561 session->ticket_lifetime = MBEDTLS_GET_UINT32_BE(p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002562 p += 4;
2563
Gilles Peskine449bd832023-01-11 14:50:10 +01002564 if (end - p < 2) {
2565 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2566 }
2567 session->ticket_len = MBEDTLS_GET_UINT16_BE(p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002568 p += 2;
2569
Gilles Peskine449bd832023-01-11 14:50:10 +01002570 if (end - p < (long int) session->ticket_len) {
2571 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2572 }
2573 if (session->ticket_len > 0) {
2574 session->ticket = mbedtls_calloc(1, session->ticket_len);
2575 if (session->ticket == NULL) {
2576 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2577 }
2578 memcpy(session->ticket, p, session->ticket_len);
Jerry Yu438ddd82022-07-07 06:55:50 +00002579 p += session->ticket_len;
2580 }
2581 }
2582#endif /* MBEDTLS_SSL_CLI_C */
2583
Gilles Peskine449bd832023-01-11 14:50:10 +01002584 return 0;
Jerry Yu438ddd82022-07-07 06:55:50 +00002585
2586}
2587#else /* MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yue36fdd62022-08-17 21:31:36 +08002588MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002589static int ssl_tls13_session_save(const mbedtls_ssl_session *session,
2590 unsigned char *buf,
2591 size_t buf_len,
2592 size_t *olen)
Jerry Yu251a12e2022-07-13 15:15:48 +08002593{
2594 ((void) session);
2595 ((void) buf);
2596 ((void) buf_len);
Jerry Yue36fdd62022-08-17 21:31:36 +08002597 *olen = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002598 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Jerry Yu251a12e2022-07-13 15:15:48 +08002599}
Jerry Yu438ddd82022-07-07 06:55:50 +00002600
Gilles Peskine449bd832023-01-11 14:50:10 +01002601static int ssl_tls13_session_load(const mbedtls_ssl_session *session,
2602 unsigned char *buf,
2603 size_t buf_len)
Jerry Yu438ddd82022-07-07 06:55:50 +00002604{
2605 ((void) session);
2606 ((void) buf);
2607 ((void) buf_len);
Gilles Peskine449bd832023-01-11 14:50:10 +01002608 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Jerry Yu438ddd82022-07-07 06:55:50 +00002609}
2610#endif /* !MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yu251a12e2022-07-13 15:15:48 +08002611#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
2612
Gilles Peskine449bd832023-01-11 14:50:10 +01002613psa_status_t mbedtls_ssl_cipher_to_psa(mbedtls_cipher_type_t mbedtls_cipher_type,
2614 size_t taglen,
2615 psa_algorithm_t *alg,
2616 psa_key_type_t *key_type,
2617 size_t *key_size)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002618{
Gilles Peskine449bd832023-01-11 14:50:10 +01002619 switch (mbedtls_cipher_type) {
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002620 case MBEDTLS_CIPHER_AES_128_CBC:
2621 *alg = PSA_ALG_CBC_NO_PADDING;
2622 *key_type = PSA_KEY_TYPE_AES;
2623 *key_size = 128;
2624 break;
2625 case MBEDTLS_CIPHER_AES_128_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002626 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002627 *key_type = PSA_KEY_TYPE_AES;
2628 *key_size = 128;
2629 break;
2630 case MBEDTLS_CIPHER_AES_128_GCM:
2631 *alg = PSA_ALG_GCM;
2632 *key_type = PSA_KEY_TYPE_AES;
2633 *key_size = 128;
2634 break;
2635 case MBEDTLS_CIPHER_AES_192_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002636 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002637 *key_type = PSA_KEY_TYPE_AES;
2638 *key_size = 192;
2639 break;
2640 case MBEDTLS_CIPHER_AES_192_GCM:
2641 *alg = PSA_ALG_GCM;
2642 *key_type = PSA_KEY_TYPE_AES;
2643 *key_size = 192;
2644 break;
2645 case MBEDTLS_CIPHER_AES_256_CBC:
2646 *alg = PSA_ALG_CBC_NO_PADDING;
2647 *key_type = PSA_KEY_TYPE_AES;
2648 *key_size = 256;
2649 break;
2650 case MBEDTLS_CIPHER_AES_256_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002651 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002652 *key_type = PSA_KEY_TYPE_AES;
2653 *key_size = 256;
2654 break;
2655 case MBEDTLS_CIPHER_AES_256_GCM:
2656 *alg = PSA_ALG_GCM;
2657 *key_type = PSA_KEY_TYPE_AES;
2658 *key_size = 256;
2659 break;
2660 case MBEDTLS_CIPHER_ARIA_128_CBC:
2661 *alg = PSA_ALG_CBC_NO_PADDING;
2662 *key_type = PSA_KEY_TYPE_ARIA;
2663 *key_size = 128;
2664 break;
2665 case MBEDTLS_CIPHER_ARIA_128_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002666 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002667 *key_type = PSA_KEY_TYPE_ARIA;
2668 *key_size = 128;
2669 break;
2670 case MBEDTLS_CIPHER_ARIA_128_GCM:
2671 *alg = PSA_ALG_GCM;
2672 *key_type = PSA_KEY_TYPE_ARIA;
2673 *key_size = 128;
2674 break;
2675 case MBEDTLS_CIPHER_ARIA_192_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002676 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002677 *key_type = PSA_KEY_TYPE_ARIA;
2678 *key_size = 192;
2679 break;
2680 case MBEDTLS_CIPHER_ARIA_192_GCM:
2681 *alg = PSA_ALG_GCM;
2682 *key_type = PSA_KEY_TYPE_ARIA;
2683 *key_size = 192;
2684 break;
2685 case MBEDTLS_CIPHER_ARIA_256_CBC:
2686 *alg = PSA_ALG_CBC_NO_PADDING;
2687 *key_type = PSA_KEY_TYPE_ARIA;
2688 *key_size = 256;
2689 break;
2690 case MBEDTLS_CIPHER_ARIA_256_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002691 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002692 *key_type = PSA_KEY_TYPE_ARIA;
2693 *key_size = 256;
2694 break;
2695 case MBEDTLS_CIPHER_ARIA_256_GCM:
2696 *alg = PSA_ALG_GCM;
2697 *key_type = PSA_KEY_TYPE_ARIA;
2698 *key_size = 256;
2699 break;
2700 case MBEDTLS_CIPHER_CAMELLIA_128_CBC:
2701 *alg = PSA_ALG_CBC_NO_PADDING;
2702 *key_type = PSA_KEY_TYPE_CAMELLIA;
2703 *key_size = 128;
2704 break;
2705 case MBEDTLS_CIPHER_CAMELLIA_128_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002706 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002707 *key_type = PSA_KEY_TYPE_CAMELLIA;
2708 *key_size = 128;
2709 break;
2710 case MBEDTLS_CIPHER_CAMELLIA_128_GCM:
2711 *alg = PSA_ALG_GCM;
2712 *key_type = PSA_KEY_TYPE_CAMELLIA;
2713 *key_size = 128;
2714 break;
2715 case MBEDTLS_CIPHER_CAMELLIA_192_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002716 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002717 *key_type = PSA_KEY_TYPE_CAMELLIA;
2718 *key_size = 192;
2719 break;
2720 case MBEDTLS_CIPHER_CAMELLIA_192_GCM:
2721 *alg = PSA_ALG_GCM;
2722 *key_type = PSA_KEY_TYPE_CAMELLIA;
2723 *key_size = 192;
2724 break;
2725 case MBEDTLS_CIPHER_CAMELLIA_256_CBC:
2726 *alg = PSA_ALG_CBC_NO_PADDING;
2727 *key_type = PSA_KEY_TYPE_CAMELLIA;
2728 *key_size = 256;
2729 break;
2730 case MBEDTLS_CIPHER_CAMELLIA_256_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002731 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002732 *key_type = PSA_KEY_TYPE_CAMELLIA;
2733 *key_size = 256;
2734 break;
2735 case MBEDTLS_CIPHER_CAMELLIA_256_GCM:
2736 *alg = PSA_ALG_GCM;
2737 *key_type = PSA_KEY_TYPE_CAMELLIA;
2738 *key_size = 256;
2739 break;
2740 case MBEDTLS_CIPHER_CHACHA20_POLY1305:
2741 *alg = PSA_ALG_CHACHA20_POLY1305;
2742 *key_type = PSA_KEY_TYPE_CHACHA20;
2743 *key_size = 256;
2744 break;
2745 case MBEDTLS_CIPHER_NULL:
2746 *alg = MBEDTLS_SSL_NULL_CIPHER;
2747 *key_type = 0;
2748 *key_size = 0;
2749 break;
2750 default:
2751 return PSA_ERROR_NOT_SUPPORTED;
2752 }
2753
2754 return PSA_SUCCESS;
2755}
Neil Armstrong8395d7a2022-05-18 11:44:56 +02002756#endif /* MBEDTLS_USE_PSA_CRYPTO || MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002757
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002758#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002759int mbedtls_ssl_conf_dh_param_bin(mbedtls_ssl_config *conf,
2760 const unsigned char *dhm_P, size_t P_len,
2761 const unsigned char *dhm_G, size_t G_len)
Hanno Beckera90658f2017-10-04 15:29:08 +01002762{
Janos Follath865b3eb2019-12-16 11:46:15 +00002763 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckera90658f2017-10-04 15:29:08 +01002764
Gilles Peskine449bd832023-01-11 14:50:10 +01002765 mbedtls_mpi_free(&conf->dhm_P);
2766 mbedtls_mpi_free(&conf->dhm_G);
Glenn Strausscee11292021-12-20 01:43:17 -05002767
Gilles Peskine449bd832023-01-11 14:50:10 +01002768 if ((ret = mbedtls_mpi_read_binary(&conf->dhm_P, dhm_P, P_len)) != 0 ||
2769 (ret = mbedtls_mpi_read_binary(&conf->dhm_G, dhm_G, G_len)) != 0) {
2770 mbedtls_mpi_free(&conf->dhm_P);
2771 mbedtls_mpi_free(&conf->dhm_G);
2772 return ret;
Hanno Beckera90658f2017-10-04 15:29:08 +01002773 }
2774
Gilles Peskine449bd832023-01-11 14:50:10 +01002775 return 0;
Hanno Beckera90658f2017-10-04 15:29:08 +01002776}
Paul Bakker5121ce52009-01-03 21:22:43 +00002777
Gilles Peskine449bd832023-01-11 14:50:10 +01002778int mbedtls_ssl_conf_dh_param_ctx(mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx)
Paul Bakker1b57b062011-01-06 15:48:19 +00002779{
Janos Follath865b3eb2019-12-16 11:46:15 +00002780 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1b57b062011-01-06 15:48:19 +00002781
Gilles Peskine449bd832023-01-11 14:50:10 +01002782 mbedtls_mpi_free(&conf->dhm_P);
2783 mbedtls_mpi_free(&conf->dhm_G);
Glenn Strausscee11292021-12-20 01:43:17 -05002784
Gilles Peskine449bd832023-01-11 14:50:10 +01002785 if ((ret = mbedtls_dhm_get_value(dhm_ctx, MBEDTLS_DHM_PARAM_P,
2786 &conf->dhm_P)) != 0 ||
2787 (ret = mbedtls_dhm_get_value(dhm_ctx, MBEDTLS_DHM_PARAM_G,
2788 &conf->dhm_G)) != 0) {
2789 mbedtls_mpi_free(&conf->dhm_P);
2790 mbedtls_mpi_free(&conf->dhm_G);
2791 return ret;
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01002792 }
Paul Bakker1b57b062011-01-06 15:48:19 +00002793
Gilles Peskine449bd832023-01-11 14:50:10 +01002794 return 0;
Paul Bakker1b57b062011-01-06 15:48:19 +00002795}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002796#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00002797
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02002798#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
2799/*
2800 * Set the minimum length for Diffie-Hellman parameters
2801 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002802void mbedtls_ssl_conf_dhm_min_bitlen(mbedtls_ssl_config *conf,
2803 unsigned int bitlen)
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02002804{
2805 conf->dhm_min_bitlen = bitlen;
2806}
2807#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
2808
Ronald Crone68ab4f2022-10-05 12:46:29 +02002809#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002810#if !defined(MBEDTLS_DEPRECATED_REMOVED) && defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002811/*
2812 * Set allowed/preferred hashes for handshake signatures
2813 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002814void mbedtls_ssl_conf_sig_hashes(mbedtls_ssl_config *conf,
2815 const int *hashes)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002816{
2817 conf->sig_hashes = hashes;
2818}
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002819#endif /* !MBEDTLS_DEPRECATED_REMOVED && MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker1cd6e002021-08-10 13:27:10 +01002820
Jerry Yuf017ee42022-01-12 15:49:48 +08002821/* Configure allowed signature algorithms for handshake */
Gilles Peskine449bd832023-01-11 14:50:10 +01002822void mbedtls_ssl_conf_sig_algs(mbedtls_ssl_config *conf,
2823 const uint16_t *sig_algs)
Hanno Becker1cd6e002021-08-10 13:27:10 +01002824{
Jerry Yuf017ee42022-01-12 15:49:48 +08002825#if !defined(MBEDTLS_DEPRECATED_REMOVED)
2826 conf->sig_hashes = NULL;
2827#endif /* !MBEDTLS_DEPRECATED_REMOVED */
2828 conf->sig_algs = sig_algs;
Hanno Becker1cd6e002021-08-10 13:27:10 +01002829}
Ronald Crone68ab4f2022-10-05 12:46:29 +02002830#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002831
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02002832#if defined(MBEDTLS_ECP_C)
Brett Warrene0edc842021-08-17 09:53:13 +01002833#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002834/*
2835 * Set the allowed elliptic curves
Brett Warrene0edc842021-08-17 09:53:13 +01002836 *
2837 * mbedtls_ssl_setup() takes the provided list
2838 * and translates it to a list of IANA TLS group identifiers,
2839 * stored in ssl->handshake->group_list.
2840 *
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002841 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002842void mbedtls_ssl_conf_curves(mbedtls_ssl_config *conf,
2843 const mbedtls_ecp_group_id *curve_list)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002844{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002845 conf->curve_list = curve_list;
Brett Warrene0edc842021-08-17 09:53:13 +01002846 conf->group_list = NULL;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002847}
Brett Warrene0edc842021-08-17 09:53:13 +01002848#endif /* MBEDTLS_DEPRECATED_REMOVED */
Hanno Becker947194e2017-04-07 13:25:49 +01002849#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002850
Brett Warrene0edc842021-08-17 09:53:13 +01002851/*
2852 * Set the allowed groups
2853 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002854void mbedtls_ssl_conf_groups(mbedtls_ssl_config *conf,
2855 const uint16_t *group_list)
Brett Warrene0edc842021-08-17 09:53:13 +01002856{
2857#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
2858 conf->curve_list = NULL;
2859#endif
2860 conf->group_list = group_list;
2861}
2862
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002863#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002864int mbedtls_ssl_set_hostname(mbedtls_ssl_context *ssl, const char *hostname)
Paul Bakker5121ce52009-01-03 21:22:43 +00002865{
Hanno Becker947194e2017-04-07 13:25:49 +01002866 /* Initialize to suppress unnecessary compiler warning */
2867 size_t hostname_len = 0;
2868
2869 /* Check if new hostname is valid before
2870 * making any change to current one */
Gilles Peskine449bd832023-01-11 14:50:10 +01002871 if (hostname != NULL) {
2872 hostname_len = strlen(hostname);
Hanno Becker947194e2017-04-07 13:25:49 +01002873
Gilles Peskine449bd832023-01-11 14:50:10 +01002874 if (hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN) {
2875 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2876 }
Hanno Becker947194e2017-04-07 13:25:49 +01002877 }
2878
2879 /* Now it's clear that we will overwrite the old hostname,
2880 * so we can free it safely */
2881
Gilles Peskine449bd832023-01-11 14:50:10 +01002882 if (ssl->hostname != NULL) {
2883 mbedtls_platform_zeroize(ssl->hostname, strlen(ssl->hostname));
2884 mbedtls_free(ssl->hostname);
Hanno Becker947194e2017-04-07 13:25:49 +01002885 }
2886
2887 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01002888
Gilles Peskine449bd832023-01-11 14:50:10 +01002889 if (hostname == NULL) {
Hanno Becker947194e2017-04-07 13:25:49 +01002890 ssl->hostname = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01002891 } else {
2892 ssl->hostname = mbedtls_calloc(1, hostname_len + 1);
2893 if (ssl->hostname == NULL) {
2894 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2895 }
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002896
Gilles Peskine449bd832023-01-11 14:50:10 +01002897 memcpy(ssl->hostname, hostname, hostname_len);
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002898
Hanno Becker947194e2017-04-07 13:25:49 +01002899 ssl->hostname[hostname_len] = '\0';
2900 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002901
Gilles Peskine449bd832023-01-11 14:50:10 +01002902 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002903}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01002904#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002905
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002906#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01002907void mbedtls_ssl_conf_sni(mbedtls_ssl_config *conf,
2908 int (*f_sni)(void *, mbedtls_ssl_context *,
2909 const unsigned char *, size_t),
2910 void *p_sni)
Paul Bakker5701cdc2012-09-27 21:49:42 +00002911{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002912 conf->f_sni = f_sni;
2913 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00002914}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002915#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00002916
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002917#if defined(MBEDTLS_SSL_ALPN)
Gilles Peskine449bd832023-01-11 14:50:10 +01002918int mbedtls_ssl_conf_alpn_protocols(mbedtls_ssl_config *conf, const char **protos)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002919{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002920 size_t cur_len, tot_len;
2921 const char **p;
2922
2923 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08002924 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
2925 * MUST NOT be truncated."
2926 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002927 */
2928 tot_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002929 for (p = protos; *p != NULL; p++) {
2930 cur_len = strlen(*p);
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002931 tot_len += cur_len;
2932
Gilles Peskine449bd832023-01-11 14:50:10 +01002933 if ((cur_len == 0) ||
2934 (cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN) ||
2935 (tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN)) {
2936 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2937 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002938 }
2939
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002940 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002941
Gilles Peskine449bd832023-01-11 14:50:10 +01002942 return 0;
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002943}
2944
Gilles Peskine449bd832023-01-11 14:50:10 +01002945const char *mbedtls_ssl_get_alpn_protocol(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002946{
Gilles Peskine449bd832023-01-11 14:50:10 +01002947 return ssl->alpn_chosen;
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002948}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002949#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002950
Johan Pascalb62bb512015-12-03 21:56:45 +01002951#if defined(MBEDTLS_SSL_DTLS_SRTP)
Gilles Peskine449bd832023-01-11 14:50:10 +01002952void mbedtls_ssl_conf_srtp_mki_value_supported(mbedtls_ssl_config *conf,
2953 int support_mki_value)
Ron Eldor591f1622018-01-22 12:30:04 +02002954{
2955 conf->dtls_srtp_mki_support = support_mki_value;
2956}
2957
Gilles Peskine449bd832023-01-11 14:50:10 +01002958int mbedtls_ssl_dtls_srtp_set_mki_value(mbedtls_ssl_context *ssl,
2959 unsigned char *mki_value,
2960 uint16_t mki_len)
Ron Eldor591f1622018-01-22 12:30:04 +02002961{
Gilles Peskine449bd832023-01-11 14:50:10 +01002962 if (mki_len > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH) {
2963 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Ron Eldora9788042018-12-05 11:04:31 +02002964 }
Ron Eldor591f1622018-01-22 12:30:04 +02002965
Gilles Peskine449bd832023-01-11 14:50:10 +01002966 if (ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_UNSUPPORTED) {
2967 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Ron Eldora9788042018-12-05 11:04:31 +02002968 }
Ron Eldor591f1622018-01-22 12:30:04 +02002969
Gilles Peskine449bd832023-01-11 14:50:10 +01002970 memcpy(ssl->dtls_srtp_info.mki_value, mki_value, mki_len);
Ron Eldor591f1622018-01-22 12:30:04 +02002971 ssl->dtls_srtp_info.mki_len = mki_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002972 return 0;
Ron Eldor591f1622018-01-22 12:30:04 +02002973}
2974
Gilles Peskine449bd832023-01-11 14:50:10 +01002975int mbedtls_ssl_conf_dtls_srtp_protection_profiles(mbedtls_ssl_config *conf,
2976 const mbedtls_ssl_srtp_profile *profiles)
Johan Pascalb62bb512015-12-03 21:56:45 +01002977{
Johan Pascal253d0262020-09-22 13:04:45 +02002978 const mbedtls_ssl_srtp_profile *p;
2979 size_t list_size = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01002980
Johan Pascal253d0262020-09-22 13:04:45 +02002981 /* check the profiles list: all entry must be valid,
2982 * its size cannot be more than the total number of supported profiles, currently 4 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002983 for (p = profiles; *p != MBEDTLS_TLS_SRTP_UNSET &&
2984 list_size <= MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH;
2985 p++) {
2986 if (mbedtls_ssl_check_srtp_profile_value(*p) != MBEDTLS_TLS_SRTP_UNSET) {
Johan Pascal76fdf1d2020-10-22 23:31:00 +02002987 list_size++;
Gilles Peskine449bd832023-01-11 14:50:10 +01002988 } else {
Johan Pascal76fdf1d2020-10-22 23:31:00 +02002989 /* unsupported value, stop parsing and set the size to an error value */
2990 list_size = MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH + 1;
Johan Pascalb62bb512015-12-03 21:56:45 +01002991 }
2992 }
2993
Gilles Peskine449bd832023-01-11 14:50:10 +01002994 if (list_size > MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH) {
2995 conf->dtls_srtp_profile_list = NULL;
2996 conf->dtls_srtp_profile_list_len = 0;
2997 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Johan Pascal253d0262020-09-22 13:04:45 +02002998 }
2999
Johan Pascal9bc97ca2020-09-21 23:44:45 +02003000 conf->dtls_srtp_profile_list = profiles;
Johan Pascal253d0262020-09-22 13:04:45 +02003001 conf->dtls_srtp_profile_list_len = list_size;
Johan Pascalb62bb512015-12-03 21:56:45 +01003002
Gilles Peskine449bd832023-01-11 14:50:10 +01003003 return 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01003004}
3005
Gilles Peskine449bd832023-01-11 14:50:10 +01003006void mbedtls_ssl_get_dtls_srtp_negotiation_result(const mbedtls_ssl_context *ssl,
3007 mbedtls_dtls_srtp_info *dtls_srtp_info)
Johan Pascalb62bb512015-12-03 21:56:45 +01003008{
Johan Pascal2258a4f2020-10-28 13:53:09 +01003009 dtls_srtp_info->chosen_dtls_srtp_profile = ssl->dtls_srtp_info.chosen_dtls_srtp_profile;
3010 /* do not copy the mki value if there is no chosen profile */
Gilles Peskine449bd832023-01-11 14:50:10 +01003011 if (dtls_srtp_info->chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET) {
Johan Pascal2258a4f2020-10-28 13:53:09 +01003012 dtls_srtp_info->mki_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003013 } else {
Johan Pascal2258a4f2020-10-28 13:53:09 +01003014 dtls_srtp_info->mki_len = ssl->dtls_srtp_info.mki_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01003015 memcpy(dtls_srtp_info->mki_value, ssl->dtls_srtp_info.mki_value,
3016 ssl->dtls_srtp_info.mki_len);
Johan Pascal2258a4f2020-10-28 13:53:09 +01003017 }
Johan Pascalb62bb512015-12-03 21:56:45 +01003018}
Johan Pascalb62bb512015-12-03 21:56:45 +01003019#endif /* MBEDTLS_SSL_DTLS_SRTP */
3020
Aditya Patwardhan3096f332022-07-26 14:31:46 +05303021#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003022void mbedtls_ssl_conf_max_version(mbedtls_ssl_config *conf, int major, int minor)
Paul Bakker490ecc82011-10-06 13:04:09 +00003023{
Glenn Strauss2dfcea22022-03-14 17:26:42 -04003024 conf->max_tls_version = (major << 8) | minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00003025}
3026
Gilles Peskine449bd832023-01-11 14:50:10 +01003027void mbedtls_ssl_conf_min_version(mbedtls_ssl_config *conf, int major, int minor)
Paul Bakker1d29fb52012-09-28 13:28:45 +00003028{
Glenn Strauss2dfcea22022-03-14 17:26:42 -04003029 conf->min_tls_version = (major << 8) | minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00003030}
Aditya Patwardhan3096f332022-07-26 14:31:46 +05303031#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker1d29fb52012-09-28 13:28:45 +00003032
Janos Follath088ce432017-04-10 12:42:31 +01003033#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003034void mbedtls_ssl_conf_cert_req_ca_list(mbedtls_ssl_config *conf,
3035 char cert_req_ca_list)
Janos Follath088ce432017-04-10 12:42:31 +01003036{
3037 conf->cert_req_ca_list = cert_req_ca_list;
3038}
3039#endif
3040
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003041#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01003042void mbedtls_ssl_conf_encrypt_then_mac(mbedtls_ssl_config *conf, char etm)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01003043{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003044 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01003045}
3046#endif
3047
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003048#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Gilles Peskine449bd832023-01-11 14:50:10 +01003049void mbedtls_ssl_conf_extended_master_secret(mbedtls_ssl_config *conf, char ems)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02003050{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003051 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02003052}
3053#endif
3054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003055#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01003056int mbedtls_ssl_conf_max_frag_len(mbedtls_ssl_config *conf, unsigned char mfl_code)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003057{
Gilles Peskine449bd832023-01-11 14:50:10 +01003058 if (mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
3059 ssl_mfl_code_to_length(mfl_code) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN) {
3060 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003061 }
3062
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01003063 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003064
Gilles Peskine449bd832023-01-11 14:50:10 +01003065 return 0;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003066}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003067#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003068
Gilles Peskine449bd832023-01-11 14:50:10 +01003069void mbedtls_ssl_conf_legacy_renegotiation(mbedtls_ssl_config *conf, int allow_legacy)
Paul Bakker48916f92012-09-16 19:57:18 +00003070{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003071 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00003072}
3073
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003074#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01003075void mbedtls_ssl_conf_renegotiation(mbedtls_ssl_config *conf, int renegotiation)
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003076{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003077 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003078}
3079
Gilles Peskine449bd832023-01-11 14:50:10 +01003080void mbedtls_ssl_conf_renegotiation_enforced(mbedtls_ssl_config *conf, int max_records)
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003081{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003082 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003083}
3084
Gilles Peskine449bd832023-01-11 14:50:10 +01003085void mbedtls_ssl_conf_renegotiation_period(mbedtls_ssl_config *conf,
3086 const unsigned char period[8])
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01003087{
Gilles Peskine449bd832023-01-11 14:50:10 +01003088 memcpy(conf->renego_period, period, 8);
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01003089}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003090#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00003091
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003092#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003093#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003094void mbedtls_ssl_conf_session_tickets(mbedtls_ssl_config *conf, int use_tickets)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003095{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01003096 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003097}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003098#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02003099
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003100#if defined(MBEDTLS_SSL_SRV_C)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08003101
Jerry Yud0766ec2022-09-22 10:46:57 +08003102#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003103void mbedtls_ssl_conf_new_session_tickets(mbedtls_ssl_config *conf,
3104 uint16_t num_tickets)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08003105{
Jerry Yud0766ec2022-09-22 10:46:57 +08003106 conf->new_session_tickets_count = num_tickets;
Jerry Yu1ad7ace2022-08-09 13:28:39 +08003107}
3108#endif
3109
Gilles Peskine449bd832023-01-11 14:50:10 +01003110void mbedtls_ssl_conf_session_tickets_cb(mbedtls_ssl_config *conf,
3111 mbedtls_ssl_ticket_write_t *f_ticket_write,
3112 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
3113 void *p_ticket)
Paul Bakker606b4ba2013-08-14 16:52:14 +02003114{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02003115 conf->f_ticket_write = f_ticket_write;
3116 conf->f_ticket_parse = f_ticket_parse;
3117 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02003118}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003119#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003120#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003121
Gilles Peskine449bd832023-01-11 14:50:10 +01003122void mbedtls_ssl_set_export_keys_cb(mbedtls_ssl_context *ssl,
3123 mbedtls_ssl_export_keys_t *f_export_keys,
3124 void *p_export_keys)
Robert Cragie4feb7ae2015-10-02 13:33:37 +01003125{
Hanno Becker7e6c1782021-06-08 09:24:55 +01003126 ssl->f_export_keys = f_export_keys;
3127 ssl->p_export_keys = p_export_keys;
Ron Eldorf5cc10d2019-05-07 18:33:40 +03003128}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01003129
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003130#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003131void mbedtls_ssl_conf_async_private_cb(
3132 mbedtls_ssl_config *conf,
3133 mbedtls_ssl_async_sign_t *f_async_sign,
3134 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
3135 mbedtls_ssl_async_resume_t *f_async_resume,
3136 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskine449bd832023-01-11 14:50:10 +01003137 void *async_config_data)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003138{
3139 conf->f_async_sign_start = f_async_sign;
3140 conf->f_async_decrypt_start = f_async_decrypt;
3141 conf->f_async_resume = f_async_resume;
3142 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003143 conf->p_async_config_data = async_config_data;
3144}
3145
Gilles Peskine449bd832023-01-11 14:50:10 +01003146void *mbedtls_ssl_conf_get_async_config_data(const mbedtls_ssl_config *conf)
Gilles Peskine8f97af72018-04-26 11:46:10 +02003147{
Gilles Peskine449bd832023-01-11 14:50:10 +01003148 return conf->p_async_config_data;
Gilles Peskine8f97af72018-04-26 11:46:10 +02003149}
3150
Gilles Peskine449bd832023-01-11 14:50:10 +01003151void *mbedtls_ssl_get_async_operation_data(const mbedtls_ssl_context *ssl)
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003152{
Gilles Peskine449bd832023-01-11 14:50:10 +01003153 if (ssl->handshake == NULL) {
3154 return NULL;
3155 } else {
3156 return ssl->handshake->user_async_ctx;
3157 }
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003158}
3159
Gilles Peskine449bd832023-01-11 14:50:10 +01003160void mbedtls_ssl_set_async_operation_data(mbedtls_ssl_context *ssl,
3161 void *ctx)
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003162{
Gilles Peskine449bd832023-01-11 14:50:10 +01003163 if (ssl->handshake != NULL) {
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003164 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +01003165 }
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003166}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003167#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003168
Paul Bakker5121ce52009-01-03 21:22:43 +00003169/*
3170 * SSL get accessors
3171 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003172uint32_t mbedtls_ssl_get_verify_result(const mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00003173{
Gilles Peskine449bd832023-01-11 14:50:10 +01003174 if (ssl->session != NULL) {
3175 return ssl->session->verify_result;
3176 }
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00003177
Gilles Peskine449bd832023-01-11 14:50:10 +01003178 if (ssl->session_negotiate != NULL) {
3179 return ssl->session_negotiate->verify_result;
3180 }
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00003181
Gilles Peskine449bd832023-01-11 14:50:10 +01003182 return 0xFFFFFFFF;
Paul Bakker5121ce52009-01-03 21:22:43 +00003183}
3184
Gilles Peskine449bd832023-01-11 14:50:10 +01003185int mbedtls_ssl_get_ciphersuite_id_from_ssl(const mbedtls_ssl_context *ssl)
Glenn Strauss8f526902022-01-13 00:04:49 -05003186{
Gilles Peskine449bd832023-01-11 14:50:10 +01003187 if (ssl == NULL || ssl->session == NULL) {
3188 return 0;
3189 }
Glenn Strauss8f526902022-01-13 00:04:49 -05003190
Gilles Peskine449bd832023-01-11 14:50:10 +01003191 return ssl->session->ciphersuite;
Glenn Strauss8f526902022-01-13 00:04:49 -05003192}
3193
Gilles Peskine449bd832023-01-11 14:50:10 +01003194const char *mbedtls_ssl_get_ciphersuite(const mbedtls_ssl_context *ssl)
Paul Bakker72f62662011-01-16 21:27:44 +00003195{
Gilles Peskine449bd832023-01-11 14:50:10 +01003196 if (ssl == NULL || ssl->session == NULL) {
3197 return NULL;
3198 }
Paul Bakker926c8e42013-03-06 10:23:34 +01003199
Gilles Peskine449bd832023-01-11 14:50:10 +01003200 return mbedtls_ssl_get_ciphersuite_name(ssl->session->ciphersuite);
Paul Bakker72f62662011-01-16 21:27:44 +00003201}
3202
Gilles Peskine449bd832023-01-11 14:50:10 +01003203const char *mbedtls_ssl_get_version(const mbedtls_ssl_context *ssl)
Paul Bakker43ca69c2011-01-15 17:35:19 +00003204{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003205#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003206 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
3207 switch (ssl->tls_version) {
Glenn Strauss60bfe602022-03-14 19:04:24 -04003208 case MBEDTLS_SSL_VERSION_TLS1_2:
Gilles Peskine449bd832023-01-11 14:50:10 +01003209 return "DTLSv1.2";
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003210 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01003211 return "unknown (DTLS)";
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003212 }
3213 }
3214#endif
3215
Gilles Peskine449bd832023-01-11 14:50:10 +01003216 switch (ssl->tls_version) {
Glenn Strauss60bfe602022-03-14 19:04:24 -04003217 case MBEDTLS_SSL_VERSION_TLS1_2:
Gilles Peskine449bd832023-01-11 14:50:10 +01003218 return "TLSv1.2";
Glenn Strauss60bfe602022-03-14 19:04:24 -04003219 case MBEDTLS_SSL_VERSION_TLS1_3:
Gilles Peskine449bd832023-01-11 14:50:10 +01003220 return "TLSv1.3";
Paul Bakker43ca69c2011-01-15 17:35:19 +00003221 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01003222 return "unknown";
Paul Bakker43ca69c2011-01-15 17:35:19 +00003223 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00003224}
3225
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003226#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01003227size_t mbedtls_ssl_get_input_max_frag_len(const mbedtls_ssl_context *ssl)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003228{
David Horstmann95d516f2021-05-04 18:36:56 +01003229 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003230 size_t read_mfl;
3231
Jerry Yuddda0502022-12-01 19:43:12 +08003232#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003233 /* Use the configured MFL for the client if we're past SERVER_HELLO_DONE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003234 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
3235 ssl->state >= MBEDTLS_SSL_SERVER_HELLO_DONE) {
3236 return ssl_mfl_code_to_length(ssl->conf->mfl_code);
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003237 }
Jerry Yuddda0502022-12-01 19:43:12 +08003238#endif
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003239
3240 /* Check if a smaller max length was negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01003241 if (ssl->session_out != NULL) {
3242 read_mfl = ssl_mfl_code_to_length(ssl->session_out->mfl_code);
3243 if (read_mfl < max_len) {
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003244 max_len = read_mfl;
3245 }
3246 }
3247
Jerry Yuddda0502022-12-01 19:43:12 +08003248 /* During a handshake, use the value being negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01003249 if (ssl->session_negotiate != NULL) {
3250 read_mfl = ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code);
3251 if (read_mfl < max_len) {
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003252 max_len = read_mfl;
3253 }
3254 }
3255
Gilles Peskine449bd832023-01-11 14:50:10 +01003256 return max_len;
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003257}
3258
Gilles Peskine449bd832023-01-11 14:50:10 +01003259size_t mbedtls_ssl_get_output_max_frag_len(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003260{
3261 size_t max_len;
3262
3263 /*
3264 * Assume mfl_code is correct since it was checked when set
3265 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003266 max_len = ssl_mfl_code_to_length(ssl->conf->mfl_code);
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003267
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02003268 /* Check if a smaller max length was negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01003269 if (ssl->session_out != NULL &&
3270 ssl_mfl_code_to_length(ssl->session_out->mfl_code) < max_len) {
3271 max_len = ssl_mfl_code_to_length(ssl->session_out->mfl_code);
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003272 }
3273
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02003274 /* During a handshake, use the value being negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01003275 if (ssl->session_negotiate != NULL &&
3276 ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code) < max_len) {
3277 max_len = ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code);
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02003278 }
3279
Gilles Peskine449bd832023-01-11 14:50:10 +01003280 return max_len;
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003281}
3282#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
3283
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003284#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003285size_t mbedtls_ssl_get_current_mtu(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003286{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04003287 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003288 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
3289 (ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
3290 ssl->state == MBEDTLS_SSL_SERVER_HELLO)) {
3291 return 0;
3292 }
Andrzej Kurekef43ce62018-10-09 08:24:12 -04003293
Gilles Peskine449bd832023-01-11 14:50:10 +01003294 if (ssl->handshake == NULL || ssl->handshake->mtu == 0) {
3295 return ssl->mtu;
3296 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003297
Gilles Peskine449bd832023-01-11 14:50:10 +01003298 if (ssl->mtu == 0) {
3299 return ssl->handshake->mtu;
3300 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003301
Gilles Peskine449bd832023-01-11 14:50:10 +01003302 return ssl->mtu < ssl->handshake->mtu ?
3303 ssl->mtu : ssl->handshake->mtu;
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003304}
3305#endif /* MBEDTLS_SSL_PROTO_DTLS */
3306
Gilles Peskine449bd832023-01-11 14:50:10 +01003307int mbedtls_ssl_get_max_out_record_payload(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003308{
3309 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
3310
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02003311#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
3312 !defined(MBEDTLS_SSL_PROTO_DTLS)
3313 (void) ssl;
3314#endif
3315
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003316#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01003317 const size_t mfl = mbedtls_ssl_get_output_max_frag_len(ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003318
Gilles Peskine449bd832023-01-11 14:50:10 +01003319 if (max_len > mfl) {
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003320 max_len = mfl;
Gilles Peskine449bd832023-01-11 14:50:10 +01003321 }
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003322#endif
3323
3324#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003325 if (mbedtls_ssl_get_current_mtu(ssl) != 0) {
3326 const size_t mtu = mbedtls_ssl_get_current_mtu(ssl);
3327 const int ret = mbedtls_ssl_get_record_expansion(ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003328 const size_t overhead = (size_t) ret;
3329
Gilles Peskine449bd832023-01-11 14:50:10 +01003330 if (ret < 0) {
3331 return ret;
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003332 }
3333
Gilles Peskine449bd832023-01-11 14:50:10 +01003334 if (mtu <= overhead) {
3335 MBEDTLS_SSL_DEBUG_MSG(1, ("MTU too low for record expansion"));
3336 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
3337 }
3338
3339 if (max_len > mtu - overhead) {
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003340 max_len = mtu - overhead;
Gilles Peskine449bd832023-01-11 14:50:10 +01003341 }
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003342 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003343#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003344
Hanno Becker0defedb2018-08-10 12:35:02 +01003345#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
3346 !defined(MBEDTLS_SSL_PROTO_DTLS)
3347 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003348#endif
3349
Gilles Peskine449bd832023-01-11 14:50:10 +01003350 return (int) max_len;
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003351}
3352
Gilles Peskine449bd832023-01-11 14:50:10 +01003353int mbedtls_ssl_get_max_in_record_payload(const mbedtls_ssl_context *ssl)
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003354{
3355 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
3356
3357#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
3358 (void) ssl;
3359#endif
3360
3361#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01003362 const size_t mfl = mbedtls_ssl_get_input_max_frag_len(ssl);
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003363
Gilles Peskine449bd832023-01-11 14:50:10 +01003364 if (max_len > mfl) {
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003365 max_len = mfl;
Gilles Peskine449bd832023-01-11 14:50:10 +01003366 }
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003367#endif
3368
Gilles Peskine449bd832023-01-11 14:50:10 +01003369 return (int) max_len;
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003370}
3371
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003372#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003373const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert(const mbedtls_ssl_context *ssl)
Paul Bakkerb0550d92012-10-30 07:51:03 +00003374{
Gilles Peskine449bd832023-01-11 14:50:10 +01003375 if (ssl == NULL || ssl->session == NULL) {
3376 return NULL;
3377 }
Paul Bakkerb0550d92012-10-30 07:51:03 +00003378
Hanno Beckere6824572019-02-07 13:18:46 +00003379#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01003380 return ssl->session->peer_cert;
Hanno Beckere6824572019-02-07 13:18:46 +00003381#else
Gilles Peskine449bd832023-01-11 14:50:10 +01003382 return NULL;
Hanno Beckere6824572019-02-07 13:18:46 +00003383#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00003384}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003385#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00003386
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003387#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003388int mbedtls_ssl_get_session(const mbedtls_ssl_context *ssl,
3389 mbedtls_ssl_session *dst)
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003390{
Hanno Beckere810bbc2021-05-14 16:01:05 +01003391 int ret;
3392
Gilles Peskine449bd832023-01-11 14:50:10 +01003393 if (ssl == NULL ||
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003394 dst == NULL ||
3395 ssl->session == NULL ||
Gilles Peskine449bd832023-01-11 14:50:10 +01003396 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT) {
3397 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003398 }
3399
Hanno Beckere810bbc2021-05-14 16:01:05 +01003400 /* Since Mbed TLS 3.0, mbedtls_ssl_get_session() is no longer
3401 * idempotent: Each session can only be exported once.
3402 *
3403 * (This is in preparation for TLS 1.3 support where we will
3404 * need the ability to export multiple sessions (aka tickets),
3405 * which will be achieved by calling mbedtls_ssl_get_session()
3406 * multiple times until it fails.)
3407 *
3408 * Check whether we have already exported the current session,
3409 * and fail if so.
3410 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003411 if (ssl->session->exported == 1) {
3412 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
3413 }
Hanno Beckere810bbc2021-05-14 16:01:05 +01003414
Gilles Peskine449bd832023-01-11 14:50:10 +01003415 ret = mbedtls_ssl_session_copy(dst, ssl->session);
3416 if (ret != 0) {
3417 return ret;
3418 }
Hanno Beckere810bbc2021-05-14 16:01:05 +01003419
3420 /* Remember that we've exported the session. */
3421 ssl->session->exported = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003422 return 0;
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003423}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003424#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003425
Paul Bakker5121ce52009-01-03 21:22:43 +00003426/*
Hanno Beckera835da52019-05-16 12:39:07 +01003427 * Define ticket header determining Mbed TLS version
3428 * and structure of the ticket.
3429 */
3430
Hanno Becker94ef3b32019-05-16 12:50:45 +01003431/*
Hanno Becker50b59662019-05-28 14:30:45 +01003432 * Define bitflag determining compile-time settings influencing
3433 * structure of serialized SSL sessions.
Hanno Becker94ef3b32019-05-16 12:50:45 +01003434 */
3435
Hanno Becker50b59662019-05-28 14:30:45 +01003436#if defined(MBEDTLS_HAVE_TIME)
Hanno Becker3e088662019-05-29 11:10:18 +01003437#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker50b59662019-05-28 14:30:45 +01003438#else
Hanno Becker3e088662019-05-29 11:10:18 +01003439#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003440#endif /* MBEDTLS_HAVE_TIME */
3441
3442#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker3e088662019-05-29 11:10:18 +01003443#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003444#else
Hanno Becker3e088662019-05-29 11:10:18 +01003445#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003446#endif /* MBEDTLS_X509_CRT_PARSE_C */
3447
3448#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Becker3e088662019-05-29 11:10:18 +01003449#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003450#else
Hanno Becker3e088662019-05-29 11:10:18 +01003451#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003452#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
3453
3454#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Becker3e088662019-05-29 11:10:18 +01003455#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003456#else
Hanno Becker3e088662019-05-29 11:10:18 +01003457#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003458#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
3459
Hanno Becker94ef3b32019-05-16 12:50:45 +01003460#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3e088662019-05-29 11:10:18 +01003461#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003462#else
Hanno Becker3e088662019-05-29 11:10:18 +01003463#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003464#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
3465
Hanno Becker94ef3b32019-05-16 12:50:45 +01003466#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3467#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
3468#else
3469#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
3470#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3471
Hanno Becker3e088662019-05-29 11:10:18 +01003472#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
3473#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
3474#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
3475#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
Hanno Becker37bdbe62021-08-01 05:38:58 +01003476#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 4
3477#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 5
Hanno Becker3e088662019-05-29 11:10:18 +01003478
Hanno Becker50b59662019-05-28 14:30:45 +01003479#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Gilles Peskine449bd832023-01-11 14:50:10 +01003480 ((uint16_t) ( \
3481 (SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT) | \
3482 (SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT) | \
3483 (SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << \
3484 SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT) | \
3485 (SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT) | \
3486 (SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT) | \
3487 (SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT)))
Hanno Becker94ef3b32019-05-16 12:50:45 +01003488
Hanno Beckerf8787072019-05-16 12:41:07 +01003489static unsigned char ssl_serialized_session_header[] = {
Hanno Becker94ef3b32019-05-16 12:50:45 +01003490 MBEDTLS_VERSION_MAJOR,
3491 MBEDTLS_VERSION_MINOR,
3492 MBEDTLS_VERSION_PATCH,
Gilles Peskine449bd832023-01-11 14:50:10 +01003493 MBEDTLS_BYTE_1(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
3494 MBEDTLS_BYTE_0(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
Hanno Beckerf8787072019-05-16 12:41:07 +01003495};
Hanno Beckera835da52019-05-16 12:39:07 +01003496
3497/*
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003498 * Serialize a session in the following format:
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02003499 * (in the presentation language of TLS, RFC 8446 section 3)
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003500 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003501 * struct {
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01003502 *
Hanno Beckerdce50972021-08-01 05:39:23 +01003503 * opaque mbedtls_version[3]; // library version: major, minor, patch
3504 * opaque session_format[2]; // library-version specific 16-bit field
3505 * // determining the format of the remaining
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003506 * // serialized data.
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01003507 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003508 * Note: When updating the format, remember to keep
3509 * these version+format bytes.
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003510 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003511 * // In this version, `session_format` determines
3512 * // the setting of those compile-time
3513 * // configuration options which influence
3514 * // the structure of mbedtls_ssl_session.
3515 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003516 * uint8_t minor_ver; // Protocol minor version. Possible values:
Jerry Yu0c2a7382022-12-06 13:27:25 +08003517 * // - TLS 1.2 (0x0303)
3518 * // - TLS 1.3 (0x0304)
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003519 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003520 * select (serialized_session.tls_version) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003521 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003522 * case MBEDTLS_SSL_VERSION_TLS1_2:
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003523 * serialized_session_tls12 data;
Jerry Yu0c2a7382022-12-06 13:27:25 +08003524 * case MBEDTLS_SSL_VERSION_TLS1_3:
Jerry Yuddda0502022-12-01 19:43:12 +08003525 * serialized_session_tls13 data;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003526 *
3527 * };
3528 *
3529 * } serialized_session;
3530 *
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003531 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003532
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003533MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003534static int ssl_session_save(const mbedtls_ssl_session *session,
3535 unsigned char omit_header,
3536 unsigned char *buf,
3537 size_t buf_len,
3538 size_t *olen)
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003539{
3540 unsigned char *p = buf;
3541 size_t used = 0;
Jerry Yu251a12e2022-07-13 15:15:48 +08003542 size_t remaining_len;
Jerry Yue36fdd62022-08-17 21:31:36 +08003543#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3544 size_t out_len;
3545 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3546#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01003547 if (session == NULL) {
3548 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
3549 }
Jerry Yu438ddd82022-07-07 06:55:50 +00003550
Gilles Peskine449bd832023-01-11 14:50:10 +01003551 if (!omit_header) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003552 /*
3553 * Add Mbed TLS version identifier
3554 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003555 used += sizeof(ssl_serialized_session_header);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003556
Gilles Peskine449bd832023-01-11 14:50:10 +01003557 if (used <= buf_len) {
3558 memcpy(p, ssl_serialized_session_header,
3559 sizeof(ssl_serialized_session_header));
3560 p += sizeof(ssl_serialized_session_header);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003561 }
3562 }
3563
3564 /*
3565 * TLS version identifier
3566 */
3567 used += 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003568 if (used <= buf_len) {
3569 *p++ = MBEDTLS_BYTE_0(session->tls_version);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003570 }
3571
3572 /* Forward to version-specific serialization routine. */
Jerry Yufca4d572022-07-21 10:37:48 +08003573 remaining_len = (buf_len >= used) ? buf_len - used : 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003574 switch (session->tls_version) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003575#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01003576 case MBEDTLS_SSL_VERSION_TLS1_2:
3577 used += ssl_tls12_session_save(session, p, remaining_len);
3578 break;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003579#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3580
Jerry Yu251a12e2022-07-13 15:15:48 +08003581#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01003582 case MBEDTLS_SSL_VERSION_TLS1_3:
3583 ret = ssl_tls13_session_save(session, p, remaining_len, &out_len);
3584 if (ret != 0 && ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL) {
3585 return ret;
3586 }
3587 used += out_len;
3588 break;
Jerry Yu251a12e2022-07-13 15:15:48 +08003589#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3590
Gilles Peskine449bd832023-01-11 14:50:10 +01003591 default:
3592 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003593 }
3594
3595 *olen = used;
Gilles Peskine449bd832023-01-11 14:50:10 +01003596 if (used > buf_len) {
3597 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
3598 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003599
Gilles Peskine449bd832023-01-11 14:50:10 +01003600 return 0;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003601}
3602
3603/*
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003604 * Public wrapper for ssl_session_save()
3605 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003606int mbedtls_ssl_session_save(const mbedtls_ssl_session *session,
3607 unsigned char *buf,
3608 size_t buf_len,
3609 size_t *olen)
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003610{
Gilles Peskine449bd832023-01-11 14:50:10 +01003611 return ssl_session_save(session, 0, buf, buf_len, olen);
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003612}
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003613
Jerry Yuf1b23ca2022-02-18 11:48:47 +08003614/*
3615 * Deserialize session, see mbedtls_ssl_session_save() for format.
3616 *
3617 * This internal version is wrapped by a public function that cleans up in
3618 * case of error, and has an extra option omit_header.
3619 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003620MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003621static int ssl_session_load(mbedtls_ssl_session *session,
3622 unsigned char omit_header,
3623 const unsigned char *buf,
3624 size_t len)
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003625{
3626 const unsigned char *p = buf;
3627 const unsigned char * const end = buf + len;
Jerry Yu438ddd82022-07-07 06:55:50 +00003628 size_t remaining_len;
3629
3630
Gilles Peskine449bd832023-01-11 14:50:10 +01003631 if (session == NULL) {
3632 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
3633 }
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003634
Gilles Peskine449bd832023-01-11 14:50:10 +01003635 if (!omit_header) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003636 /*
3637 * Check Mbed TLS version identifier
3638 */
3639
Gilles Peskine449bd832023-01-11 14:50:10 +01003640 if ((size_t) (end - p) < sizeof(ssl_serialized_session_header)) {
3641 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003642 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003643
3644 if (memcmp(p, ssl_serialized_session_header,
3645 sizeof(ssl_serialized_session_header)) != 0) {
3646 return MBEDTLS_ERR_SSL_VERSION_MISMATCH;
3647 }
3648 p += sizeof(ssl_serialized_session_header);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003649 }
3650
3651 /*
3652 * TLS version identifier
3653 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003654 if (1 > (size_t) (end - p)) {
3655 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3656 }
Glenn Straussda7851c2022-03-14 13:29:48 -04003657 session->tls_version = 0x0300 | *p++;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003658
3659 /* Dispatch according to TLS version. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003660 remaining_len = (end - p);
3661 switch (session->tls_version) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003662#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01003663 case MBEDTLS_SSL_VERSION_TLS1_2:
3664 return ssl_tls12_session_load(session, p, remaining_len);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003665#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3666
Jerry Yu438ddd82022-07-07 06:55:50 +00003667#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01003668 case MBEDTLS_SSL_VERSION_TLS1_3:
3669 return ssl_tls13_session_load(session, p, remaining_len);
Jerry Yu438ddd82022-07-07 06:55:50 +00003670#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3671
Gilles Peskine449bd832023-01-11 14:50:10 +01003672 default:
3673 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003674 }
3675}
3676
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003677/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02003678 * Deserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003679 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003680int mbedtls_ssl_session_load(mbedtls_ssl_session *session,
3681 const unsigned char *buf,
3682 size_t len)
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003683{
Gilles Peskine449bd832023-01-11 14:50:10 +01003684 int ret = ssl_session_load(session, 0, buf, len);
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003685
Gilles Peskine449bd832023-01-11 14:50:10 +01003686 if (ret != 0) {
3687 mbedtls_ssl_session_free(session);
3688 }
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003689
Gilles Peskine449bd832023-01-11 14:50:10 +01003690 return ret;
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003691}
3692
3693/*
Paul Bakker1961b702013-01-25 14:49:24 +01003694 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00003695 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003696MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003697static int ssl_prepare_handshake_step(mbedtls_ssl_context *ssl)
Hanno Becker41934dd2021-08-07 19:13:43 +01003698{
3699 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3700
Ronald Cron66dbf912022-02-02 15:33:46 +01003701 /*
3702 * We may have not been able to send to the peer all the handshake data
Ronald Cron3f20b772022-03-08 16:00:02 +01003703 * that were written into the output buffer by the previous handshake step,
3704 * if the write to the network callback returned with the
Ronald Cron66dbf912022-02-02 15:33:46 +01003705 * #MBEDTLS_ERR_SSL_WANT_WRITE error code.
3706 * We proceed to the next handshake step only when all data from the
3707 * previous one have been sent to the peer, thus we make sure that this is
3708 * the case here by calling `mbedtls_ssl_flush_output()`. The function may
3709 * return with the #MBEDTLS_ERR_SSL_WANT_WRITE error code in which case
3710 * we have to wait before to go ahead.
3711 * In the case of TLS 1.3, handshake step handlers do not send data to the
3712 * peer. Data are only sent here and through
3713 * `mbedtls_ssl_handle_pending_alert` in case an error that triggered an
Andrzej Kurek5c65c572022-04-13 14:28:52 -04003714 * alert occurred.
Ronald Cron66dbf912022-02-02 15:33:46 +01003715 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003716 if ((ret = mbedtls_ssl_flush_output(ssl)) != 0) {
3717 return ret;
3718 }
Hanno Becker41934dd2021-08-07 19:13:43 +01003719
3720#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003721 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3722 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING) {
3723 if ((ret = mbedtls_ssl_flight_transmit(ssl)) != 0) {
3724 return ret;
3725 }
Hanno Becker41934dd2021-08-07 19:13:43 +01003726 }
3727#endif /* MBEDTLS_SSL_PROTO_DTLS */
3728
Gilles Peskine449bd832023-01-11 14:50:10 +01003729 return ret;
Hanno Becker41934dd2021-08-07 19:13:43 +01003730}
3731
Gilles Peskine449bd832023-01-11 14:50:10 +01003732int mbedtls_ssl_handshake_step(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00003733{
Hanno Becker41934dd2021-08-07 19:13:43 +01003734 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003735
Gilles Peskine449bd832023-01-11 14:50:10 +01003736 if (ssl == NULL ||
Hanno Becker41934dd2021-08-07 19:13:43 +01003737 ssl->conf == NULL ||
3738 ssl->handshake == NULL ||
Gilles Peskine449bd832023-01-11 14:50:10 +01003739 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER) {
3740 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Becker41934dd2021-08-07 19:13:43 +01003741 }
3742
Gilles Peskine449bd832023-01-11 14:50:10 +01003743 ret = ssl_prepare_handshake_step(ssl);
3744 if (ret != 0) {
3745 return ret;
3746 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003747
Gilles Peskine449bd832023-01-11 14:50:10 +01003748 ret = mbedtls_ssl_handle_pending_alert(ssl);
3749 if (ret != 0) {
Jerry Yue7047812021-09-13 19:26:39 +08003750 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01003751 }
Jerry Yue7047812021-09-13 19:26:39 +08003752
Tom Cosgrove2fdc7b32022-09-21 12:33:17 +01003753 /* If ssl->conf->endpoint is not one of MBEDTLS_SSL_IS_CLIENT or
3754 * MBEDTLS_SSL_IS_SERVER, this is the return code we give */
3755 ret = MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3756
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003757#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003758 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
3759 MBEDTLS_SSL_DEBUG_MSG(2, ("client state: %s",
3760 mbedtls_ssl_states_str(ssl->state)));
Jerry Yub9930e72021-08-06 17:11:51 +08003761
Gilles Peskine449bd832023-01-11 14:50:10 +01003762 switch (ssl->state) {
Ronald Cron9f0fba32022-02-10 16:45:15 +01003763 case MBEDTLS_SSL_HELLO_REQUEST:
3764 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
Tom Cosgrove87d9c6c2022-09-22 09:27:56 +01003765 ret = 0;
Ronald Cron9f0fba32022-02-10 16:45:15 +01003766 break;
Jerry Yub9930e72021-08-06 17:11:51 +08003767
Ronald Cron9f0fba32022-02-10 16:45:15 +01003768 case MBEDTLS_SSL_CLIENT_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01003769 ret = mbedtls_ssl_write_client_hello(ssl);
Ronald Cron9f0fba32022-02-10 16:45:15 +01003770 break;
3771
3772 default:
3773#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01003774 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
3775 ret = mbedtls_ssl_tls13_handshake_client_step(ssl);
3776 } else {
3777 ret = mbedtls_ssl_handshake_client_step(ssl);
3778 }
Ronald Cron9f0fba32022-02-10 16:45:15 +01003779#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01003780 ret = mbedtls_ssl_handshake_client_step(ssl);
Ronald Cron9f0fba32022-02-10 16:45:15 +01003781#else
Gilles Peskine449bd832023-01-11 14:50:10 +01003782 ret = mbedtls_ssl_tls13_handshake_client_step(ssl);
Ronald Cron9f0fba32022-02-10 16:45:15 +01003783#endif
3784 }
Jerry Yub9930e72021-08-06 17:11:51 +08003785 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003786#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003787#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003788 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Ronald Cron6f135e12021-12-08 16:57:54 +01003789#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01003790 if (mbedtls_ssl_conf_is_tls13_only(ssl->conf)) {
3791 ret = mbedtls_ssl_tls13_handshake_server_step(ssl);
3792 }
Ronald Cron6f135e12021-12-08 16:57:54 +01003793#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yub9930e72021-08-06 17:11:51 +08003794
3795#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01003796 if (mbedtls_ssl_conf_is_tls12_only(ssl->conf)) {
3797 ret = mbedtls_ssl_handshake_server_step(ssl);
3798 }
Jerry Yub9930e72021-08-06 17:11:51 +08003799#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3800 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003801#endif
3802
Gilles Peskine449bd832023-01-11 14:50:10 +01003803 if (ret != 0) {
Jerry Yubbd5a3f2021-09-18 20:50:22 +08003804 /* handshake_step return error. And it is same
3805 * with alert_reason.
3806 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003807 if (ssl->send_alert) {
3808 ret = mbedtls_ssl_handle_pending_alert(ssl);
Jerry Yue7047812021-09-13 19:26:39 +08003809 goto cleanup;
3810 }
3811 }
3812
3813cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01003814 return ret;
Paul Bakker1961b702013-01-25 14:49:24 +01003815}
3816
3817/*
3818 * Perform the SSL handshake
3819 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003820int mbedtls_ssl_handshake(mbedtls_ssl_context *ssl)
Paul Bakker1961b702013-01-25 14:49:24 +01003821{
3822 int ret = 0;
3823
Hanno Beckera817ea42020-10-20 15:20:23 +01003824 /* Sanity checks */
3825
Gilles Peskine449bd832023-01-11 14:50:10 +01003826 if (ssl == NULL || ssl->conf == NULL) {
3827 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3828 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003829
Hanno Beckera817ea42020-10-20 15:20:23 +01003830#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003831 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3832 (ssl->f_set_timer == NULL || ssl->f_get_timer == NULL)) {
3833 MBEDTLS_SSL_DEBUG_MSG(1, ("You must use "
3834 "mbedtls_ssl_set_timer_cb() for DTLS"));
3835 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Beckera817ea42020-10-20 15:20:23 +01003836 }
3837#endif /* MBEDTLS_SSL_PROTO_DTLS */
3838
Gilles Peskine449bd832023-01-11 14:50:10 +01003839 MBEDTLS_SSL_DEBUG_MSG(2, ("=> handshake"));
Paul Bakker1961b702013-01-25 14:49:24 +01003840
Hanno Beckera817ea42020-10-20 15:20:23 +01003841 /* Main handshake loop */
Gilles Peskine449bd832023-01-11 14:50:10 +01003842 while (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER) {
3843 ret = mbedtls_ssl_handshake_step(ssl);
Paul Bakker1961b702013-01-25 14:49:24 +01003844
Gilles Peskine449bd832023-01-11 14:50:10 +01003845 if (ret != 0) {
Paul Bakker1961b702013-01-25 14:49:24 +01003846 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01003847 }
Paul Bakker1961b702013-01-25 14:49:24 +01003848 }
3849
Gilles Peskine449bd832023-01-11 14:50:10 +01003850 MBEDTLS_SSL_DEBUG_MSG(2, ("<= handshake"));
Paul Bakker5121ce52009-01-03 21:22:43 +00003851
Gilles Peskine449bd832023-01-11 14:50:10 +01003852 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003853}
3854
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003855#if defined(MBEDTLS_SSL_RENEGOTIATION)
3856#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003857/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003858 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00003859 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003860MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003861static int ssl_write_hello_request(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003862{
Janos Follath865b3eb2019-12-16 11:46:15 +00003863 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003864
Gilles Peskine449bd832023-01-11 14:50:10 +01003865 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write hello request"));
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003866
3867 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003868 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3869 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003870
Gilles Peskine449bd832023-01-11 14:50:10 +01003871 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
3872 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
3873 return ret;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003874 }
3875
Gilles Peskine449bd832023-01-11 14:50:10 +01003876 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write hello request"));
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003877
Gilles Peskine449bd832023-01-11 14:50:10 +01003878 return 0;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003879}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003880#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003881
3882/*
3883 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003884 * - any side: calling mbedtls_ssl_renegotiate(),
3885 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
3886 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02003887 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003888 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003889 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003890 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003891int mbedtls_ssl_start_renegotiation(mbedtls_ssl_context *ssl)
Paul Bakker48916f92012-09-16 19:57:18 +00003892{
Janos Follath865b3eb2019-12-16 11:46:15 +00003893 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker48916f92012-09-16 19:57:18 +00003894
Gilles Peskine449bd832023-01-11 14:50:10 +01003895 MBEDTLS_SSL_DEBUG_MSG(2, ("=> renegotiate"));
Paul Bakker48916f92012-09-16 19:57:18 +00003896
Gilles Peskine449bd832023-01-11 14:50:10 +01003897 if ((ret = ssl_handshake_init(ssl)) != 0) {
3898 return ret;
3899 }
Paul Bakker48916f92012-09-16 19:57:18 +00003900
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003901 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
3902 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003903#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003904 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3905 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING) {
3906 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003907 ssl->handshake->out_msg_seq = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003908 } else {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003909 ssl->handshake->in_msg_seq = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003910 }
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003911 }
3912#endif
3913
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003914 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
3915 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00003916
Gilles Peskine449bd832023-01-11 14:50:10 +01003917 if ((ret = mbedtls_ssl_handshake(ssl)) != 0) {
3918 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_handshake", ret);
3919 return ret;
Paul Bakker48916f92012-09-16 19:57:18 +00003920 }
3921
Gilles Peskine449bd832023-01-11 14:50:10 +01003922 MBEDTLS_SSL_DEBUG_MSG(2, ("<= renegotiate"));
Paul Bakker48916f92012-09-16 19:57:18 +00003923
Gilles Peskine449bd832023-01-11 14:50:10 +01003924 return 0;
Paul Bakker48916f92012-09-16 19:57:18 +00003925}
3926
3927/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003928 * Renegotiate current connection on client,
3929 * or request renegotiation on server
3930 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003931int mbedtls_ssl_renegotiate(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003932{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003933 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003934
Gilles Peskine449bd832023-01-11 14:50:10 +01003935 if (ssl == NULL || ssl->conf == NULL) {
3936 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3937 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003938
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003939#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003940 /* On server, just send the request */
Gilles Peskine449bd832023-01-11 14:50:10 +01003941 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
3942 if (mbedtls_ssl_is_handshake_over(ssl) == 0) {
3943 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3944 }
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003945
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003946 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02003947
3948 /* Did we already try/start sending HelloRequest? */
Gilles Peskine449bd832023-01-11 14:50:10 +01003949 if (ssl->out_left != 0) {
3950 return mbedtls_ssl_flush_output(ssl);
3951 }
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02003952
Gilles Peskine449bd832023-01-11 14:50:10 +01003953 return ssl_write_hello_request(ssl);
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003954 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003955#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003956
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003957#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003958 /*
3959 * On client, either start the renegotiation process or,
3960 * if already in progress, continue the handshake
3961 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003962 if (ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
3963 if (mbedtls_ssl_is_handshake_over(ssl) == 0) {
3964 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003965 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003966
3967 if ((ret = mbedtls_ssl_start_renegotiation(ssl)) != 0) {
3968 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_start_renegotiation", ret);
3969 return ret;
3970 }
3971 } else {
3972 if ((ret = mbedtls_ssl_handshake(ssl)) != 0) {
3973 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_handshake", ret);
3974 return ret;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003975 }
3976 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003977#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003978
Gilles Peskine449bd832023-01-11 14:50:10 +01003979 return ret;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003980}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003981#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003982
Gilles Peskine449bd832023-01-11 14:50:10 +01003983void mbedtls_ssl_handshake_free(mbedtls_ssl_context *ssl)
Paul Bakker48916f92012-09-16 19:57:18 +00003984{
Gilles Peskine9b562d52018-04-25 20:32:43 +02003985 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
3986
Gilles Peskine449bd832023-01-11 14:50:10 +01003987 if (handshake == NULL) {
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003988 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01003989 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003990
Brett Warrene0edc842021-08-17 09:53:13 +01003991#if defined(MBEDTLS_ECP_C)
3992#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003993 if (ssl->handshake->group_list_heap_allocated) {
3994 mbedtls_free((void *) handshake->group_list);
3995 }
Brett Warrene0edc842021-08-17 09:53:13 +01003996 handshake->group_list = NULL;
3997#endif /* MBEDTLS_DEPRECATED_REMOVED */
3998#endif /* MBEDTLS_ECP_C */
3999
Ronald Crone68ab4f2022-10-05 12:46:29 +02004000#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +08004001#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01004002 if (ssl->handshake->sig_algs_heap_allocated) {
4003 mbedtls_free((void *) handshake->sig_algs);
4004 }
Jerry Yuf017ee42022-01-12 15:49:48 +08004005 handshake->sig_algs = NULL;
4006#endif /* MBEDTLS_DEPRECATED_REMOVED */
Xiaofei Baic234ecf2022-02-08 09:59:23 +00004007#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01004008 if (ssl->handshake->certificate_request_context) {
4009 mbedtls_free((void *) handshake->certificate_request_context);
Xiaofei Baic234ecf2022-02-08 09:59:23 +00004010 }
4011#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Ronald Crone68ab4f2022-10-05 12:46:29 +02004012#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yuf017ee42022-01-12 15:49:48 +08004013
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02004014#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01004015 if (ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0) {
4016 ssl->conf->f_async_cancel(ssl);
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02004017 handshake->async_in_progress = 0;
4018 }
4019#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
4020
Andrzej Kurek25f27152022-08-17 16:09:31 -04004021#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -05004022#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01004023 psa_hash_abort(&handshake->fin_sha256_psa);
Andrzej Kurekeb342242019-01-29 09:14:33 -05004024#else
Gilles Peskine449bd832023-01-11 14:50:10 +01004025 mbedtls_sha256_free(&handshake->fin_sha256);
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02004026#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05004027#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004028#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -05004029#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01004030 psa_hash_abort(&handshake->fin_sha384_psa);
Andrzej Kurekeb342242019-01-29 09:14:33 -05004031#else
Gilles Peskine449bd832023-01-11 14:50:10 +01004032 mbedtls_sha512_free(&handshake->fin_sha384);
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02004033#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05004034#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02004035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004036#if defined(MBEDTLS_DHM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004037 mbedtls_dhm_free(&handshake->dhm_ctx);
Paul Bakker48916f92012-09-16 19:57:18 +00004038#endif
Neil Armstrongf3f46412022-04-12 14:43:39 +02004039#if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004040 mbedtls_ecdh_free(&handshake->ecdh_ctx);
Paul Bakker61d113b2013-07-04 11:51:43 +02004041#endif
Valerio Setti02c25b52022-11-15 14:08:42 +01004042
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02004043#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Neil Armstrongca7d5062022-05-31 14:43:23 +02004044#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01004045 psa_pake_abort(&handshake->psa_pake_ctx);
Valerio Settieb3f7882022-12-08 18:42:58 +01004046 /*
4047 * Opaque keys are not stored in the handshake's data and it's the user
4048 * responsibility to destroy them. Clear ones, instead, are created by
4049 * the TLS library and should be destroyed at the same level
4050 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004051 if (!mbedtls_svc_key_id_is_null(handshake->psa_pake_password)) {
4052 psa_destroy_key(handshake->psa_pake_password);
Valerio Settieb3f7882022-12-08 18:42:58 +01004053 }
Neil Armstrongca7d5062022-05-31 14:43:23 +02004054 handshake->psa_pake_password = MBEDTLS_SVC_KEY_ID_INIT;
4055#else
Gilles Peskine449bd832023-01-11 14:50:10 +01004056 mbedtls_ecjpake_free(&handshake->ecjpake_ctx);
Neil Armstrongca7d5062022-05-31 14:43:23 +02004057#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02004058#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004059 mbedtls_free(handshake->ecjpake_cache);
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02004060 handshake->ecjpake_cache = NULL;
4061 handshake->ecjpake_cache_len = 0;
4062#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02004063#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02004064
Janos Follath4ae5c292016-02-10 11:27:43 +00004065#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
4066 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02004067 /* explicit void pointer cast for buggy MS compiler */
Gilles Peskine449bd832023-01-11 14:50:10 +01004068 mbedtls_free((void *) handshake->curves_tls_id);
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004069#endif
4070
Ronald Cron73fe8df2022-10-05 14:31:43 +02004071#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02004072#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01004073 if (!mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque)) {
Neil Armstrong501c9322022-05-03 09:35:09 +02004074 /* The maintenance of the external PSK key slot is the
4075 * user's responsibility. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004076 if (ssl->handshake->psk_opaque_is_internal) {
4077 psa_destroy_key(ssl->handshake->psk_opaque);
Neil Armstrong501c9322022-05-03 09:35:09 +02004078 ssl->handshake->psk_opaque_is_internal = 0;
4079 }
4080 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
4081 }
Neil Armstronge952a302022-05-03 10:22:14 +02004082#else
Gilles Peskine449bd832023-01-11 14:50:10 +01004083 if (handshake->psk != NULL) {
4084 mbedtls_platform_zeroize(handshake->psk, handshake->psk_len);
4085 mbedtls_free(handshake->psk);
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004086 }
Neil Armstronge952a302022-05-03 10:22:14 +02004087#endif /* MBEDTLS_USE_PSA_CRYPTO */
Ronald Cron73fe8df2022-10-05 14:31:43 +02004088#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004089
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004090#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
4091 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02004092 /*
4093 * Free only the linked list wrapper, not the keys themselves
4094 * since the belong to the SNI callback
4095 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004096 ssl_key_cert_free(handshake->sni_key_cert);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004097#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004098
Gilles Peskineeccd8882020-03-10 12:19:08 +01004099#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01004100 mbedtls_x509_crt_restart_free(&handshake->ecrs_ctx);
4101 if (handshake->ecrs_peer_cert != NULL) {
4102 mbedtls_x509_crt_free(handshake->ecrs_peer_cert);
4103 mbedtls_free(handshake->ecrs_peer_cert);
Hanno Becker3dad3112019-02-05 17:19:52 +00004104 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02004105#endif
4106
Hanno Becker75173122019-02-06 16:18:31 +00004107#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
4108 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01004109 mbedtls_pk_free(&handshake->peer_pubkey);
Hanno Becker75173122019-02-06 16:18:31 +00004110#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
4111
XiaokangQian9b93c0d2022-02-09 06:02:25 +00004112#if defined(MBEDTLS_SSL_CLI_C) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01004113 (defined(MBEDTLS_SSL_PROTO_DTLS) || defined(MBEDTLS_SSL_PROTO_TLS1_3))
4114 mbedtls_free(handshake->cookie);
XiaokangQian9b93c0d2022-02-09 06:02:25 +00004115#endif /* MBEDTLS_SSL_CLI_C &&
4116 ( MBEDTLS_SSL_PROTO_DTLS || MBEDTLS_SSL_PROTO_TLS1_3 ) */
XiaokangQian8499b6c2022-01-27 09:00:11 +00004117
4118#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004119 mbedtls_ssl_flight_free(handshake->flight);
4120 mbedtls_ssl_buffering_free(ssl);
XiaokangQian8499b6c2022-01-27 09:00:11 +00004121#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02004122
Ronald Cronf12b81d2022-03-15 10:42:41 +01004123#if defined(MBEDTLS_ECDH_C) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01004124 (defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3))
4125 if (handshake->ecdh_psa_privkey_is_external == 0) {
4126 psa_destroy_key(handshake->ecdh_psa_privkey);
4127 }
Hanno Becker4a63ed42019-01-08 11:39:35 +00004128#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
4129
Ronald Cron6f135e12021-12-08 16:57:54 +01004130#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01004131 mbedtls_ssl_transform_free(handshake->transform_handshake);
4132 mbedtls_free(handshake->transform_handshake);
Jerry Yu3d9b5902022-11-04 14:07:25 +08004133#if defined(MBEDTLS_SSL_EARLY_DATA)
Gilles Peskine449bd832023-01-11 14:50:10 +01004134 mbedtls_ssl_transform_free(handshake->transform_earlydata);
4135 mbedtls_free(handshake->transform_earlydata);
Jerry Yu3d9b5902022-11-04 14:07:25 +08004136#endif
Ronald Cron6f135e12021-12-08 16:57:54 +01004137#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yuba9c7272021-10-30 11:54:10 +08004138
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004139
4140#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4141 /* If the buffers are too big - reallocate. Because of the way Mbed TLS
4142 * processes datagrams and the fact that a datagram is allowed to have
4143 * several records in it, it is possible that the I/O buffers are not
4144 * empty at this stage */
Gilles Peskine449bd832023-01-11 14:50:10 +01004145 handle_buffer_resizing(ssl, 1, mbedtls_ssl_get_input_buflen(ssl),
4146 mbedtls_ssl_get_output_buflen(ssl));
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004147#endif
Hanno Becker3aa186f2021-08-10 09:24:19 +01004148
Jerry Yuba9c7272021-10-30 11:54:10 +08004149 /* mbedtls_platform_zeroize MUST be last one in this function */
Gilles Peskine449bd832023-01-11 14:50:10 +01004150 mbedtls_platform_zeroize(handshake,
4151 sizeof(mbedtls_ssl_handshake_params));
Paul Bakker48916f92012-09-16 19:57:18 +00004152}
4153
Gilles Peskine449bd832023-01-11 14:50:10 +01004154void mbedtls_ssl_session_free(mbedtls_ssl_session *session)
Paul Bakker48916f92012-09-16 19:57:18 +00004155{
Gilles Peskine449bd832023-01-11 14:50:10 +01004156 if (session == NULL) {
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004157 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01004158 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004159
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004160#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004161 ssl_clear_peer_cert(session);
Paul Bakkered27a042013-04-18 22:46:23 +02004162#endif
Paul Bakker0a597072012-09-25 21:55:46 +00004163
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02004164#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Xiaokang Qianbc663a02022-10-09 11:14:39 +00004165#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
4166 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01004167 mbedtls_free(session->hostname);
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00004168#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01004169 mbedtls_free(session->ticket);
Paul Bakkera503a632013-08-14 13:48:06 +02004170#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004171
Gilles Peskine449bd832023-01-11 14:50:10 +01004172 mbedtls_platform_zeroize(session, sizeof(mbedtls_ssl_session));
Paul Bakker48916f92012-09-16 19:57:18 +00004173}
4174
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02004175#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004176
4177#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4178#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u
4179#else
4180#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u
4181#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4182
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004183#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004184
4185#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4186#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u
4187#else
4188#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u
4189#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4190
4191#if defined(MBEDTLS_SSL_ALPN)
4192#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u
4193#else
4194#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u
4195#endif /* MBEDTLS_SSL_ALPN */
4196
4197#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0
4198#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1
4199#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2
4200#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3
4201
4202#define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \
Gilles Peskine449bd832023-01-11 14:50:10 +01004203 ((uint32_t) ( \
4204 (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << \
4205 SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT) | \
4206 (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << \
4207 SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT) | \
4208 (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << \
4209 SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT) | \
4210 (SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT) | \
4211 0u))
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004212
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004213static unsigned char ssl_serialized_context_header[] = {
4214 MBEDTLS_VERSION_MAJOR,
4215 MBEDTLS_VERSION_MINOR,
4216 MBEDTLS_VERSION_PATCH,
Gilles Peskine449bd832023-01-11 14:50:10 +01004217 MBEDTLS_BYTE_1(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
4218 MBEDTLS_BYTE_0(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
4219 MBEDTLS_BYTE_2(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG),
4220 MBEDTLS_BYTE_1(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG),
4221 MBEDTLS_BYTE_0(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG),
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004222};
4223
Paul Bakker5121ce52009-01-03 21:22:43 +00004224/*
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004225 * Serialize a full SSL context
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02004226 *
4227 * The format of the serialized data is:
4228 * (in the presentation language of TLS, RFC 8446 section 3)
4229 *
4230 * // header
4231 * opaque mbedtls_version[3]; // major, minor, patch
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004232 * opaque context_format[5]; // version-specific field determining
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02004233 * // the format of the remaining
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004234 * // serialized data.
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004235 * Note: When updating the format, remember to keep these
4236 * version+format bytes. (We may make their size part of the API.)
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02004237 *
4238 * // session sub-structure
4239 * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save()
4240 * // transform sub-structure
4241 * uint8 random[64]; // ServerHello.random+ClientHello.random
4242 * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value
4243 * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use
4244 * // fields from ssl_context
4245 * uint32 badmac_seen; // DTLS: number of records with failing MAC
4246 * uint64 in_window_top; // DTLS: last validated record seq_num
4247 * uint64 in_window; // DTLS: bitmask for replay protection
4248 * uint8 disable_datagram_packing; // DTLS: only one record per datagram
4249 * uint64 cur_out_ctr; // Record layer: outgoing sequence number
4250 * uint16 mtu; // DTLS: path mtu (max outgoing fragment size)
4251 * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol
4252 *
4253 * Note that many fields of the ssl_context or sub-structures are not
4254 * serialized, as they fall in one of the following categories:
4255 *
4256 * 1. forced value (eg in_left must be 0)
4257 * 2. pointer to dynamically-allocated memory (eg session, transform)
4258 * 3. value can be re-derived from other data (eg session keys from MS)
4259 * 4. value was temporary (eg content of input buffer)
4260 * 5. value will be provided by the user again (eg I/O callbacks and context)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004261 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004262int mbedtls_ssl_context_save(mbedtls_ssl_context *ssl,
4263 unsigned char *buf,
4264 size_t buf_len,
4265 size_t *olen)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004266{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004267 unsigned char *p = buf;
4268 size_t used = 0;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004269 size_t session_len;
4270 int ret = 0;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004271
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02004272 /*
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004273 * Enforce usage restrictions, see "return BAD_INPUT_DATA" in
4274 * this function's documentation.
4275 *
4276 * These are due to assumptions/limitations in the implementation. Some of
4277 * them are likely to stay (no handshake in progress) some might go away
4278 * (only DTLS) but are currently used to simplify the implementation.
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02004279 */
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004280 /* The initial handshake must be over */
Gilles Peskine449bd832023-01-11 14:50:10 +01004281 if (mbedtls_ssl_is_handshake_over(ssl) == 0) {
4282 MBEDTLS_SSL_DEBUG_MSG(1, ("Initial handshake isn't over"));
4283 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004284 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004285 if (ssl->handshake != NULL) {
4286 MBEDTLS_SSL_DEBUG_MSG(1, ("Handshake isn't completed"));
4287 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004288 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004289 /* Double-check that sub-structures are indeed ready */
Gilles Peskine449bd832023-01-11 14:50:10 +01004290 if (ssl->transform == NULL || ssl->session == NULL) {
4291 MBEDTLS_SSL_DEBUG_MSG(1, ("Serialised structures aren't ready"));
4292 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004293 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004294 /* There must be no pending incoming or outgoing data */
Gilles Peskine449bd832023-01-11 14:50:10 +01004295 if (mbedtls_ssl_check_pending(ssl) != 0) {
4296 MBEDTLS_SSL_DEBUG_MSG(1, ("There is pending incoming data"));
4297 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004298 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004299 if (ssl->out_left != 0) {
4300 MBEDTLS_SSL_DEBUG_MSG(1, ("There is pending outgoing data"));
4301 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004302 }
Dave Rodgman556e8a32022-12-06 16:31:25 +00004303 /* Protocol must be DTLS, not TLS */
Gilles Peskine449bd832023-01-11 14:50:10 +01004304 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
4305 MBEDTLS_SSL_DEBUG_MSG(1, ("Only DTLS is supported"));
4306 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004307 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004308 /* Version must be 1.2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004309 if (ssl->tls_version != MBEDTLS_SSL_VERSION_TLS1_2) {
4310 MBEDTLS_SSL_DEBUG_MSG(1, ("Only version 1.2 supported"));
4311 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004312 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004313 /* We must be using an AEAD ciphersuite */
Gilles Peskine449bd832023-01-11 14:50:10 +01004314 if (mbedtls_ssl_transform_uses_aead(ssl->transform) != 1) {
4315 MBEDTLS_SSL_DEBUG_MSG(1, ("Only AEAD ciphersuites supported"));
4316 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004317 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004318 /* Renegotiation must not be enabled */
4319#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01004320 if (ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED) {
4321 MBEDTLS_SSL_DEBUG_MSG(1, ("Renegotiation must not be enabled"));
4322 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004323 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004324#endif
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004325
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004326 /*
4327 * Version and format identifier
4328 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004329 used += sizeof(ssl_serialized_context_header);
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004330
Gilles Peskine449bd832023-01-11 14:50:10 +01004331 if (used <= buf_len) {
4332 memcpy(p, ssl_serialized_context_header,
4333 sizeof(ssl_serialized_context_header));
4334 p += sizeof(ssl_serialized_context_header);
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004335 }
4336
4337 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004338 * Session (length + data)
4339 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004340 ret = ssl_session_save(ssl->session, 1, NULL, 0, &session_len);
4341 if (ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL) {
4342 return ret;
4343 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004344
4345 used += 4 + session_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004346 if (used <= buf_len) {
4347 MBEDTLS_PUT_UINT32_BE(session_len, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004348 p += 4;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004349
Gilles Peskine449bd832023-01-11 14:50:10 +01004350 ret = ssl_session_save(ssl->session, 1,
4351 p, session_len, &session_len);
4352 if (ret != 0) {
4353 return ret;
4354 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004355
4356 p += session_len;
4357 }
4358
4359 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004360 * Transform
4361 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004362 used += sizeof(ssl->transform->randbytes);
4363 if (used <= buf_len) {
4364 memcpy(p, ssl->transform->randbytes,
4365 sizeof(ssl->transform->randbytes));
4366 p += sizeof(ssl->transform->randbytes);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004367 }
4368
4369#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4370 used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004371 if (used <= buf_len) {
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004372 *p++ = ssl->transform->in_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004373 memcpy(p, ssl->transform->in_cid, ssl->transform->in_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004374 p += ssl->transform->in_cid_len;
4375
4376 *p++ = ssl->transform->out_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004377 memcpy(p, ssl->transform->out_cid, ssl->transform->out_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004378 p += ssl->transform->out_cid_len;
4379 }
4380#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4381
4382 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004383 * Saved fields from top-level ssl_context structure
4384 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004385 used += 4;
Gilles Peskine449bd832023-01-11 14:50:10 +01004386 if (used <= buf_len) {
4387 MBEDTLS_PUT_UINT32_BE(ssl->badmac_seen, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004388 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004389 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004390
4391#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4392 used += 16;
Gilles Peskine449bd832023-01-11 14:50:10 +01004393 if (used <= buf_len) {
4394 MBEDTLS_PUT_UINT64_BE(ssl->in_window_top, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004395 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004396
Gilles Peskine449bd832023-01-11 14:50:10 +01004397 MBEDTLS_PUT_UINT64_BE(ssl->in_window, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004398 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004399 }
4400#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4401
4402#if defined(MBEDTLS_SSL_PROTO_DTLS)
4403 used += 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004404 if (used <= buf_len) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004405 *p++ = ssl->disable_datagram_packing;
4406 }
4407#endif /* MBEDTLS_SSL_PROTO_DTLS */
4408
Jerry Yuae0b2e22021-10-08 15:21:19 +08004409 used += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Gilles Peskine449bd832023-01-11 14:50:10 +01004410 if (used <= buf_len) {
4411 memcpy(p, ssl->cur_out_ctr, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN);
Jerry Yuae0b2e22021-10-08 15:21:19 +08004412 p += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004413 }
4414
4415#if defined(MBEDTLS_SSL_PROTO_DTLS)
4416 used += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01004417 if (used <= buf_len) {
4418 MBEDTLS_PUT_UINT16_BE(ssl->mtu, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004419 p += 2;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004420 }
4421#endif /* MBEDTLS_SSL_PROTO_DTLS */
4422
4423#if defined(MBEDTLS_SSL_ALPN)
4424 {
4425 const uint8_t alpn_len = ssl->alpn_chosen
Gilles Peskine449bd832023-01-11 14:50:10 +01004426 ? (uint8_t) strlen(ssl->alpn_chosen)
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004427 : 0;
4428
4429 used += 1 + alpn_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004430 if (used <= buf_len) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004431 *p++ = alpn_len;
4432
Gilles Peskine449bd832023-01-11 14:50:10 +01004433 if (ssl->alpn_chosen != NULL) {
4434 memcpy(p, ssl->alpn_chosen, alpn_len);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004435 p += alpn_len;
4436 }
4437 }
4438 }
4439#endif /* MBEDTLS_SSL_ALPN */
4440
4441 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004442 * Done
4443 */
4444 *olen = used;
4445
Gilles Peskine449bd832023-01-11 14:50:10 +01004446 if (used > buf_len) {
4447 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
4448 }
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004449
Gilles Peskine449bd832023-01-11 14:50:10 +01004450 MBEDTLS_SSL_DEBUG_BUF(4, "saved context", buf, used);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004451
Gilles Peskine449bd832023-01-11 14:50:10 +01004452 return mbedtls_ssl_session_reset_int(ssl, 0);
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004453}
4454
4455/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02004456 * Deserialize context, see mbedtls_ssl_context_save() for format.
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004457 *
4458 * This internal version is wrapped by a public function that cleans up in
4459 * case of error.
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004460 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004461MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01004462static int ssl_context_load(mbedtls_ssl_context *ssl,
4463 const unsigned char *buf,
4464 size_t len)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004465{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004466 const unsigned char *p = buf;
4467 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004468 size_t session_len;
Janos Follath865b3eb2019-12-16 11:46:15 +00004469 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004470#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek894edde2022-09-29 06:31:14 -04004471 tls_prf_fn prf_func = NULL;
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004472#endif
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004473
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004474 /*
4475 * The context should have been freshly setup or reset.
4476 * Give the user an error in case of obvious misuse.
Manuel Pégourié-Gonnard4ca930f2019-07-26 16:31:53 +02004477 * (Checking session is useful because it won't be NULL if we're
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004478 * renegotiating, or if the user mistakenly loaded a session first.)
4479 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004480 if (ssl->state != MBEDTLS_SSL_HELLO_REQUEST ||
4481 ssl->session != NULL) {
4482 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004483 }
4484
4485 /*
4486 * We can't check that the config matches the initial one, but we can at
4487 * least check it matches the requirements for serializing.
4488 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004489 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
Glenn Strauss2dfcea22022-03-14 17:26:42 -04004490 ssl->conf->max_tls_version < MBEDTLS_SSL_VERSION_TLS1_2 ||
4491 ssl->conf->min_tls_version > MBEDTLS_SSL_VERSION_TLS1_2 ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004492#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02004493 ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004494#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01004495 0) {
4496 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004497 }
4498
Gilles Peskine449bd832023-01-11 14:50:10 +01004499 MBEDTLS_SSL_DEBUG_BUF(4, "context to load", buf, len);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004500
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004501 /*
4502 * Check version identifier
4503 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004504 if ((size_t) (end - p) < sizeof(ssl_serialized_context_header)) {
4505 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004506 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004507
4508 if (memcmp(p, ssl_serialized_context_header,
4509 sizeof(ssl_serialized_context_header)) != 0) {
4510 return MBEDTLS_ERR_SSL_VERSION_MISMATCH;
4511 }
4512 p += sizeof(ssl_serialized_context_header);
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004513
4514 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004515 * Session
4516 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004517 if ((size_t) (end - p) < 4) {
4518 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4519 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004520
Gilles Peskine449bd832023-01-11 14:50:10 +01004521 session_len = ((size_t) p[0] << 24) |
4522 ((size_t) p[1] << 16) |
4523 ((size_t) p[2] << 8) |
4524 ((size_t) p[3]);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004525 p += 4;
4526
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004527 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00004528 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004529 ssl->session = ssl->session_negotiate;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004530 ssl->session_in = ssl->session;
4531 ssl->session_out = ssl->session;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004532 ssl->session_negotiate = NULL;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004533
Gilles Peskine449bd832023-01-11 14:50:10 +01004534 if ((size_t) (end - p) < session_len) {
4535 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4536 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004537
Gilles Peskine449bd832023-01-11 14:50:10 +01004538 ret = ssl_session_load(ssl->session, 1, p, session_len);
4539 if (ret != 0) {
4540 mbedtls_ssl_session_free(ssl->session);
4541 return ret;
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004542 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004543
4544 p += session_len;
4545
4546 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004547 * Transform
4548 */
4549
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004550 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00004551 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Jerry Yu2e199812022-12-01 18:57:19 +08004552#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004553 ssl->transform = ssl->transform_negotiate;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004554 ssl->transform_in = ssl->transform;
4555 ssl->transform_out = ssl->transform;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004556 ssl->transform_negotiate = NULL;
Jerry Yu2e199812022-12-01 18:57:19 +08004557#endif
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004558
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004559#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01004560 prf_func = ssl_tls12prf_from_cs(ssl->session->ciphersuite);
4561 if (prf_func == NULL) {
4562 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4563 }
Andrzej Kurek894edde2022-09-29 06:31:14 -04004564
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004565 /* Read random bytes and populate structure */
Gilles Peskine449bd832023-01-11 14:50:10 +01004566 if ((size_t) (end - p) < sizeof(ssl->transform->randbytes)) {
4567 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4568 }
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004569
Gilles Peskine449bd832023-01-11 14:50:10 +01004570 ret = ssl_tls12_populate_transform(ssl->transform,
4571 ssl->session->ciphersuite,
4572 ssl->session->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02004573#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004574 ssl->session->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02004575#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01004576 prf_func,
4577 p, /* currently pointing to randbytes */
4578 MBEDTLS_SSL_VERSION_TLS1_2, /* (D)TLS 1.2 is forced */
4579 ssl->conf->endpoint,
4580 ssl);
4581 if (ret != 0) {
4582 return ret;
4583 }
Jerry Yu840fbb22022-02-17 14:59:29 +08004584#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004585 p += sizeof(ssl->transform->randbytes);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004586
4587#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4588 /* Read connection IDs and store them */
Gilles Peskine449bd832023-01-11 14:50:10 +01004589 if ((size_t) (end - p) < 1) {
4590 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4591 }
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004592
4593 ssl->transform->in_cid_len = *p++;
4594
Gilles Peskine449bd832023-01-11 14:50:10 +01004595 if ((size_t) (end - p) < ssl->transform->in_cid_len + 1u) {
4596 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4597 }
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004598
Gilles Peskine449bd832023-01-11 14:50:10 +01004599 memcpy(ssl->transform->in_cid, p, ssl->transform->in_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004600 p += ssl->transform->in_cid_len;
4601
4602 ssl->transform->out_cid_len = *p++;
4603
Gilles Peskine449bd832023-01-11 14:50:10 +01004604 if ((size_t) (end - p) < ssl->transform->out_cid_len) {
4605 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4606 }
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004607
Gilles Peskine449bd832023-01-11 14:50:10 +01004608 memcpy(ssl->transform->out_cid, p, ssl->transform->out_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004609 p += ssl->transform->out_cid_len;
4610#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4611
4612 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004613 * Saved fields from top-level ssl_context structure
4614 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004615 if ((size_t) (end - p) < 4) {
4616 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4617 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004618
Gilles Peskine449bd832023-01-11 14:50:10 +01004619 ssl->badmac_seen = ((uint32_t) p[0] << 24) |
4620 ((uint32_t) p[1] << 16) |
4621 ((uint32_t) p[2] << 8) |
4622 ((uint32_t) p[3]);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004623 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004624
4625#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine449bd832023-01-11 14:50:10 +01004626 if ((size_t) (end - p) < 16) {
4627 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4628 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004629
Gilles Peskine449bd832023-01-11 14:50:10 +01004630 ssl->in_window_top = ((uint64_t) p[0] << 56) |
4631 ((uint64_t) p[1] << 48) |
4632 ((uint64_t) p[2] << 40) |
4633 ((uint64_t) p[3] << 32) |
4634 ((uint64_t) p[4] << 24) |
4635 ((uint64_t) p[5] << 16) |
4636 ((uint64_t) p[6] << 8) |
4637 ((uint64_t) p[7]);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004638 p += 8;
4639
Gilles Peskine449bd832023-01-11 14:50:10 +01004640 ssl->in_window = ((uint64_t) p[0] << 56) |
4641 ((uint64_t) p[1] << 48) |
4642 ((uint64_t) p[2] << 40) |
4643 ((uint64_t) p[3] << 32) |
4644 ((uint64_t) p[4] << 24) |
4645 ((uint64_t) p[5] << 16) |
4646 ((uint64_t) p[6] << 8) |
4647 ((uint64_t) p[7]);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004648 p += 8;
4649#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4650
4651#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004652 if ((size_t) (end - p) < 1) {
4653 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4654 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004655
4656 ssl->disable_datagram_packing = *p++;
4657#endif /* MBEDTLS_SSL_PROTO_DTLS */
4658
Gilles Peskine449bd832023-01-11 14:50:10 +01004659 if ((size_t) (end - p) < sizeof(ssl->cur_out_ctr)) {
4660 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4661 }
4662 memcpy(ssl->cur_out_ctr, p, sizeof(ssl->cur_out_ctr));
4663 p += sizeof(ssl->cur_out_ctr);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004664
4665#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004666 if ((size_t) (end - p) < 2) {
4667 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4668 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004669
Gilles Peskine449bd832023-01-11 14:50:10 +01004670 ssl->mtu = (p[0] << 8) | p[1];
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004671 p += 2;
4672#endif /* MBEDTLS_SSL_PROTO_DTLS */
4673
4674#if defined(MBEDTLS_SSL_ALPN)
4675 {
4676 uint8_t alpn_len;
4677 const char **cur;
4678
Gilles Peskine449bd832023-01-11 14:50:10 +01004679 if ((size_t) (end - p) < 1) {
4680 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4681 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004682
4683 alpn_len = *p++;
4684
Gilles Peskine449bd832023-01-11 14:50:10 +01004685 if (alpn_len != 0 && ssl->conf->alpn_list != NULL) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004686 /* alpn_chosen should point to an item in the configured list */
Gilles Peskine449bd832023-01-11 14:50:10 +01004687 for (cur = ssl->conf->alpn_list; *cur != NULL; cur++) {
4688 if (strlen(*cur) == alpn_len &&
4689 memcmp(p, cur, alpn_len) == 0) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004690 ssl->alpn_chosen = *cur;
4691 break;
4692 }
4693 }
4694 }
4695
4696 /* can only happen on conf mismatch */
Gilles Peskine449bd832023-01-11 14:50:10 +01004697 if (alpn_len != 0 && ssl->alpn_chosen == NULL) {
4698 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4699 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004700
4701 p += alpn_len;
4702 }
4703#endif /* MBEDTLS_SSL_ALPN */
4704
4705 /*
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004706 * Forced fields from top-level ssl_context structure
4707 *
4708 * Most of them already set to the correct value by mbedtls_ssl_init() and
4709 * mbedtls_ssl_reset(), so we only need to set the remaining ones.
4710 */
4711 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
Glenn Strauss60bfe602022-03-14 19:04:24 -04004712 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004713
Hanno Becker361b10d2019-08-30 10:42:49 +01004714 /* Adjust pointers for header fields of outgoing records to
4715 * the given transform, accounting for explicit IV and CID. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004716 mbedtls_ssl_update_out_pointers(ssl, ssl->transform);
Hanno Becker361b10d2019-08-30 10:42:49 +01004717
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004718#if defined(MBEDTLS_SSL_PROTO_DTLS)
4719 ssl->in_epoch = 1;
4720#endif
4721
4722 /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated,
4723 * which we don't want - otherwise we'd end up freeing the wrong transform
Hanno Beckerce5f5fd2020-02-05 10:47:44 +00004724 * by calling mbedtls_ssl_handshake_wrapup_free_hs_transform()
4725 * inappropriately. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004726 if (ssl->handshake != NULL) {
4727 mbedtls_ssl_handshake_free(ssl);
4728 mbedtls_free(ssl->handshake);
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004729 ssl->handshake = NULL;
4730 }
4731
4732 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004733 * Done - should have consumed entire buffer
4734 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004735 if (p != end) {
4736 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4737 }
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004738
Gilles Peskine449bd832023-01-11 14:50:10 +01004739 return 0;
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004740}
4741
4742/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02004743 * Deserialize context: public wrapper for error cleaning
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004744 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004745int mbedtls_ssl_context_load(mbedtls_ssl_context *context,
4746 const unsigned char *buf,
4747 size_t len)
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004748{
Gilles Peskine449bd832023-01-11 14:50:10 +01004749 int ret = ssl_context_load(context, buf, len);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004750
Gilles Peskine449bd832023-01-11 14:50:10 +01004751 if (ret != 0) {
4752 mbedtls_ssl_free(context);
4753 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004754
Gilles Peskine449bd832023-01-11 14:50:10 +01004755 return ret;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004756}
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02004757#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004758
4759/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004760 * Free an SSL context
4761 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004762void mbedtls_ssl_free(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00004763{
Gilles Peskine449bd832023-01-11 14:50:10 +01004764 if (ssl == NULL) {
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004765 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01004766 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004767
Gilles Peskine449bd832023-01-11 14:50:10 +01004768 MBEDTLS_SSL_DEBUG_MSG(2, ("=> free"));
Paul Bakker5121ce52009-01-03 21:22:43 +00004769
Gilles Peskine449bd832023-01-11 14:50:10 +01004770 if (ssl->out_buf != NULL) {
sander-visserb8aa2072020-05-06 22:05:13 +02004771#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4772 size_t out_buf_len = ssl->out_buf_len;
4773#else
4774 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
4775#endif
4776
Gilles Peskine449bd832023-01-11 14:50:10 +01004777 mbedtls_platform_zeroize(ssl->out_buf, out_buf_len);
4778 mbedtls_free(ssl->out_buf);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004779 ssl->out_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004780 }
4781
Gilles Peskine449bd832023-01-11 14:50:10 +01004782 if (ssl->in_buf != NULL) {
sander-visserb8aa2072020-05-06 22:05:13 +02004783#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4784 size_t in_buf_len = ssl->in_buf_len;
4785#else
4786 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
4787#endif
4788
Gilles Peskine449bd832023-01-11 14:50:10 +01004789 mbedtls_platform_zeroize(ssl->in_buf, in_buf_len);
4790 mbedtls_free(ssl->in_buf);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004791 ssl->in_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004792 }
4793
Gilles Peskine449bd832023-01-11 14:50:10 +01004794 if (ssl->transform) {
4795 mbedtls_ssl_transform_free(ssl->transform);
4796 mbedtls_free(ssl->transform);
Paul Bakker48916f92012-09-16 19:57:18 +00004797 }
4798
Gilles Peskine449bd832023-01-11 14:50:10 +01004799 if (ssl->handshake) {
4800 mbedtls_ssl_handshake_free(ssl);
4801 mbedtls_free(ssl->handshake);
Jerry Yu2e199812022-12-01 18:57:19 +08004802
4803#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01004804 mbedtls_ssl_transform_free(ssl->transform_negotiate);
4805 mbedtls_free(ssl->transform_negotiate);
Jerry Yu2e199812022-12-01 18:57:19 +08004806#endif
4807
Gilles Peskine449bd832023-01-11 14:50:10 +01004808 mbedtls_ssl_session_free(ssl->session_negotiate);
4809 mbedtls_free(ssl->session_negotiate);
Paul Bakker48916f92012-09-16 19:57:18 +00004810 }
4811
Ronald Cron6f135e12021-12-08 16:57:54 +01004812#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01004813 mbedtls_ssl_transform_free(ssl->transform_application);
4814 mbedtls_free(ssl->transform_application);
Ronald Cron6f135e12021-12-08 16:57:54 +01004815#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker3aa186f2021-08-10 09:24:19 +01004816
Gilles Peskine449bd832023-01-11 14:50:10 +01004817 if (ssl->session) {
4818 mbedtls_ssl_session_free(ssl->session);
4819 mbedtls_free(ssl->session);
Paul Bakkerc0463502013-02-14 11:19:38 +01004820 }
4821
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02004822#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004823 if (ssl->hostname != NULL) {
4824 mbedtls_platform_zeroize(ssl->hostname, strlen(ssl->hostname));
4825 mbedtls_free(ssl->hostname);
Paul Bakker5121ce52009-01-03 21:22:43 +00004826 }
Paul Bakker0be444a2013-08-27 21:55:01 +02004827#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004828
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02004829#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004830 mbedtls_free(ssl->cli_id);
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02004831#endif
4832
Gilles Peskine449bd832023-01-11 14:50:10 +01004833 MBEDTLS_SSL_DEBUG_MSG(2, ("<= free"));
Paul Bakker2da561c2009-02-05 18:00:28 +00004834
Paul Bakker86f04f42013-02-14 11:20:09 +01004835 /* Actually clear after last debug message */
Gilles Peskine449bd832023-01-11 14:50:10 +01004836 mbedtls_platform_zeroize(ssl, sizeof(mbedtls_ssl_context));
Paul Bakker5121ce52009-01-03 21:22:43 +00004837}
4838
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004839/*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08004840 * Initialize mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004841 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004842void mbedtls_ssl_config_init(mbedtls_ssl_config *conf)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004843{
Gilles Peskine449bd832023-01-11 14:50:10 +01004844 memset(conf, 0, sizeof(mbedtls_ssl_config));
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004845}
4846
Gilles Peskineae270bf2021-06-02 00:05:29 +02004847/* The selection should be the same as mbedtls_x509_crt_profile_default in
4848 * x509_crt.c, plus Montgomery curves for ECDHE. Here, the order matters:
Gilles Peskineb1940a72021-06-02 15:18:12 +02004849 * curves with a lower resource usage come first.
Gilles Peskineae270bf2021-06-02 00:05:29 +02004850 * See the documentation of mbedtls_ssl_conf_curves() for what we promise
Gilles Peskineb1940a72021-06-02 15:18:12 +02004851 * about this list.
4852 */
Brett Warrene0edc842021-08-17 09:53:13 +01004853static uint16_t ssl_preset_default_groups[] = {
Gilles Peskineae270bf2021-06-02 00:05:29 +02004854#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004855 MBEDTLS_SSL_IANA_TLS_GROUP_X25519,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004856#endif
4857#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004858 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004859#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02004860#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004861 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004862#endif
4863#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004864 MBEDTLS_SSL_IANA_TLS_GROUP_X448,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004865#endif
4866#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004867 MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004868#endif
Gilles Peskineae270bf2021-06-02 00:05:29 +02004869#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004870 MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004871#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02004872#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004873 MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004874#endif
4875#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004876 MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004877#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004878 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Gilles Peskineae270bf2021-06-02 00:05:29 +02004879};
Gilles Peskineae270bf2021-06-02 00:05:29 +02004880
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004881static int ssl_preset_suiteb_ciphersuites[] = {
4882 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
4883 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
4884 0
4885};
4886
Ronald Crone68ab4f2022-10-05 12:46:29 +02004887#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004888
Jerry Yu909df7b2022-01-22 11:56:27 +08004889/* NOTICE:
Jerry Yu0b994b82022-01-25 17:22:12 +08004890 * For ssl_preset_*_sig_algs and ssl_tls12_preset_*_sig_algs, the following
Jerry Yu370e1462022-01-25 10:36:53 +08004891 * rules SHOULD be upheld.
4892 * - No duplicate entries.
4893 * - But if there is a good reason, do not change the order of the algorithms.
Jerry Yu09a99fc2022-07-28 14:22:17 +08004894 * - ssl_tls12_preset* is for TLS 1.2 use only.
Jerry Yu370e1462022-01-25 10:36:53 +08004895 * - ssl_preset_* is for TLS 1.3 only or hybrid TLS 1.3/1.2 handshakes.
Jerry Yu1a8b4812022-01-20 17:56:50 +08004896 */
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004897static uint16_t ssl_preset_default_sig_algs[] = {
Jerry Yu1a8b4812022-01-20 17:56:50 +08004898
Andrzej Kurek25f27152022-08-17 16:09:31 -04004899#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 +08004900 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
4901 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004902#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA &&
Jerry Yued5e9f42022-01-26 11:21:34 +08004903 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004904
Andrzej Kurek25f27152022-08-17 16:09:31 -04004905#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 +08004906 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
4907 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004908#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu909df7b2022-01-22 11:56:27 +08004909 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
4910
Andrzej Kurek25f27152022-08-17 16:09:31 -04004911#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 +08004912 defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
4913 MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004914#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yued5e9f42022-01-26 11:21:34 +08004915 MBEDTLS_ECP_DP_SECP521R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004916
Gilles Peskine449bd832023-01-11 14:50:10 +01004917#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && \
4918 defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu9bb3ee42022-06-23 10:16:33 +08004919 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
Gilles Peskine449bd832023-01-11 14:50:10 +01004920#endif \
4921 /* 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 +08004922
Gilles Peskine449bd832023-01-11 14:50:10 +01004923#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && \
4924 defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu9bb3ee42022-06-23 10:16:33 +08004925 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
Gilles Peskine449bd832023-01-11 14:50:10 +01004926#endif \
4927 /* 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 +08004928
Gilles Peskine449bd832023-01-11 14:50:10 +01004929#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && \
4930 defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu9bb3ee42022-06-23 10:16:33 +08004931 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Gilles Peskine449bd832023-01-11 14:50:10 +01004932#endif \
4933 /* 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 +08004934
Andrzej Kurek25f27152022-08-17 16:09:31 -04004935#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 +08004936 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512,
4937#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA512_C */
4938
Andrzej Kurek25f27152022-08-17 16:09:31 -04004939#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 +08004940 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384,
4941#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA384_C */
4942
Andrzej Kurek25f27152022-08-17 16:09:31 -04004943#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 +08004944 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
4945#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA256_C */
4946
Gabor Mezei15b95a62022-05-09 16:37:58 +02004947 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08004948};
4949
4950/* NOTICE: see above */
4951#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4952static uint16_t ssl_tls12_preset_default_sig_algs[] = {
Andrzej Kurek25f27152022-08-17 16:09:31 -04004953#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004954#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004955 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA512),
Jerry Yu713013f2022-01-17 18:16:35 +08004956#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004957#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4958 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
4959#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004960#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004961 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA512),
Gabor Mezeic1051b62022-05-10 13:13:58 +02004962#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004963#endif /* MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04004964#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004965#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004966 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08004967#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004968#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4969 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
4970#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004971#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004972 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384),
Gabor Mezeic1051b62022-05-10 13:13:58 +02004973#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004974#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04004975#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004976#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004977 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08004978#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004979#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4980 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
4981#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004982#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004983 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256),
Gabor Mezeic1051b62022-05-10 13:13:58 +02004984#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004985#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Gabor Mezei15b95a62022-05-09 16:37:58 +02004986 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08004987};
4988#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4989/* NOTICE: see above */
4990static uint16_t ssl_preset_suiteb_sig_algs[] = {
4991
Andrzej Kurek25f27152022-08-17 16:09:31 -04004992#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 +08004993 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
4994 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004995#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu909df7b2022-01-22 11:56:27 +08004996 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
4997
Andrzej Kurek25f27152022-08-17 16:09:31 -04004998#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 +08004999 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
5000 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005001#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu53037892022-01-25 11:02:06 +08005002 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08005003
Gilles Peskine449bd832023-01-11 14:50:10 +01005004#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && \
5005 defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu909df7b2022-01-22 11:56:27 +08005006 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Gilles Peskine449bd832023-01-11 14:50:10 +01005007#endif \
5008 /* 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 +08005009
Andrzej Kurek25f27152022-08-17 16:09:31 -04005010#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 +08005011 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005012#endif /* MBEDTLS_RSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yu53037892022-01-25 11:02:06 +08005013
Gabor Mezei15b95a62022-05-09 16:37:58 +02005014 MBEDTLS_TLS_SIG_NONE
Jerry Yu713013f2022-01-17 18:16:35 +08005015};
Jerry Yu6106fdc2022-01-12 16:36:14 +08005016
Jerry Yu909df7b2022-01-22 11:56:27 +08005017/* NOTICE: see above */
5018#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5019static uint16_t ssl_tls12_preset_suiteb_sig_algs[] = {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005020#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02005021#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005022 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256),
Jerry Yu713013f2022-01-17 18:16:35 +08005023#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02005024#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005025 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256),
Gabor Mezeic1051b62022-05-10 13:13:58 +02005026#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005027#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04005028#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02005029#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005030 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384),
Jerry Yu18c833e2022-01-25 10:55:47 +08005031#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02005032#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005033 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384),
Gabor Mezeic1051b62022-05-10 13:13:58 +02005034#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005035#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Gabor Mezei15b95a62022-05-09 16:37:58 +02005036 MBEDTLS_TLS_SIG_NONE
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01005037};
Jerry Yu909df7b2022-01-22 11:56:27 +08005038#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5039
Ronald Crone68ab4f2022-10-05 12:46:29 +02005040#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005041
Brett Warrene0edc842021-08-17 09:53:13 +01005042static uint16_t ssl_preset_suiteb_groups[] = {
Jaeden Amerod4311042019-06-03 08:27:16 +01005043#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01005044 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01005045#endif
5046#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01005047 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01005048#endif
Brett Warrene0edc842021-08-17 09:53:13 +01005049 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005050};
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005051
Ronald Crone68ab4f2022-10-05 12:46:29 +02005052#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08005053/* Function for checking `ssl_preset_*_sig_algs` and `ssl_tls12_preset_*_sig_algs`
Jerry Yu370e1462022-01-25 10:36:53 +08005054 * to make sure there are no duplicated signature algorithm entries. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005055MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01005056static int ssl_check_no_sig_alg_duplication(uint16_t *sig_algs)
Jerry Yu1a8b4812022-01-20 17:56:50 +08005057{
5058 size_t i, j;
5059 int ret = 0;
Jerry Yu909df7b2022-01-22 11:56:27 +08005060
Gilles Peskine449bd832023-01-11 14:50:10 +01005061 for (i = 0; sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++) {
5062 for (j = 0; j < i; j++) {
5063 if (sig_algs[i] != sig_algs[j]) {
Jerry Yuf377d642022-01-25 10:43:59 +08005064 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01005065 }
5066 mbedtls_printf(" entry(%04x,%" MBEDTLS_PRINTF_SIZET
5067 ") is duplicated at %" MBEDTLS_PRINTF_SIZET "\n",
5068 sig_algs[i], j, i);
Jerry Yuf377d642022-01-25 10:43:59 +08005069 ret = -1;
Jerry Yu1a8b4812022-01-20 17:56:50 +08005070 }
5071 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005072 return ret;
Jerry Yu1a8b4812022-01-20 17:56:50 +08005073}
5074
Ronald Crone68ab4f2022-10-05 12:46:29 +02005075#endif /* MBEDTLS_DEBUG_C && MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yu1a8b4812022-01-20 17:56:50 +08005076
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005077/*
Tillmann Karras588ad502015-09-25 04:27:22 +02005078 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005079 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005080int mbedtls_ssl_config_defaults(mbedtls_ssl_config *conf,
5081 int endpoint, int transport, int preset)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005082{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005083#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Janos Follath865b3eb2019-12-16 11:46:15 +00005084 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005085#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005086
Ronald Crone68ab4f2022-10-05 12:46:29 +02005087#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01005088 if (ssl_check_no_sig_alg_duplication(ssl_preset_suiteb_sig_algs)) {
5089 mbedtls_printf("ssl_preset_suiteb_sig_algs has duplicated entries\n");
5090 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu1a8b4812022-01-20 17:56:50 +08005091 }
5092
Gilles Peskine449bd832023-01-11 14:50:10 +01005093 if (ssl_check_no_sig_alg_duplication(ssl_preset_default_sig_algs)) {
5094 mbedtls_printf("ssl_preset_default_sig_algs has duplicated entries\n");
5095 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu1a8b4812022-01-20 17:56:50 +08005096 }
Jerry Yu909df7b2022-01-22 11:56:27 +08005097
5098#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01005099 if (ssl_check_no_sig_alg_duplication(ssl_tls12_preset_suiteb_sig_algs)) {
5100 mbedtls_printf("ssl_tls12_preset_suiteb_sig_algs has duplicated entries\n");
5101 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu909df7b2022-01-22 11:56:27 +08005102 }
5103
Gilles Peskine449bd832023-01-11 14:50:10 +01005104 if (ssl_check_no_sig_alg_duplication(ssl_tls12_preset_default_sig_algs)) {
5105 mbedtls_printf("ssl_tls12_preset_default_sig_algs has duplicated entries\n");
5106 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu909df7b2022-01-22 11:56:27 +08005107 }
5108#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Ronald Crone68ab4f2022-10-05 12:46:29 +02005109#endif /* MBEDTLS_DEBUG_C && MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yu1a8b4812022-01-20 17:56:50 +08005110
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02005111 /* Use the functions here so that they are covered in tests,
5112 * but otherwise access member directly for efficiency */
Gilles Peskine449bd832023-01-11 14:50:10 +01005113 mbedtls_ssl_conf_endpoint(conf, endpoint);
5114 mbedtls_ssl_conf_transport(conf, transport);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005115
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005116 /*
5117 * Things that are common to all presets
5118 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02005119#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005120 if (endpoint == MBEDTLS_SSL_IS_CLIENT) {
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02005121 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
5122#if defined(MBEDTLS_SSL_SESSION_TICKETS)
5123 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
5124#endif
5125 }
5126#endif
5127
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005128#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
5129 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
5130#endif
5131
5132#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
5133 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
5134#endif
5135
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02005136#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005137 conf->f_cookie_write = ssl_cookie_write_dummy;
5138 conf->f_cookie_check = ssl_cookie_check_dummy;
5139#endif
5140
5141#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
5142 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
5143#endif
5144
Janos Follath088ce432017-04-10 12:42:31 +01005145#if defined(MBEDTLS_SSL_SRV_C)
5146 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
TRodziewicz3946f792021-06-14 12:11:18 +02005147 conf->respect_cli_pref = MBEDTLS_SSL_SRV_CIPHERSUITE_ORDER_SERVER;
Janos Follath088ce432017-04-10 12:42:31 +01005148#endif
5149
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005150#if defined(MBEDTLS_SSL_PROTO_DTLS)
5151 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
5152 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
5153#endif
5154
5155#if defined(MBEDTLS_SSL_RENEGOTIATION)
5156 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005157 memset(conf->renego_period, 0x00, 2);
5158 memset(conf->renego_period + 2, 0xFF, 6);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005159#endif
5160
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005161#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005162 if (endpoint == MBEDTLS_SSL_IS_SERVER) {
Hanno Beckere2defad2021-07-24 05:59:17 +01005163 const unsigned char dhm_p[] =
5164 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
5165 const unsigned char dhm_g[] =
5166 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
Hanno Becker00d0a682017-10-04 13:14:29 +01005167
Gilles Peskine449bd832023-01-11 14:50:10 +01005168 if ((ret = mbedtls_ssl_conf_dh_param_bin(conf,
5169 dhm_p, sizeof(dhm_p),
5170 dhm_g, sizeof(dhm_g))) != 0) {
5171 return ret;
Hanno Beckere2defad2021-07-24 05:59:17 +01005172 }
5173 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02005174#endif
5175
Ronald Cron6f135e12021-12-08 16:57:54 +01005176#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu6ee56aa2022-12-06 17:47:22 +08005177
5178#if defined(MBEDTLS_SSL_EARLY_DATA)
Gilles Peskine449bd832023-01-11 14:50:10 +01005179 mbedtls_ssl_tls13_conf_early_data(conf, MBEDTLS_SSL_EARLY_DATA_DISABLED);
Jerry Yu6ee56aa2022-12-06 17:47:22 +08005180#if defined(MBEDTLS_SSL_SRV_C)
5181 mbedtls_ssl_tls13_conf_max_early_data_size(
Gilles Peskine449bd832023-01-11 14:50:10 +01005182 conf, MBEDTLS_SSL_MAX_EARLY_DATA_SIZE);
Jerry Yu6ee56aa2022-12-06 17:47:22 +08005183#endif
5184#endif /* MBEDTLS_SSL_EARLY_DATA */
5185
Jerry Yud0766ec2022-09-22 10:46:57 +08005186#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08005187 mbedtls_ssl_conf_new_session_tickets(
Gilles Peskine449bd832023-01-11 14:50:10 +01005188 conf, MBEDTLS_SSL_TLS1_3_DEFAULT_NEW_SESSION_TICKETS);
Jerry Yu1ad7ace2022-08-09 13:28:39 +08005189#endif
Hanno Becker71f1ed62021-07-24 06:01:47 +01005190 /*
5191 * Allow all TLS 1.3 key exchange modes by default.
5192 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00005193 conf->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Ronald Cron6f135e12021-12-08 16:57:54 +01005194#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01005195
Gilles Peskine449bd832023-01-11 14:50:10 +01005196 if (transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Glenn Strauss2dfcea22022-03-14 17:26:42 -04005197#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
XiaokangQian4d3a6042022-04-21 13:46:17 +00005198 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian060d8672022-04-21 09:24:56 +00005199 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian4d3a6042022-04-21 13:46:17 +00005200#else
Gilles Peskine449bd832023-01-11 14:50:10 +01005201 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQian060d8672022-04-21 09:24:56 +00005202#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005203 } else {
XiaokangQian4d3a6042022-04-21 13:46:17 +00005204#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01005205 if (endpoint == MBEDTLS_SSL_IS_CLIENT) {
XiaokangQian4d3a6042022-04-21 13:46:17 +00005206 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5207 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
Gilles Peskine449bd832023-01-11 14:50:10 +01005208 } else {
5209 /* Hybrid TLS 1.2 / 1.3 is not supported on server side yet */
XiaokangQian4d3a6042022-04-21 13:46:17 +00005210 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5211 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5212 }
5213#elif defined(MBEDTLS_SSL_PROTO_TLS1_3)
5214 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
5215 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
5216#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
5217 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5218 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5219#else
Gilles Peskine449bd832023-01-11 14:50:10 +01005220 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQian4d3a6042022-04-21 13:46:17 +00005221#endif
5222 }
Glenn Strauss2dfcea22022-03-14 17:26:42 -04005223
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005224 /*
5225 * Preset-specific defaults
5226 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005227 switch (preset) {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005228 /*
5229 * NSA Suite B
5230 */
5231 case MBEDTLS_SSL_PRESET_SUITEB:
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005232
Hanno Beckerd60b6c62021-04-29 12:04:11 +01005233 conf->ciphersuite_list = ssl_preset_suiteb_ciphersuites;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005234
5235#if defined(MBEDTLS_X509_CRT_PARSE_C)
5236 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005237#endif
5238
Ronald Crone68ab4f2022-10-05 12:46:29 +02005239#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08005240#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01005241 if (mbedtls_ssl_conf_is_tls12_only(conf)) {
Jerry Yu909df7b2022-01-22 11:56:27 +08005242 conf->sig_algs = ssl_tls12_preset_suiteb_sig_algs;
Gilles Peskine449bd832023-01-11 14:50:10 +01005243 } else
Jerry Yu909df7b2022-01-22 11:56:27 +08005244#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005245 conf->sig_algs = ssl_preset_suiteb_sig_algs;
Ronald Crone68ab4f2022-10-05 12:46:29 +02005246#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005247
Brett Warrene0edc842021-08-17 09:53:13 +01005248#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
5249 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005250#endif
Brett Warrene0edc842021-08-17 09:53:13 +01005251 conf->group_list = ssl_preset_suiteb_groups;
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02005252 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005253
5254 /*
5255 * Default
5256 */
5257 default:
Ronald Cronf6606552022-03-15 11:23:25 +01005258
Hanno Beckerd60b6c62021-04-29 12:04:11 +01005259 conf->ciphersuite_list = mbedtls_ssl_list_ciphersuites();
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005260
5261#if defined(MBEDTLS_X509_CRT_PARSE_C)
5262 conf->cert_profile = &mbedtls_x509_crt_profile_default;
5263#endif
5264
Ronald Crone68ab4f2022-10-05 12:46:29 +02005265#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08005266#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01005267 if (mbedtls_ssl_conf_is_tls12_only(conf)) {
Jerry Yu909df7b2022-01-22 11:56:27 +08005268 conf->sig_algs = ssl_tls12_preset_default_sig_algs;
Gilles Peskine449bd832023-01-11 14:50:10 +01005269 } else
Jerry Yu909df7b2022-01-22 11:56:27 +08005270#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005271 conf->sig_algs = ssl_preset_default_sig_algs;
Ronald Crone68ab4f2022-10-05 12:46:29 +02005272#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005273
Brett Warrene0edc842021-08-17 09:53:13 +01005274#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
5275 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005276#endif
Brett Warrene0edc842021-08-17 09:53:13 +01005277 conf->group_list = ssl_preset_default_groups;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005278
5279#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
5280 conf->dhm_min_bitlen = 1024;
5281#endif
5282 }
5283
Gilles Peskine449bd832023-01-11 14:50:10 +01005284 return 0;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005285}
5286
5287/*
5288 * Free mbedtls_ssl_config
5289 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005290void mbedtls_ssl_config_free(mbedtls_ssl_config *conf)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005291{
5292#if defined(MBEDTLS_DHM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005293 mbedtls_mpi_free(&conf->dhm_P);
5294 mbedtls_mpi_free(&conf->dhm_G);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005295#endif
5296
Ronald Cron73fe8df2022-10-05 14:31:43 +02005297#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02005298#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01005299 if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) {
Neil Armstrong501c9322022-05-03 09:35:09 +02005300 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
5301 }
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005302#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01005303 if (conf->psk != NULL) {
5304 mbedtls_platform_zeroize(conf->psk, conf->psk_len);
5305 mbedtls_free(conf->psk);
Azim Khan27e8a122018-03-21 14:24:11 +00005306 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005307 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09005308 }
5309
Gilles Peskine449bd832023-01-11 14:50:10 +01005310 if (conf->psk_identity != NULL) {
5311 mbedtls_platform_zeroize(conf->psk_identity, conf->psk_identity_len);
5312 mbedtls_free(conf->psk_identity);
Azim Khan27e8a122018-03-21 14:24:11 +00005313 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005314 conf->psk_identity_len = 0;
5315 }
Ronald Cron73fe8df2022-10-05 14:31:43 +02005316#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005317
5318#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005319 ssl_key_cert_free(conf->key_cert);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005320#endif
5321
Gilles Peskine449bd832023-01-11 14:50:10 +01005322 mbedtls_platform_zeroize(conf, sizeof(mbedtls_ssl_config));
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005323}
5324
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02005325#if defined(MBEDTLS_PK_C) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01005326 (defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C))
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005327/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005328 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005329 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005330unsigned char mbedtls_ssl_sig_from_pk(mbedtls_pk_context *pk)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005331{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005332#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005333 if (mbedtls_pk_can_do(pk, MBEDTLS_PK_RSA)) {
5334 return MBEDTLS_SSL_SIG_RSA;
5335 }
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005336#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005337#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005338 if (mbedtls_pk_can_do(pk, MBEDTLS_PK_ECDSA)) {
5339 return MBEDTLS_SSL_SIG_ECDSA;
5340 }
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005341#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005342 return MBEDTLS_SSL_SIG_ANON;
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005343}
5344
Gilles Peskine449bd832023-01-11 14:50:10 +01005345unsigned char mbedtls_ssl_sig_from_pk_alg(mbedtls_pk_type_t type)
Hanno Becker7e5437a2017-04-28 17:15:26 +01005346{
Gilles Peskine449bd832023-01-11 14:50:10 +01005347 switch (type) {
Hanno Becker7e5437a2017-04-28 17:15:26 +01005348 case MBEDTLS_PK_RSA:
Gilles Peskine449bd832023-01-11 14:50:10 +01005349 return MBEDTLS_SSL_SIG_RSA;
Hanno Becker7e5437a2017-04-28 17:15:26 +01005350 case MBEDTLS_PK_ECDSA:
5351 case MBEDTLS_PK_ECKEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01005352 return MBEDTLS_SSL_SIG_ECDSA;
Hanno Becker7e5437a2017-04-28 17:15:26 +01005353 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005354 return MBEDTLS_SSL_SIG_ANON;
Hanno Becker7e5437a2017-04-28 17:15:26 +01005355 }
5356}
5357
Gilles Peskine449bd832023-01-11 14:50:10 +01005358mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig(unsigned char sig)
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005359{
Gilles Peskine449bd832023-01-11 14:50:10 +01005360 switch (sig) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005361#if defined(MBEDTLS_RSA_C)
5362 case MBEDTLS_SSL_SIG_RSA:
Gilles Peskine449bd832023-01-11 14:50:10 +01005363 return MBEDTLS_PK_RSA;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005364#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005365#if defined(MBEDTLS_ECDSA_C)
5366 case MBEDTLS_SSL_SIG_ECDSA:
Gilles Peskine449bd832023-01-11 14:50:10 +01005367 return MBEDTLS_PK_ECDSA;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005368#endif
5369 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005370 return MBEDTLS_PK_NONE;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005371 }
5372}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02005373#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005374
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02005375/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005376 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02005377 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005378mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash(unsigned char hash)
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005379{
Gilles Peskine449bd832023-01-11 14:50:10 +01005380 switch (hash) {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005381#if defined(MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005382 case MBEDTLS_SSL_HASH_MD5:
Gilles Peskine449bd832023-01-11 14:50:10 +01005383 return MBEDTLS_MD_MD5;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005384#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005385#if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005386 case MBEDTLS_SSL_HASH_SHA1:
Gilles Peskine449bd832023-01-11 14:50:10 +01005387 return MBEDTLS_MD_SHA1;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005388#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005389#if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005390 case MBEDTLS_SSL_HASH_SHA224:
Gilles Peskine449bd832023-01-11 14:50:10 +01005391 return MBEDTLS_MD_SHA224;
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02005392#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005393#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005394 case MBEDTLS_SSL_HASH_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01005395 return MBEDTLS_MD_SHA256;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005396#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005397#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005398 case MBEDTLS_SSL_HASH_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01005399 return MBEDTLS_MD_SHA384;
Mateusz Starzyk3352a532021-04-06 14:28:22 +02005400#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005401#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005402 case MBEDTLS_SSL_HASH_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01005403 return MBEDTLS_MD_SHA512;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005404#endif
5405 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005406 return MBEDTLS_MD_NONE;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005407 }
5408}
5409
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005410/*
5411 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
5412 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005413unsigned char mbedtls_ssl_hash_from_md_alg(int md)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005414{
Gilles Peskine449bd832023-01-11 14:50:10 +01005415 switch (md) {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005416#if defined(MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005417 case MBEDTLS_MD_MD5:
Gilles Peskine449bd832023-01-11 14:50:10 +01005418 return MBEDTLS_SSL_HASH_MD5;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005419#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005420#if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005421 case MBEDTLS_MD_SHA1:
Gilles Peskine449bd832023-01-11 14:50:10 +01005422 return MBEDTLS_SSL_HASH_SHA1;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005423#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005424#if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005425 case MBEDTLS_MD_SHA224:
Gilles Peskine449bd832023-01-11 14:50:10 +01005426 return MBEDTLS_SSL_HASH_SHA224;
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02005427#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005428#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005429 case MBEDTLS_MD_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01005430 return MBEDTLS_SSL_HASH_SHA256;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005431#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005432#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005433 case MBEDTLS_MD_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01005434 return MBEDTLS_SSL_HASH_SHA384;
Mateusz Starzyk3352a532021-04-06 14:28:22 +02005435#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005436#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005437 case MBEDTLS_MD_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01005438 return MBEDTLS_SSL_HASH_SHA512;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005439#endif
5440 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005441 return MBEDTLS_SSL_HASH_NONE;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005442 }
5443}
5444
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005445/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005446 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005447 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005448 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005449int mbedtls_ssl_check_curve_tls_id(const mbedtls_ssl_context *ssl, uint16_t tls_id)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005450{
Gilles Peskine449bd832023-01-11 14:50:10 +01005451 const uint16_t *group_list = mbedtls_ssl_get_groups(ssl);
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005452
Gilles Peskine449bd832023-01-11 14:50:10 +01005453 if (group_list == NULL) {
5454 return -1;
Brett Warrene0edc842021-08-17 09:53:13 +01005455 }
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005456
Gilles Peskine449bd832023-01-11 14:50:10 +01005457 for (; *group_list != 0; group_list++) {
5458 if (*group_list == tls_id) {
5459 return 0;
5460 }
5461 }
5462
5463 return -1;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005464}
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005465
5466#if defined(MBEDTLS_ECP_C)
5467/*
5468 * Same as mbedtls_ssl_check_curve_tls_id() but with a mbedtls_ecp_group_id.
5469 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005470int mbedtls_ssl_check_curve(const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id)
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005471{
Gilles Peskine449bd832023-01-11 14:50:10 +01005472 uint16_t tls_id = mbedtls_ssl_get_tls_id_from_ecp_group_id(grp_id);
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005473
Gilles Peskine449bd832023-01-11 14:50:10 +01005474 if (tls_id == 0) {
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005475 return -1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005476 }
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005477
Gilles Peskine449bd832023-01-11 14:50:10 +01005478 return mbedtls_ssl_check_curve_tls_id(ssl, tls_id);
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005479}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02005480#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005481
Gilles Peskine449bd832023-01-11 14:50:10 +01005482#if defined(MBEDTLS_DEBUG_C)
Valerio Setti67419f02023-01-04 16:12:42 +01005483#define EC_NAME(_name_) _name_
5484#else
5485#define EC_NAME(_name_) NULL
5486#endif
5487
Valerio Setti18c9fed2022-12-30 17:44:24 +01005488static const struct {
5489 uint16_t tls_id;
5490 mbedtls_ecp_group_id ecp_group_id;
5491 psa_ecc_family_t psa_family;
5492 uint16_t bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01005493 const char *name;
Valerio Setti18c9fed2022-12-30 17:44:24 +01005494} tls_id_match_table[] =
5495{
5496#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_521)
Gilles Peskine449bd832023-01-11 14:50:10 +01005497 { 25, MBEDTLS_ECP_DP_SECP521R1, PSA_ECC_FAMILY_SECP_R1, 521, EC_NAME("secp521r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005498#endif
5499#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED) || defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512)
Gilles Peskine449bd832023-01-11 14:50:10 +01005500 { 28, MBEDTLS_ECP_DP_BP512R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 512, EC_NAME("brainpoolP512r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005501#endif
Valerio Setti67419f02023-01-04 16:12:42 +01005502#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_384)
Gilles Peskine449bd832023-01-11 14:50:10 +01005503 { 24, MBEDTLS_ECP_DP_SECP384R1, PSA_ECC_FAMILY_SECP_R1, 384, EC_NAME("secp384r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005504#endif
Valerio Setti67419f02023-01-04 16:12:42 +01005505#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED) || defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384)
Gilles Peskine449bd832023-01-11 14:50:10 +01005506 { 27, MBEDTLS_ECP_DP_BP384R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 384, EC_NAME("brainpoolP384r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005507#endif
Valerio Setti67419f02023-01-04 16:12:42 +01005508#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_256)
Gilles Peskine449bd832023-01-11 14:50:10 +01005509 { 23, MBEDTLS_ECP_DP_SECP256R1, PSA_ECC_FAMILY_SECP_R1, 256, EC_NAME("secp256r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005510#endif
5511#if defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED) || defined(PSA_WANT_ECC_SECP_K1_256)
Gilles Peskine449bd832023-01-11 14:50:10 +01005512 { 22, MBEDTLS_ECP_DP_SECP256K1, PSA_ECC_FAMILY_SECP_K1, 256, EC_NAME("secp256k1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005513#endif
Valerio Setti67419f02023-01-04 16:12:42 +01005514#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED) || defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256)
Gilles Peskine449bd832023-01-11 14:50:10 +01005515 { 26, MBEDTLS_ECP_DP_BP256R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 256, EC_NAME("brainpoolP256r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005516#endif
5517#if defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_224)
Gilles Peskine449bd832023-01-11 14:50:10 +01005518 { 21, MBEDTLS_ECP_DP_SECP224R1, PSA_ECC_FAMILY_SECP_R1, 224, EC_NAME("secp224r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005519#endif
5520#if defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED) || defined(PSA_WANT_ECC_SECP_K1_224)
Gilles Peskine449bd832023-01-11 14:50:10 +01005521 { 20, MBEDTLS_ECP_DP_SECP224K1, PSA_ECC_FAMILY_SECP_K1, 224, EC_NAME("secp224k1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005522#endif
5523#if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_192)
Gilles Peskine449bd832023-01-11 14:50:10 +01005524 { 19, MBEDTLS_ECP_DP_SECP192R1, PSA_ECC_FAMILY_SECP_R1, 192, EC_NAME("secp192r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005525#endif
5526#if defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED) || defined(PSA_WANT_ECC_SECP_K1_192)
Gilles Peskine449bd832023-01-11 14:50:10 +01005527 { 18, MBEDTLS_ECP_DP_SECP192K1, PSA_ECC_FAMILY_SECP_K1, 192, EC_NAME("secp192k1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005528#endif
5529#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED) || defined(PSA_WANT_ECC_MONTGOMERY_255)
Gilles Peskine449bd832023-01-11 14:50:10 +01005530 { 29, MBEDTLS_ECP_DP_CURVE25519, PSA_ECC_FAMILY_MONTGOMERY, 255, EC_NAME("x25519") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005531#endif
5532#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED) || defined(PSA_WANT_ECC_MONTGOMERY_448)
Gilles Peskine449bd832023-01-11 14:50:10 +01005533 { 30, MBEDTLS_ECP_DP_CURVE448, PSA_ECC_FAMILY_MONTGOMERY, 448, EC_NAME("x448") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005534#endif
5535 { 0, MBEDTLS_ECP_DP_NONE, 0, 0, NULL },
5536};
5537
Gilles Peskine449bd832023-01-11 14:50:10 +01005538int mbedtls_ssl_get_psa_curve_info_from_tls_id(uint16_t tls_id,
5539 psa_ecc_family_t *family,
5540 size_t *bits)
Valerio Setti18c9fed2022-12-30 17:44:24 +01005541{
Gilles Peskine449bd832023-01-11 14:50:10 +01005542 for (int i = 0; tls_id_match_table[i].tls_id != 0; i++) {
5543 if (tls_id_match_table[i].tls_id == tls_id) {
5544 if (family != NULL) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01005545 *family = tls_id_match_table[i].psa_family;
Gilles Peskine449bd832023-01-11 14:50:10 +01005546 }
5547 if (bits != NULL) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01005548 *bits = tls_id_match_table[i].bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01005549 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01005550 return PSA_SUCCESS;
5551 }
5552 }
5553
5554 return PSA_ERROR_NOT_SUPPORTED;
5555}
5556
Gilles Peskine449bd832023-01-11 14:50:10 +01005557mbedtls_ecp_group_id mbedtls_ssl_get_ecp_group_id_from_tls_id(uint16_t tls_id)
Valerio Setti18c9fed2022-12-30 17:44:24 +01005558{
Gilles Peskine449bd832023-01-11 14:50:10 +01005559 for (int i = 0; tls_id_match_table[i].tls_id != 0; i++) {
5560 if (tls_id_match_table[i].tls_id == tls_id) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01005561 return tls_id_match_table[i].ecp_group_id;
Gilles Peskine449bd832023-01-11 14:50:10 +01005562 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01005563 }
5564
5565 return MBEDTLS_ECP_DP_NONE;
5566}
5567
Gilles Peskine449bd832023-01-11 14:50:10 +01005568uint16_t mbedtls_ssl_get_tls_id_from_ecp_group_id(mbedtls_ecp_group_id grp_id)
Valerio Setti18c9fed2022-12-30 17:44:24 +01005569{
Gilles Peskine449bd832023-01-11 14:50:10 +01005570 for (int i = 0; tls_id_match_table[i].ecp_group_id != MBEDTLS_ECP_DP_NONE;
5571 i++) {
5572 if (tls_id_match_table[i].ecp_group_id == grp_id) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01005573 return tls_id_match_table[i].tls_id;
Gilles Peskine449bd832023-01-11 14:50:10 +01005574 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01005575 }
5576
5577 return 0;
5578}
5579
Valerio Setti67419f02023-01-04 16:12:42 +01005580#if defined(MBEDTLS_DEBUG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005581const char *mbedtls_ssl_get_curve_name_from_tls_id(uint16_t tls_id)
Valerio Setti18c9fed2022-12-30 17:44:24 +01005582{
Gilles Peskine449bd832023-01-11 14:50:10 +01005583 for (int i = 0; tls_id_match_table[i].tls_id != 0; i++) {
5584 if (tls_id_match_table[i].tls_id == tls_id) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01005585 return tls_id_match_table[i].name;
Gilles Peskine449bd832023-01-11 14:50:10 +01005586 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01005587 }
5588
5589 return NULL;
5590}
Valerio Setti67419f02023-01-04 16:12:42 +01005591#endif
Valerio Setti18c9fed2022-12-30 17:44:24 +01005592
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005593#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005594int mbedtls_ssl_check_cert_usage(const mbedtls_x509_crt *cert,
5595 const mbedtls_ssl_ciphersuite_t *ciphersuite,
5596 int cert_endpoint,
5597 uint32_t *flags)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005598{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005599 int ret = 0;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005600 int usage = 0;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005601 const char *ext_oid;
5602 size_t ext_len;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005603
Gilles Peskine449bd832023-01-11 14:50:10 +01005604 if (cert_endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005605 /* Server part of the key exchange */
Gilles Peskine449bd832023-01-11 14:50:10 +01005606 switch (ciphersuite->key_exchange) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005607 case MBEDTLS_KEY_EXCHANGE_RSA:
5608 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005609 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005610 break;
5611
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005612 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
5613 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
5614 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
5615 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005616 break;
5617
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005618 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
5619 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005620 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005621 break;
5622
5623 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005624 case MBEDTLS_KEY_EXCHANGE_NONE:
5625 case MBEDTLS_KEY_EXCHANGE_PSK:
5626 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
5627 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02005628 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005629 usage = 0;
5630 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005631 } else {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005632 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
5633 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005634 }
5635
Gilles Peskine449bd832023-01-11 14:50:10 +01005636 if (mbedtls_x509_crt_check_key_usage(cert, usage) != 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005637 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005638 ret = -1;
5639 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005640
Gilles Peskine449bd832023-01-11 14:50:10 +01005641 if (cert_endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005642 ext_oid = MBEDTLS_OID_SERVER_AUTH;
Gilles Peskine449bd832023-01-11 14:50:10 +01005643 ext_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_SERVER_AUTH);
5644 } else {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005645 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
Gilles Peskine449bd832023-01-11 14:50:10 +01005646 ext_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_CLIENT_AUTH);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005647 }
5648
Gilles Peskine449bd832023-01-11 14:50:10 +01005649 if (mbedtls_x509_crt_check_extended_key_usage(cert, ext_oid, ext_len) != 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005650 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005651 ret = -1;
5652 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005653
Gilles Peskine449bd832023-01-11 14:50:10 +01005654 return ret;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005655}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005656#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02005657
Jerry Yu148165c2021-09-24 23:20:59 +08005658#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01005659int mbedtls_ssl_get_handshake_transcript(mbedtls_ssl_context *ssl,
5660 const mbedtls_md_type_t md,
5661 unsigned char *dst,
5662 size_t dst_len,
5663 size_t *olen)
Jerry Yu148165c2021-09-24 23:20:59 +08005664{
Ronald Cronf6893e12022-01-07 22:09:01 +01005665 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5666 psa_hash_operation_t *hash_operation_to_clone;
5667 psa_hash_operation_t hash_operation = psa_hash_operation_init();
5668
Jerry Yu148165c2021-09-24 23:20:59 +08005669 *olen = 0;
Ronald Cronf6893e12022-01-07 22:09:01 +01005670
Gilles Peskine449bd832023-01-11 14:50:10 +01005671 switch (md) {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005672#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01005673 case MBEDTLS_MD_SHA384:
5674 hash_operation_to_clone = &ssl->handshake->fin_sha384_psa;
5675 break;
Ronald Cronf6893e12022-01-07 22:09:01 +01005676#endif
5677
Andrzej Kurek25f27152022-08-17 16:09:31 -04005678#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01005679 case MBEDTLS_MD_SHA256:
5680 hash_operation_to_clone = &ssl->handshake->fin_sha256_psa;
5681 break;
Ronald Cronf6893e12022-01-07 22:09:01 +01005682#endif
5683
Gilles Peskine449bd832023-01-11 14:50:10 +01005684 default:
5685 goto exit;
5686 }
5687
5688 status = psa_hash_clone(hash_operation_to_clone, &hash_operation);
5689 if (status != PSA_SUCCESS) {
Ronald Cronf6893e12022-01-07 22:09:01 +01005690 goto exit;
5691 }
5692
Gilles Peskine449bd832023-01-11 14:50:10 +01005693 status = psa_hash_finish(&hash_operation, dst, dst_len, olen);
5694 if (status != PSA_SUCCESS) {
Ronald Cronf6893e12022-01-07 22:09:01 +01005695 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005696 }
Ronald Cronf6893e12022-01-07 22:09:01 +01005697
5698exit:
Andrzej Kurekeabeb302022-10-17 07:52:51 -04005699#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
5700 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
5701 (void) ssl;
5702#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005703 return psa_ssl_status_to_mbedtls(status);
Jerry Yu148165c2021-09-24 23:20:59 +08005704}
5705#else /* MBEDTLS_USE_PSA_CRYPTO */
5706
Andrzej Kurek25f27152022-08-17 16:09:31 -04005707#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005708MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01005709static int ssl_get_handshake_transcript_sha384(mbedtls_ssl_context *ssl,
5710 unsigned char *dst,
5711 size_t dst_len,
5712 size_t *olen)
Jerry Yu24c0ec32021-09-09 14:21:07 +08005713{
Jerry Yu24c0ec32021-09-09 14:21:07 +08005714 int ret;
5715 mbedtls_sha512_context sha512;
5716
Gilles Peskine449bd832023-01-11 14:50:10 +01005717 if (dst_len < 48) {
5718 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
5719 }
Jerry Yu24c0ec32021-09-09 14:21:07 +08005720
Gilles Peskine449bd832023-01-11 14:50:10 +01005721 mbedtls_sha512_init(&sha512);
5722 mbedtls_sha512_clone(&sha512, &ssl->handshake->fin_sha384);
Jerry Yu24c0ec32021-09-09 14:21:07 +08005723
Gilles Peskine449bd832023-01-11 14:50:10 +01005724 if ((ret = mbedtls_sha512_finish(&sha512, dst)) != 0) {
5725 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_sha512_finish", ret);
Jerry Yu24c0ec32021-09-09 14:21:07 +08005726 goto exit;
5727 }
5728
5729 *olen = 48;
5730
5731exit:
5732
Gilles Peskine449bd832023-01-11 14:50:10 +01005733 mbedtls_sha512_free(&sha512);
5734 return ret;
Jerry Yu24c0ec32021-09-09 14:21:07 +08005735}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005736#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005737
Andrzej Kurek25f27152022-08-17 16:09:31 -04005738#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005739MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01005740static int ssl_get_handshake_transcript_sha256(mbedtls_ssl_context *ssl,
5741 unsigned char *dst,
5742 size_t dst_len,
5743 size_t *olen)
Jerry Yu24c0ec32021-09-09 14:21:07 +08005744{
Jerry Yu24c0ec32021-09-09 14:21:07 +08005745 int ret;
5746 mbedtls_sha256_context sha256;
5747
Gilles Peskine449bd832023-01-11 14:50:10 +01005748 if (dst_len < 32) {
5749 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
5750 }
Jerry Yu24c0ec32021-09-09 14:21:07 +08005751
Gilles Peskine449bd832023-01-11 14:50:10 +01005752 mbedtls_sha256_init(&sha256);
5753 mbedtls_sha256_clone(&sha256, &ssl->handshake->fin_sha256);
Jerry Yuc5aef882021-12-23 20:15:02 +08005754
Gilles Peskine449bd832023-01-11 14:50:10 +01005755 if ((ret = mbedtls_sha256_finish(&sha256, dst)) != 0) {
5756 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_sha256_finish", ret);
Jerry Yu24c0ec32021-09-09 14:21:07 +08005757 goto exit;
5758 }
5759
5760 *olen = 32;
5761
5762exit:
5763
Gilles Peskine449bd832023-01-11 14:50:10 +01005764 mbedtls_sha256_free(&sha256);
5765 return ret;
Jerry Yu24c0ec32021-09-09 14:21:07 +08005766}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005767#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005768
Gilles Peskine449bd832023-01-11 14:50:10 +01005769int mbedtls_ssl_get_handshake_transcript(mbedtls_ssl_context *ssl,
5770 const mbedtls_md_type_t md,
5771 unsigned char *dst,
5772 size_t dst_len,
5773 size_t *olen)
Jerry Yu24c0ec32021-09-09 14:21:07 +08005774{
Gilles Peskine449bd832023-01-11 14:50:10 +01005775 switch (md) {
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005776
Andrzej Kurek25f27152022-08-17 16:09:31 -04005777#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01005778 case MBEDTLS_MD_SHA384:
5779 return ssl_get_handshake_transcript_sha384(ssl, dst, dst_len, olen);
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005780#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005781
Andrzej Kurek25f27152022-08-17 16:09:31 -04005782#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01005783 case MBEDTLS_MD_SHA256:
5784 return ssl_get_handshake_transcript_sha256(ssl, dst, dst_len, olen);
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005785#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005786
Gilles Peskine449bd832023-01-11 14:50:10 +01005787 default:
Andrzej Kurek409248a2022-10-24 10:33:21 -04005788#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01005789 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
5790 (void) ssl;
5791 (void) dst;
5792 (void) dst_len;
5793 (void) olen;
Andrzej Kurek409248a2022-10-24 10:33:21 -04005794#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005795 break;
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005796 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005797 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu24c0ec32021-09-09 14:21:07 +08005798}
XiaokangQian647719a2021-12-07 09:16:29 +00005799
Jerry Yu148165c2021-09-24 23:20:59 +08005800#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005801
Ronald Crone68ab4f2022-10-05 12:46:29 +02005802#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Gabor Mezei078e8032022-04-27 21:17:56 +02005803/* mbedtls_ssl_parse_sig_alg_ext()
5804 *
5805 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
5806 * value (TLS 1.3 RFC8446):
5807 * enum {
5808 * ....
5809 * ecdsa_secp256r1_sha256( 0x0403 ),
5810 * ecdsa_secp384r1_sha384( 0x0503 ),
5811 * ecdsa_secp521r1_sha512( 0x0603 ),
5812 * ....
5813 * } SignatureScheme;
5814 *
5815 * struct {
5816 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
5817 * } SignatureSchemeList;
5818 *
5819 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
5820 * value (TLS 1.2 RFC5246):
5821 * enum {
5822 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
5823 * sha512(6), (255)
5824 * } HashAlgorithm;
5825 *
5826 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
5827 * SignatureAlgorithm;
5828 *
5829 * struct {
5830 * HashAlgorithm hash;
5831 * SignatureAlgorithm signature;
5832 * } SignatureAndHashAlgorithm;
5833 *
5834 * SignatureAndHashAlgorithm
5835 * supported_signature_algorithms<2..2^16-2>;
5836 *
5837 * The TLS 1.3 signature algorithm extension was defined to be a compatible
5838 * generalization of the TLS 1.2 signature algorithm extension.
5839 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
5840 * `SignatureScheme` field of TLS 1.3
5841 *
5842 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005843int mbedtls_ssl_parse_sig_alg_ext(mbedtls_ssl_context *ssl,
5844 const unsigned char *buf,
5845 const unsigned char *end)
Gabor Mezei078e8032022-04-27 21:17:56 +02005846{
5847 const unsigned char *p = buf;
5848 size_t supported_sig_algs_len = 0;
5849 const unsigned char *supported_sig_algs_end;
5850 uint16_t sig_alg;
5851 uint32_t common_idx = 0;
5852
Gilles Peskine449bd832023-01-11 14:50:10 +01005853 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
5854 supported_sig_algs_len = MBEDTLS_GET_UINT16_BE(p, 0);
Gabor Mezei078e8032022-04-27 21:17:56 +02005855 p += 2;
5856
Gilles Peskine449bd832023-01-11 14:50:10 +01005857 memset(ssl->handshake->received_sig_algs, 0,
5858 sizeof(ssl->handshake->received_sig_algs));
Gabor Mezei078e8032022-04-27 21:17:56 +02005859
Gilles Peskine449bd832023-01-11 14:50:10 +01005860 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, supported_sig_algs_len);
Gabor Mezei078e8032022-04-27 21:17:56 +02005861 supported_sig_algs_end = p + supported_sig_algs_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01005862 while (p < supported_sig_algs_end) {
5863 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, supported_sig_algs_end, 2);
5864 sig_alg = MBEDTLS_GET_UINT16_BE(p, 0);
Gabor Mezei078e8032022-04-27 21:17:56 +02005865 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01005866 MBEDTLS_SSL_DEBUG_MSG(4, ("received signature algorithm: 0x%x %s",
5867 sig_alg,
5868 mbedtls_ssl_sig_alg_to_str(sig_alg)));
Jerry Yu2fe6c632022-06-29 10:02:38 +08005869#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01005870 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 &&
5871 (!(mbedtls_ssl_sig_alg_is_supported(ssl, sig_alg) &&
5872 mbedtls_ssl_sig_alg_is_offered(ssl, sig_alg)))) {
Gabor Mezei078e8032022-04-27 21:17:56 +02005873 continue;
Jerry Yu2fe6c632022-06-29 10:02:38 +08005874 }
5875#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gabor Mezei078e8032022-04-27 21:17:56 +02005876
Gilles Peskine449bd832023-01-11 14:50:10 +01005877 MBEDTLS_SSL_DEBUG_MSG(4, ("valid signature algorithm: %s",
5878 mbedtls_ssl_sig_alg_to_str(sig_alg)));
Gabor Mezei078e8032022-04-27 21:17:56 +02005879
Gilles Peskine449bd832023-01-11 14:50:10 +01005880 if (common_idx + 1 < MBEDTLS_RECEIVED_SIG_ALGS_SIZE) {
Gabor Mezei078e8032022-04-27 21:17:56 +02005881 ssl->handshake->received_sig_algs[common_idx] = sig_alg;
5882 common_idx += 1;
5883 }
5884 }
5885 /* Check that we consumed all the message. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005886 if (p != end) {
5887 MBEDTLS_SSL_DEBUG_MSG(1,
5888 ("Signature algorithms extension length misaligned"));
5889 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
5890 MBEDTLS_ERR_SSL_DECODE_ERROR);
5891 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Gabor Mezei078e8032022-04-27 21:17:56 +02005892 }
5893
Gilles Peskine449bd832023-01-11 14:50:10 +01005894 if (common_idx == 0) {
5895 MBEDTLS_SSL_DEBUG_MSG(3, ("no signature algorithm in common"));
5896 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
5897 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
5898 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Gabor Mezei078e8032022-04-27 21:17:56 +02005899 }
5900
Gabor Mezei15b95a62022-05-09 16:37:58 +02005901 ssl->handshake->received_sig_algs[common_idx] = MBEDTLS_TLS_SIG_NONE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005902 return 0;
Gabor Mezei078e8032022-04-27 21:17:56 +02005903}
5904
Ronald Crone68ab4f2022-10-05 12:46:29 +02005905#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Gabor Mezei078e8032022-04-27 21:17:56 +02005906
Jerry Yudc7bd172022-02-17 13:44:15 +08005907#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5908
5909#if defined(MBEDTLS_USE_PSA_CRYPTO)
5910
Gilles Peskine449bd832023-01-11 14:50:10 +01005911static psa_status_t setup_psa_key_derivation(psa_key_derivation_operation_t *derivation,
5912 mbedtls_svc_key_id_t key,
5913 psa_algorithm_t alg,
5914 const unsigned char *raw_psk, size_t raw_psk_length,
5915 const unsigned char *seed, size_t seed_length,
5916 const unsigned char *label, size_t label_length,
5917 const unsigned char *other_secret,
5918 size_t other_secret_length,
5919 size_t capacity)
Jerry Yudc7bd172022-02-17 13:44:15 +08005920{
5921 psa_status_t status;
5922
Gilles Peskine449bd832023-01-11 14:50:10 +01005923 status = psa_key_derivation_setup(derivation, alg);
5924 if (status != PSA_SUCCESS) {
5925 return status;
5926 }
Jerry Yudc7bd172022-02-17 13:44:15 +08005927
Gilles Peskine449bd832023-01-11 14:50:10 +01005928 if (PSA_ALG_IS_TLS12_PRF(alg) || PSA_ALG_IS_TLS12_PSK_TO_MS(alg)) {
5929 status = psa_key_derivation_input_bytes(derivation,
5930 PSA_KEY_DERIVATION_INPUT_SEED,
5931 seed, seed_length);
5932 if (status != PSA_SUCCESS) {
5933 return status;
Przemek Stekiel1f027032022-04-05 17:12:11 +02005934 }
5935
Gilles Peskine449bd832023-01-11 14:50:10 +01005936 if (other_secret != NULL) {
5937 status = psa_key_derivation_input_bytes(derivation,
5938 PSA_KEY_DERIVATION_INPUT_OTHER_SECRET,
5939 other_secret, other_secret_length);
5940 if (status != PSA_SUCCESS) {
5941 return status;
5942 }
5943 }
5944
5945 if (mbedtls_svc_key_id_is_null(key)) {
Jerry Yudc7bd172022-02-17 13:44:15 +08005946 status = psa_key_derivation_input_bytes(
5947 derivation, PSA_KEY_DERIVATION_INPUT_SECRET,
Gilles Peskine449bd832023-01-11 14:50:10 +01005948 raw_psk, raw_psk_length);
5949 } else {
Jerry Yudc7bd172022-02-17 13:44:15 +08005950 status = psa_key_derivation_input_key(
Gilles Peskine449bd832023-01-11 14:50:10 +01005951 derivation, PSA_KEY_DERIVATION_INPUT_SECRET, key);
Jerry Yudc7bd172022-02-17 13:44:15 +08005952 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005953 if (status != PSA_SUCCESS) {
5954 return status;
5955 }
Jerry Yudc7bd172022-02-17 13:44:15 +08005956
Gilles Peskine449bd832023-01-11 14:50:10 +01005957 status = psa_key_derivation_input_bytes(derivation,
5958 PSA_KEY_DERIVATION_INPUT_LABEL,
5959 label, label_length);
5960 if (status != PSA_SUCCESS) {
5961 return status;
5962 }
5963 } else {
5964 return PSA_ERROR_NOT_SUPPORTED;
Jerry Yudc7bd172022-02-17 13:44:15 +08005965 }
5966
Gilles Peskine449bd832023-01-11 14:50:10 +01005967 status = psa_key_derivation_set_capacity(derivation, capacity);
5968 if (status != PSA_SUCCESS) {
5969 return status;
5970 }
Jerry Yudc7bd172022-02-17 13:44:15 +08005971
Gilles Peskine449bd832023-01-11 14:50:10 +01005972 return PSA_SUCCESS;
Jerry Yudc7bd172022-02-17 13:44:15 +08005973}
5974
Andrzej Kurek57d10632022-10-24 10:32:01 -04005975#if defined(PSA_WANT_ALG_SHA_384) || \
5976 defined(PSA_WANT_ALG_SHA_256)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005977MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01005978static int tls_prf_generic(mbedtls_md_type_t md_type,
5979 const unsigned char *secret, size_t slen,
5980 const char *label,
5981 const unsigned char *random, size_t rlen,
5982 unsigned char *dstbuf, size_t dlen)
Jerry Yudc7bd172022-02-17 13:44:15 +08005983{
5984 psa_status_t status;
5985 psa_algorithm_t alg;
5986 mbedtls_svc_key_id_t master_key = MBEDTLS_SVC_KEY_ID_INIT;
5987 psa_key_derivation_operation_t derivation =
5988 PSA_KEY_DERIVATION_OPERATION_INIT;
5989
Gilles Peskine449bd832023-01-11 14:50:10 +01005990 if (md_type == MBEDTLS_MD_SHA384) {
Jerry Yudc7bd172022-02-17 13:44:15 +08005991 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
Gilles Peskine449bd832023-01-11 14:50:10 +01005992 } else {
Jerry Yudc7bd172022-02-17 13:44:15 +08005993 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
Gilles Peskine449bd832023-01-11 14:50:10 +01005994 }
Jerry Yudc7bd172022-02-17 13:44:15 +08005995
5996 /* Normally a "secret" should be long enough to be impossible to
5997 * find by brute force, and in particular should not be empty. But
5998 * this PRF is also used to derive an IV, in particular in EAP-TLS,
5999 * and for this use case it makes sense to have a 0-length "secret".
6000 * Since the key API doesn't allow importing a key of length 0,
6001 * keep master_key=0, which setup_psa_key_derivation() understands
6002 * to mean a 0-length "secret" input. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006003 if (slen != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006004 psa_key_attributes_t key_attributes = psa_key_attributes_init();
Gilles Peskine449bd832023-01-11 14:50:10 +01006005 psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE);
6006 psa_set_key_algorithm(&key_attributes, alg);
6007 psa_set_key_type(&key_attributes, PSA_KEY_TYPE_DERIVE);
Jerry Yudc7bd172022-02-17 13:44:15 +08006008
Gilles Peskine449bd832023-01-11 14:50:10 +01006009 status = psa_import_key(&key_attributes, secret, slen, &master_key);
6010 if (status != PSA_SUCCESS) {
6011 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
6012 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006013 }
6014
Gilles Peskine449bd832023-01-11 14:50:10 +01006015 status = setup_psa_key_derivation(&derivation,
6016 master_key, alg,
6017 NULL, 0,
6018 random, rlen,
6019 (unsigned char const *) label,
6020 (size_t) strlen(label),
6021 NULL, 0,
6022 dlen);
6023 if (status != PSA_SUCCESS) {
6024 psa_key_derivation_abort(&derivation);
6025 psa_destroy_key(master_key);
6026 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yudc7bd172022-02-17 13:44:15 +08006027 }
6028
Gilles Peskine449bd832023-01-11 14:50:10 +01006029 status = psa_key_derivation_output_bytes(&derivation, dstbuf, dlen);
6030 if (status != PSA_SUCCESS) {
6031 psa_key_derivation_abort(&derivation);
6032 psa_destroy_key(master_key);
6033 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yudc7bd172022-02-17 13:44:15 +08006034 }
6035
Gilles Peskine449bd832023-01-11 14:50:10 +01006036 status = psa_key_derivation_abort(&derivation);
6037 if (status != PSA_SUCCESS) {
6038 psa_destroy_key(master_key);
6039 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yudc7bd172022-02-17 13:44:15 +08006040 }
6041
Gilles Peskine449bd832023-01-11 14:50:10 +01006042 if (!mbedtls_svc_key_id_is_null(master_key)) {
6043 status = psa_destroy_key(master_key);
6044 }
6045 if (status != PSA_SUCCESS) {
6046 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
6047 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006048
Gilles Peskine449bd832023-01-11 14:50:10 +01006049 return 0;
Jerry Yudc7bd172022-02-17 13:44:15 +08006050}
Andrzej Kurek57d10632022-10-24 10:32:01 -04006051#endif /* PSA_WANT_ALG_SHA_256 || PSA_WANT_ALG_SHA_384 */
Jerry Yudc7bd172022-02-17 13:44:15 +08006052#else /* MBEDTLS_USE_PSA_CRYPTO */
6053
Andrzej Kurek57d10632022-10-24 10:32:01 -04006054#if defined(MBEDTLS_MD_C) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01006055 (defined(MBEDTLS_SHA256_C) || \
6056 defined(MBEDTLS_SHA384_C))
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006057MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006058static int tls_prf_generic(mbedtls_md_type_t md_type,
6059 const unsigned char *secret, size_t slen,
6060 const char *label,
6061 const unsigned char *random, size_t rlen,
6062 unsigned char *dstbuf, size_t dlen)
Jerry Yudc7bd172022-02-17 13:44:15 +08006063{
6064 size_t nb;
6065 size_t i, j, k, md_len;
6066 unsigned char *tmp;
6067 size_t tmp_len = 0;
6068 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
6069 const mbedtls_md_info_t *md_info;
6070 mbedtls_md_context_t md_ctx;
6071 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6072
Gilles Peskine449bd832023-01-11 14:50:10 +01006073 mbedtls_md_init(&md_ctx);
Jerry Yudc7bd172022-02-17 13:44:15 +08006074
Gilles Peskine449bd832023-01-11 14:50:10 +01006075 if ((md_info = mbedtls_md_info_from_type(md_type)) == NULL) {
6076 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
6077 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006078
Gilles Peskine449bd832023-01-11 14:50:10 +01006079 md_len = mbedtls_md_get_size(md_info);
Jerry Yudc7bd172022-02-17 13:44:15 +08006080
Gilles Peskine449bd832023-01-11 14:50:10 +01006081 tmp_len = md_len + strlen(label) + rlen;
6082 tmp = mbedtls_calloc(1, tmp_len);
6083 if (tmp == NULL) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006084 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
6085 goto exit;
6086 }
6087
Gilles Peskine449bd832023-01-11 14:50:10 +01006088 nb = strlen(label);
6089 memcpy(tmp + md_len, label, nb);
6090 memcpy(tmp + md_len + nb, random, rlen);
Jerry Yudc7bd172022-02-17 13:44:15 +08006091 nb += rlen;
6092
6093 /*
6094 * Compute P_<hash>(secret, label + random)[0..dlen]
6095 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006096 if ((ret = mbedtls_md_setup(&md_ctx, md_info, 1)) != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006097 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006098 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006099
Gilles Peskine449bd832023-01-11 14:50:10 +01006100 ret = mbedtls_md_hmac_starts(&md_ctx, secret, slen);
6101 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006102 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006103 }
6104 ret = mbedtls_md_hmac_update(&md_ctx, tmp + md_len, nb);
6105 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006106 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006107 }
6108 ret = mbedtls_md_hmac_finish(&md_ctx, tmp);
6109 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006110 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006111 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006112
Gilles Peskine449bd832023-01-11 14:50:10 +01006113 for (i = 0; i < dlen; i += md_len) {
6114 ret = mbedtls_md_hmac_reset(&md_ctx);
6115 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006116 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006117 }
6118 ret = mbedtls_md_hmac_update(&md_ctx, tmp, md_len + nb);
6119 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006120 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006121 }
6122 ret = mbedtls_md_hmac_finish(&md_ctx, h_i);
6123 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006124 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006125 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006126
Gilles Peskine449bd832023-01-11 14:50:10 +01006127 ret = mbedtls_md_hmac_reset(&md_ctx);
6128 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006129 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006130 }
6131 ret = mbedtls_md_hmac_update(&md_ctx, tmp, md_len);
6132 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006133 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006134 }
6135 ret = mbedtls_md_hmac_finish(&md_ctx, tmp);
6136 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006137 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006138 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006139
Gilles Peskine449bd832023-01-11 14:50:10 +01006140 k = (i + md_len > dlen) ? dlen % md_len : md_len;
Jerry Yudc7bd172022-02-17 13:44:15 +08006141
Gilles Peskine449bd832023-01-11 14:50:10 +01006142 for (j = 0; j < k; j++) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006143 dstbuf[i + j] = h_i[j];
Gilles Peskine449bd832023-01-11 14:50:10 +01006144 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006145 }
6146
6147exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006148 mbedtls_md_free(&md_ctx);
Jerry Yudc7bd172022-02-17 13:44:15 +08006149
Gilles Peskine449bd832023-01-11 14:50:10 +01006150 if (tmp != NULL) {
6151 mbedtls_platform_zeroize(tmp, tmp_len);
6152 }
Dave Rodgman29b9b2b2022-11-01 16:08:14 +00006153
Gilles Peskine449bd832023-01-11 14:50:10 +01006154 mbedtls_platform_zeroize(h_i, sizeof(h_i));
Jerry Yudc7bd172022-02-17 13:44:15 +08006155
Gilles Peskine449bd832023-01-11 14:50:10 +01006156 mbedtls_free(tmp);
Jerry Yudc7bd172022-02-17 13:44:15 +08006157
Gilles Peskine449bd832023-01-11 14:50:10 +01006158 return ret;
Jerry Yudc7bd172022-02-17 13:44:15 +08006159}
Andrzej Kurek57d10632022-10-24 10:32:01 -04006160#endif /* MBEDTLS_MD_C && ( MBEDTLS_SHA256_C || MBEDTLS_SHA384_C ) */
Jerry Yudc7bd172022-02-17 13:44:15 +08006161#endif /* MBEDTLS_USE_PSA_CRYPTO */
6162
Andrzej Kurek25f27152022-08-17 16:09:31 -04006163#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006164MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006165static int tls_prf_sha256(const unsigned char *secret, size_t slen,
6166 const char *label,
6167 const unsigned char *random, size_t rlen,
6168 unsigned char *dstbuf, size_t dlen)
Jerry Yudc7bd172022-02-17 13:44:15 +08006169{
Gilles Peskine449bd832023-01-11 14:50:10 +01006170 return tls_prf_generic(MBEDTLS_MD_SHA256, secret, slen,
6171 label, random, rlen, dstbuf, dlen);
Jerry Yudc7bd172022-02-17 13:44:15 +08006172}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04006173#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudc7bd172022-02-17 13:44:15 +08006174
Andrzej Kurek25f27152022-08-17 16:09:31 -04006175#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006176MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006177static int tls_prf_sha384(const unsigned char *secret, size_t slen,
6178 const char *label,
6179 const unsigned char *random, size_t rlen,
6180 unsigned char *dstbuf, size_t dlen)
Jerry Yudc7bd172022-02-17 13:44:15 +08006181{
Gilles Peskine449bd832023-01-11 14:50:10 +01006182 return tls_prf_generic(MBEDTLS_MD_SHA384, secret, slen,
6183 label, random, rlen, dstbuf, dlen);
Jerry Yudc7bd172022-02-17 13:44:15 +08006184}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04006185#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudc7bd172022-02-17 13:44:15 +08006186
Jerry Yuf009d862022-02-17 14:01:37 +08006187/*
6188 * Set appropriate PRF function and other SSL / TLS1.2 functions
6189 *
6190 * Inputs:
Jerry Yuf009d862022-02-17 14:01:37 +08006191 * - hash associated with the ciphersuite (only used by TLS 1.2)
6192 *
6193 * Outputs:
6194 * - the tls_prf, calc_verify and calc_finished members of handshake structure
6195 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006196MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006197static int ssl_set_handshake_prfs(mbedtls_ssl_handshake_params *handshake,
6198 mbedtls_md_type_t hash)
Jerry Yuf009d862022-02-17 14:01:37 +08006199{
Andrzej Kurek25f27152022-08-17 16:09:31 -04006200#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01006201 if (hash == MBEDTLS_MD_SHA384) {
Jerry Yuf009d862022-02-17 14:01:37 +08006202 handshake->tls_prf = tls_prf_sha384;
6203 handshake->calc_verify = ssl_calc_verify_tls_sha384;
6204 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Gilles Peskine449bd832023-01-11 14:50:10 +01006205 } else
Jerry Yuf009d862022-02-17 14:01:37 +08006206#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04006207#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuf009d862022-02-17 14:01:37 +08006208 {
Ronald Cron81591aa2022-03-07 09:05:51 +01006209 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08006210 handshake->tls_prf = tls_prf_sha256;
6211 handshake->calc_verify = ssl_calc_verify_tls_sha256;
6212 handshake->calc_finished = ssl_calc_finished_tls_sha256;
6213 }
Ronald Cron81591aa2022-03-07 09:05:51 +01006214#else
Jerry Yuf009d862022-02-17 14:01:37 +08006215 {
Jerry Yuf009d862022-02-17 14:01:37 +08006216 (void) handshake;
Ronald Cron81591aa2022-03-07 09:05:51 +01006217 (void) hash;
Gilles Peskine449bd832023-01-11 14:50:10 +01006218 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yuf009d862022-02-17 14:01:37 +08006219 }
Ronald Cron81591aa2022-03-07 09:05:51 +01006220#endif
Jerry Yuf009d862022-02-17 14:01:37 +08006221
Gilles Peskine449bd832023-01-11 14:50:10 +01006222 return 0;
Jerry Yuf009d862022-02-17 14:01:37 +08006223}
Jerry Yud6ab2352022-02-17 14:03:43 +08006224
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006225/*
6226 * Compute master secret if needed
6227 *
6228 * Parameters:
6229 * [in/out] handshake
6230 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
6231 * (PSA-PSK) ciphersuite_info, psk_opaque
6232 * [out] premaster (cleared)
6233 * [out] master
6234 * [in] ssl: optionally used for debugging, EMS and PSA-PSK
6235 * debug: conf->f_dbg, conf->p_dbg
6236 * EMS: passed to calc_verify (debug + session_negotiate)
Ronald Crona25cf582022-03-07 11:10:36 +01006237 * PSA-PSA: conf
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006238 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006239MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006240static int ssl_compute_master(mbedtls_ssl_handshake_params *handshake,
6241 unsigned char *master,
6242 const mbedtls_ssl_context *ssl)
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006243{
6244 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6245
6246 /* cf. RFC 5246, Section 8.1:
6247 * "The master secret is always exactly 48 bytes in length." */
6248 size_t const master_secret_len = 48;
6249
6250#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
6251 unsigned char session_hash[48];
6252#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
6253
6254 /* The label for the KDF used for key expansion.
6255 * This is either "master secret" or "extended master secret"
6256 * depending on whether the Extended Master Secret extension
6257 * is used. */
6258 char const *lbl = "master secret";
6259
Przemek Stekielae4ed302022-04-05 17:15:55 +02006260 /* The seed for the KDF used for key expansion.
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006261 * - If the Extended Master Secret extension is not used,
6262 * this is ClientHello.Random + ServerHello.Random
6263 * (see Sect. 8.1 in RFC 5246).
6264 * - If the Extended Master Secret extension is used,
6265 * this is the transcript of the handshake so far.
6266 * (see Sect. 4 in RFC 7627). */
Przemek Stekielae4ed302022-04-05 17:15:55 +02006267 unsigned char const *seed = handshake->randbytes;
6268 size_t seed_len = 64;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006269
6270#if !defined(MBEDTLS_DEBUG_C) && \
6271 !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \
6272 !(defined(MBEDTLS_USE_PSA_CRYPTO) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01006273 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED))
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006274 ssl = NULL; /* make sure we don't use it except for those cases */
6275 (void) ssl;
6276#endif
6277
Gilles Peskine449bd832023-01-11 14:50:10 +01006278 if (handshake->resume != 0) {
6279 MBEDTLS_SSL_DEBUG_MSG(3, ("no premaster (session resumed)"));
6280 return 0;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006281 }
6282
6283#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Gilles Peskine449bd832023-01-11 14:50:10 +01006284 if (handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED) {
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006285 lbl = "extended master secret";
Przemek Stekielae4ed302022-04-05 17:15:55 +02006286 seed = session_hash;
Gilles Peskine449bd832023-01-11 14:50:10 +01006287 handshake->calc_verify(ssl, session_hash, &seed_len);
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006288
Gilles Peskine449bd832023-01-11 14:50:10 +01006289 MBEDTLS_SSL_DEBUG_BUF(3, "session hash for extended master secret",
6290 session_hash, seed_len);
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006291 }
Przemek Stekiel169bf0b2022-04-29 07:53:29 +02006292#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006293
Przemek Stekiel99114f32022-04-22 11:20:09 +02006294#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Przemek Stekiel8a4b7fd2022-04-28 09:22:22 +02006295 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006296 if (mbedtls_ssl_ciphersuite_uses_psk(handshake->ciphersuite_info) == 1) {
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006297 /* Perform PSK-to-MS expansion in a single step. */
6298 psa_status_t status;
6299 psa_algorithm_t alg;
6300 mbedtls_svc_key_id_t psk;
6301 psa_key_derivation_operation_t derivation =
6302 PSA_KEY_DERIVATION_OPERATION_INIT;
6303 mbedtls_md_type_t hash_alg = handshake->ciphersuite_info->mac;
6304
Gilles Peskine449bd832023-01-11 14:50:10 +01006305 MBEDTLS_SSL_DEBUG_MSG(2, ("perform PSA-based PSK-to-MS expansion"));
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006306
Gilles Peskine449bd832023-01-11 14:50:10 +01006307 psk = mbedtls_ssl_get_opaque_psk(ssl);
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006308
Gilles Peskine449bd832023-01-11 14:50:10 +01006309 if (hash_alg == MBEDTLS_MD_SHA384) {
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006310 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
Gilles Peskine449bd832023-01-11 14:50:10 +01006311 } else {
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006312 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
Gilles Peskine449bd832023-01-11 14:50:10 +01006313 }
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006314
Przemek Stekiel51a1f362022-04-13 08:57:06 +02006315 size_t other_secret_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006316 unsigned char *other_secret = NULL;
Przemek Stekielc2033402022-04-05 17:19:41 +02006317
Gilles Peskine449bd832023-01-11 14:50:10 +01006318 switch (handshake->ciphersuite_info->key_exchange) {
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006319 /* Provide other secret.
Przemek Stekiel51a1f362022-04-13 08:57:06 +02006320 * Other secret is stored in premaster, where first 2 bytes hold the
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006321 * length of the other key.
Przemek Stekielc2033402022-04-05 17:19:41 +02006322 */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006323 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Przemek Stekiel8abcee92022-04-28 09:16:28 +02006324 /* For RSA-PSK other key length is always 48 bytes. */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006325 other_secret_len = 48;
6326 other_secret = handshake->premaster + 2;
6327 break;
6328 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006329 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006330 other_secret_len = MBEDTLS_GET_UINT16_BE(handshake->premaster, 0);
6331 other_secret = handshake->premaster + 2;
6332 break;
6333 default:
6334 break;
Przemek Stekielc2033402022-04-05 17:19:41 +02006335 }
6336
Gilles Peskine449bd832023-01-11 14:50:10 +01006337 status = setup_psa_key_derivation(&derivation, psk, alg,
6338 ssl->conf->psk, ssl->conf->psk_len,
6339 seed, seed_len,
6340 (unsigned char const *) lbl,
6341 (size_t) strlen(lbl),
6342 other_secret, other_secret_len,
6343 master_secret_len);
6344 if (status != PSA_SUCCESS) {
6345 psa_key_derivation_abort(&derivation);
6346 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006347 }
6348
Gilles Peskine449bd832023-01-11 14:50:10 +01006349 status = psa_key_derivation_output_bytes(&derivation,
6350 master,
6351 master_secret_len);
6352 if (status != PSA_SUCCESS) {
6353 psa_key_derivation_abort(&derivation);
6354 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006355 }
6356
Gilles Peskine449bd832023-01-11 14:50:10 +01006357 status = psa_key_derivation_abort(&derivation);
6358 if (status != PSA_SUCCESS) {
6359 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
6360 }
6361 } else
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006362#endif
6363 {
Neil Armstrongca7d5062022-05-31 14:43:23 +02006364#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01006365 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
6366 if (handshake->ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE) {
Neil Armstrongca7d5062022-05-31 14:43:23 +02006367 psa_status_t status;
6368 psa_algorithm_t alg = PSA_ALG_TLS12_ECJPAKE_TO_PMS;
6369 psa_key_derivation_operation_t derivation =
6370 PSA_KEY_DERIVATION_OPERATION_INIT;
6371
Gilles Peskine449bd832023-01-11 14:50:10 +01006372 MBEDTLS_SSL_DEBUG_MSG(2, ("perform PSA-based PMS KDF for ECJPAKE"));
Neil Armstrongca7d5062022-05-31 14:43:23 +02006373
6374 handshake->pmslen = PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE;
6375
Gilles Peskine449bd832023-01-11 14:50:10 +01006376 status = psa_key_derivation_setup(&derivation, alg);
6377 if (status != PSA_SUCCESS) {
6378 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Neil Armstrongca7d5062022-05-31 14:43:23 +02006379 }
6380
Gilles Peskine449bd832023-01-11 14:50:10 +01006381 status = psa_key_derivation_set_capacity(&derivation,
6382 PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE);
6383 if (status != PSA_SUCCESS) {
6384 psa_key_derivation_abort(&derivation);
6385 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Neil Armstrongca7d5062022-05-31 14:43:23 +02006386 }
6387
Gilles Peskine449bd832023-01-11 14:50:10 +01006388 status = psa_pake_get_implicit_key(&handshake->psa_pake_ctx,
6389 &derivation);
6390 if (status != PSA_SUCCESS) {
6391 psa_key_derivation_abort(&derivation);
6392 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Neil Armstrongca7d5062022-05-31 14:43:23 +02006393 }
6394
Gilles Peskine449bd832023-01-11 14:50:10 +01006395 status = psa_key_derivation_output_bytes(&derivation,
6396 handshake->premaster,
6397 handshake->pmslen);
6398 if (status != PSA_SUCCESS) {
6399 psa_key_derivation_abort(&derivation);
6400 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
6401 }
6402
6403 status = psa_key_derivation_abort(&derivation);
6404 if (status != PSA_SUCCESS) {
6405 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Neil Armstrongca7d5062022-05-31 14:43:23 +02006406 }
6407 }
6408#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01006409 ret = handshake->tls_prf(handshake->premaster, handshake->pmslen,
6410 lbl, seed, seed_len,
6411 master,
6412 master_secret_len);
6413 if (ret != 0) {
6414 MBEDTLS_SSL_DEBUG_RET(1, "prf", ret);
6415 return ret;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006416 }
6417
Gilles Peskine449bd832023-01-11 14:50:10 +01006418 MBEDTLS_SSL_DEBUG_BUF(3, "premaster secret",
6419 handshake->premaster,
6420 handshake->pmslen);
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006421
Gilles Peskine449bd832023-01-11 14:50:10 +01006422 mbedtls_platform_zeroize(handshake->premaster,
6423 sizeof(handshake->premaster));
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006424 }
6425
Gilles Peskine449bd832023-01-11 14:50:10 +01006426 return 0;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006427}
6428
Gilles Peskine449bd832023-01-11 14:50:10 +01006429int mbedtls_ssl_derive_keys(mbedtls_ssl_context *ssl)
Jerry Yud62f87e2022-02-17 14:09:02 +08006430{
6431 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6432 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
6433 ssl->handshake->ciphersuite_info;
6434
Gilles Peskine449bd832023-01-11 14:50:10 +01006435 MBEDTLS_SSL_DEBUG_MSG(2, ("=> derive keys"));
Jerry Yud62f87e2022-02-17 14:09:02 +08006436
6437 /* Set PRF, calc_verify and calc_finished function pointers */
Gilles Peskine449bd832023-01-11 14:50:10 +01006438 ret = ssl_set_handshake_prfs(ssl->handshake,
6439 ciphersuite_info->mac);
6440 if (ret != 0) {
6441 MBEDTLS_SSL_DEBUG_RET(1, "ssl_set_handshake_prfs", ret);
6442 return ret;
Jerry Yud62f87e2022-02-17 14:09:02 +08006443 }
6444
6445 /* Compute master secret if needed */
Gilles Peskine449bd832023-01-11 14:50:10 +01006446 ret = ssl_compute_master(ssl->handshake,
6447 ssl->session_negotiate->master,
6448 ssl);
6449 if (ret != 0) {
6450 MBEDTLS_SSL_DEBUG_RET(1, "ssl_compute_master", ret);
6451 return ret;
Jerry Yud62f87e2022-02-17 14:09:02 +08006452 }
6453
6454 /* Swap the client and server random values:
6455 * - MS derivation wanted client+server (RFC 5246 8.1)
6456 * - key derivation wants server+client (RFC 5246 6.3) */
6457 {
6458 unsigned char tmp[64];
Gilles Peskine449bd832023-01-11 14:50:10 +01006459 memcpy(tmp, ssl->handshake->randbytes, 64);
6460 memcpy(ssl->handshake->randbytes, tmp + 32, 32);
6461 memcpy(ssl->handshake->randbytes + 32, tmp, 32);
6462 mbedtls_platform_zeroize(tmp, sizeof(tmp));
Jerry Yud62f87e2022-02-17 14:09:02 +08006463 }
6464
6465 /* Populate transform structure */
Gilles Peskine449bd832023-01-11 14:50:10 +01006466 ret = ssl_tls12_populate_transform(ssl->transform_negotiate,
6467 ssl->session_negotiate->ciphersuite,
6468 ssl->session_negotiate->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02006469#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01006470 ssl->session_negotiate->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02006471#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01006472 ssl->handshake->tls_prf,
6473 ssl->handshake->randbytes,
6474 ssl->tls_version,
6475 ssl->conf->endpoint,
6476 ssl);
6477 if (ret != 0) {
6478 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls12_populate_transform", ret);
6479 return ret;
Jerry Yud62f87e2022-02-17 14:09:02 +08006480 }
6481
6482 /* We no longer need Server/ClientHello.random values */
Gilles Peskine449bd832023-01-11 14:50:10 +01006483 mbedtls_platform_zeroize(ssl->handshake->randbytes,
6484 sizeof(ssl->handshake->randbytes));
Jerry Yud62f87e2022-02-17 14:09:02 +08006485
Gilles Peskine449bd832023-01-11 14:50:10 +01006486 MBEDTLS_SSL_DEBUG_MSG(2, ("<= derive keys"));
Jerry Yud62f87e2022-02-17 14:09:02 +08006487
Gilles Peskine449bd832023-01-11 14:50:10 +01006488 return 0;
Jerry Yud62f87e2022-02-17 14:09:02 +08006489}
Jerry Yu8392e0d2022-02-17 14:10:24 +08006490
Gilles Peskine449bd832023-01-11 14:50:10 +01006491int mbedtls_ssl_set_calc_verify_md(mbedtls_ssl_context *ssl, int md)
Ronald Cron4dcbca92022-03-07 10:21:40 +01006492{
Gilles Peskine449bd832023-01-11 14:50:10 +01006493 switch (md) {
Andrzej Kurek25f27152022-08-17 16:09:31 -04006494#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron4dcbca92022-03-07 10:21:40 +01006495 case MBEDTLS_SSL_HASH_SHA384:
6496 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
6497 break;
6498#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04006499#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron4dcbca92022-03-07 10:21:40 +01006500 case MBEDTLS_SSL_HASH_SHA256:
6501 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
6502 break;
6503#endif
6504 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006505 return -1;
Ronald Cron4dcbca92022-03-07 10:21:40 +01006506 }
Andrzej Kurekeabeb302022-10-17 07:52:51 -04006507#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
6508 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
6509 (void) ssl;
6510#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01006511 return 0;
Ronald Cron4dcbca92022-03-07 10:21:40 +01006512}
6513
Andrzej Kurek25f27152022-08-17 16:09:31 -04006514#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01006515void ssl_calc_verify_tls_sha256(const mbedtls_ssl_context *ssl,
6516 unsigned char *hash,
6517 size_t *hlen)
Jerry Yu8392e0d2022-02-17 14:10:24 +08006518{
6519#if defined(MBEDTLS_USE_PSA_CRYPTO)
6520 size_t hash_size;
6521 psa_status_t status;
6522 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
6523
Gilles Peskine449bd832023-01-11 14:50:10 +01006524 MBEDTLS_SSL_DEBUG_MSG(2, ("=> PSA calc verify sha256"));
6525 status = psa_hash_clone(&ssl->handshake->fin_sha256_psa, &sha256_psa);
6526 if (status != PSA_SUCCESS) {
6527 MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash clone failed"));
Jerry Yu8392e0d2022-02-17 14:10:24 +08006528 return;
6529 }
6530
Gilles Peskine449bd832023-01-11 14:50:10 +01006531 status = psa_hash_finish(&sha256_psa, hash, 32, &hash_size);
6532 if (status != PSA_SUCCESS) {
6533 MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash finish failed"));
Jerry Yu8392e0d2022-02-17 14:10:24 +08006534 return;
6535 }
6536
6537 *hlen = 32;
Gilles Peskine449bd832023-01-11 14:50:10 +01006538 MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated verify result", hash, *hlen);
6539 MBEDTLS_SSL_DEBUG_MSG(2, ("<= PSA calc verify"));
Jerry Yu8392e0d2022-02-17 14:10:24 +08006540#else
6541 mbedtls_sha256_context sha256;
6542
Gilles Peskine449bd832023-01-11 14:50:10 +01006543 mbedtls_sha256_init(&sha256);
Jerry Yu8392e0d2022-02-17 14:10:24 +08006544
Gilles Peskine449bd832023-01-11 14:50:10 +01006545 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc verify sha256"));
Jerry Yu8392e0d2022-02-17 14:10:24 +08006546
Gilles Peskine449bd832023-01-11 14:50:10 +01006547 mbedtls_sha256_clone(&sha256, &ssl->handshake->fin_sha256);
6548 mbedtls_sha256_finish(&sha256, hash);
Jerry Yu8392e0d2022-02-17 14:10:24 +08006549
6550 *hlen = 32;
6551
Gilles Peskine449bd832023-01-11 14:50:10 +01006552 MBEDTLS_SSL_DEBUG_BUF(3, "calculated verify result", hash, *hlen);
6553 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc verify"));
Jerry Yu8392e0d2022-02-17 14:10:24 +08006554
Gilles Peskine449bd832023-01-11 14:50:10 +01006555 mbedtls_sha256_free(&sha256);
Jerry Yu8392e0d2022-02-17 14:10:24 +08006556#endif /* MBEDTLS_USE_PSA_CRYPTO */
6557 return;
6558}
Andrzej Kurek25f27152022-08-17 16:09:31 -04006559#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu8392e0d2022-02-17 14:10:24 +08006560
Andrzej Kurek25f27152022-08-17 16:09:31 -04006561#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01006562void ssl_calc_verify_tls_sha384(const mbedtls_ssl_context *ssl,
6563 unsigned char *hash,
6564 size_t *hlen)
Jerry Yuc1cb3842022-02-17 14:13:48 +08006565{
6566#if defined(MBEDTLS_USE_PSA_CRYPTO)
6567 size_t hash_size;
6568 psa_status_t status;
6569 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
6570
Gilles Peskine449bd832023-01-11 14:50:10 +01006571 MBEDTLS_SSL_DEBUG_MSG(2, ("=> PSA calc verify sha384"));
6572 status = psa_hash_clone(&ssl->handshake->fin_sha384_psa, &sha384_psa);
6573 if (status != PSA_SUCCESS) {
6574 MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash clone failed"));
Jerry Yuc1cb3842022-02-17 14:13:48 +08006575 return;
6576 }
6577
Gilles Peskine449bd832023-01-11 14:50:10 +01006578 status = psa_hash_finish(&sha384_psa, hash, 48, &hash_size);
6579 if (status != PSA_SUCCESS) {
6580 MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash finish failed"));
Jerry Yuc1cb3842022-02-17 14:13:48 +08006581 return;
6582 }
6583
6584 *hlen = 48;
Gilles Peskine449bd832023-01-11 14:50:10 +01006585 MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated verify result", hash, *hlen);
6586 MBEDTLS_SSL_DEBUG_MSG(2, ("<= PSA calc verify"));
Jerry Yuc1cb3842022-02-17 14:13:48 +08006587#else
6588 mbedtls_sha512_context sha512;
6589
Gilles Peskine449bd832023-01-11 14:50:10 +01006590 mbedtls_sha512_init(&sha512);
Jerry Yuc1cb3842022-02-17 14:13:48 +08006591
Gilles Peskine449bd832023-01-11 14:50:10 +01006592 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc verify sha384"));
Jerry Yuc1cb3842022-02-17 14:13:48 +08006593
Gilles Peskine449bd832023-01-11 14:50:10 +01006594 mbedtls_sha512_clone(&sha512, &ssl->handshake->fin_sha384);
6595 mbedtls_sha512_finish(&sha512, hash);
Jerry Yuc1cb3842022-02-17 14:13:48 +08006596
6597 *hlen = 48;
6598
Gilles Peskine449bd832023-01-11 14:50:10 +01006599 MBEDTLS_SSL_DEBUG_BUF(3, "calculated verify result", hash, *hlen);
6600 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc verify"));
Jerry Yuc1cb3842022-02-17 14:13:48 +08006601
Gilles Peskine449bd832023-01-11 14:50:10 +01006602 mbedtls_sha512_free(&sha512);
Jerry Yuc1cb3842022-02-17 14:13:48 +08006603#endif /* MBEDTLS_USE_PSA_CRYPTO */
6604 return;
6605}
Andrzej Kurek25f27152022-08-17 16:09:31 -04006606#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yuc1cb3842022-02-17 14:13:48 +08006607
Neil Armstrong80f6f322022-05-03 17:56:38 +02006608#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
6609 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006610int mbedtls_ssl_psk_derive_premaster(mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex)
Jerry Yuce3dca42022-02-17 14:16:37 +08006611{
6612 unsigned char *p = ssl->handshake->premaster;
Gilles Peskine449bd832023-01-11 14:50:10 +01006613 unsigned char *end = p + sizeof(ssl->handshake->premaster);
Jerry Yuce3dca42022-02-17 14:16:37 +08006614 const unsigned char *psk = NULL;
6615 size_t psk_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006616 int psk_ret = mbedtls_ssl_get_psk(ssl, &psk, &psk_len);
Jerry Yuce3dca42022-02-17 14:16:37 +08006617
Gilles Peskine449bd832023-01-11 14:50:10 +01006618 if (psk_ret == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED) {
Jerry Yuce3dca42022-02-17 14:16:37 +08006619 /*
6620 * This should never happen because the existence of a PSK is always
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006621 * checked before calling this function.
6622 *
6623 * The exception is opaque DHE-PSK. For DHE-PSK fill premaster with
Przemek Stekiel8abcee92022-04-28 09:16:28 +02006624 * the shared secret without PSK.
Jerry Yuce3dca42022-02-17 14:16:37 +08006625 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006626 if (key_ex != MBEDTLS_KEY_EXCHANGE_DHE_PSK) {
6627 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
6628 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006629 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006630 }
6631
6632 /*
6633 * PMS = struct {
6634 * opaque other_secret<0..2^16-1>;
6635 * opaque psk<0..2^16-1>;
6636 * };
6637 * with "other_secret" depending on the particular key exchange
6638 */
6639#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006640 if (key_ex == MBEDTLS_KEY_EXCHANGE_PSK) {
6641 if (end - p < 2) {
6642 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6643 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006644
Gilles Peskine449bd832023-01-11 14:50:10 +01006645 MBEDTLS_PUT_UINT16_BE(psk_len, p, 0);
Jerry Yuce3dca42022-02-17 14:16:37 +08006646 p += 2;
6647
Gilles Peskine449bd832023-01-11 14:50:10 +01006648 if (end < p || (size_t) (end - p) < psk_len) {
6649 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6650 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006651
Gilles Peskine449bd832023-01-11 14:50:10 +01006652 memset(p, 0, psk_len);
Jerry Yuce3dca42022-02-17 14:16:37 +08006653 p += psk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01006654 } else
Jerry Yuce3dca42022-02-17 14:16:37 +08006655#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
6656#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006657 if (key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK) {
Jerry Yuce3dca42022-02-17 14:16:37 +08006658 /*
6659 * other_secret already set by the ClientKeyExchange message,
6660 * and is 48 bytes long
6661 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006662 if (end - p < 2) {
6663 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6664 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006665
6666 *p++ = 0;
6667 *p++ = 48;
6668 p += 48;
Gilles Peskine449bd832023-01-11 14:50:10 +01006669 } else
Jerry Yuce3dca42022-02-17 14:16:37 +08006670#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
6671#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006672 if (key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK) {
Jerry Yuce3dca42022-02-17 14:16:37 +08006673 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6674 size_t len;
6675
6676 /* Write length only when we know the actual value */
Gilles Peskine449bd832023-01-11 14:50:10 +01006677 if ((ret = mbedtls_dhm_calc_secret(&ssl->handshake->dhm_ctx,
6678 p + 2, end - (p + 2), &len,
6679 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
6680 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_calc_secret", ret);
6681 return ret;
Jerry Yuce3dca42022-02-17 14:16:37 +08006682 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006683 MBEDTLS_PUT_UINT16_BE(len, p, 0);
Jerry Yuce3dca42022-02-17 14:16:37 +08006684 p += 2 + len;
6685
Gilles Peskine449bd832023-01-11 14:50:10 +01006686 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: K ", &ssl->handshake->dhm_ctx.K);
6687 } else
Jerry Yuce3dca42022-02-17 14:16:37 +08006688#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Neil Armstrong80f6f322022-05-03 17:56:38 +02006689#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006690 if (key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK) {
Jerry Yuce3dca42022-02-17 14:16:37 +08006691 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6692 size_t zlen;
6693
Gilles Peskine449bd832023-01-11 14:50:10 +01006694 if ((ret = mbedtls_ecdh_calc_secret(&ssl->handshake->ecdh_ctx, &zlen,
6695 p + 2, end - (p + 2),
6696 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
6697 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecdh_calc_secret", ret);
6698 return ret;
Jerry Yuce3dca42022-02-17 14:16:37 +08006699 }
6700
Gilles Peskine449bd832023-01-11 14:50:10 +01006701 MBEDTLS_PUT_UINT16_BE(zlen, p, 0);
Jerry Yuce3dca42022-02-17 14:16:37 +08006702 p += 2 + zlen;
6703
Gilles Peskine449bd832023-01-11 14:50:10 +01006704 MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx,
6705 MBEDTLS_DEBUG_ECDH_Z);
6706 } else
Neil Armstrong80f6f322022-05-03 17:56:38 +02006707#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Jerry Yuce3dca42022-02-17 14:16:37 +08006708 {
Gilles Peskine449bd832023-01-11 14:50:10 +01006709 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
6710 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yuce3dca42022-02-17 14:16:37 +08006711 }
6712
6713 /* opaque psk<0..2^16-1>; */
Gilles Peskine449bd832023-01-11 14:50:10 +01006714 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 memcpy(p, psk, psk_len);
Jerry Yuce3dca42022-02-17 14:16:37 +08006726 p += psk_len;
6727
6728 ssl->handshake->pmslen = p - ssl->handshake->premaster;
6729
Gilles Peskine449bd832023-01-11 14:50:10 +01006730 return 0;
Jerry Yuce3dca42022-02-17 14:16:37 +08006731}
Neil Armstrong80f6f322022-05-03 17:56:38 +02006732#endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Jerry Yuc2c673d2022-02-17 14:20:39 +08006733
6734#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006735MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006736static int ssl_write_hello_request(mbedtls_ssl_context *ssl);
Jerry Yuc2c673d2022-02-17 14:20:39 +08006737
6738#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006739int mbedtls_ssl_resend_hello_request(mbedtls_ssl_context *ssl)
Jerry Yuc2c673d2022-02-17 14:20:39 +08006740{
6741 /* If renegotiation is not enforced, retransmit until we would reach max
6742 * timeout if we were using the usual handshake doubling scheme */
Gilles Peskine449bd832023-01-11 14:50:10 +01006743 if (ssl->conf->renego_max_records < 0) {
Jerry Yuc2c673d2022-02-17 14:20:39 +08006744 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
6745 unsigned char doublings = 1;
6746
Gilles Peskine449bd832023-01-11 14:50:10 +01006747 while (ratio != 0) {
Jerry Yuc2c673d2022-02-17 14:20:39 +08006748 ++doublings;
6749 ratio >>= 1;
6750 }
6751
Gilles Peskine449bd832023-01-11 14:50:10 +01006752 if (++ssl->renego_records_seen > doublings) {
6753 MBEDTLS_SSL_DEBUG_MSG(2, ("no longer retransmitting hello request"));
6754 return 0;
Jerry Yuc2c673d2022-02-17 14:20:39 +08006755 }
6756 }
6757
Gilles Peskine449bd832023-01-11 14:50:10 +01006758 return ssl_write_hello_request(ssl);
Jerry Yuc2c673d2022-02-17 14:20:39 +08006759}
6760#endif
6761#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Jerry Yud9526692022-02-17 14:23:47 +08006762
Jerry Yud9526692022-02-17 14:23:47 +08006763/*
6764 * Handshake functions
6765 */
6766#if !defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
6767/* No certificate support -> dummy functions */
Gilles Peskine449bd832023-01-11 14:50:10 +01006768int mbedtls_ssl_write_certificate(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08006769{
6770 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6771 ssl->handshake->ciphersuite_info;
6772
Gilles Peskine449bd832023-01-11 14:50:10 +01006773 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006774
Gilles Peskine449bd832023-01-11 14:50:10 +01006775 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
6776 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006777 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01006778 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08006779 }
6780
Gilles Peskine449bd832023-01-11 14:50:10 +01006781 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
6782 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08006783}
6784
Gilles Peskine449bd832023-01-11 14:50:10 +01006785int mbedtls_ssl_parse_certificate(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08006786{
6787 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6788 ssl->handshake->ciphersuite_info;
6789
Gilles Peskine449bd832023-01-11 14:50:10 +01006790 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006791
Gilles Peskine449bd832023-01-11 14:50:10 +01006792 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
6793 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006794 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01006795 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08006796 }
6797
Gilles Peskine449bd832023-01-11 14:50:10 +01006798 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
6799 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08006800}
6801
6802#else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
6803/* Some certificate support -> implement write and parse */
6804
Gilles Peskine449bd832023-01-11 14:50:10 +01006805int mbedtls_ssl_write_certificate(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08006806{
6807 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
6808 size_t i, n;
6809 const mbedtls_x509_crt *crt;
6810 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6811 ssl->handshake->ciphersuite_info;
6812
Gilles Peskine449bd832023-01-11 14:50:10 +01006813 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006814
Gilles Peskine449bd832023-01-11 14:50:10 +01006815 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
6816 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006817 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01006818 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08006819 }
6820
6821#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01006822 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
6823 if (ssl->handshake->client_auth == 0) {
6824 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006825 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01006826 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08006827 }
6828 }
6829#endif /* MBEDTLS_SSL_CLI_C */
6830#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01006831 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
6832 if (mbedtls_ssl_own_cert(ssl) == NULL) {
Jerry Yud9526692022-02-17 14:23:47 +08006833 /* Should never happen because we shouldn't have picked the
6834 * ciphersuite if we don't have a certificate. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006835 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08006836 }
6837 }
6838#endif
6839
Gilles Peskine449bd832023-01-11 14:50:10 +01006840 MBEDTLS_SSL_DEBUG_CRT(3, "own certificate", mbedtls_ssl_own_cert(ssl));
Jerry Yud9526692022-02-17 14:23:47 +08006841
6842 /*
6843 * 0 . 0 handshake type
6844 * 1 . 3 handshake length
6845 * 4 . 6 length of all certs
6846 * 7 . 9 length of cert. 1
6847 * 10 . n-1 peer certificate
6848 * n . n+2 length of cert. 2
6849 * n+3 . ... upper level cert, etc.
6850 */
6851 i = 7;
Gilles Peskine449bd832023-01-11 14:50:10 +01006852 crt = mbedtls_ssl_own_cert(ssl);
Jerry Yud9526692022-02-17 14:23:47 +08006853
Gilles Peskine449bd832023-01-11 14:50:10 +01006854 while (crt != NULL) {
Jerry Yud9526692022-02-17 14:23:47 +08006855 n = crt->raw.len;
Gilles Peskine449bd832023-01-11 14:50:10 +01006856 if (n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i) {
6857 MBEDTLS_SSL_DEBUG_MSG(1, ("certificate too large, %" MBEDTLS_PRINTF_SIZET
6858 " > %" MBEDTLS_PRINTF_SIZET,
6859 i + 3 + n, (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN));
6860 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
Jerry Yud9526692022-02-17 14:23:47 +08006861 }
6862
Gilles Peskine449bd832023-01-11 14:50:10 +01006863 ssl->out_msg[i] = MBEDTLS_BYTE_2(n);
6864 ssl->out_msg[i + 1] = MBEDTLS_BYTE_1(n);
6865 ssl->out_msg[i + 2] = MBEDTLS_BYTE_0(n);
Jerry Yud9526692022-02-17 14:23:47 +08006866
Gilles Peskine449bd832023-01-11 14:50:10 +01006867 i += 3; memcpy(ssl->out_msg + i, crt->raw.p, n);
Jerry Yud9526692022-02-17 14:23:47 +08006868 i += n; crt = crt->next;
6869 }
6870
Gilles Peskine449bd832023-01-11 14:50:10 +01006871 ssl->out_msg[4] = MBEDTLS_BYTE_2(i - 7);
6872 ssl->out_msg[5] = MBEDTLS_BYTE_1(i - 7);
6873 ssl->out_msg[6] = MBEDTLS_BYTE_0(i - 7);
Jerry Yud9526692022-02-17 14:23:47 +08006874
6875 ssl->out_msglen = i;
6876 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6877 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
6878
6879 ssl->state++;
6880
Gilles Peskine449bd832023-01-11 14:50:10 +01006881 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
6882 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
6883 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08006884 }
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 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08006889}
6890
6891#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6892
6893#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006894MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006895static int ssl_check_peer_crt_unchanged(mbedtls_ssl_context *ssl,
6896 unsigned char *crt_buf,
6897 size_t crt_buf_len)
Jerry Yud9526692022-02-17 14:23:47 +08006898{
6899 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6900
Gilles Peskine449bd832023-01-11 14:50:10 +01006901 if (peer_crt == NULL) {
6902 return -1;
6903 }
Jerry Yud9526692022-02-17 14:23:47 +08006904
Gilles Peskine449bd832023-01-11 14:50:10 +01006905 if (peer_crt->raw.len != crt_buf_len) {
6906 return -1;
6907 }
Jerry Yud9526692022-02-17 14:23:47 +08006908
Gilles Peskine449bd832023-01-11 14:50:10 +01006909 return memcmp(peer_crt->raw.p, crt_buf, peer_crt->raw.len);
Jerry Yud9526692022-02-17 14:23:47 +08006910}
6911#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006912MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006913static int ssl_check_peer_crt_unchanged(mbedtls_ssl_context *ssl,
6914 unsigned char *crt_buf,
6915 size_t crt_buf_len)
Jerry Yud9526692022-02-17 14:23:47 +08006916{
6917 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6918 unsigned char const * const peer_cert_digest =
6919 ssl->session->peer_cert_digest;
6920 mbedtls_md_type_t const peer_cert_digest_type =
6921 ssl->session->peer_cert_digest_type;
6922 mbedtls_md_info_t const * const digest_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01006923 mbedtls_md_info_from_type(peer_cert_digest_type);
Jerry Yud9526692022-02-17 14:23:47 +08006924 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6925 size_t digest_len;
6926
Gilles Peskine449bd832023-01-11 14:50:10 +01006927 if (peer_cert_digest == NULL || digest_info == NULL) {
6928 return -1;
6929 }
Jerry Yud9526692022-02-17 14:23:47 +08006930
Gilles Peskine449bd832023-01-11 14:50:10 +01006931 digest_len = mbedtls_md_get_size(digest_info);
6932 if (digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN) {
6933 return -1;
6934 }
Jerry Yud9526692022-02-17 14:23:47 +08006935
Gilles Peskine449bd832023-01-11 14:50:10 +01006936 ret = mbedtls_md(digest_info, crt_buf, crt_buf_len, tmp_digest);
6937 if (ret != 0) {
6938 return -1;
6939 }
Jerry Yud9526692022-02-17 14:23:47 +08006940
Gilles Peskine449bd832023-01-11 14:50:10 +01006941 return memcmp(tmp_digest, peer_cert_digest, digest_len);
Jerry Yud9526692022-02-17 14:23:47 +08006942}
6943#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6944#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6945
6946/*
6947 * Once the certificate message is read, parse it into a cert chain and
6948 * perform basic checks, but leave actual verification to the caller
6949 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006950MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006951static int ssl_parse_certificate_chain(mbedtls_ssl_context *ssl,
6952 mbedtls_x509_crt *chain)
Jerry Yud9526692022-02-17 14:23:47 +08006953{
6954 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6955#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01006956 int crt_cnt = 0;
Jerry Yud9526692022-02-17 14:23:47 +08006957#endif
6958 size_t i, n;
6959 uint8_t alert;
6960
Gilles Peskine449bd832023-01-11 14:50:10 +01006961 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
6962 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
6963 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6964 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
6965 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Jerry Yud9526692022-02-17 14:23:47 +08006966 }
6967
Gilles Peskine449bd832023-01-11 14:50:10 +01006968 if (ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE) {
6969 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6970 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
6971 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Jerry Yud9526692022-02-17 14:23:47 +08006972 }
6973
Gilles Peskine449bd832023-01-11 14:50:10 +01006974 if (ssl->in_hslen < mbedtls_ssl_hs_hdr_len(ssl) + 3 + 3) {
6975 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
6976 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6977 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
6978 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08006979 }
6980
Gilles Peskine449bd832023-01-11 14:50:10 +01006981 i = mbedtls_ssl_hs_hdr_len(ssl);
Jerry Yud9526692022-02-17 14:23:47 +08006982
6983 /*
6984 * Same message structure as in mbedtls_ssl_write_certificate()
6985 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006986 n = (ssl->in_msg[i+1] << 8) | ssl->in_msg[i+2];
Jerry Yud9526692022-02-17 14:23:47 +08006987
Gilles Peskine449bd832023-01-11 14:50:10 +01006988 if (ssl->in_msg[i] != 0 ||
6989 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len(ssl)) {
6990 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
6991 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6992 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
6993 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08006994 }
6995
6996 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6997 i += 3;
6998
6999 /* Iterate through and parse the CRTs in the provided chain. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007000 while (i < ssl->in_hslen) {
Jerry Yud9526692022-02-17 14:23:47 +08007001 /* Check that there's room for the next CRT's length fields. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007002 if (i + 3 > ssl->in_hslen) {
7003 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7004 mbedtls_ssl_send_alert_message(ssl,
7005 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7006 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
7007 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007008 }
7009 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
7010 * anything beyond 2**16 ~ 64K. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007011 if (ssl->in_msg[i] != 0) {
7012 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7013 mbedtls_ssl_send_alert_message(ssl,
7014 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7015 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT);
7016 return MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Jerry Yud9526692022-02-17 14:23:47 +08007017 }
7018
7019 /* Read length of the next CRT in the chain. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007020 n = ((unsigned int) ssl->in_msg[i + 1] << 8)
Jerry Yud9526692022-02-17 14:23:47 +08007021 | (unsigned int) ssl->in_msg[i + 2];
7022 i += 3;
7023
Gilles Peskine449bd832023-01-11 14:50:10 +01007024 if (n < 128 || i + n > ssl->in_hslen) {
7025 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7026 mbedtls_ssl_send_alert_message(ssl,
7027 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7028 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
7029 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007030 }
7031
7032 /* Check if we're handling the first CRT in the chain. */
7033#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007034 if (crt_cnt++ == 0 &&
Jerry Yud9526692022-02-17 14:23:47 +08007035 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Gilles Peskine449bd832023-01-11 14:50:10 +01007036 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
Jerry Yud9526692022-02-17 14:23:47 +08007037 /* During client-side renegotiation, check that the server's
7038 * end-CRTs hasn't changed compared to the initial handshake,
7039 * mitigating the triple handshake attack. On success, reuse
7040 * the original end-CRT instead of parsing it again. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007041 MBEDTLS_SSL_DEBUG_MSG(3, ("Check that peer CRT hasn't changed during renegotiation"));
7042 if (ssl_check_peer_crt_unchanged(ssl,
7043 &ssl->in_msg[i],
7044 n) != 0) {
7045 MBEDTLS_SSL_DEBUG_MSG(1, ("new server cert during renegotiation"));
7046 mbedtls_ssl_send_alert_message(ssl,
7047 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7048 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED);
7049 return MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Jerry Yud9526692022-02-17 14:23:47 +08007050 }
7051
7052 /* Now we can safely free the original chain. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007053 ssl_clear_peer_cert(ssl->session);
Jerry Yud9526692022-02-17 14:23:47 +08007054 }
7055#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
7056
7057 /* Parse the next certificate in the chain. */
7058#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007059 ret = mbedtls_x509_crt_parse_der(chain, ssl->in_msg + i, n);
Jerry Yud9526692022-02-17 14:23:47 +08007060#else
7061 /* If we don't need to store the CRT chain permanently, parse
7062 * it in-place from the input buffer instead of making a copy. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007063 ret = mbedtls_x509_crt_parse_der_nocopy(chain, ssl->in_msg + i, n);
Jerry Yud9526692022-02-17 14:23:47 +08007064#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007065 switch (ret) {
Jerry Yud9526692022-02-17 14:23:47 +08007066 case 0: /*ok*/
7067 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
7068 /* Ignore certificate with an unknown algorithm: maybe a
7069 prior certificate was already trusted. */
7070 break;
7071
7072 case MBEDTLS_ERR_X509_ALLOC_FAILED:
7073 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
7074 goto crt_parse_der_failed;
7075
7076 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
7077 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7078 goto crt_parse_der_failed;
7079
7080 default:
7081 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007082crt_parse_der_failed:
7083 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert);
7084 MBEDTLS_SSL_DEBUG_RET(1, " mbedtls_x509_crt_parse_der", ret);
7085 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007086 }
7087
7088 i += n;
7089 }
7090
Gilles Peskine449bd832023-01-11 14:50:10 +01007091 MBEDTLS_SSL_DEBUG_CRT(3, "peer certificate", chain);
7092 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08007093}
7094
7095#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007096MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007097static int ssl_srv_check_client_no_crt_notification(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08007098{
Gilles Peskine449bd832023-01-11 14:50:10 +01007099 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
7100 return -1;
7101 }
Jerry Yud9526692022-02-17 14:23:47 +08007102
Gilles Peskine449bd832023-01-11 14:50:10 +01007103 if (ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len(ssl) &&
Jerry Yud9526692022-02-17 14:23:47 +08007104 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
7105 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
Gilles Peskine449bd832023-01-11 14:50:10 +01007106 memcmp(ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl), "\0\0\0", 3) == 0) {
7107 MBEDTLS_SSL_DEBUG_MSG(1, ("peer has no certificate"));
7108 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08007109 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007110 return -1;
Jerry Yud9526692022-02-17 14:23:47 +08007111}
7112#endif /* MBEDTLS_SSL_SRV_C */
7113
7114/* Check if a certificate message is expected.
7115 * Return either
7116 * - SSL_CERTIFICATE_EXPECTED, or
7117 * - SSL_CERTIFICATE_SKIP
7118 * indicating whether a Certificate message is expected or not.
7119 */
7120#define SSL_CERTIFICATE_EXPECTED 0
7121#define SSL_CERTIFICATE_SKIP 1
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007122MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007123static int ssl_parse_certificate_coordinate(mbedtls_ssl_context *ssl,
7124 int authmode)
Jerry Yud9526692022-02-17 14:23:47 +08007125{
7126 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
7127 ssl->handshake->ciphersuite_info;
7128
Gilles Peskine449bd832023-01-11 14:50:10 +01007129 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
7130 return SSL_CERTIFICATE_SKIP;
7131 }
Jerry Yud9526692022-02-17 14:23:47 +08007132
7133#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007134 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
7135 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK) {
7136 return SSL_CERTIFICATE_SKIP;
7137 }
Jerry Yud9526692022-02-17 14:23:47 +08007138
Gilles Peskine449bd832023-01-11 14:50:10 +01007139 if (authmode == MBEDTLS_SSL_VERIFY_NONE) {
Jerry Yud9526692022-02-17 14:23:47 +08007140 ssl->session_negotiate->verify_result =
7141 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
Gilles Peskine449bd832023-01-11 14:50:10 +01007142 return SSL_CERTIFICATE_SKIP;
Jerry Yud9526692022-02-17 14:23:47 +08007143 }
7144 }
7145#else
7146 ((void) authmode);
7147#endif /* MBEDTLS_SSL_SRV_C */
7148
Gilles Peskine449bd832023-01-11 14:50:10 +01007149 return SSL_CERTIFICATE_EXPECTED;
Jerry Yud9526692022-02-17 14:23:47 +08007150}
7151
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007152MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007153static int ssl_parse_certificate_verify(mbedtls_ssl_context *ssl,
7154 int authmode,
7155 mbedtls_x509_crt *chain,
7156 void *rs_ctx)
Jerry Yud9526692022-02-17 14:23:47 +08007157{
7158 int ret = 0;
7159 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
7160 ssl->handshake->ciphersuite_info;
7161 int have_ca_chain = 0;
7162
7163 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
7164 void *p_vrfy;
7165
Gilles Peskine449bd832023-01-11 14:50:10 +01007166 if (authmode == MBEDTLS_SSL_VERIFY_NONE) {
7167 return 0;
7168 }
Jerry Yud9526692022-02-17 14:23:47 +08007169
Gilles Peskine449bd832023-01-11 14:50:10 +01007170 if (ssl->f_vrfy != NULL) {
7171 MBEDTLS_SSL_DEBUG_MSG(3, ("Use context-specific verification callback"));
Jerry Yud9526692022-02-17 14:23:47 +08007172 f_vrfy = ssl->f_vrfy;
7173 p_vrfy = ssl->p_vrfy;
Gilles Peskine449bd832023-01-11 14:50:10 +01007174 } else {
7175 MBEDTLS_SSL_DEBUG_MSG(3, ("Use configuration-specific verification callback"));
Jerry Yud9526692022-02-17 14:23:47 +08007176 f_vrfy = ssl->conf->f_vrfy;
7177 p_vrfy = ssl->conf->p_vrfy;
7178 }
7179
7180 /*
7181 * Main check: verify certificate
7182 */
7183#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Gilles Peskine449bd832023-01-11 14:50:10 +01007184 if (ssl->conf->f_ca_cb != NULL) {
Jerry Yud9526692022-02-17 14:23:47 +08007185 ((void) rs_ctx);
7186 have_ca_chain = 1;
7187
Gilles Peskine449bd832023-01-11 14:50:10 +01007188 MBEDTLS_SSL_DEBUG_MSG(3, ("use CA callback for X.509 CRT verification"));
Jerry Yud9526692022-02-17 14:23:47 +08007189 ret = mbedtls_x509_crt_verify_with_ca_cb(
7190 chain,
7191 ssl->conf->f_ca_cb,
7192 ssl->conf->p_ca_cb,
7193 ssl->conf->cert_profile,
7194 ssl->hostname,
7195 &ssl->session_negotiate->verify_result,
Gilles Peskine449bd832023-01-11 14:50:10 +01007196 f_vrfy, p_vrfy);
7197 } else
Jerry Yud9526692022-02-17 14:23:47 +08007198#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
7199 {
7200 mbedtls_x509_crt *ca_chain;
7201 mbedtls_x509_crl *ca_crl;
7202
7203#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01007204 if (ssl->handshake->sni_ca_chain != NULL) {
Jerry Yud9526692022-02-17 14:23:47 +08007205 ca_chain = ssl->handshake->sni_ca_chain;
7206 ca_crl = ssl->handshake->sni_ca_crl;
Gilles Peskine449bd832023-01-11 14:50:10 +01007207 } else
Jerry Yud9526692022-02-17 14:23:47 +08007208#endif
7209 {
7210 ca_chain = ssl->conf->ca_chain;
7211 ca_crl = ssl->conf->ca_crl;
7212 }
7213
Gilles Peskine449bd832023-01-11 14:50:10 +01007214 if (ca_chain != NULL) {
Jerry Yud9526692022-02-17 14:23:47 +08007215 have_ca_chain = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01007216 }
Jerry Yud9526692022-02-17 14:23:47 +08007217
7218 ret = mbedtls_x509_crt_verify_restartable(
7219 chain,
7220 ca_chain, ca_crl,
7221 ssl->conf->cert_profile,
7222 ssl->hostname,
7223 &ssl->session_negotiate->verify_result,
Gilles Peskine449bd832023-01-11 14:50:10 +01007224 f_vrfy, p_vrfy, rs_ctx);
Jerry Yud9526692022-02-17 14:23:47 +08007225 }
7226
Gilles Peskine449bd832023-01-11 14:50:10 +01007227 if (ret != 0) {
7228 MBEDTLS_SSL_DEBUG_RET(1, "x509_verify_cert", ret);
Jerry Yud9526692022-02-17 14:23:47 +08007229 }
7230
7231#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007232 if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
7233 return MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
7234 }
Jerry Yud9526692022-02-17 14:23:47 +08007235#endif
7236
7237 /*
7238 * Secondary checks: always done, but change 'ret' only if it was 0
7239 */
7240
7241#if defined(MBEDTLS_ECP_C)
7242 {
7243 const mbedtls_pk_context *pk = &chain->pk;
7244
Manuel Pégourié-Gonnard66b0d612022-06-17 10:49:29 +02007245 /* If certificate uses an EC key, make sure the curve is OK.
7246 * This is a public key, so it can't be opaque, so can_do() is a good
7247 * enough check to ensure pk_ec() is safe to use here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007248 if (mbedtls_pk_can_do(pk, MBEDTLS_PK_ECKEY)) {
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007249 /* and in the unlikely case the above assumption no longer holds
7250 * we are making sure that pk_ec() here does not return a NULL
7251 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007252 const mbedtls_ecp_keypair *ec = mbedtls_pk_ec(*pk);
7253 if (ec == NULL) {
7254 MBEDTLS_SSL_DEBUG_MSG(1, ("mbedtls_pk_ec() returned NULL"));
7255 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007256 }
Jerry Yud9526692022-02-17 14:23:47 +08007257
Gilles Peskine449bd832023-01-11 14:50:10 +01007258 if (mbedtls_ssl_check_curve(ssl, ec->grp.id) != 0) {
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007259 ssl->session_negotiate->verify_result |=
7260 MBEDTLS_X509_BADCERT_BAD_KEY;
7261
Gilles Peskine449bd832023-01-11 14:50:10 +01007262 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate (EC key curve)"));
7263 if (ret == 0) {
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007264 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Gilles Peskine449bd832023-01-11 14:50:10 +01007265 }
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007266 }
Jerry Yud9526692022-02-17 14:23:47 +08007267 }
7268 }
7269#endif /* MBEDTLS_ECP_C */
7270
Gilles Peskine449bd832023-01-11 14:50:10 +01007271 if (mbedtls_ssl_check_cert_usage(chain,
7272 ciphersuite_info,
7273 !ssl->conf->endpoint,
7274 &ssl->session_negotiate->verify_result) != 0) {
7275 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate (usage extensions)"));
7276 if (ret == 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007277 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Gilles Peskine449bd832023-01-11 14:50:10 +01007278 }
Jerry Yud9526692022-02-17 14:23:47 +08007279 }
7280
7281 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
7282 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
7283 * with details encoded in the verification flags. All other kinds
7284 * of error codes, including those from the user provided f_vrfy
7285 * functions, are treated as fatal and lead to a failure of
7286 * ssl_parse_certificate even if verification was optional. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007287 if (authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
7288 (ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
7289 ret == MBEDTLS_ERR_SSL_BAD_CERTIFICATE)) {
Jerry Yud9526692022-02-17 14:23:47 +08007290 ret = 0;
7291 }
7292
Gilles Peskine449bd832023-01-11 14:50:10 +01007293 if (have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED) {
7294 MBEDTLS_SSL_DEBUG_MSG(1, ("got no CA chain"));
Jerry Yud9526692022-02-17 14:23:47 +08007295 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
7296 }
7297
Gilles Peskine449bd832023-01-11 14:50:10 +01007298 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007299 uint8_t alert;
7300
7301 /* The certificate may have been rejected for several reasons.
7302 Pick one and send the corresponding alert. Which alert to send
7303 may be a subject of debate in some cases. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007304 if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER) {
Jerry Yud9526692022-02-17 14:23:47 +08007305 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
Gilles Peskine449bd832023-01-11 14:50:10 +01007306 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH) {
Jerry Yud9526692022-02-17 14:23:47 +08007307 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007308 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE) {
Jerry Yud9526692022-02-17 14:23:47 +08007309 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007310 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE) {
Jerry Yud9526692022-02-17 14:23:47 +08007311 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007312 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE) {
Jerry Yud9526692022-02-17 14:23:47 +08007313 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007314 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK) {
Jerry Yud9526692022-02-17 14:23:47 +08007315 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007316 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY) {
Jerry Yud9526692022-02-17 14:23:47 +08007317 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007318 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED) {
Jerry Yud9526692022-02-17 14:23:47 +08007319 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
Gilles Peskine449bd832023-01-11 14:50:10 +01007320 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED) {
Jerry Yud9526692022-02-17 14:23:47 +08007321 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
Gilles Peskine449bd832023-01-11 14:50:10 +01007322 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED) {
Jerry Yud9526692022-02-17 14:23:47 +08007323 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
Gilles Peskine449bd832023-01-11 14:50:10 +01007324 } else {
Jerry Yud9526692022-02-17 14:23:47 +08007325 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
Gilles Peskine449bd832023-01-11 14:50:10 +01007326 }
7327 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7328 alert);
Jerry Yud9526692022-02-17 14:23:47 +08007329 }
7330
7331#if defined(MBEDTLS_DEBUG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007332 if (ssl->session_negotiate->verify_result != 0) {
7333 MBEDTLS_SSL_DEBUG_MSG(3, ("! Certificate verification flags %08x",
7334 (unsigned int) ssl->session_negotiate->verify_result));
7335 } else {
7336 MBEDTLS_SSL_DEBUG_MSG(3, ("Certificate verification flags clear"));
Jerry Yud9526692022-02-17 14:23:47 +08007337 }
7338#endif /* MBEDTLS_DEBUG_C */
7339
Gilles Peskine449bd832023-01-11 14:50:10 +01007340 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007341}
7342
7343#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007344MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007345static int ssl_remember_peer_crt_digest(mbedtls_ssl_context *ssl,
7346 unsigned char *start, size_t len)
Jerry Yud9526692022-02-17 14:23:47 +08007347{
7348 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7349 /* Remember digest of the peer's end-CRT. */
7350 ssl->session_negotiate->peer_cert_digest =
Gilles Peskine449bd832023-01-11 14:50:10 +01007351 mbedtls_calloc(1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN);
7352 if (ssl->session_negotiate->peer_cert_digest == NULL) {
7353 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%d bytes) failed",
7354 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN));
7355 mbedtls_ssl_send_alert_message(ssl,
7356 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7357 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
Jerry Yud9526692022-02-17 14:23:47 +08007358
Gilles Peskine449bd832023-01-11 14:50:10 +01007359 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Jerry Yud9526692022-02-17 14:23:47 +08007360 }
7361
Gilles Peskine449bd832023-01-11 14:50:10 +01007362 ret = mbedtls_md(mbedtls_md_info_from_type(
7363 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE),
7364 start, len,
7365 ssl->session_negotiate->peer_cert_digest);
Jerry Yud9526692022-02-17 14:23:47 +08007366
7367 ssl->session_negotiate->peer_cert_digest_type =
7368 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
7369 ssl->session_negotiate->peer_cert_digest_len =
7370 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
7371
Gilles Peskine449bd832023-01-11 14:50:10 +01007372 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007373}
7374
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007375MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007376static int ssl_remember_peer_pubkey(mbedtls_ssl_context *ssl,
7377 unsigned char *start, size_t len)
Jerry Yud9526692022-02-17 14:23:47 +08007378{
7379 unsigned char *end = start + len;
7380 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7381
7382 /* Make a copy of the peer's raw public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007383 mbedtls_pk_init(&ssl->handshake->peer_pubkey);
7384 ret = mbedtls_pk_parse_subpubkey(&start, end,
7385 &ssl->handshake->peer_pubkey);
7386 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007387 /* We should have parsed the public key before. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007388 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007389 }
7390
Gilles Peskine449bd832023-01-11 14:50:10 +01007391 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08007392}
7393#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7394
Gilles Peskine449bd832023-01-11 14:50:10 +01007395int mbedtls_ssl_parse_certificate(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08007396{
7397 int ret = 0;
7398 int crt_expected;
7399#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7400 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
7401 ? ssl->handshake->sni_authmode
7402 : ssl->conf->authmode;
7403#else
7404 const int authmode = ssl->conf->authmode;
7405#endif
7406 void *rs_ctx = NULL;
7407 mbedtls_x509_crt *chain = NULL;
7408
Gilles Peskine449bd832023-01-11 14:50:10 +01007409 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007410
Gilles Peskine449bd832023-01-11 14:50:10 +01007411 crt_expected = ssl_parse_certificate_coordinate(ssl, authmode);
7412 if (crt_expected == SSL_CERTIFICATE_SKIP) {
7413 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007414 goto exit;
7415 }
7416
7417#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007418 if (ssl->handshake->ecrs_enabled &&
7419 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify) {
Jerry Yud9526692022-02-17 14:23:47 +08007420 chain = ssl->handshake->ecrs_peer_cert;
7421 ssl->handshake->ecrs_peer_cert = NULL;
7422 goto crt_verify;
7423 }
7424#endif
7425
Gilles Peskine449bd832023-01-11 14:50:10 +01007426 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007427 /* mbedtls_ssl_read_record may have sent an alert already. We
7428 let it decide whether to alert. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007429 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
Jerry Yud9526692022-02-17 14:23:47 +08007430 goto exit;
7431 }
7432
7433#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007434 if (ssl_srv_check_client_no_crt_notification(ssl) == 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007435 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
7436
Gilles Peskine449bd832023-01-11 14:50:10 +01007437 if (authmode != MBEDTLS_SSL_VERIFY_OPTIONAL) {
Jerry Yud9526692022-02-17 14:23:47 +08007438 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Gilles Peskine449bd832023-01-11 14:50:10 +01007439 }
Jerry Yud9526692022-02-17 14:23:47 +08007440
7441 goto exit;
7442 }
7443#endif /* MBEDTLS_SSL_SRV_C */
7444
7445 /* Clear existing peer CRT structure in case we tried to
7446 * reuse a session but it failed, and allocate a new one. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007447 ssl_clear_peer_cert(ssl->session_negotiate);
Jerry Yud9526692022-02-17 14:23:47 +08007448
Gilles Peskine449bd832023-01-11 14:50:10 +01007449 chain = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
7450 if (chain == NULL) {
7451 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed",
7452 sizeof(mbedtls_x509_crt)));
7453 mbedtls_ssl_send_alert_message(ssl,
7454 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7455 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
Jerry Yud9526692022-02-17 14:23:47 +08007456
7457 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
7458 goto exit;
7459 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007460 mbedtls_x509_crt_init(chain);
Jerry Yud9526692022-02-17 14:23:47 +08007461
Gilles Peskine449bd832023-01-11 14:50:10 +01007462 ret = ssl_parse_certificate_chain(ssl, chain);
7463 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007464 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007465 }
Jerry Yud9526692022-02-17 14:23:47 +08007466
7467#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007468 if (ssl->handshake->ecrs_enabled) {
Jerry Yud9526692022-02-17 14:23:47 +08007469 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Gilles Peskine449bd832023-01-11 14:50:10 +01007470 }
Jerry Yud9526692022-02-17 14:23:47 +08007471
7472crt_verify:
Gilles Peskine449bd832023-01-11 14:50:10 +01007473 if (ssl->handshake->ecrs_enabled) {
Jerry Yud9526692022-02-17 14:23:47 +08007474 rs_ctx = &ssl->handshake->ecrs_ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +01007475 }
Jerry Yud9526692022-02-17 14:23:47 +08007476#endif
7477
Gilles Peskine449bd832023-01-11 14:50:10 +01007478 ret = ssl_parse_certificate_verify(ssl, authmode,
7479 chain, rs_ctx);
7480 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007481 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007482 }
Jerry Yud9526692022-02-17 14:23:47 +08007483
7484#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7485 {
7486 unsigned char *crt_start, *pk_start;
7487 size_t crt_len, pk_len;
7488
7489 /* We parse the CRT chain without copying, so
7490 * these pointers point into the input buffer,
7491 * and are hence still valid after freeing the
7492 * CRT chain. */
7493
7494 crt_start = chain->raw.p;
7495 crt_len = chain->raw.len;
7496
7497 pk_start = chain->pk_raw.p;
7498 pk_len = chain->pk_raw.len;
7499
7500 /* Free the CRT structures before computing
7501 * digest and copying the peer's public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007502 mbedtls_x509_crt_free(chain);
7503 mbedtls_free(chain);
Jerry Yud9526692022-02-17 14:23:47 +08007504 chain = NULL;
7505
Gilles Peskine449bd832023-01-11 14:50:10 +01007506 ret = ssl_remember_peer_crt_digest(ssl, crt_start, crt_len);
7507 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007508 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007509 }
Jerry Yud9526692022-02-17 14:23:47 +08007510
Gilles Peskine449bd832023-01-11 14:50:10 +01007511 ret = ssl_remember_peer_pubkey(ssl, pk_start, pk_len);
7512 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007513 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007514 }
Jerry Yud9526692022-02-17 14:23:47 +08007515 }
7516#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7517 /* Pass ownership to session structure. */
7518 ssl->session_negotiate->peer_cert = chain;
7519 chain = NULL;
7520#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7521
Gilles Peskine449bd832023-01-11 14:50:10 +01007522 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007523
7524exit:
7525
Gilles Peskine449bd832023-01-11 14:50:10 +01007526 if (ret == 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007527 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01007528 }
Jerry Yud9526692022-02-17 14:23:47 +08007529
7530#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007531 if (ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS) {
Jerry Yud9526692022-02-17 14:23:47 +08007532 ssl->handshake->ecrs_peer_cert = chain;
7533 chain = NULL;
7534 }
7535#endif
7536
Gilles Peskine449bd832023-01-11 14:50:10 +01007537 if (chain != NULL) {
7538 mbedtls_x509_crt_free(chain);
7539 mbedtls_free(chain);
Jerry Yud9526692022-02-17 14:23:47 +08007540 }
7541
Gilles Peskine449bd832023-01-11 14:50:10 +01007542 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007543}
7544#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
7545
Andrzej Kurek25f27152022-08-17 16:09:31 -04007546#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu615bd6f2022-02-17 14:25:15 +08007547static void ssl_calc_finished_tls_sha256(
Gilles Peskine449bd832023-01-11 14:50:10 +01007548 mbedtls_ssl_context *ssl, unsigned char *buf, int from)
Jerry Yu615bd6f2022-02-17 14:25:15 +08007549{
7550 int len = 12;
7551 const char *sender;
7552 unsigned char padbuf[32];
7553#if defined(MBEDTLS_USE_PSA_CRYPTO)
7554 size_t hash_size;
7555 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
7556 psa_status_t status;
7557#else
7558 mbedtls_sha256_context sha256;
7559#endif
7560
7561 mbedtls_ssl_session *session = ssl->session_negotiate;
Gilles Peskine449bd832023-01-11 14:50:10 +01007562 if (!session) {
Jerry Yu615bd6f2022-02-17 14:25:15 +08007563 session = ssl->session;
Gilles Peskine449bd832023-01-11 14:50:10 +01007564 }
Jerry Yu615bd6f2022-02-17 14:25:15 +08007565
Gilles Peskine449bd832023-01-11 14:50:10 +01007566 sender = (from == MBEDTLS_SSL_IS_CLIENT)
Jerry Yu615bd6f2022-02-17 14:25:15 +08007567 ? "client finished"
7568 : "server finished";
7569
7570#if defined(MBEDTLS_USE_PSA_CRYPTO)
7571 sha256_psa = psa_hash_operation_init();
7572
Gilles Peskine449bd832023-01-11 14:50:10 +01007573 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc PSA finished tls sha256"));
Jerry Yu615bd6f2022-02-17 14:25:15 +08007574
Gilles Peskine449bd832023-01-11 14:50:10 +01007575 status = psa_hash_clone(&ssl->handshake->fin_sha256_psa, &sha256_psa);
7576 if (status != PSA_SUCCESS) {
7577 MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash clone failed"));
Jerry Yu615bd6f2022-02-17 14:25:15 +08007578 return;
7579 }
7580
Gilles Peskine449bd832023-01-11 14:50:10 +01007581 status = psa_hash_finish(&sha256_psa, padbuf, sizeof(padbuf), &hash_size);
7582 if (status != PSA_SUCCESS) {
7583 MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash finish failed"));
Jerry Yu615bd6f2022-02-17 14:25:15 +08007584 return;
7585 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007586 MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated padbuf", padbuf, 32);
Jerry Yu615bd6f2022-02-17 14:25:15 +08007587#else
7588
Gilles Peskine449bd832023-01-11 14:50:10 +01007589 mbedtls_sha256_init(&sha256);
Jerry Yu615bd6f2022-02-17 14:25:15 +08007590
Gilles Peskine449bd832023-01-11 14:50:10 +01007591 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc finished tls sha256"));
Jerry Yu615bd6f2022-02-17 14:25:15 +08007592
Gilles Peskine449bd832023-01-11 14:50:10 +01007593 mbedtls_sha256_clone(&sha256, &ssl->handshake->fin_sha256);
Jerry Yu615bd6f2022-02-17 14:25:15 +08007594
7595 /*
7596 * TLSv1.2:
7597 * hash = PRF( master, finished_label,
7598 * Hash( handshake ) )[0.11]
7599 */
7600
7601#if !defined(MBEDTLS_SHA256_ALT)
Gilles Peskine449bd832023-01-11 14:50:10 +01007602 MBEDTLS_SSL_DEBUG_BUF(4, "finished sha2 state", (unsigned char *)
7603 sha256.state, sizeof(sha256.state));
Jerry Yu615bd6f2022-02-17 14:25:15 +08007604#endif
7605
Gilles Peskine449bd832023-01-11 14:50:10 +01007606 mbedtls_sha256_finish(&sha256, padbuf);
7607 mbedtls_sha256_free(&sha256);
Jerry Yu615bd6f2022-02-17 14:25:15 +08007608#endif /* MBEDTLS_USE_PSA_CRYPTO */
7609
Gilles Peskine449bd832023-01-11 14:50:10 +01007610 ssl->handshake->tls_prf(session->master, 48, sender,
7611 padbuf, 32, buf, len);
Jerry Yu615bd6f2022-02-17 14:25:15 +08007612
Gilles Peskine449bd832023-01-11 14:50:10 +01007613 MBEDTLS_SSL_DEBUG_BUF(3, "calc finished result", buf, len);
Jerry Yu615bd6f2022-02-17 14:25:15 +08007614
Gilles Peskine449bd832023-01-11 14:50:10 +01007615 mbedtls_platform_zeroize(padbuf, sizeof(padbuf));
Jerry Yu615bd6f2022-02-17 14:25:15 +08007616
Gilles Peskine449bd832023-01-11 14:50:10 +01007617 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc finished"));
Jerry Yu615bd6f2022-02-17 14:25:15 +08007618}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04007619#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yu615bd6f2022-02-17 14:25:15 +08007620
Jerry Yub7ba49e2022-02-17 14:25:53 +08007621
Andrzej Kurek25f27152022-08-17 16:09:31 -04007622#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yub7ba49e2022-02-17 14:25:53 +08007623static void ssl_calc_finished_tls_sha384(
Gilles Peskine449bd832023-01-11 14:50:10 +01007624 mbedtls_ssl_context *ssl, unsigned char *buf, int from)
Jerry Yub7ba49e2022-02-17 14:25:53 +08007625{
7626 int len = 12;
7627 const char *sender;
7628 unsigned char padbuf[48];
7629#if defined(MBEDTLS_USE_PSA_CRYPTO)
7630 size_t hash_size;
7631 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
7632 psa_status_t status;
7633#else
7634 mbedtls_sha512_context sha512;
7635#endif
7636
7637 mbedtls_ssl_session *session = ssl->session_negotiate;
Gilles Peskine449bd832023-01-11 14:50:10 +01007638 if (!session) {
Jerry Yub7ba49e2022-02-17 14:25:53 +08007639 session = ssl->session;
Gilles Peskine449bd832023-01-11 14:50:10 +01007640 }
Jerry Yub7ba49e2022-02-17 14:25:53 +08007641
Gilles Peskine449bd832023-01-11 14:50:10 +01007642 sender = (from == MBEDTLS_SSL_IS_CLIENT)
Jerry Yub7ba49e2022-02-17 14:25:53 +08007643 ? "client finished"
7644 : "server finished";
7645
7646#if defined(MBEDTLS_USE_PSA_CRYPTO)
7647 sha384_psa = psa_hash_operation_init();
7648
Gilles Peskine449bd832023-01-11 14:50:10 +01007649 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc PSA finished tls sha384"));
Jerry Yub7ba49e2022-02-17 14:25:53 +08007650
Gilles Peskine449bd832023-01-11 14:50:10 +01007651 status = psa_hash_clone(&ssl->handshake->fin_sha384_psa, &sha384_psa);
7652 if (status != PSA_SUCCESS) {
7653 MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash clone failed"));
Jerry Yub7ba49e2022-02-17 14:25:53 +08007654 return;
7655 }
7656
Gilles Peskine449bd832023-01-11 14:50:10 +01007657 status = psa_hash_finish(&sha384_psa, padbuf, sizeof(padbuf), &hash_size);
7658 if (status != PSA_SUCCESS) {
7659 MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash finish failed"));
Jerry Yub7ba49e2022-02-17 14:25:53 +08007660 return;
7661 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007662 MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated padbuf", padbuf, 48);
Jerry Yub7ba49e2022-02-17 14:25:53 +08007663#else
Gilles Peskine449bd832023-01-11 14:50:10 +01007664 mbedtls_sha512_init(&sha512);
Jerry Yub7ba49e2022-02-17 14:25:53 +08007665
Gilles Peskine449bd832023-01-11 14:50:10 +01007666 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc finished tls sha384"));
Jerry Yub7ba49e2022-02-17 14:25:53 +08007667
Gilles Peskine449bd832023-01-11 14:50:10 +01007668 mbedtls_sha512_clone(&sha512, &ssl->handshake->fin_sha384);
Jerry Yub7ba49e2022-02-17 14:25:53 +08007669
7670 /*
7671 * TLSv1.2:
7672 * hash = PRF( master, finished_label,
7673 * Hash( handshake ) )[0.11]
7674 */
7675
7676#if !defined(MBEDTLS_SHA512_ALT)
Gilles Peskine449bd832023-01-11 14:50:10 +01007677 MBEDTLS_SSL_DEBUG_BUF(4, "finished sha512 state", (unsigned char *)
7678 sha512.state, sizeof(sha512.state));
Jerry Yub7ba49e2022-02-17 14:25:53 +08007679#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01007680 mbedtls_sha512_finish(&sha512, padbuf);
Jerry Yub7ba49e2022-02-17 14:25:53 +08007681
Gilles Peskine449bd832023-01-11 14:50:10 +01007682 mbedtls_sha512_free(&sha512);
Jerry Yub7ba49e2022-02-17 14:25:53 +08007683#endif
7684
Gilles Peskine449bd832023-01-11 14:50:10 +01007685 ssl->handshake->tls_prf(session->master, 48, sender,
7686 padbuf, 48, buf, len);
Jerry Yub7ba49e2022-02-17 14:25:53 +08007687
Gilles Peskine449bd832023-01-11 14:50:10 +01007688 MBEDTLS_SSL_DEBUG_BUF(3, "calc finished result", buf, len);
Jerry Yub7ba49e2022-02-17 14:25:53 +08007689
Gilles Peskine449bd832023-01-11 14:50:10 +01007690 mbedtls_platform_zeroize(padbuf, sizeof(padbuf));
Jerry Yub7ba49e2022-02-17 14:25:53 +08007691
Gilles Peskine449bd832023-01-11 14:50:10 +01007692 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc finished"));
Jerry Yub7ba49e2022-02-17 14:25:53 +08007693}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04007694#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yub7ba49e2022-02-17 14:25:53 +08007695
Gilles Peskine449bd832023-01-11 14:50:10 +01007696void mbedtls_ssl_handshake_wrapup_free_hs_transform(mbedtls_ssl_context *ssl)
Jerry Yuaef00152022-02-17 14:27:31 +08007697{
Gilles Peskine449bd832023-01-11 14:50:10 +01007698 MBEDTLS_SSL_DEBUG_MSG(3, ("=> handshake wrapup: final free"));
Jerry Yuaef00152022-02-17 14:27:31 +08007699
7700 /*
7701 * Free our handshake params
7702 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007703 mbedtls_ssl_handshake_free(ssl);
7704 mbedtls_free(ssl->handshake);
Jerry Yuaef00152022-02-17 14:27:31 +08007705 ssl->handshake = NULL;
7706
7707 /*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08007708 * Free the previous transform and switch in the current one
Jerry Yuaef00152022-02-17 14:27:31 +08007709 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007710 if (ssl->transform) {
7711 mbedtls_ssl_transform_free(ssl->transform);
7712 mbedtls_free(ssl->transform);
Jerry Yuaef00152022-02-17 14:27:31 +08007713 }
7714 ssl->transform = ssl->transform_negotiate;
7715 ssl->transform_negotiate = NULL;
7716
Gilles Peskine449bd832023-01-11 14:50:10 +01007717 MBEDTLS_SSL_DEBUG_MSG(3, ("<= handshake wrapup: final free"));
Jerry Yuaef00152022-02-17 14:27:31 +08007718}
7719
Gilles Peskine449bd832023-01-11 14:50:10 +01007720void mbedtls_ssl_handshake_wrapup(mbedtls_ssl_context *ssl)
Jerry Yu2a9fff52022-02-17 14:28:51 +08007721{
7722 int resume = ssl->handshake->resume;
7723
Gilles Peskine449bd832023-01-11 14:50:10 +01007724 MBEDTLS_SSL_DEBUG_MSG(3, ("=> handshake wrapup"));
Jerry Yu2a9fff52022-02-17 14:28:51 +08007725
7726#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01007727 if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
Jerry Yu2a9fff52022-02-17 14:28:51 +08007728 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
7729 ssl->renego_records_seen = 0;
7730 }
7731#endif
7732
7733 /*
7734 * Free the previous session and switch in the current one
7735 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007736 if (ssl->session) {
Jerry Yu2a9fff52022-02-17 14:28:51 +08007737#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
7738 /* RFC 7366 3.1: keep the EtM state */
7739 ssl->session_negotiate->encrypt_then_mac =
Gilles Peskine449bd832023-01-11 14:50:10 +01007740 ssl->session->encrypt_then_mac;
Jerry Yu2a9fff52022-02-17 14:28:51 +08007741#endif
7742
Gilles Peskine449bd832023-01-11 14:50:10 +01007743 mbedtls_ssl_session_free(ssl->session);
7744 mbedtls_free(ssl->session);
Jerry Yu2a9fff52022-02-17 14:28:51 +08007745 }
7746 ssl->session = ssl->session_negotiate;
7747 ssl->session_negotiate = NULL;
7748
7749 /*
7750 * Add cache entry
7751 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007752 if (ssl->conf->f_set_cache != NULL &&
Jerry Yu2a9fff52022-02-17 14:28:51 +08007753 ssl->session->id_len != 0 &&
Gilles Peskine449bd832023-01-11 14:50:10 +01007754 resume == 0) {
7755 if (ssl->conf->f_set_cache(ssl->conf->p_cache,
7756 ssl->session->id,
7757 ssl->session->id_len,
7758 ssl->session) != 0) {
7759 MBEDTLS_SSL_DEBUG_MSG(1, ("cache did not store session"));
7760 }
Jerry Yu2a9fff52022-02-17 14:28:51 +08007761 }
7762
7763#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007764 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7765 ssl->handshake->flight != NULL) {
Jerry Yu2a9fff52022-02-17 14:28:51 +08007766 /* Cancel handshake timer */
Gilles Peskine449bd832023-01-11 14:50:10 +01007767 mbedtls_ssl_set_timer(ssl, 0);
Jerry Yu2a9fff52022-02-17 14:28:51 +08007768
7769 /* Keep last flight around in case we need to resend it:
7770 * we need the handshake and transform structures for that */
Gilles Peskine449bd832023-01-11 14:50:10 +01007771 MBEDTLS_SSL_DEBUG_MSG(3, ("skip freeing handshake and transform"));
7772 } else
Jerry Yu2a9fff52022-02-17 14:28:51 +08007773#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01007774 mbedtls_ssl_handshake_wrapup_free_hs_transform(ssl);
Jerry Yu2a9fff52022-02-17 14:28:51 +08007775
Jerry Yu5ed73ff2022-10-27 13:08:42 +08007776 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
Jerry Yu2a9fff52022-02-17 14:28:51 +08007777
Gilles Peskine449bd832023-01-11 14:50:10 +01007778 MBEDTLS_SSL_DEBUG_MSG(3, ("<= handshake wrapup"));
Jerry Yu2a9fff52022-02-17 14:28:51 +08007779}
7780
Gilles Peskine449bd832023-01-11 14:50:10 +01007781int mbedtls_ssl_write_finished(mbedtls_ssl_context *ssl)
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007782{
7783 int ret, hash_len;
7784
Gilles Peskine449bd832023-01-11 14:50:10 +01007785 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write finished"));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007786
Gilles Peskine449bd832023-01-11 14:50:10 +01007787 mbedtls_ssl_update_out_pointers(ssl, ssl->transform_negotiate);
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007788
Gilles Peskine449bd832023-01-11 14:50:10 +01007789 ssl->handshake->calc_finished(ssl, ssl->out_msg + 4, ssl->conf->endpoint);
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007790
7791 /*
7792 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7793 * may define some other value. Currently (early 2016), no defined
7794 * ciphersuite does this (and this is unlikely to change as activity has
7795 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7796 */
7797 hash_len = 12;
7798
7799#if defined(MBEDTLS_SSL_RENEGOTIATION)
7800 ssl->verify_data_len = hash_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01007801 memcpy(ssl->own_verify_data, ssl->out_msg + 4, hash_len);
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007802#endif
7803
7804 ssl->out_msglen = 4 + hash_len;
7805 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7806 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
7807
7808 /*
7809 * In case of session resuming, invert the client and server
7810 * ChangeCipherSpec messages order.
7811 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007812 if (ssl->handshake->resume != 0) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007813#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007814 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007815 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Gilles Peskine449bd832023-01-11 14:50:10 +01007816 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007817#endif
7818#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007819 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007820 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Gilles Peskine449bd832023-01-11 14:50:10 +01007821 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007822#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01007823 } else {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007824 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01007825 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007826
7827 /*
7828 * Switch to our negotiated transform and session parameters for outbound
7829 * data.
7830 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007831 MBEDTLS_SSL_DEBUG_MSG(3, ("switching to new transform spec for outbound data"));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007832
7833#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007834 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007835 unsigned char i;
7836
7837 /* Remember current epoch settings for resending */
7838 ssl->handshake->alt_transform_out = ssl->transform_out;
Gilles Peskine449bd832023-01-11 14:50:10 +01007839 memcpy(ssl->handshake->alt_out_ctr, ssl->cur_out_ctr,
7840 sizeof(ssl->handshake->alt_out_ctr));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007841
7842 /* Set sequence_number to zero */
Gilles Peskine449bd832023-01-11 14:50:10 +01007843 memset(&ssl->cur_out_ctr[2], 0, sizeof(ssl->cur_out_ctr) - 2);
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007844
7845
7846 /* Increment epoch */
Gilles Peskine449bd832023-01-11 14:50:10 +01007847 for (i = 2; i > 0; i--) {
7848 if (++ssl->cur_out_ctr[i - 1] != 0) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007849 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01007850 }
7851 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007852
7853 /* The loop goes to its end iff the counter is wrapping */
Gilles Peskine449bd832023-01-11 14:50:10 +01007854 if (i == 0) {
7855 MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS epoch would wrap"));
7856 return MBEDTLS_ERR_SSL_COUNTER_WRAPPING;
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007857 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007858 } else
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007859#endif /* MBEDTLS_SSL_PROTO_DTLS */
Gilles Peskine449bd832023-01-11 14:50:10 +01007860 memset(ssl->cur_out_ctr, 0, sizeof(ssl->cur_out_ctr));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007861
7862 ssl->transform_out = ssl->transform_negotiate;
7863 ssl->session_out = ssl->session_negotiate;
7864
7865#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007866 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
7867 mbedtls_ssl_send_flight_completed(ssl);
7868 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007869#endif
7870
Gilles Peskine449bd832023-01-11 14:50:10 +01007871 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
7872 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
7873 return ret;
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007874 }
7875
7876#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007877 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7878 (ret = mbedtls_ssl_flight_transmit(ssl)) != 0) {
7879 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_flight_transmit", ret);
7880 return ret;
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007881 }
7882#endif
7883
Gilles Peskine449bd832023-01-11 14:50:10 +01007884 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write finished"));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007885
Gilles Peskine449bd832023-01-11 14:50:10 +01007886 return 0;
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007887}
7888
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007889#define SSL_MAX_HASH_LEN 12
7890
Gilles Peskine449bd832023-01-11 14:50:10 +01007891int mbedtls_ssl_parse_finished(mbedtls_ssl_context *ssl)
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007892{
7893 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7894 unsigned int hash_len = 12;
7895 unsigned char buf[SSL_MAX_HASH_LEN];
7896
Gilles Peskine449bd832023-01-11 14:50:10 +01007897 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse finished"));
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007898
Gilles Peskine449bd832023-01-11 14:50:10 +01007899 ssl->handshake->calc_finished(ssl, buf, ssl->conf->endpoint ^ 1);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007900
Gilles Peskine449bd832023-01-11 14:50:10 +01007901 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
7902 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007903 goto exit;
7904 }
7905
Gilles Peskine449bd832023-01-11 14:50:10 +01007906 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
7907 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
7908 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7909 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007910 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
7911 goto exit;
7912 }
7913
Gilles Peskine449bd832023-01-11 14:50:10 +01007914 if (ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED) {
7915 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7916 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007917 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
7918 goto exit;
7919 }
7920
Gilles Peskine449bd832023-01-11 14:50:10 +01007921 if (ssl->in_hslen != mbedtls_ssl_hs_hdr_len(ssl) + hash_len) {
7922 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
7923 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7924 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007925 ret = MBEDTLS_ERR_SSL_DECODE_ERROR;
7926 goto exit;
7927 }
7928
Gilles Peskine449bd832023-01-11 14:50:10 +01007929 if (mbedtls_ct_memcmp(ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl),
7930 buf, hash_len) != 0) {
7931 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
7932 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7933 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007934 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
7935 goto exit;
7936 }
7937
7938#if defined(MBEDTLS_SSL_RENEGOTIATION)
7939 ssl->verify_data_len = hash_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01007940 memcpy(ssl->peer_verify_data, buf, hash_len);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007941#endif
7942
Gilles Peskine449bd832023-01-11 14:50:10 +01007943 if (ssl->handshake->resume != 0) {
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007944#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007945 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007946 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Gilles Peskine449bd832023-01-11 14:50:10 +01007947 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007948#endif
7949#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007950 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007951 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Gilles Peskine449bd832023-01-11 14:50:10 +01007952 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007953#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01007954 } else {
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007955 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01007956 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007957
7958#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007959 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
7960 mbedtls_ssl_recv_flight_completed(ssl);
7961 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007962#endif
7963
Gilles Peskine449bd832023-01-11 14:50:10 +01007964 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse finished"));
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007965
7966exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007967 mbedtls_platform_zeroize(buf, hash_len);
7968 return ret;
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007969}
7970
Jerry Yu392112c2022-02-17 14:34:10 +08007971#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
7972/*
7973 * Helper to get TLS 1.2 PRF from ciphersuite
7974 * (Duplicates bits of logic from ssl_set_handshake_prfs().)
7975 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007976static tls_prf_fn ssl_tls12prf_from_cs(int ciphersuite_id)
Jerry Yu392112c2022-02-17 14:34:10 +08007977{
Jerry Yu392112c2022-02-17 14:34:10 +08007978 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01007979 mbedtls_ssl_ciphersuite_from_id(ciphersuite_id);
Andrzej Kurek68327742022-10-03 06:18:18 -04007980#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01007981 if (ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA384) {
7982 return tls_prf_sha384;
7983 } else
Jerry Yu392112c2022-02-17 14:34:10 +08007984#endif
Andrzej Kurek894edde2022-09-29 06:31:14 -04007985#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
7986 {
Gilles Peskine449bd832023-01-11 14:50:10 +01007987 if (ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA256) {
7988 return tls_prf_sha256;
7989 }
Andrzej Kurek894edde2022-09-29 06:31:14 -04007990 }
7991#endif
7992#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
7993 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
7994 (void) ciphersuite_info;
7995#endif
Andrzej Kurekeabeb302022-10-17 07:52:51 -04007996
Gilles Peskine449bd832023-01-11 14:50:10 +01007997 return NULL;
Jerry Yu392112c2022-02-17 14:34:10 +08007998}
7999#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Jerry Yue93ffcd2022-02-17 14:37:06 +08008000
Gilles Peskine449bd832023-01-11 14:50:10 +01008001static mbedtls_tls_prf_types tls_prf_get_type(mbedtls_ssl_tls_prf_cb *tls_prf)
Jerry Yue93ffcd2022-02-17 14:37:06 +08008002{
8003 ((void) tls_prf);
Andrzej Kurek25f27152022-08-17 16:09:31 -04008004#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01008005 if (tls_prf == tls_prf_sha384) {
8006 return MBEDTLS_SSL_TLS_PRF_SHA384;
8007 } else
Jerry Yue93ffcd2022-02-17 14:37:06 +08008008#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04008009#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01008010 if (tls_prf == tls_prf_sha256) {
8011 return MBEDTLS_SSL_TLS_PRF_SHA256;
8012 } else
Jerry Yue93ffcd2022-02-17 14:37:06 +08008013#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01008014 return MBEDTLS_SSL_TLS_PRF_NONE;
Jerry Yue93ffcd2022-02-17 14:37:06 +08008015}
8016
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008017/*
8018 * Populate a transform structure with session keys and all the other
8019 * necessary information.
8020 *
8021 * Parameters:
8022 * - [in/out]: transform: structure to populate
8023 * [in] must be just initialised with mbedtls_ssl_transform_init()
8024 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
8025 * - [in] ciphersuite
8026 * - [in] master
8027 * - [in] encrypt_then_mac
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008028 * - [in] tls_prf: pointer to PRF to use for key derivation
8029 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
Glenn Strauss07c64162022-03-14 12:34:51 -04008030 * - [in] tls_version: TLS version
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008031 * - [in] endpoint: client or server
8032 * - [in] ssl: used for:
8033 * - ssl->conf->{f,p}_export_keys
8034 * [in] optionally used for:
8035 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
8036 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02008037MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01008038static int ssl_tls12_populate_transform(mbedtls_ssl_transform *transform,
8039 int ciphersuite,
8040 const unsigned char master[48],
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008041#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01008042 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008043#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01008044 ssl_tls_prf_t tls_prf,
8045 const unsigned char randbytes[64],
8046 mbedtls_ssl_protocol_version tls_version,
8047 unsigned endpoint,
8048 const mbedtls_ssl_context *ssl)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008049{
8050 int ret = 0;
8051 unsigned char keyblk[256];
8052 unsigned char *key1;
8053 unsigned char *key2;
8054 unsigned char *mac_enc;
8055 unsigned char *mac_dec;
8056 size_t mac_key_len = 0;
8057 size_t iv_copy_len;
8058 size_t keylen;
8059 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008060 mbedtls_ssl_mode_t ssl_mode;
Neil Armstronge4512952022-03-08 09:08:22 +01008061#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008062 const mbedtls_cipher_info_t *cipher_info;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008063 const mbedtls_md_info_t *md_info;
Neil Armstronge4512952022-03-08 09:08:22 +01008064#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008065
8066#if defined(MBEDTLS_USE_PSA_CRYPTO)
8067 psa_key_type_t key_type;
8068 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8069 psa_algorithm_t alg;
Neil Armstronge4512952022-03-08 09:08:22 +01008070 psa_algorithm_t mac_alg = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008071 size_t key_bits;
8072 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
8073#endif
8074
8075#if !defined(MBEDTLS_DEBUG_C) && \
8076 !defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Gilles Peskine449bd832023-01-11 14:50:10 +01008077 if (ssl->f_export_keys == NULL) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008078 ssl = NULL; /* make sure we don't use it except for these cases */
8079 (void) ssl;
8080 }
8081#endif
8082
8083 /*
8084 * Some data just needs copying into the structure
8085 */
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008086#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008087 transform->encrypt_then_mac = encrypt_then_mac;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008088#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Glenn Strauss07c64162022-03-14 12:34:51 -04008089 transform->tls_version = tls_version;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008090
8091#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01008092 memcpy(transform->randbytes, randbytes, sizeof(transform->randbytes));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008093#endif
8094
8095#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01008096 if (tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008097 /* At the moment, we keep TLS <= 1.2 and TLS 1.3 transform
8098 * generation separate. This should never happen. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008099 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008100 }
8101#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
8102
8103 /*
8104 * Get various info structures
8105 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008106 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(ciphersuite);
8107 if (ciphersuite_info == NULL) {
8108 MBEDTLS_SSL_DEBUG_MSG(1, ("ciphersuite info for %d not found",
8109 ciphersuite));
8110 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008111 }
8112
Neil Armstrongab555e02022-04-04 11:07:59 +02008113 ssl_mode = mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008114#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01008115 encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008116#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01008117 ciphersuite_info);
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008118
Gilles Peskine449bd832023-01-11 14:50:10 +01008119 if (ssl_mode == MBEDTLS_SSL_MODE_AEAD) {
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008120 transform->taglen =
8121 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Gilles Peskine449bd832023-01-11 14:50:10 +01008122 }
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008123
8124#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008125 if ((status = mbedtls_ssl_cipher_to_psa(ciphersuite_info->cipher,
8126 transform->taglen,
8127 &alg,
8128 &key_type,
8129 &key_bits)) != PSA_SUCCESS) {
8130 ret = psa_ssl_status_to_mbedtls(status);
8131 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_cipher_to_psa", ret);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008132 goto end;
8133 }
8134#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008135 cipher_info = mbedtls_cipher_info_from_type(ciphersuite_info->cipher);
8136 if (cipher_info == NULL) {
8137 MBEDTLS_SSL_DEBUG_MSG(1, ("cipher info for %u not found",
8138 ciphersuite_info->cipher));
8139 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008140 }
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008141#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008142
Neil Armstronge4512952022-03-08 09:08:22 +01008143#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008144 mac_alg = mbedtls_hash_info_psa_from_md(ciphersuite_info->mac);
8145 if (mac_alg == 0) {
8146 MBEDTLS_SSL_DEBUG_MSG(1, ("mbedtls_hash_info_psa_from_md for %u not found",
8147 (unsigned) ciphersuite_info->mac));
8148 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Neil Armstronge4512952022-03-08 09:08:22 +01008149 }
8150#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008151 md_info = mbedtls_md_info_from_type(ciphersuite_info->mac);
8152 if (md_info == NULL) {
8153 MBEDTLS_SSL_DEBUG_MSG(1, ("mbedtls_md info for %u not found",
8154 (unsigned) ciphersuite_info->mac));
8155 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008156 }
Neil Armstronge4512952022-03-08 09:08:22 +01008157#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008158
8159#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
8160 /* Copy own and peer's CID if the use of the CID
8161 * extension has been negotiated. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008162 if (ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED) {
8163 MBEDTLS_SSL_DEBUG_MSG(3, ("Copy CIDs into SSL transform"));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008164
8165 transform->in_cid_len = ssl->own_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008166 memcpy(transform->in_cid, ssl->own_cid, ssl->own_cid_len);
8167 MBEDTLS_SSL_DEBUG_BUF(3, "Incoming CID", transform->in_cid,
8168 transform->in_cid_len);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008169
8170 transform->out_cid_len = ssl->handshake->peer_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008171 memcpy(transform->out_cid, ssl->handshake->peer_cid,
8172 ssl->handshake->peer_cid_len);
8173 MBEDTLS_SSL_DEBUG_BUF(3, "Outgoing CID", transform->out_cid,
8174 transform->out_cid_len);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008175 }
8176#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
8177
8178 /*
8179 * Compute key block using the PRF
8180 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008181 ret = tls_prf(master, 48, "key expansion", randbytes, 64, keyblk, 256);
8182 if (ret != 0) {
8183 MBEDTLS_SSL_DEBUG_RET(1, "prf", ret);
8184 return ret;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008185 }
8186
Gilles Peskine449bd832023-01-11 14:50:10 +01008187 MBEDTLS_SSL_DEBUG_MSG(3, ("ciphersuite = %s",
8188 mbedtls_ssl_get_ciphersuite_name(ciphersuite)));
8189 MBEDTLS_SSL_DEBUG_BUF(3, "master secret", master, 48);
8190 MBEDTLS_SSL_DEBUG_BUF(4, "random bytes", randbytes, 64);
8191 MBEDTLS_SSL_DEBUG_BUF(4, "key block", keyblk, 256);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008192
8193 /*
8194 * Determine the appropriate key, IV and MAC length.
8195 */
8196
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008197#if defined(MBEDTLS_USE_PSA_CRYPTO)
8198 keylen = PSA_BITS_TO_BYTES(key_bits);
8199#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008200 keylen = mbedtls_cipher_info_get_key_bitlen(cipher_info) / 8;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008201#endif
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008202
8203#if defined(MBEDTLS_GCM_C) || \
8204 defined(MBEDTLS_CCM_C) || \
8205 defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008206 if (ssl_mode == MBEDTLS_SSL_MODE_AEAD) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008207 size_t explicit_ivlen;
8208
8209 transform->maclen = 0;
8210 mac_key_len = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008211
8212 /* All modes haves 96-bit IVs, but the length of the static parts vary
8213 * with mode and version:
8214 * - For GCM and CCM in TLS 1.2, there's a static IV of 4 Bytes
8215 * (to be concatenated with a dynamically chosen IV of 8 Bytes)
8216 * - For ChaChaPoly in TLS 1.2, and all modes in TLS 1.3, there's
8217 * a static IV of 12 Bytes (to be XOR'ed with the 8 Byte record
8218 * sequence number).
8219 */
8220 transform->ivlen = 12;
David Horstmann3b2276a2022-10-06 14:49:08 +01008221
8222 int is_chachapoly = 0;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008223#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008224 is_chachapoly = (key_type == PSA_KEY_TYPE_CHACHA20);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008225#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008226 is_chachapoly = (mbedtls_cipher_info_get_mode(cipher_info)
8227 == MBEDTLS_MODE_CHACHAPOLY);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008228#endif /* MBEDTLS_USE_PSA_CRYPTO */
David Horstmann3b2276a2022-10-06 14:49:08 +01008229
Gilles Peskine449bd832023-01-11 14:50:10 +01008230 if (is_chachapoly) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008231 transform->fixed_ivlen = 12;
Gilles Peskine449bd832023-01-11 14:50:10 +01008232 } else {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008233 transform->fixed_ivlen = 4;
Gilles Peskine449bd832023-01-11 14:50:10 +01008234 }
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008235
8236 /* Minimum length of encrypted record */
8237 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
8238 transform->minlen = explicit_ivlen + transform->taglen;
Gilles Peskine449bd832023-01-11 14:50:10 +01008239 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008240#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
8241#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01008242 if (ssl_mode == MBEDTLS_SSL_MODE_STREAM ||
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008243 ssl_mode == MBEDTLS_SSL_MODE_CBC ||
Gilles Peskine449bd832023-01-11 14:50:10 +01008244 ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM) {
Neil Armstronge4512952022-03-08 09:08:22 +01008245#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008246 size_t block_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008247#else
8248 size_t block_size = cipher_info->block_size;
8249#endif /* MBEDTLS_USE_PSA_CRYPTO */
8250
8251#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronge4512952022-03-08 09:08:22 +01008252 /* Get MAC length */
8253 mac_key_len = PSA_HASH_LENGTH(mac_alg);
8254#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008255 /* Initialize HMAC contexts */
Gilles Peskine449bd832023-01-11 14:50:10 +01008256 if ((ret = mbedtls_md_setup(&transform->md_ctx_enc, md_info, 1)) != 0 ||
8257 (ret = mbedtls_md_setup(&transform->md_ctx_dec, md_info, 1)) != 0) {
8258 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_setup", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008259 goto end;
8260 }
8261
8262 /* Get MAC length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008263 mac_key_len = mbedtls_md_get_size(md_info);
Neil Armstronge4512952022-03-08 09:08:22 +01008264#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008265 transform->maclen = mac_key_len;
8266
8267 /* IV length */
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008268#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008269 transform->ivlen = PSA_CIPHER_IV_LENGTH(key_type, alg);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008270#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008271 transform->ivlen = cipher_info->iv_size;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008272#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008273
8274 /* Minimum length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008275 if (ssl_mode == MBEDTLS_SSL_MODE_STREAM) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008276 transform->minlen = transform->maclen;
Gilles Peskine449bd832023-01-11 14:50:10 +01008277 } else {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008278 /*
8279 * GenericBlockCipher:
8280 * 1. if EtM is in use: one block plus MAC
8281 * otherwise: * first multiple of blocklen greater than maclen
8282 * 2. IV
8283 */
8284#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01008285 if (ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008286 transform->minlen = transform->maclen
Gilles Peskine449bd832023-01-11 14:50:10 +01008287 + block_size;
8288 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008289#endif
8290 {
8291 transform->minlen = transform->maclen
Gilles Peskine449bd832023-01-11 14:50:10 +01008292 + block_size
8293 - transform->maclen % block_size;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008294 }
8295
Gilles Peskine449bd832023-01-11 14:50:10 +01008296 if (tls_version == MBEDTLS_SSL_VERSION_TLS1_2) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008297 transform->minlen += transform->ivlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01008298 } else {
8299 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008300 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
8301 goto end;
8302 }
8303 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008304 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008305#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
8306 {
Gilles Peskine449bd832023-01-11 14:50:10 +01008307 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
8308 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008309 }
8310
Gilles Peskine449bd832023-01-11 14:50:10 +01008311 MBEDTLS_SSL_DEBUG_MSG(3, ("keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
8312 (unsigned) keylen,
8313 (unsigned) transform->minlen,
8314 (unsigned) transform->ivlen,
8315 (unsigned) transform->maclen));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008316
8317 /*
8318 * Finally setup the cipher contexts, IVs and MAC secrets.
8319 */
8320#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008321 if (endpoint == MBEDTLS_SSL_IS_CLIENT) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008322 key1 = keyblk + mac_key_len * 2;
8323 key2 = keyblk + mac_key_len * 2 + keylen;
8324
8325 mac_enc = keyblk;
8326 mac_dec = keyblk + mac_key_len;
8327
Gilles Peskine449bd832023-01-11 14:50:10 +01008328 iv_copy_len = (transform->fixed_ivlen) ?
8329 transform->fixed_ivlen : transform->ivlen;
8330 memcpy(transform->iv_enc, key2 + keylen, iv_copy_len);
8331 memcpy(transform->iv_dec, key2 + keylen + iv_copy_len,
8332 iv_copy_len);
8333 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008334#endif /* MBEDTLS_SSL_CLI_C */
8335#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008336 if (endpoint == MBEDTLS_SSL_IS_SERVER) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008337 key1 = keyblk + mac_key_len * 2 + keylen;
8338 key2 = keyblk + mac_key_len * 2;
8339
8340 mac_enc = keyblk + mac_key_len;
8341 mac_dec = keyblk;
8342
Gilles Peskine449bd832023-01-11 14:50:10 +01008343 iv_copy_len = (transform->fixed_ivlen) ?
8344 transform->fixed_ivlen : transform->ivlen;
8345 memcpy(transform->iv_dec, key1 + keylen, iv_copy_len);
8346 memcpy(transform->iv_enc, key1 + keylen + iv_copy_len,
8347 iv_copy_len);
8348 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008349#endif /* MBEDTLS_SSL_SRV_C */
8350 {
Gilles Peskine449bd832023-01-11 14:50:10 +01008351 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008352 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
8353 goto end;
8354 }
8355
Gilles Peskine449bd832023-01-11 14:50:10 +01008356 if (ssl != NULL && ssl->f_export_keys != NULL) {
8357 ssl->f_export_keys(ssl->p_export_keys,
8358 MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET,
8359 master, 48,
8360 randbytes + 32,
8361 randbytes,
8362 tls_prf_get_type(tls_prf));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008363 }
8364
8365#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008366 transform->psa_alg = alg;
8367
Gilles Peskine449bd832023-01-11 14:50:10 +01008368 if (alg != MBEDTLS_SSL_NULL_CIPHER) {
8369 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
8370 psa_set_key_algorithm(&attributes, alg);
8371 psa_set_key_type(&attributes, key_type);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008372
Gilles Peskine449bd832023-01-11 14:50:10 +01008373 if ((status = psa_import_key(&attributes,
8374 key1,
8375 PSA_BITS_TO_BYTES(key_bits),
8376 &transform->psa_key_enc)) != PSA_SUCCESS) {
8377 MBEDTLS_SSL_DEBUG_RET(3, "psa_import_key", (int) status);
8378 ret = psa_ssl_status_to_mbedtls(status);
8379 MBEDTLS_SSL_DEBUG_RET(1, "psa_import_key", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008380 goto end;
8381 }
8382
Gilles Peskine449bd832023-01-11 14:50:10 +01008383 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008384
Gilles Peskine449bd832023-01-11 14:50:10 +01008385 if ((status = psa_import_key(&attributes,
8386 key2,
8387 PSA_BITS_TO_BYTES(key_bits),
8388 &transform->psa_key_dec)) != PSA_SUCCESS) {
8389 ret = psa_ssl_status_to_mbedtls(status);
8390 MBEDTLS_SSL_DEBUG_RET(1, "psa_import_key", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008391 goto end;
8392 }
8393 }
8394#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008395 if ((ret = mbedtls_cipher_setup(&transform->cipher_ctx_enc,
8396 cipher_info)) != 0) {
8397 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008398 goto end;
8399 }
8400
Gilles Peskine449bd832023-01-11 14:50:10 +01008401 if ((ret = mbedtls_cipher_setup(&transform->cipher_ctx_dec,
8402 cipher_info)) != 0) {
8403 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008404 goto end;
8405 }
8406
Gilles Peskine449bd832023-01-11 14:50:10 +01008407 if ((ret = mbedtls_cipher_setkey(&transform->cipher_ctx_enc, key1,
8408 (int) mbedtls_cipher_info_get_key_bitlen(cipher_info),
8409 MBEDTLS_ENCRYPT)) != 0) {
8410 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setkey", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008411 goto end;
8412 }
8413
Gilles Peskine449bd832023-01-11 14:50:10 +01008414 if ((ret = mbedtls_cipher_setkey(&transform->cipher_ctx_dec, key2,
8415 (int) mbedtls_cipher_info_get_key_bitlen(cipher_info),
8416 MBEDTLS_DECRYPT)) != 0) {
8417 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setkey", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008418 goto end;
8419 }
8420
8421#if defined(MBEDTLS_CIPHER_MODE_CBC)
Gilles Peskine449bd832023-01-11 14:50:10 +01008422 if (mbedtls_cipher_info_get_mode(cipher_info) == MBEDTLS_MODE_CBC) {
8423 if ((ret = mbedtls_cipher_set_padding_mode(&transform->cipher_ctx_enc,
8424 MBEDTLS_PADDING_NONE)) != 0) {
8425 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_set_padding_mode", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008426 goto end;
8427 }
8428
Gilles Peskine449bd832023-01-11 14:50:10 +01008429 if ((ret = mbedtls_cipher_set_padding_mode(&transform->cipher_ctx_dec,
8430 MBEDTLS_PADDING_NONE)) != 0) {
8431 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_set_padding_mode", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008432 goto end;
8433 }
8434 }
8435#endif /* MBEDTLS_CIPHER_MODE_CBC */
8436#endif /* MBEDTLS_USE_PSA_CRYPTO */
8437
Neil Armstrong29c0c042022-03-17 17:47:28 +01008438#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
8439 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
8440 For AEAD-based ciphersuites, there is nothing to do here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008441 if (mac_key_len != 0) {
Neil Armstrong29c0c042022-03-17 17:47:28 +01008442#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008443 transform->psa_mac_alg = PSA_ALG_HMAC(mac_alg);
Neil Armstrong29c0c042022-03-17 17:47:28 +01008444
Gilles Peskine449bd832023-01-11 14:50:10 +01008445 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
8446 psa_set_key_algorithm(&attributes, PSA_ALG_HMAC(mac_alg));
8447 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
Neil Armstrong29c0c042022-03-17 17:47:28 +01008448
Gilles Peskine449bd832023-01-11 14:50:10 +01008449 if ((status = psa_import_key(&attributes,
8450 mac_enc, mac_key_len,
8451 &transform->psa_mac_enc)) != PSA_SUCCESS) {
8452 ret = psa_ssl_status_to_mbedtls(status);
8453 MBEDTLS_SSL_DEBUG_RET(1, "psa_import_mac_key", ret);
Neil Armstrong29c0c042022-03-17 17:47:28 +01008454 goto end;
8455 }
8456
Gilles Peskine449bd832023-01-11 14:50:10 +01008457 if ((transform->psa_alg == MBEDTLS_SSL_NULL_CIPHER) ||
8458 ((transform->psa_alg == PSA_ALG_CBC_NO_PADDING)
Andrzej Kurek8c95ac42022-08-17 16:17:00 -04008459#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01008460 && (transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED)
Andrzej Kurek8c95ac42022-08-17 16:17:00 -04008461#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01008462 )) {
Neil Armstrong29c0c042022-03-17 17:47:28 +01008463 /* mbedtls_ct_hmac() requires the key to be exportable */
Gilles Peskine449bd832023-01-11 14:50:10 +01008464 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT |
8465 PSA_KEY_USAGE_VERIFY_HASH);
8466 } else {
8467 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
8468 }
Neil Armstrong29c0c042022-03-17 17:47:28 +01008469
Gilles Peskine449bd832023-01-11 14:50:10 +01008470 if ((status = psa_import_key(&attributes,
8471 mac_dec, mac_key_len,
8472 &transform->psa_mac_dec)) != PSA_SUCCESS) {
8473 ret = psa_ssl_status_to_mbedtls(status);
8474 MBEDTLS_SSL_DEBUG_RET(1, "psa_import_mac_key", ret);
Neil Armstrong29c0c042022-03-17 17:47:28 +01008475 goto end;
8476 }
8477#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008478 ret = mbedtls_md_hmac_starts(&transform->md_ctx_enc, mac_enc, mac_key_len);
8479 if (ret != 0) {
Neil Armstrong29c0c042022-03-17 17:47:28 +01008480 goto end;
Gilles Peskine449bd832023-01-11 14:50:10 +01008481 }
8482 ret = mbedtls_md_hmac_starts(&transform->md_ctx_dec, mac_dec, mac_key_len);
8483 if (ret != 0) {
Neil Armstrong29c0c042022-03-17 17:47:28 +01008484 goto end;
Gilles Peskine449bd832023-01-11 14:50:10 +01008485 }
Neil Armstrong29c0c042022-03-17 17:47:28 +01008486#endif /* MBEDTLS_USE_PSA_CRYPTO */
8487 }
8488#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
8489
8490 ((void) mac_dec);
8491 ((void) mac_enc);
8492
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008493end:
Gilles Peskine449bd832023-01-11 14:50:10 +01008494 mbedtls_platform_zeroize(keyblk, sizeof(keyblk));
8495 return ret;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008496}
8497
Valerio Settia08b1a42022-11-17 15:10:02 +01008498#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) && \
8499 defined(MBEDTLS_USE_PSA_CRYPTO)
Valerio Setti6b3dab02022-11-17 17:14:54 +01008500int mbedtls_psa_ecjpake_read_round(
Gilles Peskine449bd832023-01-11 14:50:10 +01008501 psa_pake_operation_t *pake_ctx,
8502 const unsigned char *buf,
8503 size_t len, mbedtls_ecjpake_rounds_t round)
Valerio Settia08b1a42022-11-17 15:10:02 +01008504{
8505 psa_status_t status;
8506 size_t input_offset = 0;
Valerio Setti819de862022-11-17 18:05:19 +01008507 /*
Valerio Setti6b3dab02022-11-17 17:14:54 +01008508 * At round one repeat the KEY_SHARE, ZK_PUBLIC & ZF_PROOF twice
8509 * At round two perform a single cycle
8510 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008511 unsigned int remaining_steps = (round == MBEDTLS_ECJPAKE_ROUND_ONE) ? 2 : 1;
Valerio Settia08b1a42022-11-17 15:10:02 +01008512
Gilles Peskine449bd832023-01-11 14:50:10 +01008513 for (; remaining_steps > 0; remaining_steps--) {
8514 for (psa_pake_step_t step = PSA_PAKE_STEP_KEY_SHARE;
Valerio Settia08b1a42022-11-17 15:10:02 +01008515 step <= PSA_PAKE_STEP_ZK_PROOF;
Gilles Peskine449bd832023-01-11 14:50:10 +01008516 ++step) {
Valerio Settia08b1a42022-11-17 15:10:02 +01008517 /* Length is stored at the first byte */
8518 size_t length = buf[input_offset];
8519 input_offset += 1;
8520
Gilles Peskine449bd832023-01-11 14:50:10 +01008521 if (input_offset + length > len) {
Valerio Settia08b1a42022-11-17 15:10:02 +01008522 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
8523 }
8524
Gilles Peskine449bd832023-01-11 14:50:10 +01008525 status = psa_pake_input(pake_ctx, step,
8526 buf + input_offset, length);
8527 if (status != PSA_SUCCESS) {
8528 return psa_ssl_status_to_mbedtls(status);
Valerio Settia08b1a42022-11-17 15:10:02 +01008529 }
8530
8531 input_offset += length;
8532 }
8533 }
8534
Gilles Peskine449bd832023-01-11 14:50:10 +01008535 if (input_offset != len) {
Valerio Setti61ea17d2022-11-18 12:11:00 +01008536 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01008537 }
Valerio Setti30ebe112022-11-17 16:23:34 +01008538
Gilles Peskine449bd832023-01-11 14:50:10 +01008539 return 0;
Valerio Settia08b1a42022-11-17 15:10:02 +01008540}
8541
Valerio Setti6b3dab02022-11-17 17:14:54 +01008542int mbedtls_psa_ecjpake_write_round(
Gilles Peskine449bd832023-01-11 14:50:10 +01008543 psa_pake_operation_t *pake_ctx,
8544 unsigned char *buf,
8545 size_t len, size_t *olen,
8546 mbedtls_ecjpake_rounds_t round)
Valerio Settia08b1a42022-11-17 15:10:02 +01008547{
8548 psa_status_t status;
8549 size_t output_offset = 0;
8550 size_t output_len;
Valerio Setti819de862022-11-17 18:05:19 +01008551 /*
Valerio Setti6b3dab02022-11-17 17:14:54 +01008552 * At round one repeat the KEY_SHARE, ZK_PUBLIC & ZF_PROOF twice
8553 * At round two perform a single cycle
8554 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008555 unsigned int remaining_steps = (round == MBEDTLS_ECJPAKE_ROUND_ONE) ? 2 : 1;
Valerio Settia08b1a42022-11-17 15:10:02 +01008556
Gilles Peskine449bd832023-01-11 14:50:10 +01008557 for (; remaining_steps > 0; remaining_steps--) {
8558 for (psa_pake_step_t step = PSA_PAKE_STEP_KEY_SHARE;
8559 step <= PSA_PAKE_STEP_ZK_PROOF;
8560 ++step) {
Valerio Setti79f6b6b2022-11-21 14:17:03 +01008561 /*
Valerio Settid4a9b1a2022-11-22 11:11:10 +01008562 * For each step, prepend 1 byte with the length of the data as
8563 * given by psa_pake_output().
Valerio Setti79f6b6b2022-11-21 14:17:03 +01008564 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008565 status = psa_pake_output(pake_ctx, step,
8566 buf + output_offset + 1,
8567 len - output_offset - 1,
8568 &output_len);
8569 if (status != PSA_SUCCESS) {
8570 return psa_ssl_status_to_mbedtls(status);
Valerio Settia08b1a42022-11-17 15:10:02 +01008571 }
8572
Valerio Setti99d88c12022-11-22 16:03:43 +01008573 *(buf + output_offset) = (uint8_t) output_len;
Valerio Setti79f6b6b2022-11-21 14:17:03 +01008574
8575 output_offset += output_len + 1;
Valerio Settia08b1a42022-11-17 15:10:02 +01008576 }
8577 }
8578
8579 *olen = output_offset;
8580
Gilles Peskine449bd832023-01-11 14:50:10 +01008581 return 0;
Valerio Settia08b1a42022-11-17 15:10:02 +01008582}
Valerio Settia08b1a42022-11-17 15:10:02 +01008583#endif //MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED && MBEDTLS_USE_PSA_CRYPTO
8584
Jerry Yuee40f9d2022-02-17 14:55:16 +08008585#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008586int mbedtls_ssl_get_key_exchange_md_tls1_2(mbedtls_ssl_context *ssl,
8587 unsigned char *hash, size_t *hashlen,
8588 unsigned char *data, size_t data_len,
8589 mbedtls_md_type_t md_alg)
Jerry Yuee40f9d2022-02-17 14:55:16 +08008590{
8591 psa_status_t status;
8592 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01008593 psa_algorithm_t hash_alg = mbedtls_hash_info_psa_from_md(md_alg);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008594
Gilles Peskine449bd832023-01-11 14:50:10 +01008595 MBEDTLS_SSL_DEBUG_MSG(3, ("Perform PSA-based computation of digest of ServerKeyExchange"));
Jerry Yuee40f9d2022-02-17 14:55:16 +08008596
Gilles Peskine449bd832023-01-11 14:50:10 +01008597 if ((status = psa_hash_setup(&hash_operation,
8598 hash_alg)) != PSA_SUCCESS) {
8599 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_setup", status);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008600 goto exit;
8601 }
8602
Gilles Peskine449bd832023-01-11 14:50:10 +01008603 if ((status = psa_hash_update(&hash_operation, ssl->handshake->randbytes,
8604 64)) != PSA_SUCCESS) {
8605 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_update", status);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008606 goto exit;
8607 }
8608
Gilles Peskine449bd832023-01-11 14:50:10 +01008609 if ((status = psa_hash_update(&hash_operation,
8610 data, data_len)) != PSA_SUCCESS) {
8611 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_update", status);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008612 goto exit;
8613 }
8614
Gilles Peskine449bd832023-01-11 14:50:10 +01008615 if ((status = psa_hash_finish(&hash_operation, hash, PSA_HASH_MAX_SIZE,
8616 hashlen)) != PSA_SUCCESS) {
8617 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_finish", status);
8618 goto exit;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008619 }
8620
8621exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008622 if (status != PSA_SUCCESS) {
8623 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8624 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
8625 switch (status) {
Jerry Yuee40f9d2022-02-17 14:55:16 +08008626 case PSA_ERROR_NOT_SUPPORTED:
Gilles Peskine449bd832023-01-11 14:50:10 +01008627 return MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008628 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
8629 case PSA_ERROR_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +01008630 return MBEDTLS_ERR_MD_BAD_INPUT_DATA;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008631 case PSA_ERROR_INSUFFICIENT_MEMORY:
Gilles Peskine449bd832023-01-11 14:50:10 +01008632 return MBEDTLS_ERR_MD_ALLOC_FAILED;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008633 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01008634 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008635 }
8636 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008637 return 0;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008638}
8639
8640#else
8641
Gilles Peskine449bd832023-01-11 14:50:10 +01008642int mbedtls_ssl_get_key_exchange_md_tls1_2(mbedtls_ssl_context *ssl,
8643 unsigned char *hash, size_t *hashlen,
8644 unsigned char *data, size_t data_len,
8645 mbedtls_md_type_t md_alg)
Jerry Yuee40f9d2022-02-17 14:55:16 +08008646{
8647 int ret = 0;
8648 mbedtls_md_context_t ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +01008649 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type(md_alg);
8650 *hashlen = mbedtls_md_get_size(md_info);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008651
Gilles Peskine449bd832023-01-11 14:50:10 +01008652 MBEDTLS_SSL_DEBUG_MSG(3, ("Perform mbedtls-based computation of digest of ServerKeyExchange"));
Jerry Yuee40f9d2022-02-17 14:55:16 +08008653
Gilles Peskine449bd832023-01-11 14:50:10 +01008654 mbedtls_md_init(&ctx);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008655
8656 /*
8657 * digitally-signed struct {
8658 * opaque client_random[32];
8659 * opaque server_random[32];
8660 * ServerDHParams params;
8661 * };
8662 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008663 if ((ret = mbedtls_md_setup(&ctx, md_info, 0)) != 0) {
8664 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_setup", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008665 goto exit;
8666 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008667 if ((ret = mbedtls_md_starts(&ctx)) != 0) {
8668 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_starts", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008669 goto exit;
8670 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008671 if ((ret = mbedtls_md_update(&ctx, ssl->handshake->randbytes, 64)) != 0) {
8672 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_update", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008673 goto exit;
8674 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008675 if ((ret = mbedtls_md_update(&ctx, data, data_len)) != 0) {
8676 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_update", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008677 goto exit;
8678 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008679 if ((ret = mbedtls_md_finish(&ctx, hash)) != 0) {
8680 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_finish", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008681 goto exit;
8682 }
8683
8684exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008685 mbedtls_md_free(&ctx);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008686
Gilles Peskine449bd832023-01-11 14:50:10 +01008687 if (ret != 0) {
8688 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8689 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
8690 }
Jerry Yuee40f9d2022-02-17 14:55:16 +08008691
Gilles Peskine449bd832023-01-11 14:50:10 +01008692 return ret;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008693}
8694#endif /* MBEDTLS_USE_PSA_CRYPTO */
8695
Jerry Yud9d91da2022-02-17 14:57:06 +08008696#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
8697
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008698/* Find the preferred hash for a given signature algorithm. */
8699unsigned int mbedtls_ssl_tls12_get_preferred_hash_for_sig_alg(
Gilles Peskine449bd832023-01-11 14:50:10 +01008700 mbedtls_ssl_context *ssl,
8701 unsigned int sig_alg)
Jerry Yud9d91da2022-02-17 14:57:06 +08008702{
Gabor Mezei078e8032022-04-27 21:17:56 +02008703 unsigned int i;
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008704 uint16_t *received_sig_algs = ssl->handshake->received_sig_algs;
Gabor Mezei078e8032022-04-27 21:17:56 +02008705
Gilles Peskine449bd832023-01-11 14:50:10 +01008706 if (sig_alg == MBEDTLS_SSL_SIG_ANON) {
8707 return MBEDTLS_SSL_HASH_NONE;
8708 }
Gabor Mezei078e8032022-04-27 21:17:56 +02008709
Gilles Peskine449bd832023-01-11 14:50:10 +01008710 for (i = 0; received_sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++) {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008711 unsigned int hash_alg_received =
Gilles Peskine449bd832023-01-11 14:50:10 +01008712 MBEDTLS_SSL_TLS12_HASH_ALG_FROM_SIG_AND_HASH_ALG(
8713 received_sig_algs[i]);
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008714 unsigned int sig_alg_received =
Gilles Peskine449bd832023-01-11 14:50:10 +01008715 MBEDTLS_SSL_TLS12_SIG_ALG_FROM_SIG_AND_HASH_ALG(
8716 received_sig_algs[i]);
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008717
Gilles Peskine449bd832023-01-11 14:50:10 +01008718 if (sig_alg == sig_alg_received) {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008719#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008720 if (ssl->handshake->key_cert && ssl->handshake->key_cert->key) {
Neil Armstrong96eceb82022-06-30 18:05:05 +02008721 psa_algorithm_t psa_hash_alg =
Gilles Peskine449bd832023-01-11 14:50:10 +01008722 mbedtls_hash_info_psa_from_md(hash_alg_received);
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008723
Gilles Peskine449bd832023-01-11 14:50:10 +01008724 if (sig_alg_received == MBEDTLS_SSL_SIG_ECDSA &&
8725 !mbedtls_pk_can_do_ext(ssl->handshake->key_cert->key,
8726 PSA_ALG_ECDSA(psa_hash_alg),
8727 PSA_KEY_USAGE_SIGN_HASH)) {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008728 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01008729 }
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008730
Gilles Peskine449bd832023-01-11 14:50:10 +01008731 if (sig_alg_received == MBEDTLS_SSL_SIG_RSA &&
8732 !mbedtls_pk_can_do_ext(ssl->handshake->key_cert->key,
8733 PSA_ALG_RSA_PKCS1V15_SIGN(
8734 psa_hash_alg),
8735 PSA_KEY_USAGE_SIGN_HASH)) {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008736 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01008737 }
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008738 }
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008739#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong96eceb82022-06-30 18:05:05 +02008740
Gilles Peskine449bd832023-01-11 14:50:10 +01008741 return hash_alg_received;
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008742 }
Jerry Yud9d91da2022-02-17 14:57:06 +08008743 }
Jerry Yud9d91da2022-02-17 14:57:06 +08008744
Gilles Peskine449bd832023-01-11 14:50:10 +01008745 return MBEDTLS_SSL_HASH_NONE;
Jerry Yud9d91da2022-02-17 14:57:06 +08008746}
8747
8748#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
8749
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008750/* Serialization of TLS 1.2 sessions:
8751 *
8752 * struct {
8753 * uint64 start_time;
8754 * uint8 ciphersuite[2]; // defined by the standard
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008755 * uint8 session_id_len; // at most 32
8756 * opaque session_id[32];
8757 * opaque master[48]; // fixed length in the standard
8758 * uint32 verify_result;
8759 * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert
8760 * opaque ticket<0..2^24-1>; // length 0 means no ticket
8761 * uint32 ticket_lifetime;
8762 * uint8 mfl_code; // up to 255 according to standard
8763 * uint8 encrypt_then_mac; // 0 or 1
8764 * } serialized_session_tls12;
8765 *
8766 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008767static size_t ssl_tls12_session_save(const mbedtls_ssl_session *session,
8768 unsigned char *buf,
8769 size_t buf_len)
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008770{
8771 unsigned char *p = buf;
8772 size_t used = 0;
8773
8774#if defined(MBEDTLS_HAVE_TIME)
8775 uint64_t start;
8776#endif
8777#if defined(MBEDTLS_X509_CRT_PARSE_C)
8778#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8779 size_t cert_len;
8780#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8781#endif /* MBEDTLS_X509_CRT_PARSE_C */
8782
8783 /*
8784 * Time
8785 */
8786#if defined(MBEDTLS_HAVE_TIME)
8787 used += 8;
8788
Gilles Peskine449bd832023-01-11 14:50:10 +01008789 if (used <= buf_len) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008790 start = (uint64_t) session->start;
8791
Gilles Peskine449bd832023-01-11 14:50:10 +01008792 MBEDTLS_PUT_UINT64_BE(start, p, 0);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008793 p += 8;
8794 }
8795#endif /* MBEDTLS_HAVE_TIME */
8796
8797 /*
8798 * Basic mandatory fields
8799 */
8800 used += 2 /* ciphersuite */
Gilles Peskine449bd832023-01-11 14:50:10 +01008801 + 1 /* id_len */
8802 + sizeof(session->id)
8803 + sizeof(session->master)
8804 + 4; /* verify_result */
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008805
Gilles Peskine449bd832023-01-11 14:50:10 +01008806 if (used <= buf_len) {
8807 MBEDTLS_PUT_UINT16_BE(session->ciphersuite, p, 0);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008808 p += 2;
8809
Gilles Peskine449bd832023-01-11 14:50:10 +01008810 *p++ = MBEDTLS_BYTE_0(session->id_len);
8811 memcpy(p, session->id, 32);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008812 p += 32;
8813
Gilles Peskine449bd832023-01-11 14:50:10 +01008814 memcpy(p, session->master, 48);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008815 p += 48;
8816
Gilles Peskine449bd832023-01-11 14:50:10 +01008817 MBEDTLS_PUT_UINT32_BE(session->verify_result, p, 0);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008818 p += 4;
8819 }
8820
8821 /*
8822 * Peer's end-entity certificate
8823 */
8824#if defined(MBEDTLS_X509_CRT_PARSE_C)
8825#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01008826 if (session->peer_cert == NULL) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008827 cert_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01008828 } else {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008829 cert_len = session->peer_cert->raw.len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008830 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008831
8832 used += 3 + cert_len;
8833
Gilles Peskine449bd832023-01-11 14:50:10 +01008834 if (used <= buf_len) {
8835 *p++ = MBEDTLS_BYTE_2(cert_len);
8836 *p++ = MBEDTLS_BYTE_1(cert_len);
8837 *p++ = MBEDTLS_BYTE_0(cert_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008838
Gilles Peskine449bd832023-01-11 14:50:10 +01008839 if (session->peer_cert != NULL) {
8840 memcpy(p, session->peer_cert->raw.p, cert_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008841 p += cert_len;
8842 }
8843 }
8844#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008845 if (session->peer_cert_digest != NULL) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008846 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008847 if (used <= buf_len) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008848 *p++ = (unsigned char) session->peer_cert_digest_type;
8849 *p++ = (unsigned char) session->peer_cert_digest_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008850 memcpy(p, session->peer_cert_digest,
8851 session->peer_cert_digest_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008852 p += session->peer_cert_digest_len;
8853 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008854 } else {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008855 used += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01008856 if (used <= buf_len) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008857 *p++ = (unsigned char) MBEDTLS_MD_NONE;
8858 *p++ = 0;
8859 }
8860 }
8861#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8862#endif /* MBEDTLS_X509_CRT_PARSE_C */
8863
8864 /*
8865 * Session ticket if any, plus associated data
8866 */
8867#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8868 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
8869
Gilles Peskine449bd832023-01-11 14:50:10 +01008870 if (used <= buf_len) {
8871 *p++ = MBEDTLS_BYTE_2(session->ticket_len);
8872 *p++ = MBEDTLS_BYTE_1(session->ticket_len);
8873 *p++ = MBEDTLS_BYTE_0(session->ticket_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008874
Gilles Peskine449bd832023-01-11 14:50:10 +01008875 if (session->ticket != NULL) {
8876 memcpy(p, session->ticket, session->ticket_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008877 p += session->ticket_len;
8878 }
8879
Gilles Peskine449bd832023-01-11 14:50:10 +01008880 MBEDTLS_PUT_UINT32_BE(session->ticket_lifetime, p, 0);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008881 p += 4;
8882 }
8883#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8884
8885 /*
8886 * Misc extension-related info
8887 */
8888#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8889 used += 1;
8890
Gilles Peskine449bd832023-01-11 14:50:10 +01008891 if (used <= buf_len) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008892 *p++ = session->mfl_code;
Gilles Peskine449bd832023-01-11 14:50:10 +01008893 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008894#endif
8895
8896#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
8897 used += 1;
8898
Gilles Peskine449bd832023-01-11 14:50:10 +01008899 if (used <= buf_len) {
8900 *p++ = MBEDTLS_BYTE_0(session->encrypt_then_mac);
8901 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008902#endif
8903
Gilles Peskine449bd832023-01-11 14:50:10 +01008904 return used;
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008905}
8906
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02008907MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01008908static int ssl_tls12_session_load(mbedtls_ssl_session *session,
8909 const unsigned char *buf,
8910 size_t len)
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008911{
8912#if defined(MBEDTLS_HAVE_TIME)
8913 uint64_t start;
8914#endif
8915#if defined(MBEDTLS_X509_CRT_PARSE_C)
8916#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8917 size_t cert_len;
8918#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8919#endif /* MBEDTLS_X509_CRT_PARSE_C */
8920
8921 const unsigned char *p = buf;
8922 const unsigned char * const end = buf + len;
8923
8924 /*
8925 * Time
8926 */
8927#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01008928 if (8 > (size_t) (end - p)) {
8929 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
8930 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008931
Gilles Peskine449bd832023-01-11 14:50:10 +01008932 start = ((uint64_t) p[0] << 56) |
8933 ((uint64_t) p[1] << 48) |
8934 ((uint64_t) p[2] << 40) |
8935 ((uint64_t) p[3] << 32) |
8936 ((uint64_t) p[4] << 24) |
8937 ((uint64_t) p[5] << 16) |
8938 ((uint64_t) p[6] << 8) |
8939 ((uint64_t) p[7]);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008940 p += 8;
8941
8942 session->start = (time_t) start;
8943#endif /* MBEDTLS_HAVE_TIME */
8944
8945 /*
8946 * Basic mandatory fields
8947 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008948 if (2 + 1 + 32 + 48 + 4 > (size_t) (end - p)) {
8949 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
8950 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008951
Gilles Peskine449bd832023-01-11 14:50:10 +01008952 session->ciphersuite = (p[0] << 8) | p[1];
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008953 p += 2;
8954
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008955 session->id_len = *p++;
Gilles Peskine449bd832023-01-11 14:50:10 +01008956 memcpy(session->id, p, 32);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008957 p += 32;
8958
Gilles Peskine449bd832023-01-11 14:50:10 +01008959 memcpy(session->master, p, 48);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008960 p += 48;
8961
Gilles Peskine449bd832023-01-11 14:50:10 +01008962 session->verify_result = ((uint32_t) p[0] << 24) |
8963 ((uint32_t) p[1] << 16) |
8964 ((uint32_t) p[2] << 8) |
8965 ((uint32_t) p[3]);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008966 p += 4;
8967
8968 /* Immediately clear invalid pointer values that have been read, in case
8969 * we exit early before we replaced them with valid ones. */
8970#if defined(MBEDTLS_X509_CRT_PARSE_C)
8971#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8972 session->peer_cert = NULL;
8973#else
8974 session->peer_cert_digest = NULL;
8975#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8976#endif /* MBEDTLS_X509_CRT_PARSE_C */
8977#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8978 session->ticket = NULL;
8979#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8980
8981 /*
8982 * Peer certificate
8983 */
8984#if defined(MBEDTLS_X509_CRT_PARSE_C)
8985#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8986 /* Deserialize CRT from the end of the ticket. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008987 if (3 > (size_t) (end - p)) {
8988 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
8989 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008990
Gilles Peskine449bd832023-01-11 14:50:10 +01008991 cert_len = (p[0] << 16) | (p[1] << 8) | p[2];
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008992 p += 3;
8993
Gilles Peskine449bd832023-01-11 14:50:10 +01008994 if (cert_len != 0) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008995 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
8996
Gilles Peskine449bd832023-01-11 14:50:10 +01008997 if (cert_len > (size_t) (end - p)) {
8998 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
8999 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009000
Gilles Peskine449bd832023-01-11 14:50:10 +01009001 session->peer_cert = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009002
Gilles Peskine449bd832023-01-11 14:50:10 +01009003 if (session->peer_cert == NULL) {
9004 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
9005 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009006
Gilles Peskine449bd832023-01-11 14:50:10 +01009007 mbedtls_x509_crt_init(session->peer_cert);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009008
Gilles Peskine449bd832023-01-11 14:50:10 +01009009 if ((ret = mbedtls_x509_crt_parse_der(session->peer_cert,
9010 p, cert_len)) != 0) {
9011 mbedtls_x509_crt_free(session->peer_cert);
9012 mbedtls_free(session->peer_cert);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009013 session->peer_cert = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01009014 return ret;
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009015 }
9016
9017 p += cert_len;
9018 }
9019#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9020 /* Deserialize CRT digest from the end of the ticket. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009021 if (2 > (size_t) (end - p)) {
9022 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9023 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009024
9025 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
9026 session->peer_cert_digest_len = (size_t) *p++;
9027
Gilles Peskine449bd832023-01-11 14:50:10 +01009028 if (session->peer_cert_digest_len != 0) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009029 const mbedtls_md_info_t *md_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01009030 mbedtls_md_info_from_type(session->peer_cert_digest_type);
9031 if (md_info == NULL) {
9032 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9033 }
9034 if (session->peer_cert_digest_len != mbedtls_md_get_size(md_info)) {
9035 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9036 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009037
Gilles Peskine449bd832023-01-11 14:50:10 +01009038 if (session->peer_cert_digest_len > (size_t) (end - p)) {
9039 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9040 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009041
9042 session->peer_cert_digest =
Gilles Peskine449bd832023-01-11 14:50:10 +01009043 mbedtls_calloc(1, session->peer_cert_digest_len);
9044 if (session->peer_cert_digest == NULL) {
9045 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
9046 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009047
Gilles Peskine449bd832023-01-11 14:50:10 +01009048 memcpy(session->peer_cert_digest, p,
9049 session->peer_cert_digest_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009050 p += session->peer_cert_digest_len;
9051 }
9052#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9053#endif /* MBEDTLS_X509_CRT_PARSE_C */
9054
9055 /*
9056 * Session ticket and associated data
9057 */
9058#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01009059 if (3 > (size_t) (end - p)) {
9060 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9061 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009062
Gilles Peskine449bd832023-01-11 14:50:10 +01009063 session->ticket_len = (p[0] << 16) | (p[1] << 8) | p[2];
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009064 p += 3;
9065
Gilles Peskine449bd832023-01-11 14:50:10 +01009066 if (session->ticket_len != 0) {
9067 if (session->ticket_len > (size_t) (end - p)) {
9068 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9069 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009070
Gilles Peskine449bd832023-01-11 14:50:10 +01009071 session->ticket = mbedtls_calloc(1, session->ticket_len);
9072 if (session->ticket == NULL) {
9073 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
9074 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009075
Gilles Peskine449bd832023-01-11 14:50:10 +01009076 memcpy(session->ticket, p, session->ticket_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009077 p += session->ticket_len;
9078 }
9079
Gilles Peskine449bd832023-01-11 14:50:10 +01009080 if (4 > (size_t) (end - p)) {
9081 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9082 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009083
Gilles Peskine449bd832023-01-11 14:50:10 +01009084 session->ticket_lifetime = ((uint32_t) p[0] << 24) |
9085 ((uint32_t) p[1] << 16) |
9086 ((uint32_t) p[2] << 8) |
9087 ((uint32_t) p[3]);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009088 p += 4;
9089#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
9090
9091 /*
9092 * Misc extension-related info
9093 */
9094#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01009095 if (1 > (size_t) (end - p)) {
9096 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9097 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009098
9099 session->mfl_code = *p++;
9100#endif
9101
9102#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01009103 if (1 > (size_t) (end - p)) {
9104 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9105 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009106
9107 session->encrypt_then_mac = *p++;
9108#endif
9109
9110 /* Done, should have consumed entire buffer */
Gilles Peskine449bd832023-01-11 14:50:10 +01009111 if (p != end) {
9112 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9113 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009114
Gilles Peskine449bd832023-01-11 14:50:10 +01009115 return 0;
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009116}
Jerry Yudc7bd172022-02-17 13:44:15 +08009117#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9118
XiaokangQian75d40ef2022-04-20 11:05:24 +00009119int mbedtls_ssl_validate_ciphersuite(
9120 const mbedtls_ssl_context *ssl,
9121 const mbedtls_ssl_ciphersuite_t *suite_info,
9122 mbedtls_ssl_protocol_version min_tls_version,
Gilles Peskine449bd832023-01-11 14:50:10 +01009123 mbedtls_ssl_protocol_version max_tls_version)
XiaokangQian75d40ef2022-04-20 11:05:24 +00009124{
9125 (void) ssl;
9126
Gilles Peskine449bd832023-01-11 14:50:10 +01009127 if (suite_info == NULL) {
9128 return -1;
9129 }
XiaokangQian75d40ef2022-04-20 11:05:24 +00009130
Gilles Peskine449bd832023-01-11 14:50:10 +01009131 if ((suite_info->min_tls_version > max_tls_version) ||
9132 (suite_info->max_tls_version < min_tls_version)) {
9133 return -1;
XiaokangQian75d40ef2022-04-20 11:05:24 +00009134 }
9135
XiaokangQian060d8672022-04-21 09:24:56 +00009136#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_CLI_C)
XiaokangQian75d40ef2022-04-20 11:05:24 +00009137#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Neil Armstrongca7d5062022-05-31 14:43:23 +02009138#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01009139 if (suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
9140 ssl->handshake->psa_pake_ctx_is_ok != 1)
Neil Armstrongca7d5062022-05-31 14:43:23 +02009141#else
Gilles Peskine449bd832023-01-11 14:50:10 +01009142 if (suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
9143 mbedtls_ecjpake_check(&ssl->handshake->ecjpake_ctx) != 0)
Neil Armstrongca7d5062022-05-31 14:43:23 +02009144#endif /* MBEDTLS_USE_PSA_CRYPTO */
XiaokangQian75d40ef2022-04-20 11:05:24 +00009145 {
Gilles Peskine449bd832023-01-11 14:50:10 +01009146 return -1;
XiaokangQian75d40ef2022-04-20 11:05:24 +00009147 }
9148#endif
9149
9150 /* Don't suggest PSK-based ciphersuite if no PSK is available. */
9151#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01009152 if (mbedtls_ssl_ciphersuite_uses_psk(suite_info) &&
9153 mbedtls_ssl_conf_has_static_psk(ssl->conf) == 0) {
9154 return -1;
XiaokangQian75d40ef2022-04-20 11:05:24 +00009155 }
9156#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
9157#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9158
Gilles Peskine449bd832023-01-11 14:50:10 +01009159 return 0;
XiaokangQian75d40ef2022-04-20 11:05:24 +00009160}
9161
Ronald Crone68ab4f2022-10-05 12:46:29 +02009162#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
XiaokangQianeaf36512022-04-24 09:07:44 +00009163/*
9164 * Function for writing a signature algorithm extension.
9165 *
9166 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
9167 * value (TLS 1.3 RFC8446):
9168 * enum {
9169 * ....
9170 * ecdsa_secp256r1_sha256( 0x0403 ),
9171 * ecdsa_secp384r1_sha384( 0x0503 ),
9172 * ecdsa_secp521r1_sha512( 0x0603 ),
9173 * ....
9174 * } SignatureScheme;
9175 *
9176 * struct {
9177 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
9178 * } SignatureSchemeList;
9179 *
9180 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
9181 * value (TLS 1.2 RFC5246):
9182 * enum {
9183 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
9184 * sha512(6), (255)
9185 * } HashAlgorithm;
9186 *
9187 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
9188 * SignatureAlgorithm;
9189 *
9190 * struct {
9191 * HashAlgorithm hash;
9192 * SignatureAlgorithm signature;
9193 * } SignatureAndHashAlgorithm;
9194 *
9195 * SignatureAndHashAlgorithm
9196 * supported_signature_algorithms<2..2^16-2>;
9197 *
9198 * The TLS 1.3 signature algorithm extension was defined to be a compatible
9199 * generalization of the TLS 1.2 signature algorithm extension.
9200 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
9201 * `SignatureScheme` field of TLS 1.3
9202 *
9203 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009204int mbedtls_ssl_write_sig_alg_ext(mbedtls_ssl_context *ssl, unsigned char *buf,
9205 const unsigned char *end, size_t *out_len)
XiaokangQianeaf36512022-04-24 09:07:44 +00009206{
9207 unsigned char *p = buf;
9208 unsigned char *supported_sig_alg; /* Start of supported_signature_algorithms */
9209 size_t supported_sig_alg_len = 0; /* Length of supported_signature_algorithms */
9210
9211 *out_len = 0;
9212
Gilles Peskine449bd832023-01-11 14:50:10 +01009213 MBEDTLS_SSL_DEBUG_MSG(3, ("adding signature_algorithms extension"));
XiaokangQianeaf36512022-04-24 09:07:44 +00009214
9215 /* Check if we have space for header and length field:
9216 * - extension_type (2 bytes)
9217 * - extension_data_length (2 bytes)
9218 * - supported_signature_algorithms_length (2 bytes)
9219 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009220 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 6);
XiaokangQianeaf36512022-04-24 09:07:44 +00009221 p += 6;
9222
9223 /*
9224 * Write supported_signature_algorithms
9225 */
9226 supported_sig_alg = p;
Gilles Peskine449bd832023-01-11 14:50:10 +01009227 const uint16_t *sig_alg = mbedtls_ssl_get_sig_algs(ssl);
9228 if (sig_alg == NULL) {
9229 return MBEDTLS_ERR_SSL_BAD_CONFIG;
9230 }
XiaokangQianeaf36512022-04-24 09:07:44 +00009231
Gilles Peskine449bd832023-01-11 14:50:10 +01009232 for (; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++) {
9233 MBEDTLS_SSL_DEBUG_MSG(3, ("got signature scheme [%x] %s",
9234 *sig_alg,
9235 mbedtls_ssl_sig_alg_to_str(*sig_alg)));
9236 if (!mbedtls_ssl_sig_alg_is_supported(ssl, *sig_alg)) {
XiaokangQianeaf36512022-04-24 09:07:44 +00009237 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01009238 }
9239 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
9240 MBEDTLS_PUT_UINT16_BE(*sig_alg, p, 0);
XiaokangQianeaf36512022-04-24 09:07:44 +00009241 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01009242 MBEDTLS_SSL_DEBUG_MSG(3, ("sent signature scheme [%x] %s",
9243 *sig_alg,
9244 mbedtls_ssl_sig_alg_to_str(*sig_alg)));
XiaokangQianeaf36512022-04-24 09:07:44 +00009245 }
9246
9247 /* Length of supported_signature_algorithms */
9248 supported_sig_alg_len = p - supported_sig_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01009249 if (supported_sig_alg_len == 0) {
9250 MBEDTLS_SSL_DEBUG_MSG(1, ("No signature algorithms defined."));
9251 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
XiaokangQianeaf36512022-04-24 09:07:44 +00009252 }
9253
Gilles Peskine449bd832023-01-11 14:50:10 +01009254 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SIG_ALG, buf, 0);
9255 MBEDTLS_PUT_UINT16_BE(supported_sig_alg_len + 2, buf, 2);
9256 MBEDTLS_PUT_UINT16_BE(supported_sig_alg_len, buf, 4);
XiaokangQianeaf36512022-04-24 09:07:44 +00009257
XiaokangQianeaf36512022-04-24 09:07:44 +00009258 *out_len = p - buf;
9259
9260#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01009261 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_SIG_ALG);
XiaokangQianeaf36512022-04-24 09:07:44 +00009262#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu0c354a22022-08-29 15:25:36 +08009263
Gilles Peskine449bd832023-01-11 14:50:10 +01009264 return 0;
XiaokangQianeaf36512022-04-24 09:07:44 +00009265}
Ronald Crone68ab4f2022-10-05 12:46:29 +02009266#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
XiaokangQianeaf36512022-04-24 09:07:44 +00009267
XiaokangQian40a35232022-05-07 09:02:40 +00009268#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
XiaokangQian9b2b7712022-05-17 02:57:00 +00009269/*
9270 * mbedtls_ssl_parse_server_name_ext
9271 *
9272 * Structure of server_name extension:
9273 *
9274 * enum {
9275 * host_name(0), (255)
9276 * } NameType;
9277 * opaque HostName<1..2^16-1>;
9278 *
9279 * struct {
9280 * NameType name_type;
9281 * select (name_type) {
9282 * case host_name: HostName;
9283 * } name;
9284 * } ServerName;
9285 * struct {
9286 * ServerName server_name_list<1..2^16-1>
9287 * } ServerNameList;
9288 */
Ronald Cronce7d76e2022-07-08 18:56:49 +02009289MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01009290int mbedtls_ssl_parse_server_name_ext(mbedtls_ssl_context *ssl,
9291 const unsigned char *buf,
9292 const unsigned char *end)
XiaokangQian40a35232022-05-07 09:02:40 +00009293{
9294 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
9295 const unsigned char *p = buf;
XiaokangQian9b2b7712022-05-17 02:57:00 +00009296 size_t server_name_list_len, hostname_len;
9297 const unsigned char *server_name_list_end;
XiaokangQian40a35232022-05-07 09:02:40 +00009298
Gilles Peskine449bd832023-01-11 14:50:10 +01009299 MBEDTLS_SSL_DEBUG_MSG(3, ("parse ServerName extension"));
XiaokangQian40a35232022-05-07 09:02:40 +00009300
Gilles Peskine449bd832023-01-11 14:50:10 +01009301 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
9302 server_name_list_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian40a35232022-05-07 09:02:40 +00009303 p += 2;
9304
Gilles Peskine449bd832023-01-11 14:50:10 +01009305 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, server_name_list_len);
XiaokangQian9b2b7712022-05-17 02:57:00 +00009306 server_name_list_end = p + server_name_list_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01009307 while (p < server_name_list_end) {
9308 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, server_name_list_end, 3);
9309 hostname_len = MBEDTLS_GET_UINT16_BE(p, 1);
9310 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, server_name_list_end,
9311 hostname_len + 3);
XiaokangQian40a35232022-05-07 09:02:40 +00009312
Gilles Peskine449bd832023-01-11 14:50:10 +01009313 if (p[0] == MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME) {
XiaokangQian75fe8c72022-06-15 09:42:45 +00009314 /* sni_name is intended to be used only during the parsing of the
9315 * ClientHello message (it is reset to NULL before the end of
9316 * the message parsing). Thus it is ok to just point to the
9317 * reception buffer and not make a copy of it.
9318 */
XiaokangQianf2a94202022-05-20 06:44:24 +00009319 ssl->handshake->sni_name = p + 3;
9320 ssl->handshake->sni_name_len = hostname_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01009321 if (ssl->conf->f_sni == NULL) {
9322 return 0;
XiaokangQian40a35232022-05-07 09:02:40 +00009323 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009324 ret = ssl->conf->f_sni(ssl->conf->p_sni,
9325 ssl, p + 3, hostname_len);
9326 if (ret != 0) {
9327 MBEDTLS_SSL_DEBUG_RET(1, "ssl_sni_wrapper", ret);
9328 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_UNRECOGNIZED_NAME,
9329 MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME);
9330 return MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME;
9331 }
9332 return 0;
XiaokangQian40a35232022-05-07 09:02:40 +00009333 }
9334
9335 p += hostname_len + 3;
9336 }
9337
Gilles Peskine449bd832023-01-11 14:50:10 +01009338 return 0;
XiaokangQian40a35232022-05-07 09:02:40 +00009339}
9340#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
9341
XiaokangQianacb39922022-06-17 10:18:48 +00009342#if defined(MBEDTLS_SSL_ALPN)
Ronald Cronce7d76e2022-07-08 18:56:49 +02009343MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01009344int mbedtls_ssl_parse_alpn_ext(mbedtls_ssl_context *ssl,
9345 const unsigned char *buf,
9346 const unsigned char *end)
XiaokangQianacb39922022-06-17 10:18:48 +00009347{
9348 const unsigned char *p = buf;
XiaokangQianc7403452022-06-23 03:24:12 +00009349 size_t protocol_name_list_len;
XiaokangQian95d5f542022-06-24 02:29:26 +00009350 const unsigned char *protocol_name_list;
9351 const unsigned char *protocol_name_list_end;
XiaokangQianc7403452022-06-23 03:24:12 +00009352 size_t protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009353
9354 /* If ALPN not configured, just ignore the extension */
Gilles Peskine449bd832023-01-11 14:50:10 +01009355 if (ssl->conf->alpn_list == NULL) {
9356 return 0;
9357 }
XiaokangQianacb39922022-06-17 10:18:48 +00009358
9359 /*
XiaokangQianc7403452022-06-23 03:24:12 +00009360 * RFC7301, section 3.1
9361 * opaque ProtocolName<1..2^8-1>;
XiaokangQianacb39922022-06-17 10:18:48 +00009362 *
XiaokangQianc7403452022-06-23 03:24:12 +00009363 * struct {
9364 * ProtocolName protocol_name_list<2..2^16-1>
9365 * } ProtocolNameList;
XiaokangQianacb39922022-06-17 10:18:48 +00009366 */
9367
XiaokangQianc7403452022-06-23 03:24:12 +00009368 /*
XiaokangQian0b776e22022-06-24 09:04:59 +00009369 * protocol_name_list_len 2 bytes
9370 * protocol_name_len 1 bytes
9371 * protocol_name >=1 byte
XiaokangQianc7403452022-06-23 03:24:12 +00009372 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009373 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 4);
XiaokangQianacb39922022-06-17 10:18:48 +00009374
Gilles Peskine449bd832023-01-11 14:50:10 +01009375 protocol_name_list_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQianacb39922022-06-17 10:18:48 +00009376 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01009377 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, protocol_name_list_len);
XiaokangQian95d5f542022-06-24 02:29:26 +00009378 protocol_name_list = p;
9379 protocol_name_list_end = p + protocol_name_list_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009380
9381 /* Validate peer's list (lengths) */
Gilles Peskine449bd832023-01-11 14:50:10 +01009382 while (p < protocol_name_list_end) {
XiaokangQian95d5f542022-06-24 02:29:26 +00009383 protocol_name_len = *p++;
Gilles Peskine449bd832023-01-11 14:50:10 +01009384 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, protocol_name_list_end,
9385 protocol_name_len);
9386 if (protocol_name_len == 0) {
XiaokangQian95d5f542022-06-24 02:29:26 +00009387 MBEDTLS_SSL_PEND_FATAL_ALERT(
9388 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
Gilles Peskine449bd832023-01-11 14:50:10 +01009389 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
9390 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
XiaokangQian95d5f542022-06-24 02:29:26 +00009391 }
9392
9393 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009394 }
9395
9396 /* Use our order of preference */
Gilles Peskine449bd832023-01-11 14:50:10 +01009397 for (const char **alpn = ssl->conf->alpn_list; *alpn != NULL; alpn++) {
9398 size_t const alpn_len = strlen(*alpn);
XiaokangQian95d5f542022-06-24 02:29:26 +00009399 p = protocol_name_list;
Gilles Peskine449bd832023-01-11 14:50:10 +01009400 while (p < protocol_name_list_end) {
XiaokangQian95d5f542022-06-24 02:29:26 +00009401 protocol_name_len = *p++;
Gilles Peskine449bd832023-01-11 14:50:10 +01009402 if (protocol_name_len == alpn_len &&
9403 memcmp(p, *alpn, alpn_len) == 0) {
XiaokangQianacb39922022-06-17 10:18:48 +00009404 ssl->alpn_chosen = *alpn;
Gilles Peskine449bd832023-01-11 14:50:10 +01009405 return 0;
XiaokangQianacb39922022-06-17 10:18:48 +00009406 }
XiaokangQian95d5f542022-06-24 02:29:26 +00009407
9408 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009409 }
9410 }
9411
XiaokangQian95d5f542022-06-24 02:29:26 +00009412 /* If we get here, no match was found */
XiaokangQianacb39922022-06-17 10:18:48 +00009413 MBEDTLS_SSL_PEND_FATAL_ALERT(
Gilles Peskine449bd832023-01-11 14:50:10 +01009414 MBEDTLS_SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL,
9415 MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL);
9416 return MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL;
XiaokangQianacb39922022-06-17 10:18:48 +00009417}
9418
Gilles Peskine449bd832023-01-11 14:50:10 +01009419int mbedtls_ssl_write_alpn_ext(mbedtls_ssl_context *ssl,
9420 unsigned char *buf,
9421 unsigned char *end,
9422 size_t *out_len)
XiaokangQianacb39922022-06-17 10:18:48 +00009423{
9424 unsigned char *p = buf;
XiaokangQian95d5f542022-06-24 02:29:26 +00009425 size_t protocol_name_len;
XiaokangQianc7403452022-06-23 03:24:12 +00009426 *out_len = 0;
XiaokangQianacb39922022-06-17 10:18:48 +00009427
Gilles Peskine449bd832023-01-11 14:50:10 +01009428 if (ssl->alpn_chosen == NULL) {
9429 return 0;
XiaokangQianacb39922022-06-17 10:18:48 +00009430 }
9431
Gilles Peskine449bd832023-01-11 14:50:10 +01009432 protocol_name_len = strlen(ssl->alpn_chosen);
9433 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 7 + protocol_name_len);
XiaokangQianacb39922022-06-17 10:18:48 +00009434
Gilles Peskine449bd832023-01-11 14:50:10 +01009435 MBEDTLS_SSL_DEBUG_MSG(3, ("server side, adding alpn extension"));
XiaokangQianacb39922022-06-17 10:18:48 +00009436 /*
9437 * 0 . 1 ext identifier
9438 * 2 . 3 ext length
9439 * 4 . 5 protocol list length
9440 * 6 . 6 protocol name length
9441 * 7 . 7+n protocol name
9442 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009443 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_ALPN, p, 0);
XiaokangQianacb39922022-06-17 10:18:48 +00009444
XiaokangQian95d5f542022-06-24 02:29:26 +00009445 *out_len = 7 + protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009446
Gilles Peskine449bd832023-01-11 14:50:10 +01009447 MBEDTLS_PUT_UINT16_BE(protocol_name_len + 3, p, 2);
9448 MBEDTLS_PUT_UINT16_BE(protocol_name_len + 1, p, 4);
XiaokangQian0b776e22022-06-24 09:04:59 +00009449 /* Note: the length of the chosen protocol has been checked to be less
9450 * than 255 bytes in `mbedtls_ssl_conf_alpn_protocols`.
9451 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009452 p[6] = MBEDTLS_BYTE_0(protocol_name_len);
XiaokangQianacb39922022-06-17 10:18:48 +00009453
Gilles Peskine449bd832023-01-11 14:50:10 +01009454 memcpy(p + 7, ssl->alpn_chosen, protocol_name_len);
Jerry Yub95dd362022-11-08 21:19:34 +08009455
9456#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01009457 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_ALPN);
Jerry Yub95dd362022-11-08 21:19:34 +08009458#endif
9459
Gilles Peskine449bd832023-01-11 14:50:10 +01009460 return 0;
XiaokangQianacb39922022-06-17 10:18:48 +00009461}
9462#endif /* MBEDTLS_SSL_ALPN */
9463
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009464#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
Xiaokang Qian03409292022-10-12 02:49:52 +00009465 defined(MBEDTLS_SSL_SESSION_TICKETS) && \
Xiaokang Qianed0620c2022-10-12 06:58:13 +00009466 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \
Xiaokang Qianed3afcd2022-10-12 08:31:11 +00009467 defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01009468int mbedtls_ssl_session_set_hostname(mbedtls_ssl_session *session,
9469 const char *hostname)
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009470{
9471 /* Initialize to suppress unnecessary compiler warning */
9472 size_t hostname_len = 0;
9473
9474 /* Check if new hostname is valid before
9475 * making any change to current one */
Gilles Peskine449bd832023-01-11 14:50:10 +01009476 if (hostname != NULL) {
9477 hostname_len = strlen(hostname);
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009478
Gilles Peskine449bd832023-01-11 14:50:10 +01009479 if (hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN) {
9480 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9481 }
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009482 }
9483
9484 /* Now it's clear that we will overwrite the old hostname,
9485 * so we can free it safely */
Gilles Peskine449bd832023-01-11 14:50:10 +01009486 if (session->hostname != NULL) {
9487 mbedtls_platform_zeroize(session->hostname,
9488 strlen(session->hostname));
9489 mbedtls_free(session->hostname);
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009490 }
9491
9492 /* Passing NULL as hostname shall clear the old one */
Gilles Peskine449bd832023-01-11 14:50:10 +01009493 if (hostname == NULL) {
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009494 session->hostname = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01009495 } else {
9496 session->hostname = mbedtls_calloc(1, hostname_len + 1);
9497 if (session->hostname == NULL) {
9498 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
9499 }
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009500
Gilles Peskine449bd832023-01-11 14:50:10 +01009501 memcpy(session->hostname, hostname, hostname_len);
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009502 }
9503
Gilles Peskine449bd832023-01-11 14:50:10 +01009504 return 0;
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009505}
Xiaokang Qian03409292022-10-12 02:49:52 +00009506#endif /* MBEDTLS_SSL_PROTO_TLS1_3 &&
9507 MBEDTLS_SSL_SESSION_TICKETS &&
Xiaokang Qianed0620c2022-10-12 06:58:13 +00009508 MBEDTLS_SSL_SERVER_NAME_INDICATION &&
Xiaokang Qianed3afcd2022-10-12 08:31:11 +00009509 MBEDTLS_SSL_CLI_C */
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009510
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009511#endif /* MBEDTLS_SSL_TLS_C */