blob: bd8fd8cf783a2809ea61c69b4e817572df1a9640 [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
738#endif /* MBEDTLS_DEBUG_C */
739
Gilles Peskine449bd832023-01-11 14:50:10 +0100740void mbedtls_ssl_optimize_checksum(mbedtls_ssl_context *ssl,
741 const mbedtls_ssl_ciphersuite_t *ciphersuite_info)
Jerry Yuc73c6182022-02-08 20:29:25 +0800742{
743 ((void) ciphersuite_info);
744
Andrzej Kurek25f27152022-08-17 16:09:31 -0400745#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +0100746 if (ciphersuite_info->mac == MBEDTLS_MD_SHA384) {
Jerry Yuc73c6182022-02-08 20:29:25 +0800747 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
Gilles Peskine449bd832023-01-11 14:50:10 +0100748 } else
Jerry Yuc73c6182022-02-08 20:29:25 +0800749#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400750#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +0100751 if (ciphersuite_info->mac != MBEDTLS_MD_SHA384) {
Jerry Yuc73c6182022-02-08 20:29:25 +0800752 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Gilles Peskine449bd832023-01-11 14:50:10 +0100753 } else
Jerry Yuc73c6182022-02-08 20:29:25 +0800754#endif
755 {
Gilles Peskine449bd832023-01-11 14:50:10 +0100756 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
Jerry Yuc73c6182022-02-08 20:29:25 +0800757 return;
758 }
759}
760
Gilles Peskine449bd832023-01-11 14:50:10 +0100761void mbedtls_ssl_add_hs_hdr_to_checksum(mbedtls_ssl_context *ssl,
762 unsigned hs_type,
763 size_t total_hs_len)
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100764{
765 unsigned char hs_hdr[4];
766
767 /* Build HS header for checksum update. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100768 hs_hdr[0] = MBEDTLS_BYTE_0(hs_type);
769 hs_hdr[1] = MBEDTLS_BYTE_2(total_hs_len);
770 hs_hdr[2] = MBEDTLS_BYTE_1(total_hs_len);
771 hs_hdr[3] = MBEDTLS_BYTE_0(total_hs_len);
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100772
Gilles Peskine449bd832023-01-11 14:50:10 +0100773 ssl->handshake->update_checksum(ssl, hs_hdr, sizeof(hs_hdr));
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100774}
775
Gilles Peskine449bd832023-01-11 14:50:10 +0100776void mbedtls_ssl_add_hs_msg_to_checksum(mbedtls_ssl_context *ssl,
777 unsigned hs_type,
778 unsigned char const *msg,
779 size_t msg_len)
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100780{
Gilles Peskine449bd832023-01-11 14:50:10 +0100781 mbedtls_ssl_add_hs_hdr_to_checksum(ssl, hs_type, msg_len);
782 ssl->handshake->update_checksum(ssl, msg, msg_len);
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100783}
784
Gilles Peskine449bd832023-01-11 14:50:10 +0100785void mbedtls_ssl_reset_checksum(mbedtls_ssl_context *ssl)
Jerry Yuc73c6182022-02-08 20:29:25 +0800786{
787 ((void) ssl);
Andrzej Kurek25f27152022-08-17 16:09:31 -0400788#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800789#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100790 psa_hash_abort(&ssl->handshake->fin_sha256_psa);
791 psa_hash_setup(&ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256);
Jerry Yuc73c6182022-02-08 20:29:25 +0800792#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100793 mbedtls_sha256_starts(&ssl->handshake->fin_sha256, 0);
Jerry Yuc73c6182022-02-08 20:29:25 +0800794#endif
795#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400796#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800797#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100798 psa_hash_abort(&ssl->handshake->fin_sha384_psa);
799 psa_hash_setup(&ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384);
Jerry Yuc73c6182022-02-08 20:29:25 +0800800#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100801 mbedtls_sha512_starts(&ssl->handshake->fin_sha384, 1);
Jerry Yuc73c6182022-02-08 20:29:25 +0800802#endif
803#endif
804}
805
Gilles Peskine449bd832023-01-11 14:50:10 +0100806static void ssl_update_checksum_start(mbedtls_ssl_context *ssl,
807 const unsigned char *buf, size_t len)
Jerry Yuc73c6182022-02-08 20:29:25 +0800808{
Andrzej Kurek25f27152022-08-17 16:09:31 -0400809#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800810#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100811 psa_hash_update(&ssl->handshake->fin_sha256_psa, buf, len);
Jerry Yuc73c6182022-02-08 20:29:25 +0800812#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100813 mbedtls_sha256_update(&ssl->handshake->fin_sha256, buf, len);
Jerry Yuc73c6182022-02-08 20:29:25 +0800814#endif
815#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400816#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800817#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100818 psa_hash_update(&ssl->handshake->fin_sha384_psa, buf, len);
Jerry Yuc73c6182022-02-08 20:29:25 +0800819#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100820 mbedtls_sha512_update(&ssl->handshake->fin_sha384, buf, len);
Jerry Yuc73c6182022-02-08 20:29:25 +0800821#endif
822#endif
Andrzej Kurekeabeb302022-10-17 07:52:51 -0400823#if !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
824 !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
825 (void) ssl;
826 (void) buf;
827 (void) len;
828#endif
Jerry Yuc73c6182022-02-08 20:29:25 +0800829}
830
Andrzej Kurek25f27152022-08-17 16:09:31 -0400831#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +0100832static void ssl_update_checksum_sha256(mbedtls_ssl_context *ssl,
833 const unsigned char *buf, size_t len)
Jerry Yuc73c6182022-02-08 20:29:25 +0800834{
835#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100836 psa_hash_update(&ssl->handshake->fin_sha256_psa, buf, len);
Jerry Yuc73c6182022-02-08 20:29:25 +0800837#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100838 mbedtls_sha256_update(&ssl->handshake->fin_sha256, buf, len);
Jerry Yuc73c6182022-02-08 20:29:25 +0800839#endif
840}
841#endif
842
Andrzej Kurek25f27152022-08-17 16:09:31 -0400843#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +0100844static void ssl_update_checksum_sha384(mbedtls_ssl_context *ssl,
845 const unsigned char *buf, size_t len)
Jerry Yuc73c6182022-02-08 20:29:25 +0800846{
847#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +0100848 psa_hash_update(&ssl->handshake->fin_sha384_psa, buf, len);
Jerry Yuc73c6182022-02-08 20:29:25 +0800849#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100850 mbedtls_sha512_update(&ssl->handshake->fin_sha384, buf, len);
Jerry Yuc73c6182022-02-08 20:29:25 +0800851#endif
852}
853#endif
854
Gilles Peskine449bd832023-01-11 14:50:10 +0100855static void ssl_handshake_params_init(mbedtls_ssl_handshake_params *handshake)
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200856{
Gilles Peskine449bd832023-01-11 14:50:10 +0100857 memset(handshake, 0, sizeof(mbedtls_ssl_handshake_params));
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200858
Andrzej Kurek25f27152022-08-17 16:09:31 -0400859#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -0500860#if defined(MBEDTLS_USE_PSA_CRYPTO)
861 handshake->fin_sha256_psa = psa_hash_operation_init();
Gilles Peskine449bd832023-01-11 14:50:10 +0100862 psa_hash_setup(&handshake->fin_sha256_psa, PSA_ALG_SHA_256);
Andrzej Kurekeb342242019-01-29 09:14:33 -0500863#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100864 mbedtls_sha256_init(&handshake->fin_sha256);
865 mbedtls_sha256_starts(&handshake->fin_sha256, 0);
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200866#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -0500867#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400868#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -0500869#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -0500870 handshake->fin_sha384_psa = psa_hash_operation_init();
Gilles Peskine449bd832023-01-11 14:50:10 +0100871 psa_hash_setup(&handshake->fin_sha384_psa, PSA_ALG_SHA_384);
Andrzej Kurekeb342242019-01-29 09:14:33 -0500872#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100873 mbedtls_sha512_init(&handshake->fin_sha384);
874 mbedtls_sha512_starts(&handshake->fin_sha384, 1);
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200875#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -0500876#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200877
878 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +0100879
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200880#if defined(MBEDTLS_DHM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100881 mbedtls_dhm_init(&handshake->dhm_ctx);
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200882#endif
Neil Armstrongf3f46412022-04-12 14:43:39 +0200883#if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100884 mbedtls_ecdh_init(&handshake->ecdh_ctx);
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200885#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +0200886#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Neil Armstrongca7d5062022-05-31 14:43:23 +0200887#if defined(MBEDTLS_USE_PSA_CRYPTO)
888 handshake->psa_pake_ctx = psa_pake_operation_init();
889 handshake->psa_pake_password = MBEDTLS_SVC_KEY_ID_INIT;
890#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100891 mbedtls_ecjpake_init(&handshake->ecjpake_ctx);
Neil Armstrongca7d5062022-05-31 14:43:23 +0200892#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +0200893#if defined(MBEDTLS_SSL_CLI_C)
894 handshake->ecjpake_cache = NULL;
895 handshake->ecjpake_cache_len = 0;
896#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +0200897#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +0200898
Gilles Peskineeccd8882020-03-10 12:19:08 +0100899#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100900 mbedtls_x509_crt_restart_init(&handshake->ecrs_ctx);
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +0200901#endif
902
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +0200903#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
904 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
905#endif
Hanno Becker75173122019-02-06 16:18:31 +0000906
907#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
908 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100909 mbedtls_pk_init(&handshake->peer_pubkey);
Hanno Becker75173122019-02-06 16:18:31 +0000910#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200911}
912
Gilles Peskine449bd832023-01-11 14:50:10 +0100913void mbedtls_ssl_transform_init(mbedtls_ssl_transform *transform)
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200914{
Gilles Peskine449bd832023-01-11 14:50:10 +0100915 memset(transform, 0, sizeof(mbedtls_ssl_transform));
Paul Bakker84bbeb52014-07-01 14:53:22 +0200916
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +0100917#if defined(MBEDTLS_USE_PSA_CRYPTO)
918 transform->psa_key_enc = MBEDTLS_SVC_KEY_ID_INIT;
919 transform->psa_key_dec = MBEDTLS_SVC_KEY_ID_INIT;
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +0100920#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100921 mbedtls_cipher_init(&transform->cipher_ctx_enc);
922 mbedtls_cipher_init(&transform->cipher_ctx_dec);
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +0100923#endif
924
Hanno Beckerfd86ca82020-11-30 08:54:23 +0000925#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong39b8e7d2022-02-23 09:24:45 +0100926#if defined(MBEDTLS_USE_PSA_CRYPTO)
927 transform->psa_mac_enc = MBEDTLS_SVC_KEY_ID_INIT;
928 transform->psa_mac_dec = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrongcf8841a2022-02-24 11:17:45 +0100929#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100930 mbedtls_md_init(&transform->md_ctx_enc);
931 mbedtls_md_init(&transform->md_ctx_dec);
Hanno Beckerd56ed242018-01-03 15:32:51 +0000932#endif
Neil Armstrongcf8841a2022-02-24 11:17:45 +0100933#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200934}
935
Gilles Peskine449bd832023-01-11 14:50:10 +0100936void mbedtls_ssl_session_init(mbedtls_ssl_session *session)
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200937{
Gilles Peskine449bd832023-01-11 14:50:10 +0100938 memset(session, 0, sizeof(mbedtls_ssl_session));
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200939}
940
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200941MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100942static int ssl_handshake_init(mbedtls_ssl_context *ssl)
Paul Bakker48916f92012-09-16 19:57:18 +0000943{
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200944 /* Clear old handshake information if present */
Jerry Yu2e199812022-12-01 18:57:19 +0800945#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +0100946 if (ssl->transform_negotiate) {
947 mbedtls_ssl_transform_free(ssl->transform_negotiate);
948 }
Jerry Yu2e199812022-12-01 18:57:19 +0800949#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100950 if (ssl->session_negotiate) {
951 mbedtls_ssl_session_free(ssl->session_negotiate);
952 }
953 if (ssl->handshake) {
954 mbedtls_ssl_handshake_free(ssl);
955 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200956
Jerry Yu2e199812022-12-01 18:57:19 +0800957#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200958 /*
959 * Either the pointers are now NULL or cleared properly and can be freed.
960 * Now allocate missing structures.
961 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100962 if (ssl->transform_negotiate == NULL) {
963 ssl->transform_negotiate = mbedtls_calloc(1, sizeof(mbedtls_ssl_transform));
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200964 }
Jerry Yu2e199812022-12-01 18:57:19 +0800965#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker48916f92012-09-16 19:57:18 +0000966
Gilles Peskine449bd832023-01-11 14:50:10 +0100967 if (ssl->session_negotiate == NULL) {
968 ssl->session_negotiate = mbedtls_calloc(1, sizeof(mbedtls_ssl_session));
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200969 }
Paul Bakker48916f92012-09-16 19:57:18 +0000970
Gilles Peskine449bd832023-01-11 14:50:10 +0100971 if (ssl->handshake == NULL) {
972 ssl->handshake = mbedtls_calloc(1, sizeof(mbedtls_ssl_handshake_params));
Paul Bakkerb9cfaa02013-10-11 18:58:55 +0200973 }
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500974#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
975 /* If the buffers are too small - reallocate */
Andrzej Kurek8ea68722020-04-03 06:40:47 -0400976
Gilles Peskine449bd832023-01-11 14:50:10 +0100977 handle_buffer_resizing(ssl, 0, MBEDTLS_SSL_IN_BUFFER_LEN,
978 MBEDTLS_SSL_OUT_BUFFER_LEN);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500979#endif
Paul Bakker48916f92012-09-16 19:57:18 +0000980
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200981 /* All pointers should exist and can be directly freed without issue */
Gilles Peskine449bd832023-01-11 14:50:10 +0100982 if (ssl->handshake == NULL ||
Jerry Yu2e199812022-12-01 18:57:19 +0800983#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker48916f92012-09-16 19:57:18 +0000984 ssl->transform_negotiate == NULL ||
Jerry Yu2e199812022-12-01 18:57:19 +0800985#endif
Gilles Peskine449bd832023-01-11 14:50:10 +0100986 ssl->session_negotiate == NULL) {
987 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc() of ssl sub-contexts failed"));
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200988
Gilles Peskine449bd832023-01-11 14:50:10 +0100989 mbedtls_free(ssl->handshake);
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200990 ssl->handshake = NULL;
Jerry Yu2e199812022-12-01 18:57:19 +0800991
992#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +0100993 mbedtls_free(ssl->transform_negotiate);
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200994 ssl->transform_negotiate = NULL;
Jerry Yu2e199812022-12-01 18:57:19 +0800995#endif
996
Gilles Peskine449bd832023-01-11 14:50:10 +0100997 mbedtls_free(ssl->session_negotiate);
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200998 ssl->session_negotiate = NULL;
999
Gilles Peskine449bd832023-01-11 14:50:10 +01001000 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Paul Bakker48916f92012-09-16 19:57:18 +00001001 }
1002
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001003 /* Initialize structures */
Gilles Peskine449bd832023-01-11 14:50:10 +01001004 mbedtls_ssl_session_init(ssl->session_negotiate);
1005 ssl_handshake_params_init(ssl->handshake);
Paul Bakker968afaa2014-07-09 11:09:24 +02001006
Jerry Yu2e199812022-12-01 18:57:19 +08001007#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01001008 mbedtls_ssl_transform_init(ssl->transform_negotiate);
Jerry Yu2e199812022-12-01 18:57:19 +08001009#endif
1010
Jerry Yud0766ec2022-09-22 10:46:57 +08001011#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
1012 defined(MBEDTLS_SSL_SRV_C) && \
1013 defined(MBEDTLS_SSL_SESSION_TICKETS)
1014 ssl->handshake->new_session_tickets_count =
Gilles Peskine449bd832023-01-11 14:50:10 +01001015 ssl->conf->new_session_tickets_count;
Jerry Yud0766ec2022-09-22 10:46:57 +08001016#endif
1017
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001018#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001019 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001020 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001021
Gilles Peskine449bd832023-01-11 14:50:10 +01001022 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001023 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Gilles Peskine449bd832023-01-11 14:50:10 +01001024 } else {
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001025 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Gilles Peskine449bd832023-01-11 14:50:10 +01001026 }
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001027
Gilles Peskine449bd832023-01-11 14:50:10 +01001028 mbedtls_ssl_set_timer(ssl, 0);
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001029 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001030#endif
1031
Brett Warrene0edc842021-08-17 09:53:13 +01001032/*
1033 * curve_list is translated to IANA TLS group identifiers here because
1034 * mbedtls_ssl_conf_curves returns void and so can't return
1035 * any error codes.
1036 */
1037#if defined(MBEDTLS_ECP_C)
1038#if !defined(MBEDTLS_DEPRECATED_REMOVED)
1039 /* Heap allocate and translate curve_list from internal to IANA group ids */
Gilles Peskine449bd832023-01-11 14:50:10 +01001040 if (ssl->conf->curve_list != NULL) {
Brett Warrene0edc842021-08-17 09:53:13 +01001041 size_t length;
1042 const mbedtls_ecp_group_id *curve_list = ssl->conf->curve_list;
1043
Gilles Peskine449bd832023-01-11 14:50:10 +01001044 for (length = 0; (curve_list[length] != MBEDTLS_ECP_DP_NONE) &&
1045 (length < MBEDTLS_ECP_DP_MAX); length++) {
1046 }
Brett Warrene0edc842021-08-17 09:53:13 +01001047
1048 /* Leave room for zero termination */
Gilles Peskine449bd832023-01-11 14:50:10 +01001049 uint16_t *group_list = mbedtls_calloc(length + 1, sizeof(uint16_t));
1050 if (group_list == NULL) {
1051 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
1052 }
Brett Warrene0edc842021-08-17 09:53:13 +01001053
Gilles Peskine449bd832023-01-11 14:50:10 +01001054 for (size_t i = 0; i < length; i++) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01001055 uint16_t tls_id = mbedtls_ssl_get_tls_id_from_ecp_group_id(
Gilles Peskine449bd832023-01-11 14:50:10 +01001056 curve_list[i]);
1057 if (tls_id == 0) {
1058 mbedtls_free(group_list);
1059 return MBEDTLS_ERR_SSL_BAD_CONFIG;
Brett Warrene0edc842021-08-17 09:53:13 +01001060 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01001061 group_list[i] = tls_id;
Brett Warrene0edc842021-08-17 09:53:13 +01001062 }
1063
1064 group_list[length] = 0;
1065
1066 ssl->handshake->group_list = group_list;
1067 ssl->handshake->group_list_heap_allocated = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001068 } else {
Brett Warrene0edc842021-08-17 09:53:13 +01001069 ssl->handshake->group_list = ssl->conf->group_list;
1070 ssl->handshake->group_list_heap_allocated = 0;
1071 }
1072#endif /* MBEDTLS_DEPRECATED_REMOVED */
1073#endif /* MBEDTLS_ECP_C */
1074
Ronald Crone68ab4f2022-10-05 12:46:29 +02001075#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +08001076#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Jerry Yua69269a2022-01-17 21:06:01 +08001077#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yu713013f2022-01-17 18:16:35 +08001078 /* Heap allocate and translate sig_hashes from internal hash identifiers to
1079 signature algorithms IANA identifiers. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001080 if (mbedtls_ssl_conf_is_tls12_only(ssl->conf) &&
1081 ssl->conf->sig_hashes != NULL) {
Jerry Yuf017ee42022-01-12 15:49:48 +08001082 const int *md;
1083 const int *sig_hashes = ssl->conf->sig_hashes;
Jerry Yub476a442022-01-21 18:14:45 +08001084 size_t sig_algs_len = 0;
Jerry Yuf017ee42022-01-12 15:49:48 +08001085 uint16_t *p;
1086
Jerry Yub476a442022-01-21 18:14:45 +08001087#if defined(static_assert)
Gilles Peskine449bd832023-01-11 14:50:10 +01001088 static_assert(MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN
1089 <= (SIZE_MAX - (2 * sizeof(uint16_t))),
1090 "MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN too big");
Jerry Yua68dca22022-01-20 16:28:27 +08001091#endif
1092
Gilles Peskine449bd832023-01-11 14:50:10 +01001093 for (md = sig_hashes; *md != MBEDTLS_MD_NONE; md++) {
1094 if (mbedtls_ssl_hash_from_md_alg(*md) == MBEDTLS_SSL_HASH_NONE) {
Jerry Yuf017ee42022-01-12 15:49:48 +08001095 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001096 }
Jerry Yub476a442022-01-21 18:14:45 +08001097#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001098 sig_algs_len += sizeof(uint16_t);
Jerry Yub476a442022-01-21 18:14:45 +08001099#endif
Jerry Yua68dca22022-01-20 16:28:27 +08001100
Jerry Yub476a442022-01-21 18:14:45 +08001101#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001102 sig_algs_len += sizeof(uint16_t);
Jerry Yub476a442022-01-21 18:14:45 +08001103#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001104 if (sig_algs_len > MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN) {
1105 return MBEDTLS_ERR_SSL_BAD_CONFIG;
1106 }
Jerry Yuf017ee42022-01-12 15:49:48 +08001107 }
1108
Gilles Peskine449bd832023-01-11 14:50:10 +01001109 if (sig_algs_len < MBEDTLS_SSL_MIN_SIG_ALG_LIST_LEN) {
1110 return MBEDTLS_ERR_SSL_BAD_CONFIG;
1111 }
Jerry Yuf017ee42022-01-12 15:49:48 +08001112
Gilles Peskine449bd832023-01-11 14:50:10 +01001113 ssl->handshake->sig_algs = mbedtls_calloc(1, sig_algs_len +
1114 sizeof(uint16_t));
1115 if (ssl->handshake->sig_algs == NULL) {
1116 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
1117 }
Jerry Yuf017ee42022-01-12 15:49:48 +08001118
Gilles Peskine449bd832023-01-11 14:50:10 +01001119 p = (uint16_t *) ssl->handshake->sig_algs;
1120 for (md = sig_hashes; *md != MBEDTLS_MD_NONE; md++) {
1121 unsigned char hash = mbedtls_ssl_hash_from_md_alg(*md);
1122 if (hash == MBEDTLS_SSL_HASH_NONE) {
Jerry Yuf017ee42022-01-12 15:49:48 +08001123 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001124 }
Jerry Yu6106fdc2022-01-12 16:36:14 +08001125#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001126 *p = ((hash << 8) | MBEDTLS_SSL_SIG_ECDSA);
Jerry Yuf017ee42022-01-12 15:49:48 +08001127 p++;
Jerry Yu6106fdc2022-01-12 16:36:14 +08001128#endif
1129#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001130 *p = ((hash << 8) | MBEDTLS_SSL_SIG_RSA);
Jerry Yuf017ee42022-01-12 15:49:48 +08001131 p++;
Jerry Yu6106fdc2022-01-12 16:36:14 +08001132#endif
Jerry Yuf017ee42022-01-12 15:49:48 +08001133 }
Gabor Mezei15b95a62022-05-09 16:37:58 +02001134 *p = MBEDTLS_TLS_SIG_NONE;
Jerry Yuf017ee42022-01-12 15:49:48 +08001135 ssl->handshake->sig_algs_heap_allocated = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001136 } else
Jerry Yua69269a2022-01-17 21:06:01 +08001137#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Jerry Yuf017ee42022-01-12 15:49:48 +08001138 {
Jerry Yuf017ee42022-01-12 15:49:48 +08001139 ssl->handshake->sig_algs_heap_allocated = 0;
1140 }
Jerry Yucc539102022-06-27 16:27:35 +08001141#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Ronald Crone68ab4f2022-10-05 12:46:29 +02001142#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Gilles Peskine449bd832023-01-11 14:50:10 +01001143 return 0;
Paul Bakker48916f92012-09-16 19:57:18 +00001144}
1145
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001146#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001147/* Dummy cookie callbacks for defaults */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001148MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001149static int ssl_cookie_write_dummy(void *ctx,
1150 unsigned char **p, unsigned char *end,
1151 const unsigned char *cli_id, size_t cli_id_len)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001152{
1153 ((void) ctx);
1154 ((void) p);
1155 ((void) end);
1156 ((void) cli_id);
1157 ((void) cli_id_len);
1158
Gilles Peskine449bd832023-01-11 14:50:10 +01001159 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001160}
1161
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001162MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001163static int ssl_cookie_check_dummy(void *ctx,
1164 const unsigned char *cookie, size_t cookie_len,
1165 const unsigned char *cli_id, size_t cli_id_len)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001166{
1167 ((void) ctx);
1168 ((void) cookie);
1169 ((void) cookie_len);
1170 ((void) cli_id);
1171 ((void) cli_id_len);
1172
Gilles Peskine449bd832023-01-11 14:50:10 +01001173 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001174}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001175#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001176
Paul Bakker5121ce52009-01-03 21:22:43 +00001177/*
1178 * Initialize an SSL context
1179 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001180void mbedtls_ssl_init(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001181{
Gilles Peskine449bd832023-01-11 14:50:10 +01001182 memset(ssl, 0, sizeof(mbedtls_ssl_context));
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001183}
1184
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001185MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001186static int ssl_conf_version_check(const mbedtls_ssl_context *ssl)
Jerry Yu60835a82021-08-04 10:13:52 +08001187{
Ronald Cron086ee0b2022-03-15 15:18:51 +01001188 const mbedtls_ssl_config *conf = ssl->conf;
1189
Ronald Cron6f135e12021-12-08 16:57:54 +01001190#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001191 if (mbedtls_ssl_conf_is_tls13_only(conf)) {
1192 if (conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
1193 MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS 1.3 is not yet supported."));
1194 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQianed582dd2022-04-13 08:21:05 +00001195 }
1196
Gilles Peskine449bd832023-01-11 14:50:10 +01001197 MBEDTLS_SSL_DEBUG_MSG(4, ("The SSL configuration is tls13 only."));
1198 return 0;
Jerry Yu60835a82021-08-04 10:13:52 +08001199 }
1200#endif
1201
1202#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01001203 if (mbedtls_ssl_conf_is_tls12_only(conf)) {
1204 MBEDTLS_SSL_DEBUG_MSG(4, ("The SSL configuration is tls12 only."));
1205 return 0;
Jerry Yu60835a82021-08-04 10:13:52 +08001206 }
1207#endif
1208
Ronald Cron6f135e12021-12-08 16:57:54 +01001209#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001210 if (mbedtls_ssl_conf_is_hybrid_tls12_tls13(conf)) {
1211 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
1212 MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS not yet supported in Hybrid TLS 1.3 + TLS 1.2"));
1213 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQianed582dd2022-04-13 08:21:05 +00001214 }
1215
Gilles Peskine449bd832023-01-11 14:50:10 +01001216 if (conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
1217 MBEDTLS_SSL_DEBUG_MSG(1, ("TLS 1.3 server is not supported yet."));
1218 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQian8f9dfe42022-04-15 02:52:39 +00001219 }
1220
1221
Gilles Peskine449bd832023-01-11 14:50:10 +01001222 MBEDTLS_SSL_DEBUG_MSG(4, ("The SSL configuration is TLS 1.3 or TLS 1.2."));
1223 return 0;
Jerry Yu60835a82021-08-04 10:13:52 +08001224 }
1225#endif
1226
Gilles Peskine449bd832023-01-11 14:50:10 +01001227 MBEDTLS_SSL_DEBUG_MSG(1, ("The SSL configuration is invalid."));
1228 return MBEDTLS_ERR_SSL_BAD_CONFIG;
Jerry Yu60835a82021-08-04 10:13:52 +08001229}
1230
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001231MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu60835a82021-08-04 10:13:52 +08001232static int ssl_conf_check(const mbedtls_ssl_context *ssl)
1233{
1234 int ret;
Gilles Peskine449bd832023-01-11 14:50:10 +01001235 ret = ssl_conf_version_check(ssl);
1236 if (ret != 0) {
1237 return ret;
1238 }
Jerry Yu60835a82021-08-04 10:13:52 +08001239
Jerry Yudef7ae42022-10-30 14:13:19 +08001240#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1241 /* RFC 8446 section 4.4.3
1242 *
1243 * If the verification fails, the receiver MUST terminate the handshake with
1244 * a "decrypt_error" alert.
1245 *
1246 * If the client is configured as TLS 1.3 only with optional verify, return
1247 * bad config.
1248 *
1249 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001250 if (mbedtls_ssl_conf_tls13_ephemeral_enabled(
1251 (mbedtls_ssl_context *) ssl) &&
Jerry Yudef7ae42022-10-30 14:13:19 +08001252 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
1253 ssl->conf->max_tls_version == MBEDTLS_SSL_VERSION_TLS1_3 &&
1254 ssl->conf->min_tls_version == MBEDTLS_SSL_VERSION_TLS1_3 &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001255 ssl->conf->authmode == MBEDTLS_SSL_VERIFY_OPTIONAL) {
Jerry Yudef7ae42022-10-30 14:13:19 +08001256 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +01001257 1, ("Optional verify auth mode "
1258 "is not available for TLS 1.3 client"));
1259 return MBEDTLS_ERR_SSL_BAD_CONFIG;
Jerry Yudef7ae42022-10-30 14:13:19 +08001260 }
1261#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
1262
Jerry Yu60835a82021-08-04 10:13:52 +08001263 /* Space for further checks */
1264
Gilles Peskine449bd832023-01-11 14:50:10 +01001265 return 0;
Jerry Yu60835a82021-08-04 10:13:52 +08001266}
1267
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001268/*
1269 * Setup an SSL context
1270 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01001271
Gilles Peskine449bd832023-01-11 14:50:10 +01001272int mbedtls_ssl_setup(mbedtls_ssl_context *ssl,
1273 const mbedtls_ssl_config *conf)
Paul Bakker5121ce52009-01-03 21:22:43 +00001274{
Janos Follath865b3eb2019-12-16 11:46:15 +00001275 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Darryl Greenb33cc762019-11-28 14:29:44 +00001276 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1277 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00001278
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001279 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00001280
Gilles Peskine449bd832023-01-11 14:50:10 +01001281 if ((ret = ssl_conf_check(ssl)) != 0) {
1282 return ret;
1283 }
Jerry Yu60835a82021-08-04 10:13:52 +08001284
Paul Bakker62f2dee2012-09-28 07:31:51 +00001285 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001286 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00001287 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02001288
1289 /* Set to NULL in case of an error condition */
1290 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02001291
Darryl Greenb33cc762019-11-28 14:29:44 +00001292#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1293 ssl->in_buf_len = in_buf_len;
1294#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001295 ssl->in_buf = mbedtls_calloc(1, in_buf_len);
1296 if (ssl->in_buf == NULL) {
1297 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", in_buf_len));
k-stachowiak9f7798e2018-07-31 16:52:32 +02001298 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02001299 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10001300 }
1301
Darryl Greenb33cc762019-11-28 14:29:44 +00001302#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1303 ssl->out_buf_len = out_buf_len;
1304#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001305 ssl->out_buf = mbedtls_calloc(1, out_buf_len);
1306 if (ssl->out_buf == NULL) {
1307 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", out_buf_len));
k-stachowiak9f7798e2018-07-31 16:52:32 +02001308 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02001309 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00001310 }
1311
Gilles Peskine449bd832023-01-11 14:50:10 +01001312 mbedtls_ssl_reset_in_out_pointers(ssl);
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02001313
Johan Pascalb62bb512015-12-03 21:56:45 +01001314#if defined(MBEDTLS_SSL_DTLS_SRTP)
Gilles Peskine449bd832023-01-11 14:50:10 +01001315 memset(&ssl->dtls_srtp_info, 0, sizeof(ssl->dtls_srtp_info));
Johan Pascalb62bb512015-12-03 21:56:45 +01001316#endif
1317
Gilles Peskine449bd832023-01-11 14:50:10 +01001318 if ((ret = ssl_handshake_init(ssl)) != 0) {
k-stachowiaka47911c2018-07-04 17:41:58 +02001319 goto error;
Gilles Peskine449bd832023-01-11 14:50:10 +01001320 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001321
Gilles Peskine449bd832023-01-11 14:50:10 +01001322 return 0;
k-stachowiaka47911c2018-07-04 17:41:58 +02001323
1324error:
Gilles Peskine449bd832023-01-11 14:50:10 +01001325 mbedtls_free(ssl->in_buf);
1326 mbedtls_free(ssl->out_buf);
k-stachowiaka47911c2018-07-04 17:41:58 +02001327
1328 ssl->conf = NULL;
1329
Darryl Greenb33cc762019-11-28 14:29:44 +00001330#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1331 ssl->in_buf_len = 0;
1332 ssl->out_buf_len = 0;
1333#endif
k-stachowiaka47911c2018-07-04 17:41:58 +02001334 ssl->in_buf = NULL;
1335 ssl->out_buf = NULL;
1336
1337 ssl->in_hdr = NULL;
1338 ssl->in_ctr = NULL;
1339 ssl->in_len = NULL;
1340 ssl->in_iv = NULL;
1341 ssl->in_msg = NULL;
1342
1343 ssl->out_hdr = NULL;
1344 ssl->out_ctr = NULL;
1345 ssl->out_len = NULL;
1346 ssl->out_iv = NULL;
1347 ssl->out_msg = NULL;
1348
Gilles Peskine449bd832023-01-11 14:50:10 +01001349 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001350}
1351
1352/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00001353 * Reset an initialized and used SSL context for re-use while retaining
1354 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001355 *
1356 * If partial is non-zero, keep data in the input buffer and client ID.
1357 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00001358 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001359void mbedtls_ssl_session_reset_msg_layer(mbedtls_ssl_context *ssl,
1360 int partial)
Paul Bakker7eb013f2011-10-06 12:37:39 +00001361{
Darryl Greenb33cc762019-11-28 14:29:44 +00001362#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1363 size_t in_buf_len = ssl->in_buf_len;
1364 size_t out_buf_len = ssl->out_buf_len;
1365#else
1366 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1367 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
1368#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001369
Hanno Beckerb0302c42021-08-03 09:39:42 +01001370#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || !defined(MBEDTLS_SSL_SRV_C)
1371 partial = 0;
Hanno Becker7e772132018-08-10 12:38:21 +01001372#endif
1373
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001374 /* Cancel any possibly running timer */
Gilles Peskine449bd832023-01-11 14:50:10 +01001375 mbedtls_ssl_set_timer(ssl, 0);
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001376
Gilles Peskine449bd832023-01-11 14:50:10 +01001377 mbedtls_ssl_reset_in_out_pointers(ssl);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001378
1379 /* Reset incoming message parsing */
1380 ssl->in_offt = NULL;
1381 ssl->nb_zero = 0;
1382 ssl->in_msgtype = 0;
1383 ssl->in_msglen = 0;
1384 ssl->in_hslen = 0;
1385 ssl->keep_current_message = 0;
1386 ssl->transform_in = NULL;
1387
1388#if defined(MBEDTLS_SSL_PROTO_DTLS)
1389 ssl->next_record_offset = 0;
1390 ssl->in_epoch = 0;
1391#endif
1392
1393 /* Keep current datagram if partial == 1 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001394 if (partial == 0) {
Hanno Beckerb0302c42021-08-03 09:39:42 +01001395 ssl->in_left = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001396 memset(ssl->in_buf, 0, in_buf_len);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001397 }
1398
Ronald Cronad8c17b2022-06-10 17:18:09 +02001399 ssl->send_alert = 0;
1400
Hanno Beckerb0302c42021-08-03 09:39:42 +01001401 /* Reset outgoing message writing */
1402 ssl->out_msgtype = 0;
1403 ssl->out_msglen = 0;
1404 ssl->out_left = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001405 memset(ssl->out_buf, 0, out_buf_len);
1406 memset(ssl->cur_out_ctr, 0, sizeof(ssl->cur_out_ctr));
Hanno Beckerb0302c42021-08-03 09:39:42 +01001407 ssl->transform_out = NULL;
1408
1409#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine449bd832023-01-11 14:50:10 +01001410 mbedtls_ssl_dtls_replay_reset(ssl);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001411#endif
1412
XiaokangQian2b01dc32022-01-21 02:53:13 +00001413#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01001414 if (ssl->transform) {
1415 mbedtls_ssl_transform_free(ssl->transform);
1416 mbedtls_free(ssl->transform);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001417 ssl->transform = NULL;
1418 }
XiaokangQian2b01dc32022-01-21 02:53:13 +00001419#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1420
XiaokangQian2b01dc32022-01-21 02:53:13 +00001421#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001422 mbedtls_ssl_transform_free(ssl->transform_application);
1423 mbedtls_free(ssl->transform_application);
XiaokangQian2b01dc32022-01-21 02:53:13 +00001424 ssl->transform_application = NULL;
1425
Gilles Peskine449bd832023-01-11 14:50:10 +01001426 if (ssl->handshake != NULL) {
Jerry Yu3d9b5902022-11-04 14:07:25 +08001427#if defined(MBEDTLS_SSL_EARLY_DATA)
Gilles Peskine449bd832023-01-11 14:50:10 +01001428 mbedtls_ssl_transform_free(ssl->handshake->transform_earlydata);
1429 mbedtls_free(ssl->handshake->transform_earlydata);
XiaokangQian2b01dc32022-01-21 02:53:13 +00001430 ssl->handshake->transform_earlydata = NULL;
Jerry Yu3d9b5902022-11-04 14:07:25 +08001431#endif
XiaokangQian2b01dc32022-01-21 02:53:13 +00001432
Gilles Peskine449bd832023-01-11 14:50:10 +01001433 mbedtls_ssl_transform_free(ssl->handshake->transform_handshake);
1434 mbedtls_free(ssl->handshake->transform_handshake);
XiaokangQian2b01dc32022-01-21 02:53:13 +00001435 ssl->handshake->transform_handshake = NULL;
1436 }
1437
XiaokangQian2b01dc32022-01-21 02:53:13 +00001438#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerb0302c42021-08-03 09:39:42 +01001439}
1440
Gilles Peskine449bd832023-01-11 14:50:10 +01001441int mbedtls_ssl_session_reset_int(mbedtls_ssl_context *ssl, int partial)
Hanno Beckerb0302c42021-08-03 09:39:42 +01001442{
1443 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1444
1445 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
1446
Gilles Peskine449bd832023-01-11 14:50:10 +01001447 mbedtls_ssl_session_reset_msg_layer(ssl, partial);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001448
1449 /* Reset renegotiation state */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001450#if defined(MBEDTLS_SSL_RENEGOTIATION)
1451 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001452 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00001453
1454 ssl->verify_data_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001455 memset(ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN);
1456 memset(ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN);
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001457#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001458 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00001459
Hanno Beckerb0302c42021-08-03 09:39:42 +01001460 ssl->session_in = NULL;
Hanno Becker78640902018-08-13 16:35:15 +01001461 ssl->session_out = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001462 if (ssl->session) {
1463 mbedtls_ssl_session_free(ssl->session);
1464 mbedtls_free(ssl->session);
Paul Bakkerc0463502013-02-14 11:19:38 +01001465 ssl->session = NULL;
1466 }
1467
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001468#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02001469 ssl->alpn_chosen = NULL;
1470#endif
1471
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001472#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
David Horstmann3b2276a2022-10-06 14:49:08 +01001473 int free_cli_id = 1;
Hanno Becker4ccbf062018-08-10 11:20:38 +01001474#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Gilles Peskine449bd832023-01-11 14:50:10 +01001475 free_cli_id = (partial == 0);
Hanno Becker4ccbf062018-08-10 11:20:38 +01001476#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001477 if (free_cli_id) {
1478 mbedtls_free(ssl->cli_id);
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001479 ssl->cli_id = NULL;
1480 ssl->cli_id_len = 0;
1481 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02001482#endif
1483
Gilles Peskine449bd832023-01-11 14:50:10 +01001484 if ((ret = ssl_handshake_init(ssl)) != 0) {
1485 return ret;
1486 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00001487
Gilles Peskine449bd832023-01-11 14:50:10 +01001488 return 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00001489}
1490
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02001491/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001492 * Reset an initialized and used SSL context for re-use while retaining
1493 * all application-set variables, function pointers and data.
1494 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001495int mbedtls_ssl_session_reset(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001496{
Gilles Peskine449bd832023-01-11 14:50:10 +01001497 return mbedtls_ssl_session_reset_int(ssl, 0);
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001498}
1499
1500/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001501 * SSL set accessors
1502 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001503void mbedtls_ssl_conf_endpoint(mbedtls_ssl_config *conf, int endpoint)
Paul Bakker5121ce52009-01-03 21:22:43 +00001504{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001505 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00001506}
1507
Gilles Peskine449bd832023-01-11 14:50:10 +01001508void mbedtls_ssl_conf_transport(mbedtls_ssl_config *conf, int transport)
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001509{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001510 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001511}
1512
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001513#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine449bd832023-01-11 14:50:10 +01001514void mbedtls_ssl_conf_dtls_anti_replay(mbedtls_ssl_config *conf, char mode)
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001515{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001516 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001517}
1518#endif
1519
Gilles Peskine449bd832023-01-11 14:50:10 +01001520void mbedtls_ssl_conf_dtls_badmac_limit(mbedtls_ssl_config *conf, unsigned limit)
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001521{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001522 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001523}
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001524
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001525#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01001526
Gilles Peskine449bd832023-01-11 14:50:10 +01001527void mbedtls_ssl_set_datagram_packing(mbedtls_ssl_context *ssl,
1528 unsigned allow_packing)
Hanno Becker04da1892018-08-14 13:22:10 +01001529{
1530 ssl->disable_datagram_packing = !allow_packing;
1531}
1532
Gilles Peskine449bd832023-01-11 14:50:10 +01001533void mbedtls_ssl_conf_handshake_timeout(mbedtls_ssl_config *conf,
1534 uint32_t min, uint32_t max)
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001535{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001536 conf->hs_timeout_min = min;
1537 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001538}
1539#endif
1540
Gilles Peskine449bd832023-01-11 14:50:10 +01001541void mbedtls_ssl_conf_authmode(mbedtls_ssl_config *conf, int authmode)
Paul Bakker5121ce52009-01-03 21:22:43 +00001542{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001543 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00001544}
1545
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001546#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001547void mbedtls_ssl_conf_verify(mbedtls_ssl_config *conf,
1548 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
1549 void *p_vrfy)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001550{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001551 conf->f_vrfy = f_vrfy;
1552 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001553}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001554#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001555
Gilles Peskine449bd832023-01-11 14:50:10 +01001556void mbedtls_ssl_conf_rng(mbedtls_ssl_config *conf,
1557 int (*f_rng)(void *, unsigned char *, size_t),
1558 void *p_rng)
Paul Bakker5121ce52009-01-03 21:22:43 +00001559{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001560 conf->f_rng = f_rng;
1561 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00001562}
1563
Gilles Peskine449bd832023-01-11 14:50:10 +01001564void mbedtls_ssl_conf_dbg(mbedtls_ssl_config *conf,
1565 void (*f_dbg)(void *, int, const char *, int, const char *),
1566 void *p_dbg)
Paul Bakker5121ce52009-01-03 21:22:43 +00001567{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001568 conf->f_dbg = f_dbg;
1569 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00001570}
1571
Gilles Peskine449bd832023-01-11 14:50:10 +01001572void mbedtls_ssl_set_bio(mbedtls_ssl_context *ssl,
1573 void *p_bio,
1574 mbedtls_ssl_send_t *f_send,
1575 mbedtls_ssl_recv_t *f_recv,
1576 mbedtls_ssl_recv_timeout_t *f_recv_timeout)
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001577{
1578 ssl->p_bio = p_bio;
1579 ssl->f_send = f_send;
1580 ssl->f_recv = f_recv;
1581 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001582}
1583
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02001584#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001585void mbedtls_ssl_set_mtu(mbedtls_ssl_context *ssl, uint16_t mtu)
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02001586{
1587 ssl->mtu = mtu;
1588}
1589#endif
1590
Gilles Peskine449bd832023-01-11 14:50:10 +01001591void mbedtls_ssl_conf_read_timeout(mbedtls_ssl_config *conf, uint32_t timeout)
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001592{
1593 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001594}
1595
Gilles Peskine449bd832023-01-11 14:50:10 +01001596void mbedtls_ssl_set_timer_cb(mbedtls_ssl_context *ssl,
1597 void *p_timer,
1598 mbedtls_ssl_set_timer_t *f_set_timer,
1599 mbedtls_ssl_get_timer_t *f_get_timer)
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001600{
1601 ssl->p_timer = p_timer;
1602 ssl->f_set_timer = f_set_timer;
1603 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001604
1605 /* Make sure we start with no timer running */
Gilles Peskine449bd832023-01-11 14:50:10 +01001606 mbedtls_ssl_set_timer(ssl, 0);
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001607}
1608
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001609#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001610void mbedtls_ssl_conf_session_cache(mbedtls_ssl_config *conf,
1611 void *p_cache,
1612 mbedtls_ssl_cache_get_t *f_get_cache,
1613 mbedtls_ssl_cache_set_t *f_set_cache)
Paul Bakker5121ce52009-01-03 21:22:43 +00001614{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01001615 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001616 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001617 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00001618}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001619#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001620
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001621#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001622int mbedtls_ssl_set_session(mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session)
Paul Bakker5121ce52009-01-03 21:22:43 +00001623{
Janos Follath865b3eb2019-12-16 11:46:15 +00001624 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001625
Gilles Peskine449bd832023-01-11 14:50:10 +01001626 if (ssl == NULL ||
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001627 session == NULL ||
1628 ssl->session_negotiate == NULL ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001629 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT) {
1630 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001631 }
1632
Gilles Peskine449bd832023-01-11 14:50:10 +01001633 if (ssl->handshake->resume == 1) {
1634 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
1635 }
Hanno Beckere810bbc2021-05-14 16:01:05 +01001636
Jerry Yu21092062022-10-10 21:21:31 +08001637#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001638 if (session->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
Jerry Yu21092062022-10-10 21:21:31 +08001639 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01001640 mbedtls_ssl_ciphersuite_from_id(session->ciphersuite);
Jerry Yu21092062022-10-10 21:21:31 +08001641
Gilles Peskine449bd832023-01-11 14:50:10 +01001642 if (mbedtls_ssl_validate_ciphersuite(
Jerry Yu21092062022-10-10 21:21:31 +08001643 ssl, ciphersuite_info, MBEDTLS_SSL_VERSION_TLS1_3,
Gilles Peskine449bd832023-01-11 14:50:10 +01001644 MBEDTLS_SSL_VERSION_TLS1_3) != 0) {
1645 MBEDTLS_SSL_DEBUG_MSG(4, ("%d is not a valid TLS 1.3 ciphersuite.",
1646 session->ciphersuite));
1647 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jerry Yu21092062022-10-10 21:21:31 +08001648 }
Jerry Yu40afab62022-10-08 10:42:13 +08001649 }
Jerry Yu21092062022-10-10 21:21:31 +08001650#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu40afab62022-10-08 10:42:13 +08001651
Gilles Peskine449bd832023-01-11 14:50:10 +01001652 if ((ret = mbedtls_ssl_session_copy(ssl->session_negotiate,
1653 session)) != 0) {
1654 return ret;
1655 }
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001656
Paul Bakker0a597072012-09-25 21:55:46 +00001657 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001658
Gilles Peskine449bd832023-01-11 14:50:10 +01001659 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001660}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001661#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001662
Gilles Peskine449bd832023-01-11 14:50:10 +01001663void mbedtls_ssl_conf_ciphersuites(mbedtls_ssl_config *conf,
1664 const int *ciphersuites)
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001665{
Hanno Beckerd60b6c62021-04-29 12:04:11 +01001666 conf->ciphersuite_list = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00001667}
1668
Ronald Cron6f135e12021-12-08 16:57:54 +01001669#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001670void mbedtls_ssl_conf_tls13_key_exchange_modes(mbedtls_ssl_config *conf,
1671 const int kex_modes)
Hanno Becker71f1ed62021-07-24 06:01:47 +01001672{
Xiaofei Bai746f9482021-11-12 08:53:56 +00001673 conf->tls13_kex_modes = kex_modes & MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Hanno Becker71f1ed62021-07-24 06:01:47 +01001674}
Xiaokang Qian72de95d2022-10-25 02:54:33 +00001675
1676#if defined(MBEDTLS_SSL_EARLY_DATA)
Gilles Peskine449bd832023-01-11 14:50:10 +01001677void mbedtls_ssl_tls13_conf_early_data(mbedtls_ssl_config *conf,
1678 int early_data_enabled)
Xiaokang Qian72de95d2022-10-25 02:54:33 +00001679{
1680 conf->early_data_enabled = early_data_enabled;
1681}
Jerry Yucc4e0072022-11-22 17:22:22 +08001682
1683#if defined(MBEDTLS_SSL_SRV_C)
1684void mbedtls_ssl_tls13_conf_max_early_data_size(
Gilles Peskine449bd832023-01-11 14:50:10 +01001685 mbedtls_ssl_config *conf, uint32_t max_early_data_size)
Jerry Yucc4e0072022-11-22 17:22:22 +08001686{
Jerry Yu39da9852022-12-06 16:58:36 +08001687 conf->max_early_data_size = max_early_data_size;
Jerry Yucc4e0072022-11-22 17:22:22 +08001688}
1689#endif /* MBEDTLS_SSL_SRV_C */
1690
Xiaokang Qian72de95d2022-10-25 02:54:33 +00001691#endif /* MBEDTLS_SSL_EARLY_DATA */
Ronald Cron6f135e12021-12-08 16:57:54 +01001692#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01001693
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001694#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001695void mbedtls_ssl_conf_cert_profile(mbedtls_ssl_config *conf,
1696 const mbedtls_x509_crt_profile *profile)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02001697{
1698 conf->cert_profile = profile;
1699}
1700
Gilles Peskine449bd832023-01-11 14:50:10 +01001701static void ssl_key_cert_free(mbedtls_ssl_key_cert *key_cert)
Glenn Strauss36872db2022-01-22 05:06:31 -05001702{
1703 mbedtls_ssl_key_cert *cur = key_cert, *next;
1704
Gilles Peskine449bd832023-01-11 14:50:10 +01001705 while (cur != NULL) {
Glenn Strauss36872db2022-01-22 05:06:31 -05001706 next = cur->next;
Gilles Peskine449bd832023-01-11 14:50:10 +01001707 mbedtls_free(cur);
Glenn Strauss36872db2022-01-22 05:06:31 -05001708 cur = next;
1709 }
1710}
1711
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001712/* Append a new keycert entry to a (possibly empty) list */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001713MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001714static int ssl_append_key_cert(mbedtls_ssl_key_cert **head,
1715 mbedtls_x509_crt *cert,
1716 mbedtls_pk_context *key)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001717{
niisato8ee24222018-06-25 19:05:48 +09001718 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001719
Gilles Peskine449bd832023-01-11 14:50:10 +01001720 if (cert == NULL) {
Glenn Strauss36872db2022-01-22 05:06:31 -05001721 /* Free list if cert is null */
Gilles Peskine449bd832023-01-11 14:50:10 +01001722 ssl_key_cert_free(*head);
Glenn Strauss36872db2022-01-22 05:06:31 -05001723 *head = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001724 return 0;
Glenn Strauss36872db2022-01-22 05:06:31 -05001725 }
1726
Gilles Peskine449bd832023-01-11 14:50:10 +01001727 new_cert = mbedtls_calloc(1, sizeof(mbedtls_ssl_key_cert));
1728 if (new_cert == NULL) {
1729 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
1730 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001731
niisato8ee24222018-06-25 19:05:48 +09001732 new_cert->cert = cert;
1733 new_cert->key = key;
1734 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001735
Glenn Strauss36872db2022-01-22 05:06:31 -05001736 /* Update head if the list was null, else add to the end */
Gilles Peskine449bd832023-01-11 14:50:10 +01001737 if (*head == NULL) {
niisato8ee24222018-06-25 19:05:48 +09001738 *head = new_cert;
Gilles Peskine449bd832023-01-11 14:50:10 +01001739 } else {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001740 mbedtls_ssl_key_cert *cur = *head;
Gilles Peskine449bd832023-01-11 14:50:10 +01001741 while (cur->next != NULL) {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001742 cur = cur->next;
Gilles Peskine449bd832023-01-11 14:50:10 +01001743 }
niisato8ee24222018-06-25 19:05:48 +09001744 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001745 }
1746
Gilles Peskine449bd832023-01-11 14:50:10 +01001747 return 0;
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001748}
1749
Gilles Peskine449bd832023-01-11 14:50:10 +01001750int mbedtls_ssl_conf_own_cert(mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001751 mbedtls_x509_crt *own_cert,
Gilles Peskine449bd832023-01-11 14:50:10 +01001752 mbedtls_pk_context *pk_key)
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001753{
Gilles Peskine449bd832023-01-11 14:50:10 +01001754 return ssl_append_key_cert(&conf->key_cert, own_cert, pk_key);
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001755}
1756
Gilles Peskine449bd832023-01-11 14:50:10 +01001757void mbedtls_ssl_conf_ca_chain(mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001758 mbedtls_x509_crt *ca_chain,
Gilles Peskine449bd832023-01-11 14:50:10 +01001759 mbedtls_x509_crl *ca_crl)
Paul Bakker5121ce52009-01-03 21:22:43 +00001760{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001761 conf->ca_chain = ca_chain;
1762 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00001763
1764#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1765 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1766 * cannot be used together. */
1767 conf->f_ca_cb = NULL;
1768 conf->p_ca_cb = NULL;
1769#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00001770}
Hanno Becker5adaad92019-03-27 16:54:37 +00001771
1772#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Gilles Peskine449bd832023-01-11 14:50:10 +01001773void mbedtls_ssl_conf_ca_cb(mbedtls_ssl_config *conf,
1774 mbedtls_x509_crt_ca_cb_t f_ca_cb,
1775 void *p_ca_cb)
Hanno Becker5adaad92019-03-27 16:54:37 +00001776{
1777 conf->f_ca_cb = f_ca_cb;
1778 conf->p_ca_cb = p_ca_cb;
1779
1780 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1781 * cannot be used together. */
1782 conf->ca_chain = NULL;
1783 conf->ca_crl = NULL;
1784}
1785#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001786#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00001787
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001788#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01001789const unsigned char *mbedtls_ssl_get_hs_sni(mbedtls_ssl_context *ssl,
1790 size_t *name_len)
Glenn Strauss69894072022-01-24 12:58:00 -05001791{
1792 *name_len = ssl->handshake->sni_name_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001793 return ssl->handshake->sni_name;
Glenn Strauss69894072022-01-24 12:58:00 -05001794}
1795
Gilles Peskine449bd832023-01-11 14:50:10 +01001796int mbedtls_ssl_set_hs_own_cert(mbedtls_ssl_context *ssl,
1797 mbedtls_x509_crt *own_cert,
1798 mbedtls_pk_context *pk_key)
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001799{
Gilles Peskine449bd832023-01-11 14:50:10 +01001800 return ssl_append_key_cert(&ssl->handshake->sni_key_cert,
1801 own_cert, pk_key);
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001802}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02001803
Gilles Peskine449bd832023-01-11 14:50:10 +01001804void mbedtls_ssl_set_hs_ca_chain(mbedtls_ssl_context *ssl,
1805 mbedtls_x509_crt *ca_chain,
1806 mbedtls_x509_crl *ca_crl)
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02001807{
1808 ssl->handshake->sni_ca_chain = ca_chain;
1809 ssl->handshake->sni_ca_crl = ca_crl;
1810}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001811
Glenn Strauss999ef702022-03-11 01:37:23 -05001812#if defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001813void mbedtls_ssl_set_hs_dn_hints(mbedtls_ssl_context *ssl,
1814 const mbedtls_x509_crt *crt)
Glenn Strauss999ef702022-03-11 01:37:23 -05001815{
1816 ssl->handshake->dn_hints = crt;
1817}
1818#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
1819
Gilles Peskine449bd832023-01-11 14:50:10 +01001820void mbedtls_ssl_set_hs_authmode(mbedtls_ssl_context *ssl,
1821 int authmode)
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001822{
1823 ssl->handshake->sni_authmode = authmode;
1824}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001825#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
1826
Hanno Becker8927c832019-04-03 12:52:50 +01001827#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001828void mbedtls_ssl_set_verify(mbedtls_ssl_context *ssl,
1829 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
1830 void *p_vrfy)
Hanno Becker8927c832019-04-03 12:52:50 +01001831{
1832 ssl->f_vrfy = f_vrfy;
1833 ssl->p_vrfy = p_vrfy;
1834}
1835#endif
1836
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02001837#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001838
Valerio Setti016f6822022-12-09 14:17:50 +01001839#if defined(MBEDTLS_USE_PSA_CRYPTO)
1840static psa_status_t mbedtls_ssl_set_hs_ecjpake_password_common(
Gilles Peskine449bd832023-01-11 14:50:10 +01001841 mbedtls_ssl_context *ssl,
1842 mbedtls_svc_key_id_t pwd)
Valerio Setti016f6822022-12-09 14:17:50 +01001843{
1844 psa_status_t status;
1845 psa_pake_role_t psa_role;
1846 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
1847
Gilles Peskine449bd832023-01-11 14:50:10 +01001848 psa_pake_cs_set_algorithm(&cipher_suite, PSA_ALG_JPAKE);
1849 psa_pake_cs_set_primitive(&cipher_suite,
1850 PSA_PAKE_PRIMITIVE(PSA_PAKE_PRIMITIVE_TYPE_ECC,
1851 PSA_ECC_FAMILY_SECP_R1,
1852 256));
1853 psa_pake_cs_set_hash(&cipher_suite, PSA_ALG_SHA_256);
Valerio Setti016f6822022-12-09 14:17:50 +01001854
Gilles Peskine449bd832023-01-11 14:50:10 +01001855 status = psa_pake_setup(&ssl->handshake->psa_pake_ctx, &cipher_suite);
1856 if (status != PSA_SUCCESS) {
Valerio Setti016f6822022-12-09 14:17:50 +01001857 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001858 }
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001859
Gilles Peskine449bd832023-01-11 14:50:10 +01001860 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Neil Armstrongca7d5062022-05-31 14:43:23 +02001861 psa_role = PSA_PAKE_ROLE_SERVER;
Gilles Peskine449bd832023-01-11 14:50:10 +01001862 } else {
Neil Armstrongca7d5062022-05-31 14:43:23 +02001863 psa_role = PSA_PAKE_ROLE_CLIENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001864 }
Neil Armstrongca7d5062022-05-31 14:43:23 +02001865
Gilles Peskine449bd832023-01-11 14:50:10 +01001866 status = psa_pake_set_role(&ssl->handshake->psa_pake_ctx, psa_role);
1867 if (status != PSA_SUCCESS) {
Valerio Setti016f6822022-12-09 14:17:50 +01001868 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001869 }
Valerio Setti016f6822022-12-09 14:17:50 +01001870
Gilles Peskine449bd832023-01-11 14:50:10 +01001871 status = psa_pake_set_password_key(&ssl->handshake->psa_pake_ctx, pwd);
1872 if (status != PSA_SUCCESS) {
Valerio Setti016f6822022-12-09 14:17:50 +01001873 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001874 }
Valerio Setti016f6822022-12-09 14:17:50 +01001875
1876 ssl->handshake->psa_pake_ctx_is_ok = 1;
1877
Gilles Peskine449bd832023-01-11 14:50:10 +01001878 return PSA_SUCCESS;
Valerio Setti016f6822022-12-09 14:17:50 +01001879}
1880
Gilles Peskine449bd832023-01-11 14:50:10 +01001881int mbedtls_ssl_set_hs_ecjpake_password(mbedtls_ssl_context *ssl,
1882 const unsigned char *pw,
1883 size_t pw_len)
Valerio Setti016f6822022-12-09 14:17:50 +01001884{
1885 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1886 psa_status_t status;
1887
Gilles Peskine449bd832023-01-11 14:50:10 +01001888 if (ssl->handshake == NULL || ssl->conf == NULL) {
1889 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Neil Armstrongca7d5062022-05-31 14:43:23 +02001890 }
1891
Gilles Peskine449bd832023-01-11 14:50:10 +01001892 /* Empty password is not valid */
1893 if ((pw == NULL) || (pw_len == 0)) {
1894 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
1895 }
1896
1897 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
1898 psa_set_key_algorithm(&attributes, PSA_ALG_JPAKE);
1899 psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD);
1900
1901 status = psa_import_key(&attributes, pw, pw_len,
1902 &ssl->handshake->psa_pake_password);
1903 if (status != PSA_SUCCESS) {
1904 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
1905 }
1906
1907 status = mbedtls_ssl_set_hs_ecjpake_password_common(ssl,
1908 ssl->handshake->psa_pake_password);
1909 if (status != PSA_SUCCESS) {
1910 psa_destroy_key(ssl->handshake->psa_pake_password);
1911 psa_pake_abort(&ssl->handshake->psa_pake_ctx);
1912 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
1913 }
1914
1915 return 0;
Valerio Setti02c25b52022-11-15 14:08:42 +01001916}
Valerio Settia9a97dc2022-11-28 18:26:16 +01001917
Gilles Peskine449bd832023-01-11 14:50:10 +01001918int mbedtls_ssl_set_hs_ecjpake_password_opaque(mbedtls_ssl_context *ssl,
1919 mbedtls_svc_key_id_t pwd)
Valerio Settia9a97dc2022-11-28 18:26:16 +01001920{
Valerio Settia9a97dc2022-11-28 18:26:16 +01001921 psa_status_t status;
1922
Gilles Peskine449bd832023-01-11 14:50:10 +01001923 if (ssl->handshake == NULL || ssl->conf == NULL) {
1924 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Neil Armstrongca7d5062022-05-31 14:43:23 +02001925 }
1926
Gilles Peskine449bd832023-01-11 14:50:10 +01001927 if (mbedtls_svc_key_id_is_null(pwd)) {
1928 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
1929 }
1930
1931 status = mbedtls_ssl_set_hs_ecjpake_password_common(ssl, pwd);
1932 if (status != PSA_SUCCESS) {
1933 psa_pake_abort(&ssl->handshake->psa_pake_ctx);
1934 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
1935 }
1936
1937 return 0;
Valerio Setti02c25b52022-11-15 14:08:42 +01001938}
1939#else /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01001940int mbedtls_ssl_set_hs_ecjpake_password(mbedtls_ssl_context *ssl,
1941 const unsigned char *pw,
1942 size_t pw_len)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001943{
1944 mbedtls_ecjpake_role role;
1945
Gilles Peskine449bd832023-01-11 14:50:10 +01001946 if (ssl->handshake == NULL || ssl->conf == NULL) {
1947 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
1948 }
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001949
Valerio Settic689ed82022-12-07 14:40:38 +01001950 /* Empty password is not valid */
Gilles Peskine449bd832023-01-11 14:50:10 +01001951 if ((pw == NULL) || (pw_len == 0)) {
1952 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
1953 }
Valerio Settic689ed82022-12-07 14:40:38 +01001954
Gilles Peskine449bd832023-01-11 14:50:10 +01001955 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001956 role = MBEDTLS_ECJPAKE_SERVER;
Gilles Peskine449bd832023-01-11 14:50:10 +01001957 } else {
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001958 role = MBEDTLS_ECJPAKE_CLIENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001959 }
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001960
Gilles Peskine449bd832023-01-11 14:50:10 +01001961 return mbedtls_ecjpake_setup(&ssl->handshake->ecjpake_ctx,
1962 role,
1963 MBEDTLS_MD_SHA256,
1964 MBEDTLS_ECP_DP_SECP256R1,
1965 pw, pw_len);
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001966}
Valerio Setti02c25b52022-11-15 14:08:42 +01001967#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02001968#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001969
Ronald Cron73fe8df2022-10-05 14:31:43 +02001970#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001971int mbedtls_ssl_conf_has_static_psk(mbedtls_ssl_config const *conf)
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001972{
Gilles Peskine449bd832023-01-11 14:50:10 +01001973 if (conf->psk_identity == NULL ||
1974 conf->psk_identity_len == 0) {
1975 return 0;
Ronald Crond29e13e2022-10-19 10:33:48 +02001976 }
1977
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001978#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001979 if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) {
1980 return 1;
1981 }
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001982#endif /* MBEDTLS_USE_PSA_CRYPTO */
Ronald Crond29e13e2022-10-19 10:33:48 +02001983
Gilles Peskine449bd832023-01-11 14:50:10 +01001984 if (conf->psk != NULL && conf->psk_len != 0) {
1985 return 1;
1986 }
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001987
Gilles Peskine449bd832023-01-11 14:50:10 +01001988 return 0;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01001989}
1990
Gilles Peskine449bd832023-01-11 14:50:10 +01001991static void ssl_conf_remove_psk(mbedtls_ssl_config *conf)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001992{
1993 /* Remove reference to existing PSK, if any. */
1994#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01001995 if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) {
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001996 /* The maintenance of the PSK key slot is the
1997 * user's responsibility. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001998 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01001999 }
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002000#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01002001 if (conf->psk != NULL) {
2002 mbedtls_platform_zeroize(conf->psk, conf->psk_len);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002003
Gilles Peskine449bd832023-01-11 14:50:10 +01002004 mbedtls_free(conf->psk);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002005 conf->psk = NULL;
2006 conf->psk_len = 0;
2007 }
2008
2009 /* Remove reference to PSK identity, if any. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002010 if (conf->psk_identity != NULL) {
2011 mbedtls_free(conf->psk_identity);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002012 conf->psk_identity = NULL;
2013 conf->psk_identity_len = 0;
2014 }
2015}
2016
Hanno Becker7390c712018-11-15 13:33:04 +00002017/* This function assumes that PSK identity in the SSL config is unset.
2018 * It checks that the provided identity is well-formed and attempts
2019 * to make a copy of it in the SSL config.
2020 * On failure, the PSK identity in the config remains unset. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002021MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002022static int ssl_conf_set_psk_identity(mbedtls_ssl_config *conf,
2023 unsigned char const *psk_identity,
2024 size_t psk_identity_len)
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002025{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02002026 /* Identity len will be encoded on two bytes */
Gilles Peskine449bd832023-01-11 14:50:10 +01002027 if (psk_identity == NULL ||
Ronald Cron2a87e9b2022-10-19 10:55:26 +02002028 psk_identity_len == 0 ||
Gilles Peskine449bd832023-01-11 14:50:10 +01002029 (psk_identity_len >> 16) != 0 ||
2030 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN) {
2031 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02002032 }
2033
Gilles Peskine449bd832023-01-11 14:50:10 +01002034 conf->psk_identity = mbedtls_calloc(1, psk_identity_len);
2035 if (conf->psk_identity == NULL) {
2036 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2037 }
Paul Bakker6db455e2013-09-18 17:29:31 +02002038
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01002039 conf->psk_identity_len = psk_identity_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002040 memcpy(conf->psk_identity, psk_identity, conf->psk_identity_len);
Paul Bakker5ad403f2013-09-18 21:21:30 +02002041
Gilles Peskine449bd832023-01-11 14:50:10 +01002042 return 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02002043}
2044
Gilles Peskine449bd832023-01-11 14:50:10 +01002045int mbedtls_ssl_conf_psk(mbedtls_ssl_config *conf,
2046 const unsigned char *psk, size_t psk_len,
2047 const unsigned char *psk_identity, size_t psk_identity_len)
Hanno Becker7390c712018-11-15 13:33:04 +00002048{
Janos Follath865b3eb2019-12-16 11:46:15 +00002049 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002050
2051 /* We currently only support one PSK, raw or opaque. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002052 if (mbedtls_ssl_conf_has_static_psk(conf)) {
2053 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
2054 }
Hanno Becker7390c712018-11-15 13:33:04 +00002055
2056 /* Check and set raw PSK */
Gilles Peskine449bd832023-01-11 14:50:10 +01002057 if (psk == NULL) {
2058 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2059 }
2060 if (psk_len == 0) {
2061 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2062 }
2063 if (psk_len > MBEDTLS_PSK_MAX_LEN) {
2064 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2065 }
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01002066
Gilles Peskine449bd832023-01-11 14:50:10 +01002067 if ((conf->psk = mbedtls_calloc(1, psk_len)) == NULL) {
2068 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2069 }
Hanno Becker7390c712018-11-15 13:33:04 +00002070 conf->psk_len = psk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002071 memcpy(conf->psk, psk, conf->psk_len);
Hanno Becker7390c712018-11-15 13:33:04 +00002072
2073 /* Check and set PSK Identity */
Gilles Peskine449bd832023-01-11 14:50:10 +01002074 ret = ssl_conf_set_psk_identity(conf, psk_identity, psk_identity_len);
2075 if (ret != 0) {
2076 ssl_conf_remove_psk(conf);
2077 }
Hanno Becker7390c712018-11-15 13:33:04 +00002078
Gilles Peskine449bd832023-01-11 14:50:10 +01002079 return ret;
Hanno Becker7390c712018-11-15 13:33:04 +00002080}
2081
Gilles Peskine449bd832023-01-11 14:50:10 +01002082static void ssl_remove_psk(mbedtls_ssl_context *ssl)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002083{
2084#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002085 if (!mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque)) {
Neil Armstrong501c9322022-05-03 09:35:09 +02002086 /* The maintenance of the external PSK key slot is the
2087 * user's responsibility. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002088 if (ssl->handshake->psk_opaque_is_internal) {
2089 psa_destroy_key(ssl->handshake->psk_opaque);
Neil Armstrong501c9322022-05-03 09:35:09 +02002090 ssl->handshake->psk_opaque_is_internal = 0;
2091 }
Ronald Croncf56a0a2020-08-04 09:51:30 +02002092 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002093 }
Neil Armstronge952a302022-05-03 10:22:14 +02002094#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002095 if (ssl->handshake->psk != NULL) {
2096 mbedtls_platform_zeroize(ssl->handshake->psk,
2097 ssl->handshake->psk_len);
2098 mbedtls_free(ssl->handshake->psk);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002099 ssl->handshake->psk_len = 0;
2100 }
Neil Armstronge952a302022-05-03 10:22:14 +02002101#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002102}
2103
Gilles Peskine449bd832023-01-11 14:50:10 +01002104int mbedtls_ssl_set_hs_psk(mbedtls_ssl_context *ssl,
2105 const unsigned char *psk, size_t psk_len)
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002106{
Neil Armstrong501c9322022-05-03 09:35:09 +02002107#if defined(MBEDTLS_USE_PSA_CRYPTO)
2108 psa_key_attributes_t key_attributes = psa_key_attributes_init();
Jerry Yu5d01c052022-08-17 10:18:10 +08002109 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jerry Yu24b8c812022-08-20 19:06:56 +08002110 psa_algorithm_t alg = PSA_ALG_NONE;
Jerry Yu5d01c052022-08-17 10:18:10 +08002111 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrong501c9322022-05-03 09:35:09 +02002112#endif /* MBEDTLS_USE_PSA_CRYPTO */
2113
Gilles Peskine449bd832023-01-11 14:50:10 +01002114 if (psk == NULL || ssl->handshake == NULL) {
2115 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2116 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002117
Gilles Peskine449bd832023-01-11 14:50:10 +01002118 if (psk_len > MBEDTLS_PSK_MAX_LEN) {
2119 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2120 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002121
Gilles Peskine449bd832023-01-11 14:50:10 +01002122 ssl_remove_psk(ssl);
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002123
Neil Armstrong501c9322022-05-03 09:35:09 +02002124#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yuccc68a42022-07-26 16:39:20 +08002125#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01002126 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2) {
2127 if (ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384) {
2128 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
2129 } else {
2130 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
2131 }
2132 psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE);
Jerry Yuccc68a42022-07-26 16:39:20 +08002133 }
2134#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Neil Armstrong501c9322022-05-03 09:35:09 +02002135
Jerry Yu568ec252022-07-22 21:27:34 +08002136#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01002137 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
2138 alg = PSA_ALG_HKDF_EXTRACT(PSA_ALG_ANY_HASH);
2139 psa_set_key_usage_flags(&key_attributes,
2140 PSA_KEY_USAGE_DERIVE | PSA_KEY_USAGE_EXPORT);
Jerry Yuccc68a42022-07-26 16:39:20 +08002141 }
2142#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
2143
Gilles Peskine449bd832023-01-11 14:50:10 +01002144 psa_set_key_algorithm(&key_attributes, alg);
2145 psa_set_key_type(&key_attributes, PSA_KEY_TYPE_DERIVE);
Neil Armstrong501c9322022-05-03 09:35:09 +02002146
Gilles Peskine449bd832023-01-11 14:50:10 +01002147 status = psa_import_key(&key_attributes, psk, psk_len, &key);
2148 if (status != PSA_SUCCESS) {
2149 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2150 }
Neil Armstrong501c9322022-05-03 09:35:09 +02002151
2152 /* Allow calling psa_destroy_key() on psk remove */
2153 ssl->handshake->psk_opaque_is_internal = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01002154 return mbedtls_ssl_set_hs_psk_opaque(ssl, key);
Neil Armstrong501c9322022-05-03 09:35:09 +02002155#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002156 if ((ssl->handshake->psk = mbedtls_calloc(1, psk_len)) == NULL) {
2157 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2158 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002159
2160 ssl->handshake->psk_len = psk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002161 memcpy(ssl->handshake->psk, psk, ssl->handshake->psk_len);
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002162
Gilles Peskine449bd832023-01-11 14:50:10 +01002163 return 0;
Neil Armstrong501c9322022-05-03 09:35:09 +02002164#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002165}
2166
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002167#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002168int mbedtls_ssl_conf_psk_opaque(mbedtls_ssl_config *conf,
2169 mbedtls_svc_key_id_t psk,
2170 const unsigned char *psk_identity,
2171 size_t psk_identity_len)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002172{
Janos Follath865b3eb2019-12-16 11:46:15 +00002173 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002174
2175 /* We currently only support one PSK, raw or opaque. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002176 if (mbedtls_ssl_conf_has_static_psk(conf)) {
2177 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
2178 }
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002179
Hanno Becker7390c712018-11-15 13:33:04 +00002180 /* Check and set opaque PSK */
Gilles Peskine449bd832023-01-11 14:50:10 +01002181 if (mbedtls_svc_key_id_is_null(psk)) {
2182 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2183 }
Ronald Croncf56a0a2020-08-04 09:51:30 +02002184 conf->psk_opaque = psk;
Hanno Becker7390c712018-11-15 13:33:04 +00002185
2186 /* Check and set PSK Identity */
Gilles Peskine449bd832023-01-11 14:50:10 +01002187 ret = ssl_conf_set_psk_identity(conf, psk_identity,
2188 psk_identity_len);
2189 if (ret != 0) {
2190 ssl_conf_remove_psk(conf);
2191 }
Hanno Becker7390c712018-11-15 13:33:04 +00002192
Gilles Peskine449bd832023-01-11 14:50:10 +01002193 return ret;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002194}
2195
Gilles Peskine449bd832023-01-11 14:50:10 +01002196int mbedtls_ssl_set_hs_psk_opaque(mbedtls_ssl_context *ssl,
2197 mbedtls_svc_key_id_t psk)
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002198{
Gilles Peskine449bd832023-01-11 14:50:10 +01002199 if ((mbedtls_svc_key_id_is_null(psk)) ||
2200 (ssl->handshake == NULL)) {
2201 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2202 }
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002203
Gilles Peskine449bd832023-01-11 14:50:10 +01002204 ssl_remove_psk(ssl);
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002205 ssl->handshake->psk_opaque = psk;
Gilles Peskine449bd832023-01-11 14:50:10 +01002206 return 0;
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002207}
2208#endif /* MBEDTLS_USE_PSA_CRYPTO */
2209
Jerry Yu8897c072022-08-12 13:56:53 +08002210#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002211void mbedtls_ssl_conf_psk_cb(mbedtls_ssl_config *conf,
2212 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
2213 size_t),
2214 void *p_psk)
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002215{
2216 conf->f_psk = f_psk;
2217 conf->p_psk = p_psk;
2218}
Jerry Yu8897c072022-08-12 13:56:53 +08002219#endif /* MBEDTLS_SSL_SRV_C */
2220
Ronald Cron73fe8df2022-10-05 14:31:43 +02002221#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002222
2223#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine301711e2022-04-26 16:57:05 +02002224static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
Gilles Peskine449bd832023-01-11 14:50:10 +01002225 psa_algorithm_t alg)
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002226{
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002227#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01002228 if (alg == PSA_ALG_CBC_NO_PADDING) {
2229 return MBEDTLS_SSL_MODE_CBC;
2230 }
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002231#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
Gilles Peskine449bd832023-01-11 14:50:10 +01002232 if (PSA_ALG_IS_AEAD(alg)) {
2233 return MBEDTLS_SSL_MODE_AEAD;
2234 }
2235 return MBEDTLS_SSL_MODE_STREAM;
Gilles Peskine301711e2022-04-26 16:57:05 +02002236}
2237
2238#else /* MBEDTLS_USE_PSA_CRYPTO */
2239
2240static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
Gilles Peskine449bd832023-01-11 14:50:10 +01002241 mbedtls_cipher_mode_t mode)
Gilles Peskine301711e2022-04-26 16:57:05 +02002242{
2243#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01002244 if (mode == MBEDTLS_MODE_CBC) {
2245 return MBEDTLS_SSL_MODE_CBC;
2246 }
Gilles Peskine301711e2022-04-26 16:57:05 +02002247#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
2248
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002249#if defined(MBEDTLS_GCM_C) || \
2250 defined(MBEDTLS_CCM_C) || \
2251 defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002252 if (mode == MBEDTLS_MODE_GCM ||
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002253 mode == MBEDTLS_MODE_CCM ||
Gilles Peskine449bd832023-01-11 14:50:10 +01002254 mode == MBEDTLS_MODE_CHACHAPOLY) {
2255 return MBEDTLS_SSL_MODE_AEAD;
2256 }
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002257#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002258
Gilles Peskine449bd832023-01-11 14:50:10 +01002259 return MBEDTLS_SSL_MODE_STREAM;
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002260}
Gilles Peskine301711e2022-04-26 16:57:05 +02002261#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002262
Gilles Peskinee108d982022-04-26 16:50:40 +02002263static mbedtls_ssl_mode_t mbedtls_ssl_get_actual_mode(
2264 mbedtls_ssl_mode_t base_mode,
Gilles Peskine449bd832023-01-11 14:50:10 +01002265 int encrypt_then_mac)
Gilles Peskinee108d982022-04-26 16:50:40 +02002266{
2267#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01002268 if (encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED &&
2269 base_mode == MBEDTLS_SSL_MODE_CBC) {
2270 return MBEDTLS_SSL_MODE_CBC_ETM;
Gilles Peskinee108d982022-04-26 16:50:40 +02002271 }
2272#else
2273 (void) encrypt_then_mac;
2274#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002275 return base_mode;
Gilles Peskinee108d982022-04-26 16:50:40 +02002276}
2277
Neil Armstrongab555e02022-04-04 11:07:59 +02002278mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_transform(
Gilles Peskine449bd832023-01-11 14:50:10 +01002279 const mbedtls_ssl_transform *transform)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002280{
Gilles Peskinee108d982022-04-26 16:50:40 +02002281 mbedtls_ssl_mode_t base_mode = mbedtls_ssl_get_base_mode(
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002282#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002283 transform->psa_alg
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002284#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002285 mbedtls_cipher_get_cipher_mode(&transform->cipher_ctx_enc)
Gilles Peskinee108d982022-04-26 16:50:40 +02002286#endif
2287 );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002288
Gilles Peskinee108d982022-04-26 16:50:40 +02002289 int encrypt_then_mac = 0;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002290#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskinee108d982022-04-26 16:50:40 +02002291 encrypt_then_mac = transform->encrypt_then_mac;
2292#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002293 return mbedtls_ssl_get_actual_mode(base_mode, encrypt_then_mac);
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002294}
2295
Neil Armstrongab555e02022-04-04 11:07:59 +02002296mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002297#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01002298 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002299#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01002300 const mbedtls_ssl_ciphersuite_t *suite)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002301{
Gilles Peskinee108d982022-04-26 16:50:40 +02002302 mbedtls_ssl_mode_t base_mode = MBEDTLS_SSL_MODE_STREAM;
2303
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002304#if defined(MBEDTLS_USE_PSA_CRYPTO)
2305 psa_status_t status;
2306 psa_algorithm_t alg;
2307 psa_key_type_t type;
2308 size_t size;
Gilles Peskine449bd832023-01-11 14:50:10 +01002309 status = mbedtls_ssl_cipher_to_psa(suite->cipher, 0, &alg, &type, &size);
2310 if (status == PSA_SUCCESS) {
2311 base_mode = mbedtls_ssl_get_base_mode(alg);
2312 }
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002313#else
2314 const mbedtls_cipher_info_t *cipher =
Gilles Peskine449bd832023-01-11 14:50:10 +01002315 mbedtls_cipher_info_from_type(suite->cipher);
2316 if (cipher != NULL) {
Gilles Peskinee108d982022-04-26 16:50:40 +02002317 base_mode =
2318 mbedtls_ssl_get_base_mode(
Gilles Peskine449bd832023-01-11 14:50:10 +01002319 mbedtls_cipher_info_get_mode(cipher));
Gilles Peskinee108d982022-04-26 16:50:40 +02002320 }
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002321#endif /* MBEDTLS_USE_PSA_CRYPTO */
2322
Gilles Peskinee108d982022-04-26 16:50:40 +02002323#if !defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
2324 int encrypt_then_mac = 0;
2325#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002326 return mbedtls_ssl_get_actual_mode(base_mode, encrypt_then_mac);
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002327}
2328
Neil Armstrong8395d7a2022-05-18 11:44:56 +02002329#if defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu251a12e2022-07-13 15:15:48 +08002330
2331#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu438ddd82022-07-07 06:55:50 +00002332/* Serialization of TLS 1.3 sessions:
2333 *
2334 * struct {
Xiaokang Qian126bf8e2022-10-13 02:22:40 +00002335 * opaque hostname<0..2^16-1>;
Jerry Yu438ddd82022-07-07 06:55:50 +00002336 * uint64 ticket_received;
2337 * uint32 ticket_lifetime;
Jerry Yu34191072022-08-18 10:32:09 +08002338 * opaque ticket<1..2^16-1>;
Jerry Yu438ddd82022-07-07 06:55:50 +00002339 * } ClientOnlyData;
2340 *
2341 * struct {
2342 * uint8 endpoint;
2343 * uint8 ciphersuite[2];
2344 * uint32 ticket_age_add;
2345 * uint8 ticket_flags;
2346 * opaque resumption_key<0..255>;
2347 * select ( endpoint ) {
2348 * case client: ClientOnlyData;
2349 * case server: uint64 start_time;
2350 * };
2351 * } serialized_session_tls13;
2352 *
2353 */
2354#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yue36fdd62022-08-17 21:31:36 +08002355MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002356static int ssl_tls13_session_save(const mbedtls_ssl_session *session,
2357 unsigned char *buf,
2358 size_t buf_len,
2359 size_t *olen)
Jerry Yu438ddd82022-07-07 06:55:50 +00002360{
2361 unsigned char *p = buf;
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00002362#if defined(MBEDTLS_SSL_CLI_C) && \
2363 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01002364 size_t hostname_len = (session->hostname == NULL) ?
2365 0 : strlen(session->hostname) + 1;
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00002366#endif
Jerry Yubc7c1a42022-07-21 22:57:37 +08002367 size_t needed = 1 /* endpoint */
2368 + 2 /* ciphersuite */
2369 + 4 /* ticket_age_add */
Jerry Yu34191072022-08-18 10:32:09 +08002370 + 1 /* ticket_flags */
2371 + 1; /* resumption_key length */
Jerry Yue36fdd62022-08-17 21:31:36 +08002372 *olen = 0;
Jerry Yu34191072022-08-18 10:32:09 +08002373
Gilles Peskine449bd832023-01-11 14:50:10 +01002374 if (session->resumption_key_len > MBEDTLS_SSL_TLS1_3_TICKET_RESUMPTION_KEY_LEN) {
2375 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2376 }
Jerry Yu34191072022-08-18 10:32:09 +08002377 needed += session->resumption_key_len; /* resumption_key */
2378
Jerry Yu438ddd82022-07-07 06:55:50 +00002379#if defined(MBEDTLS_HAVE_TIME)
2380 needed += 8; /* start_time or ticket_received */
2381#endif
2382
2383#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002384 if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Xiaokang Qian126bf8e2022-10-13 02:22:40 +00002385#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaokang Qianbc663a02022-10-09 11:14:39 +00002386 needed += 2 /* hostname_len */
Gilles Peskine449bd832023-01-11 14:50:10 +01002387 + hostname_len; /* hostname */
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00002388#endif
2389
Jerry Yu438ddd82022-07-07 06:55:50 +00002390 needed += 4 /* ticket_lifetime */
Jerry Yue36fdd62022-08-17 21:31:36 +08002391 + 2; /* ticket_len */
Jerry Yu34191072022-08-18 10:32:09 +08002392
2393 /* Check size_t overflow */
Gilles Peskine449bd832023-01-11 14:50:10 +01002394 if (session->ticket_len > SIZE_MAX - needed) {
2395 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2396 }
Jerry Yu34191072022-08-18 10:32:09 +08002397
Jerry Yue28d9742022-08-18 15:44:03 +08002398 needed += session->ticket_len; /* ticket */
Jerry Yu438ddd82022-07-07 06:55:50 +00002399 }
2400#endif /* MBEDTLS_SSL_CLI_C */
2401
Jerry Yue36fdd62022-08-17 21:31:36 +08002402 *olen = needed;
Gilles Peskine449bd832023-01-11 14:50:10 +01002403 if (needed > buf_len) {
2404 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
2405 }
Jerry Yu438ddd82022-07-07 06:55:50 +00002406
2407 p[0] = session->endpoint;
Gilles Peskine449bd832023-01-11 14:50:10 +01002408 MBEDTLS_PUT_UINT16_BE(session->ciphersuite, p, 1);
2409 MBEDTLS_PUT_UINT32_BE(session->ticket_age_add, p, 3);
Jerry Yu438ddd82022-07-07 06:55:50 +00002410 p[7] = session->ticket_flags;
2411
2412 /* save resumption_key */
Jerry Yubc7c1a42022-07-21 22:57:37 +08002413 p[8] = session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002414 p += 9;
Gilles Peskine449bd832023-01-11 14:50:10 +01002415 memcpy(p, session->resumption_key, session->resumption_key_len);
Jerry Yubc7c1a42022-07-21 22:57:37 +08002416 p += session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002417
2418#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002419 if (session->endpoint == MBEDTLS_SSL_IS_SERVER) {
2420 MBEDTLS_PUT_UINT64_BE((uint64_t) session->start, p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002421 p += 8;
2422 }
2423#endif /* MBEDTLS_HAVE_TIME */
2424
2425#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002426 if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002427#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01002428 MBEDTLS_PUT_UINT16_BE(hostname_len, p, 0);
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002429 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002430 if (hostname_len > 0) {
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002431 /* save host name */
Gilles Peskine449bd832023-01-11 14:50:10 +01002432 memcpy(p, session->hostname, hostname_len);
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002433 p += hostname_len;
2434 }
2435#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
2436
Jerry Yu438ddd82022-07-07 06:55:50 +00002437#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01002438 MBEDTLS_PUT_UINT64_BE((uint64_t) session->ticket_received, p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002439 p += 8;
2440#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002441 MBEDTLS_PUT_UINT32_BE(session->ticket_lifetime, p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002442 p += 4;
2443
Gilles Peskine449bd832023-01-11 14:50:10 +01002444 MBEDTLS_PUT_UINT16_BE(session->ticket_len, p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002445 p += 2;
Jerry Yu34191072022-08-18 10:32:09 +08002446
Gilles Peskine449bd832023-01-11 14:50:10 +01002447 if (session->ticket != NULL && session->ticket_len > 0) {
2448 memcpy(p, session->ticket, session->ticket_len);
Jerry Yu438ddd82022-07-07 06:55:50 +00002449 p += session->ticket_len;
2450 }
2451 }
2452#endif /* MBEDTLS_SSL_CLI_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01002453 return 0;
Jerry Yu438ddd82022-07-07 06:55:50 +00002454}
2455
2456MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002457static int ssl_tls13_session_load(mbedtls_ssl_session *session,
2458 const unsigned char *buf,
2459 size_t len)
Jerry Yu438ddd82022-07-07 06:55:50 +00002460{
2461 const unsigned char *p = buf;
2462 const unsigned char *end = buf + len;
2463
Gilles Peskine449bd832023-01-11 14:50:10 +01002464 if (end - p < 9) {
2465 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2466 }
Jerry Yu438ddd82022-07-07 06:55:50 +00002467 session->endpoint = p[0];
Gilles Peskine449bd832023-01-11 14:50:10 +01002468 session->ciphersuite = MBEDTLS_GET_UINT16_BE(p, 1);
2469 session->ticket_age_add = MBEDTLS_GET_UINT32_BE(p, 3);
Jerry Yu438ddd82022-07-07 06:55:50 +00002470 session->ticket_flags = p[7];
2471
2472 /* load resumption_key */
Jerry Yubc7c1a42022-07-21 22:57:37 +08002473 session->resumption_key_len = p[8];
Jerry Yu438ddd82022-07-07 06:55:50 +00002474 p += 9;
2475
Gilles Peskine449bd832023-01-11 14:50:10 +01002476 if (end - p < session->resumption_key_len) {
2477 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2478 }
Jerry Yu438ddd82022-07-07 06:55:50 +00002479
Gilles Peskine449bd832023-01-11 14:50:10 +01002480 if (sizeof(session->resumption_key) < session->resumption_key_len) {
2481 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2482 }
2483 memcpy(session->resumption_key, p, session->resumption_key_len);
Jerry Yubc7c1a42022-07-21 22:57:37 +08002484 p += session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002485
2486#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002487 if (session->endpoint == MBEDTLS_SSL_IS_SERVER) {
2488 if (end - p < 8) {
2489 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2490 }
2491 session->start = MBEDTLS_GET_UINT64_BE(p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002492 p += 8;
2493 }
2494#endif /* MBEDTLS_HAVE_TIME */
2495
2496#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002497 if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002498#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01002499 defined(MBEDTLS_SSL_SESSION_TICKETS)
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002500 size_t hostname_len;
2501 /* load host name */
Gilles Peskine449bd832023-01-11 14:50:10 +01002502 if (end - p < 2) {
2503 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2504 }
2505 hostname_len = MBEDTLS_GET_UINT16_BE(p, 0);
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002506 p += 2;
2507
Gilles Peskine449bd832023-01-11 14:50:10 +01002508 if (end - p < (long int) hostname_len) {
2509 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2510 }
2511 if (hostname_len > 0) {
2512 session->hostname = mbedtls_calloc(1, hostname_len);
2513 if (session->hostname == NULL) {
2514 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2515 }
2516 memcpy(session->hostname, p, hostname_len);
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002517 p += hostname_len;
2518 }
2519#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION &&
2520 MBEDTLS_SSL_SESSION_TICKETS */
2521
Jerry Yu438ddd82022-07-07 06:55:50 +00002522#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01002523 if (end - p < 8) {
2524 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2525 }
2526 session->ticket_received = MBEDTLS_GET_UINT64_BE(p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002527 p += 8;
2528#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002529 if (end - p < 4) {
2530 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2531 }
2532 session->ticket_lifetime = MBEDTLS_GET_UINT32_BE(p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002533 p += 4;
2534
Gilles Peskine449bd832023-01-11 14:50:10 +01002535 if (end - p < 2) {
2536 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2537 }
2538 session->ticket_len = MBEDTLS_GET_UINT16_BE(p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002539 p += 2;
2540
Gilles Peskine449bd832023-01-11 14:50:10 +01002541 if (end - p < (long int) session->ticket_len) {
2542 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2543 }
2544 if (session->ticket_len > 0) {
2545 session->ticket = mbedtls_calloc(1, session->ticket_len);
2546 if (session->ticket == NULL) {
2547 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2548 }
2549 memcpy(session->ticket, p, session->ticket_len);
Jerry Yu438ddd82022-07-07 06:55:50 +00002550 p += session->ticket_len;
2551 }
2552 }
2553#endif /* MBEDTLS_SSL_CLI_C */
2554
Gilles Peskine449bd832023-01-11 14:50:10 +01002555 return 0;
Jerry Yu438ddd82022-07-07 06:55:50 +00002556
2557}
2558#else /* MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yue36fdd62022-08-17 21:31:36 +08002559MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002560static int ssl_tls13_session_save(const mbedtls_ssl_session *session,
2561 unsigned char *buf,
2562 size_t buf_len,
2563 size_t *olen)
Jerry Yu251a12e2022-07-13 15:15:48 +08002564{
2565 ((void) session);
2566 ((void) buf);
2567 ((void) buf_len);
Jerry Yue36fdd62022-08-17 21:31:36 +08002568 *olen = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002569 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Jerry Yu251a12e2022-07-13 15:15:48 +08002570}
Jerry Yu438ddd82022-07-07 06:55:50 +00002571
Gilles Peskine449bd832023-01-11 14:50:10 +01002572static int ssl_tls13_session_load(const mbedtls_ssl_session *session,
2573 unsigned char *buf,
2574 size_t buf_len)
Jerry Yu438ddd82022-07-07 06:55:50 +00002575{
2576 ((void) session);
2577 ((void) buf);
2578 ((void) buf_len);
Gilles Peskine449bd832023-01-11 14:50:10 +01002579 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Jerry Yu438ddd82022-07-07 06:55:50 +00002580}
2581#endif /* !MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yu251a12e2022-07-13 15:15:48 +08002582#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
2583
Gilles Peskine449bd832023-01-11 14:50:10 +01002584psa_status_t mbedtls_ssl_cipher_to_psa(mbedtls_cipher_type_t mbedtls_cipher_type,
2585 size_t taglen,
2586 psa_algorithm_t *alg,
2587 psa_key_type_t *key_type,
2588 size_t *key_size)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002589{
Gilles Peskine449bd832023-01-11 14:50:10 +01002590 switch (mbedtls_cipher_type) {
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002591 case MBEDTLS_CIPHER_AES_128_CBC:
2592 *alg = PSA_ALG_CBC_NO_PADDING;
2593 *key_type = PSA_KEY_TYPE_AES;
2594 *key_size = 128;
2595 break;
2596 case MBEDTLS_CIPHER_AES_128_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002597 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002598 *key_type = PSA_KEY_TYPE_AES;
2599 *key_size = 128;
2600 break;
2601 case MBEDTLS_CIPHER_AES_128_GCM:
2602 *alg = PSA_ALG_GCM;
2603 *key_type = PSA_KEY_TYPE_AES;
2604 *key_size = 128;
2605 break;
2606 case MBEDTLS_CIPHER_AES_192_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002607 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002608 *key_type = PSA_KEY_TYPE_AES;
2609 *key_size = 192;
2610 break;
2611 case MBEDTLS_CIPHER_AES_192_GCM:
2612 *alg = PSA_ALG_GCM;
2613 *key_type = PSA_KEY_TYPE_AES;
2614 *key_size = 192;
2615 break;
2616 case MBEDTLS_CIPHER_AES_256_CBC:
2617 *alg = PSA_ALG_CBC_NO_PADDING;
2618 *key_type = PSA_KEY_TYPE_AES;
2619 *key_size = 256;
2620 break;
2621 case MBEDTLS_CIPHER_AES_256_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002622 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002623 *key_type = PSA_KEY_TYPE_AES;
2624 *key_size = 256;
2625 break;
2626 case MBEDTLS_CIPHER_AES_256_GCM:
2627 *alg = PSA_ALG_GCM;
2628 *key_type = PSA_KEY_TYPE_AES;
2629 *key_size = 256;
2630 break;
2631 case MBEDTLS_CIPHER_ARIA_128_CBC:
2632 *alg = PSA_ALG_CBC_NO_PADDING;
2633 *key_type = PSA_KEY_TYPE_ARIA;
2634 *key_size = 128;
2635 break;
2636 case MBEDTLS_CIPHER_ARIA_128_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002637 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002638 *key_type = PSA_KEY_TYPE_ARIA;
2639 *key_size = 128;
2640 break;
2641 case MBEDTLS_CIPHER_ARIA_128_GCM:
2642 *alg = PSA_ALG_GCM;
2643 *key_type = PSA_KEY_TYPE_ARIA;
2644 *key_size = 128;
2645 break;
2646 case MBEDTLS_CIPHER_ARIA_192_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002647 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002648 *key_type = PSA_KEY_TYPE_ARIA;
2649 *key_size = 192;
2650 break;
2651 case MBEDTLS_CIPHER_ARIA_192_GCM:
2652 *alg = PSA_ALG_GCM;
2653 *key_type = PSA_KEY_TYPE_ARIA;
2654 *key_size = 192;
2655 break;
2656 case MBEDTLS_CIPHER_ARIA_256_CBC:
2657 *alg = PSA_ALG_CBC_NO_PADDING;
2658 *key_type = PSA_KEY_TYPE_ARIA;
2659 *key_size = 256;
2660 break;
2661 case MBEDTLS_CIPHER_ARIA_256_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002662 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002663 *key_type = PSA_KEY_TYPE_ARIA;
2664 *key_size = 256;
2665 break;
2666 case MBEDTLS_CIPHER_ARIA_256_GCM:
2667 *alg = PSA_ALG_GCM;
2668 *key_type = PSA_KEY_TYPE_ARIA;
2669 *key_size = 256;
2670 break;
2671 case MBEDTLS_CIPHER_CAMELLIA_128_CBC:
2672 *alg = PSA_ALG_CBC_NO_PADDING;
2673 *key_type = PSA_KEY_TYPE_CAMELLIA;
2674 *key_size = 128;
2675 break;
2676 case MBEDTLS_CIPHER_CAMELLIA_128_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002677 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002678 *key_type = PSA_KEY_TYPE_CAMELLIA;
2679 *key_size = 128;
2680 break;
2681 case MBEDTLS_CIPHER_CAMELLIA_128_GCM:
2682 *alg = PSA_ALG_GCM;
2683 *key_type = PSA_KEY_TYPE_CAMELLIA;
2684 *key_size = 128;
2685 break;
2686 case MBEDTLS_CIPHER_CAMELLIA_192_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002687 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002688 *key_type = PSA_KEY_TYPE_CAMELLIA;
2689 *key_size = 192;
2690 break;
2691 case MBEDTLS_CIPHER_CAMELLIA_192_GCM:
2692 *alg = PSA_ALG_GCM;
2693 *key_type = PSA_KEY_TYPE_CAMELLIA;
2694 *key_size = 192;
2695 break;
2696 case MBEDTLS_CIPHER_CAMELLIA_256_CBC:
2697 *alg = PSA_ALG_CBC_NO_PADDING;
2698 *key_type = PSA_KEY_TYPE_CAMELLIA;
2699 *key_size = 256;
2700 break;
2701 case MBEDTLS_CIPHER_CAMELLIA_256_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002702 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002703 *key_type = PSA_KEY_TYPE_CAMELLIA;
2704 *key_size = 256;
2705 break;
2706 case MBEDTLS_CIPHER_CAMELLIA_256_GCM:
2707 *alg = PSA_ALG_GCM;
2708 *key_type = PSA_KEY_TYPE_CAMELLIA;
2709 *key_size = 256;
2710 break;
2711 case MBEDTLS_CIPHER_CHACHA20_POLY1305:
2712 *alg = PSA_ALG_CHACHA20_POLY1305;
2713 *key_type = PSA_KEY_TYPE_CHACHA20;
2714 *key_size = 256;
2715 break;
2716 case MBEDTLS_CIPHER_NULL:
2717 *alg = MBEDTLS_SSL_NULL_CIPHER;
2718 *key_type = 0;
2719 *key_size = 0;
2720 break;
2721 default:
2722 return PSA_ERROR_NOT_SUPPORTED;
2723 }
2724
2725 return PSA_SUCCESS;
2726}
Neil Armstrong8395d7a2022-05-18 11:44:56 +02002727#endif /* MBEDTLS_USE_PSA_CRYPTO || MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002728
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002729#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002730int mbedtls_ssl_conf_dh_param_bin(mbedtls_ssl_config *conf,
2731 const unsigned char *dhm_P, size_t P_len,
2732 const unsigned char *dhm_G, size_t G_len)
Hanno Beckera90658f2017-10-04 15:29:08 +01002733{
Janos Follath865b3eb2019-12-16 11:46:15 +00002734 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckera90658f2017-10-04 15:29:08 +01002735
Gilles Peskine449bd832023-01-11 14:50:10 +01002736 mbedtls_mpi_free(&conf->dhm_P);
2737 mbedtls_mpi_free(&conf->dhm_G);
Glenn Strausscee11292021-12-20 01:43:17 -05002738
Gilles Peskine449bd832023-01-11 14:50:10 +01002739 if ((ret = mbedtls_mpi_read_binary(&conf->dhm_P, dhm_P, P_len)) != 0 ||
2740 (ret = mbedtls_mpi_read_binary(&conf->dhm_G, dhm_G, G_len)) != 0) {
2741 mbedtls_mpi_free(&conf->dhm_P);
2742 mbedtls_mpi_free(&conf->dhm_G);
2743 return ret;
Hanno Beckera90658f2017-10-04 15:29:08 +01002744 }
2745
Gilles Peskine449bd832023-01-11 14:50:10 +01002746 return 0;
Hanno Beckera90658f2017-10-04 15:29:08 +01002747}
Paul Bakker5121ce52009-01-03 21:22:43 +00002748
Gilles Peskine449bd832023-01-11 14:50:10 +01002749int mbedtls_ssl_conf_dh_param_ctx(mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx)
Paul Bakker1b57b062011-01-06 15:48:19 +00002750{
Janos Follath865b3eb2019-12-16 11:46:15 +00002751 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1b57b062011-01-06 15:48:19 +00002752
Gilles Peskine449bd832023-01-11 14:50:10 +01002753 mbedtls_mpi_free(&conf->dhm_P);
2754 mbedtls_mpi_free(&conf->dhm_G);
Glenn Strausscee11292021-12-20 01:43:17 -05002755
Gilles Peskine449bd832023-01-11 14:50:10 +01002756 if ((ret = mbedtls_dhm_get_value(dhm_ctx, MBEDTLS_DHM_PARAM_P,
2757 &conf->dhm_P)) != 0 ||
2758 (ret = mbedtls_dhm_get_value(dhm_ctx, MBEDTLS_DHM_PARAM_G,
2759 &conf->dhm_G)) != 0) {
2760 mbedtls_mpi_free(&conf->dhm_P);
2761 mbedtls_mpi_free(&conf->dhm_G);
2762 return ret;
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01002763 }
Paul Bakker1b57b062011-01-06 15:48:19 +00002764
Gilles Peskine449bd832023-01-11 14:50:10 +01002765 return 0;
Paul Bakker1b57b062011-01-06 15:48:19 +00002766}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002767#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00002768
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02002769#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
2770/*
2771 * Set the minimum length for Diffie-Hellman parameters
2772 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002773void mbedtls_ssl_conf_dhm_min_bitlen(mbedtls_ssl_config *conf,
2774 unsigned int bitlen)
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02002775{
2776 conf->dhm_min_bitlen = bitlen;
2777}
2778#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
2779
Ronald Crone68ab4f2022-10-05 12:46:29 +02002780#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002781#if !defined(MBEDTLS_DEPRECATED_REMOVED) && defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002782/*
2783 * Set allowed/preferred hashes for handshake signatures
2784 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002785void mbedtls_ssl_conf_sig_hashes(mbedtls_ssl_config *conf,
2786 const int *hashes)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002787{
2788 conf->sig_hashes = hashes;
2789}
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002790#endif /* !MBEDTLS_DEPRECATED_REMOVED && MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker1cd6e002021-08-10 13:27:10 +01002791
Jerry Yuf017ee42022-01-12 15:49:48 +08002792/* Configure allowed signature algorithms for handshake */
Gilles Peskine449bd832023-01-11 14:50:10 +01002793void mbedtls_ssl_conf_sig_algs(mbedtls_ssl_config *conf,
2794 const uint16_t *sig_algs)
Hanno Becker1cd6e002021-08-10 13:27:10 +01002795{
Jerry Yuf017ee42022-01-12 15:49:48 +08002796#if !defined(MBEDTLS_DEPRECATED_REMOVED)
2797 conf->sig_hashes = NULL;
2798#endif /* !MBEDTLS_DEPRECATED_REMOVED */
2799 conf->sig_algs = sig_algs;
Hanno Becker1cd6e002021-08-10 13:27:10 +01002800}
Ronald Crone68ab4f2022-10-05 12:46:29 +02002801#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002802
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02002803#if defined(MBEDTLS_ECP_C)
Brett Warrene0edc842021-08-17 09:53:13 +01002804#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002805/*
2806 * Set the allowed elliptic curves
Brett Warrene0edc842021-08-17 09:53:13 +01002807 *
2808 * mbedtls_ssl_setup() takes the provided list
2809 * and translates it to a list of IANA TLS group identifiers,
2810 * stored in ssl->handshake->group_list.
2811 *
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002812 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002813void mbedtls_ssl_conf_curves(mbedtls_ssl_config *conf,
2814 const mbedtls_ecp_group_id *curve_list)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002815{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002816 conf->curve_list = curve_list;
Brett Warrene0edc842021-08-17 09:53:13 +01002817 conf->group_list = NULL;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002818}
Brett Warrene0edc842021-08-17 09:53:13 +01002819#endif /* MBEDTLS_DEPRECATED_REMOVED */
Hanno Becker947194e2017-04-07 13:25:49 +01002820#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002821
Brett Warrene0edc842021-08-17 09:53:13 +01002822/*
2823 * Set the allowed groups
2824 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002825void mbedtls_ssl_conf_groups(mbedtls_ssl_config *conf,
2826 const uint16_t *group_list)
Brett Warrene0edc842021-08-17 09:53:13 +01002827{
2828#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
2829 conf->curve_list = NULL;
2830#endif
2831 conf->group_list = group_list;
2832}
2833
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002834#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002835int mbedtls_ssl_set_hostname(mbedtls_ssl_context *ssl, const char *hostname)
Paul Bakker5121ce52009-01-03 21:22:43 +00002836{
Hanno Becker947194e2017-04-07 13:25:49 +01002837 /* Initialize to suppress unnecessary compiler warning */
2838 size_t hostname_len = 0;
2839
2840 /* Check if new hostname is valid before
2841 * making any change to current one */
Gilles Peskine449bd832023-01-11 14:50:10 +01002842 if (hostname != NULL) {
2843 hostname_len = strlen(hostname);
Hanno Becker947194e2017-04-07 13:25:49 +01002844
Gilles Peskine449bd832023-01-11 14:50:10 +01002845 if (hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN) {
2846 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2847 }
Hanno Becker947194e2017-04-07 13:25:49 +01002848 }
2849
2850 /* Now it's clear that we will overwrite the old hostname,
2851 * so we can free it safely */
2852
Gilles Peskine449bd832023-01-11 14:50:10 +01002853 if (ssl->hostname != NULL) {
2854 mbedtls_platform_zeroize(ssl->hostname, strlen(ssl->hostname));
2855 mbedtls_free(ssl->hostname);
Hanno Becker947194e2017-04-07 13:25:49 +01002856 }
2857
2858 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01002859
Gilles Peskine449bd832023-01-11 14:50:10 +01002860 if (hostname == NULL) {
Hanno Becker947194e2017-04-07 13:25:49 +01002861 ssl->hostname = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01002862 } else {
2863 ssl->hostname = mbedtls_calloc(1, hostname_len + 1);
2864 if (ssl->hostname == NULL) {
2865 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2866 }
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002867
Gilles Peskine449bd832023-01-11 14:50:10 +01002868 memcpy(ssl->hostname, hostname, hostname_len);
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002869
Hanno Becker947194e2017-04-07 13:25:49 +01002870 ssl->hostname[hostname_len] = '\0';
2871 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002872
Gilles Peskine449bd832023-01-11 14:50:10 +01002873 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002874}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01002875#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002876
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002877#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01002878void mbedtls_ssl_conf_sni(mbedtls_ssl_config *conf,
2879 int (*f_sni)(void *, mbedtls_ssl_context *,
2880 const unsigned char *, size_t),
2881 void *p_sni)
Paul Bakker5701cdc2012-09-27 21:49:42 +00002882{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002883 conf->f_sni = f_sni;
2884 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00002885}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002886#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00002887
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002888#if defined(MBEDTLS_SSL_ALPN)
Gilles Peskine449bd832023-01-11 14:50:10 +01002889int mbedtls_ssl_conf_alpn_protocols(mbedtls_ssl_config *conf, const char **protos)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002890{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002891 size_t cur_len, tot_len;
2892 const char **p;
2893
2894 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08002895 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
2896 * MUST NOT be truncated."
2897 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002898 */
2899 tot_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002900 for (p = protos; *p != NULL; p++) {
2901 cur_len = strlen(*p);
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002902 tot_len += cur_len;
2903
Gilles Peskine449bd832023-01-11 14:50:10 +01002904 if ((cur_len == 0) ||
2905 (cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN) ||
2906 (tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN)) {
2907 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2908 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002909 }
2910
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002911 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002912
Gilles Peskine449bd832023-01-11 14:50:10 +01002913 return 0;
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002914}
2915
Gilles Peskine449bd832023-01-11 14:50:10 +01002916const char *mbedtls_ssl_get_alpn_protocol(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002917{
Gilles Peskine449bd832023-01-11 14:50:10 +01002918 return ssl->alpn_chosen;
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002919}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002920#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002921
Johan Pascalb62bb512015-12-03 21:56:45 +01002922#if defined(MBEDTLS_SSL_DTLS_SRTP)
Gilles Peskine449bd832023-01-11 14:50:10 +01002923void mbedtls_ssl_conf_srtp_mki_value_supported(mbedtls_ssl_config *conf,
2924 int support_mki_value)
Ron Eldor591f1622018-01-22 12:30:04 +02002925{
2926 conf->dtls_srtp_mki_support = support_mki_value;
2927}
2928
Gilles Peskine449bd832023-01-11 14:50:10 +01002929int mbedtls_ssl_dtls_srtp_set_mki_value(mbedtls_ssl_context *ssl,
2930 unsigned char *mki_value,
2931 uint16_t mki_len)
Ron Eldor591f1622018-01-22 12:30:04 +02002932{
Gilles Peskine449bd832023-01-11 14:50:10 +01002933 if (mki_len > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH) {
2934 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Ron Eldora9788042018-12-05 11:04:31 +02002935 }
Ron Eldor591f1622018-01-22 12:30:04 +02002936
Gilles Peskine449bd832023-01-11 14:50:10 +01002937 if (ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_UNSUPPORTED) {
2938 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Ron Eldora9788042018-12-05 11:04:31 +02002939 }
Ron Eldor591f1622018-01-22 12:30:04 +02002940
Gilles Peskine449bd832023-01-11 14:50:10 +01002941 memcpy(ssl->dtls_srtp_info.mki_value, mki_value, mki_len);
Ron Eldor591f1622018-01-22 12:30:04 +02002942 ssl->dtls_srtp_info.mki_len = mki_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002943 return 0;
Ron Eldor591f1622018-01-22 12:30:04 +02002944}
2945
Gilles Peskine449bd832023-01-11 14:50:10 +01002946int mbedtls_ssl_conf_dtls_srtp_protection_profiles(mbedtls_ssl_config *conf,
2947 const mbedtls_ssl_srtp_profile *profiles)
Johan Pascalb62bb512015-12-03 21:56:45 +01002948{
Johan Pascal253d0262020-09-22 13:04:45 +02002949 const mbedtls_ssl_srtp_profile *p;
2950 size_t list_size = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01002951
Johan Pascal253d0262020-09-22 13:04:45 +02002952 /* check the profiles list: all entry must be valid,
2953 * its size cannot be more than the total number of supported profiles, currently 4 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002954 for (p = profiles; *p != MBEDTLS_TLS_SRTP_UNSET &&
2955 list_size <= MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH;
2956 p++) {
2957 if (mbedtls_ssl_check_srtp_profile_value(*p) != MBEDTLS_TLS_SRTP_UNSET) {
Johan Pascal76fdf1d2020-10-22 23:31:00 +02002958 list_size++;
Gilles Peskine449bd832023-01-11 14:50:10 +01002959 } else {
Johan Pascal76fdf1d2020-10-22 23:31:00 +02002960 /* unsupported value, stop parsing and set the size to an error value */
2961 list_size = MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH + 1;
Johan Pascalb62bb512015-12-03 21:56:45 +01002962 }
2963 }
2964
Gilles Peskine449bd832023-01-11 14:50:10 +01002965 if (list_size > MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH) {
2966 conf->dtls_srtp_profile_list = NULL;
2967 conf->dtls_srtp_profile_list_len = 0;
2968 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Johan Pascal253d0262020-09-22 13:04:45 +02002969 }
2970
Johan Pascal9bc97ca2020-09-21 23:44:45 +02002971 conf->dtls_srtp_profile_list = profiles;
Johan Pascal253d0262020-09-22 13:04:45 +02002972 conf->dtls_srtp_profile_list_len = list_size;
Johan Pascalb62bb512015-12-03 21:56:45 +01002973
Gilles Peskine449bd832023-01-11 14:50:10 +01002974 return 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01002975}
2976
Gilles Peskine449bd832023-01-11 14:50:10 +01002977void mbedtls_ssl_get_dtls_srtp_negotiation_result(const mbedtls_ssl_context *ssl,
2978 mbedtls_dtls_srtp_info *dtls_srtp_info)
Johan Pascalb62bb512015-12-03 21:56:45 +01002979{
Johan Pascal2258a4f2020-10-28 13:53:09 +01002980 dtls_srtp_info->chosen_dtls_srtp_profile = ssl->dtls_srtp_info.chosen_dtls_srtp_profile;
2981 /* do not copy the mki value if there is no chosen profile */
Gilles Peskine449bd832023-01-11 14:50:10 +01002982 if (dtls_srtp_info->chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET) {
Johan Pascal2258a4f2020-10-28 13:53:09 +01002983 dtls_srtp_info->mki_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002984 } else {
Johan Pascal2258a4f2020-10-28 13:53:09 +01002985 dtls_srtp_info->mki_len = ssl->dtls_srtp_info.mki_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002986 memcpy(dtls_srtp_info->mki_value, ssl->dtls_srtp_info.mki_value,
2987 ssl->dtls_srtp_info.mki_len);
Johan Pascal2258a4f2020-10-28 13:53:09 +01002988 }
Johan Pascalb62bb512015-12-03 21:56:45 +01002989}
Johan Pascalb62bb512015-12-03 21:56:45 +01002990#endif /* MBEDTLS_SSL_DTLS_SRTP */
2991
Aditya Patwardhan3096f332022-07-26 14:31:46 +05302992#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002993void mbedtls_ssl_conf_max_version(mbedtls_ssl_config *conf, int major, int minor)
Paul Bakker490ecc82011-10-06 13:04:09 +00002994{
Glenn Strauss2dfcea22022-03-14 17:26:42 -04002995 conf->max_tls_version = (major << 8) | minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00002996}
2997
Gilles Peskine449bd832023-01-11 14:50:10 +01002998void mbedtls_ssl_conf_min_version(mbedtls_ssl_config *conf, int major, int minor)
Paul Bakker1d29fb52012-09-28 13:28:45 +00002999{
Glenn Strauss2dfcea22022-03-14 17:26:42 -04003000 conf->min_tls_version = (major << 8) | minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00003001}
Aditya Patwardhan3096f332022-07-26 14:31:46 +05303002#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker1d29fb52012-09-28 13:28:45 +00003003
Janos Follath088ce432017-04-10 12:42:31 +01003004#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003005void mbedtls_ssl_conf_cert_req_ca_list(mbedtls_ssl_config *conf,
3006 char cert_req_ca_list)
Janos Follath088ce432017-04-10 12:42:31 +01003007{
3008 conf->cert_req_ca_list = cert_req_ca_list;
3009}
3010#endif
3011
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003012#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01003013void mbedtls_ssl_conf_encrypt_then_mac(mbedtls_ssl_config *conf, char etm)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01003014{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003015 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01003016}
3017#endif
3018
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003019#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Gilles Peskine449bd832023-01-11 14:50:10 +01003020void mbedtls_ssl_conf_extended_master_secret(mbedtls_ssl_config *conf, char ems)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02003021{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003022 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02003023}
3024#endif
3025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003026#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01003027int mbedtls_ssl_conf_max_frag_len(mbedtls_ssl_config *conf, unsigned char mfl_code)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003028{
Gilles Peskine449bd832023-01-11 14:50:10 +01003029 if (mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
3030 ssl_mfl_code_to_length(mfl_code) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN) {
3031 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003032 }
3033
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01003034 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003035
Gilles Peskine449bd832023-01-11 14:50:10 +01003036 return 0;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003037}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003038#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003039
Gilles Peskine449bd832023-01-11 14:50:10 +01003040void mbedtls_ssl_conf_legacy_renegotiation(mbedtls_ssl_config *conf, int allow_legacy)
Paul Bakker48916f92012-09-16 19:57:18 +00003041{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003042 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00003043}
3044
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003045#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01003046void mbedtls_ssl_conf_renegotiation(mbedtls_ssl_config *conf, int renegotiation)
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003047{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003048 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003049}
3050
Gilles Peskine449bd832023-01-11 14:50:10 +01003051void mbedtls_ssl_conf_renegotiation_enforced(mbedtls_ssl_config *conf, int max_records)
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003052{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003053 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003054}
3055
Gilles Peskine449bd832023-01-11 14:50:10 +01003056void mbedtls_ssl_conf_renegotiation_period(mbedtls_ssl_config *conf,
3057 const unsigned char period[8])
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01003058{
Gilles Peskine449bd832023-01-11 14:50:10 +01003059 memcpy(conf->renego_period, period, 8);
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01003060}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003061#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00003062
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003063#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003064#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003065void mbedtls_ssl_conf_session_tickets(mbedtls_ssl_config *conf, int use_tickets)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003066{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01003067 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003068}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003069#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02003070
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003071#if defined(MBEDTLS_SSL_SRV_C)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08003072
Jerry Yud0766ec2022-09-22 10:46:57 +08003073#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003074void mbedtls_ssl_conf_new_session_tickets(mbedtls_ssl_config *conf,
3075 uint16_t num_tickets)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08003076{
Jerry Yud0766ec2022-09-22 10:46:57 +08003077 conf->new_session_tickets_count = num_tickets;
Jerry Yu1ad7ace2022-08-09 13:28:39 +08003078}
3079#endif
3080
Gilles Peskine449bd832023-01-11 14:50:10 +01003081void mbedtls_ssl_conf_session_tickets_cb(mbedtls_ssl_config *conf,
3082 mbedtls_ssl_ticket_write_t *f_ticket_write,
3083 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
3084 void *p_ticket)
Paul Bakker606b4ba2013-08-14 16:52:14 +02003085{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02003086 conf->f_ticket_write = f_ticket_write;
3087 conf->f_ticket_parse = f_ticket_parse;
3088 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02003089}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003090#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003091#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003092
Gilles Peskine449bd832023-01-11 14:50:10 +01003093void mbedtls_ssl_set_export_keys_cb(mbedtls_ssl_context *ssl,
3094 mbedtls_ssl_export_keys_t *f_export_keys,
3095 void *p_export_keys)
Robert Cragie4feb7ae2015-10-02 13:33:37 +01003096{
Hanno Becker7e6c1782021-06-08 09:24:55 +01003097 ssl->f_export_keys = f_export_keys;
3098 ssl->p_export_keys = p_export_keys;
Ron Eldorf5cc10d2019-05-07 18:33:40 +03003099}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01003100
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003101#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003102void mbedtls_ssl_conf_async_private_cb(
3103 mbedtls_ssl_config *conf,
3104 mbedtls_ssl_async_sign_t *f_async_sign,
3105 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
3106 mbedtls_ssl_async_resume_t *f_async_resume,
3107 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskine449bd832023-01-11 14:50:10 +01003108 void *async_config_data)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003109{
3110 conf->f_async_sign_start = f_async_sign;
3111 conf->f_async_decrypt_start = f_async_decrypt;
3112 conf->f_async_resume = f_async_resume;
3113 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003114 conf->p_async_config_data = async_config_data;
3115}
3116
Gilles Peskine449bd832023-01-11 14:50:10 +01003117void *mbedtls_ssl_conf_get_async_config_data(const mbedtls_ssl_config *conf)
Gilles Peskine8f97af72018-04-26 11:46:10 +02003118{
Gilles Peskine449bd832023-01-11 14:50:10 +01003119 return conf->p_async_config_data;
Gilles Peskine8f97af72018-04-26 11:46:10 +02003120}
3121
Gilles Peskine449bd832023-01-11 14:50:10 +01003122void *mbedtls_ssl_get_async_operation_data(const mbedtls_ssl_context *ssl)
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003123{
Gilles Peskine449bd832023-01-11 14:50:10 +01003124 if (ssl->handshake == NULL) {
3125 return NULL;
3126 } else {
3127 return ssl->handshake->user_async_ctx;
3128 }
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003129}
3130
Gilles Peskine449bd832023-01-11 14:50:10 +01003131void mbedtls_ssl_set_async_operation_data(mbedtls_ssl_context *ssl,
3132 void *ctx)
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003133{
Gilles Peskine449bd832023-01-11 14:50:10 +01003134 if (ssl->handshake != NULL) {
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003135 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +01003136 }
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003137}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003138#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003139
Paul Bakker5121ce52009-01-03 21:22:43 +00003140/*
3141 * SSL get accessors
3142 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003143uint32_t mbedtls_ssl_get_verify_result(const mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00003144{
Gilles Peskine449bd832023-01-11 14:50:10 +01003145 if (ssl->session != NULL) {
3146 return ssl->session->verify_result;
3147 }
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00003148
Gilles Peskine449bd832023-01-11 14:50:10 +01003149 if (ssl->session_negotiate != NULL) {
3150 return ssl->session_negotiate->verify_result;
3151 }
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00003152
Gilles Peskine449bd832023-01-11 14:50:10 +01003153 return 0xFFFFFFFF;
Paul Bakker5121ce52009-01-03 21:22:43 +00003154}
3155
Gilles Peskine449bd832023-01-11 14:50:10 +01003156int mbedtls_ssl_get_ciphersuite_id_from_ssl(const mbedtls_ssl_context *ssl)
Glenn Strauss8f526902022-01-13 00:04:49 -05003157{
Gilles Peskine449bd832023-01-11 14:50:10 +01003158 if (ssl == NULL || ssl->session == NULL) {
3159 return 0;
3160 }
Glenn Strauss8f526902022-01-13 00:04:49 -05003161
Gilles Peskine449bd832023-01-11 14:50:10 +01003162 return ssl->session->ciphersuite;
Glenn Strauss8f526902022-01-13 00:04:49 -05003163}
3164
Gilles Peskine449bd832023-01-11 14:50:10 +01003165const char *mbedtls_ssl_get_ciphersuite(const mbedtls_ssl_context *ssl)
Paul Bakker72f62662011-01-16 21:27:44 +00003166{
Gilles Peskine449bd832023-01-11 14:50:10 +01003167 if (ssl == NULL || ssl->session == NULL) {
3168 return NULL;
3169 }
Paul Bakker926c8e42013-03-06 10:23:34 +01003170
Gilles Peskine449bd832023-01-11 14:50:10 +01003171 return mbedtls_ssl_get_ciphersuite_name(ssl->session->ciphersuite);
Paul Bakker72f62662011-01-16 21:27:44 +00003172}
3173
Gilles Peskine449bd832023-01-11 14:50:10 +01003174const char *mbedtls_ssl_get_version(const mbedtls_ssl_context *ssl)
Paul Bakker43ca69c2011-01-15 17:35:19 +00003175{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003176#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003177 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
3178 switch (ssl->tls_version) {
Glenn Strauss60bfe602022-03-14 19:04:24 -04003179 case MBEDTLS_SSL_VERSION_TLS1_2:
Gilles Peskine449bd832023-01-11 14:50:10 +01003180 return "DTLSv1.2";
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003181 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01003182 return "unknown (DTLS)";
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003183 }
3184 }
3185#endif
3186
Gilles Peskine449bd832023-01-11 14:50:10 +01003187 switch (ssl->tls_version) {
Glenn Strauss60bfe602022-03-14 19:04:24 -04003188 case MBEDTLS_SSL_VERSION_TLS1_2:
Gilles Peskine449bd832023-01-11 14:50:10 +01003189 return "TLSv1.2";
Glenn Strauss60bfe602022-03-14 19:04:24 -04003190 case MBEDTLS_SSL_VERSION_TLS1_3:
Gilles Peskine449bd832023-01-11 14:50:10 +01003191 return "TLSv1.3";
Paul Bakker43ca69c2011-01-15 17:35:19 +00003192 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01003193 return "unknown";
Paul Bakker43ca69c2011-01-15 17:35:19 +00003194 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00003195}
3196
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003197#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01003198size_t mbedtls_ssl_get_input_max_frag_len(const mbedtls_ssl_context *ssl)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003199{
David Horstmann95d516f2021-05-04 18:36:56 +01003200 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003201 size_t read_mfl;
3202
Jerry Yuddda0502022-12-01 19:43:12 +08003203#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003204 /* Use the configured MFL for the client if we're past SERVER_HELLO_DONE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003205 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
3206 ssl->state >= MBEDTLS_SSL_SERVER_HELLO_DONE) {
3207 return ssl_mfl_code_to_length(ssl->conf->mfl_code);
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003208 }
Jerry Yuddda0502022-12-01 19:43:12 +08003209#endif
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003210
3211 /* Check if a smaller max length was negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01003212 if (ssl->session_out != NULL) {
3213 read_mfl = ssl_mfl_code_to_length(ssl->session_out->mfl_code);
3214 if (read_mfl < max_len) {
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003215 max_len = read_mfl;
3216 }
3217 }
3218
Jerry Yuddda0502022-12-01 19:43:12 +08003219 /* During a handshake, use the value being negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01003220 if (ssl->session_negotiate != NULL) {
3221 read_mfl = ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code);
3222 if (read_mfl < max_len) {
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003223 max_len = read_mfl;
3224 }
3225 }
3226
Gilles Peskine449bd832023-01-11 14:50:10 +01003227 return max_len;
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003228}
3229
Gilles Peskine449bd832023-01-11 14:50:10 +01003230size_t mbedtls_ssl_get_output_max_frag_len(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003231{
3232 size_t max_len;
3233
3234 /*
3235 * Assume mfl_code is correct since it was checked when set
3236 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003237 max_len = ssl_mfl_code_to_length(ssl->conf->mfl_code);
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003238
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02003239 /* Check if a smaller max length was negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01003240 if (ssl->session_out != NULL &&
3241 ssl_mfl_code_to_length(ssl->session_out->mfl_code) < max_len) {
3242 max_len = ssl_mfl_code_to_length(ssl->session_out->mfl_code);
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003243 }
3244
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02003245 /* During a handshake, use the value being negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01003246 if (ssl->session_negotiate != NULL &&
3247 ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code) < max_len) {
3248 max_len = ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code);
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02003249 }
3250
Gilles Peskine449bd832023-01-11 14:50:10 +01003251 return max_len;
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003252}
3253#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
3254
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003255#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003256size_t mbedtls_ssl_get_current_mtu(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003257{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04003258 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003259 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
3260 (ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
3261 ssl->state == MBEDTLS_SSL_SERVER_HELLO)) {
3262 return 0;
3263 }
Andrzej Kurekef43ce62018-10-09 08:24:12 -04003264
Gilles Peskine449bd832023-01-11 14:50:10 +01003265 if (ssl->handshake == NULL || ssl->handshake->mtu == 0) {
3266 return ssl->mtu;
3267 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003268
Gilles Peskine449bd832023-01-11 14:50:10 +01003269 if (ssl->mtu == 0) {
3270 return ssl->handshake->mtu;
3271 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003272
Gilles Peskine449bd832023-01-11 14:50:10 +01003273 return ssl->mtu < ssl->handshake->mtu ?
3274 ssl->mtu : ssl->handshake->mtu;
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003275}
3276#endif /* MBEDTLS_SSL_PROTO_DTLS */
3277
Gilles Peskine449bd832023-01-11 14:50:10 +01003278int mbedtls_ssl_get_max_out_record_payload(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003279{
3280 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
3281
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02003282#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
3283 !defined(MBEDTLS_SSL_PROTO_DTLS)
3284 (void) ssl;
3285#endif
3286
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003287#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01003288 const size_t mfl = mbedtls_ssl_get_output_max_frag_len(ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003289
Gilles Peskine449bd832023-01-11 14:50:10 +01003290 if (max_len > mfl) {
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003291 max_len = mfl;
Gilles Peskine449bd832023-01-11 14:50:10 +01003292 }
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003293#endif
3294
3295#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003296 if (mbedtls_ssl_get_current_mtu(ssl) != 0) {
3297 const size_t mtu = mbedtls_ssl_get_current_mtu(ssl);
3298 const int ret = mbedtls_ssl_get_record_expansion(ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003299 const size_t overhead = (size_t) ret;
3300
Gilles Peskine449bd832023-01-11 14:50:10 +01003301 if (ret < 0) {
3302 return ret;
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003303 }
3304
Gilles Peskine449bd832023-01-11 14:50:10 +01003305 if (mtu <= overhead) {
3306 MBEDTLS_SSL_DEBUG_MSG(1, ("MTU too low for record expansion"));
3307 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
3308 }
3309
3310 if (max_len > mtu - overhead) {
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003311 max_len = mtu - overhead;
Gilles Peskine449bd832023-01-11 14:50:10 +01003312 }
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003313 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003314#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003315
Hanno Becker0defedb2018-08-10 12:35:02 +01003316#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
3317 !defined(MBEDTLS_SSL_PROTO_DTLS)
3318 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003319#endif
3320
Gilles Peskine449bd832023-01-11 14:50:10 +01003321 return (int) max_len;
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003322}
3323
Gilles Peskine449bd832023-01-11 14:50:10 +01003324int mbedtls_ssl_get_max_in_record_payload(const mbedtls_ssl_context *ssl)
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003325{
3326 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
3327
3328#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
3329 (void) ssl;
3330#endif
3331
3332#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01003333 const size_t mfl = mbedtls_ssl_get_input_max_frag_len(ssl);
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003334
Gilles Peskine449bd832023-01-11 14:50:10 +01003335 if (max_len > mfl) {
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003336 max_len = mfl;
Gilles Peskine449bd832023-01-11 14:50:10 +01003337 }
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003338#endif
3339
Gilles Peskine449bd832023-01-11 14:50:10 +01003340 return (int) max_len;
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003341}
3342
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003343#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003344const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert(const mbedtls_ssl_context *ssl)
Paul Bakkerb0550d92012-10-30 07:51:03 +00003345{
Gilles Peskine449bd832023-01-11 14:50:10 +01003346 if (ssl == NULL || ssl->session == NULL) {
3347 return NULL;
3348 }
Paul Bakkerb0550d92012-10-30 07:51:03 +00003349
Hanno Beckere6824572019-02-07 13:18:46 +00003350#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01003351 return ssl->session->peer_cert;
Hanno Beckere6824572019-02-07 13:18:46 +00003352#else
Gilles Peskine449bd832023-01-11 14:50:10 +01003353 return NULL;
Hanno Beckere6824572019-02-07 13:18:46 +00003354#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00003355}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003356#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00003357
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003358#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003359int mbedtls_ssl_get_session(const mbedtls_ssl_context *ssl,
3360 mbedtls_ssl_session *dst)
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003361{
Hanno Beckere810bbc2021-05-14 16:01:05 +01003362 int ret;
3363
Gilles Peskine449bd832023-01-11 14:50:10 +01003364 if (ssl == NULL ||
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003365 dst == NULL ||
3366 ssl->session == NULL ||
Gilles Peskine449bd832023-01-11 14:50:10 +01003367 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT) {
3368 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003369 }
3370
Hanno Beckere810bbc2021-05-14 16:01:05 +01003371 /* Since Mbed TLS 3.0, mbedtls_ssl_get_session() is no longer
3372 * idempotent: Each session can only be exported once.
3373 *
3374 * (This is in preparation for TLS 1.3 support where we will
3375 * need the ability to export multiple sessions (aka tickets),
3376 * which will be achieved by calling mbedtls_ssl_get_session()
3377 * multiple times until it fails.)
3378 *
3379 * Check whether we have already exported the current session,
3380 * and fail if so.
3381 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003382 if (ssl->session->exported == 1) {
3383 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
3384 }
Hanno Beckere810bbc2021-05-14 16:01:05 +01003385
Gilles Peskine449bd832023-01-11 14:50:10 +01003386 ret = mbedtls_ssl_session_copy(dst, ssl->session);
3387 if (ret != 0) {
3388 return ret;
3389 }
Hanno Beckere810bbc2021-05-14 16:01:05 +01003390
3391 /* Remember that we've exported the session. */
3392 ssl->session->exported = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003393 return 0;
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003394}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003395#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003396
Paul Bakker5121ce52009-01-03 21:22:43 +00003397/*
Hanno Beckera835da52019-05-16 12:39:07 +01003398 * Define ticket header determining Mbed TLS version
3399 * and structure of the ticket.
3400 */
3401
Hanno Becker94ef3b32019-05-16 12:50:45 +01003402/*
Hanno Becker50b59662019-05-28 14:30:45 +01003403 * Define bitflag determining compile-time settings influencing
3404 * structure of serialized SSL sessions.
Hanno Becker94ef3b32019-05-16 12:50:45 +01003405 */
3406
Hanno Becker50b59662019-05-28 14:30:45 +01003407#if defined(MBEDTLS_HAVE_TIME)
Hanno Becker3e088662019-05-29 11:10:18 +01003408#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker50b59662019-05-28 14:30:45 +01003409#else
Hanno Becker3e088662019-05-29 11:10:18 +01003410#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003411#endif /* MBEDTLS_HAVE_TIME */
3412
3413#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker3e088662019-05-29 11:10:18 +01003414#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003415#else
Hanno Becker3e088662019-05-29 11:10:18 +01003416#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003417#endif /* MBEDTLS_X509_CRT_PARSE_C */
3418
3419#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Becker3e088662019-05-29 11:10:18 +01003420#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003421#else
Hanno Becker3e088662019-05-29 11:10:18 +01003422#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003423#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
3424
3425#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Becker3e088662019-05-29 11:10:18 +01003426#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003427#else
Hanno Becker3e088662019-05-29 11:10:18 +01003428#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003429#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
3430
Hanno Becker94ef3b32019-05-16 12:50:45 +01003431#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3e088662019-05-29 11:10:18 +01003432#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003433#else
Hanno Becker3e088662019-05-29 11:10:18 +01003434#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003435#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
3436
Hanno Becker94ef3b32019-05-16 12:50:45 +01003437#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3438#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
3439#else
3440#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
3441#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3442
Hanno Becker3e088662019-05-29 11:10:18 +01003443#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
3444#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
3445#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
3446#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
Hanno Becker37bdbe62021-08-01 05:38:58 +01003447#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 4
3448#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 5
Hanno Becker3e088662019-05-29 11:10:18 +01003449
Hanno Becker50b59662019-05-28 14:30:45 +01003450#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Gilles Peskine449bd832023-01-11 14:50:10 +01003451 ((uint16_t) ( \
3452 (SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT) | \
3453 (SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT) | \
3454 (SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << \
3455 SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT) | \
3456 (SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT) | \
3457 (SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT) | \
3458 (SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT)))
Hanno Becker94ef3b32019-05-16 12:50:45 +01003459
Hanno Beckerf8787072019-05-16 12:41:07 +01003460static unsigned char ssl_serialized_session_header[] = {
Hanno Becker94ef3b32019-05-16 12:50:45 +01003461 MBEDTLS_VERSION_MAJOR,
3462 MBEDTLS_VERSION_MINOR,
3463 MBEDTLS_VERSION_PATCH,
Gilles Peskine449bd832023-01-11 14:50:10 +01003464 MBEDTLS_BYTE_1(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
3465 MBEDTLS_BYTE_0(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
Hanno Beckerf8787072019-05-16 12:41:07 +01003466};
Hanno Beckera835da52019-05-16 12:39:07 +01003467
3468/*
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003469 * Serialize a session in the following format:
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02003470 * (in the presentation language of TLS, RFC 8446 section 3)
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003471 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003472 * struct {
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01003473 *
Hanno Beckerdce50972021-08-01 05:39:23 +01003474 * opaque mbedtls_version[3]; // library version: major, minor, patch
3475 * opaque session_format[2]; // library-version specific 16-bit field
3476 * // determining the format of the remaining
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003477 * // serialized data.
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01003478 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003479 * Note: When updating the format, remember to keep
3480 * these version+format bytes.
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003481 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003482 * // In this version, `session_format` determines
3483 * // the setting of those compile-time
3484 * // configuration options which influence
3485 * // the structure of mbedtls_ssl_session.
3486 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003487 * uint8_t minor_ver; // Protocol minor version. Possible values:
Jerry Yu0c2a7382022-12-06 13:27:25 +08003488 * // - TLS 1.2 (0x0303)
3489 * // - TLS 1.3 (0x0304)
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003490 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003491 * select (serialized_session.tls_version) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003492 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003493 * case MBEDTLS_SSL_VERSION_TLS1_2:
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003494 * serialized_session_tls12 data;
Jerry Yu0c2a7382022-12-06 13:27:25 +08003495 * case MBEDTLS_SSL_VERSION_TLS1_3:
Jerry Yuddda0502022-12-01 19:43:12 +08003496 * serialized_session_tls13 data;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003497 *
3498 * };
3499 *
3500 * } serialized_session;
3501 *
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003502 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003503
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003504MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003505static int ssl_session_save(const mbedtls_ssl_session *session,
3506 unsigned char omit_header,
3507 unsigned char *buf,
3508 size_t buf_len,
3509 size_t *olen)
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003510{
3511 unsigned char *p = buf;
3512 size_t used = 0;
Jerry Yu251a12e2022-07-13 15:15:48 +08003513 size_t remaining_len;
Jerry Yue36fdd62022-08-17 21:31:36 +08003514#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3515 size_t out_len;
3516 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3517#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01003518 if (session == NULL) {
3519 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
3520 }
Jerry Yu438ddd82022-07-07 06:55:50 +00003521
Gilles Peskine449bd832023-01-11 14:50:10 +01003522 if (!omit_header) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003523 /*
3524 * Add Mbed TLS version identifier
3525 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003526 used += sizeof(ssl_serialized_session_header);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003527
Gilles Peskine449bd832023-01-11 14:50:10 +01003528 if (used <= buf_len) {
3529 memcpy(p, ssl_serialized_session_header,
3530 sizeof(ssl_serialized_session_header));
3531 p += sizeof(ssl_serialized_session_header);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003532 }
3533 }
3534
3535 /*
3536 * TLS version identifier
3537 */
3538 used += 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003539 if (used <= buf_len) {
3540 *p++ = MBEDTLS_BYTE_0(session->tls_version);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003541 }
3542
3543 /* Forward to version-specific serialization routine. */
Jerry Yufca4d572022-07-21 10:37:48 +08003544 remaining_len = (buf_len >= used) ? buf_len - used : 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003545 switch (session->tls_version) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003546#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01003547 case MBEDTLS_SSL_VERSION_TLS1_2:
3548 used += ssl_tls12_session_save(session, p, remaining_len);
3549 break;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003550#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3551
Jerry Yu251a12e2022-07-13 15:15:48 +08003552#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01003553 case MBEDTLS_SSL_VERSION_TLS1_3:
3554 ret = ssl_tls13_session_save(session, p, remaining_len, &out_len);
3555 if (ret != 0 && ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL) {
3556 return ret;
3557 }
3558 used += out_len;
3559 break;
Jerry Yu251a12e2022-07-13 15:15:48 +08003560#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3561
Gilles Peskine449bd832023-01-11 14:50:10 +01003562 default:
3563 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003564 }
3565
3566 *olen = used;
Gilles Peskine449bd832023-01-11 14:50:10 +01003567 if (used > buf_len) {
3568 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
3569 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003570
Gilles Peskine449bd832023-01-11 14:50:10 +01003571 return 0;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003572}
3573
3574/*
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003575 * Public wrapper for ssl_session_save()
3576 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003577int mbedtls_ssl_session_save(const mbedtls_ssl_session *session,
3578 unsigned char *buf,
3579 size_t buf_len,
3580 size_t *olen)
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003581{
Gilles Peskine449bd832023-01-11 14:50:10 +01003582 return ssl_session_save(session, 0, buf, buf_len, olen);
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003583}
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003584
Jerry Yuf1b23ca2022-02-18 11:48:47 +08003585/*
3586 * Deserialize session, see mbedtls_ssl_session_save() for format.
3587 *
3588 * This internal version is wrapped by a public function that cleans up in
3589 * case of error, and has an extra option omit_header.
3590 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003591MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003592static int ssl_session_load(mbedtls_ssl_session *session,
3593 unsigned char omit_header,
3594 const unsigned char *buf,
3595 size_t len)
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003596{
3597 const unsigned char *p = buf;
3598 const unsigned char * const end = buf + len;
Jerry Yu438ddd82022-07-07 06:55:50 +00003599 size_t remaining_len;
3600
3601
Gilles Peskine449bd832023-01-11 14:50:10 +01003602 if (session == NULL) {
3603 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
3604 }
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003605
Gilles Peskine449bd832023-01-11 14:50:10 +01003606 if (!omit_header) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003607 /*
3608 * Check Mbed TLS version identifier
3609 */
3610
Gilles Peskine449bd832023-01-11 14:50:10 +01003611 if ((size_t) (end - p) < sizeof(ssl_serialized_session_header)) {
3612 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003613 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003614
3615 if (memcmp(p, ssl_serialized_session_header,
3616 sizeof(ssl_serialized_session_header)) != 0) {
3617 return MBEDTLS_ERR_SSL_VERSION_MISMATCH;
3618 }
3619 p += sizeof(ssl_serialized_session_header);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003620 }
3621
3622 /*
3623 * TLS version identifier
3624 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003625 if (1 > (size_t) (end - p)) {
3626 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3627 }
Glenn Straussda7851c2022-03-14 13:29:48 -04003628 session->tls_version = 0x0300 | *p++;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003629
3630 /* Dispatch according to TLS version. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003631 remaining_len = (end - p);
3632 switch (session->tls_version) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003633#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01003634 case MBEDTLS_SSL_VERSION_TLS1_2:
3635 return ssl_tls12_session_load(session, p, remaining_len);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003636#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3637
Jerry Yu438ddd82022-07-07 06:55:50 +00003638#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01003639 case MBEDTLS_SSL_VERSION_TLS1_3:
3640 return ssl_tls13_session_load(session, p, remaining_len);
Jerry Yu438ddd82022-07-07 06:55:50 +00003641#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3642
Gilles Peskine449bd832023-01-11 14:50:10 +01003643 default:
3644 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003645 }
3646}
3647
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003648/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02003649 * Deserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003650 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003651int mbedtls_ssl_session_load(mbedtls_ssl_session *session,
3652 const unsigned char *buf,
3653 size_t len)
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003654{
Gilles Peskine449bd832023-01-11 14:50:10 +01003655 int ret = ssl_session_load(session, 0, buf, len);
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003656
Gilles Peskine449bd832023-01-11 14:50:10 +01003657 if (ret != 0) {
3658 mbedtls_ssl_session_free(session);
3659 }
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003660
Gilles Peskine449bd832023-01-11 14:50:10 +01003661 return ret;
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003662}
3663
3664/*
Paul Bakker1961b702013-01-25 14:49:24 +01003665 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00003666 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003667MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003668static int ssl_prepare_handshake_step(mbedtls_ssl_context *ssl)
Hanno Becker41934dd2021-08-07 19:13:43 +01003669{
3670 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3671
Ronald Cron66dbf912022-02-02 15:33:46 +01003672 /*
3673 * We may have not been able to send to the peer all the handshake data
Ronald Cron3f20b772022-03-08 16:00:02 +01003674 * that were written into the output buffer by the previous handshake step,
3675 * if the write to the network callback returned with the
Ronald Cron66dbf912022-02-02 15:33:46 +01003676 * #MBEDTLS_ERR_SSL_WANT_WRITE error code.
3677 * We proceed to the next handshake step only when all data from the
3678 * previous one have been sent to the peer, thus we make sure that this is
3679 * the case here by calling `mbedtls_ssl_flush_output()`. The function may
3680 * return with the #MBEDTLS_ERR_SSL_WANT_WRITE error code in which case
3681 * we have to wait before to go ahead.
3682 * In the case of TLS 1.3, handshake step handlers do not send data to the
3683 * peer. Data are only sent here and through
3684 * `mbedtls_ssl_handle_pending_alert` in case an error that triggered an
Andrzej Kurek5c65c572022-04-13 14:28:52 -04003685 * alert occurred.
Ronald Cron66dbf912022-02-02 15:33:46 +01003686 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003687 if ((ret = mbedtls_ssl_flush_output(ssl)) != 0) {
3688 return ret;
3689 }
Hanno Becker41934dd2021-08-07 19:13:43 +01003690
3691#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003692 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3693 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING) {
3694 if ((ret = mbedtls_ssl_flight_transmit(ssl)) != 0) {
3695 return ret;
3696 }
Hanno Becker41934dd2021-08-07 19:13:43 +01003697 }
3698#endif /* MBEDTLS_SSL_PROTO_DTLS */
3699
Gilles Peskine449bd832023-01-11 14:50:10 +01003700 return ret;
Hanno Becker41934dd2021-08-07 19:13:43 +01003701}
3702
Gilles Peskine449bd832023-01-11 14:50:10 +01003703int mbedtls_ssl_handshake_step(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00003704{
Hanno Becker41934dd2021-08-07 19:13:43 +01003705 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003706
Gilles Peskine449bd832023-01-11 14:50:10 +01003707 if (ssl == NULL ||
Hanno Becker41934dd2021-08-07 19:13:43 +01003708 ssl->conf == NULL ||
3709 ssl->handshake == NULL ||
Gilles Peskine449bd832023-01-11 14:50:10 +01003710 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER) {
3711 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Becker41934dd2021-08-07 19:13:43 +01003712 }
3713
Gilles Peskine449bd832023-01-11 14:50:10 +01003714 ret = ssl_prepare_handshake_step(ssl);
3715 if (ret != 0) {
3716 return ret;
3717 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003718
Gilles Peskine449bd832023-01-11 14:50:10 +01003719 ret = mbedtls_ssl_handle_pending_alert(ssl);
3720 if (ret != 0) {
Jerry Yue7047812021-09-13 19:26:39 +08003721 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01003722 }
Jerry Yue7047812021-09-13 19:26:39 +08003723
Tom Cosgrove2fdc7b32022-09-21 12:33:17 +01003724 /* If ssl->conf->endpoint is not one of MBEDTLS_SSL_IS_CLIENT or
3725 * MBEDTLS_SSL_IS_SERVER, this is the return code we give */
3726 ret = MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3727
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003728#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003729 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
3730 MBEDTLS_SSL_DEBUG_MSG(2, ("client state: %s",
3731 mbedtls_ssl_states_str(ssl->state)));
Jerry Yub9930e72021-08-06 17:11:51 +08003732
Gilles Peskine449bd832023-01-11 14:50:10 +01003733 switch (ssl->state) {
Ronald Cron9f0fba32022-02-10 16:45:15 +01003734 case MBEDTLS_SSL_HELLO_REQUEST:
3735 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
Tom Cosgrove87d9c6c2022-09-22 09:27:56 +01003736 ret = 0;
Ronald Cron9f0fba32022-02-10 16:45:15 +01003737 break;
Jerry Yub9930e72021-08-06 17:11:51 +08003738
Ronald Cron9f0fba32022-02-10 16:45:15 +01003739 case MBEDTLS_SSL_CLIENT_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01003740 ret = mbedtls_ssl_write_client_hello(ssl);
Ronald Cron9f0fba32022-02-10 16:45:15 +01003741 break;
3742
3743 default:
3744#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01003745 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
3746 ret = mbedtls_ssl_tls13_handshake_client_step(ssl);
3747 } else {
3748 ret = mbedtls_ssl_handshake_client_step(ssl);
3749 }
Ronald Cron9f0fba32022-02-10 16:45:15 +01003750#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01003751 ret = mbedtls_ssl_handshake_client_step(ssl);
Ronald Cron9f0fba32022-02-10 16:45:15 +01003752#else
Gilles Peskine449bd832023-01-11 14:50:10 +01003753 ret = mbedtls_ssl_tls13_handshake_client_step(ssl);
Ronald Cron9f0fba32022-02-10 16:45:15 +01003754#endif
3755 }
Jerry Yub9930e72021-08-06 17:11:51 +08003756 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003757#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003758#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003759 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Ronald Cron6f135e12021-12-08 16:57:54 +01003760#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01003761 if (mbedtls_ssl_conf_is_tls13_only(ssl->conf)) {
3762 ret = mbedtls_ssl_tls13_handshake_server_step(ssl);
3763 }
Ronald Cron6f135e12021-12-08 16:57:54 +01003764#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yub9930e72021-08-06 17:11:51 +08003765
3766#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01003767 if (mbedtls_ssl_conf_is_tls12_only(ssl->conf)) {
3768 ret = mbedtls_ssl_handshake_server_step(ssl);
3769 }
Jerry Yub9930e72021-08-06 17:11:51 +08003770#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3771 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003772#endif
3773
Gilles Peskine449bd832023-01-11 14:50:10 +01003774 if (ret != 0) {
Jerry Yubbd5a3f2021-09-18 20:50:22 +08003775 /* handshake_step return error. And it is same
3776 * with alert_reason.
3777 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003778 if (ssl->send_alert) {
3779 ret = mbedtls_ssl_handle_pending_alert(ssl);
Jerry Yue7047812021-09-13 19:26:39 +08003780 goto cleanup;
3781 }
3782 }
3783
3784cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01003785 return ret;
Paul Bakker1961b702013-01-25 14:49:24 +01003786}
3787
3788/*
3789 * Perform the SSL handshake
3790 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003791int mbedtls_ssl_handshake(mbedtls_ssl_context *ssl)
Paul Bakker1961b702013-01-25 14:49:24 +01003792{
3793 int ret = 0;
3794
Hanno Beckera817ea42020-10-20 15:20:23 +01003795 /* Sanity checks */
3796
Gilles Peskine449bd832023-01-11 14:50:10 +01003797 if (ssl == NULL || ssl->conf == NULL) {
3798 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3799 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003800
Hanno Beckera817ea42020-10-20 15:20:23 +01003801#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003802 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3803 (ssl->f_set_timer == NULL || ssl->f_get_timer == NULL)) {
3804 MBEDTLS_SSL_DEBUG_MSG(1, ("You must use "
3805 "mbedtls_ssl_set_timer_cb() for DTLS"));
3806 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Beckera817ea42020-10-20 15:20:23 +01003807 }
3808#endif /* MBEDTLS_SSL_PROTO_DTLS */
3809
Gilles Peskine449bd832023-01-11 14:50:10 +01003810 MBEDTLS_SSL_DEBUG_MSG(2, ("=> handshake"));
Paul Bakker1961b702013-01-25 14:49:24 +01003811
Hanno Beckera817ea42020-10-20 15:20:23 +01003812 /* Main handshake loop */
Gilles Peskine449bd832023-01-11 14:50:10 +01003813 while (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER) {
3814 ret = mbedtls_ssl_handshake_step(ssl);
Paul Bakker1961b702013-01-25 14:49:24 +01003815
Gilles Peskine449bd832023-01-11 14:50:10 +01003816 if (ret != 0) {
Paul Bakker1961b702013-01-25 14:49:24 +01003817 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01003818 }
Paul Bakker1961b702013-01-25 14:49:24 +01003819 }
3820
Gilles Peskine449bd832023-01-11 14:50:10 +01003821 MBEDTLS_SSL_DEBUG_MSG(2, ("<= handshake"));
Paul Bakker5121ce52009-01-03 21:22:43 +00003822
Gilles Peskine449bd832023-01-11 14:50:10 +01003823 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003824}
3825
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003826#if defined(MBEDTLS_SSL_RENEGOTIATION)
3827#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003828/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003829 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00003830 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003831MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003832static int ssl_write_hello_request(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003833{
Janos Follath865b3eb2019-12-16 11:46:15 +00003834 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003835
Gilles Peskine449bd832023-01-11 14:50:10 +01003836 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write hello request"));
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003837
3838 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003839 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3840 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003841
Gilles Peskine449bd832023-01-11 14:50:10 +01003842 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
3843 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
3844 return ret;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003845 }
3846
Gilles Peskine449bd832023-01-11 14:50:10 +01003847 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write hello request"));
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003848
Gilles Peskine449bd832023-01-11 14:50:10 +01003849 return 0;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003850}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003851#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003852
3853/*
3854 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003855 * - any side: calling mbedtls_ssl_renegotiate(),
3856 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
3857 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02003858 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003859 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003860 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003861 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003862int mbedtls_ssl_start_renegotiation(mbedtls_ssl_context *ssl)
Paul Bakker48916f92012-09-16 19:57:18 +00003863{
Janos Follath865b3eb2019-12-16 11:46:15 +00003864 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker48916f92012-09-16 19:57:18 +00003865
Gilles Peskine449bd832023-01-11 14:50:10 +01003866 MBEDTLS_SSL_DEBUG_MSG(2, ("=> renegotiate"));
Paul Bakker48916f92012-09-16 19:57:18 +00003867
Gilles Peskine449bd832023-01-11 14:50:10 +01003868 if ((ret = ssl_handshake_init(ssl)) != 0) {
3869 return ret;
3870 }
Paul Bakker48916f92012-09-16 19:57:18 +00003871
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003872 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
3873 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003874#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003875 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3876 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING) {
3877 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003878 ssl->handshake->out_msg_seq = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003879 } else {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003880 ssl->handshake->in_msg_seq = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003881 }
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003882 }
3883#endif
3884
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003885 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
3886 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00003887
Gilles Peskine449bd832023-01-11 14:50:10 +01003888 if ((ret = mbedtls_ssl_handshake(ssl)) != 0) {
3889 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_handshake", ret);
3890 return ret;
Paul Bakker48916f92012-09-16 19:57:18 +00003891 }
3892
Gilles Peskine449bd832023-01-11 14:50:10 +01003893 MBEDTLS_SSL_DEBUG_MSG(2, ("<= renegotiate"));
Paul Bakker48916f92012-09-16 19:57:18 +00003894
Gilles Peskine449bd832023-01-11 14:50:10 +01003895 return 0;
Paul Bakker48916f92012-09-16 19:57:18 +00003896}
3897
3898/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003899 * Renegotiate current connection on client,
3900 * or request renegotiation on server
3901 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003902int mbedtls_ssl_renegotiate(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003903{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003904 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003905
Gilles Peskine449bd832023-01-11 14:50:10 +01003906 if (ssl == NULL || ssl->conf == NULL) {
3907 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3908 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003909
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003910#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003911 /* On server, just send the request */
Gilles Peskine449bd832023-01-11 14:50:10 +01003912 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
3913 if (mbedtls_ssl_is_handshake_over(ssl) == 0) {
3914 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3915 }
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003916
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003917 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02003918
3919 /* Did we already try/start sending HelloRequest? */
Gilles Peskine449bd832023-01-11 14:50:10 +01003920 if (ssl->out_left != 0) {
3921 return mbedtls_ssl_flush_output(ssl);
3922 }
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02003923
Gilles Peskine449bd832023-01-11 14:50:10 +01003924 return ssl_write_hello_request(ssl);
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003925 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003926#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003927
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003928#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003929 /*
3930 * On client, either start the renegotiation process or,
3931 * if already in progress, continue the handshake
3932 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003933 if (ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
3934 if (mbedtls_ssl_is_handshake_over(ssl) == 0) {
3935 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003936 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003937
3938 if ((ret = mbedtls_ssl_start_renegotiation(ssl)) != 0) {
3939 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_start_renegotiation", ret);
3940 return ret;
3941 }
3942 } else {
3943 if ((ret = mbedtls_ssl_handshake(ssl)) != 0) {
3944 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_handshake", ret);
3945 return ret;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003946 }
3947 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003948#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003949
Gilles Peskine449bd832023-01-11 14:50:10 +01003950 return ret;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003951}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003952#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003953
Gilles Peskine449bd832023-01-11 14:50:10 +01003954void mbedtls_ssl_handshake_free(mbedtls_ssl_context *ssl)
Paul Bakker48916f92012-09-16 19:57:18 +00003955{
Gilles Peskine9b562d52018-04-25 20:32:43 +02003956 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
3957
Gilles Peskine449bd832023-01-11 14:50:10 +01003958 if (handshake == NULL) {
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003959 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01003960 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02003961
Brett Warrene0edc842021-08-17 09:53:13 +01003962#if defined(MBEDTLS_ECP_C)
3963#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003964 if (ssl->handshake->group_list_heap_allocated) {
3965 mbedtls_free((void *) handshake->group_list);
3966 }
Brett Warrene0edc842021-08-17 09:53:13 +01003967 handshake->group_list = NULL;
3968#endif /* MBEDTLS_DEPRECATED_REMOVED */
3969#endif /* MBEDTLS_ECP_C */
3970
Ronald Crone68ab4f2022-10-05 12:46:29 +02003971#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +08003972#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003973 if (ssl->handshake->sig_algs_heap_allocated) {
3974 mbedtls_free((void *) handshake->sig_algs);
3975 }
Jerry Yuf017ee42022-01-12 15:49:48 +08003976 handshake->sig_algs = NULL;
3977#endif /* MBEDTLS_DEPRECATED_REMOVED */
Xiaofei Baic234ecf2022-02-08 09:59:23 +00003978#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01003979 if (ssl->handshake->certificate_request_context) {
3980 mbedtls_free((void *) handshake->certificate_request_context);
Xiaofei Baic234ecf2022-02-08 09:59:23 +00003981 }
3982#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Ronald Crone68ab4f2022-10-05 12:46:29 +02003983#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yuf017ee42022-01-12 15:49:48 +08003984
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003985#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01003986 if (ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0) {
3987 ssl->conf->f_async_cancel(ssl);
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003988 handshake->async_in_progress = 0;
3989 }
3990#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
3991
Andrzej Kurek25f27152022-08-17 16:09:31 -04003992#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -05003993#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01003994 psa_hash_abort(&handshake->fin_sha256_psa);
Andrzej Kurekeb342242019-01-29 09:14:33 -05003995#else
Gilles Peskine449bd832023-01-11 14:50:10 +01003996 mbedtls_sha256_free(&handshake->fin_sha256);
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02003997#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05003998#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04003999#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -05004000#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01004001 psa_hash_abort(&handshake->fin_sha384_psa);
Andrzej Kurekeb342242019-01-29 09:14:33 -05004002#else
Gilles Peskine449bd832023-01-11 14:50:10 +01004003 mbedtls_sha512_free(&handshake->fin_sha384);
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02004004#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05004005#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02004006
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004007#if defined(MBEDTLS_DHM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004008 mbedtls_dhm_free(&handshake->dhm_ctx);
Paul Bakker48916f92012-09-16 19:57:18 +00004009#endif
Neil Armstrongf3f46412022-04-12 14:43:39 +02004010#if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004011 mbedtls_ecdh_free(&handshake->ecdh_ctx);
Paul Bakker61d113b2013-07-04 11:51:43 +02004012#endif
Valerio Setti02c25b52022-11-15 14:08:42 +01004013
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02004014#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Neil Armstrongca7d5062022-05-31 14:43:23 +02004015#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01004016 psa_pake_abort(&handshake->psa_pake_ctx);
Valerio Settieb3f7882022-12-08 18:42:58 +01004017 /*
4018 * Opaque keys are not stored in the handshake's data and it's the user
4019 * responsibility to destroy them. Clear ones, instead, are created by
4020 * the TLS library and should be destroyed at the same level
4021 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004022 if (!mbedtls_svc_key_id_is_null(handshake->psa_pake_password)) {
4023 psa_destroy_key(handshake->psa_pake_password);
Valerio Settieb3f7882022-12-08 18:42:58 +01004024 }
Neil Armstrongca7d5062022-05-31 14:43:23 +02004025 handshake->psa_pake_password = MBEDTLS_SVC_KEY_ID_INIT;
4026#else
Gilles Peskine449bd832023-01-11 14:50:10 +01004027 mbedtls_ecjpake_free(&handshake->ecjpake_ctx);
Neil Armstrongca7d5062022-05-31 14:43:23 +02004028#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02004029#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004030 mbedtls_free(handshake->ecjpake_cache);
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02004031 handshake->ecjpake_cache = NULL;
4032 handshake->ecjpake_cache_len = 0;
4033#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02004034#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02004035
Janos Follath4ae5c292016-02-10 11:27:43 +00004036#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
4037 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02004038 /* explicit void pointer cast for buggy MS compiler */
Gilles Peskine449bd832023-01-11 14:50:10 +01004039 mbedtls_free((void *) handshake->curves_tls_id);
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004040#endif
4041
Ronald Cron73fe8df2022-10-05 14:31:43 +02004042#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02004043#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01004044 if (!mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque)) {
Neil Armstrong501c9322022-05-03 09:35:09 +02004045 /* The maintenance of the external PSK key slot is the
4046 * user's responsibility. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004047 if (ssl->handshake->psk_opaque_is_internal) {
4048 psa_destroy_key(ssl->handshake->psk_opaque);
Neil Armstrong501c9322022-05-03 09:35:09 +02004049 ssl->handshake->psk_opaque_is_internal = 0;
4050 }
4051 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
4052 }
Neil Armstronge952a302022-05-03 10:22:14 +02004053#else
Gilles Peskine449bd832023-01-11 14:50:10 +01004054 if (handshake->psk != NULL) {
4055 mbedtls_platform_zeroize(handshake->psk, handshake->psk_len);
4056 mbedtls_free(handshake->psk);
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004057 }
Neil Armstronge952a302022-05-03 10:22:14 +02004058#endif /* MBEDTLS_USE_PSA_CRYPTO */
Ronald Cron73fe8df2022-10-05 14:31:43 +02004059#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004060
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004061#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
4062 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02004063 /*
4064 * Free only the linked list wrapper, not the keys themselves
4065 * since the belong to the SNI callback
4066 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004067 ssl_key_cert_free(handshake->sni_key_cert);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004068#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004069
Gilles Peskineeccd8882020-03-10 12:19:08 +01004070#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01004071 mbedtls_x509_crt_restart_free(&handshake->ecrs_ctx);
4072 if (handshake->ecrs_peer_cert != NULL) {
4073 mbedtls_x509_crt_free(handshake->ecrs_peer_cert);
4074 mbedtls_free(handshake->ecrs_peer_cert);
Hanno Becker3dad3112019-02-05 17:19:52 +00004075 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02004076#endif
4077
Hanno Becker75173122019-02-06 16:18:31 +00004078#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
4079 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01004080 mbedtls_pk_free(&handshake->peer_pubkey);
Hanno Becker75173122019-02-06 16:18:31 +00004081#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
4082
XiaokangQian9b93c0d2022-02-09 06:02:25 +00004083#if defined(MBEDTLS_SSL_CLI_C) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01004084 (defined(MBEDTLS_SSL_PROTO_DTLS) || defined(MBEDTLS_SSL_PROTO_TLS1_3))
4085 mbedtls_free(handshake->cookie);
XiaokangQian9b93c0d2022-02-09 06:02:25 +00004086#endif /* MBEDTLS_SSL_CLI_C &&
4087 ( MBEDTLS_SSL_PROTO_DTLS || MBEDTLS_SSL_PROTO_TLS1_3 ) */
XiaokangQian8499b6c2022-01-27 09:00:11 +00004088
4089#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004090 mbedtls_ssl_flight_free(handshake->flight);
4091 mbedtls_ssl_buffering_free(ssl);
XiaokangQian8499b6c2022-01-27 09:00:11 +00004092#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02004093
Ronald Cronf12b81d2022-03-15 10:42:41 +01004094#if defined(MBEDTLS_ECDH_C) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01004095 (defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3))
4096 if (handshake->ecdh_psa_privkey_is_external == 0) {
4097 psa_destroy_key(handshake->ecdh_psa_privkey);
4098 }
Hanno Becker4a63ed42019-01-08 11:39:35 +00004099#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
4100
Ronald Cron6f135e12021-12-08 16:57:54 +01004101#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01004102 mbedtls_ssl_transform_free(handshake->transform_handshake);
4103 mbedtls_free(handshake->transform_handshake);
Jerry Yu3d9b5902022-11-04 14:07:25 +08004104#if defined(MBEDTLS_SSL_EARLY_DATA)
Gilles Peskine449bd832023-01-11 14:50:10 +01004105 mbedtls_ssl_transform_free(handshake->transform_earlydata);
4106 mbedtls_free(handshake->transform_earlydata);
Jerry Yu3d9b5902022-11-04 14:07:25 +08004107#endif
Ronald Cron6f135e12021-12-08 16:57:54 +01004108#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yuba9c7272021-10-30 11:54:10 +08004109
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004110
4111#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4112 /* If the buffers are too big - reallocate. Because of the way Mbed TLS
4113 * processes datagrams and the fact that a datagram is allowed to have
4114 * several records in it, it is possible that the I/O buffers are not
4115 * empty at this stage */
Gilles Peskine449bd832023-01-11 14:50:10 +01004116 handle_buffer_resizing(ssl, 1, mbedtls_ssl_get_input_buflen(ssl),
4117 mbedtls_ssl_get_output_buflen(ssl));
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004118#endif
Hanno Becker3aa186f2021-08-10 09:24:19 +01004119
Jerry Yuba9c7272021-10-30 11:54:10 +08004120 /* mbedtls_platform_zeroize MUST be last one in this function */
Gilles Peskine449bd832023-01-11 14:50:10 +01004121 mbedtls_platform_zeroize(handshake,
4122 sizeof(mbedtls_ssl_handshake_params));
Paul Bakker48916f92012-09-16 19:57:18 +00004123}
4124
Gilles Peskine449bd832023-01-11 14:50:10 +01004125void mbedtls_ssl_session_free(mbedtls_ssl_session *session)
Paul Bakker48916f92012-09-16 19:57:18 +00004126{
Gilles Peskine449bd832023-01-11 14:50:10 +01004127 if (session == NULL) {
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004128 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01004129 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004130
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004131#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004132 ssl_clear_peer_cert(session);
Paul Bakkered27a042013-04-18 22:46:23 +02004133#endif
Paul Bakker0a597072012-09-25 21:55:46 +00004134
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02004135#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Xiaokang Qianbc663a02022-10-09 11:14:39 +00004136#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
4137 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01004138 mbedtls_free(session->hostname);
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00004139#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01004140 mbedtls_free(session->ticket);
Paul Bakkera503a632013-08-14 13:48:06 +02004141#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004142
Gilles Peskine449bd832023-01-11 14:50:10 +01004143 mbedtls_platform_zeroize(session, sizeof(mbedtls_ssl_session));
Paul Bakker48916f92012-09-16 19:57:18 +00004144}
4145
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02004146#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004147
4148#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4149#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u
4150#else
4151#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u
4152#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4153
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004154#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004155
4156#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4157#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u
4158#else
4159#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u
4160#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4161
4162#if defined(MBEDTLS_SSL_ALPN)
4163#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u
4164#else
4165#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u
4166#endif /* MBEDTLS_SSL_ALPN */
4167
4168#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0
4169#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1
4170#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2
4171#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3
4172
4173#define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \
Gilles Peskine449bd832023-01-11 14:50:10 +01004174 ((uint32_t) ( \
4175 (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << \
4176 SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT) | \
4177 (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << \
4178 SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT) | \
4179 (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << \
4180 SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT) | \
4181 (SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT) | \
4182 0u))
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004183
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004184static unsigned char ssl_serialized_context_header[] = {
4185 MBEDTLS_VERSION_MAJOR,
4186 MBEDTLS_VERSION_MINOR,
4187 MBEDTLS_VERSION_PATCH,
Gilles Peskine449bd832023-01-11 14:50:10 +01004188 MBEDTLS_BYTE_1(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
4189 MBEDTLS_BYTE_0(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
4190 MBEDTLS_BYTE_2(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG),
4191 MBEDTLS_BYTE_1(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG),
4192 MBEDTLS_BYTE_0(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG),
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004193};
4194
Paul Bakker5121ce52009-01-03 21:22:43 +00004195/*
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004196 * Serialize a full SSL context
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02004197 *
4198 * The format of the serialized data is:
4199 * (in the presentation language of TLS, RFC 8446 section 3)
4200 *
4201 * // header
4202 * opaque mbedtls_version[3]; // major, minor, patch
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004203 * opaque context_format[5]; // version-specific field determining
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02004204 * // the format of the remaining
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004205 * // serialized data.
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004206 * Note: When updating the format, remember to keep these
4207 * version+format bytes. (We may make their size part of the API.)
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02004208 *
4209 * // session sub-structure
4210 * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save()
4211 * // transform sub-structure
4212 * uint8 random[64]; // ServerHello.random+ClientHello.random
4213 * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value
4214 * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use
4215 * // fields from ssl_context
4216 * uint32 badmac_seen; // DTLS: number of records with failing MAC
4217 * uint64 in_window_top; // DTLS: last validated record seq_num
4218 * uint64 in_window; // DTLS: bitmask for replay protection
4219 * uint8 disable_datagram_packing; // DTLS: only one record per datagram
4220 * uint64 cur_out_ctr; // Record layer: outgoing sequence number
4221 * uint16 mtu; // DTLS: path mtu (max outgoing fragment size)
4222 * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol
4223 *
4224 * Note that many fields of the ssl_context or sub-structures are not
4225 * serialized, as they fall in one of the following categories:
4226 *
4227 * 1. forced value (eg in_left must be 0)
4228 * 2. pointer to dynamically-allocated memory (eg session, transform)
4229 * 3. value can be re-derived from other data (eg session keys from MS)
4230 * 4. value was temporary (eg content of input buffer)
4231 * 5. value will be provided by the user again (eg I/O callbacks and context)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004232 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004233int mbedtls_ssl_context_save(mbedtls_ssl_context *ssl,
4234 unsigned char *buf,
4235 size_t buf_len,
4236 size_t *olen)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004237{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004238 unsigned char *p = buf;
4239 size_t used = 0;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004240 size_t session_len;
4241 int ret = 0;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004242
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02004243 /*
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004244 * Enforce usage restrictions, see "return BAD_INPUT_DATA" in
4245 * this function's documentation.
4246 *
4247 * These are due to assumptions/limitations in the implementation. Some of
4248 * them are likely to stay (no handshake in progress) some might go away
4249 * (only DTLS) but are currently used to simplify the implementation.
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02004250 */
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004251 /* The initial handshake must be over */
Gilles Peskine449bd832023-01-11 14:50:10 +01004252 if (mbedtls_ssl_is_handshake_over(ssl) == 0) {
4253 MBEDTLS_SSL_DEBUG_MSG(1, ("Initial handshake isn't over"));
4254 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004255 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004256 if (ssl->handshake != NULL) {
4257 MBEDTLS_SSL_DEBUG_MSG(1, ("Handshake isn't completed"));
4258 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004259 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004260 /* Double-check that sub-structures are indeed ready */
Gilles Peskine449bd832023-01-11 14:50:10 +01004261 if (ssl->transform == NULL || ssl->session == NULL) {
4262 MBEDTLS_SSL_DEBUG_MSG(1, ("Serialised structures aren't ready"));
4263 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004264 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004265 /* There must be no pending incoming or outgoing data */
Gilles Peskine449bd832023-01-11 14:50:10 +01004266 if (mbedtls_ssl_check_pending(ssl) != 0) {
4267 MBEDTLS_SSL_DEBUG_MSG(1, ("There is pending incoming data"));
4268 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004269 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004270 if (ssl->out_left != 0) {
4271 MBEDTLS_SSL_DEBUG_MSG(1, ("There is pending outgoing data"));
4272 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004273 }
Dave Rodgman556e8a32022-12-06 16:31:25 +00004274 /* Protocol must be DTLS, not TLS */
Gilles Peskine449bd832023-01-11 14:50:10 +01004275 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
4276 MBEDTLS_SSL_DEBUG_MSG(1, ("Only DTLS is supported"));
4277 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004278 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004279 /* Version must be 1.2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004280 if (ssl->tls_version != MBEDTLS_SSL_VERSION_TLS1_2) {
4281 MBEDTLS_SSL_DEBUG_MSG(1, ("Only version 1.2 supported"));
4282 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004283 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004284 /* We must be using an AEAD ciphersuite */
Gilles Peskine449bd832023-01-11 14:50:10 +01004285 if (mbedtls_ssl_transform_uses_aead(ssl->transform) != 1) {
4286 MBEDTLS_SSL_DEBUG_MSG(1, ("Only AEAD ciphersuites supported"));
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 /* Renegotiation must not be enabled */
4290#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01004291 if (ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED) {
4292 MBEDTLS_SSL_DEBUG_MSG(1, ("Renegotiation must not be enabled"));
4293 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004294 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004295#endif
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004296
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004297 /*
4298 * Version and format identifier
4299 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004300 used += sizeof(ssl_serialized_context_header);
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004301
Gilles Peskine449bd832023-01-11 14:50:10 +01004302 if (used <= buf_len) {
4303 memcpy(p, ssl_serialized_context_header,
4304 sizeof(ssl_serialized_context_header));
4305 p += sizeof(ssl_serialized_context_header);
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004306 }
4307
4308 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004309 * Session (length + data)
4310 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004311 ret = ssl_session_save(ssl->session, 1, NULL, 0, &session_len);
4312 if (ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL) {
4313 return ret;
4314 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004315
4316 used += 4 + session_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004317 if (used <= buf_len) {
4318 MBEDTLS_PUT_UINT32_BE(session_len, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004319 p += 4;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004320
Gilles Peskine449bd832023-01-11 14:50:10 +01004321 ret = ssl_session_save(ssl->session, 1,
4322 p, session_len, &session_len);
4323 if (ret != 0) {
4324 return ret;
4325 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004326
4327 p += session_len;
4328 }
4329
4330 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004331 * Transform
4332 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004333 used += sizeof(ssl->transform->randbytes);
4334 if (used <= buf_len) {
4335 memcpy(p, ssl->transform->randbytes,
4336 sizeof(ssl->transform->randbytes));
4337 p += sizeof(ssl->transform->randbytes);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004338 }
4339
4340#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4341 used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004342 if (used <= buf_len) {
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004343 *p++ = ssl->transform->in_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004344 memcpy(p, ssl->transform->in_cid, ssl->transform->in_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004345 p += ssl->transform->in_cid_len;
4346
4347 *p++ = ssl->transform->out_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004348 memcpy(p, ssl->transform->out_cid, ssl->transform->out_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004349 p += ssl->transform->out_cid_len;
4350 }
4351#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4352
4353 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004354 * Saved fields from top-level ssl_context structure
4355 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004356 used += 4;
Gilles Peskine449bd832023-01-11 14:50:10 +01004357 if (used <= buf_len) {
4358 MBEDTLS_PUT_UINT32_BE(ssl->badmac_seen, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004359 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004360 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004361
4362#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4363 used += 16;
Gilles Peskine449bd832023-01-11 14:50:10 +01004364 if (used <= buf_len) {
4365 MBEDTLS_PUT_UINT64_BE(ssl->in_window_top, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004366 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004367
Gilles Peskine449bd832023-01-11 14:50:10 +01004368 MBEDTLS_PUT_UINT64_BE(ssl->in_window, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004369 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004370 }
4371#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4372
4373#if defined(MBEDTLS_SSL_PROTO_DTLS)
4374 used += 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004375 if (used <= buf_len) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004376 *p++ = ssl->disable_datagram_packing;
4377 }
4378#endif /* MBEDTLS_SSL_PROTO_DTLS */
4379
Jerry Yuae0b2e22021-10-08 15:21:19 +08004380 used += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Gilles Peskine449bd832023-01-11 14:50:10 +01004381 if (used <= buf_len) {
4382 memcpy(p, ssl->cur_out_ctr, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN);
Jerry Yuae0b2e22021-10-08 15:21:19 +08004383 p += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004384 }
4385
4386#if defined(MBEDTLS_SSL_PROTO_DTLS)
4387 used += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01004388 if (used <= buf_len) {
4389 MBEDTLS_PUT_UINT16_BE(ssl->mtu, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004390 p += 2;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004391 }
4392#endif /* MBEDTLS_SSL_PROTO_DTLS */
4393
4394#if defined(MBEDTLS_SSL_ALPN)
4395 {
4396 const uint8_t alpn_len = ssl->alpn_chosen
Gilles Peskine449bd832023-01-11 14:50:10 +01004397 ? (uint8_t) strlen(ssl->alpn_chosen)
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004398 : 0;
4399
4400 used += 1 + alpn_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004401 if (used <= buf_len) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004402 *p++ = alpn_len;
4403
Gilles Peskine449bd832023-01-11 14:50:10 +01004404 if (ssl->alpn_chosen != NULL) {
4405 memcpy(p, ssl->alpn_chosen, alpn_len);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004406 p += alpn_len;
4407 }
4408 }
4409 }
4410#endif /* MBEDTLS_SSL_ALPN */
4411
4412 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004413 * Done
4414 */
4415 *olen = used;
4416
Gilles Peskine449bd832023-01-11 14:50:10 +01004417 if (used > buf_len) {
4418 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
4419 }
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004420
Gilles Peskine449bd832023-01-11 14:50:10 +01004421 MBEDTLS_SSL_DEBUG_BUF(4, "saved context", buf, used);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004422
Gilles Peskine449bd832023-01-11 14:50:10 +01004423 return mbedtls_ssl_session_reset_int(ssl, 0);
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004424}
4425
4426/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02004427 * Deserialize context, see mbedtls_ssl_context_save() for format.
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004428 *
4429 * This internal version is wrapped by a public function that cleans up in
4430 * case of error.
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004431 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004432MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01004433static int ssl_context_load(mbedtls_ssl_context *ssl,
4434 const unsigned char *buf,
4435 size_t len)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004436{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004437 const unsigned char *p = buf;
4438 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004439 size_t session_len;
Janos Follath865b3eb2019-12-16 11:46:15 +00004440 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004441#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek894edde2022-09-29 06:31:14 -04004442 tls_prf_fn prf_func = NULL;
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004443#endif
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004444
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004445 /*
4446 * The context should have been freshly setup or reset.
4447 * Give the user an error in case of obvious misuse.
Manuel Pégourié-Gonnard4ca930f2019-07-26 16:31:53 +02004448 * (Checking session is useful because it won't be NULL if we're
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004449 * renegotiating, or if the user mistakenly loaded a session first.)
4450 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004451 if (ssl->state != MBEDTLS_SSL_HELLO_REQUEST ||
4452 ssl->session != NULL) {
4453 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004454 }
4455
4456 /*
4457 * We can't check that the config matches the initial one, but we can at
4458 * least check it matches the requirements for serializing.
4459 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004460 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
Glenn Strauss2dfcea22022-03-14 17:26:42 -04004461 ssl->conf->max_tls_version < MBEDTLS_SSL_VERSION_TLS1_2 ||
4462 ssl->conf->min_tls_version > MBEDTLS_SSL_VERSION_TLS1_2 ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004463#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02004464 ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004465#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01004466 0) {
4467 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004468 }
4469
Gilles Peskine449bd832023-01-11 14:50:10 +01004470 MBEDTLS_SSL_DEBUG_BUF(4, "context to load", buf, len);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004471
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004472 /*
4473 * Check version identifier
4474 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004475 if ((size_t) (end - p) < sizeof(ssl_serialized_context_header)) {
4476 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004477 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004478
4479 if (memcmp(p, ssl_serialized_context_header,
4480 sizeof(ssl_serialized_context_header)) != 0) {
4481 return MBEDTLS_ERR_SSL_VERSION_MISMATCH;
4482 }
4483 p += sizeof(ssl_serialized_context_header);
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004484
4485 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004486 * Session
4487 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004488 if ((size_t) (end - p) < 4) {
4489 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4490 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004491
Gilles Peskine449bd832023-01-11 14:50:10 +01004492 session_len = ((size_t) p[0] << 24) |
4493 ((size_t) p[1] << 16) |
4494 ((size_t) p[2] << 8) |
4495 ((size_t) p[3]);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004496 p += 4;
4497
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004498 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00004499 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004500 ssl->session = ssl->session_negotiate;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004501 ssl->session_in = ssl->session;
4502 ssl->session_out = ssl->session;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004503 ssl->session_negotiate = NULL;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004504
Gilles Peskine449bd832023-01-11 14:50:10 +01004505 if ((size_t) (end - p) < session_len) {
4506 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4507 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004508
Gilles Peskine449bd832023-01-11 14:50:10 +01004509 ret = ssl_session_load(ssl->session, 1, p, session_len);
4510 if (ret != 0) {
4511 mbedtls_ssl_session_free(ssl->session);
4512 return ret;
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004513 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004514
4515 p += session_len;
4516
4517 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004518 * Transform
4519 */
4520
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004521 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00004522 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Jerry Yu2e199812022-12-01 18:57:19 +08004523#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004524 ssl->transform = ssl->transform_negotiate;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004525 ssl->transform_in = ssl->transform;
4526 ssl->transform_out = ssl->transform;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004527 ssl->transform_negotiate = NULL;
Jerry Yu2e199812022-12-01 18:57:19 +08004528#endif
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004529
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004530#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01004531 prf_func = ssl_tls12prf_from_cs(ssl->session->ciphersuite);
4532 if (prf_func == NULL) {
4533 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4534 }
Andrzej Kurek894edde2022-09-29 06:31:14 -04004535
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004536 /* Read random bytes and populate structure */
Gilles Peskine449bd832023-01-11 14:50:10 +01004537 if ((size_t) (end - p) < sizeof(ssl->transform->randbytes)) {
4538 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4539 }
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004540
Gilles Peskine449bd832023-01-11 14:50:10 +01004541 ret = ssl_tls12_populate_transform(ssl->transform,
4542 ssl->session->ciphersuite,
4543 ssl->session->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02004544#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004545 ssl->session->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02004546#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01004547 prf_func,
4548 p, /* currently pointing to randbytes */
4549 MBEDTLS_SSL_VERSION_TLS1_2, /* (D)TLS 1.2 is forced */
4550 ssl->conf->endpoint,
4551 ssl);
4552 if (ret != 0) {
4553 return ret;
4554 }
Jerry Yu840fbb22022-02-17 14:59:29 +08004555#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004556 p += sizeof(ssl->transform->randbytes);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004557
4558#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4559 /* Read connection IDs and store them */
Gilles Peskine449bd832023-01-11 14:50:10 +01004560 if ((size_t) (end - p) < 1) {
4561 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4562 }
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004563
4564 ssl->transform->in_cid_len = *p++;
4565
Gilles Peskine449bd832023-01-11 14:50:10 +01004566 if ((size_t) (end - p) < ssl->transform->in_cid_len + 1u) {
4567 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4568 }
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004569
Gilles Peskine449bd832023-01-11 14:50:10 +01004570 memcpy(ssl->transform->in_cid, p, ssl->transform->in_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004571 p += ssl->transform->in_cid_len;
4572
4573 ssl->transform->out_cid_len = *p++;
4574
Gilles Peskine449bd832023-01-11 14:50:10 +01004575 if ((size_t) (end - p) < ssl->transform->out_cid_len) {
4576 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4577 }
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004578
Gilles Peskine449bd832023-01-11 14:50:10 +01004579 memcpy(ssl->transform->out_cid, p, ssl->transform->out_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004580 p += ssl->transform->out_cid_len;
4581#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4582
4583 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004584 * Saved fields from top-level ssl_context structure
4585 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004586 if ((size_t) (end - p) < 4) {
4587 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4588 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004589
Gilles Peskine449bd832023-01-11 14:50:10 +01004590 ssl->badmac_seen = ((uint32_t) p[0] << 24) |
4591 ((uint32_t) p[1] << 16) |
4592 ((uint32_t) p[2] << 8) |
4593 ((uint32_t) p[3]);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004594 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004595
4596#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine449bd832023-01-11 14:50:10 +01004597 if ((size_t) (end - p) < 16) {
4598 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4599 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004600
Gilles Peskine449bd832023-01-11 14:50:10 +01004601 ssl->in_window_top = ((uint64_t) p[0] << 56) |
4602 ((uint64_t) p[1] << 48) |
4603 ((uint64_t) p[2] << 40) |
4604 ((uint64_t) p[3] << 32) |
4605 ((uint64_t) p[4] << 24) |
4606 ((uint64_t) p[5] << 16) |
4607 ((uint64_t) p[6] << 8) |
4608 ((uint64_t) p[7]);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004609 p += 8;
4610
Gilles Peskine449bd832023-01-11 14:50:10 +01004611 ssl->in_window = ((uint64_t) p[0] << 56) |
4612 ((uint64_t) p[1] << 48) |
4613 ((uint64_t) p[2] << 40) |
4614 ((uint64_t) p[3] << 32) |
4615 ((uint64_t) p[4] << 24) |
4616 ((uint64_t) p[5] << 16) |
4617 ((uint64_t) p[6] << 8) |
4618 ((uint64_t) p[7]);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004619 p += 8;
4620#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4621
4622#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004623 if ((size_t) (end - p) < 1) {
4624 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4625 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004626
4627 ssl->disable_datagram_packing = *p++;
4628#endif /* MBEDTLS_SSL_PROTO_DTLS */
4629
Gilles Peskine449bd832023-01-11 14:50:10 +01004630 if ((size_t) (end - p) < sizeof(ssl->cur_out_ctr)) {
4631 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4632 }
4633 memcpy(ssl->cur_out_ctr, p, sizeof(ssl->cur_out_ctr));
4634 p += sizeof(ssl->cur_out_ctr);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004635
4636#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004637 if ((size_t) (end - p) < 2) {
4638 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4639 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004640
Gilles Peskine449bd832023-01-11 14:50:10 +01004641 ssl->mtu = (p[0] << 8) | p[1];
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004642 p += 2;
4643#endif /* MBEDTLS_SSL_PROTO_DTLS */
4644
4645#if defined(MBEDTLS_SSL_ALPN)
4646 {
4647 uint8_t alpn_len;
4648 const char **cur;
4649
Gilles Peskine449bd832023-01-11 14:50:10 +01004650 if ((size_t) (end - p) < 1) {
4651 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4652 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004653
4654 alpn_len = *p++;
4655
Gilles Peskine449bd832023-01-11 14:50:10 +01004656 if (alpn_len != 0 && ssl->conf->alpn_list != NULL) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004657 /* alpn_chosen should point to an item in the configured list */
Gilles Peskine449bd832023-01-11 14:50:10 +01004658 for (cur = ssl->conf->alpn_list; *cur != NULL; cur++) {
4659 if (strlen(*cur) == alpn_len &&
4660 memcmp(p, cur, alpn_len) == 0) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004661 ssl->alpn_chosen = *cur;
4662 break;
4663 }
4664 }
4665 }
4666
4667 /* can only happen on conf mismatch */
Gilles Peskine449bd832023-01-11 14:50:10 +01004668 if (alpn_len != 0 && ssl->alpn_chosen == NULL) {
4669 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4670 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004671
4672 p += alpn_len;
4673 }
4674#endif /* MBEDTLS_SSL_ALPN */
4675
4676 /*
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004677 * Forced fields from top-level ssl_context structure
4678 *
4679 * Most of them already set to the correct value by mbedtls_ssl_init() and
4680 * mbedtls_ssl_reset(), so we only need to set the remaining ones.
4681 */
4682 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
Glenn Strauss60bfe602022-03-14 19:04:24 -04004683 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004684
Hanno Becker361b10d2019-08-30 10:42:49 +01004685 /* Adjust pointers for header fields of outgoing records to
4686 * the given transform, accounting for explicit IV and CID. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004687 mbedtls_ssl_update_out_pointers(ssl, ssl->transform);
Hanno Becker361b10d2019-08-30 10:42:49 +01004688
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004689#if defined(MBEDTLS_SSL_PROTO_DTLS)
4690 ssl->in_epoch = 1;
4691#endif
4692
4693 /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated,
4694 * which we don't want - otherwise we'd end up freeing the wrong transform
Hanno Beckerce5f5fd2020-02-05 10:47:44 +00004695 * by calling mbedtls_ssl_handshake_wrapup_free_hs_transform()
4696 * inappropriately. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004697 if (ssl->handshake != NULL) {
4698 mbedtls_ssl_handshake_free(ssl);
4699 mbedtls_free(ssl->handshake);
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004700 ssl->handshake = NULL;
4701 }
4702
4703 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004704 * Done - should have consumed entire buffer
4705 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004706 if (p != end) {
4707 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4708 }
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004709
Gilles Peskine449bd832023-01-11 14:50:10 +01004710 return 0;
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004711}
4712
4713/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02004714 * Deserialize context: public wrapper for error cleaning
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004715 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004716int mbedtls_ssl_context_load(mbedtls_ssl_context *context,
4717 const unsigned char *buf,
4718 size_t len)
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004719{
Gilles Peskine449bd832023-01-11 14:50:10 +01004720 int ret = ssl_context_load(context, buf, len);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004721
Gilles Peskine449bd832023-01-11 14:50:10 +01004722 if (ret != 0) {
4723 mbedtls_ssl_free(context);
4724 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004725
Gilles Peskine449bd832023-01-11 14:50:10 +01004726 return ret;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004727}
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02004728#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004729
4730/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004731 * Free an SSL context
4732 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004733void mbedtls_ssl_free(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00004734{
Gilles Peskine449bd832023-01-11 14:50:10 +01004735 if (ssl == NULL) {
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004736 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01004737 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004738
Gilles Peskine449bd832023-01-11 14:50:10 +01004739 MBEDTLS_SSL_DEBUG_MSG(2, ("=> free"));
Paul Bakker5121ce52009-01-03 21:22:43 +00004740
Gilles Peskine449bd832023-01-11 14:50:10 +01004741 if (ssl->out_buf != NULL) {
sander-visserb8aa2072020-05-06 22:05:13 +02004742#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4743 size_t out_buf_len = ssl->out_buf_len;
4744#else
4745 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
4746#endif
4747
Gilles Peskine449bd832023-01-11 14:50:10 +01004748 mbedtls_platform_zeroize(ssl->out_buf, out_buf_len);
4749 mbedtls_free(ssl->out_buf);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004750 ssl->out_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004751 }
4752
Gilles Peskine449bd832023-01-11 14:50:10 +01004753 if (ssl->in_buf != NULL) {
sander-visserb8aa2072020-05-06 22:05:13 +02004754#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4755 size_t in_buf_len = ssl->in_buf_len;
4756#else
4757 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
4758#endif
4759
Gilles Peskine449bd832023-01-11 14:50:10 +01004760 mbedtls_platform_zeroize(ssl->in_buf, in_buf_len);
4761 mbedtls_free(ssl->in_buf);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004762 ssl->in_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004763 }
4764
Gilles Peskine449bd832023-01-11 14:50:10 +01004765 if (ssl->transform) {
4766 mbedtls_ssl_transform_free(ssl->transform);
4767 mbedtls_free(ssl->transform);
Paul Bakker48916f92012-09-16 19:57:18 +00004768 }
4769
Gilles Peskine449bd832023-01-11 14:50:10 +01004770 if (ssl->handshake) {
4771 mbedtls_ssl_handshake_free(ssl);
4772 mbedtls_free(ssl->handshake);
Jerry Yu2e199812022-12-01 18:57:19 +08004773
4774#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01004775 mbedtls_ssl_transform_free(ssl->transform_negotiate);
4776 mbedtls_free(ssl->transform_negotiate);
Jerry Yu2e199812022-12-01 18:57:19 +08004777#endif
4778
Gilles Peskine449bd832023-01-11 14:50:10 +01004779 mbedtls_ssl_session_free(ssl->session_negotiate);
4780 mbedtls_free(ssl->session_negotiate);
Paul Bakker48916f92012-09-16 19:57:18 +00004781 }
4782
Ronald Cron6f135e12021-12-08 16:57:54 +01004783#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01004784 mbedtls_ssl_transform_free(ssl->transform_application);
4785 mbedtls_free(ssl->transform_application);
Ronald Cron6f135e12021-12-08 16:57:54 +01004786#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker3aa186f2021-08-10 09:24:19 +01004787
Gilles Peskine449bd832023-01-11 14:50:10 +01004788 if (ssl->session) {
4789 mbedtls_ssl_session_free(ssl->session);
4790 mbedtls_free(ssl->session);
Paul Bakkerc0463502013-02-14 11:19:38 +01004791 }
4792
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02004793#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004794 if (ssl->hostname != NULL) {
4795 mbedtls_platform_zeroize(ssl->hostname, strlen(ssl->hostname));
4796 mbedtls_free(ssl->hostname);
Paul Bakker5121ce52009-01-03 21:22:43 +00004797 }
Paul Bakker0be444a2013-08-27 21:55:01 +02004798#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004799
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02004800#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004801 mbedtls_free(ssl->cli_id);
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02004802#endif
4803
Gilles Peskine449bd832023-01-11 14:50:10 +01004804 MBEDTLS_SSL_DEBUG_MSG(2, ("<= free"));
Paul Bakker2da561c2009-02-05 18:00:28 +00004805
Paul Bakker86f04f42013-02-14 11:20:09 +01004806 /* Actually clear after last debug message */
Gilles Peskine449bd832023-01-11 14:50:10 +01004807 mbedtls_platform_zeroize(ssl, sizeof(mbedtls_ssl_context));
Paul Bakker5121ce52009-01-03 21:22:43 +00004808}
4809
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004810/*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08004811 * Initialize mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004812 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004813void mbedtls_ssl_config_init(mbedtls_ssl_config *conf)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004814{
Gilles Peskine449bd832023-01-11 14:50:10 +01004815 memset(conf, 0, sizeof(mbedtls_ssl_config));
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004816}
4817
Gilles Peskineae270bf2021-06-02 00:05:29 +02004818/* The selection should be the same as mbedtls_x509_crt_profile_default in
4819 * x509_crt.c, plus Montgomery curves for ECDHE. Here, the order matters:
Gilles Peskineb1940a72021-06-02 15:18:12 +02004820 * curves with a lower resource usage come first.
Gilles Peskineae270bf2021-06-02 00:05:29 +02004821 * See the documentation of mbedtls_ssl_conf_curves() for what we promise
Gilles Peskineb1940a72021-06-02 15:18:12 +02004822 * about this list.
4823 */
Brett Warrene0edc842021-08-17 09:53:13 +01004824static uint16_t ssl_preset_default_groups[] = {
Gilles Peskineae270bf2021-06-02 00:05:29 +02004825#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004826 MBEDTLS_SSL_IANA_TLS_GROUP_X25519,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004827#endif
4828#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004829 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004830#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02004831#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004832 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004833#endif
4834#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004835 MBEDTLS_SSL_IANA_TLS_GROUP_X448,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004836#endif
4837#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004838 MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004839#endif
Gilles Peskineae270bf2021-06-02 00:05:29 +02004840#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004841 MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004842#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02004843#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004844 MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004845#endif
4846#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004847 MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004848#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004849 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Gilles Peskineae270bf2021-06-02 00:05:29 +02004850};
Gilles Peskineae270bf2021-06-02 00:05:29 +02004851
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004852static int ssl_preset_suiteb_ciphersuites[] = {
4853 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
4854 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
4855 0
4856};
4857
Ronald Crone68ab4f2022-10-05 12:46:29 +02004858#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004859
Jerry Yu909df7b2022-01-22 11:56:27 +08004860/* NOTICE:
Jerry Yu0b994b82022-01-25 17:22:12 +08004861 * For ssl_preset_*_sig_algs and ssl_tls12_preset_*_sig_algs, the following
Jerry Yu370e1462022-01-25 10:36:53 +08004862 * rules SHOULD be upheld.
4863 * - No duplicate entries.
4864 * - But if there is a good reason, do not change the order of the algorithms.
Jerry Yu09a99fc2022-07-28 14:22:17 +08004865 * - ssl_tls12_preset* is for TLS 1.2 use only.
Jerry Yu370e1462022-01-25 10:36:53 +08004866 * - ssl_preset_* is for TLS 1.3 only or hybrid TLS 1.3/1.2 handshakes.
Jerry Yu1a8b4812022-01-20 17:56:50 +08004867 */
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004868static uint16_t ssl_preset_default_sig_algs[] = {
Jerry Yu1a8b4812022-01-20 17:56:50 +08004869
Andrzej Kurek25f27152022-08-17 16:09:31 -04004870#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 +08004871 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
4872 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004873#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA &&
Jerry Yued5e9f42022-01-26 11:21:34 +08004874 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004875
Andrzej Kurek25f27152022-08-17 16:09:31 -04004876#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 +08004877 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
4878 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004879#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu909df7b2022-01-22 11:56:27 +08004880 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
4881
Andrzej Kurek25f27152022-08-17 16:09:31 -04004882#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 +08004883 defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
4884 MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004885#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yued5e9f42022-01-26 11:21:34 +08004886 MBEDTLS_ECP_DP_SECP521R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004887
Gilles Peskine449bd832023-01-11 14:50:10 +01004888#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && \
4889 defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu9bb3ee42022-06-23 10:16:33 +08004890 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
Gilles Peskine449bd832023-01-11 14:50:10 +01004891#endif \
4892 /* 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 +08004893
Gilles Peskine449bd832023-01-11 14:50:10 +01004894#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && \
4895 defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu9bb3ee42022-06-23 10:16:33 +08004896 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
Gilles Peskine449bd832023-01-11 14:50:10 +01004897#endif \
4898 /* 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 +08004899
Gilles Peskine449bd832023-01-11 14:50:10 +01004900#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && \
4901 defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu9bb3ee42022-06-23 10:16:33 +08004902 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Gilles Peskine449bd832023-01-11 14:50:10 +01004903#endif \
4904 /* 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 +08004905
Andrzej Kurek25f27152022-08-17 16:09:31 -04004906#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 +08004907 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512,
4908#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA512_C */
4909
Andrzej Kurek25f27152022-08-17 16:09:31 -04004910#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 +08004911 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384,
4912#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA384_C */
4913
Andrzej Kurek25f27152022-08-17 16:09:31 -04004914#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 +08004915 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
4916#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA256_C */
4917
Gabor Mezei15b95a62022-05-09 16:37:58 +02004918 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08004919};
4920
4921/* NOTICE: see above */
4922#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4923static uint16_t ssl_tls12_preset_default_sig_algs[] = {
Andrzej Kurek25f27152022-08-17 16:09:31 -04004924#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004925#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004926 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA512),
Jerry Yu713013f2022-01-17 18:16:35 +08004927#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004928#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4929 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
4930#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004931#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004932 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA512),
Gabor Mezeic1051b62022-05-10 13:13:58 +02004933#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004934#endif /* MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04004935#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004936#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004937 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08004938#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004939#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4940 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
4941#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004942#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004943 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384),
Gabor Mezeic1051b62022-05-10 13:13:58 +02004944#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004945#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04004946#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004947#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004948 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08004949#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08004950#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
4951 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
4952#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02004953#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004954 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256),
Gabor Mezeic1051b62022-05-10 13:13:58 +02004955#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004956#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Gabor Mezei15b95a62022-05-09 16:37:58 +02004957 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08004958};
4959#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
4960/* NOTICE: see above */
4961static uint16_t ssl_preset_suiteb_sig_algs[] = {
4962
Andrzej Kurek25f27152022-08-17 16:09:31 -04004963#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 +08004964 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
4965 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004966#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu909df7b2022-01-22 11:56:27 +08004967 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
4968
Andrzej Kurek25f27152022-08-17 16:09:31 -04004969#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 +08004970 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
4971 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004972#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu53037892022-01-25 11:02:06 +08004973 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004974
Gilles Peskine449bd832023-01-11 14:50:10 +01004975#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && \
4976 defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu909df7b2022-01-22 11:56:27 +08004977 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Gilles Peskine449bd832023-01-11 14:50:10 +01004978#endif \
4979 /* 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 +08004980
Andrzej Kurek25f27152022-08-17 16:09:31 -04004981#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 +08004982 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004983#endif /* MBEDTLS_RSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yu53037892022-01-25 11:02:06 +08004984
Gabor Mezei15b95a62022-05-09 16:37:58 +02004985 MBEDTLS_TLS_SIG_NONE
Jerry Yu713013f2022-01-17 18:16:35 +08004986};
Jerry Yu6106fdc2022-01-12 16:36:14 +08004987
Jerry Yu909df7b2022-01-22 11:56:27 +08004988/* NOTICE: see above */
4989#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
4990static uint16_t ssl_tls12_preset_suiteb_sig_algs[] = {
Andrzej Kurek25f27152022-08-17 16:09:31 -04004991#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02004992#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004993 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256),
Jerry Yu713013f2022-01-17 18:16:35 +08004994#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02004995#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004996 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256),
Gabor Mezeic1051b62022-05-10 13:13:58 +02004997#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004998#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04004999#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02005000#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005001 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384),
Jerry Yu18c833e2022-01-25 10:55:47 +08005002#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02005003#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005004 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384),
Gabor Mezeic1051b62022-05-10 13:13:58 +02005005#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005006#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Gabor Mezei15b95a62022-05-09 16:37:58 +02005007 MBEDTLS_TLS_SIG_NONE
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01005008};
Jerry Yu909df7b2022-01-22 11:56:27 +08005009#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5010
Ronald Crone68ab4f2022-10-05 12:46:29 +02005011#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005012
Brett Warrene0edc842021-08-17 09:53:13 +01005013static uint16_t ssl_preset_suiteb_groups[] = {
Jaeden Amerod4311042019-06-03 08:27:16 +01005014#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01005015 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01005016#endif
5017#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01005018 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01005019#endif
Brett Warrene0edc842021-08-17 09:53:13 +01005020 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005021};
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005022
Ronald Crone68ab4f2022-10-05 12:46:29 +02005023#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08005024/* Function for checking `ssl_preset_*_sig_algs` and `ssl_tls12_preset_*_sig_algs`
Jerry Yu370e1462022-01-25 10:36:53 +08005025 * to make sure there are no duplicated signature algorithm entries. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005026MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01005027static int ssl_check_no_sig_alg_duplication(uint16_t *sig_algs)
Jerry Yu1a8b4812022-01-20 17:56:50 +08005028{
5029 size_t i, j;
5030 int ret = 0;
Jerry Yu909df7b2022-01-22 11:56:27 +08005031
Gilles Peskine449bd832023-01-11 14:50:10 +01005032 for (i = 0; sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++) {
5033 for (j = 0; j < i; j++) {
5034 if (sig_algs[i] != sig_algs[j]) {
Jerry Yuf377d642022-01-25 10:43:59 +08005035 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01005036 }
5037 mbedtls_printf(" entry(%04x,%" MBEDTLS_PRINTF_SIZET
5038 ") is duplicated at %" MBEDTLS_PRINTF_SIZET "\n",
5039 sig_algs[i], j, i);
Jerry Yuf377d642022-01-25 10:43:59 +08005040 ret = -1;
Jerry Yu1a8b4812022-01-20 17:56:50 +08005041 }
5042 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005043 return ret;
Jerry Yu1a8b4812022-01-20 17:56:50 +08005044}
5045
Ronald Crone68ab4f2022-10-05 12:46:29 +02005046#endif /* MBEDTLS_DEBUG_C && MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yu1a8b4812022-01-20 17:56:50 +08005047
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005048/*
Tillmann Karras588ad502015-09-25 04:27:22 +02005049 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005050 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005051int mbedtls_ssl_config_defaults(mbedtls_ssl_config *conf,
5052 int endpoint, int transport, int preset)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005053{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005054#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Janos Follath865b3eb2019-12-16 11:46:15 +00005055 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005056#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005057
Ronald Crone68ab4f2022-10-05 12:46:29 +02005058#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01005059 if (ssl_check_no_sig_alg_duplication(ssl_preset_suiteb_sig_algs)) {
5060 mbedtls_printf("ssl_preset_suiteb_sig_algs has duplicated entries\n");
5061 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu1a8b4812022-01-20 17:56:50 +08005062 }
5063
Gilles Peskine449bd832023-01-11 14:50:10 +01005064 if (ssl_check_no_sig_alg_duplication(ssl_preset_default_sig_algs)) {
5065 mbedtls_printf("ssl_preset_default_sig_algs has duplicated entries\n");
5066 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu1a8b4812022-01-20 17:56:50 +08005067 }
Jerry Yu909df7b2022-01-22 11:56:27 +08005068
5069#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01005070 if (ssl_check_no_sig_alg_duplication(ssl_tls12_preset_suiteb_sig_algs)) {
5071 mbedtls_printf("ssl_tls12_preset_suiteb_sig_algs has duplicated entries\n");
5072 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu909df7b2022-01-22 11:56:27 +08005073 }
5074
Gilles Peskine449bd832023-01-11 14:50:10 +01005075 if (ssl_check_no_sig_alg_duplication(ssl_tls12_preset_default_sig_algs)) {
5076 mbedtls_printf("ssl_tls12_preset_default_sig_algs has duplicated entries\n");
5077 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu909df7b2022-01-22 11:56:27 +08005078 }
5079#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Ronald Crone68ab4f2022-10-05 12:46:29 +02005080#endif /* MBEDTLS_DEBUG_C && MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yu1a8b4812022-01-20 17:56:50 +08005081
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02005082 /* Use the functions here so that they are covered in tests,
5083 * but otherwise access member directly for efficiency */
Gilles Peskine449bd832023-01-11 14:50:10 +01005084 mbedtls_ssl_conf_endpoint(conf, endpoint);
5085 mbedtls_ssl_conf_transport(conf, transport);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005086
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005087 /*
5088 * Things that are common to all presets
5089 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02005090#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005091 if (endpoint == MBEDTLS_SSL_IS_CLIENT) {
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02005092 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
5093#if defined(MBEDTLS_SSL_SESSION_TICKETS)
5094 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
5095#endif
5096 }
5097#endif
5098
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005099#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
5100 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
5101#endif
5102
5103#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
5104 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
5105#endif
5106
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02005107#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005108 conf->f_cookie_write = ssl_cookie_write_dummy;
5109 conf->f_cookie_check = ssl_cookie_check_dummy;
5110#endif
5111
5112#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
5113 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
5114#endif
5115
Janos Follath088ce432017-04-10 12:42:31 +01005116#if defined(MBEDTLS_SSL_SRV_C)
5117 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
TRodziewicz3946f792021-06-14 12:11:18 +02005118 conf->respect_cli_pref = MBEDTLS_SSL_SRV_CIPHERSUITE_ORDER_SERVER;
Janos Follath088ce432017-04-10 12:42:31 +01005119#endif
5120
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005121#if defined(MBEDTLS_SSL_PROTO_DTLS)
5122 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
5123 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
5124#endif
5125
5126#if defined(MBEDTLS_SSL_RENEGOTIATION)
5127 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005128 memset(conf->renego_period, 0x00, 2);
5129 memset(conf->renego_period + 2, 0xFF, 6);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005130#endif
5131
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005132#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005133 if (endpoint == MBEDTLS_SSL_IS_SERVER) {
Hanno Beckere2defad2021-07-24 05:59:17 +01005134 const unsigned char dhm_p[] =
5135 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
5136 const unsigned char dhm_g[] =
5137 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
Hanno Becker00d0a682017-10-04 13:14:29 +01005138
Gilles Peskine449bd832023-01-11 14:50:10 +01005139 if ((ret = mbedtls_ssl_conf_dh_param_bin(conf,
5140 dhm_p, sizeof(dhm_p),
5141 dhm_g, sizeof(dhm_g))) != 0) {
5142 return ret;
Hanno Beckere2defad2021-07-24 05:59:17 +01005143 }
5144 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02005145#endif
5146
Ronald Cron6f135e12021-12-08 16:57:54 +01005147#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu6ee56aa2022-12-06 17:47:22 +08005148
5149#if defined(MBEDTLS_SSL_EARLY_DATA)
Gilles Peskine449bd832023-01-11 14:50:10 +01005150 mbedtls_ssl_tls13_conf_early_data(conf, MBEDTLS_SSL_EARLY_DATA_DISABLED);
Jerry Yu6ee56aa2022-12-06 17:47:22 +08005151#if defined(MBEDTLS_SSL_SRV_C)
5152 mbedtls_ssl_tls13_conf_max_early_data_size(
Gilles Peskine449bd832023-01-11 14:50:10 +01005153 conf, MBEDTLS_SSL_MAX_EARLY_DATA_SIZE);
Jerry Yu6ee56aa2022-12-06 17:47:22 +08005154#endif
5155#endif /* MBEDTLS_SSL_EARLY_DATA */
5156
Jerry Yud0766ec2022-09-22 10:46:57 +08005157#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08005158 mbedtls_ssl_conf_new_session_tickets(
Gilles Peskine449bd832023-01-11 14:50:10 +01005159 conf, MBEDTLS_SSL_TLS1_3_DEFAULT_NEW_SESSION_TICKETS);
Jerry Yu1ad7ace2022-08-09 13:28:39 +08005160#endif
Hanno Becker71f1ed62021-07-24 06:01:47 +01005161 /*
5162 * Allow all TLS 1.3 key exchange modes by default.
5163 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00005164 conf->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Ronald Cron6f135e12021-12-08 16:57:54 +01005165#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01005166
Gilles Peskine449bd832023-01-11 14:50:10 +01005167 if (transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Glenn Strauss2dfcea22022-03-14 17:26:42 -04005168#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
XiaokangQian4d3a6042022-04-21 13:46:17 +00005169 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian060d8672022-04-21 09:24:56 +00005170 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian4d3a6042022-04-21 13:46:17 +00005171#else
Gilles Peskine449bd832023-01-11 14:50:10 +01005172 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQian060d8672022-04-21 09:24:56 +00005173#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005174 } else {
XiaokangQian4d3a6042022-04-21 13:46:17 +00005175#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01005176 if (endpoint == MBEDTLS_SSL_IS_CLIENT) {
XiaokangQian4d3a6042022-04-21 13:46:17 +00005177 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5178 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
Gilles Peskine449bd832023-01-11 14:50:10 +01005179 } else {
5180 /* Hybrid TLS 1.2 / 1.3 is not supported on server side yet */
XiaokangQian4d3a6042022-04-21 13:46:17 +00005181 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5182 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5183 }
5184#elif defined(MBEDTLS_SSL_PROTO_TLS1_3)
5185 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
5186 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
5187#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
5188 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5189 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5190#else
Gilles Peskine449bd832023-01-11 14:50:10 +01005191 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQian4d3a6042022-04-21 13:46:17 +00005192#endif
5193 }
Glenn Strauss2dfcea22022-03-14 17:26:42 -04005194
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005195 /*
5196 * Preset-specific defaults
5197 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005198 switch (preset) {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005199 /*
5200 * NSA Suite B
5201 */
5202 case MBEDTLS_SSL_PRESET_SUITEB:
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005203
Hanno Beckerd60b6c62021-04-29 12:04:11 +01005204 conf->ciphersuite_list = ssl_preset_suiteb_ciphersuites;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005205
5206#if defined(MBEDTLS_X509_CRT_PARSE_C)
5207 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005208#endif
5209
Ronald Crone68ab4f2022-10-05 12:46:29 +02005210#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08005211#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01005212 if (mbedtls_ssl_conf_is_tls12_only(conf)) {
Jerry Yu909df7b2022-01-22 11:56:27 +08005213 conf->sig_algs = ssl_tls12_preset_suiteb_sig_algs;
Gilles Peskine449bd832023-01-11 14:50:10 +01005214 } else
Jerry Yu909df7b2022-01-22 11:56:27 +08005215#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005216 conf->sig_algs = ssl_preset_suiteb_sig_algs;
Ronald Crone68ab4f2022-10-05 12:46:29 +02005217#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005218
Brett Warrene0edc842021-08-17 09:53:13 +01005219#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
5220 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005221#endif
Brett Warrene0edc842021-08-17 09:53:13 +01005222 conf->group_list = ssl_preset_suiteb_groups;
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02005223 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005224
5225 /*
5226 * Default
5227 */
5228 default:
Ronald Cronf6606552022-03-15 11:23:25 +01005229
Hanno Beckerd60b6c62021-04-29 12:04:11 +01005230 conf->ciphersuite_list = mbedtls_ssl_list_ciphersuites();
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005231
5232#if defined(MBEDTLS_X509_CRT_PARSE_C)
5233 conf->cert_profile = &mbedtls_x509_crt_profile_default;
5234#endif
5235
Ronald Crone68ab4f2022-10-05 12:46:29 +02005236#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08005237#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01005238 if (mbedtls_ssl_conf_is_tls12_only(conf)) {
Jerry Yu909df7b2022-01-22 11:56:27 +08005239 conf->sig_algs = ssl_tls12_preset_default_sig_algs;
Gilles Peskine449bd832023-01-11 14:50:10 +01005240 } else
Jerry Yu909df7b2022-01-22 11:56:27 +08005241#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005242 conf->sig_algs = ssl_preset_default_sig_algs;
Ronald Crone68ab4f2022-10-05 12:46:29 +02005243#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005244
Brett Warrene0edc842021-08-17 09:53:13 +01005245#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
5246 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005247#endif
Brett Warrene0edc842021-08-17 09:53:13 +01005248 conf->group_list = ssl_preset_default_groups;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005249
5250#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
5251 conf->dhm_min_bitlen = 1024;
5252#endif
5253 }
5254
Gilles Peskine449bd832023-01-11 14:50:10 +01005255 return 0;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005256}
5257
5258/*
5259 * Free mbedtls_ssl_config
5260 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005261void mbedtls_ssl_config_free(mbedtls_ssl_config *conf)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005262{
5263#if defined(MBEDTLS_DHM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005264 mbedtls_mpi_free(&conf->dhm_P);
5265 mbedtls_mpi_free(&conf->dhm_G);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005266#endif
5267
Ronald Cron73fe8df2022-10-05 14:31:43 +02005268#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02005269#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01005270 if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) {
Neil Armstrong501c9322022-05-03 09:35:09 +02005271 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
5272 }
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005273#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01005274 if (conf->psk != NULL) {
5275 mbedtls_platform_zeroize(conf->psk, conf->psk_len);
5276 mbedtls_free(conf->psk);
Azim Khan27e8a122018-03-21 14:24:11 +00005277 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005278 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09005279 }
5280
Gilles Peskine449bd832023-01-11 14:50:10 +01005281 if (conf->psk_identity != NULL) {
5282 mbedtls_platform_zeroize(conf->psk_identity, conf->psk_identity_len);
5283 mbedtls_free(conf->psk_identity);
Azim Khan27e8a122018-03-21 14:24:11 +00005284 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005285 conf->psk_identity_len = 0;
5286 }
Ronald Cron73fe8df2022-10-05 14:31:43 +02005287#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005288
5289#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005290 ssl_key_cert_free(conf->key_cert);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005291#endif
5292
Gilles Peskine449bd832023-01-11 14:50:10 +01005293 mbedtls_platform_zeroize(conf, sizeof(mbedtls_ssl_config));
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005294}
5295
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02005296#if defined(MBEDTLS_PK_C) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01005297 (defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C))
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005298/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005299 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005300 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005301unsigned char mbedtls_ssl_sig_from_pk(mbedtls_pk_context *pk)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005302{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005303#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005304 if (mbedtls_pk_can_do(pk, MBEDTLS_PK_RSA)) {
5305 return MBEDTLS_SSL_SIG_RSA;
5306 }
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005307#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005308#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005309 if (mbedtls_pk_can_do(pk, MBEDTLS_PK_ECDSA)) {
5310 return MBEDTLS_SSL_SIG_ECDSA;
5311 }
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005312#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005313 return MBEDTLS_SSL_SIG_ANON;
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005314}
5315
Gilles Peskine449bd832023-01-11 14:50:10 +01005316unsigned char mbedtls_ssl_sig_from_pk_alg(mbedtls_pk_type_t type)
Hanno Becker7e5437a2017-04-28 17:15:26 +01005317{
Gilles Peskine449bd832023-01-11 14:50:10 +01005318 switch (type) {
Hanno Becker7e5437a2017-04-28 17:15:26 +01005319 case MBEDTLS_PK_RSA:
Gilles Peskine449bd832023-01-11 14:50:10 +01005320 return MBEDTLS_SSL_SIG_RSA;
Hanno Becker7e5437a2017-04-28 17:15:26 +01005321 case MBEDTLS_PK_ECDSA:
5322 case MBEDTLS_PK_ECKEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01005323 return MBEDTLS_SSL_SIG_ECDSA;
Hanno Becker7e5437a2017-04-28 17:15:26 +01005324 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005325 return MBEDTLS_SSL_SIG_ANON;
Hanno Becker7e5437a2017-04-28 17:15:26 +01005326 }
5327}
5328
Gilles Peskine449bd832023-01-11 14:50:10 +01005329mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig(unsigned char sig)
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005330{
Gilles Peskine449bd832023-01-11 14:50:10 +01005331 switch (sig) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005332#if defined(MBEDTLS_RSA_C)
5333 case MBEDTLS_SSL_SIG_RSA:
Gilles Peskine449bd832023-01-11 14:50:10 +01005334 return MBEDTLS_PK_RSA;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005335#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005336#if defined(MBEDTLS_ECDSA_C)
5337 case MBEDTLS_SSL_SIG_ECDSA:
Gilles Peskine449bd832023-01-11 14:50:10 +01005338 return MBEDTLS_PK_ECDSA;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005339#endif
5340 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005341 return MBEDTLS_PK_NONE;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005342 }
5343}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02005344#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005345
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02005346/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005347 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02005348 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005349mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash(unsigned char hash)
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005350{
Gilles Peskine449bd832023-01-11 14:50:10 +01005351 switch (hash) {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005352#if defined(MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005353 case MBEDTLS_SSL_HASH_MD5:
Gilles Peskine449bd832023-01-11 14:50:10 +01005354 return MBEDTLS_MD_MD5;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005355#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005356#if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005357 case MBEDTLS_SSL_HASH_SHA1:
Gilles Peskine449bd832023-01-11 14:50:10 +01005358 return MBEDTLS_MD_SHA1;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005359#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005360#if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005361 case MBEDTLS_SSL_HASH_SHA224:
Gilles Peskine449bd832023-01-11 14:50:10 +01005362 return MBEDTLS_MD_SHA224;
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02005363#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005364#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005365 case MBEDTLS_SSL_HASH_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01005366 return MBEDTLS_MD_SHA256;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005367#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005368#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005369 case MBEDTLS_SSL_HASH_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01005370 return MBEDTLS_MD_SHA384;
Mateusz Starzyk3352a532021-04-06 14:28:22 +02005371#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005372#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005373 case MBEDTLS_SSL_HASH_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01005374 return MBEDTLS_MD_SHA512;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005375#endif
5376 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005377 return MBEDTLS_MD_NONE;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005378 }
5379}
5380
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005381/*
5382 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
5383 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005384unsigned char mbedtls_ssl_hash_from_md_alg(int md)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005385{
Gilles Peskine449bd832023-01-11 14:50:10 +01005386 switch (md) {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005387#if defined(MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005388 case MBEDTLS_MD_MD5:
Gilles Peskine449bd832023-01-11 14:50:10 +01005389 return MBEDTLS_SSL_HASH_MD5;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005390#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005391#if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005392 case MBEDTLS_MD_SHA1:
Gilles Peskine449bd832023-01-11 14:50:10 +01005393 return MBEDTLS_SSL_HASH_SHA1;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005394#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005395#if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005396 case MBEDTLS_MD_SHA224:
Gilles Peskine449bd832023-01-11 14:50:10 +01005397 return MBEDTLS_SSL_HASH_SHA224;
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02005398#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005399#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005400 case MBEDTLS_MD_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01005401 return MBEDTLS_SSL_HASH_SHA256;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005402#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005403#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005404 case MBEDTLS_MD_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01005405 return MBEDTLS_SSL_HASH_SHA384;
Mateusz Starzyk3352a532021-04-06 14:28:22 +02005406#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005407#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005408 case MBEDTLS_MD_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01005409 return MBEDTLS_SSL_HASH_SHA512;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005410#endif
5411 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005412 return MBEDTLS_SSL_HASH_NONE;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005413 }
5414}
5415
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005416/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005417 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005418 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005419 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005420int mbedtls_ssl_check_curve_tls_id(const mbedtls_ssl_context *ssl, uint16_t tls_id)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005421{
Gilles Peskine449bd832023-01-11 14:50:10 +01005422 const uint16_t *group_list = mbedtls_ssl_get_groups(ssl);
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005423
Gilles Peskine449bd832023-01-11 14:50:10 +01005424 if (group_list == NULL) {
5425 return -1;
Brett Warrene0edc842021-08-17 09:53:13 +01005426 }
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005427
Gilles Peskine449bd832023-01-11 14:50:10 +01005428 for (; *group_list != 0; group_list++) {
5429 if (*group_list == tls_id) {
5430 return 0;
5431 }
5432 }
5433
5434 return -1;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005435}
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005436
5437#if defined(MBEDTLS_ECP_C)
5438/*
5439 * Same as mbedtls_ssl_check_curve_tls_id() but with a mbedtls_ecp_group_id.
5440 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005441int mbedtls_ssl_check_curve(const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id)
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005442{
Gilles Peskine449bd832023-01-11 14:50:10 +01005443 uint16_t tls_id = mbedtls_ssl_get_tls_id_from_ecp_group_id(grp_id);
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005444
Gilles Peskine449bd832023-01-11 14:50:10 +01005445 if (tls_id == 0) {
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005446 return -1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005447 }
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005448
Gilles Peskine449bd832023-01-11 14:50:10 +01005449 return mbedtls_ssl_check_curve_tls_id(ssl, tls_id);
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005450}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02005451#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005452
Gilles Peskine449bd832023-01-11 14:50:10 +01005453#if defined(MBEDTLS_DEBUG_C)
Valerio Setti67419f02023-01-04 16:12:42 +01005454#define EC_NAME(_name_) _name_
5455#else
5456#define EC_NAME(_name_) NULL
5457#endif
5458
Valerio Setti18c9fed2022-12-30 17:44:24 +01005459static const struct {
5460 uint16_t tls_id;
5461 mbedtls_ecp_group_id ecp_group_id;
5462 psa_ecc_family_t psa_family;
5463 uint16_t bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01005464 const char *name;
Valerio Setti18c9fed2022-12-30 17:44:24 +01005465} tls_id_match_table[] =
5466{
5467#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_521)
Gilles Peskine449bd832023-01-11 14:50:10 +01005468 { 25, MBEDTLS_ECP_DP_SECP521R1, PSA_ECC_FAMILY_SECP_R1, 521, EC_NAME("secp521r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005469#endif
5470#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED) || defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512)
Gilles Peskine449bd832023-01-11 14:50:10 +01005471 { 28, MBEDTLS_ECP_DP_BP512R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 512, EC_NAME("brainpoolP512r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005472#endif
Valerio Setti67419f02023-01-04 16:12:42 +01005473#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_384)
Gilles Peskine449bd832023-01-11 14:50:10 +01005474 { 24, MBEDTLS_ECP_DP_SECP384R1, PSA_ECC_FAMILY_SECP_R1, 384, EC_NAME("secp384r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005475#endif
Valerio Setti67419f02023-01-04 16:12:42 +01005476#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED) || defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384)
Gilles Peskine449bd832023-01-11 14:50:10 +01005477 { 27, MBEDTLS_ECP_DP_BP384R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 384, EC_NAME("brainpoolP384r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005478#endif
Valerio Setti67419f02023-01-04 16:12:42 +01005479#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_256)
Gilles Peskine449bd832023-01-11 14:50:10 +01005480 { 23, MBEDTLS_ECP_DP_SECP256R1, PSA_ECC_FAMILY_SECP_R1, 256, EC_NAME("secp256r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005481#endif
5482#if defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED) || defined(PSA_WANT_ECC_SECP_K1_256)
Gilles Peskine449bd832023-01-11 14:50:10 +01005483 { 22, MBEDTLS_ECP_DP_SECP256K1, PSA_ECC_FAMILY_SECP_K1, 256, EC_NAME("secp256k1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005484#endif
Valerio Setti67419f02023-01-04 16:12:42 +01005485#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED) || defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256)
Gilles Peskine449bd832023-01-11 14:50:10 +01005486 { 26, MBEDTLS_ECP_DP_BP256R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 256, EC_NAME("brainpoolP256r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005487#endif
5488#if defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_224)
Gilles Peskine449bd832023-01-11 14:50:10 +01005489 { 21, MBEDTLS_ECP_DP_SECP224R1, PSA_ECC_FAMILY_SECP_R1, 224, EC_NAME("secp224r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005490#endif
5491#if defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED) || defined(PSA_WANT_ECC_SECP_K1_224)
Gilles Peskine449bd832023-01-11 14:50:10 +01005492 { 20, MBEDTLS_ECP_DP_SECP224K1, PSA_ECC_FAMILY_SECP_K1, 224, EC_NAME("secp224k1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005493#endif
5494#if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_192)
Gilles Peskine449bd832023-01-11 14:50:10 +01005495 { 19, MBEDTLS_ECP_DP_SECP192R1, PSA_ECC_FAMILY_SECP_R1, 192, EC_NAME("secp192r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005496#endif
5497#if defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED) || defined(PSA_WANT_ECC_SECP_K1_192)
Gilles Peskine449bd832023-01-11 14:50:10 +01005498 { 18, MBEDTLS_ECP_DP_SECP192K1, PSA_ECC_FAMILY_SECP_K1, 192, EC_NAME("secp192k1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005499#endif
5500#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED) || defined(PSA_WANT_ECC_MONTGOMERY_255)
Gilles Peskine449bd832023-01-11 14:50:10 +01005501 { 29, MBEDTLS_ECP_DP_CURVE25519, PSA_ECC_FAMILY_MONTGOMERY, 255, EC_NAME("x25519") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005502#endif
5503#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED) || defined(PSA_WANT_ECC_MONTGOMERY_448)
Gilles Peskine449bd832023-01-11 14:50:10 +01005504 { 30, MBEDTLS_ECP_DP_CURVE448, PSA_ECC_FAMILY_MONTGOMERY, 448, EC_NAME("x448") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005505#endif
5506 { 0, MBEDTLS_ECP_DP_NONE, 0, 0, NULL },
5507};
5508
Gilles Peskine449bd832023-01-11 14:50:10 +01005509int mbedtls_ssl_get_psa_curve_info_from_tls_id(uint16_t tls_id,
5510 psa_ecc_family_t *family,
5511 size_t *bits)
Valerio Setti18c9fed2022-12-30 17:44:24 +01005512{
Gilles Peskine449bd832023-01-11 14:50:10 +01005513 for (int i = 0; tls_id_match_table[i].tls_id != 0; i++) {
5514 if (tls_id_match_table[i].tls_id == tls_id) {
5515 if (family != NULL) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01005516 *family = tls_id_match_table[i].psa_family;
Gilles Peskine449bd832023-01-11 14:50:10 +01005517 }
5518 if (bits != NULL) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01005519 *bits = tls_id_match_table[i].bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01005520 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01005521 return PSA_SUCCESS;
5522 }
5523 }
5524
5525 return PSA_ERROR_NOT_SUPPORTED;
5526}
5527
Gilles Peskine449bd832023-01-11 14:50:10 +01005528mbedtls_ecp_group_id mbedtls_ssl_get_ecp_group_id_from_tls_id(uint16_t tls_id)
Valerio Setti18c9fed2022-12-30 17:44:24 +01005529{
Gilles Peskine449bd832023-01-11 14:50:10 +01005530 for (int i = 0; tls_id_match_table[i].tls_id != 0; i++) {
5531 if (tls_id_match_table[i].tls_id == tls_id) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01005532 return tls_id_match_table[i].ecp_group_id;
Gilles Peskine449bd832023-01-11 14:50:10 +01005533 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01005534 }
5535
5536 return MBEDTLS_ECP_DP_NONE;
5537}
5538
Gilles Peskine449bd832023-01-11 14:50:10 +01005539uint16_t mbedtls_ssl_get_tls_id_from_ecp_group_id(mbedtls_ecp_group_id grp_id)
Valerio Setti18c9fed2022-12-30 17:44:24 +01005540{
Gilles Peskine449bd832023-01-11 14:50:10 +01005541 for (int i = 0; tls_id_match_table[i].ecp_group_id != MBEDTLS_ECP_DP_NONE;
5542 i++) {
5543 if (tls_id_match_table[i].ecp_group_id == grp_id) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01005544 return tls_id_match_table[i].tls_id;
Gilles Peskine449bd832023-01-11 14:50:10 +01005545 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01005546 }
5547
5548 return 0;
5549}
5550
Valerio Setti67419f02023-01-04 16:12:42 +01005551#if defined(MBEDTLS_DEBUG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005552const char *mbedtls_ssl_get_curve_name_from_tls_id(uint16_t tls_id)
Valerio Setti18c9fed2022-12-30 17:44:24 +01005553{
Gilles Peskine449bd832023-01-11 14:50:10 +01005554 for (int i = 0; tls_id_match_table[i].tls_id != 0; i++) {
5555 if (tls_id_match_table[i].tls_id == tls_id) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01005556 return tls_id_match_table[i].name;
Gilles Peskine449bd832023-01-11 14:50:10 +01005557 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01005558 }
5559
5560 return NULL;
5561}
Valerio Setti67419f02023-01-04 16:12:42 +01005562#endif
Valerio Setti18c9fed2022-12-30 17:44:24 +01005563
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005564#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005565int mbedtls_ssl_check_cert_usage(const mbedtls_x509_crt *cert,
5566 const mbedtls_ssl_ciphersuite_t *ciphersuite,
5567 int cert_endpoint,
5568 uint32_t *flags)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005569{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005570 int ret = 0;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005571 int usage = 0;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005572 const char *ext_oid;
5573 size_t ext_len;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005574
Gilles Peskine449bd832023-01-11 14:50:10 +01005575 if (cert_endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005576 /* Server part of the key exchange */
Gilles Peskine449bd832023-01-11 14:50:10 +01005577 switch (ciphersuite->key_exchange) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005578 case MBEDTLS_KEY_EXCHANGE_RSA:
5579 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005580 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005581 break;
5582
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005583 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
5584 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
5585 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
5586 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005587 break;
5588
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005589 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
5590 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005591 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005592 break;
5593
5594 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005595 case MBEDTLS_KEY_EXCHANGE_NONE:
5596 case MBEDTLS_KEY_EXCHANGE_PSK:
5597 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
5598 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02005599 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005600 usage = 0;
5601 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005602 } else {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005603 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
5604 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005605 }
5606
Gilles Peskine449bd832023-01-11 14:50:10 +01005607 if (mbedtls_x509_crt_check_key_usage(cert, usage) != 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005608 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005609 ret = -1;
5610 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005611
Gilles Peskine449bd832023-01-11 14:50:10 +01005612 if (cert_endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005613 ext_oid = MBEDTLS_OID_SERVER_AUTH;
Gilles Peskine449bd832023-01-11 14:50:10 +01005614 ext_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_SERVER_AUTH);
5615 } else {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005616 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
Gilles Peskine449bd832023-01-11 14:50:10 +01005617 ext_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_CLIENT_AUTH);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005618 }
5619
Gilles Peskine449bd832023-01-11 14:50:10 +01005620 if (mbedtls_x509_crt_check_extended_key_usage(cert, ext_oid, ext_len) != 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005621 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005622 ret = -1;
5623 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005624
Gilles Peskine449bd832023-01-11 14:50:10 +01005625 return ret;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005626}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005627#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02005628
Jerry Yu148165c2021-09-24 23:20:59 +08005629#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01005630int mbedtls_ssl_get_handshake_transcript(mbedtls_ssl_context *ssl,
5631 const mbedtls_md_type_t md,
5632 unsigned char *dst,
5633 size_t dst_len,
5634 size_t *olen)
Jerry Yu148165c2021-09-24 23:20:59 +08005635{
Ronald Cronf6893e12022-01-07 22:09:01 +01005636 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5637 psa_hash_operation_t *hash_operation_to_clone;
5638 psa_hash_operation_t hash_operation = psa_hash_operation_init();
5639
Jerry Yu148165c2021-09-24 23:20:59 +08005640 *olen = 0;
Ronald Cronf6893e12022-01-07 22:09:01 +01005641
Gilles Peskine449bd832023-01-11 14:50:10 +01005642 switch (md) {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005643#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01005644 case MBEDTLS_MD_SHA384:
5645 hash_operation_to_clone = &ssl->handshake->fin_sha384_psa;
5646 break;
Ronald Cronf6893e12022-01-07 22:09:01 +01005647#endif
5648
Andrzej Kurek25f27152022-08-17 16:09:31 -04005649#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01005650 case MBEDTLS_MD_SHA256:
5651 hash_operation_to_clone = &ssl->handshake->fin_sha256_psa;
5652 break;
Ronald Cronf6893e12022-01-07 22:09:01 +01005653#endif
5654
Gilles Peskine449bd832023-01-11 14:50:10 +01005655 default:
5656 goto exit;
5657 }
5658
5659 status = psa_hash_clone(hash_operation_to_clone, &hash_operation);
5660 if (status != PSA_SUCCESS) {
Ronald Cronf6893e12022-01-07 22:09:01 +01005661 goto exit;
5662 }
5663
Gilles Peskine449bd832023-01-11 14:50:10 +01005664 status = psa_hash_finish(&hash_operation, dst, dst_len, olen);
5665 if (status != PSA_SUCCESS) {
Ronald Cronf6893e12022-01-07 22:09:01 +01005666 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005667 }
Ronald Cronf6893e12022-01-07 22:09:01 +01005668
5669exit:
Andrzej Kurekeabeb302022-10-17 07:52:51 -04005670#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
5671 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
5672 (void) ssl;
5673#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005674 return psa_ssl_status_to_mbedtls(status);
Jerry Yu148165c2021-09-24 23:20:59 +08005675}
5676#else /* MBEDTLS_USE_PSA_CRYPTO */
5677
Andrzej Kurek25f27152022-08-17 16:09:31 -04005678#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005679MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01005680static int ssl_get_handshake_transcript_sha384(mbedtls_ssl_context *ssl,
5681 unsigned char *dst,
5682 size_t dst_len,
5683 size_t *olen)
Jerry Yu24c0ec32021-09-09 14:21:07 +08005684{
Jerry Yu24c0ec32021-09-09 14:21:07 +08005685 int ret;
5686 mbedtls_sha512_context sha512;
5687
Gilles Peskine449bd832023-01-11 14:50:10 +01005688 if (dst_len < 48) {
5689 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
5690 }
Jerry Yu24c0ec32021-09-09 14:21:07 +08005691
Gilles Peskine449bd832023-01-11 14:50:10 +01005692 mbedtls_sha512_init(&sha512);
5693 mbedtls_sha512_clone(&sha512, &ssl->handshake->fin_sha384);
Jerry Yu24c0ec32021-09-09 14:21:07 +08005694
Gilles Peskine449bd832023-01-11 14:50:10 +01005695 if ((ret = mbedtls_sha512_finish(&sha512, dst)) != 0) {
5696 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_sha512_finish", ret);
Jerry Yu24c0ec32021-09-09 14:21:07 +08005697 goto exit;
5698 }
5699
5700 *olen = 48;
5701
5702exit:
5703
Gilles Peskine449bd832023-01-11 14:50:10 +01005704 mbedtls_sha512_free(&sha512);
5705 return ret;
Jerry Yu24c0ec32021-09-09 14:21:07 +08005706}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005707#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005708
Andrzej Kurek25f27152022-08-17 16:09:31 -04005709#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005710MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01005711static int ssl_get_handshake_transcript_sha256(mbedtls_ssl_context *ssl,
5712 unsigned char *dst,
5713 size_t dst_len,
5714 size_t *olen)
Jerry Yu24c0ec32021-09-09 14:21:07 +08005715{
Jerry Yu24c0ec32021-09-09 14:21:07 +08005716 int ret;
5717 mbedtls_sha256_context sha256;
5718
Gilles Peskine449bd832023-01-11 14:50:10 +01005719 if (dst_len < 32) {
5720 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
5721 }
Jerry Yu24c0ec32021-09-09 14:21:07 +08005722
Gilles Peskine449bd832023-01-11 14:50:10 +01005723 mbedtls_sha256_init(&sha256);
5724 mbedtls_sha256_clone(&sha256, &ssl->handshake->fin_sha256);
Jerry Yuc5aef882021-12-23 20:15:02 +08005725
Gilles Peskine449bd832023-01-11 14:50:10 +01005726 if ((ret = mbedtls_sha256_finish(&sha256, dst)) != 0) {
5727 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_sha256_finish", ret);
Jerry Yu24c0ec32021-09-09 14:21:07 +08005728 goto exit;
5729 }
5730
5731 *olen = 32;
5732
5733exit:
5734
Gilles Peskine449bd832023-01-11 14:50:10 +01005735 mbedtls_sha256_free(&sha256);
5736 return ret;
Jerry Yu24c0ec32021-09-09 14:21:07 +08005737}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005738#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005739
Gilles Peskine449bd832023-01-11 14:50:10 +01005740int mbedtls_ssl_get_handshake_transcript(mbedtls_ssl_context *ssl,
5741 const mbedtls_md_type_t md,
5742 unsigned char *dst,
5743 size_t dst_len,
5744 size_t *olen)
Jerry Yu24c0ec32021-09-09 14:21:07 +08005745{
Gilles Peskine449bd832023-01-11 14:50:10 +01005746 switch (md) {
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005747
Andrzej Kurek25f27152022-08-17 16:09:31 -04005748#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01005749 case MBEDTLS_MD_SHA384:
5750 return ssl_get_handshake_transcript_sha384(ssl, dst, dst_len, olen);
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005751#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005752
Andrzej Kurek25f27152022-08-17 16:09:31 -04005753#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01005754 case MBEDTLS_MD_SHA256:
5755 return ssl_get_handshake_transcript_sha256(ssl, dst, dst_len, olen);
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005756#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005757
Gilles Peskine449bd832023-01-11 14:50:10 +01005758 default:
Andrzej Kurek409248a2022-10-24 10:33:21 -04005759#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01005760 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
5761 (void) ssl;
5762 (void) dst;
5763 (void) dst_len;
5764 (void) olen;
Andrzej Kurek409248a2022-10-24 10:33:21 -04005765#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005766 break;
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005767 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005768 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu24c0ec32021-09-09 14:21:07 +08005769}
XiaokangQian647719a2021-12-07 09:16:29 +00005770
Jerry Yu148165c2021-09-24 23:20:59 +08005771#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005772
Ronald Crone68ab4f2022-10-05 12:46:29 +02005773#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Gabor Mezei078e8032022-04-27 21:17:56 +02005774/* mbedtls_ssl_parse_sig_alg_ext()
5775 *
5776 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
5777 * value (TLS 1.3 RFC8446):
5778 * enum {
5779 * ....
5780 * ecdsa_secp256r1_sha256( 0x0403 ),
5781 * ecdsa_secp384r1_sha384( 0x0503 ),
5782 * ecdsa_secp521r1_sha512( 0x0603 ),
5783 * ....
5784 * } SignatureScheme;
5785 *
5786 * struct {
5787 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
5788 * } SignatureSchemeList;
5789 *
5790 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
5791 * value (TLS 1.2 RFC5246):
5792 * enum {
5793 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
5794 * sha512(6), (255)
5795 * } HashAlgorithm;
5796 *
5797 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
5798 * SignatureAlgorithm;
5799 *
5800 * struct {
5801 * HashAlgorithm hash;
5802 * SignatureAlgorithm signature;
5803 * } SignatureAndHashAlgorithm;
5804 *
5805 * SignatureAndHashAlgorithm
5806 * supported_signature_algorithms<2..2^16-2>;
5807 *
5808 * The TLS 1.3 signature algorithm extension was defined to be a compatible
5809 * generalization of the TLS 1.2 signature algorithm extension.
5810 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
5811 * `SignatureScheme` field of TLS 1.3
5812 *
5813 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005814int mbedtls_ssl_parse_sig_alg_ext(mbedtls_ssl_context *ssl,
5815 const unsigned char *buf,
5816 const unsigned char *end)
Gabor Mezei078e8032022-04-27 21:17:56 +02005817{
5818 const unsigned char *p = buf;
5819 size_t supported_sig_algs_len = 0;
5820 const unsigned char *supported_sig_algs_end;
5821 uint16_t sig_alg;
5822 uint32_t common_idx = 0;
5823
Gilles Peskine449bd832023-01-11 14:50:10 +01005824 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
5825 supported_sig_algs_len = MBEDTLS_GET_UINT16_BE(p, 0);
Gabor Mezei078e8032022-04-27 21:17:56 +02005826 p += 2;
5827
Gilles Peskine449bd832023-01-11 14:50:10 +01005828 memset(ssl->handshake->received_sig_algs, 0,
5829 sizeof(ssl->handshake->received_sig_algs));
Gabor Mezei078e8032022-04-27 21:17:56 +02005830
Gilles Peskine449bd832023-01-11 14:50:10 +01005831 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, supported_sig_algs_len);
Gabor Mezei078e8032022-04-27 21:17:56 +02005832 supported_sig_algs_end = p + supported_sig_algs_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01005833 while (p < supported_sig_algs_end) {
5834 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, supported_sig_algs_end, 2);
5835 sig_alg = MBEDTLS_GET_UINT16_BE(p, 0);
Gabor Mezei078e8032022-04-27 21:17:56 +02005836 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01005837 MBEDTLS_SSL_DEBUG_MSG(4, ("received signature algorithm: 0x%x %s",
5838 sig_alg,
5839 mbedtls_ssl_sig_alg_to_str(sig_alg)));
Jerry Yu2fe6c632022-06-29 10:02:38 +08005840#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01005841 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 &&
5842 (!(mbedtls_ssl_sig_alg_is_supported(ssl, sig_alg) &&
5843 mbedtls_ssl_sig_alg_is_offered(ssl, sig_alg)))) {
Gabor Mezei078e8032022-04-27 21:17:56 +02005844 continue;
Jerry Yu2fe6c632022-06-29 10:02:38 +08005845 }
5846#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gabor Mezei078e8032022-04-27 21:17:56 +02005847
Gilles Peskine449bd832023-01-11 14:50:10 +01005848 MBEDTLS_SSL_DEBUG_MSG(4, ("valid signature algorithm: %s",
5849 mbedtls_ssl_sig_alg_to_str(sig_alg)));
Gabor Mezei078e8032022-04-27 21:17:56 +02005850
Gilles Peskine449bd832023-01-11 14:50:10 +01005851 if (common_idx + 1 < MBEDTLS_RECEIVED_SIG_ALGS_SIZE) {
Gabor Mezei078e8032022-04-27 21:17:56 +02005852 ssl->handshake->received_sig_algs[common_idx] = sig_alg;
5853 common_idx += 1;
5854 }
5855 }
5856 /* Check that we consumed all the message. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005857 if (p != end) {
5858 MBEDTLS_SSL_DEBUG_MSG(1,
5859 ("Signature algorithms extension length misaligned"));
5860 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
5861 MBEDTLS_ERR_SSL_DECODE_ERROR);
5862 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Gabor Mezei078e8032022-04-27 21:17:56 +02005863 }
5864
Gilles Peskine449bd832023-01-11 14:50:10 +01005865 if (common_idx == 0) {
5866 MBEDTLS_SSL_DEBUG_MSG(3, ("no signature algorithm in common"));
5867 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
5868 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
5869 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Gabor Mezei078e8032022-04-27 21:17:56 +02005870 }
5871
Gabor Mezei15b95a62022-05-09 16:37:58 +02005872 ssl->handshake->received_sig_algs[common_idx] = MBEDTLS_TLS_SIG_NONE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005873 return 0;
Gabor Mezei078e8032022-04-27 21:17:56 +02005874}
5875
Ronald Crone68ab4f2022-10-05 12:46:29 +02005876#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Gabor Mezei078e8032022-04-27 21:17:56 +02005877
Jerry Yudc7bd172022-02-17 13:44:15 +08005878#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5879
5880#if defined(MBEDTLS_USE_PSA_CRYPTO)
5881
Gilles Peskine449bd832023-01-11 14:50:10 +01005882static psa_status_t setup_psa_key_derivation(psa_key_derivation_operation_t *derivation,
5883 mbedtls_svc_key_id_t key,
5884 psa_algorithm_t alg,
5885 const unsigned char *raw_psk, size_t raw_psk_length,
5886 const unsigned char *seed, size_t seed_length,
5887 const unsigned char *label, size_t label_length,
5888 const unsigned char *other_secret,
5889 size_t other_secret_length,
5890 size_t capacity)
Jerry Yudc7bd172022-02-17 13:44:15 +08005891{
5892 psa_status_t status;
5893
Gilles Peskine449bd832023-01-11 14:50:10 +01005894 status = psa_key_derivation_setup(derivation, alg);
5895 if (status != PSA_SUCCESS) {
5896 return status;
5897 }
Jerry Yudc7bd172022-02-17 13:44:15 +08005898
Gilles Peskine449bd832023-01-11 14:50:10 +01005899 if (PSA_ALG_IS_TLS12_PRF(alg) || PSA_ALG_IS_TLS12_PSK_TO_MS(alg)) {
5900 status = psa_key_derivation_input_bytes(derivation,
5901 PSA_KEY_DERIVATION_INPUT_SEED,
5902 seed, seed_length);
5903 if (status != PSA_SUCCESS) {
5904 return status;
Przemek Stekiel1f027032022-04-05 17:12:11 +02005905 }
5906
Gilles Peskine449bd832023-01-11 14:50:10 +01005907 if (other_secret != NULL) {
5908 status = psa_key_derivation_input_bytes(derivation,
5909 PSA_KEY_DERIVATION_INPUT_OTHER_SECRET,
5910 other_secret, other_secret_length);
5911 if (status != PSA_SUCCESS) {
5912 return status;
5913 }
5914 }
5915
5916 if (mbedtls_svc_key_id_is_null(key)) {
Jerry Yudc7bd172022-02-17 13:44:15 +08005917 status = psa_key_derivation_input_bytes(
5918 derivation, PSA_KEY_DERIVATION_INPUT_SECRET,
Gilles Peskine449bd832023-01-11 14:50:10 +01005919 raw_psk, raw_psk_length);
5920 } else {
Jerry Yudc7bd172022-02-17 13:44:15 +08005921 status = psa_key_derivation_input_key(
Gilles Peskine449bd832023-01-11 14:50:10 +01005922 derivation, PSA_KEY_DERIVATION_INPUT_SECRET, key);
Jerry Yudc7bd172022-02-17 13:44:15 +08005923 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005924 if (status != PSA_SUCCESS) {
5925 return status;
5926 }
Jerry Yudc7bd172022-02-17 13:44:15 +08005927
Gilles Peskine449bd832023-01-11 14:50:10 +01005928 status = psa_key_derivation_input_bytes(derivation,
5929 PSA_KEY_DERIVATION_INPUT_LABEL,
5930 label, label_length);
5931 if (status != PSA_SUCCESS) {
5932 return status;
5933 }
5934 } else {
5935 return PSA_ERROR_NOT_SUPPORTED;
Jerry Yudc7bd172022-02-17 13:44:15 +08005936 }
5937
Gilles Peskine449bd832023-01-11 14:50:10 +01005938 status = psa_key_derivation_set_capacity(derivation, capacity);
5939 if (status != PSA_SUCCESS) {
5940 return status;
5941 }
Jerry Yudc7bd172022-02-17 13:44:15 +08005942
Gilles Peskine449bd832023-01-11 14:50:10 +01005943 return PSA_SUCCESS;
Jerry Yudc7bd172022-02-17 13:44:15 +08005944}
5945
Andrzej Kurek57d10632022-10-24 10:32:01 -04005946#if defined(PSA_WANT_ALG_SHA_384) || \
5947 defined(PSA_WANT_ALG_SHA_256)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005948MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01005949static int tls_prf_generic(mbedtls_md_type_t md_type,
5950 const unsigned char *secret, size_t slen,
5951 const char *label,
5952 const unsigned char *random, size_t rlen,
5953 unsigned char *dstbuf, size_t dlen)
Jerry Yudc7bd172022-02-17 13:44:15 +08005954{
5955 psa_status_t status;
5956 psa_algorithm_t alg;
5957 mbedtls_svc_key_id_t master_key = MBEDTLS_SVC_KEY_ID_INIT;
5958 psa_key_derivation_operation_t derivation =
5959 PSA_KEY_DERIVATION_OPERATION_INIT;
5960
Gilles Peskine449bd832023-01-11 14:50:10 +01005961 if (md_type == MBEDTLS_MD_SHA384) {
Jerry Yudc7bd172022-02-17 13:44:15 +08005962 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
Gilles Peskine449bd832023-01-11 14:50:10 +01005963 } else {
Jerry Yudc7bd172022-02-17 13:44:15 +08005964 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
Gilles Peskine449bd832023-01-11 14:50:10 +01005965 }
Jerry Yudc7bd172022-02-17 13:44:15 +08005966
5967 /* Normally a "secret" should be long enough to be impossible to
5968 * find by brute force, and in particular should not be empty. But
5969 * this PRF is also used to derive an IV, in particular in EAP-TLS,
5970 * and for this use case it makes sense to have a 0-length "secret".
5971 * Since the key API doesn't allow importing a key of length 0,
5972 * keep master_key=0, which setup_psa_key_derivation() understands
5973 * to mean a 0-length "secret" input. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005974 if (slen != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08005975 psa_key_attributes_t key_attributes = psa_key_attributes_init();
Gilles Peskine449bd832023-01-11 14:50:10 +01005976 psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE);
5977 psa_set_key_algorithm(&key_attributes, alg);
5978 psa_set_key_type(&key_attributes, PSA_KEY_TYPE_DERIVE);
Jerry Yudc7bd172022-02-17 13:44:15 +08005979
Gilles Peskine449bd832023-01-11 14:50:10 +01005980 status = psa_import_key(&key_attributes, secret, slen, &master_key);
5981 if (status != PSA_SUCCESS) {
5982 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
5983 }
Jerry Yudc7bd172022-02-17 13:44:15 +08005984 }
5985
Gilles Peskine449bd832023-01-11 14:50:10 +01005986 status = setup_psa_key_derivation(&derivation,
5987 master_key, alg,
5988 NULL, 0,
5989 random, rlen,
5990 (unsigned char const *) label,
5991 (size_t) strlen(label),
5992 NULL, 0,
5993 dlen);
5994 if (status != PSA_SUCCESS) {
5995 psa_key_derivation_abort(&derivation);
5996 psa_destroy_key(master_key);
5997 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yudc7bd172022-02-17 13:44:15 +08005998 }
5999
Gilles Peskine449bd832023-01-11 14:50:10 +01006000 status = psa_key_derivation_output_bytes(&derivation, dstbuf, dlen);
6001 if (status != PSA_SUCCESS) {
6002 psa_key_derivation_abort(&derivation);
6003 psa_destroy_key(master_key);
6004 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yudc7bd172022-02-17 13:44:15 +08006005 }
6006
Gilles Peskine449bd832023-01-11 14:50:10 +01006007 status = psa_key_derivation_abort(&derivation);
6008 if (status != PSA_SUCCESS) {
6009 psa_destroy_key(master_key);
6010 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yudc7bd172022-02-17 13:44:15 +08006011 }
6012
Gilles Peskine449bd832023-01-11 14:50:10 +01006013 if (!mbedtls_svc_key_id_is_null(master_key)) {
6014 status = psa_destroy_key(master_key);
6015 }
6016 if (status != PSA_SUCCESS) {
6017 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
6018 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006019
Gilles Peskine449bd832023-01-11 14:50:10 +01006020 return 0;
Jerry Yudc7bd172022-02-17 13:44:15 +08006021}
Andrzej Kurek57d10632022-10-24 10:32:01 -04006022#endif /* PSA_WANT_ALG_SHA_256 || PSA_WANT_ALG_SHA_384 */
Jerry Yudc7bd172022-02-17 13:44:15 +08006023#else /* MBEDTLS_USE_PSA_CRYPTO */
6024
Andrzej Kurek57d10632022-10-24 10:32:01 -04006025#if defined(MBEDTLS_MD_C) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01006026 (defined(MBEDTLS_SHA256_C) || \
6027 defined(MBEDTLS_SHA384_C))
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006028MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006029static int tls_prf_generic(mbedtls_md_type_t md_type,
6030 const unsigned char *secret, size_t slen,
6031 const char *label,
6032 const unsigned char *random, size_t rlen,
6033 unsigned char *dstbuf, size_t dlen)
Jerry Yudc7bd172022-02-17 13:44:15 +08006034{
6035 size_t nb;
6036 size_t i, j, k, md_len;
6037 unsigned char *tmp;
6038 size_t tmp_len = 0;
6039 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
6040 const mbedtls_md_info_t *md_info;
6041 mbedtls_md_context_t md_ctx;
6042 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6043
Gilles Peskine449bd832023-01-11 14:50:10 +01006044 mbedtls_md_init(&md_ctx);
Jerry Yudc7bd172022-02-17 13:44:15 +08006045
Gilles Peskine449bd832023-01-11 14:50:10 +01006046 if ((md_info = mbedtls_md_info_from_type(md_type)) == NULL) {
6047 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
6048 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006049
Gilles Peskine449bd832023-01-11 14:50:10 +01006050 md_len = mbedtls_md_get_size(md_info);
Jerry Yudc7bd172022-02-17 13:44:15 +08006051
Gilles Peskine449bd832023-01-11 14:50:10 +01006052 tmp_len = md_len + strlen(label) + rlen;
6053 tmp = mbedtls_calloc(1, tmp_len);
6054 if (tmp == NULL) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006055 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
6056 goto exit;
6057 }
6058
Gilles Peskine449bd832023-01-11 14:50:10 +01006059 nb = strlen(label);
6060 memcpy(tmp + md_len, label, nb);
6061 memcpy(tmp + md_len + nb, random, rlen);
Jerry Yudc7bd172022-02-17 13:44:15 +08006062 nb += rlen;
6063
6064 /*
6065 * Compute P_<hash>(secret, label + random)[0..dlen]
6066 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006067 if ((ret = mbedtls_md_setup(&md_ctx, md_info, 1)) != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006068 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006069 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006070
Gilles Peskine449bd832023-01-11 14:50:10 +01006071 ret = mbedtls_md_hmac_starts(&md_ctx, secret, slen);
6072 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006073 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006074 }
6075 ret = mbedtls_md_hmac_update(&md_ctx, tmp + md_len, nb);
6076 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006077 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006078 }
6079 ret = mbedtls_md_hmac_finish(&md_ctx, tmp);
6080 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006081 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006082 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006083
Gilles Peskine449bd832023-01-11 14:50:10 +01006084 for (i = 0; i < dlen; i += md_len) {
6085 ret = mbedtls_md_hmac_reset(&md_ctx);
6086 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006087 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006088 }
6089 ret = mbedtls_md_hmac_update(&md_ctx, tmp, md_len + nb);
6090 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006091 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006092 }
6093 ret = mbedtls_md_hmac_finish(&md_ctx, h_i);
6094 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006095 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006096 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006097
Gilles Peskine449bd832023-01-11 14:50:10 +01006098 ret = mbedtls_md_hmac_reset(&md_ctx);
6099 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006100 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006101 }
6102 ret = mbedtls_md_hmac_update(&md_ctx, tmp, md_len);
6103 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006104 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006105 }
6106 ret = mbedtls_md_hmac_finish(&md_ctx, tmp);
6107 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006108 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006109 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006110
Gilles Peskine449bd832023-01-11 14:50:10 +01006111 k = (i + md_len > dlen) ? dlen % md_len : md_len;
Jerry Yudc7bd172022-02-17 13:44:15 +08006112
Gilles Peskine449bd832023-01-11 14:50:10 +01006113 for (j = 0; j < k; j++) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006114 dstbuf[i + j] = h_i[j];
Gilles Peskine449bd832023-01-11 14:50:10 +01006115 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006116 }
6117
6118exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006119 mbedtls_md_free(&md_ctx);
Jerry Yudc7bd172022-02-17 13:44:15 +08006120
Gilles Peskine449bd832023-01-11 14:50:10 +01006121 if (tmp != NULL) {
6122 mbedtls_platform_zeroize(tmp, tmp_len);
6123 }
Dave Rodgman29b9b2b2022-11-01 16:08:14 +00006124
Gilles Peskine449bd832023-01-11 14:50:10 +01006125 mbedtls_platform_zeroize(h_i, sizeof(h_i));
Jerry Yudc7bd172022-02-17 13:44:15 +08006126
Gilles Peskine449bd832023-01-11 14:50:10 +01006127 mbedtls_free(tmp);
Jerry Yudc7bd172022-02-17 13:44:15 +08006128
Gilles Peskine449bd832023-01-11 14:50:10 +01006129 return ret;
Jerry Yudc7bd172022-02-17 13:44:15 +08006130}
Andrzej Kurek57d10632022-10-24 10:32:01 -04006131#endif /* MBEDTLS_MD_C && ( MBEDTLS_SHA256_C || MBEDTLS_SHA384_C ) */
Jerry Yudc7bd172022-02-17 13:44:15 +08006132#endif /* MBEDTLS_USE_PSA_CRYPTO */
6133
Andrzej Kurek25f27152022-08-17 16:09:31 -04006134#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006135MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006136static int tls_prf_sha256(const unsigned char *secret, size_t slen,
6137 const char *label,
6138 const unsigned char *random, size_t rlen,
6139 unsigned char *dstbuf, size_t dlen)
Jerry Yudc7bd172022-02-17 13:44:15 +08006140{
Gilles Peskine449bd832023-01-11 14:50:10 +01006141 return tls_prf_generic(MBEDTLS_MD_SHA256, secret, slen,
6142 label, random, rlen, dstbuf, dlen);
Jerry Yudc7bd172022-02-17 13:44:15 +08006143}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04006144#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudc7bd172022-02-17 13:44:15 +08006145
Andrzej Kurek25f27152022-08-17 16:09:31 -04006146#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006147MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006148static int tls_prf_sha384(const unsigned char *secret, size_t slen,
6149 const char *label,
6150 const unsigned char *random, size_t rlen,
6151 unsigned char *dstbuf, size_t dlen)
Jerry Yudc7bd172022-02-17 13:44:15 +08006152{
Gilles Peskine449bd832023-01-11 14:50:10 +01006153 return tls_prf_generic(MBEDTLS_MD_SHA384, secret, slen,
6154 label, random, rlen, dstbuf, dlen);
Jerry Yudc7bd172022-02-17 13:44:15 +08006155}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04006156#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudc7bd172022-02-17 13:44:15 +08006157
Jerry Yuf009d862022-02-17 14:01:37 +08006158/*
6159 * Set appropriate PRF function and other SSL / TLS1.2 functions
6160 *
6161 * Inputs:
Jerry Yuf009d862022-02-17 14:01:37 +08006162 * - hash associated with the ciphersuite (only used by TLS 1.2)
6163 *
6164 * Outputs:
6165 * - the tls_prf, calc_verify and calc_finished members of handshake structure
6166 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006167MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006168static int ssl_set_handshake_prfs(mbedtls_ssl_handshake_params *handshake,
6169 mbedtls_md_type_t hash)
Jerry Yuf009d862022-02-17 14:01:37 +08006170{
Andrzej Kurek25f27152022-08-17 16:09:31 -04006171#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01006172 if (hash == MBEDTLS_MD_SHA384) {
Jerry Yuf009d862022-02-17 14:01:37 +08006173 handshake->tls_prf = tls_prf_sha384;
6174 handshake->calc_verify = ssl_calc_verify_tls_sha384;
6175 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Gilles Peskine449bd832023-01-11 14:50:10 +01006176 } else
Jerry Yuf009d862022-02-17 14:01:37 +08006177#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04006178#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuf009d862022-02-17 14:01:37 +08006179 {
Ronald Cron81591aa2022-03-07 09:05:51 +01006180 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08006181 handshake->tls_prf = tls_prf_sha256;
6182 handshake->calc_verify = ssl_calc_verify_tls_sha256;
6183 handshake->calc_finished = ssl_calc_finished_tls_sha256;
6184 }
Ronald Cron81591aa2022-03-07 09:05:51 +01006185#else
Jerry Yuf009d862022-02-17 14:01:37 +08006186 {
Jerry Yuf009d862022-02-17 14:01:37 +08006187 (void) handshake;
Ronald Cron81591aa2022-03-07 09:05:51 +01006188 (void) hash;
Gilles Peskine449bd832023-01-11 14:50:10 +01006189 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yuf009d862022-02-17 14:01:37 +08006190 }
Ronald Cron81591aa2022-03-07 09:05:51 +01006191#endif
Jerry Yuf009d862022-02-17 14:01:37 +08006192
Gilles Peskine449bd832023-01-11 14:50:10 +01006193 return 0;
Jerry Yuf009d862022-02-17 14:01:37 +08006194}
Jerry Yud6ab2352022-02-17 14:03:43 +08006195
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006196/*
6197 * Compute master secret if needed
6198 *
6199 * Parameters:
6200 * [in/out] handshake
6201 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
6202 * (PSA-PSK) ciphersuite_info, psk_opaque
6203 * [out] premaster (cleared)
6204 * [out] master
6205 * [in] ssl: optionally used for debugging, EMS and PSA-PSK
6206 * debug: conf->f_dbg, conf->p_dbg
6207 * EMS: passed to calc_verify (debug + session_negotiate)
Ronald Crona25cf582022-03-07 11:10:36 +01006208 * PSA-PSA: conf
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006209 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006210MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006211static int ssl_compute_master(mbedtls_ssl_handshake_params *handshake,
6212 unsigned char *master,
6213 const mbedtls_ssl_context *ssl)
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006214{
6215 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6216
6217 /* cf. RFC 5246, Section 8.1:
6218 * "The master secret is always exactly 48 bytes in length." */
6219 size_t const master_secret_len = 48;
6220
6221#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
6222 unsigned char session_hash[48];
6223#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
6224
6225 /* The label for the KDF used for key expansion.
6226 * This is either "master secret" or "extended master secret"
6227 * depending on whether the Extended Master Secret extension
6228 * is used. */
6229 char const *lbl = "master secret";
6230
Przemek Stekielae4ed302022-04-05 17:15:55 +02006231 /* The seed for the KDF used for key expansion.
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006232 * - If the Extended Master Secret extension is not used,
6233 * this is ClientHello.Random + ServerHello.Random
6234 * (see Sect. 8.1 in RFC 5246).
6235 * - If the Extended Master Secret extension is used,
6236 * this is the transcript of the handshake so far.
6237 * (see Sect. 4 in RFC 7627). */
Przemek Stekielae4ed302022-04-05 17:15:55 +02006238 unsigned char const *seed = handshake->randbytes;
6239 size_t seed_len = 64;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006240
6241#if !defined(MBEDTLS_DEBUG_C) && \
6242 !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \
6243 !(defined(MBEDTLS_USE_PSA_CRYPTO) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01006244 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED))
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006245 ssl = NULL; /* make sure we don't use it except for those cases */
6246 (void) ssl;
6247#endif
6248
Gilles Peskine449bd832023-01-11 14:50:10 +01006249 if (handshake->resume != 0) {
6250 MBEDTLS_SSL_DEBUG_MSG(3, ("no premaster (session resumed)"));
6251 return 0;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006252 }
6253
6254#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Gilles Peskine449bd832023-01-11 14:50:10 +01006255 if (handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED) {
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006256 lbl = "extended master secret";
Przemek Stekielae4ed302022-04-05 17:15:55 +02006257 seed = session_hash;
Gilles Peskine449bd832023-01-11 14:50:10 +01006258 handshake->calc_verify(ssl, session_hash, &seed_len);
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006259
Gilles Peskine449bd832023-01-11 14:50:10 +01006260 MBEDTLS_SSL_DEBUG_BUF(3, "session hash for extended master secret",
6261 session_hash, seed_len);
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006262 }
Przemek Stekiel169bf0b2022-04-29 07:53:29 +02006263#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006264
Przemek Stekiel99114f32022-04-22 11:20:09 +02006265#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Przemek Stekiel8a4b7fd2022-04-28 09:22:22 +02006266 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006267 if (mbedtls_ssl_ciphersuite_uses_psk(handshake->ciphersuite_info) == 1) {
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006268 /* Perform PSK-to-MS expansion in a single step. */
6269 psa_status_t status;
6270 psa_algorithm_t alg;
6271 mbedtls_svc_key_id_t psk;
6272 psa_key_derivation_operation_t derivation =
6273 PSA_KEY_DERIVATION_OPERATION_INIT;
6274 mbedtls_md_type_t hash_alg = handshake->ciphersuite_info->mac;
6275
Gilles Peskine449bd832023-01-11 14:50:10 +01006276 MBEDTLS_SSL_DEBUG_MSG(2, ("perform PSA-based PSK-to-MS expansion"));
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006277
Gilles Peskine449bd832023-01-11 14:50:10 +01006278 psk = mbedtls_ssl_get_opaque_psk(ssl);
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006279
Gilles Peskine449bd832023-01-11 14:50:10 +01006280 if (hash_alg == MBEDTLS_MD_SHA384) {
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006281 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
Gilles Peskine449bd832023-01-11 14:50:10 +01006282 } else {
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006283 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
Gilles Peskine449bd832023-01-11 14:50:10 +01006284 }
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006285
Przemek Stekiel51a1f362022-04-13 08:57:06 +02006286 size_t other_secret_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006287 unsigned char *other_secret = NULL;
Przemek Stekielc2033402022-04-05 17:19:41 +02006288
Gilles Peskine449bd832023-01-11 14:50:10 +01006289 switch (handshake->ciphersuite_info->key_exchange) {
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006290 /* Provide other secret.
Przemek Stekiel51a1f362022-04-13 08:57:06 +02006291 * Other secret is stored in premaster, where first 2 bytes hold the
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006292 * length of the other key.
Przemek Stekielc2033402022-04-05 17:19:41 +02006293 */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006294 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Przemek Stekiel8abcee92022-04-28 09:16:28 +02006295 /* For RSA-PSK other key length is always 48 bytes. */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006296 other_secret_len = 48;
6297 other_secret = handshake->premaster + 2;
6298 break;
6299 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006300 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006301 other_secret_len = MBEDTLS_GET_UINT16_BE(handshake->premaster, 0);
6302 other_secret = handshake->premaster + 2;
6303 break;
6304 default:
6305 break;
Przemek Stekielc2033402022-04-05 17:19:41 +02006306 }
6307
Gilles Peskine449bd832023-01-11 14:50:10 +01006308 status = setup_psa_key_derivation(&derivation, psk, alg,
6309 ssl->conf->psk, ssl->conf->psk_len,
6310 seed, seed_len,
6311 (unsigned char const *) lbl,
6312 (size_t) strlen(lbl),
6313 other_secret, other_secret_len,
6314 master_secret_len);
6315 if (status != PSA_SUCCESS) {
6316 psa_key_derivation_abort(&derivation);
6317 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006318 }
6319
Gilles Peskine449bd832023-01-11 14:50:10 +01006320 status = psa_key_derivation_output_bytes(&derivation,
6321 master,
6322 master_secret_len);
6323 if (status != PSA_SUCCESS) {
6324 psa_key_derivation_abort(&derivation);
6325 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006326 }
6327
Gilles Peskine449bd832023-01-11 14:50:10 +01006328 status = psa_key_derivation_abort(&derivation);
6329 if (status != PSA_SUCCESS) {
6330 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
6331 }
6332 } else
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006333#endif
6334 {
Neil Armstrongca7d5062022-05-31 14:43:23 +02006335#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01006336 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
6337 if (handshake->ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE) {
Neil Armstrongca7d5062022-05-31 14:43:23 +02006338 psa_status_t status;
6339 psa_algorithm_t alg = PSA_ALG_TLS12_ECJPAKE_TO_PMS;
6340 psa_key_derivation_operation_t derivation =
6341 PSA_KEY_DERIVATION_OPERATION_INIT;
6342
Gilles Peskine449bd832023-01-11 14:50:10 +01006343 MBEDTLS_SSL_DEBUG_MSG(2, ("perform PSA-based PMS KDF for ECJPAKE"));
Neil Armstrongca7d5062022-05-31 14:43:23 +02006344
6345 handshake->pmslen = PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE;
6346
Gilles Peskine449bd832023-01-11 14:50:10 +01006347 status = psa_key_derivation_setup(&derivation, alg);
6348 if (status != PSA_SUCCESS) {
6349 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Neil Armstrongca7d5062022-05-31 14:43:23 +02006350 }
6351
Gilles Peskine449bd832023-01-11 14:50:10 +01006352 status = psa_key_derivation_set_capacity(&derivation,
6353 PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE);
6354 if (status != PSA_SUCCESS) {
6355 psa_key_derivation_abort(&derivation);
6356 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Neil Armstrongca7d5062022-05-31 14:43:23 +02006357 }
6358
Gilles Peskine449bd832023-01-11 14:50:10 +01006359 status = psa_pake_get_implicit_key(&handshake->psa_pake_ctx,
6360 &derivation);
6361 if (status != PSA_SUCCESS) {
6362 psa_key_derivation_abort(&derivation);
6363 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Neil Armstrongca7d5062022-05-31 14:43:23 +02006364 }
6365
Gilles Peskine449bd832023-01-11 14:50:10 +01006366 status = psa_key_derivation_output_bytes(&derivation,
6367 handshake->premaster,
6368 handshake->pmslen);
6369 if (status != PSA_SUCCESS) {
6370 psa_key_derivation_abort(&derivation);
6371 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
6372 }
6373
6374 status = psa_key_derivation_abort(&derivation);
6375 if (status != PSA_SUCCESS) {
6376 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Neil Armstrongca7d5062022-05-31 14:43:23 +02006377 }
6378 }
6379#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01006380 ret = handshake->tls_prf(handshake->premaster, handshake->pmslen,
6381 lbl, seed, seed_len,
6382 master,
6383 master_secret_len);
6384 if (ret != 0) {
6385 MBEDTLS_SSL_DEBUG_RET(1, "prf", ret);
6386 return ret;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006387 }
6388
Gilles Peskine449bd832023-01-11 14:50:10 +01006389 MBEDTLS_SSL_DEBUG_BUF(3, "premaster secret",
6390 handshake->premaster,
6391 handshake->pmslen);
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006392
Gilles Peskine449bd832023-01-11 14:50:10 +01006393 mbedtls_platform_zeroize(handshake->premaster,
6394 sizeof(handshake->premaster));
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006395 }
6396
Gilles Peskine449bd832023-01-11 14:50:10 +01006397 return 0;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006398}
6399
Gilles Peskine449bd832023-01-11 14:50:10 +01006400int mbedtls_ssl_derive_keys(mbedtls_ssl_context *ssl)
Jerry Yud62f87e2022-02-17 14:09:02 +08006401{
6402 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6403 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
6404 ssl->handshake->ciphersuite_info;
6405
Gilles Peskine449bd832023-01-11 14:50:10 +01006406 MBEDTLS_SSL_DEBUG_MSG(2, ("=> derive keys"));
Jerry Yud62f87e2022-02-17 14:09:02 +08006407
6408 /* Set PRF, calc_verify and calc_finished function pointers */
Gilles Peskine449bd832023-01-11 14:50:10 +01006409 ret = ssl_set_handshake_prfs(ssl->handshake,
6410 ciphersuite_info->mac);
6411 if (ret != 0) {
6412 MBEDTLS_SSL_DEBUG_RET(1, "ssl_set_handshake_prfs", ret);
6413 return ret;
Jerry Yud62f87e2022-02-17 14:09:02 +08006414 }
6415
6416 /* Compute master secret if needed */
Gilles Peskine449bd832023-01-11 14:50:10 +01006417 ret = ssl_compute_master(ssl->handshake,
6418 ssl->session_negotiate->master,
6419 ssl);
6420 if (ret != 0) {
6421 MBEDTLS_SSL_DEBUG_RET(1, "ssl_compute_master", ret);
6422 return ret;
Jerry Yud62f87e2022-02-17 14:09:02 +08006423 }
6424
6425 /* Swap the client and server random values:
6426 * - MS derivation wanted client+server (RFC 5246 8.1)
6427 * - key derivation wants server+client (RFC 5246 6.3) */
6428 {
6429 unsigned char tmp[64];
Gilles Peskine449bd832023-01-11 14:50:10 +01006430 memcpy(tmp, ssl->handshake->randbytes, 64);
6431 memcpy(ssl->handshake->randbytes, tmp + 32, 32);
6432 memcpy(ssl->handshake->randbytes + 32, tmp, 32);
6433 mbedtls_platform_zeroize(tmp, sizeof(tmp));
Jerry Yud62f87e2022-02-17 14:09:02 +08006434 }
6435
6436 /* Populate transform structure */
Gilles Peskine449bd832023-01-11 14:50:10 +01006437 ret = ssl_tls12_populate_transform(ssl->transform_negotiate,
6438 ssl->session_negotiate->ciphersuite,
6439 ssl->session_negotiate->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02006440#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01006441 ssl->session_negotiate->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02006442#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01006443 ssl->handshake->tls_prf,
6444 ssl->handshake->randbytes,
6445 ssl->tls_version,
6446 ssl->conf->endpoint,
6447 ssl);
6448 if (ret != 0) {
6449 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls12_populate_transform", ret);
6450 return ret;
Jerry Yud62f87e2022-02-17 14:09:02 +08006451 }
6452
6453 /* We no longer need Server/ClientHello.random values */
Gilles Peskine449bd832023-01-11 14:50:10 +01006454 mbedtls_platform_zeroize(ssl->handshake->randbytes,
6455 sizeof(ssl->handshake->randbytes));
Jerry Yud62f87e2022-02-17 14:09:02 +08006456
Gilles Peskine449bd832023-01-11 14:50:10 +01006457 MBEDTLS_SSL_DEBUG_MSG(2, ("<= derive keys"));
Jerry Yud62f87e2022-02-17 14:09:02 +08006458
Gilles Peskine449bd832023-01-11 14:50:10 +01006459 return 0;
Jerry Yud62f87e2022-02-17 14:09:02 +08006460}
Jerry Yu8392e0d2022-02-17 14:10:24 +08006461
Gilles Peskine449bd832023-01-11 14:50:10 +01006462int mbedtls_ssl_set_calc_verify_md(mbedtls_ssl_context *ssl, int md)
Ronald Cron4dcbca92022-03-07 10:21:40 +01006463{
Gilles Peskine449bd832023-01-11 14:50:10 +01006464 switch (md) {
Andrzej Kurek25f27152022-08-17 16:09:31 -04006465#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron4dcbca92022-03-07 10:21:40 +01006466 case MBEDTLS_SSL_HASH_SHA384:
6467 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
6468 break;
6469#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04006470#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron4dcbca92022-03-07 10:21:40 +01006471 case MBEDTLS_SSL_HASH_SHA256:
6472 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
6473 break;
6474#endif
6475 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006476 return -1;
Ronald Cron4dcbca92022-03-07 10:21:40 +01006477 }
Andrzej Kurekeabeb302022-10-17 07:52:51 -04006478#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
6479 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
6480 (void) ssl;
6481#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01006482 return 0;
Ronald Cron4dcbca92022-03-07 10:21:40 +01006483}
6484
Andrzej Kurek25f27152022-08-17 16:09:31 -04006485#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01006486void ssl_calc_verify_tls_sha256(const mbedtls_ssl_context *ssl,
6487 unsigned char *hash,
6488 size_t *hlen)
Jerry Yu8392e0d2022-02-17 14:10:24 +08006489{
6490#if defined(MBEDTLS_USE_PSA_CRYPTO)
6491 size_t hash_size;
6492 psa_status_t status;
6493 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
6494
Gilles Peskine449bd832023-01-11 14:50:10 +01006495 MBEDTLS_SSL_DEBUG_MSG(2, ("=> PSA calc verify sha256"));
6496 status = psa_hash_clone(&ssl->handshake->fin_sha256_psa, &sha256_psa);
6497 if (status != PSA_SUCCESS) {
6498 MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash clone failed"));
Jerry Yu8392e0d2022-02-17 14:10:24 +08006499 return;
6500 }
6501
Gilles Peskine449bd832023-01-11 14:50:10 +01006502 status = psa_hash_finish(&sha256_psa, hash, 32, &hash_size);
6503 if (status != PSA_SUCCESS) {
6504 MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash finish failed"));
Jerry Yu8392e0d2022-02-17 14:10:24 +08006505 return;
6506 }
6507
6508 *hlen = 32;
Gilles Peskine449bd832023-01-11 14:50:10 +01006509 MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated verify result", hash, *hlen);
6510 MBEDTLS_SSL_DEBUG_MSG(2, ("<= PSA calc verify"));
Jerry Yu8392e0d2022-02-17 14:10:24 +08006511#else
6512 mbedtls_sha256_context sha256;
6513
Gilles Peskine449bd832023-01-11 14:50:10 +01006514 mbedtls_sha256_init(&sha256);
Jerry Yu8392e0d2022-02-17 14:10:24 +08006515
Gilles Peskine449bd832023-01-11 14:50:10 +01006516 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc verify sha256"));
Jerry Yu8392e0d2022-02-17 14:10:24 +08006517
Gilles Peskine449bd832023-01-11 14:50:10 +01006518 mbedtls_sha256_clone(&sha256, &ssl->handshake->fin_sha256);
6519 mbedtls_sha256_finish(&sha256, hash);
Jerry Yu8392e0d2022-02-17 14:10:24 +08006520
6521 *hlen = 32;
6522
Gilles Peskine449bd832023-01-11 14:50:10 +01006523 MBEDTLS_SSL_DEBUG_BUF(3, "calculated verify result", hash, *hlen);
6524 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc verify"));
Jerry Yu8392e0d2022-02-17 14:10:24 +08006525
Gilles Peskine449bd832023-01-11 14:50:10 +01006526 mbedtls_sha256_free(&sha256);
Jerry Yu8392e0d2022-02-17 14:10:24 +08006527#endif /* MBEDTLS_USE_PSA_CRYPTO */
6528 return;
6529}
Andrzej Kurek25f27152022-08-17 16:09:31 -04006530#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu8392e0d2022-02-17 14:10:24 +08006531
Andrzej Kurek25f27152022-08-17 16:09:31 -04006532#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01006533void ssl_calc_verify_tls_sha384(const mbedtls_ssl_context *ssl,
6534 unsigned char *hash,
6535 size_t *hlen)
Jerry Yuc1cb3842022-02-17 14:13:48 +08006536{
6537#if defined(MBEDTLS_USE_PSA_CRYPTO)
6538 size_t hash_size;
6539 psa_status_t status;
6540 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
6541
Gilles Peskine449bd832023-01-11 14:50:10 +01006542 MBEDTLS_SSL_DEBUG_MSG(2, ("=> PSA calc verify sha384"));
6543 status = psa_hash_clone(&ssl->handshake->fin_sha384_psa, &sha384_psa);
6544 if (status != PSA_SUCCESS) {
6545 MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash clone failed"));
Jerry Yuc1cb3842022-02-17 14:13:48 +08006546 return;
6547 }
6548
Gilles Peskine449bd832023-01-11 14:50:10 +01006549 status = psa_hash_finish(&sha384_psa, hash, 48, &hash_size);
6550 if (status != PSA_SUCCESS) {
6551 MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash finish failed"));
Jerry Yuc1cb3842022-02-17 14:13:48 +08006552 return;
6553 }
6554
6555 *hlen = 48;
Gilles Peskine449bd832023-01-11 14:50:10 +01006556 MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated verify result", hash, *hlen);
6557 MBEDTLS_SSL_DEBUG_MSG(2, ("<= PSA calc verify"));
Jerry Yuc1cb3842022-02-17 14:13:48 +08006558#else
6559 mbedtls_sha512_context sha512;
6560
Gilles Peskine449bd832023-01-11 14:50:10 +01006561 mbedtls_sha512_init(&sha512);
Jerry Yuc1cb3842022-02-17 14:13:48 +08006562
Gilles Peskine449bd832023-01-11 14:50:10 +01006563 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc verify sha384"));
Jerry Yuc1cb3842022-02-17 14:13:48 +08006564
Gilles Peskine449bd832023-01-11 14:50:10 +01006565 mbedtls_sha512_clone(&sha512, &ssl->handshake->fin_sha384);
6566 mbedtls_sha512_finish(&sha512, hash);
Jerry Yuc1cb3842022-02-17 14:13:48 +08006567
6568 *hlen = 48;
6569
Gilles Peskine449bd832023-01-11 14:50:10 +01006570 MBEDTLS_SSL_DEBUG_BUF(3, "calculated verify result", hash, *hlen);
6571 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc verify"));
Jerry Yuc1cb3842022-02-17 14:13:48 +08006572
Gilles Peskine449bd832023-01-11 14:50:10 +01006573 mbedtls_sha512_free(&sha512);
Jerry Yuc1cb3842022-02-17 14:13:48 +08006574#endif /* MBEDTLS_USE_PSA_CRYPTO */
6575 return;
6576}
Andrzej Kurek25f27152022-08-17 16:09:31 -04006577#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yuc1cb3842022-02-17 14:13:48 +08006578
Neil Armstrong80f6f322022-05-03 17:56:38 +02006579#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
6580 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006581int mbedtls_ssl_psk_derive_premaster(mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex)
Jerry Yuce3dca42022-02-17 14:16:37 +08006582{
6583 unsigned char *p = ssl->handshake->premaster;
Gilles Peskine449bd832023-01-11 14:50:10 +01006584 unsigned char *end = p + sizeof(ssl->handshake->premaster);
Jerry Yuce3dca42022-02-17 14:16:37 +08006585 const unsigned char *psk = NULL;
6586 size_t psk_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006587 int psk_ret = mbedtls_ssl_get_psk(ssl, &psk, &psk_len);
Jerry Yuce3dca42022-02-17 14:16:37 +08006588
Gilles Peskine449bd832023-01-11 14:50:10 +01006589 if (psk_ret == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED) {
Jerry Yuce3dca42022-02-17 14:16:37 +08006590 /*
6591 * This should never happen because the existence of a PSK is always
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006592 * checked before calling this function.
6593 *
6594 * The exception is opaque DHE-PSK. For DHE-PSK fill premaster with
Przemek Stekiel8abcee92022-04-28 09:16:28 +02006595 * the shared secret without PSK.
Jerry Yuce3dca42022-02-17 14:16:37 +08006596 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006597 if (key_ex != MBEDTLS_KEY_EXCHANGE_DHE_PSK) {
6598 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
6599 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006600 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006601 }
6602
6603 /*
6604 * PMS = struct {
6605 * opaque other_secret<0..2^16-1>;
6606 * opaque psk<0..2^16-1>;
6607 * };
6608 * with "other_secret" depending on the particular key exchange
6609 */
6610#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006611 if (key_ex == MBEDTLS_KEY_EXCHANGE_PSK) {
6612 if (end - p < 2) {
6613 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6614 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006615
Gilles Peskine449bd832023-01-11 14:50:10 +01006616 MBEDTLS_PUT_UINT16_BE(psk_len, p, 0);
Jerry Yuce3dca42022-02-17 14:16:37 +08006617 p += 2;
6618
Gilles Peskine449bd832023-01-11 14:50:10 +01006619 if (end < p || (size_t) (end - p) < psk_len) {
6620 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6621 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006622
Gilles Peskine449bd832023-01-11 14:50:10 +01006623 memset(p, 0, psk_len);
Jerry Yuce3dca42022-02-17 14:16:37 +08006624 p += psk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01006625 } else
Jerry Yuce3dca42022-02-17 14:16:37 +08006626#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
6627#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006628 if (key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK) {
Jerry Yuce3dca42022-02-17 14:16:37 +08006629 /*
6630 * other_secret already set by the ClientKeyExchange message,
6631 * and is 48 bytes long
6632 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006633 if (end - p < 2) {
6634 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6635 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006636
6637 *p++ = 0;
6638 *p++ = 48;
6639 p += 48;
Gilles Peskine449bd832023-01-11 14:50:10 +01006640 } else
Jerry Yuce3dca42022-02-17 14:16:37 +08006641#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
6642#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006643 if (key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK) {
Jerry Yuce3dca42022-02-17 14:16:37 +08006644 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6645 size_t len;
6646
6647 /* Write length only when we know the actual value */
Gilles Peskine449bd832023-01-11 14:50:10 +01006648 if ((ret = mbedtls_dhm_calc_secret(&ssl->handshake->dhm_ctx,
6649 p + 2, end - (p + 2), &len,
6650 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
6651 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_calc_secret", ret);
6652 return ret;
Jerry Yuce3dca42022-02-17 14:16:37 +08006653 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006654 MBEDTLS_PUT_UINT16_BE(len, p, 0);
Jerry Yuce3dca42022-02-17 14:16:37 +08006655 p += 2 + len;
6656
Gilles Peskine449bd832023-01-11 14:50:10 +01006657 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: K ", &ssl->handshake->dhm_ctx.K);
6658 } else
Jerry Yuce3dca42022-02-17 14:16:37 +08006659#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Neil Armstrong80f6f322022-05-03 17:56:38 +02006660#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006661 if (key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK) {
Jerry Yuce3dca42022-02-17 14:16:37 +08006662 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6663 size_t zlen;
6664
Gilles Peskine449bd832023-01-11 14:50:10 +01006665 if ((ret = mbedtls_ecdh_calc_secret(&ssl->handshake->ecdh_ctx, &zlen,
6666 p + 2, end - (p + 2),
6667 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
6668 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecdh_calc_secret", ret);
6669 return ret;
Jerry Yuce3dca42022-02-17 14:16:37 +08006670 }
6671
Gilles Peskine449bd832023-01-11 14:50:10 +01006672 MBEDTLS_PUT_UINT16_BE(zlen, p, 0);
Jerry Yuce3dca42022-02-17 14:16:37 +08006673 p += 2 + zlen;
6674
Gilles Peskine449bd832023-01-11 14:50:10 +01006675 MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx,
6676 MBEDTLS_DEBUG_ECDH_Z);
6677 } else
Neil Armstrong80f6f322022-05-03 17:56:38 +02006678#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Jerry Yuce3dca42022-02-17 14:16:37 +08006679 {
Gilles Peskine449bd832023-01-11 14:50:10 +01006680 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
6681 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yuce3dca42022-02-17 14:16:37 +08006682 }
6683
6684 /* opaque psk<0..2^16-1>; */
Gilles Peskine449bd832023-01-11 14:50:10 +01006685 if (end - p < 2) {
6686 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6687 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006688
Gilles Peskine449bd832023-01-11 14:50:10 +01006689 MBEDTLS_PUT_UINT16_BE(psk_len, p, 0);
Jerry Yuce3dca42022-02-17 14:16:37 +08006690 p += 2;
6691
Gilles Peskine449bd832023-01-11 14:50:10 +01006692 if (end < p || (size_t) (end - p) < psk_len) {
6693 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6694 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006695
Gilles Peskine449bd832023-01-11 14:50:10 +01006696 memcpy(p, psk, psk_len);
Jerry Yuce3dca42022-02-17 14:16:37 +08006697 p += psk_len;
6698
6699 ssl->handshake->pmslen = p - ssl->handshake->premaster;
6700
Gilles Peskine449bd832023-01-11 14:50:10 +01006701 return 0;
Jerry Yuce3dca42022-02-17 14:16:37 +08006702}
Neil Armstrong80f6f322022-05-03 17:56:38 +02006703#endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Jerry Yuc2c673d2022-02-17 14:20:39 +08006704
6705#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006706MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006707static int ssl_write_hello_request(mbedtls_ssl_context *ssl);
Jerry Yuc2c673d2022-02-17 14:20:39 +08006708
6709#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006710int mbedtls_ssl_resend_hello_request(mbedtls_ssl_context *ssl)
Jerry Yuc2c673d2022-02-17 14:20:39 +08006711{
6712 /* If renegotiation is not enforced, retransmit until we would reach max
6713 * timeout if we were using the usual handshake doubling scheme */
Gilles Peskine449bd832023-01-11 14:50:10 +01006714 if (ssl->conf->renego_max_records < 0) {
Jerry Yuc2c673d2022-02-17 14:20:39 +08006715 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
6716 unsigned char doublings = 1;
6717
Gilles Peskine449bd832023-01-11 14:50:10 +01006718 while (ratio != 0) {
Jerry Yuc2c673d2022-02-17 14:20:39 +08006719 ++doublings;
6720 ratio >>= 1;
6721 }
6722
Gilles Peskine449bd832023-01-11 14:50:10 +01006723 if (++ssl->renego_records_seen > doublings) {
6724 MBEDTLS_SSL_DEBUG_MSG(2, ("no longer retransmitting hello request"));
6725 return 0;
Jerry Yuc2c673d2022-02-17 14:20:39 +08006726 }
6727 }
6728
Gilles Peskine449bd832023-01-11 14:50:10 +01006729 return ssl_write_hello_request(ssl);
Jerry Yuc2c673d2022-02-17 14:20:39 +08006730}
6731#endif
6732#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Jerry Yud9526692022-02-17 14:23:47 +08006733
Jerry Yud9526692022-02-17 14:23:47 +08006734/*
6735 * Handshake functions
6736 */
6737#if !defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
6738/* No certificate support -> dummy functions */
Gilles Peskine449bd832023-01-11 14:50:10 +01006739int mbedtls_ssl_write_certificate(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08006740{
6741 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6742 ssl->handshake->ciphersuite_info;
6743
Gilles Peskine449bd832023-01-11 14:50:10 +01006744 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006745
Gilles Peskine449bd832023-01-11 14:50:10 +01006746 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
6747 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006748 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01006749 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08006750 }
6751
Gilles Peskine449bd832023-01-11 14:50:10 +01006752 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
6753 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08006754}
6755
Gilles Peskine449bd832023-01-11 14:50:10 +01006756int mbedtls_ssl_parse_certificate(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08006757{
6758 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6759 ssl->handshake->ciphersuite_info;
6760
Gilles Peskine449bd832023-01-11 14:50:10 +01006761 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006762
Gilles Peskine449bd832023-01-11 14:50:10 +01006763 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
6764 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006765 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01006766 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08006767 }
6768
Gilles Peskine449bd832023-01-11 14:50:10 +01006769 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
6770 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08006771}
6772
6773#else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
6774/* Some certificate support -> implement write and parse */
6775
Gilles Peskine449bd832023-01-11 14:50:10 +01006776int mbedtls_ssl_write_certificate(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08006777{
6778 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
6779 size_t i, n;
6780 const mbedtls_x509_crt *crt;
6781 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6782 ssl->handshake->ciphersuite_info;
6783
Gilles Peskine449bd832023-01-11 14:50:10 +01006784 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006785
Gilles Peskine449bd832023-01-11 14:50:10 +01006786 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
6787 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006788 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01006789 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08006790 }
6791
6792#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01006793 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
6794 if (ssl->handshake->client_auth == 0) {
6795 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006796 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01006797 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08006798 }
6799 }
6800#endif /* MBEDTLS_SSL_CLI_C */
6801#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01006802 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
6803 if (mbedtls_ssl_own_cert(ssl) == NULL) {
Jerry Yud9526692022-02-17 14:23:47 +08006804 /* Should never happen because we shouldn't have picked the
6805 * ciphersuite if we don't have a certificate. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006806 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08006807 }
6808 }
6809#endif
6810
Gilles Peskine449bd832023-01-11 14:50:10 +01006811 MBEDTLS_SSL_DEBUG_CRT(3, "own certificate", mbedtls_ssl_own_cert(ssl));
Jerry Yud9526692022-02-17 14:23:47 +08006812
6813 /*
6814 * 0 . 0 handshake type
6815 * 1 . 3 handshake length
6816 * 4 . 6 length of all certs
6817 * 7 . 9 length of cert. 1
6818 * 10 . n-1 peer certificate
6819 * n . n+2 length of cert. 2
6820 * n+3 . ... upper level cert, etc.
6821 */
6822 i = 7;
Gilles Peskine449bd832023-01-11 14:50:10 +01006823 crt = mbedtls_ssl_own_cert(ssl);
Jerry Yud9526692022-02-17 14:23:47 +08006824
Gilles Peskine449bd832023-01-11 14:50:10 +01006825 while (crt != NULL) {
Jerry Yud9526692022-02-17 14:23:47 +08006826 n = crt->raw.len;
Gilles Peskine449bd832023-01-11 14:50:10 +01006827 if (n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i) {
6828 MBEDTLS_SSL_DEBUG_MSG(1, ("certificate too large, %" MBEDTLS_PRINTF_SIZET
6829 " > %" MBEDTLS_PRINTF_SIZET,
6830 i + 3 + n, (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN));
6831 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
Jerry Yud9526692022-02-17 14:23:47 +08006832 }
6833
Gilles Peskine449bd832023-01-11 14:50:10 +01006834 ssl->out_msg[i] = MBEDTLS_BYTE_2(n);
6835 ssl->out_msg[i + 1] = MBEDTLS_BYTE_1(n);
6836 ssl->out_msg[i + 2] = MBEDTLS_BYTE_0(n);
Jerry Yud9526692022-02-17 14:23:47 +08006837
Gilles Peskine449bd832023-01-11 14:50:10 +01006838 i += 3; memcpy(ssl->out_msg + i, crt->raw.p, n);
Jerry Yud9526692022-02-17 14:23:47 +08006839 i += n; crt = crt->next;
6840 }
6841
Gilles Peskine449bd832023-01-11 14:50:10 +01006842 ssl->out_msg[4] = MBEDTLS_BYTE_2(i - 7);
6843 ssl->out_msg[5] = MBEDTLS_BYTE_1(i - 7);
6844 ssl->out_msg[6] = MBEDTLS_BYTE_0(i - 7);
Jerry Yud9526692022-02-17 14:23:47 +08006845
6846 ssl->out_msglen = i;
6847 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6848 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
6849
6850 ssl->state++;
6851
Gilles Peskine449bd832023-01-11 14:50:10 +01006852 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
6853 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
6854 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08006855 }
6856
Gilles Peskine449bd832023-01-11 14:50:10 +01006857 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006858
Gilles Peskine449bd832023-01-11 14:50:10 +01006859 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08006860}
6861
6862#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6863
6864#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006865MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006866static int ssl_check_peer_crt_unchanged(mbedtls_ssl_context *ssl,
6867 unsigned char *crt_buf,
6868 size_t crt_buf_len)
Jerry Yud9526692022-02-17 14:23:47 +08006869{
6870 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6871
Gilles Peskine449bd832023-01-11 14:50:10 +01006872 if (peer_crt == NULL) {
6873 return -1;
6874 }
Jerry Yud9526692022-02-17 14:23:47 +08006875
Gilles Peskine449bd832023-01-11 14:50:10 +01006876 if (peer_crt->raw.len != crt_buf_len) {
6877 return -1;
6878 }
Jerry Yud9526692022-02-17 14:23:47 +08006879
Gilles Peskine449bd832023-01-11 14:50:10 +01006880 return memcmp(peer_crt->raw.p, crt_buf, peer_crt->raw.len);
Jerry Yud9526692022-02-17 14:23:47 +08006881}
6882#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006883MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006884static int ssl_check_peer_crt_unchanged(mbedtls_ssl_context *ssl,
6885 unsigned char *crt_buf,
6886 size_t crt_buf_len)
Jerry Yud9526692022-02-17 14:23:47 +08006887{
6888 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6889 unsigned char const * const peer_cert_digest =
6890 ssl->session->peer_cert_digest;
6891 mbedtls_md_type_t const peer_cert_digest_type =
6892 ssl->session->peer_cert_digest_type;
6893 mbedtls_md_info_t const * const digest_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01006894 mbedtls_md_info_from_type(peer_cert_digest_type);
Jerry Yud9526692022-02-17 14:23:47 +08006895 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
6896 size_t digest_len;
6897
Gilles Peskine449bd832023-01-11 14:50:10 +01006898 if (peer_cert_digest == NULL || digest_info == NULL) {
6899 return -1;
6900 }
Jerry Yud9526692022-02-17 14:23:47 +08006901
Gilles Peskine449bd832023-01-11 14:50:10 +01006902 digest_len = mbedtls_md_get_size(digest_info);
6903 if (digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN) {
6904 return -1;
6905 }
Jerry Yud9526692022-02-17 14:23:47 +08006906
Gilles Peskine449bd832023-01-11 14:50:10 +01006907 ret = mbedtls_md(digest_info, crt_buf, crt_buf_len, tmp_digest);
6908 if (ret != 0) {
6909 return -1;
6910 }
Jerry Yud9526692022-02-17 14:23:47 +08006911
Gilles Peskine449bd832023-01-11 14:50:10 +01006912 return memcmp(tmp_digest, peer_cert_digest, digest_len);
Jerry Yud9526692022-02-17 14:23:47 +08006913}
6914#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
6915#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
6916
6917/*
6918 * Once the certificate message is read, parse it into a cert chain and
6919 * perform basic checks, but leave actual verification to the caller
6920 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006921MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006922static int ssl_parse_certificate_chain(mbedtls_ssl_context *ssl,
6923 mbedtls_x509_crt *chain)
Jerry Yud9526692022-02-17 14:23:47 +08006924{
6925 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6926#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01006927 int crt_cnt = 0;
Jerry Yud9526692022-02-17 14:23:47 +08006928#endif
6929 size_t i, n;
6930 uint8_t alert;
6931
Gilles Peskine449bd832023-01-11 14:50:10 +01006932 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
6933 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
6934 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6935 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
6936 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Jerry Yud9526692022-02-17 14:23:47 +08006937 }
6938
Gilles Peskine449bd832023-01-11 14:50:10 +01006939 if (ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE) {
6940 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6941 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
6942 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Jerry Yud9526692022-02-17 14:23:47 +08006943 }
6944
Gilles Peskine449bd832023-01-11 14:50:10 +01006945 if (ssl->in_hslen < mbedtls_ssl_hs_hdr_len(ssl) + 3 + 3) {
6946 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
6947 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6948 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
6949 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08006950 }
6951
Gilles Peskine449bd832023-01-11 14:50:10 +01006952 i = mbedtls_ssl_hs_hdr_len(ssl);
Jerry Yud9526692022-02-17 14:23:47 +08006953
6954 /*
6955 * Same message structure as in mbedtls_ssl_write_certificate()
6956 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006957 n = (ssl->in_msg[i+1] << 8) | ssl->in_msg[i+2];
Jerry Yud9526692022-02-17 14:23:47 +08006958
Gilles Peskine449bd832023-01-11 14:50:10 +01006959 if (ssl->in_msg[i] != 0 ||
6960 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len(ssl)) {
6961 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
6962 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6963 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
6964 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08006965 }
6966
6967 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
6968 i += 3;
6969
6970 /* Iterate through and parse the CRTs in the provided chain. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006971 while (i < ssl->in_hslen) {
Jerry Yud9526692022-02-17 14:23:47 +08006972 /* Check that there's room for the next CRT's length fields. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006973 if (i + 3 > ssl->in_hslen) {
6974 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
6975 mbedtls_ssl_send_alert_message(ssl,
6976 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 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
6981 * anything beyond 2**16 ~ 64K. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006982 if (ssl->in_msg[i] != 0) {
6983 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
6984 mbedtls_ssl_send_alert_message(ssl,
6985 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6986 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT);
6987 return MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Jerry Yud9526692022-02-17 14:23:47 +08006988 }
6989
6990 /* Read length of the next CRT in the chain. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006991 n = ((unsigned int) ssl->in_msg[i + 1] << 8)
Jerry Yud9526692022-02-17 14:23:47 +08006992 | (unsigned int) ssl->in_msg[i + 2];
6993 i += 3;
6994
Gilles Peskine449bd832023-01-11 14:50:10 +01006995 if (n < 128 || i + n > ssl->in_hslen) {
6996 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
6997 mbedtls_ssl_send_alert_message(ssl,
6998 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
6999 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
7000 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007001 }
7002
7003 /* Check if we're handling the first CRT in the chain. */
7004#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007005 if (crt_cnt++ == 0 &&
Jerry Yud9526692022-02-17 14:23:47 +08007006 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Gilles Peskine449bd832023-01-11 14:50:10 +01007007 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
Jerry Yud9526692022-02-17 14:23:47 +08007008 /* During client-side renegotiation, check that the server's
7009 * end-CRTs hasn't changed compared to the initial handshake,
7010 * mitigating the triple handshake attack. On success, reuse
7011 * the original end-CRT instead of parsing it again. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007012 MBEDTLS_SSL_DEBUG_MSG(3, ("Check that peer CRT hasn't changed during renegotiation"));
7013 if (ssl_check_peer_crt_unchanged(ssl,
7014 &ssl->in_msg[i],
7015 n) != 0) {
7016 MBEDTLS_SSL_DEBUG_MSG(1, ("new server cert during renegotiation"));
7017 mbedtls_ssl_send_alert_message(ssl,
7018 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7019 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED);
7020 return MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Jerry Yud9526692022-02-17 14:23:47 +08007021 }
7022
7023 /* Now we can safely free the original chain. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007024 ssl_clear_peer_cert(ssl->session);
Jerry Yud9526692022-02-17 14:23:47 +08007025 }
7026#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
7027
7028 /* Parse the next certificate in the chain. */
7029#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007030 ret = mbedtls_x509_crt_parse_der(chain, ssl->in_msg + i, n);
Jerry Yud9526692022-02-17 14:23:47 +08007031#else
7032 /* If we don't need to store the CRT chain permanently, parse
7033 * it in-place from the input buffer instead of making a copy. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007034 ret = mbedtls_x509_crt_parse_der_nocopy(chain, ssl->in_msg + i, n);
Jerry Yud9526692022-02-17 14:23:47 +08007035#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007036 switch (ret) {
Jerry Yud9526692022-02-17 14:23:47 +08007037 case 0: /*ok*/
7038 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
7039 /* Ignore certificate with an unknown algorithm: maybe a
7040 prior certificate was already trusted. */
7041 break;
7042
7043 case MBEDTLS_ERR_X509_ALLOC_FAILED:
7044 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
7045 goto crt_parse_der_failed;
7046
7047 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
7048 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7049 goto crt_parse_der_failed;
7050
7051 default:
7052 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007053crt_parse_der_failed:
7054 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert);
7055 MBEDTLS_SSL_DEBUG_RET(1, " mbedtls_x509_crt_parse_der", ret);
7056 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007057 }
7058
7059 i += n;
7060 }
7061
Gilles Peskine449bd832023-01-11 14:50:10 +01007062 MBEDTLS_SSL_DEBUG_CRT(3, "peer certificate", chain);
7063 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08007064}
7065
7066#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007067MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007068static int ssl_srv_check_client_no_crt_notification(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08007069{
Gilles Peskine449bd832023-01-11 14:50:10 +01007070 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
7071 return -1;
7072 }
Jerry Yud9526692022-02-17 14:23:47 +08007073
Gilles Peskine449bd832023-01-11 14:50:10 +01007074 if (ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len(ssl) &&
Jerry Yud9526692022-02-17 14:23:47 +08007075 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
7076 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
Gilles Peskine449bd832023-01-11 14:50:10 +01007077 memcmp(ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl), "\0\0\0", 3) == 0) {
7078 MBEDTLS_SSL_DEBUG_MSG(1, ("peer has no certificate"));
7079 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08007080 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007081 return -1;
Jerry Yud9526692022-02-17 14:23:47 +08007082}
7083#endif /* MBEDTLS_SSL_SRV_C */
7084
7085/* Check if a certificate message is expected.
7086 * Return either
7087 * - SSL_CERTIFICATE_EXPECTED, or
7088 * - SSL_CERTIFICATE_SKIP
7089 * indicating whether a Certificate message is expected or not.
7090 */
7091#define SSL_CERTIFICATE_EXPECTED 0
7092#define SSL_CERTIFICATE_SKIP 1
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007093MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007094static int ssl_parse_certificate_coordinate(mbedtls_ssl_context *ssl,
7095 int authmode)
Jerry Yud9526692022-02-17 14:23:47 +08007096{
7097 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
7098 ssl->handshake->ciphersuite_info;
7099
Gilles Peskine449bd832023-01-11 14:50:10 +01007100 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
7101 return SSL_CERTIFICATE_SKIP;
7102 }
Jerry Yud9526692022-02-17 14:23:47 +08007103
7104#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007105 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
7106 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK) {
7107 return SSL_CERTIFICATE_SKIP;
7108 }
Jerry Yud9526692022-02-17 14:23:47 +08007109
Gilles Peskine449bd832023-01-11 14:50:10 +01007110 if (authmode == MBEDTLS_SSL_VERIFY_NONE) {
Jerry Yud9526692022-02-17 14:23:47 +08007111 ssl->session_negotiate->verify_result =
7112 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
Gilles Peskine449bd832023-01-11 14:50:10 +01007113 return SSL_CERTIFICATE_SKIP;
Jerry Yud9526692022-02-17 14:23:47 +08007114 }
7115 }
7116#else
7117 ((void) authmode);
7118#endif /* MBEDTLS_SSL_SRV_C */
7119
Gilles Peskine449bd832023-01-11 14:50:10 +01007120 return SSL_CERTIFICATE_EXPECTED;
Jerry Yud9526692022-02-17 14:23:47 +08007121}
7122
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007123MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007124static int ssl_parse_certificate_verify(mbedtls_ssl_context *ssl,
7125 int authmode,
7126 mbedtls_x509_crt *chain,
7127 void *rs_ctx)
Jerry Yud9526692022-02-17 14:23:47 +08007128{
7129 int ret = 0;
7130 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
7131 ssl->handshake->ciphersuite_info;
7132 int have_ca_chain = 0;
7133
7134 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
7135 void *p_vrfy;
7136
Gilles Peskine449bd832023-01-11 14:50:10 +01007137 if (authmode == MBEDTLS_SSL_VERIFY_NONE) {
7138 return 0;
7139 }
Jerry Yud9526692022-02-17 14:23:47 +08007140
Gilles Peskine449bd832023-01-11 14:50:10 +01007141 if (ssl->f_vrfy != NULL) {
7142 MBEDTLS_SSL_DEBUG_MSG(3, ("Use context-specific verification callback"));
Jerry Yud9526692022-02-17 14:23:47 +08007143 f_vrfy = ssl->f_vrfy;
7144 p_vrfy = ssl->p_vrfy;
Gilles Peskine449bd832023-01-11 14:50:10 +01007145 } else {
7146 MBEDTLS_SSL_DEBUG_MSG(3, ("Use configuration-specific verification callback"));
Jerry Yud9526692022-02-17 14:23:47 +08007147 f_vrfy = ssl->conf->f_vrfy;
7148 p_vrfy = ssl->conf->p_vrfy;
7149 }
7150
7151 /*
7152 * Main check: verify certificate
7153 */
7154#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Gilles Peskine449bd832023-01-11 14:50:10 +01007155 if (ssl->conf->f_ca_cb != NULL) {
Jerry Yud9526692022-02-17 14:23:47 +08007156 ((void) rs_ctx);
7157 have_ca_chain = 1;
7158
Gilles Peskine449bd832023-01-11 14:50:10 +01007159 MBEDTLS_SSL_DEBUG_MSG(3, ("use CA callback for X.509 CRT verification"));
Jerry Yud9526692022-02-17 14:23:47 +08007160 ret = mbedtls_x509_crt_verify_with_ca_cb(
7161 chain,
7162 ssl->conf->f_ca_cb,
7163 ssl->conf->p_ca_cb,
7164 ssl->conf->cert_profile,
7165 ssl->hostname,
7166 &ssl->session_negotiate->verify_result,
Gilles Peskine449bd832023-01-11 14:50:10 +01007167 f_vrfy, p_vrfy);
7168 } else
Jerry Yud9526692022-02-17 14:23:47 +08007169#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
7170 {
7171 mbedtls_x509_crt *ca_chain;
7172 mbedtls_x509_crl *ca_crl;
7173
7174#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01007175 if (ssl->handshake->sni_ca_chain != NULL) {
Jerry Yud9526692022-02-17 14:23:47 +08007176 ca_chain = ssl->handshake->sni_ca_chain;
7177 ca_crl = ssl->handshake->sni_ca_crl;
Gilles Peskine449bd832023-01-11 14:50:10 +01007178 } else
Jerry Yud9526692022-02-17 14:23:47 +08007179#endif
7180 {
7181 ca_chain = ssl->conf->ca_chain;
7182 ca_crl = ssl->conf->ca_crl;
7183 }
7184
Gilles Peskine449bd832023-01-11 14:50:10 +01007185 if (ca_chain != NULL) {
Jerry Yud9526692022-02-17 14:23:47 +08007186 have_ca_chain = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01007187 }
Jerry Yud9526692022-02-17 14:23:47 +08007188
7189 ret = mbedtls_x509_crt_verify_restartable(
7190 chain,
7191 ca_chain, ca_crl,
7192 ssl->conf->cert_profile,
7193 ssl->hostname,
7194 &ssl->session_negotiate->verify_result,
Gilles Peskine449bd832023-01-11 14:50:10 +01007195 f_vrfy, p_vrfy, rs_ctx);
Jerry Yud9526692022-02-17 14:23:47 +08007196 }
7197
Gilles Peskine449bd832023-01-11 14:50:10 +01007198 if (ret != 0) {
7199 MBEDTLS_SSL_DEBUG_RET(1, "x509_verify_cert", ret);
Jerry Yud9526692022-02-17 14:23:47 +08007200 }
7201
7202#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007203 if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
7204 return MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
7205 }
Jerry Yud9526692022-02-17 14:23:47 +08007206#endif
7207
7208 /*
7209 * Secondary checks: always done, but change 'ret' only if it was 0
7210 */
7211
7212#if defined(MBEDTLS_ECP_C)
7213 {
7214 const mbedtls_pk_context *pk = &chain->pk;
7215
Manuel Pégourié-Gonnard66b0d612022-06-17 10:49:29 +02007216 /* If certificate uses an EC key, make sure the curve is OK.
7217 * This is a public key, so it can't be opaque, so can_do() is a good
7218 * enough check to ensure pk_ec() is safe to use here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007219 if (mbedtls_pk_can_do(pk, MBEDTLS_PK_ECKEY)) {
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007220 /* and in the unlikely case the above assumption no longer holds
7221 * we are making sure that pk_ec() here does not return a NULL
7222 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007223 const mbedtls_ecp_keypair *ec = mbedtls_pk_ec(*pk);
7224 if (ec == NULL) {
7225 MBEDTLS_SSL_DEBUG_MSG(1, ("mbedtls_pk_ec() returned NULL"));
7226 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007227 }
Jerry Yud9526692022-02-17 14:23:47 +08007228
Gilles Peskine449bd832023-01-11 14:50:10 +01007229 if (mbedtls_ssl_check_curve(ssl, ec->grp.id) != 0) {
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007230 ssl->session_negotiate->verify_result |=
7231 MBEDTLS_X509_BADCERT_BAD_KEY;
7232
Gilles Peskine449bd832023-01-11 14:50:10 +01007233 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate (EC key curve)"));
7234 if (ret == 0) {
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007235 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Gilles Peskine449bd832023-01-11 14:50:10 +01007236 }
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007237 }
Jerry Yud9526692022-02-17 14:23:47 +08007238 }
7239 }
7240#endif /* MBEDTLS_ECP_C */
7241
Gilles Peskine449bd832023-01-11 14:50:10 +01007242 if (mbedtls_ssl_check_cert_usage(chain,
7243 ciphersuite_info,
7244 !ssl->conf->endpoint,
7245 &ssl->session_negotiate->verify_result) != 0) {
7246 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate (usage extensions)"));
7247 if (ret == 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007248 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Gilles Peskine449bd832023-01-11 14:50:10 +01007249 }
Jerry Yud9526692022-02-17 14:23:47 +08007250 }
7251
7252 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
7253 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
7254 * with details encoded in the verification flags. All other kinds
7255 * of error codes, including those from the user provided f_vrfy
7256 * functions, are treated as fatal and lead to a failure of
7257 * ssl_parse_certificate even if verification was optional. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007258 if (authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
7259 (ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
7260 ret == MBEDTLS_ERR_SSL_BAD_CERTIFICATE)) {
Jerry Yud9526692022-02-17 14:23:47 +08007261 ret = 0;
7262 }
7263
Gilles Peskine449bd832023-01-11 14:50:10 +01007264 if (have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED) {
7265 MBEDTLS_SSL_DEBUG_MSG(1, ("got no CA chain"));
Jerry Yud9526692022-02-17 14:23:47 +08007266 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
7267 }
7268
Gilles Peskine449bd832023-01-11 14:50:10 +01007269 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007270 uint8_t alert;
7271
7272 /* The certificate may have been rejected for several reasons.
7273 Pick one and send the corresponding alert. Which alert to send
7274 may be a subject of debate in some cases. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007275 if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER) {
Jerry Yud9526692022-02-17 14:23:47 +08007276 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
Gilles Peskine449bd832023-01-11 14:50:10 +01007277 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH) {
Jerry Yud9526692022-02-17 14:23:47 +08007278 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007279 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE) {
Jerry Yud9526692022-02-17 14:23:47 +08007280 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007281 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE) {
Jerry Yud9526692022-02-17 14:23:47 +08007282 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007283 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE) {
Jerry Yud9526692022-02-17 14:23:47 +08007284 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007285 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK) {
Jerry Yud9526692022-02-17 14:23:47 +08007286 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007287 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY) {
Jerry Yud9526692022-02-17 14:23:47 +08007288 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007289 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED) {
Jerry Yud9526692022-02-17 14:23:47 +08007290 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
Gilles Peskine449bd832023-01-11 14:50:10 +01007291 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED) {
Jerry Yud9526692022-02-17 14:23:47 +08007292 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
Gilles Peskine449bd832023-01-11 14:50:10 +01007293 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED) {
Jerry Yud9526692022-02-17 14:23:47 +08007294 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
Gilles Peskine449bd832023-01-11 14:50:10 +01007295 } else {
Jerry Yud9526692022-02-17 14:23:47 +08007296 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
Gilles Peskine449bd832023-01-11 14:50:10 +01007297 }
7298 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7299 alert);
Jerry Yud9526692022-02-17 14:23:47 +08007300 }
7301
7302#if defined(MBEDTLS_DEBUG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007303 if (ssl->session_negotiate->verify_result != 0) {
7304 MBEDTLS_SSL_DEBUG_MSG(3, ("! Certificate verification flags %08x",
7305 (unsigned int) ssl->session_negotiate->verify_result));
7306 } else {
7307 MBEDTLS_SSL_DEBUG_MSG(3, ("Certificate verification flags clear"));
Jerry Yud9526692022-02-17 14:23:47 +08007308 }
7309#endif /* MBEDTLS_DEBUG_C */
7310
Gilles Peskine449bd832023-01-11 14:50:10 +01007311 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007312}
7313
7314#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007315MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007316static int ssl_remember_peer_crt_digest(mbedtls_ssl_context *ssl,
7317 unsigned char *start, size_t len)
Jerry Yud9526692022-02-17 14:23:47 +08007318{
7319 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7320 /* Remember digest of the peer's end-CRT. */
7321 ssl->session_negotiate->peer_cert_digest =
Gilles Peskine449bd832023-01-11 14:50:10 +01007322 mbedtls_calloc(1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN);
7323 if (ssl->session_negotiate->peer_cert_digest == NULL) {
7324 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%d bytes) failed",
7325 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN));
7326 mbedtls_ssl_send_alert_message(ssl,
7327 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7328 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
Jerry Yud9526692022-02-17 14:23:47 +08007329
Gilles Peskine449bd832023-01-11 14:50:10 +01007330 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Jerry Yud9526692022-02-17 14:23:47 +08007331 }
7332
Gilles Peskine449bd832023-01-11 14:50:10 +01007333 ret = mbedtls_md(mbedtls_md_info_from_type(
7334 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE),
7335 start, len,
7336 ssl->session_negotiate->peer_cert_digest);
Jerry Yud9526692022-02-17 14:23:47 +08007337
7338 ssl->session_negotiate->peer_cert_digest_type =
7339 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
7340 ssl->session_negotiate->peer_cert_digest_len =
7341 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
7342
Gilles Peskine449bd832023-01-11 14:50:10 +01007343 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007344}
7345
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007346MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007347static int ssl_remember_peer_pubkey(mbedtls_ssl_context *ssl,
7348 unsigned char *start, size_t len)
Jerry Yud9526692022-02-17 14:23:47 +08007349{
7350 unsigned char *end = start + len;
7351 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7352
7353 /* Make a copy of the peer's raw public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007354 mbedtls_pk_init(&ssl->handshake->peer_pubkey);
7355 ret = mbedtls_pk_parse_subpubkey(&start, end,
7356 &ssl->handshake->peer_pubkey);
7357 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007358 /* We should have parsed the public key before. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007359 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007360 }
7361
Gilles Peskine449bd832023-01-11 14:50:10 +01007362 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08007363}
7364#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7365
Gilles Peskine449bd832023-01-11 14:50:10 +01007366int mbedtls_ssl_parse_certificate(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08007367{
7368 int ret = 0;
7369 int crt_expected;
7370#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7371 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
7372 ? ssl->handshake->sni_authmode
7373 : ssl->conf->authmode;
7374#else
7375 const int authmode = ssl->conf->authmode;
7376#endif
7377 void *rs_ctx = NULL;
7378 mbedtls_x509_crt *chain = NULL;
7379
Gilles Peskine449bd832023-01-11 14:50:10 +01007380 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007381
Gilles Peskine449bd832023-01-11 14:50:10 +01007382 crt_expected = ssl_parse_certificate_coordinate(ssl, authmode);
7383 if (crt_expected == SSL_CERTIFICATE_SKIP) {
7384 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007385 goto exit;
7386 }
7387
7388#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007389 if (ssl->handshake->ecrs_enabled &&
7390 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify) {
Jerry Yud9526692022-02-17 14:23:47 +08007391 chain = ssl->handshake->ecrs_peer_cert;
7392 ssl->handshake->ecrs_peer_cert = NULL;
7393 goto crt_verify;
7394 }
7395#endif
7396
Gilles Peskine449bd832023-01-11 14:50:10 +01007397 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007398 /* mbedtls_ssl_read_record may have sent an alert already. We
7399 let it decide whether to alert. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007400 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
Jerry Yud9526692022-02-17 14:23:47 +08007401 goto exit;
7402 }
7403
7404#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007405 if (ssl_srv_check_client_no_crt_notification(ssl) == 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007406 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
7407
Gilles Peskine449bd832023-01-11 14:50:10 +01007408 if (authmode != MBEDTLS_SSL_VERIFY_OPTIONAL) {
Jerry Yud9526692022-02-17 14:23:47 +08007409 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Gilles Peskine449bd832023-01-11 14:50:10 +01007410 }
Jerry Yud9526692022-02-17 14:23:47 +08007411
7412 goto exit;
7413 }
7414#endif /* MBEDTLS_SSL_SRV_C */
7415
7416 /* Clear existing peer CRT structure in case we tried to
7417 * reuse a session but it failed, and allocate a new one. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007418 ssl_clear_peer_cert(ssl->session_negotiate);
Jerry Yud9526692022-02-17 14:23:47 +08007419
Gilles Peskine449bd832023-01-11 14:50:10 +01007420 chain = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
7421 if (chain == NULL) {
7422 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed",
7423 sizeof(mbedtls_x509_crt)));
7424 mbedtls_ssl_send_alert_message(ssl,
7425 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7426 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
Jerry Yud9526692022-02-17 14:23:47 +08007427
7428 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
7429 goto exit;
7430 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007431 mbedtls_x509_crt_init(chain);
Jerry Yud9526692022-02-17 14:23:47 +08007432
Gilles Peskine449bd832023-01-11 14:50:10 +01007433 ret = ssl_parse_certificate_chain(ssl, chain);
7434 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007435 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007436 }
Jerry Yud9526692022-02-17 14:23:47 +08007437
7438#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007439 if (ssl->handshake->ecrs_enabled) {
Jerry Yud9526692022-02-17 14:23:47 +08007440 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Gilles Peskine449bd832023-01-11 14:50:10 +01007441 }
Jerry Yud9526692022-02-17 14:23:47 +08007442
7443crt_verify:
Gilles Peskine449bd832023-01-11 14:50:10 +01007444 if (ssl->handshake->ecrs_enabled) {
Jerry Yud9526692022-02-17 14:23:47 +08007445 rs_ctx = &ssl->handshake->ecrs_ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +01007446 }
Jerry Yud9526692022-02-17 14:23:47 +08007447#endif
7448
Gilles Peskine449bd832023-01-11 14:50:10 +01007449 ret = ssl_parse_certificate_verify(ssl, authmode,
7450 chain, rs_ctx);
7451 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007452 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007453 }
Jerry Yud9526692022-02-17 14:23:47 +08007454
7455#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7456 {
7457 unsigned char *crt_start, *pk_start;
7458 size_t crt_len, pk_len;
7459
7460 /* We parse the CRT chain without copying, so
7461 * these pointers point into the input buffer,
7462 * and are hence still valid after freeing the
7463 * CRT chain. */
7464
7465 crt_start = chain->raw.p;
7466 crt_len = chain->raw.len;
7467
7468 pk_start = chain->pk_raw.p;
7469 pk_len = chain->pk_raw.len;
7470
7471 /* Free the CRT structures before computing
7472 * digest and copying the peer's public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007473 mbedtls_x509_crt_free(chain);
7474 mbedtls_free(chain);
Jerry Yud9526692022-02-17 14:23:47 +08007475 chain = NULL;
7476
Gilles Peskine449bd832023-01-11 14:50:10 +01007477 ret = ssl_remember_peer_crt_digest(ssl, crt_start, crt_len);
7478 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007479 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007480 }
Jerry Yud9526692022-02-17 14:23:47 +08007481
Gilles Peskine449bd832023-01-11 14:50:10 +01007482 ret = ssl_remember_peer_pubkey(ssl, pk_start, pk_len);
7483 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007484 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007485 }
Jerry Yud9526692022-02-17 14:23:47 +08007486 }
7487#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7488 /* Pass ownership to session structure. */
7489 ssl->session_negotiate->peer_cert = chain;
7490 chain = NULL;
7491#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7492
Gilles Peskine449bd832023-01-11 14:50:10 +01007493 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007494
7495exit:
7496
Gilles Peskine449bd832023-01-11 14:50:10 +01007497 if (ret == 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007498 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01007499 }
Jerry Yud9526692022-02-17 14:23:47 +08007500
7501#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007502 if (ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS) {
Jerry Yud9526692022-02-17 14:23:47 +08007503 ssl->handshake->ecrs_peer_cert = chain;
7504 chain = NULL;
7505 }
7506#endif
7507
Gilles Peskine449bd832023-01-11 14:50:10 +01007508 if (chain != NULL) {
7509 mbedtls_x509_crt_free(chain);
7510 mbedtls_free(chain);
Jerry Yud9526692022-02-17 14:23:47 +08007511 }
7512
Gilles Peskine449bd832023-01-11 14:50:10 +01007513 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007514}
7515#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
7516
Andrzej Kurek25f27152022-08-17 16:09:31 -04007517#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu615bd6f2022-02-17 14:25:15 +08007518static void ssl_calc_finished_tls_sha256(
Gilles Peskine449bd832023-01-11 14:50:10 +01007519 mbedtls_ssl_context *ssl, unsigned char *buf, int from)
Jerry Yu615bd6f2022-02-17 14:25:15 +08007520{
7521 int len = 12;
7522 const char *sender;
7523 unsigned char padbuf[32];
7524#if defined(MBEDTLS_USE_PSA_CRYPTO)
7525 size_t hash_size;
7526 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
7527 psa_status_t status;
7528#else
7529 mbedtls_sha256_context sha256;
7530#endif
7531
7532 mbedtls_ssl_session *session = ssl->session_negotiate;
Gilles Peskine449bd832023-01-11 14:50:10 +01007533 if (!session) {
Jerry Yu615bd6f2022-02-17 14:25:15 +08007534 session = ssl->session;
Gilles Peskine449bd832023-01-11 14:50:10 +01007535 }
Jerry Yu615bd6f2022-02-17 14:25:15 +08007536
Gilles Peskine449bd832023-01-11 14:50:10 +01007537 sender = (from == MBEDTLS_SSL_IS_CLIENT)
Jerry Yu615bd6f2022-02-17 14:25:15 +08007538 ? "client finished"
7539 : "server finished";
7540
7541#if defined(MBEDTLS_USE_PSA_CRYPTO)
7542 sha256_psa = psa_hash_operation_init();
7543
Gilles Peskine449bd832023-01-11 14:50:10 +01007544 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc PSA finished tls sha256"));
Jerry Yu615bd6f2022-02-17 14:25:15 +08007545
Gilles Peskine449bd832023-01-11 14:50:10 +01007546 status = psa_hash_clone(&ssl->handshake->fin_sha256_psa, &sha256_psa);
7547 if (status != PSA_SUCCESS) {
7548 MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash clone failed"));
Jerry Yu615bd6f2022-02-17 14:25:15 +08007549 return;
7550 }
7551
Gilles Peskine449bd832023-01-11 14:50:10 +01007552 status = psa_hash_finish(&sha256_psa, padbuf, sizeof(padbuf), &hash_size);
7553 if (status != PSA_SUCCESS) {
7554 MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash finish failed"));
Jerry Yu615bd6f2022-02-17 14:25:15 +08007555 return;
7556 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007557 MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated padbuf", padbuf, 32);
Jerry Yu615bd6f2022-02-17 14:25:15 +08007558#else
7559
Gilles Peskine449bd832023-01-11 14:50:10 +01007560 mbedtls_sha256_init(&sha256);
Jerry Yu615bd6f2022-02-17 14:25:15 +08007561
Gilles Peskine449bd832023-01-11 14:50:10 +01007562 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc finished tls sha256"));
Jerry Yu615bd6f2022-02-17 14:25:15 +08007563
Gilles Peskine449bd832023-01-11 14:50:10 +01007564 mbedtls_sha256_clone(&sha256, &ssl->handshake->fin_sha256);
Jerry Yu615bd6f2022-02-17 14:25:15 +08007565
7566 /*
7567 * TLSv1.2:
7568 * hash = PRF( master, finished_label,
7569 * Hash( handshake ) )[0.11]
7570 */
7571
7572#if !defined(MBEDTLS_SHA256_ALT)
Gilles Peskine449bd832023-01-11 14:50:10 +01007573 MBEDTLS_SSL_DEBUG_BUF(4, "finished sha2 state", (unsigned char *)
7574 sha256.state, sizeof(sha256.state));
Jerry Yu615bd6f2022-02-17 14:25:15 +08007575#endif
7576
Gilles Peskine449bd832023-01-11 14:50:10 +01007577 mbedtls_sha256_finish(&sha256, padbuf);
7578 mbedtls_sha256_free(&sha256);
Jerry Yu615bd6f2022-02-17 14:25:15 +08007579#endif /* MBEDTLS_USE_PSA_CRYPTO */
7580
Gilles Peskine449bd832023-01-11 14:50:10 +01007581 ssl->handshake->tls_prf(session->master, 48, sender,
7582 padbuf, 32, buf, len);
Jerry Yu615bd6f2022-02-17 14:25:15 +08007583
Gilles Peskine449bd832023-01-11 14:50:10 +01007584 MBEDTLS_SSL_DEBUG_BUF(3, "calc finished result", buf, len);
Jerry Yu615bd6f2022-02-17 14:25:15 +08007585
Gilles Peskine449bd832023-01-11 14:50:10 +01007586 mbedtls_platform_zeroize(padbuf, sizeof(padbuf));
Jerry Yu615bd6f2022-02-17 14:25:15 +08007587
Gilles Peskine449bd832023-01-11 14:50:10 +01007588 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc finished"));
Jerry Yu615bd6f2022-02-17 14:25:15 +08007589}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04007590#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yu615bd6f2022-02-17 14:25:15 +08007591
Jerry Yub7ba49e2022-02-17 14:25:53 +08007592
Andrzej Kurek25f27152022-08-17 16:09:31 -04007593#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yub7ba49e2022-02-17 14:25:53 +08007594static void ssl_calc_finished_tls_sha384(
Gilles Peskine449bd832023-01-11 14:50:10 +01007595 mbedtls_ssl_context *ssl, unsigned char *buf, int from)
Jerry Yub7ba49e2022-02-17 14:25:53 +08007596{
7597 int len = 12;
7598 const char *sender;
7599 unsigned char padbuf[48];
7600#if defined(MBEDTLS_USE_PSA_CRYPTO)
7601 size_t hash_size;
7602 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
7603 psa_status_t status;
7604#else
7605 mbedtls_sha512_context sha512;
7606#endif
7607
7608 mbedtls_ssl_session *session = ssl->session_negotiate;
Gilles Peskine449bd832023-01-11 14:50:10 +01007609 if (!session) {
Jerry Yub7ba49e2022-02-17 14:25:53 +08007610 session = ssl->session;
Gilles Peskine449bd832023-01-11 14:50:10 +01007611 }
Jerry Yub7ba49e2022-02-17 14:25:53 +08007612
Gilles Peskine449bd832023-01-11 14:50:10 +01007613 sender = (from == MBEDTLS_SSL_IS_CLIENT)
Jerry Yub7ba49e2022-02-17 14:25:53 +08007614 ? "client finished"
7615 : "server finished";
7616
7617#if defined(MBEDTLS_USE_PSA_CRYPTO)
7618 sha384_psa = psa_hash_operation_init();
7619
Gilles Peskine449bd832023-01-11 14:50:10 +01007620 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc PSA finished tls sha384"));
Jerry Yub7ba49e2022-02-17 14:25:53 +08007621
Gilles Peskine449bd832023-01-11 14:50:10 +01007622 status = psa_hash_clone(&ssl->handshake->fin_sha384_psa, &sha384_psa);
7623 if (status != PSA_SUCCESS) {
7624 MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash clone failed"));
Jerry Yub7ba49e2022-02-17 14:25:53 +08007625 return;
7626 }
7627
Gilles Peskine449bd832023-01-11 14:50:10 +01007628 status = psa_hash_finish(&sha384_psa, padbuf, sizeof(padbuf), &hash_size);
7629 if (status != PSA_SUCCESS) {
7630 MBEDTLS_SSL_DEBUG_MSG(2, ("PSA hash finish failed"));
Jerry Yub7ba49e2022-02-17 14:25:53 +08007631 return;
7632 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007633 MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated padbuf", padbuf, 48);
Jerry Yub7ba49e2022-02-17 14:25:53 +08007634#else
Gilles Peskine449bd832023-01-11 14:50:10 +01007635 mbedtls_sha512_init(&sha512);
Jerry Yub7ba49e2022-02-17 14:25:53 +08007636
Gilles Peskine449bd832023-01-11 14:50:10 +01007637 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc finished tls sha384"));
Jerry Yub7ba49e2022-02-17 14:25:53 +08007638
Gilles Peskine449bd832023-01-11 14:50:10 +01007639 mbedtls_sha512_clone(&sha512, &ssl->handshake->fin_sha384);
Jerry Yub7ba49e2022-02-17 14:25:53 +08007640
7641 /*
7642 * TLSv1.2:
7643 * hash = PRF( master, finished_label,
7644 * Hash( handshake ) )[0.11]
7645 */
7646
7647#if !defined(MBEDTLS_SHA512_ALT)
Gilles Peskine449bd832023-01-11 14:50:10 +01007648 MBEDTLS_SSL_DEBUG_BUF(4, "finished sha512 state", (unsigned char *)
7649 sha512.state, sizeof(sha512.state));
Jerry Yub7ba49e2022-02-17 14:25:53 +08007650#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01007651 mbedtls_sha512_finish(&sha512, padbuf);
Jerry Yub7ba49e2022-02-17 14:25:53 +08007652
Gilles Peskine449bd832023-01-11 14:50:10 +01007653 mbedtls_sha512_free(&sha512);
Jerry Yub7ba49e2022-02-17 14:25:53 +08007654#endif
7655
Gilles Peskine449bd832023-01-11 14:50:10 +01007656 ssl->handshake->tls_prf(session->master, 48, sender,
7657 padbuf, 48, buf, len);
Jerry Yub7ba49e2022-02-17 14:25:53 +08007658
Gilles Peskine449bd832023-01-11 14:50:10 +01007659 MBEDTLS_SSL_DEBUG_BUF(3, "calc finished result", buf, len);
Jerry Yub7ba49e2022-02-17 14:25:53 +08007660
Gilles Peskine449bd832023-01-11 14:50:10 +01007661 mbedtls_platform_zeroize(padbuf, sizeof(padbuf));
Jerry Yub7ba49e2022-02-17 14:25:53 +08007662
Gilles Peskine449bd832023-01-11 14:50:10 +01007663 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc finished"));
Jerry Yub7ba49e2022-02-17 14:25:53 +08007664}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04007665#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yub7ba49e2022-02-17 14:25:53 +08007666
Gilles Peskine449bd832023-01-11 14:50:10 +01007667void mbedtls_ssl_handshake_wrapup_free_hs_transform(mbedtls_ssl_context *ssl)
Jerry Yuaef00152022-02-17 14:27:31 +08007668{
Gilles Peskine449bd832023-01-11 14:50:10 +01007669 MBEDTLS_SSL_DEBUG_MSG(3, ("=> handshake wrapup: final free"));
Jerry Yuaef00152022-02-17 14:27:31 +08007670
7671 /*
7672 * Free our handshake params
7673 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007674 mbedtls_ssl_handshake_free(ssl);
7675 mbedtls_free(ssl->handshake);
Jerry Yuaef00152022-02-17 14:27:31 +08007676 ssl->handshake = NULL;
7677
7678 /*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08007679 * Free the previous transform and switch in the current one
Jerry Yuaef00152022-02-17 14:27:31 +08007680 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007681 if (ssl->transform) {
7682 mbedtls_ssl_transform_free(ssl->transform);
7683 mbedtls_free(ssl->transform);
Jerry Yuaef00152022-02-17 14:27:31 +08007684 }
7685 ssl->transform = ssl->transform_negotiate;
7686 ssl->transform_negotiate = NULL;
7687
Gilles Peskine449bd832023-01-11 14:50:10 +01007688 MBEDTLS_SSL_DEBUG_MSG(3, ("<= handshake wrapup: final free"));
Jerry Yuaef00152022-02-17 14:27:31 +08007689}
7690
Gilles Peskine449bd832023-01-11 14:50:10 +01007691void mbedtls_ssl_handshake_wrapup(mbedtls_ssl_context *ssl)
Jerry Yu2a9fff52022-02-17 14:28:51 +08007692{
7693 int resume = ssl->handshake->resume;
7694
Gilles Peskine449bd832023-01-11 14:50:10 +01007695 MBEDTLS_SSL_DEBUG_MSG(3, ("=> handshake wrapup"));
Jerry Yu2a9fff52022-02-17 14:28:51 +08007696
7697#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01007698 if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
Jerry Yu2a9fff52022-02-17 14:28:51 +08007699 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
7700 ssl->renego_records_seen = 0;
7701 }
7702#endif
7703
7704 /*
7705 * Free the previous session and switch in the current one
7706 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007707 if (ssl->session) {
Jerry Yu2a9fff52022-02-17 14:28:51 +08007708#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
7709 /* RFC 7366 3.1: keep the EtM state */
7710 ssl->session_negotiate->encrypt_then_mac =
Gilles Peskine449bd832023-01-11 14:50:10 +01007711 ssl->session->encrypt_then_mac;
Jerry Yu2a9fff52022-02-17 14:28:51 +08007712#endif
7713
Gilles Peskine449bd832023-01-11 14:50:10 +01007714 mbedtls_ssl_session_free(ssl->session);
7715 mbedtls_free(ssl->session);
Jerry Yu2a9fff52022-02-17 14:28:51 +08007716 }
7717 ssl->session = ssl->session_negotiate;
7718 ssl->session_negotiate = NULL;
7719
7720 /*
7721 * Add cache entry
7722 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007723 if (ssl->conf->f_set_cache != NULL &&
Jerry Yu2a9fff52022-02-17 14:28:51 +08007724 ssl->session->id_len != 0 &&
Gilles Peskine449bd832023-01-11 14:50:10 +01007725 resume == 0) {
7726 if (ssl->conf->f_set_cache(ssl->conf->p_cache,
7727 ssl->session->id,
7728 ssl->session->id_len,
7729 ssl->session) != 0) {
7730 MBEDTLS_SSL_DEBUG_MSG(1, ("cache did not store session"));
7731 }
Jerry Yu2a9fff52022-02-17 14:28:51 +08007732 }
7733
7734#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007735 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7736 ssl->handshake->flight != NULL) {
Jerry Yu2a9fff52022-02-17 14:28:51 +08007737 /* Cancel handshake timer */
Gilles Peskine449bd832023-01-11 14:50:10 +01007738 mbedtls_ssl_set_timer(ssl, 0);
Jerry Yu2a9fff52022-02-17 14:28:51 +08007739
7740 /* Keep last flight around in case we need to resend it:
7741 * we need the handshake and transform structures for that */
Gilles Peskine449bd832023-01-11 14:50:10 +01007742 MBEDTLS_SSL_DEBUG_MSG(3, ("skip freeing handshake and transform"));
7743 } else
Jerry Yu2a9fff52022-02-17 14:28:51 +08007744#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01007745 mbedtls_ssl_handshake_wrapup_free_hs_transform(ssl);
Jerry Yu2a9fff52022-02-17 14:28:51 +08007746
Jerry Yu5ed73ff2022-10-27 13:08:42 +08007747 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
Jerry Yu2a9fff52022-02-17 14:28:51 +08007748
Gilles Peskine449bd832023-01-11 14:50:10 +01007749 MBEDTLS_SSL_DEBUG_MSG(3, ("<= handshake wrapup"));
Jerry Yu2a9fff52022-02-17 14:28:51 +08007750}
7751
Gilles Peskine449bd832023-01-11 14:50:10 +01007752int mbedtls_ssl_write_finished(mbedtls_ssl_context *ssl)
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007753{
7754 int ret, hash_len;
7755
Gilles Peskine449bd832023-01-11 14:50:10 +01007756 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write finished"));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007757
Gilles Peskine449bd832023-01-11 14:50:10 +01007758 mbedtls_ssl_update_out_pointers(ssl, ssl->transform_negotiate);
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007759
Gilles Peskine449bd832023-01-11 14:50:10 +01007760 ssl->handshake->calc_finished(ssl, ssl->out_msg + 4, ssl->conf->endpoint);
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007761
7762 /*
7763 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7764 * may define some other value. Currently (early 2016), no defined
7765 * ciphersuite does this (and this is unlikely to change as activity has
7766 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7767 */
7768 hash_len = 12;
7769
7770#if defined(MBEDTLS_SSL_RENEGOTIATION)
7771 ssl->verify_data_len = hash_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01007772 memcpy(ssl->own_verify_data, ssl->out_msg + 4, hash_len);
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007773#endif
7774
7775 ssl->out_msglen = 4 + hash_len;
7776 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7777 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
7778
7779 /*
7780 * In case of session resuming, invert the client and server
7781 * ChangeCipherSpec messages order.
7782 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007783 if (ssl->handshake->resume != 0) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007784#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007785 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007786 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Gilles Peskine449bd832023-01-11 14:50:10 +01007787 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007788#endif
7789#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007790 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007791 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Gilles Peskine449bd832023-01-11 14:50:10 +01007792 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007793#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01007794 } else {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007795 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01007796 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007797
7798 /*
7799 * Switch to our negotiated transform and session parameters for outbound
7800 * data.
7801 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007802 MBEDTLS_SSL_DEBUG_MSG(3, ("switching to new transform spec for outbound data"));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007803
7804#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007805 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007806 unsigned char i;
7807
7808 /* Remember current epoch settings for resending */
7809 ssl->handshake->alt_transform_out = ssl->transform_out;
Gilles Peskine449bd832023-01-11 14:50:10 +01007810 memcpy(ssl->handshake->alt_out_ctr, ssl->cur_out_ctr,
7811 sizeof(ssl->handshake->alt_out_ctr));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007812
7813 /* Set sequence_number to zero */
Gilles Peskine449bd832023-01-11 14:50:10 +01007814 memset(&ssl->cur_out_ctr[2], 0, sizeof(ssl->cur_out_ctr) - 2);
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007815
7816
7817 /* Increment epoch */
Gilles Peskine449bd832023-01-11 14:50:10 +01007818 for (i = 2; i > 0; i--) {
7819 if (++ssl->cur_out_ctr[i - 1] != 0) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007820 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01007821 }
7822 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007823
7824 /* The loop goes to its end iff the counter is wrapping */
Gilles Peskine449bd832023-01-11 14:50:10 +01007825 if (i == 0) {
7826 MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS epoch would wrap"));
7827 return MBEDTLS_ERR_SSL_COUNTER_WRAPPING;
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007828 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007829 } else
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007830#endif /* MBEDTLS_SSL_PROTO_DTLS */
Gilles Peskine449bd832023-01-11 14:50:10 +01007831 memset(ssl->cur_out_ctr, 0, sizeof(ssl->cur_out_ctr));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007832
7833 ssl->transform_out = ssl->transform_negotiate;
7834 ssl->session_out = ssl->session_negotiate;
7835
7836#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007837 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
7838 mbedtls_ssl_send_flight_completed(ssl);
7839 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007840#endif
7841
Gilles Peskine449bd832023-01-11 14:50:10 +01007842 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
7843 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
7844 return ret;
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007845 }
7846
7847#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007848 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7849 (ret = mbedtls_ssl_flight_transmit(ssl)) != 0) {
7850 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_flight_transmit", ret);
7851 return ret;
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007852 }
7853#endif
7854
Gilles Peskine449bd832023-01-11 14:50:10 +01007855 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write finished"));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007856
Gilles Peskine449bd832023-01-11 14:50:10 +01007857 return 0;
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007858}
7859
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007860#define SSL_MAX_HASH_LEN 12
7861
Gilles Peskine449bd832023-01-11 14:50:10 +01007862int mbedtls_ssl_parse_finished(mbedtls_ssl_context *ssl)
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007863{
7864 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7865 unsigned int hash_len = 12;
7866 unsigned char buf[SSL_MAX_HASH_LEN];
7867
Gilles Peskine449bd832023-01-11 14:50:10 +01007868 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse finished"));
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007869
Gilles Peskine449bd832023-01-11 14:50:10 +01007870 ssl->handshake->calc_finished(ssl, buf, ssl->conf->endpoint ^ 1);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007871
Gilles Peskine449bd832023-01-11 14:50:10 +01007872 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
7873 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007874 goto exit;
7875 }
7876
Gilles Peskine449bd832023-01-11 14:50:10 +01007877 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
7878 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
7879 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7880 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007881 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
7882 goto exit;
7883 }
7884
Gilles Peskine449bd832023-01-11 14:50:10 +01007885 if (ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED) {
7886 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7887 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007888 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
7889 goto exit;
7890 }
7891
Gilles Peskine449bd832023-01-11 14:50:10 +01007892 if (ssl->in_hslen != mbedtls_ssl_hs_hdr_len(ssl) + hash_len) {
7893 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
7894 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7895 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007896 ret = MBEDTLS_ERR_SSL_DECODE_ERROR;
7897 goto exit;
7898 }
7899
Gilles Peskine449bd832023-01-11 14:50:10 +01007900 if (mbedtls_ct_memcmp(ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl),
7901 buf, hash_len) != 0) {
7902 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
7903 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7904 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007905 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
7906 goto exit;
7907 }
7908
7909#if defined(MBEDTLS_SSL_RENEGOTIATION)
7910 ssl->verify_data_len = hash_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01007911 memcpy(ssl->peer_verify_data, buf, hash_len);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007912#endif
7913
Gilles Peskine449bd832023-01-11 14:50:10 +01007914 if (ssl->handshake->resume != 0) {
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007915#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007916 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007917 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Gilles Peskine449bd832023-01-11 14:50:10 +01007918 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007919#endif
7920#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007921 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007922 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Gilles Peskine449bd832023-01-11 14:50:10 +01007923 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007924#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01007925 } else {
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007926 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01007927 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007928
7929#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007930 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
7931 mbedtls_ssl_recv_flight_completed(ssl);
7932 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007933#endif
7934
Gilles Peskine449bd832023-01-11 14:50:10 +01007935 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse finished"));
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007936
7937exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01007938 mbedtls_platform_zeroize(buf, hash_len);
7939 return ret;
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007940}
7941
Jerry Yu392112c2022-02-17 14:34:10 +08007942#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
7943/*
7944 * Helper to get TLS 1.2 PRF from ciphersuite
7945 * (Duplicates bits of logic from ssl_set_handshake_prfs().)
7946 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007947static tls_prf_fn ssl_tls12prf_from_cs(int ciphersuite_id)
Jerry Yu392112c2022-02-17 14:34:10 +08007948{
Jerry Yu392112c2022-02-17 14:34:10 +08007949 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01007950 mbedtls_ssl_ciphersuite_from_id(ciphersuite_id);
Andrzej Kurek68327742022-10-03 06:18:18 -04007951#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01007952 if (ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA384) {
7953 return tls_prf_sha384;
7954 } else
Jerry Yu392112c2022-02-17 14:34:10 +08007955#endif
Andrzej Kurek894edde2022-09-29 06:31:14 -04007956#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
7957 {
Gilles Peskine449bd832023-01-11 14:50:10 +01007958 if (ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA256) {
7959 return tls_prf_sha256;
7960 }
Andrzej Kurek894edde2022-09-29 06:31:14 -04007961 }
7962#endif
7963#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
7964 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
7965 (void) ciphersuite_info;
7966#endif
Andrzej Kurekeabeb302022-10-17 07:52:51 -04007967
Gilles Peskine449bd832023-01-11 14:50:10 +01007968 return NULL;
Jerry Yu392112c2022-02-17 14:34:10 +08007969}
7970#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Jerry Yue93ffcd2022-02-17 14:37:06 +08007971
Gilles Peskine449bd832023-01-11 14:50:10 +01007972static mbedtls_tls_prf_types tls_prf_get_type(mbedtls_ssl_tls_prf_cb *tls_prf)
Jerry Yue93ffcd2022-02-17 14:37:06 +08007973{
7974 ((void) tls_prf);
Andrzej Kurek25f27152022-08-17 16:09:31 -04007975#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01007976 if (tls_prf == tls_prf_sha384) {
7977 return MBEDTLS_SSL_TLS_PRF_SHA384;
7978 } else
Jerry Yue93ffcd2022-02-17 14:37:06 +08007979#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04007980#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01007981 if (tls_prf == tls_prf_sha256) {
7982 return MBEDTLS_SSL_TLS_PRF_SHA256;
7983 } else
Jerry Yue93ffcd2022-02-17 14:37:06 +08007984#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01007985 return MBEDTLS_SSL_TLS_PRF_NONE;
Jerry Yue93ffcd2022-02-17 14:37:06 +08007986}
7987
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007988/*
7989 * Populate a transform structure with session keys and all the other
7990 * necessary information.
7991 *
7992 * Parameters:
7993 * - [in/out]: transform: structure to populate
7994 * [in] must be just initialised with mbedtls_ssl_transform_init()
7995 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
7996 * - [in] ciphersuite
7997 * - [in] master
7998 * - [in] encrypt_then_mac
Jerry Yu9bccc4c2022-02-17 14:38:28 +08007999 * - [in] tls_prf: pointer to PRF to use for key derivation
8000 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
Glenn Strauss07c64162022-03-14 12:34:51 -04008001 * - [in] tls_version: TLS version
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008002 * - [in] endpoint: client or server
8003 * - [in] ssl: used for:
8004 * - ssl->conf->{f,p}_export_keys
8005 * [in] optionally used for:
8006 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
8007 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02008008MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01008009static int ssl_tls12_populate_transform(mbedtls_ssl_transform *transform,
8010 int ciphersuite,
8011 const unsigned char master[48],
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008012#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01008013 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008014#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01008015 ssl_tls_prf_t tls_prf,
8016 const unsigned char randbytes[64],
8017 mbedtls_ssl_protocol_version tls_version,
8018 unsigned endpoint,
8019 const mbedtls_ssl_context *ssl)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008020{
8021 int ret = 0;
8022 unsigned char keyblk[256];
8023 unsigned char *key1;
8024 unsigned char *key2;
8025 unsigned char *mac_enc;
8026 unsigned char *mac_dec;
8027 size_t mac_key_len = 0;
8028 size_t iv_copy_len;
8029 size_t keylen;
8030 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008031 mbedtls_ssl_mode_t ssl_mode;
Neil Armstronge4512952022-03-08 09:08:22 +01008032#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008033 const mbedtls_cipher_info_t *cipher_info;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008034 const mbedtls_md_info_t *md_info;
Neil Armstronge4512952022-03-08 09:08:22 +01008035#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008036
8037#if defined(MBEDTLS_USE_PSA_CRYPTO)
8038 psa_key_type_t key_type;
8039 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8040 psa_algorithm_t alg;
Neil Armstronge4512952022-03-08 09:08:22 +01008041 psa_algorithm_t mac_alg = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008042 size_t key_bits;
8043 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
8044#endif
8045
8046#if !defined(MBEDTLS_DEBUG_C) && \
8047 !defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Gilles Peskine449bd832023-01-11 14:50:10 +01008048 if (ssl->f_export_keys == NULL) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008049 ssl = NULL; /* make sure we don't use it except for these cases */
8050 (void) ssl;
8051 }
8052#endif
8053
8054 /*
8055 * Some data just needs copying into the structure
8056 */
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008057#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008058 transform->encrypt_then_mac = encrypt_then_mac;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008059#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Glenn Strauss07c64162022-03-14 12:34:51 -04008060 transform->tls_version = tls_version;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008061
8062#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01008063 memcpy(transform->randbytes, randbytes, sizeof(transform->randbytes));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008064#endif
8065
8066#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01008067 if (tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008068 /* At the moment, we keep TLS <= 1.2 and TLS 1.3 transform
8069 * generation separate. This should never happen. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008070 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008071 }
8072#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
8073
8074 /*
8075 * Get various info structures
8076 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008077 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(ciphersuite);
8078 if (ciphersuite_info == NULL) {
8079 MBEDTLS_SSL_DEBUG_MSG(1, ("ciphersuite info for %d not found",
8080 ciphersuite));
8081 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008082 }
8083
Neil Armstrongab555e02022-04-04 11:07:59 +02008084 ssl_mode = mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008085#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01008086 encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008087#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01008088 ciphersuite_info);
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008089
Gilles Peskine449bd832023-01-11 14:50:10 +01008090 if (ssl_mode == MBEDTLS_SSL_MODE_AEAD) {
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008091 transform->taglen =
8092 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Gilles Peskine449bd832023-01-11 14:50:10 +01008093 }
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008094
8095#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008096 if ((status = mbedtls_ssl_cipher_to_psa(ciphersuite_info->cipher,
8097 transform->taglen,
8098 &alg,
8099 &key_type,
8100 &key_bits)) != PSA_SUCCESS) {
8101 ret = psa_ssl_status_to_mbedtls(status);
8102 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_cipher_to_psa", ret);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008103 goto end;
8104 }
8105#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008106 cipher_info = mbedtls_cipher_info_from_type(ciphersuite_info->cipher);
8107 if (cipher_info == NULL) {
8108 MBEDTLS_SSL_DEBUG_MSG(1, ("cipher info for %u not found",
8109 ciphersuite_info->cipher));
8110 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008111 }
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008112#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008113
Neil Armstronge4512952022-03-08 09:08:22 +01008114#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008115 mac_alg = mbedtls_hash_info_psa_from_md(ciphersuite_info->mac);
8116 if (mac_alg == 0) {
8117 MBEDTLS_SSL_DEBUG_MSG(1, ("mbedtls_hash_info_psa_from_md for %u not found",
8118 (unsigned) ciphersuite_info->mac));
8119 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Neil Armstronge4512952022-03-08 09:08:22 +01008120 }
8121#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008122 md_info = mbedtls_md_info_from_type(ciphersuite_info->mac);
8123 if (md_info == NULL) {
8124 MBEDTLS_SSL_DEBUG_MSG(1, ("mbedtls_md info for %u not found",
8125 (unsigned) ciphersuite_info->mac));
8126 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008127 }
Neil Armstronge4512952022-03-08 09:08:22 +01008128#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008129
8130#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
8131 /* Copy own and peer's CID if the use of the CID
8132 * extension has been negotiated. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008133 if (ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED) {
8134 MBEDTLS_SSL_DEBUG_MSG(3, ("Copy CIDs into SSL transform"));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008135
8136 transform->in_cid_len = ssl->own_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008137 memcpy(transform->in_cid, ssl->own_cid, ssl->own_cid_len);
8138 MBEDTLS_SSL_DEBUG_BUF(3, "Incoming CID", transform->in_cid,
8139 transform->in_cid_len);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008140
8141 transform->out_cid_len = ssl->handshake->peer_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008142 memcpy(transform->out_cid, ssl->handshake->peer_cid,
8143 ssl->handshake->peer_cid_len);
8144 MBEDTLS_SSL_DEBUG_BUF(3, "Outgoing CID", transform->out_cid,
8145 transform->out_cid_len);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008146 }
8147#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
8148
8149 /*
8150 * Compute key block using the PRF
8151 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008152 ret = tls_prf(master, 48, "key expansion", randbytes, 64, keyblk, 256);
8153 if (ret != 0) {
8154 MBEDTLS_SSL_DEBUG_RET(1, "prf", ret);
8155 return ret;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008156 }
8157
Gilles Peskine449bd832023-01-11 14:50:10 +01008158 MBEDTLS_SSL_DEBUG_MSG(3, ("ciphersuite = %s",
8159 mbedtls_ssl_get_ciphersuite_name(ciphersuite)));
8160 MBEDTLS_SSL_DEBUG_BUF(3, "master secret", master, 48);
8161 MBEDTLS_SSL_DEBUG_BUF(4, "random bytes", randbytes, 64);
8162 MBEDTLS_SSL_DEBUG_BUF(4, "key block", keyblk, 256);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008163
8164 /*
8165 * Determine the appropriate key, IV and MAC length.
8166 */
8167
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008168#if defined(MBEDTLS_USE_PSA_CRYPTO)
8169 keylen = PSA_BITS_TO_BYTES(key_bits);
8170#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008171 keylen = mbedtls_cipher_info_get_key_bitlen(cipher_info) / 8;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008172#endif
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008173
8174#if defined(MBEDTLS_GCM_C) || \
8175 defined(MBEDTLS_CCM_C) || \
8176 defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008177 if (ssl_mode == MBEDTLS_SSL_MODE_AEAD) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008178 size_t explicit_ivlen;
8179
8180 transform->maclen = 0;
8181 mac_key_len = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008182
8183 /* All modes haves 96-bit IVs, but the length of the static parts vary
8184 * with mode and version:
8185 * - For GCM and CCM in TLS 1.2, there's a static IV of 4 Bytes
8186 * (to be concatenated with a dynamically chosen IV of 8 Bytes)
8187 * - For ChaChaPoly in TLS 1.2, and all modes in TLS 1.3, there's
8188 * a static IV of 12 Bytes (to be XOR'ed with the 8 Byte record
8189 * sequence number).
8190 */
8191 transform->ivlen = 12;
David Horstmann3b2276a2022-10-06 14:49:08 +01008192
8193 int is_chachapoly = 0;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008194#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008195 is_chachapoly = (key_type == PSA_KEY_TYPE_CHACHA20);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008196#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008197 is_chachapoly = (mbedtls_cipher_info_get_mode(cipher_info)
8198 == MBEDTLS_MODE_CHACHAPOLY);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008199#endif /* MBEDTLS_USE_PSA_CRYPTO */
David Horstmann3b2276a2022-10-06 14:49:08 +01008200
Gilles Peskine449bd832023-01-11 14:50:10 +01008201 if (is_chachapoly) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008202 transform->fixed_ivlen = 12;
Gilles Peskine449bd832023-01-11 14:50:10 +01008203 } else {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008204 transform->fixed_ivlen = 4;
Gilles Peskine449bd832023-01-11 14:50:10 +01008205 }
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008206
8207 /* Minimum length of encrypted record */
8208 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
8209 transform->minlen = explicit_ivlen + transform->taglen;
Gilles Peskine449bd832023-01-11 14:50:10 +01008210 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008211#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
8212#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01008213 if (ssl_mode == MBEDTLS_SSL_MODE_STREAM ||
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008214 ssl_mode == MBEDTLS_SSL_MODE_CBC ||
Gilles Peskine449bd832023-01-11 14:50:10 +01008215 ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM) {
Neil Armstronge4512952022-03-08 09:08:22 +01008216#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008217 size_t block_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008218#else
8219 size_t block_size = cipher_info->block_size;
8220#endif /* MBEDTLS_USE_PSA_CRYPTO */
8221
8222#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronge4512952022-03-08 09:08:22 +01008223 /* Get MAC length */
8224 mac_key_len = PSA_HASH_LENGTH(mac_alg);
8225#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008226 /* Initialize HMAC contexts */
Gilles Peskine449bd832023-01-11 14:50:10 +01008227 if ((ret = mbedtls_md_setup(&transform->md_ctx_enc, md_info, 1)) != 0 ||
8228 (ret = mbedtls_md_setup(&transform->md_ctx_dec, md_info, 1)) != 0) {
8229 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_setup", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008230 goto end;
8231 }
8232
8233 /* Get MAC length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008234 mac_key_len = mbedtls_md_get_size(md_info);
Neil Armstronge4512952022-03-08 09:08:22 +01008235#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008236 transform->maclen = mac_key_len;
8237
8238 /* IV length */
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008239#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008240 transform->ivlen = PSA_CIPHER_IV_LENGTH(key_type, alg);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008241#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008242 transform->ivlen = cipher_info->iv_size;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008243#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008244
8245 /* Minimum length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008246 if (ssl_mode == MBEDTLS_SSL_MODE_STREAM) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008247 transform->minlen = transform->maclen;
Gilles Peskine449bd832023-01-11 14:50:10 +01008248 } else {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008249 /*
8250 * GenericBlockCipher:
8251 * 1. if EtM is in use: one block plus MAC
8252 * otherwise: * first multiple of blocklen greater than maclen
8253 * 2. IV
8254 */
8255#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01008256 if (ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008257 transform->minlen = transform->maclen
Gilles Peskine449bd832023-01-11 14:50:10 +01008258 + block_size;
8259 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008260#endif
8261 {
8262 transform->minlen = transform->maclen
Gilles Peskine449bd832023-01-11 14:50:10 +01008263 + block_size
8264 - transform->maclen % block_size;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008265 }
8266
Gilles Peskine449bd832023-01-11 14:50:10 +01008267 if (tls_version == MBEDTLS_SSL_VERSION_TLS1_2) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008268 transform->minlen += transform->ivlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01008269 } else {
8270 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008271 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
8272 goto end;
8273 }
8274 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008275 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008276#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
8277 {
Gilles Peskine449bd832023-01-11 14:50:10 +01008278 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
8279 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008280 }
8281
Gilles Peskine449bd832023-01-11 14:50:10 +01008282 MBEDTLS_SSL_DEBUG_MSG(3, ("keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
8283 (unsigned) keylen,
8284 (unsigned) transform->minlen,
8285 (unsigned) transform->ivlen,
8286 (unsigned) transform->maclen));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008287
8288 /*
8289 * Finally setup the cipher contexts, IVs and MAC secrets.
8290 */
8291#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008292 if (endpoint == MBEDTLS_SSL_IS_CLIENT) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008293 key1 = keyblk + mac_key_len * 2;
8294 key2 = keyblk + mac_key_len * 2 + keylen;
8295
8296 mac_enc = keyblk;
8297 mac_dec = keyblk + mac_key_len;
8298
Gilles Peskine449bd832023-01-11 14:50:10 +01008299 iv_copy_len = (transform->fixed_ivlen) ?
8300 transform->fixed_ivlen : transform->ivlen;
8301 memcpy(transform->iv_enc, key2 + keylen, iv_copy_len);
8302 memcpy(transform->iv_dec, key2 + keylen + iv_copy_len,
8303 iv_copy_len);
8304 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008305#endif /* MBEDTLS_SSL_CLI_C */
8306#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008307 if (endpoint == MBEDTLS_SSL_IS_SERVER) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008308 key1 = keyblk + mac_key_len * 2 + keylen;
8309 key2 = keyblk + mac_key_len * 2;
8310
8311 mac_enc = keyblk + mac_key_len;
8312 mac_dec = keyblk;
8313
Gilles Peskine449bd832023-01-11 14:50:10 +01008314 iv_copy_len = (transform->fixed_ivlen) ?
8315 transform->fixed_ivlen : transform->ivlen;
8316 memcpy(transform->iv_dec, key1 + keylen, iv_copy_len);
8317 memcpy(transform->iv_enc, key1 + keylen + iv_copy_len,
8318 iv_copy_len);
8319 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008320#endif /* MBEDTLS_SSL_SRV_C */
8321 {
Gilles Peskine449bd832023-01-11 14:50:10 +01008322 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008323 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
8324 goto end;
8325 }
8326
Gilles Peskine449bd832023-01-11 14:50:10 +01008327 if (ssl != NULL && ssl->f_export_keys != NULL) {
8328 ssl->f_export_keys(ssl->p_export_keys,
8329 MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET,
8330 master, 48,
8331 randbytes + 32,
8332 randbytes,
8333 tls_prf_get_type(tls_prf));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008334 }
8335
8336#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008337 transform->psa_alg = alg;
8338
Gilles Peskine449bd832023-01-11 14:50:10 +01008339 if (alg != MBEDTLS_SSL_NULL_CIPHER) {
8340 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
8341 psa_set_key_algorithm(&attributes, alg);
8342 psa_set_key_type(&attributes, key_type);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008343
Gilles Peskine449bd832023-01-11 14:50:10 +01008344 if ((status = psa_import_key(&attributes,
8345 key1,
8346 PSA_BITS_TO_BYTES(key_bits),
8347 &transform->psa_key_enc)) != PSA_SUCCESS) {
8348 MBEDTLS_SSL_DEBUG_RET(3, "psa_import_key", (int) status);
8349 ret = psa_ssl_status_to_mbedtls(status);
8350 MBEDTLS_SSL_DEBUG_RET(1, "psa_import_key", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008351 goto end;
8352 }
8353
Gilles Peskine449bd832023-01-11 14:50:10 +01008354 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008355
Gilles Peskine449bd832023-01-11 14:50:10 +01008356 if ((status = psa_import_key(&attributes,
8357 key2,
8358 PSA_BITS_TO_BYTES(key_bits),
8359 &transform->psa_key_dec)) != PSA_SUCCESS) {
8360 ret = psa_ssl_status_to_mbedtls(status);
8361 MBEDTLS_SSL_DEBUG_RET(1, "psa_import_key", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008362 goto end;
8363 }
8364 }
8365#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008366 if ((ret = mbedtls_cipher_setup(&transform->cipher_ctx_enc,
8367 cipher_info)) != 0) {
8368 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008369 goto end;
8370 }
8371
Gilles Peskine449bd832023-01-11 14:50:10 +01008372 if ((ret = mbedtls_cipher_setup(&transform->cipher_ctx_dec,
8373 cipher_info)) != 0) {
8374 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008375 goto end;
8376 }
8377
Gilles Peskine449bd832023-01-11 14:50:10 +01008378 if ((ret = mbedtls_cipher_setkey(&transform->cipher_ctx_enc, key1,
8379 (int) mbedtls_cipher_info_get_key_bitlen(cipher_info),
8380 MBEDTLS_ENCRYPT)) != 0) {
8381 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setkey", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008382 goto end;
8383 }
8384
Gilles Peskine449bd832023-01-11 14:50:10 +01008385 if ((ret = mbedtls_cipher_setkey(&transform->cipher_ctx_dec, key2,
8386 (int) mbedtls_cipher_info_get_key_bitlen(cipher_info),
8387 MBEDTLS_DECRYPT)) != 0) {
8388 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setkey", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008389 goto end;
8390 }
8391
8392#if defined(MBEDTLS_CIPHER_MODE_CBC)
Gilles Peskine449bd832023-01-11 14:50:10 +01008393 if (mbedtls_cipher_info_get_mode(cipher_info) == MBEDTLS_MODE_CBC) {
8394 if ((ret = mbedtls_cipher_set_padding_mode(&transform->cipher_ctx_enc,
8395 MBEDTLS_PADDING_NONE)) != 0) {
8396 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_set_padding_mode", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008397 goto end;
8398 }
8399
Gilles Peskine449bd832023-01-11 14:50:10 +01008400 if ((ret = mbedtls_cipher_set_padding_mode(&transform->cipher_ctx_dec,
8401 MBEDTLS_PADDING_NONE)) != 0) {
8402 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_set_padding_mode", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008403 goto end;
8404 }
8405 }
8406#endif /* MBEDTLS_CIPHER_MODE_CBC */
8407#endif /* MBEDTLS_USE_PSA_CRYPTO */
8408
Neil Armstrong29c0c042022-03-17 17:47:28 +01008409#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
8410 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
8411 For AEAD-based ciphersuites, there is nothing to do here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008412 if (mac_key_len != 0) {
Neil Armstrong29c0c042022-03-17 17:47:28 +01008413#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008414 transform->psa_mac_alg = PSA_ALG_HMAC(mac_alg);
Neil Armstrong29c0c042022-03-17 17:47:28 +01008415
Gilles Peskine449bd832023-01-11 14:50:10 +01008416 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
8417 psa_set_key_algorithm(&attributes, PSA_ALG_HMAC(mac_alg));
8418 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
Neil Armstrong29c0c042022-03-17 17:47:28 +01008419
Gilles Peskine449bd832023-01-11 14:50:10 +01008420 if ((status = psa_import_key(&attributes,
8421 mac_enc, mac_key_len,
8422 &transform->psa_mac_enc)) != PSA_SUCCESS) {
8423 ret = psa_ssl_status_to_mbedtls(status);
8424 MBEDTLS_SSL_DEBUG_RET(1, "psa_import_mac_key", ret);
Neil Armstrong29c0c042022-03-17 17:47:28 +01008425 goto end;
8426 }
8427
Gilles Peskine449bd832023-01-11 14:50:10 +01008428 if ((transform->psa_alg == MBEDTLS_SSL_NULL_CIPHER) ||
8429 ((transform->psa_alg == PSA_ALG_CBC_NO_PADDING)
Andrzej Kurek8c95ac42022-08-17 16:17:00 -04008430#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01008431 && (transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED)
Andrzej Kurek8c95ac42022-08-17 16:17:00 -04008432#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01008433 )) {
Neil Armstrong29c0c042022-03-17 17:47:28 +01008434 /* mbedtls_ct_hmac() requires the key to be exportable */
Gilles Peskine449bd832023-01-11 14:50:10 +01008435 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT |
8436 PSA_KEY_USAGE_VERIFY_HASH);
8437 } else {
8438 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
8439 }
Neil Armstrong29c0c042022-03-17 17:47:28 +01008440
Gilles Peskine449bd832023-01-11 14:50:10 +01008441 if ((status = psa_import_key(&attributes,
8442 mac_dec, mac_key_len,
8443 &transform->psa_mac_dec)) != PSA_SUCCESS) {
8444 ret = psa_ssl_status_to_mbedtls(status);
8445 MBEDTLS_SSL_DEBUG_RET(1, "psa_import_mac_key", ret);
Neil Armstrong29c0c042022-03-17 17:47:28 +01008446 goto end;
8447 }
8448#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008449 ret = mbedtls_md_hmac_starts(&transform->md_ctx_enc, mac_enc, mac_key_len);
8450 if (ret != 0) {
Neil Armstrong29c0c042022-03-17 17:47:28 +01008451 goto end;
Gilles Peskine449bd832023-01-11 14:50:10 +01008452 }
8453 ret = mbedtls_md_hmac_starts(&transform->md_ctx_dec, mac_dec, mac_key_len);
8454 if (ret != 0) {
Neil Armstrong29c0c042022-03-17 17:47:28 +01008455 goto end;
Gilles Peskine449bd832023-01-11 14:50:10 +01008456 }
Neil Armstrong29c0c042022-03-17 17:47:28 +01008457#endif /* MBEDTLS_USE_PSA_CRYPTO */
8458 }
8459#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
8460
8461 ((void) mac_dec);
8462 ((void) mac_enc);
8463
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008464end:
Gilles Peskine449bd832023-01-11 14:50:10 +01008465 mbedtls_platform_zeroize(keyblk, sizeof(keyblk));
8466 return ret;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008467}
8468
Valerio Settia08b1a42022-11-17 15:10:02 +01008469#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) && \
8470 defined(MBEDTLS_USE_PSA_CRYPTO)
Valerio Setti6b3dab02022-11-17 17:14:54 +01008471int mbedtls_psa_ecjpake_read_round(
Gilles Peskine449bd832023-01-11 14:50:10 +01008472 psa_pake_operation_t *pake_ctx,
8473 const unsigned char *buf,
8474 size_t len, mbedtls_ecjpake_rounds_t round)
Valerio Settia08b1a42022-11-17 15:10:02 +01008475{
8476 psa_status_t status;
8477 size_t input_offset = 0;
Valerio Setti819de862022-11-17 18:05:19 +01008478 /*
Valerio Setti6b3dab02022-11-17 17:14:54 +01008479 * At round one repeat the KEY_SHARE, ZK_PUBLIC & ZF_PROOF twice
8480 * At round two perform a single cycle
8481 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008482 unsigned int remaining_steps = (round == MBEDTLS_ECJPAKE_ROUND_ONE) ? 2 : 1;
Valerio Settia08b1a42022-11-17 15:10:02 +01008483
Gilles Peskine449bd832023-01-11 14:50:10 +01008484 for (; remaining_steps > 0; remaining_steps--) {
8485 for (psa_pake_step_t step = PSA_PAKE_STEP_KEY_SHARE;
Valerio Settia08b1a42022-11-17 15:10:02 +01008486 step <= PSA_PAKE_STEP_ZK_PROOF;
Gilles Peskine449bd832023-01-11 14:50:10 +01008487 ++step) {
Valerio Settia08b1a42022-11-17 15:10:02 +01008488 /* Length is stored at the first byte */
8489 size_t length = buf[input_offset];
8490 input_offset += 1;
8491
Gilles Peskine449bd832023-01-11 14:50:10 +01008492 if (input_offset + length > len) {
Valerio Settia08b1a42022-11-17 15:10:02 +01008493 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
8494 }
8495
Gilles Peskine449bd832023-01-11 14:50:10 +01008496 status = psa_pake_input(pake_ctx, step,
8497 buf + input_offset, length);
8498 if (status != PSA_SUCCESS) {
8499 return psa_ssl_status_to_mbedtls(status);
Valerio Settia08b1a42022-11-17 15:10:02 +01008500 }
8501
8502 input_offset += length;
8503 }
8504 }
8505
Gilles Peskine449bd832023-01-11 14:50:10 +01008506 if (input_offset != len) {
Valerio Setti61ea17d2022-11-18 12:11:00 +01008507 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01008508 }
Valerio Setti30ebe112022-11-17 16:23:34 +01008509
Gilles Peskine449bd832023-01-11 14:50:10 +01008510 return 0;
Valerio Settia08b1a42022-11-17 15:10:02 +01008511}
8512
Valerio Setti6b3dab02022-11-17 17:14:54 +01008513int mbedtls_psa_ecjpake_write_round(
Gilles Peskine449bd832023-01-11 14:50:10 +01008514 psa_pake_operation_t *pake_ctx,
8515 unsigned char *buf,
8516 size_t len, size_t *olen,
8517 mbedtls_ecjpake_rounds_t round)
Valerio Settia08b1a42022-11-17 15:10:02 +01008518{
8519 psa_status_t status;
8520 size_t output_offset = 0;
8521 size_t output_len;
Valerio Setti819de862022-11-17 18:05:19 +01008522 /*
Valerio Setti6b3dab02022-11-17 17:14:54 +01008523 * At round one repeat the KEY_SHARE, ZK_PUBLIC & ZF_PROOF twice
8524 * At round two perform a single cycle
8525 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008526 unsigned int remaining_steps = (round == MBEDTLS_ECJPAKE_ROUND_ONE) ? 2 : 1;
Valerio Settia08b1a42022-11-17 15:10:02 +01008527
Gilles Peskine449bd832023-01-11 14:50:10 +01008528 for (; remaining_steps > 0; remaining_steps--) {
8529 for (psa_pake_step_t step = PSA_PAKE_STEP_KEY_SHARE;
8530 step <= PSA_PAKE_STEP_ZK_PROOF;
8531 ++step) {
Valerio Setti79f6b6b2022-11-21 14:17:03 +01008532 /*
Valerio Settid4a9b1a2022-11-22 11:11:10 +01008533 * For each step, prepend 1 byte with the length of the data as
8534 * given by psa_pake_output().
Valerio Setti79f6b6b2022-11-21 14:17:03 +01008535 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008536 status = psa_pake_output(pake_ctx, step,
8537 buf + output_offset + 1,
8538 len - output_offset - 1,
8539 &output_len);
8540 if (status != PSA_SUCCESS) {
8541 return psa_ssl_status_to_mbedtls(status);
Valerio Settia08b1a42022-11-17 15:10:02 +01008542 }
8543
Valerio Setti99d88c12022-11-22 16:03:43 +01008544 *(buf + output_offset) = (uint8_t) output_len;
Valerio Setti79f6b6b2022-11-21 14:17:03 +01008545
8546 output_offset += output_len + 1;
Valerio Settia08b1a42022-11-17 15:10:02 +01008547 }
8548 }
8549
8550 *olen = output_offset;
8551
Gilles Peskine449bd832023-01-11 14:50:10 +01008552 return 0;
Valerio Settia08b1a42022-11-17 15:10:02 +01008553}
Valerio Settia08b1a42022-11-17 15:10:02 +01008554#endif //MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED && MBEDTLS_USE_PSA_CRYPTO
8555
Jerry Yuee40f9d2022-02-17 14:55:16 +08008556#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008557int mbedtls_ssl_get_key_exchange_md_tls1_2(mbedtls_ssl_context *ssl,
8558 unsigned char *hash, size_t *hashlen,
8559 unsigned char *data, size_t data_len,
8560 mbedtls_md_type_t md_alg)
Jerry Yuee40f9d2022-02-17 14:55:16 +08008561{
8562 psa_status_t status;
8563 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01008564 psa_algorithm_t hash_alg = mbedtls_hash_info_psa_from_md(md_alg);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008565
Gilles Peskine449bd832023-01-11 14:50:10 +01008566 MBEDTLS_SSL_DEBUG_MSG(3, ("Perform PSA-based computation of digest of ServerKeyExchange"));
Jerry Yuee40f9d2022-02-17 14:55:16 +08008567
Gilles Peskine449bd832023-01-11 14:50:10 +01008568 if ((status = psa_hash_setup(&hash_operation,
8569 hash_alg)) != PSA_SUCCESS) {
8570 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_setup", status);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008571 goto exit;
8572 }
8573
Gilles Peskine449bd832023-01-11 14:50:10 +01008574 if ((status = psa_hash_update(&hash_operation, ssl->handshake->randbytes,
8575 64)) != PSA_SUCCESS) {
8576 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_update", status);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008577 goto exit;
8578 }
8579
Gilles Peskine449bd832023-01-11 14:50:10 +01008580 if ((status = psa_hash_update(&hash_operation,
8581 data, data_len)) != PSA_SUCCESS) {
8582 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_update", status);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008583 goto exit;
8584 }
8585
Gilles Peskine449bd832023-01-11 14:50:10 +01008586 if ((status = psa_hash_finish(&hash_operation, hash, PSA_HASH_MAX_SIZE,
8587 hashlen)) != PSA_SUCCESS) {
8588 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_finish", status);
8589 goto exit;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008590 }
8591
8592exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008593 if (status != PSA_SUCCESS) {
8594 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8595 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
8596 switch (status) {
Jerry Yuee40f9d2022-02-17 14:55:16 +08008597 case PSA_ERROR_NOT_SUPPORTED:
Gilles Peskine449bd832023-01-11 14:50:10 +01008598 return MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008599 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
8600 case PSA_ERROR_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +01008601 return MBEDTLS_ERR_MD_BAD_INPUT_DATA;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008602 case PSA_ERROR_INSUFFICIENT_MEMORY:
Gilles Peskine449bd832023-01-11 14:50:10 +01008603 return MBEDTLS_ERR_MD_ALLOC_FAILED;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008604 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01008605 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008606 }
8607 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008608 return 0;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008609}
8610
8611#else
8612
Gilles Peskine449bd832023-01-11 14:50:10 +01008613int mbedtls_ssl_get_key_exchange_md_tls1_2(mbedtls_ssl_context *ssl,
8614 unsigned char *hash, size_t *hashlen,
8615 unsigned char *data, size_t data_len,
8616 mbedtls_md_type_t md_alg)
Jerry Yuee40f9d2022-02-17 14:55:16 +08008617{
8618 int ret = 0;
8619 mbedtls_md_context_t ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +01008620 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type(md_alg);
8621 *hashlen = mbedtls_md_get_size(md_info);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008622
Gilles Peskine449bd832023-01-11 14:50:10 +01008623 MBEDTLS_SSL_DEBUG_MSG(3, ("Perform mbedtls-based computation of digest of ServerKeyExchange"));
Jerry Yuee40f9d2022-02-17 14:55:16 +08008624
Gilles Peskine449bd832023-01-11 14:50:10 +01008625 mbedtls_md_init(&ctx);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008626
8627 /*
8628 * digitally-signed struct {
8629 * opaque client_random[32];
8630 * opaque server_random[32];
8631 * ServerDHParams params;
8632 * };
8633 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008634 if ((ret = mbedtls_md_setup(&ctx, md_info, 0)) != 0) {
8635 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_setup", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008636 goto exit;
8637 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008638 if ((ret = mbedtls_md_starts(&ctx)) != 0) {
8639 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_starts", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008640 goto exit;
8641 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008642 if ((ret = mbedtls_md_update(&ctx, ssl->handshake->randbytes, 64)) != 0) {
8643 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_update", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008644 goto exit;
8645 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008646 if ((ret = mbedtls_md_update(&ctx, data, data_len)) != 0) {
8647 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_update", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008648 goto exit;
8649 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008650 if ((ret = mbedtls_md_finish(&ctx, hash)) != 0) {
8651 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_finish", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008652 goto exit;
8653 }
8654
8655exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008656 mbedtls_md_free(&ctx);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008657
Gilles Peskine449bd832023-01-11 14:50:10 +01008658 if (ret != 0) {
8659 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8660 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
8661 }
Jerry Yuee40f9d2022-02-17 14:55:16 +08008662
Gilles Peskine449bd832023-01-11 14:50:10 +01008663 return ret;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008664}
8665#endif /* MBEDTLS_USE_PSA_CRYPTO */
8666
Jerry Yud9d91da2022-02-17 14:57:06 +08008667#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
8668
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008669/* Find the preferred hash for a given signature algorithm. */
8670unsigned int mbedtls_ssl_tls12_get_preferred_hash_for_sig_alg(
Gilles Peskine449bd832023-01-11 14:50:10 +01008671 mbedtls_ssl_context *ssl,
8672 unsigned int sig_alg)
Jerry Yud9d91da2022-02-17 14:57:06 +08008673{
Gabor Mezei078e8032022-04-27 21:17:56 +02008674 unsigned int i;
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008675 uint16_t *received_sig_algs = ssl->handshake->received_sig_algs;
Gabor Mezei078e8032022-04-27 21:17:56 +02008676
Gilles Peskine449bd832023-01-11 14:50:10 +01008677 if (sig_alg == MBEDTLS_SSL_SIG_ANON) {
8678 return MBEDTLS_SSL_HASH_NONE;
8679 }
Gabor Mezei078e8032022-04-27 21:17:56 +02008680
Gilles Peskine449bd832023-01-11 14:50:10 +01008681 for (i = 0; received_sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++) {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008682 unsigned int hash_alg_received =
Gilles Peskine449bd832023-01-11 14:50:10 +01008683 MBEDTLS_SSL_TLS12_HASH_ALG_FROM_SIG_AND_HASH_ALG(
8684 received_sig_algs[i]);
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008685 unsigned int sig_alg_received =
Gilles Peskine449bd832023-01-11 14:50:10 +01008686 MBEDTLS_SSL_TLS12_SIG_ALG_FROM_SIG_AND_HASH_ALG(
8687 received_sig_algs[i]);
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008688
Gilles Peskine449bd832023-01-11 14:50:10 +01008689 if (sig_alg == sig_alg_received) {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008690#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008691 if (ssl->handshake->key_cert && ssl->handshake->key_cert->key) {
Neil Armstrong96eceb82022-06-30 18:05:05 +02008692 psa_algorithm_t psa_hash_alg =
Gilles Peskine449bd832023-01-11 14:50:10 +01008693 mbedtls_hash_info_psa_from_md(hash_alg_received);
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008694
Gilles Peskine449bd832023-01-11 14:50:10 +01008695 if (sig_alg_received == MBEDTLS_SSL_SIG_ECDSA &&
8696 !mbedtls_pk_can_do_ext(ssl->handshake->key_cert->key,
8697 PSA_ALG_ECDSA(psa_hash_alg),
8698 PSA_KEY_USAGE_SIGN_HASH)) {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008699 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01008700 }
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008701
Gilles Peskine449bd832023-01-11 14:50:10 +01008702 if (sig_alg_received == MBEDTLS_SSL_SIG_RSA &&
8703 !mbedtls_pk_can_do_ext(ssl->handshake->key_cert->key,
8704 PSA_ALG_RSA_PKCS1V15_SIGN(
8705 psa_hash_alg),
8706 PSA_KEY_USAGE_SIGN_HASH)) {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008707 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01008708 }
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008709 }
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008710#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong96eceb82022-06-30 18:05:05 +02008711
Gilles Peskine449bd832023-01-11 14:50:10 +01008712 return hash_alg_received;
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008713 }
Jerry Yud9d91da2022-02-17 14:57:06 +08008714 }
Jerry Yud9d91da2022-02-17 14:57:06 +08008715
Gilles Peskine449bd832023-01-11 14:50:10 +01008716 return MBEDTLS_SSL_HASH_NONE;
Jerry Yud9d91da2022-02-17 14:57:06 +08008717}
8718
8719#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
8720
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008721/* Serialization of TLS 1.2 sessions:
8722 *
8723 * struct {
8724 * uint64 start_time;
8725 * uint8 ciphersuite[2]; // defined by the standard
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008726 * uint8 session_id_len; // at most 32
8727 * opaque session_id[32];
8728 * opaque master[48]; // fixed length in the standard
8729 * uint32 verify_result;
8730 * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert
8731 * opaque ticket<0..2^24-1>; // length 0 means no ticket
8732 * uint32 ticket_lifetime;
8733 * uint8 mfl_code; // up to 255 according to standard
8734 * uint8 encrypt_then_mac; // 0 or 1
8735 * } serialized_session_tls12;
8736 *
8737 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008738static size_t ssl_tls12_session_save(const mbedtls_ssl_session *session,
8739 unsigned char *buf,
8740 size_t buf_len)
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008741{
8742 unsigned char *p = buf;
8743 size_t used = 0;
8744
8745#if defined(MBEDTLS_HAVE_TIME)
8746 uint64_t start;
8747#endif
8748#if defined(MBEDTLS_X509_CRT_PARSE_C)
8749#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8750 size_t cert_len;
8751#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8752#endif /* MBEDTLS_X509_CRT_PARSE_C */
8753
8754 /*
8755 * Time
8756 */
8757#if defined(MBEDTLS_HAVE_TIME)
8758 used += 8;
8759
Gilles Peskine449bd832023-01-11 14:50:10 +01008760 if (used <= buf_len) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008761 start = (uint64_t) session->start;
8762
Gilles Peskine449bd832023-01-11 14:50:10 +01008763 MBEDTLS_PUT_UINT64_BE(start, p, 0);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008764 p += 8;
8765 }
8766#endif /* MBEDTLS_HAVE_TIME */
8767
8768 /*
8769 * Basic mandatory fields
8770 */
8771 used += 2 /* ciphersuite */
Gilles Peskine449bd832023-01-11 14:50:10 +01008772 + 1 /* id_len */
8773 + sizeof(session->id)
8774 + sizeof(session->master)
8775 + 4; /* verify_result */
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008776
Gilles Peskine449bd832023-01-11 14:50:10 +01008777 if (used <= buf_len) {
8778 MBEDTLS_PUT_UINT16_BE(session->ciphersuite, p, 0);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008779 p += 2;
8780
Gilles Peskine449bd832023-01-11 14:50:10 +01008781 *p++ = MBEDTLS_BYTE_0(session->id_len);
8782 memcpy(p, session->id, 32);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008783 p += 32;
8784
Gilles Peskine449bd832023-01-11 14:50:10 +01008785 memcpy(p, session->master, 48);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008786 p += 48;
8787
Gilles Peskine449bd832023-01-11 14:50:10 +01008788 MBEDTLS_PUT_UINT32_BE(session->verify_result, p, 0);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008789 p += 4;
8790 }
8791
8792 /*
8793 * Peer's end-entity certificate
8794 */
8795#if defined(MBEDTLS_X509_CRT_PARSE_C)
8796#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01008797 if (session->peer_cert == NULL) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008798 cert_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01008799 } else {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008800 cert_len = session->peer_cert->raw.len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008801 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008802
8803 used += 3 + cert_len;
8804
Gilles Peskine449bd832023-01-11 14:50:10 +01008805 if (used <= buf_len) {
8806 *p++ = MBEDTLS_BYTE_2(cert_len);
8807 *p++ = MBEDTLS_BYTE_1(cert_len);
8808 *p++ = MBEDTLS_BYTE_0(cert_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008809
Gilles Peskine449bd832023-01-11 14:50:10 +01008810 if (session->peer_cert != NULL) {
8811 memcpy(p, session->peer_cert->raw.p, cert_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008812 p += cert_len;
8813 }
8814 }
8815#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008816 if (session->peer_cert_digest != NULL) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008817 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008818 if (used <= buf_len) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008819 *p++ = (unsigned char) session->peer_cert_digest_type;
8820 *p++ = (unsigned char) session->peer_cert_digest_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008821 memcpy(p, session->peer_cert_digest,
8822 session->peer_cert_digest_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008823 p += session->peer_cert_digest_len;
8824 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008825 } else {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008826 used += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01008827 if (used <= buf_len) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008828 *p++ = (unsigned char) MBEDTLS_MD_NONE;
8829 *p++ = 0;
8830 }
8831 }
8832#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8833#endif /* MBEDTLS_X509_CRT_PARSE_C */
8834
8835 /*
8836 * Session ticket if any, plus associated data
8837 */
8838#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8839 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
8840
Gilles Peskine449bd832023-01-11 14:50:10 +01008841 if (used <= buf_len) {
8842 *p++ = MBEDTLS_BYTE_2(session->ticket_len);
8843 *p++ = MBEDTLS_BYTE_1(session->ticket_len);
8844 *p++ = MBEDTLS_BYTE_0(session->ticket_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008845
Gilles Peskine449bd832023-01-11 14:50:10 +01008846 if (session->ticket != NULL) {
8847 memcpy(p, session->ticket, session->ticket_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008848 p += session->ticket_len;
8849 }
8850
Gilles Peskine449bd832023-01-11 14:50:10 +01008851 MBEDTLS_PUT_UINT32_BE(session->ticket_lifetime, p, 0);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008852 p += 4;
8853 }
8854#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8855
8856 /*
8857 * Misc extension-related info
8858 */
8859#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8860 used += 1;
8861
Gilles Peskine449bd832023-01-11 14:50:10 +01008862 if (used <= buf_len) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008863 *p++ = session->mfl_code;
Gilles Peskine449bd832023-01-11 14:50:10 +01008864 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008865#endif
8866
8867#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
8868 used += 1;
8869
Gilles Peskine449bd832023-01-11 14:50:10 +01008870 if (used <= buf_len) {
8871 *p++ = MBEDTLS_BYTE_0(session->encrypt_then_mac);
8872 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008873#endif
8874
Gilles Peskine449bd832023-01-11 14:50:10 +01008875 return used;
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008876}
8877
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02008878MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01008879static int ssl_tls12_session_load(mbedtls_ssl_session *session,
8880 const unsigned char *buf,
8881 size_t len)
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008882{
8883#if defined(MBEDTLS_HAVE_TIME)
8884 uint64_t start;
8885#endif
8886#if defined(MBEDTLS_X509_CRT_PARSE_C)
8887#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8888 size_t cert_len;
8889#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8890#endif /* MBEDTLS_X509_CRT_PARSE_C */
8891
8892 const unsigned char *p = buf;
8893 const unsigned char * const end = buf + len;
8894
8895 /*
8896 * Time
8897 */
8898#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01008899 if (8 > (size_t) (end - p)) {
8900 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
8901 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008902
Gilles Peskine449bd832023-01-11 14:50:10 +01008903 start = ((uint64_t) p[0] << 56) |
8904 ((uint64_t) p[1] << 48) |
8905 ((uint64_t) p[2] << 40) |
8906 ((uint64_t) p[3] << 32) |
8907 ((uint64_t) p[4] << 24) |
8908 ((uint64_t) p[5] << 16) |
8909 ((uint64_t) p[6] << 8) |
8910 ((uint64_t) p[7]);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008911 p += 8;
8912
8913 session->start = (time_t) start;
8914#endif /* MBEDTLS_HAVE_TIME */
8915
8916 /*
8917 * Basic mandatory fields
8918 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008919 if (2 + 1 + 32 + 48 + 4 > (size_t) (end - p)) {
8920 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
8921 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008922
Gilles Peskine449bd832023-01-11 14:50:10 +01008923 session->ciphersuite = (p[0] << 8) | p[1];
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008924 p += 2;
8925
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008926 session->id_len = *p++;
Gilles Peskine449bd832023-01-11 14:50:10 +01008927 memcpy(session->id, p, 32);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008928 p += 32;
8929
Gilles Peskine449bd832023-01-11 14:50:10 +01008930 memcpy(session->master, p, 48);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008931 p += 48;
8932
Gilles Peskine449bd832023-01-11 14:50:10 +01008933 session->verify_result = ((uint32_t) p[0] << 24) |
8934 ((uint32_t) p[1] << 16) |
8935 ((uint32_t) p[2] << 8) |
8936 ((uint32_t) p[3]);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008937 p += 4;
8938
8939 /* Immediately clear invalid pointer values that have been read, in case
8940 * we exit early before we replaced them with valid ones. */
8941#if defined(MBEDTLS_X509_CRT_PARSE_C)
8942#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8943 session->peer_cert = NULL;
8944#else
8945 session->peer_cert_digest = NULL;
8946#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8947#endif /* MBEDTLS_X509_CRT_PARSE_C */
8948#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8949 session->ticket = NULL;
8950#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8951
8952 /*
8953 * Peer certificate
8954 */
8955#if defined(MBEDTLS_X509_CRT_PARSE_C)
8956#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8957 /* Deserialize CRT from the end of the ticket. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008958 if (3 > (size_t) (end - p)) {
8959 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
8960 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008961
Gilles Peskine449bd832023-01-11 14:50:10 +01008962 cert_len = (p[0] << 16) | (p[1] << 8) | p[2];
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008963 p += 3;
8964
Gilles Peskine449bd832023-01-11 14:50:10 +01008965 if (cert_len != 0) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008966 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
8967
Gilles Peskine449bd832023-01-11 14:50:10 +01008968 if (cert_len > (size_t) (end - p)) {
8969 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
8970 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008971
Gilles Peskine449bd832023-01-11 14:50:10 +01008972 session->peer_cert = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008973
Gilles Peskine449bd832023-01-11 14:50:10 +01008974 if (session->peer_cert == NULL) {
8975 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
8976 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008977
Gilles Peskine449bd832023-01-11 14:50:10 +01008978 mbedtls_x509_crt_init(session->peer_cert);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008979
Gilles Peskine449bd832023-01-11 14:50:10 +01008980 if ((ret = mbedtls_x509_crt_parse_der(session->peer_cert,
8981 p, cert_len)) != 0) {
8982 mbedtls_x509_crt_free(session->peer_cert);
8983 mbedtls_free(session->peer_cert);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008984 session->peer_cert = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01008985 return ret;
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008986 }
8987
8988 p += cert_len;
8989 }
8990#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8991 /* Deserialize CRT digest from the end of the ticket. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008992 if (2 > (size_t) (end - p)) {
8993 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
8994 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008995
8996 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
8997 session->peer_cert_digest_len = (size_t) *p++;
8998
Gilles Peskine449bd832023-01-11 14:50:10 +01008999 if (session->peer_cert_digest_len != 0) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009000 const mbedtls_md_info_t *md_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01009001 mbedtls_md_info_from_type(session->peer_cert_digest_type);
9002 if (md_info == NULL) {
9003 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9004 }
9005 if (session->peer_cert_digest_len != mbedtls_md_get_size(md_info)) {
9006 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9007 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009008
Gilles Peskine449bd832023-01-11 14:50:10 +01009009 if (session->peer_cert_digest_len > (size_t) (end - p)) {
9010 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9011 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009012
9013 session->peer_cert_digest =
Gilles Peskine449bd832023-01-11 14:50:10 +01009014 mbedtls_calloc(1, session->peer_cert_digest_len);
9015 if (session->peer_cert_digest == NULL) {
9016 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
9017 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009018
Gilles Peskine449bd832023-01-11 14:50:10 +01009019 memcpy(session->peer_cert_digest, p,
9020 session->peer_cert_digest_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009021 p += session->peer_cert_digest_len;
9022 }
9023#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9024#endif /* MBEDTLS_X509_CRT_PARSE_C */
9025
9026 /*
9027 * Session ticket and associated data
9028 */
9029#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01009030 if (3 > (size_t) (end - p)) {
9031 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9032 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009033
Gilles Peskine449bd832023-01-11 14:50:10 +01009034 session->ticket_len = (p[0] << 16) | (p[1] << 8) | p[2];
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009035 p += 3;
9036
Gilles Peskine449bd832023-01-11 14:50:10 +01009037 if (session->ticket_len != 0) {
9038 if (session->ticket_len > (size_t) (end - p)) {
9039 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9040 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009041
Gilles Peskine449bd832023-01-11 14:50:10 +01009042 session->ticket = mbedtls_calloc(1, session->ticket_len);
9043 if (session->ticket == NULL) {
9044 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
9045 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009046
Gilles Peskine449bd832023-01-11 14:50:10 +01009047 memcpy(session->ticket, p, session->ticket_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009048 p += session->ticket_len;
9049 }
9050
Gilles Peskine449bd832023-01-11 14:50:10 +01009051 if (4 > (size_t) (end - p)) {
9052 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9053 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009054
Gilles Peskine449bd832023-01-11 14:50:10 +01009055 session->ticket_lifetime = ((uint32_t) p[0] << 24) |
9056 ((uint32_t) p[1] << 16) |
9057 ((uint32_t) p[2] << 8) |
9058 ((uint32_t) p[3]);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009059 p += 4;
9060#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
9061
9062 /*
9063 * Misc extension-related info
9064 */
9065#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01009066 if (1 > (size_t) (end - p)) {
9067 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9068 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009069
9070 session->mfl_code = *p++;
9071#endif
9072
9073#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01009074 if (1 > (size_t) (end - p)) {
9075 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9076 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009077
9078 session->encrypt_then_mac = *p++;
9079#endif
9080
9081 /* Done, should have consumed entire buffer */
Gilles Peskine449bd832023-01-11 14:50:10 +01009082 if (p != end) {
9083 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9084 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009085
Gilles Peskine449bd832023-01-11 14:50:10 +01009086 return 0;
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009087}
Jerry Yudc7bd172022-02-17 13:44:15 +08009088#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9089
XiaokangQian75d40ef2022-04-20 11:05:24 +00009090int mbedtls_ssl_validate_ciphersuite(
9091 const mbedtls_ssl_context *ssl,
9092 const mbedtls_ssl_ciphersuite_t *suite_info,
9093 mbedtls_ssl_protocol_version min_tls_version,
Gilles Peskine449bd832023-01-11 14:50:10 +01009094 mbedtls_ssl_protocol_version max_tls_version)
XiaokangQian75d40ef2022-04-20 11:05:24 +00009095{
9096 (void) ssl;
9097
Gilles Peskine449bd832023-01-11 14:50:10 +01009098 if (suite_info == NULL) {
9099 return -1;
9100 }
XiaokangQian75d40ef2022-04-20 11:05:24 +00009101
Gilles Peskine449bd832023-01-11 14:50:10 +01009102 if ((suite_info->min_tls_version > max_tls_version) ||
9103 (suite_info->max_tls_version < min_tls_version)) {
9104 return -1;
XiaokangQian75d40ef2022-04-20 11:05:24 +00009105 }
9106
XiaokangQian060d8672022-04-21 09:24:56 +00009107#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_CLI_C)
XiaokangQian75d40ef2022-04-20 11:05:24 +00009108#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Neil Armstrongca7d5062022-05-31 14:43:23 +02009109#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01009110 if (suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
9111 ssl->handshake->psa_pake_ctx_is_ok != 1)
Neil Armstrongca7d5062022-05-31 14:43:23 +02009112#else
Gilles Peskine449bd832023-01-11 14:50:10 +01009113 if (suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
9114 mbedtls_ecjpake_check(&ssl->handshake->ecjpake_ctx) != 0)
Neil Armstrongca7d5062022-05-31 14:43:23 +02009115#endif /* MBEDTLS_USE_PSA_CRYPTO */
XiaokangQian75d40ef2022-04-20 11:05:24 +00009116 {
Gilles Peskine449bd832023-01-11 14:50:10 +01009117 return -1;
XiaokangQian75d40ef2022-04-20 11:05:24 +00009118 }
9119#endif
9120
9121 /* Don't suggest PSK-based ciphersuite if no PSK is available. */
9122#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01009123 if (mbedtls_ssl_ciphersuite_uses_psk(suite_info) &&
9124 mbedtls_ssl_conf_has_static_psk(ssl->conf) == 0) {
9125 return -1;
XiaokangQian75d40ef2022-04-20 11:05:24 +00009126 }
9127#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
9128#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9129
Gilles Peskine449bd832023-01-11 14:50:10 +01009130 return 0;
XiaokangQian75d40ef2022-04-20 11:05:24 +00009131}
9132
Ronald Crone68ab4f2022-10-05 12:46:29 +02009133#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
XiaokangQianeaf36512022-04-24 09:07:44 +00009134/*
9135 * Function for writing a signature algorithm extension.
9136 *
9137 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
9138 * value (TLS 1.3 RFC8446):
9139 * enum {
9140 * ....
9141 * ecdsa_secp256r1_sha256( 0x0403 ),
9142 * ecdsa_secp384r1_sha384( 0x0503 ),
9143 * ecdsa_secp521r1_sha512( 0x0603 ),
9144 * ....
9145 * } SignatureScheme;
9146 *
9147 * struct {
9148 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
9149 * } SignatureSchemeList;
9150 *
9151 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
9152 * value (TLS 1.2 RFC5246):
9153 * enum {
9154 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
9155 * sha512(6), (255)
9156 * } HashAlgorithm;
9157 *
9158 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
9159 * SignatureAlgorithm;
9160 *
9161 * struct {
9162 * HashAlgorithm hash;
9163 * SignatureAlgorithm signature;
9164 * } SignatureAndHashAlgorithm;
9165 *
9166 * SignatureAndHashAlgorithm
9167 * supported_signature_algorithms<2..2^16-2>;
9168 *
9169 * The TLS 1.3 signature algorithm extension was defined to be a compatible
9170 * generalization of the TLS 1.2 signature algorithm extension.
9171 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
9172 * `SignatureScheme` field of TLS 1.3
9173 *
9174 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009175int mbedtls_ssl_write_sig_alg_ext(mbedtls_ssl_context *ssl, unsigned char *buf,
9176 const unsigned char *end, size_t *out_len)
XiaokangQianeaf36512022-04-24 09:07:44 +00009177{
9178 unsigned char *p = buf;
9179 unsigned char *supported_sig_alg; /* Start of supported_signature_algorithms */
9180 size_t supported_sig_alg_len = 0; /* Length of supported_signature_algorithms */
9181
9182 *out_len = 0;
9183
Gilles Peskine449bd832023-01-11 14:50:10 +01009184 MBEDTLS_SSL_DEBUG_MSG(3, ("adding signature_algorithms extension"));
XiaokangQianeaf36512022-04-24 09:07:44 +00009185
9186 /* Check if we have space for header and length field:
9187 * - extension_type (2 bytes)
9188 * - extension_data_length (2 bytes)
9189 * - supported_signature_algorithms_length (2 bytes)
9190 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009191 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 6);
XiaokangQianeaf36512022-04-24 09:07:44 +00009192 p += 6;
9193
9194 /*
9195 * Write supported_signature_algorithms
9196 */
9197 supported_sig_alg = p;
Gilles Peskine449bd832023-01-11 14:50:10 +01009198 const uint16_t *sig_alg = mbedtls_ssl_get_sig_algs(ssl);
9199 if (sig_alg == NULL) {
9200 return MBEDTLS_ERR_SSL_BAD_CONFIG;
9201 }
XiaokangQianeaf36512022-04-24 09:07:44 +00009202
Gilles Peskine449bd832023-01-11 14:50:10 +01009203 for (; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++) {
9204 MBEDTLS_SSL_DEBUG_MSG(3, ("got signature scheme [%x] %s",
9205 *sig_alg,
9206 mbedtls_ssl_sig_alg_to_str(*sig_alg)));
9207 if (!mbedtls_ssl_sig_alg_is_supported(ssl, *sig_alg)) {
XiaokangQianeaf36512022-04-24 09:07:44 +00009208 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01009209 }
9210 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
9211 MBEDTLS_PUT_UINT16_BE(*sig_alg, p, 0);
XiaokangQianeaf36512022-04-24 09:07:44 +00009212 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01009213 MBEDTLS_SSL_DEBUG_MSG(3, ("sent signature scheme [%x] %s",
9214 *sig_alg,
9215 mbedtls_ssl_sig_alg_to_str(*sig_alg)));
XiaokangQianeaf36512022-04-24 09:07:44 +00009216 }
9217
9218 /* Length of supported_signature_algorithms */
9219 supported_sig_alg_len = p - supported_sig_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01009220 if (supported_sig_alg_len == 0) {
9221 MBEDTLS_SSL_DEBUG_MSG(1, ("No signature algorithms defined."));
9222 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
XiaokangQianeaf36512022-04-24 09:07:44 +00009223 }
9224
Gilles Peskine449bd832023-01-11 14:50:10 +01009225 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SIG_ALG, buf, 0);
9226 MBEDTLS_PUT_UINT16_BE(supported_sig_alg_len + 2, buf, 2);
9227 MBEDTLS_PUT_UINT16_BE(supported_sig_alg_len, buf, 4);
XiaokangQianeaf36512022-04-24 09:07:44 +00009228
XiaokangQianeaf36512022-04-24 09:07:44 +00009229 *out_len = p - buf;
9230
9231#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01009232 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_SIG_ALG);
XiaokangQianeaf36512022-04-24 09:07:44 +00009233#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu0c354a22022-08-29 15:25:36 +08009234
Gilles Peskine449bd832023-01-11 14:50:10 +01009235 return 0;
XiaokangQianeaf36512022-04-24 09:07:44 +00009236}
Ronald Crone68ab4f2022-10-05 12:46:29 +02009237#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
XiaokangQianeaf36512022-04-24 09:07:44 +00009238
XiaokangQian40a35232022-05-07 09:02:40 +00009239#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
XiaokangQian9b2b7712022-05-17 02:57:00 +00009240/*
9241 * mbedtls_ssl_parse_server_name_ext
9242 *
9243 * Structure of server_name extension:
9244 *
9245 * enum {
9246 * host_name(0), (255)
9247 * } NameType;
9248 * opaque HostName<1..2^16-1>;
9249 *
9250 * struct {
9251 * NameType name_type;
9252 * select (name_type) {
9253 * case host_name: HostName;
9254 * } name;
9255 * } ServerName;
9256 * struct {
9257 * ServerName server_name_list<1..2^16-1>
9258 * } ServerNameList;
9259 */
Ronald Cronce7d76e2022-07-08 18:56:49 +02009260MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01009261int mbedtls_ssl_parse_server_name_ext(mbedtls_ssl_context *ssl,
9262 const unsigned char *buf,
9263 const unsigned char *end)
XiaokangQian40a35232022-05-07 09:02:40 +00009264{
9265 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
9266 const unsigned char *p = buf;
XiaokangQian9b2b7712022-05-17 02:57:00 +00009267 size_t server_name_list_len, hostname_len;
9268 const unsigned char *server_name_list_end;
XiaokangQian40a35232022-05-07 09:02:40 +00009269
Gilles Peskine449bd832023-01-11 14:50:10 +01009270 MBEDTLS_SSL_DEBUG_MSG(3, ("parse ServerName extension"));
XiaokangQian40a35232022-05-07 09:02:40 +00009271
Gilles Peskine449bd832023-01-11 14:50:10 +01009272 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
9273 server_name_list_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian40a35232022-05-07 09:02:40 +00009274 p += 2;
9275
Gilles Peskine449bd832023-01-11 14:50:10 +01009276 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, server_name_list_len);
XiaokangQian9b2b7712022-05-17 02:57:00 +00009277 server_name_list_end = p + server_name_list_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01009278 while (p < server_name_list_end) {
9279 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, server_name_list_end, 3);
9280 hostname_len = MBEDTLS_GET_UINT16_BE(p, 1);
9281 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, server_name_list_end,
9282 hostname_len + 3);
XiaokangQian40a35232022-05-07 09:02:40 +00009283
Gilles Peskine449bd832023-01-11 14:50:10 +01009284 if (p[0] == MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME) {
XiaokangQian75fe8c72022-06-15 09:42:45 +00009285 /* sni_name is intended to be used only during the parsing of the
9286 * ClientHello message (it is reset to NULL before the end of
9287 * the message parsing). Thus it is ok to just point to the
9288 * reception buffer and not make a copy of it.
9289 */
XiaokangQianf2a94202022-05-20 06:44:24 +00009290 ssl->handshake->sni_name = p + 3;
9291 ssl->handshake->sni_name_len = hostname_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01009292 if (ssl->conf->f_sni == NULL) {
9293 return 0;
XiaokangQian40a35232022-05-07 09:02:40 +00009294 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009295 ret = ssl->conf->f_sni(ssl->conf->p_sni,
9296 ssl, p + 3, hostname_len);
9297 if (ret != 0) {
9298 MBEDTLS_SSL_DEBUG_RET(1, "ssl_sni_wrapper", ret);
9299 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_UNRECOGNIZED_NAME,
9300 MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME);
9301 return MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME;
9302 }
9303 return 0;
XiaokangQian40a35232022-05-07 09:02:40 +00009304 }
9305
9306 p += hostname_len + 3;
9307 }
9308
Gilles Peskine449bd832023-01-11 14:50:10 +01009309 return 0;
XiaokangQian40a35232022-05-07 09:02:40 +00009310}
9311#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
9312
XiaokangQianacb39922022-06-17 10:18:48 +00009313#if defined(MBEDTLS_SSL_ALPN)
Ronald Cronce7d76e2022-07-08 18:56:49 +02009314MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01009315int mbedtls_ssl_parse_alpn_ext(mbedtls_ssl_context *ssl,
9316 const unsigned char *buf,
9317 const unsigned char *end)
XiaokangQianacb39922022-06-17 10:18:48 +00009318{
9319 const unsigned char *p = buf;
XiaokangQianc7403452022-06-23 03:24:12 +00009320 size_t protocol_name_list_len;
XiaokangQian95d5f542022-06-24 02:29:26 +00009321 const unsigned char *protocol_name_list;
9322 const unsigned char *protocol_name_list_end;
XiaokangQianc7403452022-06-23 03:24:12 +00009323 size_t protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009324
9325 /* If ALPN not configured, just ignore the extension */
Gilles Peskine449bd832023-01-11 14:50:10 +01009326 if (ssl->conf->alpn_list == NULL) {
9327 return 0;
9328 }
XiaokangQianacb39922022-06-17 10:18:48 +00009329
9330 /*
XiaokangQianc7403452022-06-23 03:24:12 +00009331 * RFC7301, section 3.1
9332 * opaque ProtocolName<1..2^8-1>;
XiaokangQianacb39922022-06-17 10:18:48 +00009333 *
XiaokangQianc7403452022-06-23 03:24:12 +00009334 * struct {
9335 * ProtocolName protocol_name_list<2..2^16-1>
9336 * } ProtocolNameList;
XiaokangQianacb39922022-06-17 10:18:48 +00009337 */
9338
XiaokangQianc7403452022-06-23 03:24:12 +00009339 /*
XiaokangQian0b776e22022-06-24 09:04:59 +00009340 * protocol_name_list_len 2 bytes
9341 * protocol_name_len 1 bytes
9342 * protocol_name >=1 byte
XiaokangQianc7403452022-06-23 03:24:12 +00009343 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009344 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 4);
XiaokangQianacb39922022-06-17 10:18:48 +00009345
Gilles Peskine449bd832023-01-11 14:50:10 +01009346 protocol_name_list_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQianacb39922022-06-17 10:18:48 +00009347 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01009348 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, protocol_name_list_len);
XiaokangQian95d5f542022-06-24 02:29:26 +00009349 protocol_name_list = p;
9350 protocol_name_list_end = p + protocol_name_list_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009351
9352 /* Validate peer's list (lengths) */
Gilles Peskine449bd832023-01-11 14:50:10 +01009353 while (p < protocol_name_list_end) {
XiaokangQian95d5f542022-06-24 02:29:26 +00009354 protocol_name_len = *p++;
Gilles Peskine449bd832023-01-11 14:50:10 +01009355 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, protocol_name_list_end,
9356 protocol_name_len);
9357 if (protocol_name_len == 0) {
XiaokangQian95d5f542022-06-24 02:29:26 +00009358 MBEDTLS_SSL_PEND_FATAL_ALERT(
9359 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
Gilles Peskine449bd832023-01-11 14:50:10 +01009360 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
9361 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
XiaokangQian95d5f542022-06-24 02:29:26 +00009362 }
9363
9364 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009365 }
9366
9367 /* Use our order of preference */
Gilles Peskine449bd832023-01-11 14:50:10 +01009368 for (const char **alpn = ssl->conf->alpn_list; *alpn != NULL; alpn++) {
9369 size_t const alpn_len = strlen(*alpn);
XiaokangQian95d5f542022-06-24 02:29:26 +00009370 p = protocol_name_list;
Gilles Peskine449bd832023-01-11 14:50:10 +01009371 while (p < protocol_name_list_end) {
XiaokangQian95d5f542022-06-24 02:29:26 +00009372 protocol_name_len = *p++;
Gilles Peskine449bd832023-01-11 14:50:10 +01009373 if (protocol_name_len == alpn_len &&
9374 memcmp(p, *alpn, alpn_len) == 0) {
XiaokangQianacb39922022-06-17 10:18:48 +00009375 ssl->alpn_chosen = *alpn;
Gilles Peskine449bd832023-01-11 14:50:10 +01009376 return 0;
XiaokangQianacb39922022-06-17 10:18:48 +00009377 }
XiaokangQian95d5f542022-06-24 02:29:26 +00009378
9379 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009380 }
9381 }
9382
XiaokangQian95d5f542022-06-24 02:29:26 +00009383 /* If we get here, no match was found */
XiaokangQianacb39922022-06-17 10:18:48 +00009384 MBEDTLS_SSL_PEND_FATAL_ALERT(
Gilles Peskine449bd832023-01-11 14:50:10 +01009385 MBEDTLS_SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL,
9386 MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL);
9387 return MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL;
XiaokangQianacb39922022-06-17 10:18:48 +00009388}
9389
Gilles Peskine449bd832023-01-11 14:50:10 +01009390int mbedtls_ssl_write_alpn_ext(mbedtls_ssl_context *ssl,
9391 unsigned char *buf,
9392 unsigned char *end,
9393 size_t *out_len)
XiaokangQianacb39922022-06-17 10:18:48 +00009394{
9395 unsigned char *p = buf;
XiaokangQian95d5f542022-06-24 02:29:26 +00009396 size_t protocol_name_len;
XiaokangQianc7403452022-06-23 03:24:12 +00009397 *out_len = 0;
XiaokangQianacb39922022-06-17 10:18:48 +00009398
Gilles Peskine449bd832023-01-11 14:50:10 +01009399 if (ssl->alpn_chosen == NULL) {
9400 return 0;
XiaokangQianacb39922022-06-17 10:18:48 +00009401 }
9402
Gilles Peskine449bd832023-01-11 14:50:10 +01009403 protocol_name_len = strlen(ssl->alpn_chosen);
9404 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 7 + protocol_name_len);
XiaokangQianacb39922022-06-17 10:18:48 +00009405
Gilles Peskine449bd832023-01-11 14:50:10 +01009406 MBEDTLS_SSL_DEBUG_MSG(3, ("server side, adding alpn extension"));
XiaokangQianacb39922022-06-17 10:18:48 +00009407 /*
9408 * 0 . 1 ext identifier
9409 * 2 . 3 ext length
9410 * 4 . 5 protocol list length
9411 * 6 . 6 protocol name length
9412 * 7 . 7+n protocol name
9413 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009414 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_ALPN, p, 0);
XiaokangQianacb39922022-06-17 10:18:48 +00009415
XiaokangQian95d5f542022-06-24 02:29:26 +00009416 *out_len = 7 + protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009417
Gilles Peskine449bd832023-01-11 14:50:10 +01009418 MBEDTLS_PUT_UINT16_BE(protocol_name_len + 3, p, 2);
9419 MBEDTLS_PUT_UINT16_BE(protocol_name_len + 1, p, 4);
XiaokangQian0b776e22022-06-24 09:04:59 +00009420 /* Note: the length of the chosen protocol has been checked to be less
9421 * than 255 bytes in `mbedtls_ssl_conf_alpn_protocols`.
9422 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009423 p[6] = MBEDTLS_BYTE_0(protocol_name_len);
XiaokangQianacb39922022-06-17 10:18:48 +00009424
Gilles Peskine449bd832023-01-11 14:50:10 +01009425 memcpy(p + 7, ssl->alpn_chosen, protocol_name_len);
Jerry Yub95dd362022-11-08 21:19:34 +08009426
9427#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01009428 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_ALPN);
Jerry Yub95dd362022-11-08 21:19:34 +08009429#endif
9430
Gilles Peskine449bd832023-01-11 14:50:10 +01009431 return 0;
XiaokangQianacb39922022-06-17 10:18:48 +00009432}
9433#endif /* MBEDTLS_SSL_ALPN */
9434
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009435#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
Xiaokang Qian03409292022-10-12 02:49:52 +00009436 defined(MBEDTLS_SSL_SESSION_TICKETS) && \
Xiaokang Qianed0620c2022-10-12 06:58:13 +00009437 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \
Xiaokang Qianed3afcd2022-10-12 08:31:11 +00009438 defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01009439int mbedtls_ssl_session_set_hostname(mbedtls_ssl_session *session,
9440 const char *hostname)
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009441{
9442 /* Initialize to suppress unnecessary compiler warning */
9443 size_t hostname_len = 0;
9444
9445 /* Check if new hostname is valid before
9446 * making any change to current one */
Gilles Peskine449bd832023-01-11 14:50:10 +01009447 if (hostname != NULL) {
9448 hostname_len = strlen(hostname);
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009449
Gilles Peskine449bd832023-01-11 14:50:10 +01009450 if (hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN) {
9451 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9452 }
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009453 }
9454
9455 /* Now it's clear that we will overwrite the old hostname,
9456 * so we can free it safely */
Gilles Peskine449bd832023-01-11 14:50:10 +01009457 if (session->hostname != NULL) {
9458 mbedtls_platform_zeroize(session->hostname,
9459 strlen(session->hostname));
9460 mbedtls_free(session->hostname);
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009461 }
9462
9463 /* Passing NULL as hostname shall clear the old one */
Gilles Peskine449bd832023-01-11 14:50:10 +01009464 if (hostname == NULL) {
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009465 session->hostname = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01009466 } else {
9467 session->hostname = mbedtls_calloc(1, hostname_len + 1);
9468 if (session->hostname == NULL) {
9469 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
9470 }
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009471
Gilles Peskine449bd832023-01-11 14:50:10 +01009472 memcpy(session->hostname, hostname, hostname_len);
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009473 }
9474
Gilles Peskine449bd832023-01-11 14:50:10 +01009475 return 0;
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009476}
Xiaokang Qian03409292022-10-12 02:49:52 +00009477#endif /* MBEDTLS_SSL_PROTO_TLS1_3 &&
9478 MBEDTLS_SSL_SESSION_TICKETS &&
Xiaokang Qianed0620c2022-10-12 06:58:13 +00009479 MBEDTLS_SSL_SERVER_NAME_INDICATION &&
Xiaokang Qianed3afcd2022-10-12 08:31:11 +00009480 MBEDTLS_SSL_CLI_C */
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009481
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009482#endif /* MBEDTLS_SSL_TLS_C */