blob: e8f1750122e5b8fe5c719094908730c6a3ac93d9 [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
Andrzej Kurek8a045ce2022-12-23 11:00:06 -050055#if defined(MBEDTLS_USE_PSA_CRYPTO)
56#define PSA_TO_MBEDTLS_ERR(status) PSA_TO_MBEDTLS_ERR_LIST(status, \
57 psa_to_ssl_errors, \
58 psa_generic_status_to_mbedtls)
59#endif
60
Ronald Cronad8c17b2022-06-10 17:18:09 +020061#if defined(MBEDTLS_TEST_HOOKS)
62static mbedtls_ssl_chk_buf_ptr_args chk_buf_ptr_fail_args;
63
64void mbedtls_ssl_set_chk_buf_ptr_fail_args(
Gilles Peskine449bd832023-01-11 14:50:10 +010065 const uint8_t *cur, const uint8_t *end, size_t need)
Ronald Cronad8c17b2022-06-10 17:18:09 +020066{
67 chk_buf_ptr_fail_args.cur = cur;
68 chk_buf_ptr_fail_args.end = end;
69 chk_buf_ptr_fail_args.need = need;
70}
71
Gilles Peskine449bd832023-01-11 14:50:10 +010072void mbedtls_ssl_reset_chk_buf_ptr_fail_args(void)
Ronald Cronad8c17b2022-06-10 17:18:09 +020073{
Gilles Peskine449bd832023-01-11 14:50:10 +010074 memset(&chk_buf_ptr_fail_args, 0, sizeof(chk_buf_ptr_fail_args));
Ronald Cronad8c17b2022-06-10 17:18:09 +020075}
76
Gilles Peskine449bd832023-01-11 14:50:10 +010077int mbedtls_ssl_cmp_chk_buf_ptr_fail_args(mbedtls_ssl_chk_buf_ptr_args *args)
Ronald Cronad8c17b2022-06-10 17:18:09 +020078{
Gilles Peskine449bd832023-01-11 14:50:10 +010079 return (chk_buf_ptr_fail_args.cur != args->cur) ||
80 (chk_buf_ptr_fail_args.end != args->end) ||
81 (chk_buf_ptr_fail_args.need != args->need);
Ronald Cronad8c17b2022-06-10 17:18:09 +020082}
83#endif /* MBEDTLS_TEST_HOOKS */
84
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +020085#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker2b1e3542018-08-06 11:19:13 +010086
Hanno Beckera0e20d02019-05-15 14:03:01 +010087#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Hanno Beckerf8542cf2019-04-09 15:22:03 +010088/* Top-level Connection ID API */
89
Gilles Peskine449bd832023-01-11 14:50:10 +010090int mbedtls_ssl_conf_cid(mbedtls_ssl_config *conf,
91 size_t len,
92 int ignore_other_cid)
Hanno Beckerad4a1372019-05-03 13:06:44 +010093{
Gilles Peskine449bd832023-01-11 14:50:10 +010094 if (len > MBEDTLS_SSL_CID_IN_LEN_MAX) {
95 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
96 }
Hanno Beckerad4a1372019-05-03 13:06:44 +010097
Gilles Peskine449bd832023-01-11 14:50:10 +010098 if (ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_FAIL &&
99 ignore_other_cid != MBEDTLS_SSL_UNEXPECTED_CID_IGNORE) {
100 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Becker611ac772019-05-14 11:45:26 +0100101 }
102
103 conf->ignore_unexpected_cid = ignore_other_cid;
Hanno Beckerad4a1372019-05-03 13:06:44 +0100104 conf->cid_len = len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100105 return 0;
Hanno Beckerad4a1372019-05-03 13:06:44 +0100106}
107
Gilles Peskine449bd832023-01-11 14:50:10 +0100108int mbedtls_ssl_set_cid(mbedtls_ssl_context *ssl,
109 int enable,
110 unsigned char const *own_cid,
111 size_t own_cid_len)
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100112{
Gilles Peskine449bd832023-01-11 14:50:10 +0100113 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
114 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
115 }
Hanno Becker76a79ab2019-05-03 14:38:32 +0100116
Hanno Beckerca092242019-04-25 16:01:49 +0100117 ssl->negotiate_cid = enable;
Gilles Peskine449bd832023-01-11 14:50:10 +0100118 if (enable == MBEDTLS_SSL_CID_DISABLED) {
119 MBEDTLS_SSL_DEBUG_MSG(3, ("Disable use of CID extension."));
120 return 0;
Hanno Beckerca092242019-04-25 16:01:49 +0100121 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100122 MBEDTLS_SSL_DEBUG_MSG(3, ("Enable use of CID extension."));
123 MBEDTLS_SSL_DEBUG_BUF(3, "Own CID", own_cid, own_cid_len);
Hanno Beckerca092242019-04-25 16:01:49 +0100124
Gilles Peskine449bd832023-01-11 14:50:10 +0100125 if (own_cid_len != ssl->conf->cid_len) {
126 MBEDTLS_SSL_DEBUG_MSG(3, ("CID length %u does not match CID length %u in config",
127 (unsigned) own_cid_len,
128 (unsigned) ssl->conf->cid_len));
129 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Beckerca092242019-04-25 16:01:49 +0100130 }
131
Gilles Peskine449bd832023-01-11 14:50:10 +0100132 memcpy(ssl->own_cid, own_cid, own_cid_len);
Hanno Beckerb7ee0cf2019-04-30 14:07:31 +0100133 /* Truncation is not an issue here because
134 * MBEDTLS_SSL_CID_IN_LEN_MAX at most 255. */
135 ssl->own_cid_len = (uint8_t) own_cid_len;
Hanno Beckerca092242019-04-25 16:01:49 +0100136
Gilles Peskine449bd832023-01-11 14:50:10 +0100137 return 0;
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100138}
139
Gilles Peskine449bd832023-01-11 14:50:10 +0100140int mbedtls_ssl_get_own_cid(mbedtls_ssl_context *ssl,
141 int *enabled,
142 unsigned char own_cid[MBEDTLS_SSL_CID_OUT_LEN_MAX],
143 size_t *own_cid_len)
Paul Elliott0113cf12022-03-11 20:26:47 +0000144{
145 *enabled = MBEDTLS_SSL_CID_DISABLED;
146
Gilles Peskine449bd832023-01-11 14:50:10 +0100147 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
148 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
149 }
Paul Elliott0113cf12022-03-11 20:26:47 +0000150
151 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID length is
152 * zero as this is indistinguishable from not requesting to use
153 * the CID extension. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100154 if (ssl->own_cid_len == 0 || ssl->negotiate_cid == MBEDTLS_SSL_CID_DISABLED) {
155 return 0;
156 }
Paul Elliott0113cf12022-03-11 20:26:47 +0000157
Gilles Peskine449bd832023-01-11 14:50:10 +0100158 if (own_cid_len != NULL) {
Paul Elliott0113cf12022-03-11 20:26:47 +0000159 *own_cid_len = ssl->own_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100160 if (own_cid != NULL) {
161 memcpy(own_cid, ssl->own_cid, ssl->own_cid_len);
162 }
Paul Elliott0113cf12022-03-11 20:26:47 +0000163 }
164
165 *enabled = MBEDTLS_SSL_CID_ENABLED;
166
Gilles Peskine449bd832023-01-11 14:50:10 +0100167 return 0;
Paul Elliott0113cf12022-03-11 20:26:47 +0000168}
169
Gilles Peskine449bd832023-01-11 14:50:10 +0100170int mbedtls_ssl_get_peer_cid(mbedtls_ssl_context *ssl,
171 int *enabled,
172 unsigned char peer_cid[MBEDTLS_SSL_CID_OUT_LEN_MAX],
173 size_t *peer_cid_len)
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100174{
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100175 *enabled = MBEDTLS_SSL_CID_DISABLED;
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100176
Gilles Peskine449bd832023-01-11 14:50:10 +0100177 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
178 mbedtls_ssl_is_handshake_over(ssl) == 0) {
179 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Becker76a79ab2019-05-03 14:38:32 +0100180 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100181
Hanno Beckerc5f24222019-05-03 12:54:52 +0100182 /* We report MBEDTLS_SSL_CID_DISABLED in case the CID extensions
183 * were used, but client and server requested the empty CID.
184 * This is indistinguishable from not using the CID extension
185 * in the first place. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100186 if (ssl->transform_in->in_cid_len == 0 &&
187 ssl->transform_in->out_cid_len == 0) {
188 return 0;
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100189 }
190
Gilles Peskine449bd832023-01-11 14:50:10 +0100191 if (peer_cid_len != NULL) {
Hanno Becker615ef172019-05-22 16:50:35 +0100192 *peer_cid_len = ssl->transform_in->out_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +0100193 if (peer_cid != NULL) {
194 memcpy(peer_cid, ssl->transform_in->out_cid,
195 ssl->transform_in->out_cid_len);
Hanno Becker615ef172019-05-22 16:50:35 +0100196 }
197 }
Hanno Beckerb1f89cd2019-04-26 17:08:02 +0100198
199 *enabled = MBEDTLS_SSL_CID_ENABLED;
200
Gilles Peskine449bd832023-01-11 14:50:10 +0100201 return 0;
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100202}
Hanno Beckera0e20d02019-05-15 14:03:01 +0100203#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
Hanno Beckerf8542cf2019-04-09 15:22:03 +0100204
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200205#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard0ac247f2014-09-30 22:21:31 +0200206
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200207#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200208/*
209 * Convert max_fragment_length codes to length.
210 * RFC 6066 says:
211 * enum{
212 * 2^9(1), 2^10(2), 2^11(3), 2^12(4), (255)
213 * } MaxFragmentLength;
214 * and we add 0 -> extension unused
215 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100216static unsigned int ssl_mfl_code_to_length(int mfl)
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200217{
Gilles Peskine449bd832023-01-11 14:50:10 +0100218 switch (mfl) {
219 case MBEDTLS_SSL_MAX_FRAG_LEN_NONE:
220 return MBEDTLS_TLS_EXT_ADV_CONTENT_LEN;
221 case MBEDTLS_SSL_MAX_FRAG_LEN_512:
222 return 512;
223 case MBEDTLS_SSL_MAX_FRAG_LEN_1024:
224 return 1024;
225 case MBEDTLS_SSL_MAX_FRAG_LEN_2048:
226 return 2048;
227 case MBEDTLS_SSL_MAX_FRAG_LEN_4096:
228 return 4096;
229 default:
230 return MBEDTLS_TLS_EXT_ADV_CONTENT_LEN;
Angus Grattond8213d02016-05-25 20:56:48 +1000231 }
232}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200233#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard581e6b62013-07-18 12:32:27 +0200234
Gilles Peskine449bd832023-01-11 14:50:10 +0100235int mbedtls_ssl_session_copy(mbedtls_ssl_session *dst,
236 const mbedtls_ssl_session *src)
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200237{
Gilles Peskine449bd832023-01-11 14:50:10 +0100238 mbedtls_ssl_session_free(dst);
239 memcpy(dst, src, sizeof(mbedtls_ssl_session));
吴敬辉0b716112021-11-29 10:46:35 +0800240#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
241 dst->ticket = NULL;
Xiaokang Qian87306442022-10-12 09:47:38 +0000242#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
243 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaokang Qian126bf8e2022-10-13 02:22:40 +0000244 dst->hostname = NULL;
吴敬辉0b716112021-11-29 10:46:35 +0800245#endif
Xiaokang Qian87306442022-10-12 09:47:38 +0000246#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
吴敬辉0b716112021-11-29 10:46:35 +0800247
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200248#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker6d1986e2019-02-07 12:27:42 +0000249
250#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100251 if (src->peer_cert != NULL) {
Janos Follath865b3eb2019-12-16 11:46:15 +0000252 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker2292d1f2013-09-15 17:06:49 +0200253
Gilles Peskine449bd832023-01-11 14:50:10 +0100254 dst->peer_cert = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
255 if (dst->peer_cert == NULL) {
256 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
257 }
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200258
Gilles Peskine449bd832023-01-11 14:50:10 +0100259 mbedtls_x509_crt_init(dst->peer_cert);
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200260
Gilles Peskine449bd832023-01-11 14:50:10 +0100261 if ((ret = mbedtls_x509_crt_parse_der(dst->peer_cert, src->peer_cert->raw.p,
262 src->peer_cert->raw.len)) != 0) {
263 mbedtls_free(dst->peer_cert);
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200264 dst->peer_cert = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +0100265 return ret;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200266 }
267 }
Hanno Becker6d1986e2019-02-07 12:27:42 +0000268#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100269 if (src->peer_cert_digest != NULL) {
Hanno Becker9198ad12019-02-05 17:00:50 +0000270 dst->peer_cert_digest =
Gilles Peskine449bd832023-01-11 14:50:10 +0100271 mbedtls_calloc(1, src->peer_cert_digest_len);
272 if (dst->peer_cert_digest == NULL) {
273 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
274 }
Hanno Becker9198ad12019-02-05 17:00:50 +0000275
Gilles Peskine449bd832023-01-11 14:50:10 +0100276 memcpy(dst->peer_cert_digest, src->peer_cert_digest,
277 src->peer_cert_digest_len);
Hanno Becker9198ad12019-02-05 17:00:50 +0000278 dst->peer_cert_digest_type = src->peer_cert_digest_type;
Hanno Beckeraccc5992019-02-25 10:06:59 +0000279 dst->peer_cert_digest_len = src->peer_cert_digest_len;
Hanno Becker9198ad12019-02-05 17:00:50 +0000280 }
281#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
282
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200283#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200284
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200285#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100286 if (src->ticket != NULL) {
287 dst->ticket = mbedtls_calloc(1, src->ticket_len);
288 if (dst->ticket == NULL) {
289 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
290 }
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200291
Gilles Peskine449bd832023-01-11 14:50:10 +0100292 memcpy(dst->ticket, src->ticket, src->ticket_len);
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200293 }
Xiaokang Qian126bf8e2022-10-13 02:22:40 +0000294
295#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
296 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +0100297 if (src->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Xiaokang Qian126bf8e2022-10-13 02:22:40 +0000298 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine449bd832023-01-11 14:50:10 +0100299 ret = mbedtls_ssl_session_set_hostname(dst, src->hostname);
300 if (ret != 0) {
301 return ret;
302 }
Xiaokang Qian126bf8e2022-10-13 02:22:40 +0000303 }
304#endif /* MBEDTLS_SSL_PROTO_TLS1_3 &&
305 MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +0200306#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200307
Gilles Peskine449bd832023-01-11 14:50:10 +0100308 return 0;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +0200309}
310
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500311#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200312MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100313static int resize_buffer(unsigned char **buffer, size_t len_new, size_t *len_old)
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500314{
Gilles Peskine449bd832023-01-11 14:50:10 +0100315 unsigned char *resized_buffer = mbedtls_calloc(1, len_new);
316 if (resized_buffer == NULL) {
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500317 return -1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100318 }
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500319
320 /* We want to copy len_new bytes when downsizing the buffer, and
321 * len_old bytes when upsizing, so we choose the smaller of two sizes,
322 * to fit one buffer into another. Size checks, ensuring that no data is
323 * lost, are done outside of this function. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100324 memcpy(resized_buffer, *buffer,
325 (len_new < *len_old) ? len_new : *len_old);
326 mbedtls_platform_zeroize(*buffer, *len_old);
327 mbedtls_free(*buffer);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500328
329 *buffer = resized_buffer;
330 *len_old = len_new;
331
332 return 0;
333}
Andrzej Kurek4a063792020-10-21 15:08:44 +0200334
Gilles Peskine449bd832023-01-11 14:50:10 +0100335static void handle_buffer_resizing(mbedtls_ssl_context *ssl, int downsizing,
336 size_t in_buf_new_len,
337 size_t out_buf_new_len)
Andrzej Kurek4a063792020-10-21 15:08:44 +0200338{
339 int modified = 0;
340 size_t written_in = 0, iv_offset_in = 0, len_offset_in = 0;
341 size_t written_out = 0, iv_offset_out = 0, len_offset_out = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100342 if (ssl->in_buf != NULL) {
Andrzej Kurek4a063792020-10-21 15:08:44 +0200343 written_in = ssl->in_msg - ssl->in_buf;
344 iv_offset_in = ssl->in_iv - ssl->in_buf;
345 len_offset_in = ssl->in_len - ssl->in_buf;
Gilles Peskine449bd832023-01-11 14:50:10 +0100346 if (downsizing ?
Andrzej Kurek4a063792020-10-21 15:08:44 +0200347 ssl->in_buf_len > in_buf_new_len && ssl->in_left < in_buf_new_len :
Gilles Peskine449bd832023-01-11 14:50:10 +0100348 ssl->in_buf_len < in_buf_new_len) {
349 if (resize_buffer(&ssl->in_buf, in_buf_new_len, &ssl->in_buf_len) != 0) {
350 MBEDTLS_SSL_DEBUG_MSG(1, ("input buffer resizing failed - out of memory"));
351 } else {
352 MBEDTLS_SSL_DEBUG_MSG(2, ("Reallocating in_buf to %" MBEDTLS_PRINTF_SIZET,
353 in_buf_new_len));
Andrzej Kurek4a063792020-10-21 15:08:44 +0200354 modified = 1;
355 }
356 }
357 }
358
Gilles Peskine449bd832023-01-11 14:50:10 +0100359 if (ssl->out_buf != NULL) {
Andrzej Kurek4a063792020-10-21 15:08:44 +0200360 written_out = ssl->out_msg - ssl->out_buf;
361 iv_offset_out = ssl->out_iv - ssl->out_buf;
362 len_offset_out = ssl->out_len - ssl->out_buf;
Gilles Peskine449bd832023-01-11 14:50:10 +0100363 if (downsizing ?
Andrzej Kurek4a063792020-10-21 15:08:44 +0200364 ssl->out_buf_len > out_buf_new_len && ssl->out_left < out_buf_new_len :
Gilles Peskine449bd832023-01-11 14:50:10 +0100365 ssl->out_buf_len < out_buf_new_len) {
366 if (resize_buffer(&ssl->out_buf, out_buf_new_len, &ssl->out_buf_len) != 0) {
367 MBEDTLS_SSL_DEBUG_MSG(1, ("output buffer resizing failed - out of memory"));
368 } else {
369 MBEDTLS_SSL_DEBUG_MSG(2, ("Reallocating out_buf to %" MBEDTLS_PRINTF_SIZET,
370 out_buf_new_len));
Andrzej Kurek4a063792020-10-21 15:08:44 +0200371 modified = 1;
372 }
373 }
374 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100375 if (modified) {
Andrzej Kurek4a063792020-10-21 15:08:44 +0200376 /* Update pointers here to avoid doing it twice. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100377 mbedtls_ssl_reset_in_out_pointers(ssl);
Andrzej Kurek4a063792020-10-21 15:08:44 +0200378 /* Fields below might not be properly updated with record
379 * splitting or with CID, so they are manually updated here. */
380 ssl->out_msg = ssl->out_buf + written_out;
381 ssl->out_len = ssl->out_buf + len_offset_out;
382 ssl->out_iv = ssl->out_buf + iv_offset_out;
383
384 ssl->in_msg = ssl->in_buf + written_in;
385 ssl->in_len = ssl->in_buf + len_offset_in;
386 ssl->in_iv = ssl->in_buf + iv_offset_in;
387 }
388}
Andrzej Kurek0afa2a12020-03-03 10:39:58 -0500389#endif /* MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH */
390
Jerry Yudb8c48a2022-01-27 14:54:54 +0800391#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yued14c932022-02-17 13:40:45 +0800392
393#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Gilles Peskine449bd832023-01-11 14:50:10 +0100394typedef int (*tls_prf_fn)(const unsigned char *secret, size_t slen,
395 const char *label,
396 const unsigned char *random, size_t rlen,
397 unsigned char *dstbuf, size_t dlen);
Jerry Yued14c932022-02-17 13:40:45 +0800398
Gilles Peskine449bd832023-01-11 14:50:10 +0100399static tls_prf_fn ssl_tls12prf_from_cs(int ciphersuite_id);
Jerry Yued14c932022-02-17 13:40:45 +0800400
401#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
402
403/* Type for the TLS PRF */
404typedef int ssl_tls_prf_t(const unsigned char *, size_t, const char *,
405 const unsigned char *, size_t,
406 unsigned char *, size_t);
407
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200408MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100409static int ssl_tls12_populate_transform(mbedtls_ssl_transform *transform,
410 int ciphersuite,
411 const unsigned char master[48],
Neil Armstrongf2c82f02022-04-05 11:16:53 +0200412#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +0100413 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +0200414#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +0100415 ssl_tls_prf_t tls_prf,
416 const unsigned char randbytes[64],
417 mbedtls_ssl_protocol_version tls_version,
418 unsigned endpoint,
419 const mbedtls_ssl_context *ssl);
Jerry Yued14c932022-02-17 13:40:45 +0800420
Andrzej Kurek25f27152022-08-17 16:09:31 -0400421#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +0200422MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100423static int tls_prf_sha256(const unsigned char *secret, size_t slen,
Ron Eldor51d3ab52019-05-12 14:54:30 +0300424 const char *label,
425 const unsigned char *random, size_t rlen,
Gilles Peskine449bd832023-01-11 14:50:10 +0100426 unsigned char *dstbuf, size_t dlen);
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100427static int ssl_calc_verify_tls_sha256(const mbedtls_ssl_context *, unsigned char *, size_t *);
428static int ssl_calc_finished_tls_sha256(mbedtls_ssl_context *, unsigned char *, int);
Gilles Peskine449bd832023-01-11 14:50:10 +0100429
430#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
431
432#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
433MBEDTLS_CHECK_RETURN_CRITICAL
434static int tls_prf_sha384(const unsigned char *secret, size_t slen,
435 const char *label,
436 const unsigned char *random, size_t rlen,
437 unsigned char *dstbuf, size_t dlen);
438
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100439static int ssl_calc_verify_tls_sha384(const mbedtls_ssl_context *, unsigned char *, size_t *);
440static int ssl_calc_finished_tls_sha384(mbedtls_ssl_context *, unsigned char *, int);
Gilles Peskine449bd832023-01-11 14:50:10 +0100441#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
442
443static size_t ssl_tls12_session_save(const mbedtls_ssl_session *session,
444 unsigned char *buf,
445 size_t buf_len);
446
447MBEDTLS_CHECK_RETURN_CRITICAL
448static int ssl_tls12_session_load(mbedtls_ssl_session *session,
449 const unsigned char *buf,
450 size_t len);
451#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
452
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100453static int ssl_update_checksum_start(mbedtls_ssl_context *, const unsigned char *, size_t);
Gilles Peskine449bd832023-01-11 14:50:10 +0100454
455#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100456static int ssl_update_checksum_sha256(mbedtls_ssl_context *, const unsigned char *, size_t);
Gilles Peskine449bd832023-01-11 14:50:10 +0100457#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
458
459#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100460static int ssl_update_checksum_sha384(mbedtls_ssl_context *, const unsigned char *, size_t);
Gilles Peskine449bd832023-01-11 14:50:10 +0100461#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
462
463int mbedtls_ssl_tls_prf(const mbedtls_tls_prf_types prf,
464 const unsigned char *secret, size_t slen,
465 const char *label,
466 const unsigned char *random, size_t rlen,
467 unsigned char *dstbuf, size_t dlen)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300468{
469 mbedtls_ssl_tls_prf_cb *tls_prf = NULL;
470
Gilles Peskine449bd832023-01-11 14:50:10 +0100471 switch (prf) {
Ron Eldord2f25f72019-05-15 14:54:22 +0300472#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek25f27152022-08-17 16:09:31 -0400473#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300474 case MBEDTLS_SSL_TLS_PRF_SHA384:
475 tls_prf = tls_prf_sha384;
Gilles Peskine449bd832023-01-11 14:50:10 +0100476 break;
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400477#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -0400478#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ron Eldor51d3ab52019-05-12 14:54:30 +0300479 case MBEDTLS_SSL_TLS_PRF_SHA256:
480 tls_prf = tls_prf_sha256;
Gilles Peskine449bd832023-01-11 14:50:10 +0100481 break;
Andrzej Kurekcccb0442022-08-19 03:42:11 -0400482#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Ron Eldord2f25f72019-05-15 14:54:22 +0300483#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100484 default:
485 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Ron Eldor51d3ab52019-05-12 14:54:30 +0300486 }
487
Gilles Peskine449bd832023-01-11 14:50:10 +0100488 return tls_prf(secret, slen, label, random, rlen, dstbuf, dlen);
Ron Eldor51d3ab52019-05-12 14:54:30 +0300489}
490
Jerry Yuc73c6182022-02-08 20:29:25 +0800491#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100492static void ssl_clear_peer_cert(mbedtls_ssl_session *session)
Jerry Yuc73c6182022-02-08 20:29:25 +0800493{
494#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100495 if (session->peer_cert != NULL) {
496 mbedtls_x509_crt_free(session->peer_cert);
497 mbedtls_free(session->peer_cert);
Jerry Yuc73c6182022-02-08 20:29:25 +0800498 session->peer_cert = NULL;
499 }
500#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100501 if (session->peer_cert_digest != NULL) {
Jerry Yuc73c6182022-02-08 20:29:25 +0800502 /* Zeroization is not necessary. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100503 mbedtls_free(session->peer_cert_digest);
Jerry Yuc73c6182022-02-08 20:29:25 +0800504 session->peer_cert_digest = NULL;
505 session->peer_cert_digest_type = MBEDTLS_MD_NONE;
506 session->peer_cert_digest_len = 0;
507 }
508#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
509}
510#endif /* MBEDTLS_X509_CRT_PARSE_C */
511
Gilles Peskine449bd832023-01-11 14:50:10 +0100512uint32_t mbedtls_ssl_get_extension_id(unsigned int extension_type)
Jerry Yu7a485c12022-10-31 13:08:18 +0800513{
Gilles Peskine449bd832023-01-11 14:50:10 +0100514 switch (extension_type) {
Jerry Yu7a485c12022-10-31 13:08:18 +0800515 case MBEDTLS_TLS_EXT_SERVERNAME:
Gilles Peskine449bd832023-01-11 14:50:10 +0100516 return MBEDTLS_SSL_EXT_ID_SERVERNAME;
Jerry Yu7a485c12022-10-31 13:08:18 +0800517
518 case MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100519 return MBEDTLS_SSL_EXT_ID_MAX_FRAGMENT_LENGTH;
Jerry Yu7a485c12022-10-31 13:08:18 +0800520
521 case MBEDTLS_TLS_EXT_STATUS_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +0100522 return MBEDTLS_SSL_EXT_ID_STATUS_REQUEST;
Jerry Yu7a485c12022-10-31 13:08:18 +0800523
524 case MBEDTLS_TLS_EXT_SUPPORTED_GROUPS:
Gilles Peskine449bd832023-01-11 14:50:10 +0100525 return MBEDTLS_SSL_EXT_ID_SUPPORTED_GROUPS;
Jerry Yu7a485c12022-10-31 13:08:18 +0800526
527 case MBEDTLS_TLS_EXT_SIG_ALG:
Gilles Peskine449bd832023-01-11 14:50:10 +0100528 return MBEDTLS_SSL_EXT_ID_SIG_ALG;
Jerry Yu7a485c12022-10-31 13:08:18 +0800529
530 case MBEDTLS_TLS_EXT_USE_SRTP:
Gilles Peskine449bd832023-01-11 14:50:10 +0100531 return MBEDTLS_SSL_EXT_ID_USE_SRTP;
Jerry Yu7a485c12022-10-31 13:08:18 +0800532
533 case MBEDTLS_TLS_EXT_HEARTBEAT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100534 return MBEDTLS_SSL_EXT_ID_HEARTBEAT;
Jerry Yu7a485c12022-10-31 13:08:18 +0800535
536 case MBEDTLS_TLS_EXT_ALPN:
Gilles Peskine449bd832023-01-11 14:50:10 +0100537 return MBEDTLS_SSL_EXT_ID_ALPN;
Jerry Yu7a485c12022-10-31 13:08:18 +0800538
539 case MBEDTLS_TLS_EXT_SCT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100540 return MBEDTLS_SSL_EXT_ID_SCT;
Jerry Yu7a485c12022-10-31 13:08:18 +0800541
542 case MBEDTLS_TLS_EXT_CLI_CERT_TYPE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100543 return MBEDTLS_SSL_EXT_ID_CLI_CERT_TYPE;
Jerry Yu7a485c12022-10-31 13:08:18 +0800544
545 case MBEDTLS_TLS_EXT_SERV_CERT_TYPE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100546 return MBEDTLS_SSL_EXT_ID_SERV_CERT_TYPE;
Jerry Yu7a485c12022-10-31 13:08:18 +0800547
548 case MBEDTLS_TLS_EXT_PADDING:
Gilles Peskine449bd832023-01-11 14:50:10 +0100549 return MBEDTLS_SSL_EXT_ID_PADDING;
Jerry Yu7a485c12022-10-31 13:08:18 +0800550
551 case MBEDTLS_TLS_EXT_PRE_SHARED_KEY:
Gilles Peskine449bd832023-01-11 14:50:10 +0100552 return MBEDTLS_SSL_EXT_ID_PRE_SHARED_KEY;
Jerry Yu7a485c12022-10-31 13:08:18 +0800553
554 case MBEDTLS_TLS_EXT_EARLY_DATA:
Gilles Peskine449bd832023-01-11 14:50:10 +0100555 return MBEDTLS_SSL_EXT_ID_EARLY_DATA;
Jerry Yu7a485c12022-10-31 13:08:18 +0800556
557 case MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS:
Gilles Peskine449bd832023-01-11 14:50:10 +0100558 return MBEDTLS_SSL_EXT_ID_SUPPORTED_VERSIONS;
Jerry Yu7a485c12022-10-31 13:08:18 +0800559
560 case MBEDTLS_TLS_EXT_COOKIE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100561 return MBEDTLS_SSL_EXT_ID_COOKIE;
Jerry Yu7a485c12022-10-31 13:08:18 +0800562
563 case MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES:
Gilles Peskine449bd832023-01-11 14:50:10 +0100564 return MBEDTLS_SSL_EXT_ID_PSK_KEY_EXCHANGE_MODES;
Jerry Yu7a485c12022-10-31 13:08:18 +0800565
566 case MBEDTLS_TLS_EXT_CERT_AUTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100567 return MBEDTLS_SSL_EXT_ID_CERT_AUTH;
Jerry Yu7a485c12022-10-31 13:08:18 +0800568
569 case MBEDTLS_TLS_EXT_OID_FILTERS:
Gilles Peskine449bd832023-01-11 14:50:10 +0100570 return MBEDTLS_SSL_EXT_ID_OID_FILTERS;
Jerry Yu7a485c12022-10-31 13:08:18 +0800571
572 case MBEDTLS_TLS_EXT_POST_HANDSHAKE_AUTH:
Gilles Peskine449bd832023-01-11 14:50:10 +0100573 return MBEDTLS_SSL_EXT_ID_POST_HANDSHAKE_AUTH;
Jerry Yu7a485c12022-10-31 13:08:18 +0800574
575 case MBEDTLS_TLS_EXT_SIG_ALG_CERT:
Gilles Peskine449bd832023-01-11 14:50:10 +0100576 return MBEDTLS_SSL_EXT_ID_SIG_ALG_CERT;
Jerry Yu7a485c12022-10-31 13:08:18 +0800577
578 case MBEDTLS_TLS_EXT_KEY_SHARE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100579 return MBEDTLS_SSL_EXT_ID_KEY_SHARE;
Jerry Yu7a485c12022-10-31 13:08:18 +0800580
581 case MBEDTLS_TLS_EXT_TRUNCATED_HMAC:
Gilles Peskine449bd832023-01-11 14:50:10 +0100582 return MBEDTLS_SSL_EXT_ID_TRUNCATED_HMAC;
Jerry Yu7a485c12022-10-31 13:08:18 +0800583
584 case MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS:
Gilles Peskine449bd832023-01-11 14:50:10 +0100585 return MBEDTLS_SSL_EXT_ID_SUPPORTED_POINT_FORMATS;
Jerry Yu7a485c12022-10-31 13:08:18 +0800586
587 case MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC:
Gilles Peskine449bd832023-01-11 14:50:10 +0100588 return MBEDTLS_SSL_EXT_ID_ENCRYPT_THEN_MAC;
Jerry Yu7a485c12022-10-31 13:08:18 +0800589
590 case MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET:
Gilles Peskine449bd832023-01-11 14:50:10 +0100591 return MBEDTLS_SSL_EXT_ID_EXTENDED_MASTER_SECRET;
Jerry Yu7a485c12022-10-31 13:08:18 +0800592
593 case MBEDTLS_TLS_EXT_SESSION_TICKET:
Gilles Peskine449bd832023-01-11 14:50:10 +0100594 return MBEDTLS_SSL_EXT_ID_SESSION_TICKET;
Jerry Yu7a485c12022-10-31 13:08:18 +0800595
596 }
597
Gilles Peskine449bd832023-01-11 14:50:10 +0100598 return MBEDTLS_SSL_EXT_ID_UNRECOGNIZED;
Jerry Yu7a485c12022-10-31 13:08:18 +0800599}
600
Gilles Peskine449bd832023-01-11 14:50:10 +0100601uint32_t mbedtls_ssl_get_extension_mask(unsigned int extension_type)
Jerry Yu7a485c12022-10-31 13:08:18 +0800602{
Gilles Peskine449bd832023-01-11 14:50:10 +0100603 return 1 << mbedtls_ssl_get_extension_id(extension_type);
Jerry Yu7a485c12022-10-31 13:08:18 +0800604}
605
Jerry Yud25cab02022-10-31 12:48:30 +0800606#if defined(MBEDTLS_DEBUG_C)
607static const char *extension_name_table[] = {
Jerry Yuea52ed92022-11-08 21:01:17 +0800608 [MBEDTLS_SSL_EXT_ID_UNRECOGNIZED] = "unrecognized",
Jerry Yud25cab02022-10-31 12:48:30 +0800609 [MBEDTLS_SSL_EXT_ID_SERVERNAME] = "server_name",
610 [MBEDTLS_SSL_EXT_ID_MAX_FRAGMENT_LENGTH] = "max_fragment_length",
611 [MBEDTLS_SSL_EXT_ID_STATUS_REQUEST] = "status_request",
612 [MBEDTLS_SSL_EXT_ID_SUPPORTED_GROUPS] = "supported_groups",
613 [MBEDTLS_SSL_EXT_ID_SIG_ALG] = "signature_algorithms",
614 [MBEDTLS_SSL_EXT_ID_USE_SRTP] = "use_srtp",
615 [MBEDTLS_SSL_EXT_ID_HEARTBEAT] = "heartbeat",
616 [MBEDTLS_SSL_EXT_ID_ALPN] = "application_layer_protocol_negotiation",
617 [MBEDTLS_SSL_EXT_ID_SCT] = "signed_certificate_timestamp",
618 [MBEDTLS_SSL_EXT_ID_CLI_CERT_TYPE] = "client_certificate_type",
619 [MBEDTLS_SSL_EXT_ID_SERV_CERT_TYPE] = "server_certificate_type",
620 [MBEDTLS_SSL_EXT_ID_PADDING] = "padding",
621 [MBEDTLS_SSL_EXT_ID_PRE_SHARED_KEY] = "pre_shared_key",
622 [MBEDTLS_SSL_EXT_ID_EARLY_DATA] = "early_data",
623 [MBEDTLS_SSL_EXT_ID_SUPPORTED_VERSIONS] = "supported_versions",
624 [MBEDTLS_SSL_EXT_ID_COOKIE] = "cookie",
625 [MBEDTLS_SSL_EXT_ID_PSK_KEY_EXCHANGE_MODES] = "psk_key_exchange_modes",
626 [MBEDTLS_SSL_EXT_ID_CERT_AUTH] = "certificate_authorities",
627 [MBEDTLS_SSL_EXT_ID_OID_FILTERS] = "oid_filters",
628 [MBEDTLS_SSL_EXT_ID_POST_HANDSHAKE_AUTH] = "post_handshake_auth",
629 [MBEDTLS_SSL_EXT_ID_SIG_ALG_CERT] = "signature_algorithms_cert",
630 [MBEDTLS_SSL_EXT_ID_KEY_SHARE] = "key_share",
631 [MBEDTLS_SSL_EXT_ID_TRUNCATED_HMAC] = "truncated_hmac",
632 [MBEDTLS_SSL_EXT_ID_SUPPORTED_POINT_FORMATS] = "supported_point_formats",
633 [MBEDTLS_SSL_EXT_ID_ENCRYPT_THEN_MAC] = "encrypt_then_mac",
634 [MBEDTLS_SSL_EXT_ID_EXTENDED_MASTER_SECRET] = "extended_master_secret",
635 [MBEDTLS_SSL_EXT_ID_SESSION_TICKET] = "session_ticket"
636};
637
Gilles Peskine449bd832023-01-11 14:50:10 +0100638static unsigned int extension_type_table[] = {
Jerry Yud25cab02022-10-31 12:48:30 +0800639 [MBEDTLS_SSL_EXT_ID_UNRECOGNIZED] = 0xff,
640 [MBEDTLS_SSL_EXT_ID_SERVERNAME] = MBEDTLS_TLS_EXT_SERVERNAME,
641 [MBEDTLS_SSL_EXT_ID_MAX_FRAGMENT_LENGTH] = MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH,
642 [MBEDTLS_SSL_EXT_ID_STATUS_REQUEST] = MBEDTLS_TLS_EXT_STATUS_REQUEST,
643 [MBEDTLS_SSL_EXT_ID_SUPPORTED_GROUPS] = MBEDTLS_TLS_EXT_SUPPORTED_GROUPS,
644 [MBEDTLS_SSL_EXT_ID_SIG_ALG] = MBEDTLS_TLS_EXT_SIG_ALG,
645 [MBEDTLS_SSL_EXT_ID_USE_SRTP] = MBEDTLS_TLS_EXT_USE_SRTP,
646 [MBEDTLS_SSL_EXT_ID_HEARTBEAT] = MBEDTLS_TLS_EXT_HEARTBEAT,
647 [MBEDTLS_SSL_EXT_ID_ALPN] = MBEDTLS_TLS_EXT_ALPN,
648 [MBEDTLS_SSL_EXT_ID_SCT] = MBEDTLS_TLS_EXT_SCT,
649 [MBEDTLS_SSL_EXT_ID_CLI_CERT_TYPE] = MBEDTLS_TLS_EXT_CLI_CERT_TYPE,
650 [MBEDTLS_SSL_EXT_ID_SERV_CERT_TYPE] = MBEDTLS_TLS_EXT_SERV_CERT_TYPE,
651 [MBEDTLS_SSL_EXT_ID_PADDING] = MBEDTLS_TLS_EXT_PADDING,
652 [MBEDTLS_SSL_EXT_ID_PRE_SHARED_KEY] = MBEDTLS_TLS_EXT_PRE_SHARED_KEY,
653 [MBEDTLS_SSL_EXT_ID_EARLY_DATA] = MBEDTLS_TLS_EXT_EARLY_DATA,
654 [MBEDTLS_SSL_EXT_ID_SUPPORTED_VERSIONS] = MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS,
655 [MBEDTLS_SSL_EXT_ID_COOKIE] = MBEDTLS_TLS_EXT_COOKIE,
656 [MBEDTLS_SSL_EXT_ID_PSK_KEY_EXCHANGE_MODES] = MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES,
657 [MBEDTLS_SSL_EXT_ID_CERT_AUTH] = MBEDTLS_TLS_EXT_CERT_AUTH,
658 [MBEDTLS_SSL_EXT_ID_OID_FILTERS] = MBEDTLS_TLS_EXT_OID_FILTERS,
659 [MBEDTLS_SSL_EXT_ID_POST_HANDSHAKE_AUTH] = MBEDTLS_TLS_EXT_POST_HANDSHAKE_AUTH,
660 [MBEDTLS_SSL_EXT_ID_SIG_ALG_CERT] = MBEDTLS_TLS_EXT_SIG_ALG_CERT,
661 [MBEDTLS_SSL_EXT_ID_KEY_SHARE] = MBEDTLS_TLS_EXT_KEY_SHARE,
662 [MBEDTLS_SSL_EXT_ID_TRUNCATED_HMAC] = MBEDTLS_TLS_EXT_TRUNCATED_HMAC,
663 [MBEDTLS_SSL_EXT_ID_SUPPORTED_POINT_FORMATS] = MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS,
664 [MBEDTLS_SSL_EXT_ID_ENCRYPT_THEN_MAC] = MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC,
665 [MBEDTLS_SSL_EXT_ID_EXTENDED_MASTER_SECRET] = MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET,
666 [MBEDTLS_SSL_EXT_ID_SESSION_TICKET] = MBEDTLS_TLS_EXT_SESSION_TICKET
667};
668
Gilles Peskine449bd832023-01-11 14:50:10 +0100669const char *mbedtls_ssl_get_extension_name(unsigned int extension_type)
Jerry Yud25cab02022-10-31 12:48:30 +0800670{
Gilles Peskine449bd832023-01-11 14:50:10 +0100671 return extension_name_table[
672 mbedtls_ssl_get_extension_id(extension_type)];
Jerry Yud25cab02022-10-31 12:48:30 +0800673}
674
Gilles Peskine449bd832023-01-11 14:50:10 +0100675static const char *ssl_tls13_get_hs_msg_name(int hs_msg_type)
Jerry Yud25cab02022-10-31 12:48:30 +0800676{
Gilles Peskine449bd832023-01-11 14:50:10 +0100677 switch (hs_msg_type) {
Jerry Yud25cab02022-10-31 12:48:30 +0800678 case MBEDTLS_SSL_HS_CLIENT_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +0100679 return "ClientHello";
Jerry Yud25cab02022-10-31 12:48:30 +0800680 case MBEDTLS_SSL_HS_SERVER_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +0100681 return "ServerHello";
Jerry Yud25cab02022-10-31 12:48:30 +0800682 case MBEDTLS_SSL_TLS1_3_HS_HELLO_RETRY_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +0100683 return "HelloRetryRequest";
Jerry Yud25cab02022-10-31 12:48:30 +0800684 case MBEDTLS_SSL_HS_NEW_SESSION_TICKET:
Gilles Peskine449bd832023-01-11 14:50:10 +0100685 return "NewSessionTicket";
Jerry Yud25cab02022-10-31 12:48:30 +0800686 case MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS:
Gilles Peskine449bd832023-01-11 14:50:10 +0100687 return "EncryptedExtensions";
Jerry Yud25cab02022-10-31 12:48:30 +0800688 case MBEDTLS_SSL_HS_CERTIFICATE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100689 return "Certificate";
Jerry Yud25cab02022-10-31 12:48:30 +0800690 case MBEDTLS_SSL_HS_CERTIFICATE_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +0100691 return "CertificateRequest";
Jerry Yud25cab02022-10-31 12:48:30 +0800692 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100693 return "Unknown";
Jerry Yud25cab02022-10-31 12:48:30 +0800694}
695
Gilles Peskine449bd832023-01-11 14:50:10 +0100696void mbedtls_ssl_print_extension(const mbedtls_ssl_context *ssl,
697 int level, const char *file, int line,
698 int hs_msg_type, unsigned int extension_type,
699 const char *extra_msg0, const char *extra_msg1)
Jerry Yud25cab02022-10-31 12:48:30 +0800700{
701 const char *extra_msg;
Gilles Peskine449bd832023-01-11 14:50:10 +0100702 if (extra_msg0 && extra_msg1) {
Jerry Yud25cab02022-10-31 12:48:30 +0800703 mbedtls_debug_print_msg(
704 ssl, level, file, line,
705 "%s: %s(%u) extension %s %s.",
Gilles Peskine449bd832023-01-11 14:50:10 +0100706 ssl_tls13_get_hs_msg_name(hs_msg_type),
707 mbedtls_ssl_get_extension_name(extension_type),
Jerry Yud25cab02022-10-31 12:48:30 +0800708 extension_type,
Gilles Peskine449bd832023-01-11 14:50:10 +0100709 extra_msg0, extra_msg1);
Jerry Yud25cab02022-10-31 12:48:30 +0800710 return;
711 }
712
713 extra_msg = extra_msg0 ? extra_msg0 : extra_msg1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100714 if (extra_msg) {
Jerry Yud25cab02022-10-31 12:48:30 +0800715 mbedtls_debug_print_msg(
716 ssl, level, file, line,
Gilles Peskine449bd832023-01-11 14:50:10 +0100717 "%s: %s(%u) extension %s.", ssl_tls13_get_hs_msg_name(hs_msg_type),
718 mbedtls_ssl_get_extension_name(extension_type), extension_type,
719 extra_msg);
Jerry Yud25cab02022-10-31 12:48:30 +0800720 return;
721 }
722
723 mbedtls_debug_print_msg(
724 ssl, level, file, line,
Gilles Peskine449bd832023-01-11 14:50:10 +0100725 "%s: %s(%u) extension.", ssl_tls13_get_hs_msg_name(hs_msg_type),
726 mbedtls_ssl_get_extension_name(extension_type), extension_type);
Jerry Yud25cab02022-10-31 12:48:30 +0800727}
728
Gilles Peskine449bd832023-01-11 14:50:10 +0100729void mbedtls_ssl_print_extensions(const mbedtls_ssl_context *ssl,
730 int level, const char *file, int line,
731 int hs_msg_type, uint32_t extensions_mask,
732 const char *extra)
Jerry Yud25cab02022-10-31 12:48:30 +0800733{
734
Gilles Peskine449bd832023-01-11 14:50:10 +0100735 for (unsigned i = 0;
736 i < sizeof(extension_name_table) / sizeof(extension_name_table[0]);
737 i++) {
Jerry Yu79aa7212022-11-08 21:30:21 +0800738 mbedtls_ssl_print_extension(
Jerry Yuea52ed92022-11-08 21:01:17 +0800739 ssl, level, file, line, hs_msg_type, extension_type_table[i],
Gilles Peskine449bd832023-01-11 14:50:10 +0100740 extensions_mask & (1 << i) ? "exists" : "does not exist", extra);
Jerry Yud25cab02022-10-31 12:48:30 +0800741 }
742}
743
Pengyu Lvee455c02023-01-12 14:37:24 +0800744#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS)
745#define ARRAY_LENGTH(a) (sizeof(a) / sizeof(*(a)))
746
747static const char *ticket_flag_name_table[] =
748{
749 [0] = "ALLOW_PSK_RESUMPTION",
750 [2] = "ALLOW_PSK_EPHEMERAL_RESUMPTION",
751 [3] = "ALLOW_EARLY_DATA",
752};
753
Pengyu Lv4938a562023-01-16 11:28:49 +0800754void mbedtls_ssl_print_ticket_flags(const mbedtls_ssl_context *ssl,
755 int level, const char *file, int line,
756 unsigned int flags)
Pengyu Lvee455c02023-01-12 14:37:24 +0800757{
758 size_t i;
759
760 mbedtls_debug_print_msg(ssl, level, file, line,
Pengyu Lv4938a562023-01-16 11:28:49 +0800761 "print ticket_flags (0x%02x)", flags);
762
763 flags = flags & MBEDTLS_SSL_TLS1_3_TICKET_FLAGS_MASK;
Pengyu Lvee455c02023-01-12 14:37:24 +0800764
765 for (i = 0; i < ARRAY_LENGTH(ticket_flag_name_table); i++) {
Pengyu Lv4938a562023-01-16 11:28:49 +0800766 if ((flags & (1 << i))) {
Pengyu Lvee455c02023-01-12 14:37:24 +0800767 mbedtls_debug_print_msg(ssl, level, file, line, "- %s is set.",
768 ticket_flag_name_table[i]);
769 }
770 }
771}
772#endif /* MBEDTLS_SSL_PROTO_TLS1_3 && MBEDTLS_SSL_SESSION_TICKETS */
773
Jerry Yud25cab02022-10-31 12:48:30 +0800774#endif /* MBEDTLS_DEBUG_C */
775
Gilles Peskine449bd832023-01-11 14:50:10 +0100776void mbedtls_ssl_optimize_checksum(mbedtls_ssl_context *ssl,
777 const mbedtls_ssl_ciphersuite_t *ciphersuite_info)
Jerry Yuc73c6182022-02-08 20:29:25 +0800778{
779 ((void) ciphersuite_info);
780
Andrzej Kurek25f27152022-08-17 16:09:31 -0400781#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +0100782 if (ciphersuite_info->mac == MBEDTLS_MD_SHA384) {
Jerry Yuc73c6182022-02-08 20:29:25 +0800783 ssl->handshake->update_checksum = ssl_update_checksum_sha384;
Gilles Peskine449bd832023-01-11 14:50:10 +0100784 } else
Jerry Yuc73c6182022-02-08 20:29:25 +0800785#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400786#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +0100787 if (ciphersuite_info->mac != MBEDTLS_MD_SHA384) {
Jerry Yuc73c6182022-02-08 20:29:25 +0800788 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Gilles Peskine449bd832023-01-11 14:50:10 +0100789 } else
Jerry Yuc73c6182022-02-08 20:29:25 +0800790#endif
791 {
Gilles Peskine449bd832023-01-11 14:50:10 +0100792 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
Jerry Yuc73c6182022-02-08 20:29:25 +0800793 return;
794 }
795}
796
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100797int mbedtls_ssl_add_hs_hdr_to_checksum(mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100798 unsigned hs_type,
799 size_t total_hs_len)
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100800{
801 unsigned char hs_hdr[4];
802
803 /* Build HS header for checksum update. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100804 hs_hdr[0] = MBEDTLS_BYTE_0(hs_type);
805 hs_hdr[1] = MBEDTLS_BYTE_2(total_hs_len);
806 hs_hdr[2] = MBEDTLS_BYTE_1(total_hs_len);
807 hs_hdr[3] = MBEDTLS_BYTE_0(total_hs_len);
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100808
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100809 return ssl->handshake->update_checksum(ssl, hs_hdr, sizeof(hs_hdr));
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100810}
811
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100812int mbedtls_ssl_add_hs_msg_to_checksum(mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100813 unsigned hs_type,
814 unsigned char const *msg,
815 size_t msg_len)
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100816{
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100817 int ret;
818 ret = mbedtls_ssl_add_hs_hdr_to_checksum(ssl, hs_type, msg_len);
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100819 if (ret != 0) {
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100820 return ret;
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100821 }
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100822 return ssl->handshake->update_checksum(ssl, msg, msg_len);
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100823}
824
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100825int mbedtls_ssl_reset_checksum(mbedtls_ssl_context *ssl)
Jerry Yuc73c6182022-02-08 20:29:25 +0800826{
Manuel Pégourié-Gonnard626aaed2023-02-06 22:03:06 +0100827#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) || \
828 defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100829#if defined(MBEDTLS_USE_PSA_CRYPTO)
830 psa_status_t status;
831#else
832 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
833#endif
Manuel Pégourié-Gonnard626aaed2023-02-06 22:03:06 +0100834#else /* SHA-256 or SHA-384 */
Jerry Yuc73c6182022-02-08 20:29:25 +0800835 ((void) ssl);
Manuel Pégourié-Gonnard626aaed2023-02-06 22:03:06 +0100836#endif /* SHA-256 or SHA-384 */
Andrzej Kurek25f27152022-08-17 16:09:31 -0400837#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800838#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100839 status = psa_hash_abort(&ssl->handshake->fin_sha256_psa);
840 if (status != PSA_SUCCESS) {
841 return mbedtls_md_error_from_psa(status);
842 }
843 status = psa_hash_setup(&ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256);
844 if (status != PSA_SUCCESS) {
845 return mbedtls_md_error_from_psa(status);
846 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800847#else
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100848 ret = mbedtls_sha256_starts(&ssl->handshake->fin_sha256, 0);
849 if (ret != 0) {
850 return ret;
851 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800852#endif
853#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400854#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800855#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100856 status = psa_hash_abort(&ssl->handshake->fin_sha384_psa);
857 if (status != PSA_SUCCESS) {
858 return mbedtls_md_error_from_psa(status);
859 }
860 status = psa_hash_setup(&ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384);
861 if (status != PSA_SUCCESS) {
862 return mbedtls_md_error_from_psa(status);
863 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800864#else
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100865 ret = mbedtls_sha512_starts(&ssl->handshake->fin_sha384, 1);
866 if (ret != 0) {
867 return ret;
868 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800869#endif
870#endif
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100871 return 0;
Jerry Yuc73c6182022-02-08 20:29:25 +0800872}
873
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100874static int ssl_update_checksum_start(mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100875 const unsigned char *buf, size_t len)
Jerry Yuc73c6182022-02-08 20:29:25 +0800876{
Manuel Pégourié-Gonnard626aaed2023-02-06 22:03:06 +0100877#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA) || \
878 defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100879#if defined(MBEDTLS_USE_PSA_CRYPTO)
880 psa_status_t status;
881#else
882 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
883#endif
Manuel Pégourié-Gonnard626aaed2023-02-06 22:03:06 +0100884#else /* SHA-256 or SHA-384 */
885 ((void) ssl);
886 (void) buf;
887 (void) len;
888#endif /* SHA-256 or SHA-384 */
Andrzej Kurek25f27152022-08-17 16:09:31 -0400889#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800890#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100891 status = psa_hash_update(&ssl->handshake->fin_sha256_psa, buf, len);
892 if (status != PSA_SUCCESS) {
893 return mbedtls_md_error_from_psa(status);
894 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800895#else
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100896 ret = mbedtls_sha256_update(&ssl->handshake->fin_sha256, buf, len);
897 if (ret != 0) {
898 return ret;
899 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800900#endif
901#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400902#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuc73c6182022-02-08 20:29:25 +0800903#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100904 status = psa_hash_update(&ssl->handshake->fin_sha384_psa, buf, len);
905 if (status != PSA_SUCCESS) {
906 return mbedtls_md_error_from_psa(status);
907 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800908#else
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100909 ret = mbedtls_sha512_update(&ssl->handshake->fin_sha384, buf, len);
910 if (ret != 0) {
911 return ret;
912 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800913#endif
914#endif
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100915 return 0;
Jerry Yuc73c6182022-02-08 20:29:25 +0800916}
917
Andrzej Kurek25f27152022-08-17 16:09:31 -0400918#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100919static int ssl_update_checksum_sha256(mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100920 const unsigned char *buf, size_t len)
Jerry Yuc73c6182022-02-08 20:29:25 +0800921{
922#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100923 return mbedtls_md_error_from_psa(psa_hash_update(
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100924 &ssl->handshake->fin_sha256_psa, buf, len));
Jerry Yuc73c6182022-02-08 20:29:25 +0800925#else
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100926 return mbedtls_sha256_update(&ssl->handshake->fin_sha256, buf, len);
Jerry Yuc73c6182022-02-08 20:29:25 +0800927#endif
928}
929#endif
930
Andrzej Kurek25f27152022-08-17 16:09:31 -0400931#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100932static int ssl_update_checksum_sha384(mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100933 const unsigned char *buf, size_t len)
Jerry Yuc73c6182022-02-08 20:29:25 +0800934{
935#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100936 return mbedtls_md_error_from_psa(psa_hash_update(
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100937 &ssl->handshake->fin_sha384_psa, buf, len));
Jerry Yuc73c6182022-02-08 20:29:25 +0800938#else
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100939 return mbedtls_sha512_update(&ssl->handshake->fin_sha384, buf, len);
Jerry Yuc73c6182022-02-08 20:29:25 +0800940#endif
941}
942#endif
943
Gilles Peskine449bd832023-01-11 14:50:10 +0100944static void ssl_handshake_params_init(mbedtls_ssl_handshake_params *handshake)
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200945{
Gilles Peskine449bd832023-01-11 14:50:10 +0100946 memset(handshake, 0, sizeof(mbedtls_ssl_handshake_params));
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200947
Andrzej Kurek25f27152022-08-17 16:09:31 -0400948#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -0500949#if defined(MBEDTLS_USE_PSA_CRYPTO)
950 handshake->fin_sha256_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -0500951#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100952 mbedtls_sha256_init(&handshake->fin_sha256);
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200953#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -0500954#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -0400955#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -0500956#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -0500957 handshake->fin_sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -0500958#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100959 mbedtls_sha512_init(&handshake->fin_sha384);
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200960#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -0500961#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200962
963 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +0100964
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200965#if defined(MBEDTLS_DHM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100966 mbedtls_dhm_init(&handshake->dhm_ctx);
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200967#endif
Neil Armstrongf3f46412022-04-12 14:43:39 +0200968#if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100969 mbedtls_ecdh_init(&handshake->ecdh_ctx);
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200970#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +0200971#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Neil Armstrongca7d5062022-05-31 14:43:23 +0200972#if defined(MBEDTLS_USE_PSA_CRYPTO)
973 handshake->psa_pake_ctx = psa_pake_operation_init();
974 handshake->psa_pake_password = MBEDTLS_SVC_KEY_ID_INIT;
975#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100976 mbedtls_ecjpake_init(&handshake->ecjpake_ctx);
Neil Armstrongca7d5062022-05-31 14:43:23 +0200977#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +0200978#if defined(MBEDTLS_SSL_CLI_C)
979 handshake->ecjpake_cache = NULL;
980 handshake->ecjpake_cache_len = 0;
981#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +0200982#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +0200983
Gilles Peskineeccd8882020-03-10 12:19:08 +0100984#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +0100985 mbedtls_x509_crt_restart_init(&handshake->ecrs_ctx);
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +0200986#endif
987
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +0200988#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
989 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
990#endif
Hanno Becker75173122019-02-06 16:18:31 +0000991
992#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
993 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100994 mbedtls_pk_init(&handshake->peer_pubkey);
Hanno Becker75173122019-02-06 16:18:31 +0000995#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200996}
997
Gilles Peskine449bd832023-01-11 14:50:10 +0100998void mbedtls_ssl_transform_init(mbedtls_ssl_transform *transform)
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200999{
Gilles Peskine449bd832023-01-11 14:50:10 +01001000 memset(transform, 0, sizeof(mbedtls_ssl_transform));
Paul Bakker84bbeb52014-07-01 14:53:22 +02001001
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +01001002#if defined(MBEDTLS_USE_PSA_CRYPTO)
1003 transform->psa_key_enc = MBEDTLS_SVC_KEY_ID_INIT;
1004 transform->psa_key_dec = MBEDTLS_SVC_KEY_ID_INIT;
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01001005#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001006 mbedtls_cipher_init(&transform->cipher_ctx_enc);
1007 mbedtls_cipher_init(&transform->cipher_ctx_dec);
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +01001008#endif
1009
Hanno Beckerfd86ca82020-11-30 08:54:23 +00001010#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong39b8e7d2022-02-23 09:24:45 +01001011#if defined(MBEDTLS_USE_PSA_CRYPTO)
1012 transform->psa_mac_enc = MBEDTLS_SVC_KEY_ID_INIT;
1013 transform->psa_mac_dec = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrongcf8841a2022-02-24 11:17:45 +01001014#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001015 mbedtls_md_init(&transform->md_ctx_enc);
1016 mbedtls_md_init(&transform->md_ctx_dec);
Hanno Beckerd56ed242018-01-03 15:32:51 +00001017#endif
Neil Armstrongcf8841a2022-02-24 11:17:45 +01001018#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001019}
1020
Gilles Peskine449bd832023-01-11 14:50:10 +01001021void mbedtls_ssl_session_init(mbedtls_ssl_session *session)
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001022{
Gilles Peskine449bd832023-01-11 14:50:10 +01001023 memset(session, 0, sizeof(mbedtls_ssl_session));
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001024}
1025
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001026MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001027static int ssl_handshake_init(mbedtls_ssl_context *ssl)
Paul Bakker48916f92012-09-16 19:57:18 +00001028{
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001029 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1030
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001031 /* Clear old handshake information if present */
Jerry Yu2e199812022-12-01 18:57:19 +08001032#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01001033 if (ssl->transform_negotiate) {
1034 mbedtls_ssl_transform_free(ssl->transform_negotiate);
1035 }
Jerry Yu2e199812022-12-01 18:57:19 +08001036#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001037 if (ssl->session_negotiate) {
1038 mbedtls_ssl_session_free(ssl->session_negotiate);
1039 }
1040 if (ssl->handshake) {
1041 mbedtls_ssl_handshake_free(ssl);
1042 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001043
Jerry Yu2e199812022-12-01 18:57:19 +08001044#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001045 /*
1046 * Either the pointers are now NULL or cleared properly and can be freed.
1047 * Now allocate missing structures.
1048 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001049 if (ssl->transform_negotiate == NULL) {
1050 ssl->transform_negotiate = mbedtls_calloc(1, sizeof(mbedtls_ssl_transform));
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001051 }
Jerry Yu2e199812022-12-01 18:57:19 +08001052#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker48916f92012-09-16 19:57:18 +00001053
Gilles Peskine449bd832023-01-11 14:50:10 +01001054 if (ssl->session_negotiate == NULL) {
1055 ssl->session_negotiate = mbedtls_calloc(1, sizeof(mbedtls_ssl_session));
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001056 }
Paul Bakker48916f92012-09-16 19:57:18 +00001057
Gilles Peskine449bd832023-01-11 14:50:10 +01001058 if (ssl->handshake == NULL) {
1059 ssl->handshake = mbedtls_calloc(1, sizeof(mbedtls_ssl_handshake_params));
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001060 }
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05001061#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1062 /* If the buffers are too small - reallocate */
Andrzej Kurek8ea68722020-04-03 06:40:47 -04001063
Gilles Peskine449bd832023-01-11 14:50:10 +01001064 handle_buffer_resizing(ssl, 0, MBEDTLS_SSL_IN_BUFFER_LEN,
1065 MBEDTLS_SSL_OUT_BUFFER_LEN);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05001066#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001067
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001068 /* All pointers should exist and can be directly freed without issue */
Gilles Peskine449bd832023-01-11 14:50:10 +01001069 if (ssl->handshake == NULL ||
Jerry Yu2e199812022-12-01 18:57:19 +08001070#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker48916f92012-09-16 19:57:18 +00001071 ssl->transform_negotiate == NULL ||
Jerry Yu2e199812022-12-01 18:57:19 +08001072#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001073 ssl->session_negotiate == NULL) {
1074 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc() of ssl sub-contexts failed"));
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001075
Gilles Peskine449bd832023-01-11 14:50:10 +01001076 mbedtls_free(ssl->handshake);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001077 ssl->handshake = NULL;
Jerry Yu2e199812022-12-01 18:57:19 +08001078
1079#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01001080 mbedtls_free(ssl->transform_negotiate);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001081 ssl->transform_negotiate = NULL;
Jerry Yu2e199812022-12-01 18:57:19 +08001082#endif
1083
Gilles Peskine449bd832023-01-11 14:50:10 +01001084 mbedtls_free(ssl->session_negotiate);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001085 ssl->session_negotiate = NULL;
1086
Gilles Peskine449bd832023-01-11 14:50:10 +01001087 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Paul Bakker48916f92012-09-16 19:57:18 +00001088 }
1089
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001090 /* Initialize structures */
Gilles Peskine449bd832023-01-11 14:50:10 +01001091 mbedtls_ssl_session_init(ssl->session_negotiate);
1092 ssl_handshake_params_init(ssl->handshake);
Paul Bakker968afaa2014-07-09 11:09:24 +02001093
Jerry Yu2e199812022-12-01 18:57:19 +08001094#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01001095 mbedtls_ssl_transform_init(ssl->transform_negotiate);
Jerry Yu2e199812022-12-01 18:57:19 +08001096#endif
1097
Manuel Pégourié-Gonnard537f2312023-02-05 10:17:45 +01001098 /* Setup handshake checksums */
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001099 ret = mbedtls_ssl_reset_checksum(ssl);
1100 if (ret != 0) {
1101 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_reset_checksum", ret);
1102 return ret;
1103 }
Manuel Pégourié-Gonnard537f2312023-02-05 10:17:45 +01001104
Jerry Yud0766ec2022-09-22 10:46:57 +08001105#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
1106 defined(MBEDTLS_SSL_SRV_C) && \
1107 defined(MBEDTLS_SSL_SESSION_TICKETS)
1108 ssl->handshake->new_session_tickets_count =
Gilles Peskine449bd832023-01-11 14:50:10 +01001109 ssl->conf->new_session_tickets_count;
Jerry Yud0766ec2022-09-22 10:46:57 +08001110#endif
1111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001112#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001113 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001114 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001115
Gilles Peskine449bd832023-01-11 14:50:10 +01001116 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001117 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Gilles Peskine449bd832023-01-11 14:50:10 +01001118 } else {
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001119 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Gilles Peskine449bd832023-01-11 14:50:10 +01001120 }
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001121
Gilles Peskine449bd832023-01-11 14:50:10 +01001122 mbedtls_ssl_set_timer(ssl, 0);
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001123 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001124#endif
1125
Brett Warrene0edc842021-08-17 09:53:13 +01001126/*
1127 * curve_list is translated to IANA TLS group identifiers here because
1128 * mbedtls_ssl_conf_curves returns void and so can't return
1129 * any error codes.
1130 */
1131#if defined(MBEDTLS_ECP_C)
1132#if !defined(MBEDTLS_DEPRECATED_REMOVED)
1133 /* Heap allocate and translate curve_list from internal to IANA group ids */
Gilles Peskine449bd832023-01-11 14:50:10 +01001134 if (ssl->conf->curve_list != NULL) {
Brett Warrene0edc842021-08-17 09:53:13 +01001135 size_t length;
1136 const mbedtls_ecp_group_id *curve_list = ssl->conf->curve_list;
1137
Gilles Peskine449bd832023-01-11 14:50:10 +01001138 for (length = 0; (curve_list[length] != MBEDTLS_ECP_DP_NONE) &&
1139 (length < MBEDTLS_ECP_DP_MAX); length++) {
1140 }
Brett Warrene0edc842021-08-17 09:53:13 +01001141
1142 /* Leave room for zero termination */
Gilles Peskine449bd832023-01-11 14:50:10 +01001143 uint16_t *group_list = mbedtls_calloc(length + 1, sizeof(uint16_t));
1144 if (group_list == NULL) {
1145 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
1146 }
Brett Warrene0edc842021-08-17 09:53:13 +01001147
Gilles Peskine449bd832023-01-11 14:50:10 +01001148 for (size_t i = 0; i < length; i++) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01001149 uint16_t tls_id = mbedtls_ssl_get_tls_id_from_ecp_group_id(
Gilles Peskine449bd832023-01-11 14:50:10 +01001150 curve_list[i]);
1151 if (tls_id == 0) {
1152 mbedtls_free(group_list);
1153 return MBEDTLS_ERR_SSL_BAD_CONFIG;
Brett Warrene0edc842021-08-17 09:53:13 +01001154 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01001155 group_list[i] = tls_id;
Brett Warrene0edc842021-08-17 09:53:13 +01001156 }
1157
1158 group_list[length] = 0;
1159
1160 ssl->handshake->group_list = group_list;
1161 ssl->handshake->group_list_heap_allocated = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001162 } else {
Brett Warrene0edc842021-08-17 09:53:13 +01001163 ssl->handshake->group_list = ssl->conf->group_list;
1164 ssl->handshake->group_list_heap_allocated = 0;
1165 }
1166#endif /* MBEDTLS_DEPRECATED_REMOVED */
1167#endif /* MBEDTLS_ECP_C */
1168
Ronald Crone68ab4f2022-10-05 12:46:29 +02001169#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +08001170#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Jerry Yua69269a2022-01-17 21:06:01 +08001171#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yu713013f2022-01-17 18:16:35 +08001172 /* Heap allocate and translate sig_hashes from internal hash identifiers to
1173 signature algorithms IANA identifiers. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001174 if (mbedtls_ssl_conf_is_tls12_only(ssl->conf) &&
1175 ssl->conf->sig_hashes != NULL) {
Jerry Yuf017ee42022-01-12 15:49:48 +08001176 const int *md;
1177 const int *sig_hashes = ssl->conf->sig_hashes;
Jerry Yub476a442022-01-21 18:14:45 +08001178 size_t sig_algs_len = 0;
Jerry Yuf017ee42022-01-12 15:49:48 +08001179 uint16_t *p;
1180
Jerry Yub476a442022-01-21 18:14:45 +08001181#if defined(static_assert)
Gilles Peskine449bd832023-01-11 14:50:10 +01001182 static_assert(MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN
1183 <= (SIZE_MAX - (2 * sizeof(uint16_t))),
1184 "MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN too big");
Jerry Yua68dca22022-01-20 16:28:27 +08001185#endif
1186
Gilles Peskine449bd832023-01-11 14:50:10 +01001187 for (md = sig_hashes; *md != MBEDTLS_MD_NONE; md++) {
1188 if (mbedtls_ssl_hash_from_md_alg(*md) == MBEDTLS_SSL_HASH_NONE) {
Jerry Yuf017ee42022-01-12 15:49:48 +08001189 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001190 }
Jerry Yub476a442022-01-21 18:14:45 +08001191#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001192 sig_algs_len += sizeof(uint16_t);
Jerry Yub476a442022-01-21 18:14:45 +08001193#endif
Jerry Yua68dca22022-01-20 16:28:27 +08001194
Jerry Yub476a442022-01-21 18:14:45 +08001195#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001196 sig_algs_len += sizeof(uint16_t);
Jerry Yub476a442022-01-21 18:14:45 +08001197#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001198 if (sig_algs_len > MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN) {
1199 return MBEDTLS_ERR_SSL_BAD_CONFIG;
1200 }
Jerry Yuf017ee42022-01-12 15:49:48 +08001201 }
1202
Gilles Peskine449bd832023-01-11 14:50:10 +01001203 if (sig_algs_len < MBEDTLS_SSL_MIN_SIG_ALG_LIST_LEN) {
1204 return MBEDTLS_ERR_SSL_BAD_CONFIG;
1205 }
Jerry Yuf017ee42022-01-12 15:49:48 +08001206
Gilles Peskine449bd832023-01-11 14:50:10 +01001207 ssl->handshake->sig_algs = mbedtls_calloc(1, sig_algs_len +
1208 sizeof(uint16_t));
1209 if (ssl->handshake->sig_algs == NULL) {
1210 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
1211 }
Jerry Yuf017ee42022-01-12 15:49:48 +08001212
Gilles Peskine449bd832023-01-11 14:50:10 +01001213 p = (uint16_t *) ssl->handshake->sig_algs;
1214 for (md = sig_hashes; *md != MBEDTLS_MD_NONE; md++) {
1215 unsigned char hash = mbedtls_ssl_hash_from_md_alg(*md);
1216 if (hash == MBEDTLS_SSL_HASH_NONE) {
Jerry Yuf017ee42022-01-12 15:49:48 +08001217 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001218 }
Jerry Yu6106fdc2022-01-12 16:36:14 +08001219#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001220 *p = ((hash << 8) | MBEDTLS_SSL_SIG_ECDSA);
Jerry Yuf017ee42022-01-12 15:49:48 +08001221 p++;
Jerry Yu6106fdc2022-01-12 16:36:14 +08001222#endif
1223#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001224 *p = ((hash << 8) | MBEDTLS_SSL_SIG_RSA);
Jerry Yuf017ee42022-01-12 15:49:48 +08001225 p++;
Jerry Yu6106fdc2022-01-12 16:36:14 +08001226#endif
Jerry Yuf017ee42022-01-12 15:49:48 +08001227 }
Gabor Mezei15b95a62022-05-09 16:37:58 +02001228 *p = MBEDTLS_TLS_SIG_NONE;
Jerry Yuf017ee42022-01-12 15:49:48 +08001229 ssl->handshake->sig_algs_heap_allocated = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001230 } else
Jerry Yua69269a2022-01-17 21:06:01 +08001231#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Jerry Yuf017ee42022-01-12 15:49:48 +08001232 {
Jerry Yuf017ee42022-01-12 15:49:48 +08001233 ssl->handshake->sig_algs_heap_allocated = 0;
1234 }
Jerry Yucc539102022-06-27 16:27:35 +08001235#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Ronald Crone68ab4f2022-10-05 12:46:29 +02001236#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Gilles Peskine449bd832023-01-11 14:50:10 +01001237 return 0;
Paul Bakker48916f92012-09-16 19:57:18 +00001238}
1239
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001240#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001241/* Dummy cookie callbacks for defaults */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001242MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001243static int ssl_cookie_write_dummy(void *ctx,
1244 unsigned char **p, unsigned char *end,
1245 const unsigned char *cli_id, size_t cli_id_len)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001246{
1247 ((void) ctx);
1248 ((void) p);
1249 ((void) end);
1250 ((void) cli_id);
1251 ((void) cli_id_len);
1252
Gilles Peskine449bd832023-01-11 14:50:10 +01001253 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001254}
1255
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001256MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001257static int ssl_cookie_check_dummy(void *ctx,
1258 const unsigned char *cookie, size_t cookie_len,
1259 const unsigned char *cli_id, size_t cli_id_len)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001260{
1261 ((void) ctx);
1262 ((void) cookie);
1263 ((void) cookie_len);
1264 ((void) cli_id);
1265 ((void) cli_id_len);
1266
Gilles Peskine449bd832023-01-11 14:50:10 +01001267 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001268}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001269#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001270
Paul Bakker5121ce52009-01-03 21:22:43 +00001271/*
1272 * Initialize an SSL context
1273 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001274void mbedtls_ssl_init(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001275{
Gilles Peskine449bd832023-01-11 14:50:10 +01001276 memset(ssl, 0, sizeof(mbedtls_ssl_context));
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001277}
1278
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001279MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001280static int ssl_conf_version_check(const mbedtls_ssl_context *ssl)
Jerry Yu60835a82021-08-04 10:13:52 +08001281{
Ronald Cron086ee0b2022-03-15 15:18:51 +01001282 const mbedtls_ssl_config *conf = ssl->conf;
1283
Ronald Cron6f135e12021-12-08 16:57:54 +01001284#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001285 if (mbedtls_ssl_conf_is_tls13_only(conf)) {
1286 if (conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
1287 MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS 1.3 is not yet supported."));
1288 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQianed582dd2022-04-13 08:21:05 +00001289 }
1290
Gilles Peskine449bd832023-01-11 14:50:10 +01001291 MBEDTLS_SSL_DEBUG_MSG(4, ("The SSL configuration is tls13 only."));
1292 return 0;
Jerry Yu60835a82021-08-04 10:13:52 +08001293 }
1294#endif
1295
1296#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01001297 if (mbedtls_ssl_conf_is_tls12_only(conf)) {
1298 MBEDTLS_SSL_DEBUG_MSG(4, ("The SSL configuration is tls12 only."));
1299 return 0;
Jerry Yu60835a82021-08-04 10:13:52 +08001300 }
1301#endif
1302
Ronald Cron6f135e12021-12-08 16:57:54 +01001303#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001304 if (mbedtls_ssl_conf_is_hybrid_tls12_tls13(conf)) {
1305 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
1306 MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS not yet supported in Hybrid TLS 1.3 + TLS 1.2"));
1307 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQianed582dd2022-04-13 08:21:05 +00001308 }
1309
Gilles Peskine449bd832023-01-11 14:50:10 +01001310 if (conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
1311 MBEDTLS_SSL_DEBUG_MSG(1, ("TLS 1.3 server is not supported yet."));
1312 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQian8f9dfe42022-04-15 02:52:39 +00001313 }
1314
1315
Gilles Peskine449bd832023-01-11 14:50:10 +01001316 MBEDTLS_SSL_DEBUG_MSG(4, ("The SSL configuration is TLS 1.3 or TLS 1.2."));
1317 return 0;
Jerry Yu60835a82021-08-04 10:13:52 +08001318 }
1319#endif
1320
Gilles Peskine449bd832023-01-11 14:50:10 +01001321 MBEDTLS_SSL_DEBUG_MSG(1, ("The SSL configuration is invalid."));
1322 return MBEDTLS_ERR_SSL_BAD_CONFIG;
Jerry Yu60835a82021-08-04 10:13:52 +08001323}
1324
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001325MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu60835a82021-08-04 10:13:52 +08001326static int ssl_conf_check(const mbedtls_ssl_context *ssl)
1327{
1328 int ret;
Gilles Peskine449bd832023-01-11 14:50:10 +01001329 ret = ssl_conf_version_check(ssl);
1330 if (ret != 0) {
1331 return ret;
1332 }
Jerry Yu60835a82021-08-04 10:13:52 +08001333
Jerry Yudef7ae42022-10-30 14:13:19 +08001334#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1335 /* RFC 8446 section 4.4.3
1336 *
1337 * If the verification fails, the receiver MUST terminate the handshake with
1338 * a "decrypt_error" alert.
1339 *
1340 * If the client is configured as TLS 1.3 only with optional verify, return
1341 * bad config.
1342 *
1343 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001344 if (mbedtls_ssl_conf_tls13_ephemeral_enabled(
1345 (mbedtls_ssl_context *) ssl) &&
Jerry Yudef7ae42022-10-30 14:13:19 +08001346 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
1347 ssl->conf->max_tls_version == MBEDTLS_SSL_VERSION_TLS1_3 &&
1348 ssl->conf->min_tls_version == MBEDTLS_SSL_VERSION_TLS1_3 &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001349 ssl->conf->authmode == MBEDTLS_SSL_VERIFY_OPTIONAL) {
Jerry Yudef7ae42022-10-30 14:13:19 +08001350 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +01001351 1, ("Optional verify auth mode "
1352 "is not available for TLS 1.3 client"));
1353 return MBEDTLS_ERR_SSL_BAD_CONFIG;
Jerry Yudef7ae42022-10-30 14:13:19 +08001354 }
1355#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
1356
Jerry Yu60835a82021-08-04 10:13:52 +08001357 /* Space for further checks */
1358
Gilles Peskine449bd832023-01-11 14:50:10 +01001359 return 0;
Jerry Yu60835a82021-08-04 10:13:52 +08001360}
1361
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001362/*
1363 * Setup an SSL context
1364 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01001365
Gilles Peskine449bd832023-01-11 14:50:10 +01001366int mbedtls_ssl_setup(mbedtls_ssl_context *ssl,
1367 const mbedtls_ssl_config *conf)
Paul Bakker5121ce52009-01-03 21:22:43 +00001368{
Janos Follath865b3eb2019-12-16 11:46:15 +00001369 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Darryl Greenb33cc762019-11-28 14:29:44 +00001370 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1371 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00001372
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001373 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00001374
Gilles Peskine449bd832023-01-11 14:50:10 +01001375 if ((ret = ssl_conf_check(ssl)) != 0) {
1376 return ret;
1377 }
Jerry Yu60835a82021-08-04 10:13:52 +08001378
Paul Bakker62f2dee2012-09-28 07:31:51 +00001379 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001380 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00001381 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02001382
1383 /* Set to NULL in case of an error condition */
1384 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02001385
Darryl Greenb33cc762019-11-28 14:29:44 +00001386#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1387 ssl->in_buf_len = in_buf_len;
1388#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001389 ssl->in_buf = mbedtls_calloc(1, in_buf_len);
1390 if (ssl->in_buf == NULL) {
1391 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", in_buf_len));
k-stachowiak9f7798e2018-07-31 16:52:32 +02001392 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02001393 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10001394 }
1395
Darryl Greenb33cc762019-11-28 14:29:44 +00001396#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1397 ssl->out_buf_len = out_buf_len;
1398#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001399 ssl->out_buf = mbedtls_calloc(1, out_buf_len);
1400 if (ssl->out_buf == NULL) {
1401 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", out_buf_len));
k-stachowiak9f7798e2018-07-31 16:52:32 +02001402 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02001403 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00001404 }
1405
Gilles Peskine449bd832023-01-11 14:50:10 +01001406 mbedtls_ssl_reset_in_out_pointers(ssl);
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02001407
Johan Pascalb62bb512015-12-03 21:56:45 +01001408#if defined(MBEDTLS_SSL_DTLS_SRTP)
Gilles Peskine449bd832023-01-11 14:50:10 +01001409 memset(&ssl->dtls_srtp_info, 0, sizeof(ssl->dtls_srtp_info));
Johan Pascalb62bb512015-12-03 21:56:45 +01001410#endif
1411
Gilles Peskine449bd832023-01-11 14:50:10 +01001412 if ((ret = ssl_handshake_init(ssl)) != 0) {
k-stachowiaka47911c2018-07-04 17:41:58 +02001413 goto error;
Gilles Peskine449bd832023-01-11 14:50:10 +01001414 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001415
Gilles Peskine449bd832023-01-11 14:50:10 +01001416 return 0;
k-stachowiaka47911c2018-07-04 17:41:58 +02001417
1418error:
Gilles Peskine449bd832023-01-11 14:50:10 +01001419 mbedtls_free(ssl->in_buf);
1420 mbedtls_free(ssl->out_buf);
k-stachowiaka47911c2018-07-04 17:41:58 +02001421
1422 ssl->conf = NULL;
1423
Darryl Greenb33cc762019-11-28 14:29:44 +00001424#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1425 ssl->in_buf_len = 0;
1426 ssl->out_buf_len = 0;
1427#endif
k-stachowiaka47911c2018-07-04 17:41:58 +02001428 ssl->in_buf = NULL;
1429 ssl->out_buf = NULL;
1430
1431 ssl->in_hdr = NULL;
1432 ssl->in_ctr = NULL;
1433 ssl->in_len = NULL;
1434 ssl->in_iv = NULL;
1435 ssl->in_msg = NULL;
1436
1437 ssl->out_hdr = NULL;
1438 ssl->out_ctr = NULL;
1439 ssl->out_len = NULL;
1440 ssl->out_iv = NULL;
1441 ssl->out_msg = NULL;
1442
Gilles Peskine449bd832023-01-11 14:50:10 +01001443 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001444}
1445
1446/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00001447 * Reset an initialized and used SSL context for re-use while retaining
1448 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001449 *
1450 * If partial is non-zero, keep data in the input buffer and client ID.
1451 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00001452 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001453void mbedtls_ssl_session_reset_msg_layer(mbedtls_ssl_context *ssl,
1454 int partial)
Paul Bakker7eb013f2011-10-06 12:37:39 +00001455{
Darryl Greenb33cc762019-11-28 14:29:44 +00001456#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1457 size_t in_buf_len = ssl->in_buf_len;
1458 size_t out_buf_len = ssl->out_buf_len;
1459#else
1460 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1461 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
1462#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001463
Hanno Beckerb0302c42021-08-03 09:39:42 +01001464#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || !defined(MBEDTLS_SSL_SRV_C)
1465 partial = 0;
Hanno Becker7e772132018-08-10 12:38:21 +01001466#endif
1467
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001468 /* Cancel any possibly running timer */
Gilles Peskine449bd832023-01-11 14:50:10 +01001469 mbedtls_ssl_set_timer(ssl, 0);
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001470
Gilles Peskine449bd832023-01-11 14:50:10 +01001471 mbedtls_ssl_reset_in_out_pointers(ssl);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001472
1473 /* Reset incoming message parsing */
1474 ssl->in_offt = NULL;
1475 ssl->nb_zero = 0;
1476 ssl->in_msgtype = 0;
1477 ssl->in_msglen = 0;
1478 ssl->in_hslen = 0;
1479 ssl->keep_current_message = 0;
1480 ssl->transform_in = NULL;
1481
1482#if defined(MBEDTLS_SSL_PROTO_DTLS)
1483 ssl->next_record_offset = 0;
1484 ssl->in_epoch = 0;
1485#endif
1486
1487 /* Keep current datagram if partial == 1 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001488 if (partial == 0) {
Hanno Beckerb0302c42021-08-03 09:39:42 +01001489 ssl->in_left = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001490 memset(ssl->in_buf, 0, in_buf_len);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001491 }
1492
Ronald Cronad8c17b2022-06-10 17:18:09 +02001493 ssl->send_alert = 0;
1494
Hanno Beckerb0302c42021-08-03 09:39:42 +01001495 /* Reset outgoing message writing */
1496 ssl->out_msgtype = 0;
1497 ssl->out_msglen = 0;
1498 ssl->out_left = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001499 memset(ssl->out_buf, 0, out_buf_len);
1500 memset(ssl->cur_out_ctr, 0, sizeof(ssl->cur_out_ctr));
Hanno Beckerb0302c42021-08-03 09:39:42 +01001501 ssl->transform_out = NULL;
1502
1503#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine449bd832023-01-11 14:50:10 +01001504 mbedtls_ssl_dtls_replay_reset(ssl);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001505#endif
1506
XiaokangQian2b01dc32022-01-21 02:53:13 +00001507#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01001508 if (ssl->transform) {
1509 mbedtls_ssl_transform_free(ssl->transform);
1510 mbedtls_free(ssl->transform);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001511 ssl->transform = NULL;
1512 }
XiaokangQian2b01dc32022-01-21 02:53:13 +00001513#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1514
XiaokangQian2b01dc32022-01-21 02:53:13 +00001515#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001516 mbedtls_ssl_transform_free(ssl->transform_application);
1517 mbedtls_free(ssl->transform_application);
XiaokangQian2b01dc32022-01-21 02:53:13 +00001518 ssl->transform_application = NULL;
1519
Gilles Peskine449bd832023-01-11 14:50:10 +01001520 if (ssl->handshake != NULL) {
Jerry Yu3d9b5902022-11-04 14:07:25 +08001521#if defined(MBEDTLS_SSL_EARLY_DATA)
Gilles Peskine449bd832023-01-11 14:50:10 +01001522 mbedtls_ssl_transform_free(ssl->handshake->transform_earlydata);
1523 mbedtls_free(ssl->handshake->transform_earlydata);
XiaokangQian2b01dc32022-01-21 02:53:13 +00001524 ssl->handshake->transform_earlydata = NULL;
Jerry Yu3d9b5902022-11-04 14:07:25 +08001525#endif
XiaokangQian2b01dc32022-01-21 02:53:13 +00001526
Gilles Peskine449bd832023-01-11 14:50:10 +01001527 mbedtls_ssl_transform_free(ssl->handshake->transform_handshake);
1528 mbedtls_free(ssl->handshake->transform_handshake);
XiaokangQian2b01dc32022-01-21 02:53:13 +00001529 ssl->handshake->transform_handshake = NULL;
1530 }
1531
XiaokangQian2b01dc32022-01-21 02:53:13 +00001532#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerb0302c42021-08-03 09:39:42 +01001533}
1534
Gilles Peskine449bd832023-01-11 14:50:10 +01001535int mbedtls_ssl_session_reset_int(mbedtls_ssl_context *ssl, int partial)
Hanno Beckerb0302c42021-08-03 09:39:42 +01001536{
1537 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1538
1539 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
1540
Gilles Peskine449bd832023-01-11 14:50:10 +01001541 mbedtls_ssl_session_reset_msg_layer(ssl, partial);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001542
1543 /* Reset renegotiation state */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001544#if defined(MBEDTLS_SSL_RENEGOTIATION)
1545 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001546 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00001547
1548 ssl->verify_data_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001549 memset(ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN);
1550 memset(ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN);
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001551#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001552 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00001553
Hanno Beckerb0302c42021-08-03 09:39:42 +01001554 ssl->session_in = NULL;
Hanno Becker78640902018-08-13 16:35:15 +01001555 ssl->session_out = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001556 if (ssl->session) {
1557 mbedtls_ssl_session_free(ssl->session);
1558 mbedtls_free(ssl->session);
Paul Bakkerc0463502013-02-14 11:19:38 +01001559 ssl->session = NULL;
1560 }
1561
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001562#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02001563 ssl->alpn_chosen = NULL;
1564#endif
1565
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001566#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
David Horstmann3b2276a2022-10-06 14:49:08 +01001567 int free_cli_id = 1;
Hanno Becker4ccbf062018-08-10 11:20:38 +01001568#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Gilles Peskine449bd832023-01-11 14:50:10 +01001569 free_cli_id = (partial == 0);
Hanno Becker4ccbf062018-08-10 11:20:38 +01001570#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001571 if (free_cli_id) {
1572 mbedtls_free(ssl->cli_id);
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001573 ssl->cli_id = NULL;
1574 ssl->cli_id_len = 0;
1575 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02001576#endif
1577
Gilles Peskine449bd832023-01-11 14:50:10 +01001578 if ((ret = ssl_handshake_init(ssl)) != 0) {
1579 return ret;
1580 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00001581
Gilles Peskine449bd832023-01-11 14:50:10 +01001582 return 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00001583}
1584
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02001585/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001586 * Reset an initialized and used SSL context for re-use while retaining
1587 * all application-set variables, function pointers and data.
1588 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001589int mbedtls_ssl_session_reset(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001590{
Gilles Peskine449bd832023-01-11 14:50:10 +01001591 return mbedtls_ssl_session_reset_int(ssl, 0);
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001592}
1593
1594/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001595 * SSL set accessors
1596 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001597void mbedtls_ssl_conf_endpoint(mbedtls_ssl_config *conf, int endpoint)
Paul Bakker5121ce52009-01-03 21:22:43 +00001598{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001599 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00001600}
1601
Gilles Peskine449bd832023-01-11 14:50:10 +01001602void mbedtls_ssl_conf_transport(mbedtls_ssl_config *conf, int transport)
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001603{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001604 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001605}
1606
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001607#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine449bd832023-01-11 14:50:10 +01001608void mbedtls_ssl_conf_dtls_anti_replay(mbedtls_ssl_config *conf, char mode)
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001609{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001610 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001611}
1612#endif
1613
Gilles Peskine449bd832023-01-11 14:50:10 +01001614void mbedtls_ssl_conf_dtls_badmac_limit(mbedtls_ssl_config *conf, unsigned limit)
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001615{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001616 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001617}
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001618
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001619#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01001620
Gilles Peskine449bd832023-01-11 14:50:10 +01001621void mbedtls_ssl_set_datagram_packing(mbedtls_ssl_context *ssl,
1622 unsigned allow_packing)
Hanno Becker04da1892018-08-14 13:22:10 +01001623{
1624 ssl->disable_datagram_packing = !allow_packing;
1625}
1626
Gilles Peskine449bd832023-01-11 14:50:10 +01001627void mbedtls_ssl_conf_handshake_timeout(mbedtls_ssl_config *conf,
1628 uint32_t min, uint32_t max)
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001629{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001630 conf->hs_timeout_min = min;
1631 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001632}
1633#endif
1634
Gilles Peskine449bd832023-01-11 14:50:10 +01001635void mbedtls_ssl_conf_authmode(mbedtls_ssl_config *conf, int authmode)
Paul Bakker5121ce52009-01-03 21:22:43 +00001636{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001637 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00001638}
1639
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001640#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001641void mbedtls_ssl_conf_verify(mbedtls_ssl_config *conf,
1642 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
1643 void *p_vrfy)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001644{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001645 conf->f_vrfy = f_vrfy;
1646 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001647}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001648#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001649
Gilles Peskine449bd832023-01-11 14:50:10 +01001650void mbedtls_ssl_conf_rng(mbedtls_ssl_config *conf,
1651 int (*f_rng)(void *, unsigned char *, size_t),
1652 void *p_rng)
Paul Bakker5121ce52009-01-03 21:22:43 +00001653{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001654 conf->f_rng = f_rng;
1655 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00001656}
1657
Gilles Peskine449bd832023-01-11 14:50:10 +01001658void mbedtls_ssl_conf_dbg(mbedtls_ssl_config *conf,
1659 void (*f_dbg)(void *, int, const char *, int, const char *),
1660 void *p_dbg)
Paul Bakker5121ce52009-01-03 21:22:43 +00001661{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001662 conf->f_dbg = f_dbg;
1663 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00001664}
1665
Gilles Peskine449bd832023-01-11 14:50:10 +01001666void mbedtls_ssl_set_bio(mbedtls_ssl_context *ssl,
1667 void *p_bio,
1668 mbedtls_ssl_send_t *f_send,
1669 mbedtls_ssl_recv_t *f_recv,
1670 mbedtls_ssl_recv_timeout_t *f_recv_timeout)
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001671{
1672 ssl->p_bio = p_bio;
1673 ssl->f_send = f_send;
1674 ssl->f_recv = f_recv;
1675 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001676}
1677
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02001678#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001679void mbedtls_ssl_set_mtu(mbedtls_ssl_context *ssl, uint16_t mtu)
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02001680{
1681 ssl->mtu = mtu;
1682}
1683#endif
1684
Gilles Peskine449bd832023-01-11 14:50:10 +01001685void mbedtls_ssl_conf_read_timeout(mbedtls_ssl_config *conf, uint32_t timeout)
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001686{
1687 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001688}
1689
Gilles Peskine449bd832023-01-11 14:50:10 +01001690void mbedtls_ssl_set_timer_cb(mbedtls_ssl_context *ssl,
1691 void *p_timer,
1692 mbedtls_ssl_set_timer_t *f_set_timer,
1693 mbedtls_ssl_get_timer_t *f_get_timer)
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001694{
1695 ssl->p_timer = p_timer;
1696 ssl->f_set_timer = f_set_timer;
1697 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001698
1699 /* Make sure we start with no timer running */
Gilles Peskine449bd832023-01-11 14:50:10 +01001700 mbedtls_ssl_set_timer(ssl, 0);
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001701}
1702
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001703#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001704void mbedtls_ssl_conf_session_cache(mbedtls_ssl_config *conf,
1705 void *p_cache,
1706 mbedtls_ssl_cache_get_t *f_get_cache,
1707 mbedtls_ssl_cache_set_t *f_set_cache)
Paul Bakker5121ce52009-01-03 21:22:43 +00001708{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01001709 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001710 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001711 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00001712}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001713#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001714
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001715#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001716int mbedtls_ssl_set_session(mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session)
Paul Bakker5121ce52009-01-03 21:22:43 +00001717{
Janos Follath865b3eb2019-12-16 11:46:15 +00001718 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001719
Gilles Peskine449bd832023-01-11 14:50:10 +01001720 if (ssl == NULL ||
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001721 session == NULL ||
1722 ssl->session_negotiate == NULL ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001723 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT) {
1724 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001725 }
1726
Gilles Peskine449bd832023-01-11 14:50:10 +01001727 if (ssl->handshake->resume == 1) {
1728 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
1729 }
Hanno Beckere810bbc2021-05-14 16:01:05 +01001730
Jerry Yu21092062022-10-10 21:21:31 +08001731#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001732 if (session->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
Jerry Yu21092062022-10-10 21:21:31 +08001733 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01001734 mbedtls_ssl_ciphersuite_from_id(session->ciphersuite);
Jerry Yu21092062022-10-10 21:21:31 +08001735
Gilles Peskine449bd832023-01-11 14:50:10 +01001736 if (mbedtls_ssl_validate_ciphersuite(
Jerry Yu21092062022-10-10 21:21:31 +08001737 ssl, ciphersuite_info, MBEDTLS_SSL_VERSION_TLS1_3,
Gilles Peskine449bd832023-01-11 14:50:10 +01001738 MBEDTLS_SSL_VERSION_TLS1_3) != 0) {
1739 MBEDTLS_SSL_DEBUG_MSG(4, ("%d is not a valid TLS 1.3 ciphersuite.",
1740 session->ciphersuite));
1741 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jerry Yu21092062022-10-10 21:21:31 +08001742 }
Jerry Yu40afab62022-10-08 10:42:13 +08001743 }
Jerry Yu21092062022-10-10 21:21:31 +08001744#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu40afab62022-10-08 10:42:13 +08001745
Gilles Peskine449bd832023-01-11 14:50:10 +01001746 if ((ret = mbedtls_ssl_session_copy(ssl->session_negotiate,
1747 session)) != 0) {
1748 return ret;
1749 }
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001750
Paul Bakker0a597072012-09-25 21:55:46 +00001751 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001752
Gilles Peskine449bd832023-01-11 14:50:10 +01001753 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001754}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001755#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001756
Gilles Peskine449bd832023-01-11 14:50:10 +01001757void mbedtls_ssl_conf_ciphersuites(mbedtls_ssl_config *conf,
1758 const int *ciphersuites)
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001759{
Hanno Beckerd60b6c62021-04-29 12:04:11 +01001760 conf->ciphersuite_list = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00001761}
1762
Ronald Cron6f135e12021-12-08 16:57:54 +01001763#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001764void mbedtls_ssl_conf_tls13_key_exchange_modes(mbedtls_ssl_config *conf,
1765 const int kex_modes)
Hanno Becker71f1ed62021-07-24 06:01:47 +01001766{
Xiaofei Bai746f9482021-11-12 08:53:56 +00001767 conf->tls13_kex_modes = kex_modes & MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Hanno Becker71f1ed62021-07-24 06:01:47 +01001768}
Xiaokang Qian72de95d2022-10-25 02:54:33 +00001769
1770#if defined(MBEDTLS_SSL_EARLY_DATA)
Gilles Peskine449bd832023-01-11 14:50:10 +01001771void mbedtls_ssl_tls13_conf_early_data(mbedtls_ssl_config *conf,
1772 int early_data_enabled)
Xiaokang Qian72de95d2022-10-25 02:54:33 +00001773{
1774 conf->early_data_enabled = early_data_enabled;
1775}
Jerry Yucc4e0072022-11-22 17:22:22 +08001776
1777#if defined(MBEDTLS_SSL_SRV_C)
1778void mbedtls_ssl_tls13_conf_max_early_data_size(
Gilles Peskine449bd832023-01-11 14:50:10 +01001779 mbedtls_ssl_config *conf, uint32_t max_early_data_size)
Jerry Yucc4e0072022-11-22 17:22:22 +08001780{
Jerry Yu39da9852022-12-06 16:58:36 +08001781 conf->max_early_data_size = max_early_data_size;
Jerry Yucc4e0072022-11-22 17:22:22 +08001782}
1783#endif /* MBEDTLS_SSL_SRV_C */
1784
Xiaokang Qian72de95d2022-10-25 02:54:33 +00001785#endif /* MBEDTLS_SSL_EARLY_DATA */
Ronald Cron6f135e12021-12-08 16:57:54 +01001786#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01001787
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001788#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001789void mbedtls_ssl_conf_cert_profile(mbedtls_ssl_config *conf,
1790 const mbedtls_x509_crt_profile *profile)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02001791{
1792 conf->cert_profile = profile;
1793}
1794
Gilles Peskine449bd832023-01-11 14:50:10 +01001795static void ssl_key_cert_free(mbedtls_ssl_key_cert *key_cert)
Glenn Strauss36872db2022-01-22 05:06:31 -05001796{
1797 mbedtls_ssl_key_cert *cur = key_cert, *next;
1798
Gilles Peskine449bd832023-01-11 14:50:10 +01001799 while (cur != NULL) {
Glenn Strauss36872db2022-01-22 05:06:31 -05001800 next = cur->next;
Gilles Peskine449bd832023-01-11 14:50:10 +01001801 mbedtls_free(cur);
Glenn Strauss36872db2022-01-22 05:06:31 -05001802 cur = next;
1803 }
1804}
1805
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001806/* Append a new keycert entry to a (possibly empty) list */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001807MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001808static int ssl_append_key_cert(mbedtls_ssl_key_cert **head,
1809 mbedtls_x509_crt *cert,
1810 mbedtls_pk_context *key)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001811{
niisato8ee24222018-06-25 19:05:48 +09001812 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001813
Gilles Peskine449bd832023-01-11 14:50:10 +01001814 if (cert == NULL) {
Glenn Strauss36872db2022-01-22 05:06:31 -05001815 /* Free list if cert is null */
Gilles Peskine449bd832023-01-11 14:50:10 +01001816 ssl_key_cert_free(*head);
Glenn Strauss36872db2022-01-22 05:06:31 -05001817 *head = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001818 return 0;
Glenn Strauss36872db2022-01-22 05:06:31 -05001819 }
1820
Gilles Peskine449bd832023-01-11 14:50:10 +01001821 new_cert = mbedtls_calloc(1, sizeof(mbedtls_ssl_key_cert));
1822 if (new_cert == NULL) {
1823 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
1824 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001825
niisato8ee24222018-06-25 19:05:48 +09001826 new_cert->cert = cert;
1827 new_cert->key = key;
1828 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001829
Glenn Strauss36872db2022-01-22 05:06:31 -05001830 /* Update head if the list was null, else add to the end */
Gilles Peskine449bd832023-01-11 14:50:10 +01001831 if (*head == NULL) {
niisato8ee24222018-06-25 19:05:48 +09001832 *head = new_cert;
Gilles Peskine449bd832023-01-11 14:50:10 +01001833 } else {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001834 mbedtls_ssl_key_cert *cur = *head;
Gilles Peskine449bd832023-01-11 14:50:10 +01001835 while (cur->next != NULL) {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001836 cur = cur->next;
Gilles Peskine449bd832023-01-11 14:50:10 +01001837 }
niisato8ee24222018-06-25 19:05:48 +09001838 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001839 }
1840
Gilles Peskine449bd832023-01-11 14:50:10 +01001841 return 0;
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001842}
1843
Gilles Peskine449bd832023-01-11 14:50:10 +01001844int mbedtls_ssl_conf_own_cert(mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001845 mbedtls_x509_crt *own_cert,
Gilles Peskine449bd832023-01-11 14:50:10 +01001846 mbedtls_pk_context *pk_key)
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001847{
Gilles Peskine449bd832023-01-11 14:50:10 +01001848 return ssl_append_key_cert(&conf->key_cert, own_cert, pk_key);
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001849}
1850
Gilles Peskine449bd832023-01-11 14:50:10 +01001851void mbedtls_ssl_conf_ca_chain(mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001852 mbedtls_x509_crt *ca_chain,
Gilles Peskine449bd832023-01-11 14:50:10 +01001853 mbedtls_x509_crl *ca_crl)
Paul Bakker5121ce52009-01-03 21:22:43 +00001854{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001855 conf->ca_chain = ca_chain;
1856 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00001857
1858#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1859 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1860 * cannot be used together. */
1861 conf->f_ca_cb = NULL;
1862 conf->p_ca_cb = NULL;
1863#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00001864}
Hanno Becker5adaad92019-03-27 16:54:37 +00001865
1866#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Gilles Peskine449bd832023-01-11 14:50:10 +01001867void mbedtls_ssl_conf_ca_cb(mbedtls_ssl_config *conf,
1868 mbedtls_x509_crt_ca_cb_t f_ca_cb,
1869 void *p_ca_cb)
Hanno Becker5adaad92019-03-27 16:54:37 +00001870{
1871 conf->f_ca_cb = f_ca_cb;
1872 conf->p_ca_cb = p_ca_cb;
1873
1874 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1875 * cannot be used together. */
1876 conf->ca_chain = NULL;
1877 conf->ca_crl = NULL;
1878}
1879#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001880#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00001881
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001882#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01001883const unsigned char *mbedtls_ssl_get_hs_sni(mbedtls_ssl_context *ssl,
1884 size_t *name_len)
Glenn Strauss69894072022-01-24 12:58:00 -05001885{
1886 *name_len = ssl->handshake->sni_name_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001887 return ssl->handshake->sni_name;
Glenn Strauss69894072022-01-24 12:58:00 -05001888}
1889
Gilles Peskine449bd832023-01-11 14:50:10 +01001890int mbedtls_ssl_set_hs_own_cert(mbedtls_ssl_context *ssl,
1891 mbedtls_x509_crt *own_cert,
1892 mbedtls_pk_context *pk_key)
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001893{
Gilles Peskine449bd832023-01-11 14:50:10 +01001894 return ssl_append_key_cert(&ssl->handshake->sni_key_cert,
1895 own_cert, pk_key);
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001896}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02001897
Gilles Peskine449bd832023-01-11 14:50:10 +01001898void mbedtls_ssl_set_hs_ca_chain(mbedtls_ssl_context *ssl,
1899 mbedtls_x509_crt *ca_chain,
1900 mbedtls_x509_crl *ca_crl)
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02001901{
1902 ssl->handshake->sni_ca_chain = ca_chain;
1903 ssl->handshake->sni_ca_crl = ca_crl;
1904}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001905
Glenn Strauss999ef702022-03-11 01:37:23 -05001906#if defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001907void mbedtls_ssl_set_hs_dn_hints(mbedtls_ssl_context *ssl,
1908 const mbedtls_x509_crt *crt)
Glenn Strauss999ef702022-03-11 01:37:23 -05001909{
1910 ssl->handshake->dn_hints = crt;
1911}
1912#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
1913
Gilles Peskine449bd832023-01-11 14:50:10 +01001914void mbedtls_ssl_set_hs_authmode(mbedtls_ssl_context *ssl,
1915 int authmode)
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001916{
1917 ssl->handshake->sni_authmode = authmode;
1918}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001919#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
1920
Hanno Becker8927c832019-04-03 12:52:50 +01001921#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001922void mbedtls_ssl_set_verify(mbedtls_ssl_context *ssl,
1923 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
1924 void *p_vrfy)
Hanno Becker8927c832019-04-03 12:52:50 +01001925{
1926 ssl->f_vrfy = f_vrfy;
1927 ssl->p_vrfy = p_vrfy;
1928}
1929#endif
1930
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02001931#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001932
Valerio Setti016f6822022-12-09 14:17:50 +01001933#if defined(MBEDTLS_USE_PSA_CRYPTO)
1934static psa_status_t mbedtls_ssl_set_hs_ecjpake_password_common(
Gilles Peskine449bd832023-01-11 14:50:10 +01001935 mbedtls_ssl_context *ssl,
1936 mbedtls_svc_key_id_t pwd)
Valerio Setti016f6822022-12-09 14:17:50 +01001937{
1938 psa_status_t status;
1939 psa_pake_role_t psa_role;
1940 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
1941
Gilles Peskine449bd832023-01-11 14:50:10 +01001942 psa_pake_cs_set_algorithm(&cipher_suite, PSA_ALG_JPAKE);
1943 psa_pake_cs_set_primitive(&cipher_suite,
1944 PSA_PAKE_PRIMITIVE(PSA_PAKE_PRIMITIVE_TYPE_ECC,
1945 PSA_ECC_FAMILY_SECP_R1,
1946 256));
1947 psa_pake_cs_set_hash(&cipher_suite, PSA_ALG_SHA_256);
Valerio Setti016f6822022-12-09 14:17:50 +01001948
Gilles Peskine449bd832023-01-11 14:50:10 +01001949 status = psa_pake_setup(&ssl->handshake->psa_pake_ctx, &cipher_suite);
1950 if (status != PSA_SUCCESS) {
Valerio Setti016f6822022-12-09 14:17:50 +01001951 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001952 }
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001953
Gilles Peskine449bd832023-01-11 14:50:10 +01001954 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Neil Armstrongca7d5062022-05-31 14:43:23 +02001955 psa_role = PSA_PAKE_ROLE_SERVER;
Gilles Peskine449bd832023-01-11 14:50:10 +01001956 } else {
Neil Armstrongca7d5062022-05-31 14:43:23 +02001957 psa_role = PSA_PAKE_ROLE_CLIENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01001958 }
Neil Armstrongca7d5062022-05-31 14:43:23 +02001959
Gilles Peskine449bd832023-01-11 14:50:10 +01001960 status = psa_pake_set_role(&ssl->handshake->psa_pake_ctx, psa_role);
1961 if (status != PSA_SUCCESS) {
Valerio Setti016f6822022-12-09 14:17:50 +01001962 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001963 }
Valerio Setti016f6822022-12-09 14:17:50 +01001964
Gilles Peskine449bd832023-01-11 14:50:10 +01001965 status = psa_pake_set_password_key(&ssl->handshake->psa_pake_ctx, pwd);
1966 if (status != PSA_SUCCESS) {
Valerio Setti016f6822022-12-09 14:17:50 +01001967 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001968 }
Valerio Setti016f6822022-12-09 14:17:50 +01001969
1970 ssl->handshake->psa_pake_ctx_is_ok = 1;
1971
Gilles Peskine449bd832023-01-11 14:50:10 +01001972 return PSA_SUCCESS;
Valerio Setti016f6822022-12-09 14:17:50 +01001973}
1974
Gilles Peskine449bd832023-01-11 14:50:10 +01001975int mbedtls_ssl_set_hs_ecjpake_password(mbedtls_ssl_context *ssl,
1976 const unsigned char *pw,
1977 size_t pw_len)
Valerio Setti016f6822022-12-09 14:17:50 +01001978{
1979 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
1980 psa_status_t status;
1981
Gilles Peskine449bd832023-01-11 14:50:10 +01001982 if (ssl->handshake == NULL || ssl->conf == NULL) {
1983 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Neil Armstrongca7d5062022-05-31 14:43:23 +02001984 }
1985
Gilles Peskine449bd832023-01-11 14:50:10 +01001986 /* Empty password is not valid */
1987 if ((pw == NULL) || (pw_len == 0)) {
1988 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
1989 }
1990
1991 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
1992 psa_set_key_algorithm(&attributes, PSA_ALG_JPAKE);
1993 psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD);
1994
1995 status = psa_import_key(&attributes, pw, pw_len,
1996 &ssl->handshake->psa_pake_password);
1997 if (status != PSA_SUCCESS) {
1998 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
1999 }
2000
2001 status = mbedtls_ssl_set_hs_ecjpake_password_common(ssl,
2002 ssl->handshake->psa_pake_password);
2003 if (status != PSA_SUCCESS) {
2004 psa_destroy_key(ssl->handshake->psa_pake_password);
2005 psa_pake_abort(&ssl->handshake->psa_pake_ctx);
2006 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2007 }
2008
2009 return 0;
Valerio Setti02c25b52022-11-15 14:08:42 +01002010}
Valerio Settia9a97dc2022-11-28 18:26:16 +01002011
Gilles Peskine449bd832023-01-11 14:50:10 +01002012int mbedtls_ssl_set_hs_ecjpake_password_opaque(mbedtls_ssl_context *ssl,
2013 mbedtls_svc_key_id_t pwd)
Valerio Settia9a97dc2022-11-28 18:26:16 +01002014{
Valerio Settia9a97dc2022-11-28 18:26:16 +01002015 psa_status_t status;
2016
Gilles Peskine449bd832023-01-11 14:50:10 +01002017 if (ssl->handshake == NULL || ssl->conf == NULL) {
2018 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Neil Armstrongca7d5062022-05-31 14:43:23 +02002019 }
2020
Gilles Peskine449bd832023-01-11 14:50:10 +01002021 if (mbedtls_svc_key_id_is_null(pwd)) {
2022 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2023 }
2024
2025 status = mbedtls_ssl_set_hs_ecjpake_password_common(ssl, pwd);
2026 if (status != PSA_SUCCESS) {
2027 psa_pake_abort(&ssl->handshake->psa_pake_ctx);
2028 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2029 }
2030
2031 return 0;
Valerio Setti02c25b52022-11-15 14:08:42 +01002032}
2033#else /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01002034int mbedtls_ssl_set_hs_ecjpake_password(mbedtls_ssl_context *ssl,
2035 const unsigned char *pw,
2036 size_t pw_len)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002037{
2038 mbedtls_ecjpake_role role;
2039
Gilles Peskine449bd832023-01-11 14:50:10 +01002040 if (ssl->handshake == NULL || ssl->conf == NULL) {
2041 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2042 }
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002043
Valerio Settic689ed82022-12-07 14:40:38 +01002044 /* Empty password is not valid */
Gilles Peskine449bd832023-01-11 14:50:10 +01002045 if ((pw == NULL) || (pw_len == 0)) {
2046 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2047 }
Valerio Settic689ed82022-12-07 14:40:38 +01002048
Gilles Peskine449bd832023-01-11 14:50:10 +01002049 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002050 role = MBEDTLS_ECJPAKE_SERVER;
Gilles Peskine449bd832023-01-11 14:50:10 +01002051 } else {
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002052 role = MBEDTLS_ECJPAKE_CLIENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01002053 }
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002054
Gilles Peskine449bd832023-01-11 14:50:10 +01002055 return mbedtls_ecjpake_setup(&ssl->handshake->ecjpake_ctx,
2056 role,
2057 MBEDTLS_MD_SHA256,
2058 MBEDTLS_ECP_DP_SECP256R1,
2059 pw, pw_len);
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002060}
Valerio Setti02c25b52022-11-15 14:08:42 +01002061#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02002062#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002063
Ronald Cron73fe8df2022-10-05 14:31:43 +02002064#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002065int mbedtls_ssl_conf_has_static_psk(mbedtls_ssl_config const *conf)
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002066{
Gilles Peskine449bd832023-01-11 14:50:10 +01002067 if (conf->psk_identity == NULL ||
2068 conf->psk_identity_len == 0) {
2069 return 0;
Ronald Crond29e13e2022-10-19 10:33:48 +02002070 }
2071
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002072#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002073 if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) {
2074 return 1;
2075 }
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002076#endif /* MBEDTLS_USE_PSA_CRYPTO */
Ronald Crond29e13e2022-10-19 10:33:48 +02002077
Gilles Peskine449bd832023-01-11 14:50:10 +01002078 if (conf->psk != NULL && conf->psk_len != 0) {
2079 return 1;
2080 }
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002081
Gilles Peskine449bd832023-01-11 14:50:10 +01002082 return 0;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002083}
2084
Gilles Peskine449bd832023-01-11 14:50:10 +01002085static void ssl_conf_remove_psk(mbedtls_ssl_config *conf)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002086{
2087 /* Remove reference to existing PSK, if any. */
2088#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002089 if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) {
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002090 /* The maintenance of the PSK key slot is the
2091 * user's responsibility. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002092 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002093 }
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002094#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01002095 if (conf->psk != NULL) {
2096 mbedtls_platform_zeroize(conf->psk, conf->psk_len);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002097
Gilles Peskine449bd832023-01-11 14:50:10 +01002098 mbedtls_free(conf->psk);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002099 conf->psk = NULL;
2100 conf->psk_len = 0;
2101 }
2102
2103 /* Remove reference to PSK identity, if any. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002104 if (conf->psk_identity != NULL) {
2105 mbedtls_free(conf->psk_identity);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002106 conf->psk_identity = NULL;
2107 conf->psk_identity_len = 0;
2108 }
2109}
2110
Hanno Becker7390c712018-11-15 13:33:04 +00002111/* This function assumes that PSK identity in the SSL config is unset.
2112 * It checks that the provided identity is well-formed and attempts
2113 * to make a copy of it in the SSL config.
2114 * On failure, the PSK identity in the config remains unset. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002115MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002116static int ssl_conf_set_psk_identity(mbedtls_ssl_config *conf,
2117 unsigned char const *psk_identity,
2118 size_t psk_identity_len)
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002119{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02002120 /* Identity len will be encoded on two bytes */
Gilles Peskine449bd832023-01-11 14:50:10 +01002121 if (psk_identity == NULL ||
Ronald Cron2a87e9b2022-10-19 10:55:26 +02002122 psk_identity_len == 0 ||
Gilles Peskine449bd832023-01-11 14:50:10 +01002123 (psk_identity_len >> 16) != 0 ||
2124 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN) {
2125 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02002126 }
2127
Gilles Peskine449bd832023-01-11 14:50:10 +01002128 conf->psk_identity = mbedtls_calloc(1, psk_identity_len);
2129 if (conf->psk_identity == NULL) {
2130 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2131 }
Paul Bakker6db455e2013-09-18 17:29:31 +02002132
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01002133 conf->psk_identity_len = psk_identity_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002134 memcpy(conf->psk_identity, psk_identity, conf->psk_identity_len);
Paul Bakker5ad403f2013-09-18 21:21:30 +02002135
Gilles Peskine449bd832023-01-11 14:50:10 +01002136 return 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02002137}
2138
Gilles Peskine449bd832023-01-11 14:50:10 +01002139int mbedtls_ssl_conf_psk(mbedtls_ssl_config *conf,
2140 const unsigned char *psk, size_t psk_len,
2141 const unsigned char *psk_identity, size_t psk_identity_len)
Hanno Becker7390c712018-11-15 13:33:04 +00002142{
Janos Follath865b3eb2019-12-16 11:46:15 +00002143 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002144
2145 /* We currently only support one PSK, raw or opaque. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002146 if (mbedtls_ssl_conf_has_static_psk(conf)) {
2147 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
2148 }
Hanno Becker7390c712018-11-15 13:33:04 +00002149
2150 /* Check and set raw PSK */
Gilles Peskine449bd832023-01-11 14:50:10 +01002151 if (psk == NULL) {
2152 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2153 }
2154 if (psk_len == 0) {
2155 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2156 }
2157 if (psk_len > MBEDTLS_PSK_MAX_LEN) {
2158 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2159 }
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01002160
Gilles Peskine449bd832023-01-11 14:50:10 +01002161 if ((conf->psk = mbedtls_calloc(1, psk_len)) == NULL) {
2162 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2163 }
Hanno Becker7390c712018-11-15 13:33:04 +00002164 conf->psk_len = psk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002165 memcpy(conf->psk, psk, conf->psk_len);
Hanno Becker7390c712018-11-15 13:33:04 +00002166
2167 /* Check and set PSK Identity */
Gilles Peskine449bd832023-01-11 14:50:10 +01002168 ret = ssl_conf_set_psk_identity(conf, psk_identity, psk_identity_len);
2169 if (ret != 0) {
2170 ssl_conf_remove_psk(conf);
2171 }
Hanno Becker7390c712018-11-15 13:33:04 +00002172
Gilles Peskine449bd832023-01-11 14:50:10 +01002173 return ret;
Hanno Becker7390c712018-11-15 13:33:04 +00002174}
2175
Gilles Peskine449bd832023-01-11 14:50:10 +01002176static void ssl_remove_psk(mbedtls_ssl_context *ssl)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002177{
2178#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002179 if (!mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque)) {
Neil Armstrong501c9322022-05-03 09:35:09 +02002180 /* The maintenance of the external PSK key slot is the
2181 * user's responsibility. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002182 if (ssl->handshake->psk_opaque_is_internal) {
2183 psa_destroy_key(ssl->handshake->psk_opaque);
Neil Armstrong501c9322022-05-03 09:35:09 +02002184 ssl->handshake->psk_opaque_is_internal = 0;
2185 }
Ronald Croncf56a0a2020-08-04 09:51:30 +02002186 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002187 }
Neil Armstronge952a302022-05-03 10:22:14 +02002188#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002189 if (ssl->handshake->psk != NULL) {
2190 mbedtls_platform_zeroize(ssl->handshake->psk,
2191 ssl->handshake->psk_len);
2192 mbedtls_free(ssl->handshake->psk);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002193 ssl->handshake->psk_len = 0;
2194 }
Neil Armstronge952a302022-05-03 10:22:14 +02002195#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002196}
2197
Gilles Peskine449bd832023-01-11 14:50:10 +01002198int mbedtls_ssl_set_hs_psk(mbedtls_ssl_context *ssl,
2199 const unsigned char *psk, size_t psk_len)
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002200{
Neil Armstrong501c9322022-05-03 09:35:09 +02002201#if defined(MBEDTLS_USE_PSA_CRYPTO)
2202 psa_key_attributes_t key_attributes = psa_key_attributes_init();
Jerry Yu5d01c052022-08-17 10:18:10 +08002203 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jerry Yu24b8c812022-08-20 19:06:56 +08002204 psa_algorithm_t alg = PSA_ALG_NONE;
Jerry Yu5d01c052022-08-17 10:18:10 +08002205 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrong501c9322022-05-03 09:35:09 +02002206#endif /* MBEDTLS_USE_PSA_CRYPTO */
2207
Gilles Peskine449bd832023-01-11 14:50:10 +01002208 if (psk == NULL || ssl->handshake == NULL) {
2209 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2210 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002211
Gilles Peskine449bd832023-01-11 14:50:10 +01002212 if (psk_len > MBEDTLS_PSK_MAX_LEN) {
2213 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2214 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002215
Gilles Peskine449bd832023-01-11 14:50:10 +01002216 ssl_remove_psk(ssl);
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002217
Neil Armstrong501c9322022-05-03 09:35:09 +02002218#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yuccc68a42022-07-26 16:39:20 +08002219#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01002220 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2) {
2221 if (ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384) {
2222 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
2223 } else {
2224 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
2225 }
2226 psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE);
Jerry Yuccc68a42022-07-26 16:39:20 +08002227 }
2228#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Neil Armstrong501c9322022-05-03 09:35:09 +02002229
Jerry Yu568ec252022-07-22 21:27:34 +08002230#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01002231 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
2232 alg = PSA_ALG_HKDF_EXTRACT(PSA_ALG_ANY_HASH);
2233 psa_set_key_usage_flags(&key_attributes,
2234 PSA_KEY_USAGE_DERIVE | PSA_KEY_USAGE_EXPORT);
Jerry Yuccc68a42022-07-26 16:39:20 +08002235 }
2236#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
2237
Gilles Peskine449bd832023-01-11 14:50:10 +01002238 psa_set_key_algorithm(&key_attributes, alg);
2239 psa_set_key_type(&key_attributes, PSA_KEY_TYPE_DERIVE);
Neil Armstrong501c9322022-05-03 09:35:09 +02002240
Gilles Peskine449bd832023-01-11 14:50:10 +01002241 status = psa_import_key(&key_attributes, psk, psk_len, &key);
2242 if (status != PSA_SUCCESS) {
2243 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2244 }
Neil Armstrong501c9322022-05-03 09:35:09 +02002245
2246 /* Allow calling psa_destroy_key() on psk remove */
2247 ssl->handshake->psk_opaque_is_internal = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01002248 return mbedtls_ssl_set_hs_psk_opaque(ssl, key);
Neil Armstrong501c9322022-05-03 09:35:09 +02002249#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002250 if ((ssl->handshake->psk = mbedtls_calloc(1, psk_len)) == NULL) {
2251 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2252 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002253
2254 ssl->handshake->psk_len = psk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002255 memcpy(ssl->handshake->psk, psk, ssl->handshake->psk_len);
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002256
Gilles Peskine449bd832023-01-11 14:50:10 +01002257 return 0;
Neil Armstrong501c9322022-05-03 09:35:09 +02002258#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002259}
2260
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002261#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002262int mbedtls_ssl_conf_psk_opaque(mbedtls_ssl_config *conf,
2263 mbedtls_svc_key_id_t psk,
2264 const unsigned char *psk_identity,
2265 size_t psk_identity_len)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002266{
Janos Follath865b3eb2019-12-16 11:46:15 +00002267 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002268
2269 /* We currently only support one PSK, raw or opaque. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002270 if (mbedtls_ssl_conf_has_static_psk(conf)) {
2271 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
2272 }
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002273
Hanno Becker7390c712018-11-15 13:33:04 +00002274 /* Check and set opaque PSK */
Gilles Peskine449bd832023-01-11 14:50:10 +01002275 if (mbedtls_svc_key_id_is_null(psk)) {
2276 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2277 }
Ronald Croncf56a0a2020-08-04 09:51:30 +02002278 conf->psk_opaque = psk;
Hanno Becker7390c712018-11-15 13:33:04 +00002279
2280 /* Check and set PSK Identity */
Gilles Peskine449bd832023-01-11 14:50:10 +01002281 ret = ssl_conf_set_psk_identity(conf, psk_identity,
2282 psk_identity_len);
2283 if (ret != 0) {
2284 ssl_conf_remove_psk(conf);
2285 }
Hanno Becker7390c712018-11-15 13:33:04 +00002286
Gilles Peskine449bd832023-01-11 14:50:10 +01002287 return ret;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002288}
2289
Gilles Peskine449bd832023-01-11 14:50:10 +01002290int mbedtls_ssl_set_hs_psk_opaque(mbedtls_ssl_context *ssl,
2291 mbedtls_svc_key_id_t psk)
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002292{
Gilles Peskine449bd832023-01-11 14:50:10 +01002293 if ((mbedtls_svc_key_id_is_null(psk)) ||
2294 (ssl->handshake == NULL)) {
2295 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2296 }
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002297
Gilles Peskine449bd832023-01-11 14:50:10 +01002298 ssl_remove_psk(ssl);
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002299 ssl->handshake->psk_opaque = psk;
Gilles Peskine449bd832023-01-11 14:50:10 +01002300 return 0;
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002301}
2302#endif /* MBEDTLS_USE_PSA_CRYPTO */
2303
Jerry Yu8897c072022-08-12 13:56:53 +08002304#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002305void mbedtls_ssl_conf_psk_cb(mbedtls_ssl_config *conf,
2306 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
2307 size_t),
2308 void *p_psk)
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002309{
2310 conf->f_psk = f_psk;
2311 conf->p_psk = p_psk;
2312}
Jerry Yu8897c072022-08-12 13:56:53 +08002313#endif /* MBEDTLS_SSL_SRV_C */
2314
Ronald Cron73fe8df2022-10-05 14:31:43 +02002315#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002316
2317#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine301711e2022-04-26 16:57:05 +02002318static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
Gilles Peskine449bd832023-01-11 14:50:10 +01002319 psa_algorithm_t alg)
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002320{
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002321#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01002322 if (alg == PSA_ALG_CBC_NO_PADDING) {
2323 return MBEDTLS_SSL_MODE_CBC;
2324 }
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002325#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
Gilles Peskine449bd832023-01-11 14:50:10 +01002326 if (PSA_ALG_IS_AEAD(alg)) {
2327 return MBEDTLS_SSL_MODE_AEAD;
2328 }
2329 return MBEDTLS_SSL_MODE_STREAM;
Gilles Peskine301711e2022-04-26 16:57:05 +02002330}
2331
2332#else /* MBEDTLS_USE_PSA_CRYPTO */
2333
2334static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
Gilles Peskine449bd832023-01-11 14:50:10 +01002335 mbedtls_cipher_mode_t mode)
Gilles Peskine301711e2022-04-26 16:57:05 +02002336{
2337#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01002338 if (mode == MBEDTLS_MODE_CBC) {
2339 return MBEDTLS_SSL_MODE_CBC;
2340 }
Gilles Peskine301711e2022-04-26 16:57:05 +02002341#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
2342
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002343#if defined(MBEDTLS_GCM_C) || \
2344 defined(MBEDTLS_CCM_C) || \
2345 defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002346 if (mode == MBEDTLS_MODE_GCM ||
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002347 mode == MBEDTLS_MODE_CCM ||
Gilles Peskine449bd832023-01-11 14:50:10 +01002348 mode == MBEDTLS_MODE_CHACHAPOLY) {
2349 return MBEDTLS_SSL_MODE_AEAD;
2350 }
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002351#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002352
Gilles Peskine449bd832023-01-11 14:50:10 +01002353 return MBEDTLS_SSL_MODE_STREAM;
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002354}
Gilles Peskine301711e2022-04-26 16:57:05 +02002355#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002356
Gilles Peskinee108d982022-04-26 16:50:40 +02002357static mbedtls_ssl_mode_t mbedtls_ssl_get_actual_mode(
2358 mbedtls_ssl_mode_t base_mode,
Gilles Peskine449bd832023-01-11 14:50:10 +01002359 int encrypt_then_mac)
Gilles Peskinee108d982022-04-26 16:50:40 +02002360{
2361#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01002362 if (encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED &&
2363 base_mode == MBEDTLS_SSL_MODE_CBC) {
2364 return MBEDTLS_SSL_MODE_CBC_ETM;
Gilles Peskinee108d982022-04-26 16:50:40 +02002365 }
2366#else
2367 (void) encrypt_then_mac;
2368#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002369 return base_mode;
Gilles Peskinee108d982022-04-26 16:50:40 +02002370}
2371
Neil Armstrongab555e02022-04-04 11:07:59 +02002372mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_transform(
Gilles Peskine449bd832023-01-11 14:50:10 +01002373 const mbedtls_ssl_transform *transform)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002374{
Gilles Peskinee108d982022-04-26 16:50:40 +02002375 mbedtls_ssl_mode_t base_mode = mbedtls_ssl_get_base_mode(
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002376#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002377 transform->psa_alg
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002378#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002379 mbedtls_cipher_get_cipher_mode(&transform->cipher_ctx_enc)
Gilles Peskinee108d982022-04-26 16:50:40 +02002380#endif
2381 );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002382
Gilles Peskinee108d982022-04-26 16:50:40 +02002383 int encrypt_then_mac = 0;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002384#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskinee108d982022-04-26 16:50:40 +02002385 encrypt_then_mac = transform->encrypt_then_mac;
2386#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002387 return mbedtls_ssl_get_actual_mode(base_mode, encrypt_then_mac);
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002388}
2389
Neil Armstrongab555e02022-04-04 11:07:59 +02002390mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002391#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01002392 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002393#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01002394 const mbedtls_ssl_ciphersuite_t *suite)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002395{
Gilles Peskinee108d982022-04-26 16:50:40 +02002396 mbedtls_ssl_mode_t base_mode = MBEDTLS_SSL_MODE_STREAM;
2397
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002398#if defined(MBEDTLS_USE_PSA_CRYPTO)
2399 psa_status_t status;
2400 psa_algorithm_t alg;
2401 psa_key_type_t type;
2402 size_t size;
Gilles Peskine449bd832023-01-11 14:50:10 +01002403 status = mbedtls_ssl_cipher_to_psa(suite->cipher, 0, &alg, &type, &size);
2404 if (status == PSA_SUCCESS) {
2405 base_mode = mbedtls_ssl_get_base_mode(alg);
2406 }
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002407#else
2408 const mbedtls_cipher_info_t *cipher =
Gilles Peskine449bd832023-01-11 14:50:10 +01002409 mbedtls_cipher_info_from_type(suite->cipher);
2410 if (cipher != NULL) {
Gilles Peskinee108d982022-04-26 16:50:40 +02002411 base_mode =
2412 mbedtls_ssl_get_base_mode(
Gilles Peskine449bd832023-01-11 14:50:10 +01002413 mbedtls_cipher_info_get_mode(cipher));
Gilles Peskinee108d982022-04-26 16:50:40 +02002414 }
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002415#endif /* MBEDTLS_USE_PSA_CRYPTO */
2416
Gilles Peskinee108d982022-04-26 16:50:40 +02002417#if !defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
2418 int encrypt_then_mac = 0;
2419#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002420 return mbedtls_ssl_get_actual_mode(base_mode, encrypt_then_mac);
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002421}
2422
Neil Armstrong8395d7a2022-05-18 11:44:56 +02002423#if defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu251a12e2022-07-13 15:15:48 +08002424
2425#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu438ddd82022-07-07 06:55:50 +00002426/* Serialization of TLS 1.3 sessions:
2427 *
2428 * struct {
Xiaokang Qian126bf8e2022-10-13 02:22:40 +00002429 * opaque hostname<0..2^16-1>;
Jerry Yu438ddd82022-07-07 06:55:50 +00002430 * uint64 ticket_received;
2431 * uint32 ticket_lifetime;
Jerry Yu34191072022-08-18 10:32:09 +08002432 * opaque ticket<1..2^16-1>;
Jerry Yu438ddd82022-07-07 06:55:50 +00002433 * } ClientOnlyData;
2434 *
2435 * struct {
2436 * uint8 endpoint;
2437 * uint8 ciphersuite[2];
2438 * uint32 ticket_age_add;
2439 * uint8 ticket_flags;
2440 * opaque resumption_key<0..255>;
2441 * select ( endpoint ) {
2442 * case client: ClientOnlyData;
2443 * case server: uint64 start_time;
2444 * };
2445 * } serialized_session_tls13;
2446 *
2447 */
2448#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yue36fdd62022-08-17 21:31:36 +08002449MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002450static int ssl_tls13_session_save(const mbedtls_ssl_session *session,
2451 unsigned char *buf,
2452 size_t buf_len,
2453 size_t *olen)
Jerry Yu438ddd82022-07-07 06:55:50 +00002454{
2455 unsigned char *p = buf;
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00002456#if defined(MBEDTLS_SSL_CLI_C) && \
2457 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01002458 size_t hostname_len = (session->hostname == NULL) ?
2459 0 : strlen(session->hostname) + 1;
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00002460#endif
Jerry Yubc7c1a42022-07-21 22:57:37 +08002461 size_t needed = 1 /* endpoint */
2462 + 2 /* ciphersuite */
2463 + 4 /* ticket_age_add */
Jerry Yu34191072022-08-18 10:32:09 +08002464 + 1 /* ticket_flags */
2465 + 1; /* resumption_key length */
Jerry Yue36fdd62022-08-17 21:31:36 +08002466 *olen = 0;
Jerry Yu34191072022-08-18 10:32:09 +08002467
Gilles Peskine449bd832023-01-11 14:50:10 +01002468 if (session->resumption_key_len > MBEDTLS_SSL_TLS1_3_TICKET_RESUMPTION_KEY_LEN) {
2469 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2470 }
Jerry Yu34191072022-08-18 10:32:09 +08002471 needed += session->resumption_key_len; /* resumption_key */
2472
Jerry Yu438ddd82022-07-07 06:55:50 +00002473#if defined(MBEDTLS_HAVE_TIME)
2474 needed += 8; /* start_time or ticket_received */
2475#endif
2476
2477#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002478 if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Xiaokang Qian126bf8e2022-10-13 02:22:40 +00002479#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaokang Qianbc663a02022-10-09 11:14:39 +00002480 needed += 2 /* hostname_len */
Gilles Peskine449bd832023-01-11 14:50:10 +01002481 + hostname_len; /* hostname */
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00002482#endif
2483
Jerry Yu438ddd82022-07-07 06:55:50 +00002484 needed += 4 /* ticket_lifetime */
Jerry Yue36fdd62022-08-17 21:31:36 +08002485 + 2; /* ticket_len */
Jerry Yu34191072022-08-18 10:32:09 +08002486
2487 /* Check size_t overflow */
Gilles Peskine449bd832023-01-11 14:50:10 +01002488 if (session->ticket_len > SIZE_MAX - needed) {
2489 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2490 }
Jerry Yu34191072022-08-18 10:32:09 +08002491
Jerry Yue28d9742022-08-18 15:44:03 +08002492 needed += session->ticket_len; /* ticket */
Jerry Yu438ddd82022-07-07 06:55:50 +00002493 }
2494#endif /* MBEDTLS_SSL_CLI_C */
2495
Jerry Yue36fdd62022-08-17 21:31:36 +08002496 *olen = needed;
Gilles Peskine449bd832023-01-11 14:50:10 +01002497 if (needed > buf_len) {
2498 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
2499 }
Jerry Yu438ddd82022-07-07 06:55:50 +00002500
2501 p[0] = session->endpoint;
Gilles Peskine449bd832023-01-11 14:50:10 +01002502 MBEDTLS_PUT_UINT16_BE(session->ciphersuite, p, 1);
2503 MBEDTLS_PUT_UINT32_BE(session->ticket_age_add, p, 3);
Jerry Yu438ddd82022-07-07 06:55:50 +00002504 p[7] = session->ticket_flags;
2505
2506 /* save resumption_key */
Jerry Yubc7c1a42022-07-21 22:57:37 +08002507 p[8] = session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002508 p += 9;
Gilles Peskine449bd832023-01-11 14:50:10 +01002509 memcpy(p, session->resumption_key, session->resumption_key_len);
Jerry Yubc7c1a42022-07-21 22:57:37 +08002510 p += session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002511
2512#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002513 if (session->endpoint == MBEDTLS_SSL_IS_SERVER) {
2514 MBEDTLS_PUT_UINT64_BE((uint64_t) session->start, p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002515 p += 8;
2516 }
2517#endif /* MBEDTLS_HAVE_TIME */
2518
2519#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002520 if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002521#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01002522 MBEDTLS_PUT_UINT16_BE(hostname_len, p, 0);
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002523 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002524 if (hostname_len > 0) {
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002525 /* save host name */
Gilles Peskine449bd832023-01-11 14:50:10 +01002526 memcpy(p, session->hostname, hostname_len);
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002527 p += hostname_len;
2528 }
2529#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
2530
Jerry Yu438ddd82022-07-07 06:55:50 +00002531#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01002532 MBEDTLS_PUT_UINT64_BE((uint64_t) session->ticket_received, p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002533 p += 8;
2534#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002535 MBEDTLS_PUT_UINT32_BE(session->ticket_lifetime, p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002536 p += 4;
2537
Gilles Peskine449bd832023-01-11 14:50:10 +01002538 MBEDTLS_PUT_UINT16_BE(session->ticket_len, p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002539 p += 2;
Jerry Yu34191072022-08-18 10:32:09 +08002540
Gilles Peskine449bd832023-01-11 14:50:10 +01002541 if (session->ticket != NULL && session->ticket_len > 0) {
2542 memcpy(p, session->ticket, session->ticket_len);
Jerry Yu438ddd82022-07-07 06:55:50 +00002543 p += session->ticket_len;
2544 }
2545 }
2546#endif /* MBEDTLS_SSL_CLI_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01002547 return 0;
Jerry Yu438ddd82022-07-07 06:55:50 +00002548}
2549
2550MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002551static int ssl_tls13_session_load(mbedtls_ssl_session *session,
2552 const unsigned char *buf,
2553 size_t len)
Jerry Yu438ddd82022-07-07 06:55:50 +00002554{
2555 const unsigned char *p = buf;
2556 const unsigned char *end = buf + len;
2557
Gilles Peskine449bd832023-01-11 14:50:10 +01002558 if (end - p < 9) {
2559 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2560 }
Jerry Yu438ddd82022-07-07 06:55:50 +00002561 session->endpoint = p[0];
Gilles Peskine449bd832023-01-11 14:50:10 +01002562 session->ciphersuite = MBEDTLS_GET_UINT16_BE(p, 1);
2563 session->ticket_age_add = MBEDTLS_GET_UINT32_BE(p, 3);
Jerry Yu438ddd82022-07-07 06:55:50 +00002564 session->ticket_flags = p[7];
2565
2566 /* load resumption_key */
Jerry Yubc7c1a42022-07-21 22:57:37 +08002567 session->resumption_key_len = p[8];
Jerry Yu438ddd82022-07-07 06:55:50 +00002568 p += 9;
2569
Gilles Peskine449bd832023-01-11 14:50:10 +01002570 if (end - p < session->resumption_key_len) {
2571 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2572 }
Jerry Yu438ddd82022-07-07 06:55:50 +00002573
Gilles Peskine449bd832023-01-11 14:50:10 +01002574 if (sizeof(session->resumption_key) < session->resumption_key_len) {
2575 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2576 }
2577 memcpy(session->resumption_key, p, session->resumption_key_len);
Jerry Yubc7c1a42022-07-21 22:57:37 +08002578 p += session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002579
2580#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002581 if (session->endpoint == MBEDTLS_SSL_IS_SERVER) {
2582 if (end - p < 8) {
2583 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2584 }
2585 session->start = MBEDTLS_GET_UINT64_BE(p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002586 p += 8;
2587 }
2588#endif /* MBEDTLS_HAVE_TIME */
2589
2590#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002591 if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002592#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01002593 defined(MBEDTLS_SSL_SESSION_TICKETS)
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002594 size_t hostname_len;
2595 /* load host name */
Gilles Peskine449bd832023-01-11 14:50:10 +01002596 if (end - p < 2) {
2597 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2598 }
2599 hostname_len = MBEDTLS_GET_UINT16_BE(p, 0);
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002600 p += 2;
2601
Gilles Peskine449bd832023-01-11 14:50:10 +01002602 if (end - p < (long int) hostname_len) {
2603 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2604 }
2605 if (hostname_len > 0) {
2606 session->hostname = mbedtls_calloc(1, hostname_len);
2607 if (session->hostname == NULL) {
2608 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2609 }
2610 memcpy(session->hostname, p, hostname_len);
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002611 p += hostname_len;
2612 }
2613#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION &&
2614 MBEDTLS_SSL_SESSION_TICKETS */
2615
Jerry Yu438ddd82022-07-07 06:55:50 +00002616#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01002617 if (end - p < 8) {
2618 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2619 }
2620 session->ticket_received = MBEDTLS_GET_UINT64_BE(p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002621 p += 8;
2622#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002623 if (end - p < 4) {
2624 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2625 }
2626 session->ticket_lifetime = MBEDTLS_GET_UINT32_BE(p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002627 p += 4;
2628
Gilles Peskine449bd832023-01-11 14:50:10 +01002629 if (end - p < 2) {
2630 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2631 }
2632 session->ticket_len = MBEDTLS_GET_UINT16_BE(p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002633 p += 2;
2634
Gilles Peskine449bd832023-01-11 14:50:10 +01002635 if (end - p < (long int) session->ticket_len) {
2636 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2637 }
2638 if (session->ticket_len > 0) {
2639 session->ticket = mbedtls_calloc(1, session->ticket_len);
2640 if (session->ticket == NULL) {
2641 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2642 }
2643 memcpy(session->ticket, p, session->ticket_len);
Jerry Yu438ddd82022-07-07 06:55:50 +00002644 p += session->ticket_len;
2645 }
2646 }
2647#endif /* MBEDTLS_SSL_CLI_C */
2648
Gilles Peskine449bd832023-01-11 14:50:10 +01002649 return 0;
Jerry Yu438ddd82022-07-07 06:55:50 +00002650
2651}
2652#else /* MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yue36fdd62022-08-17 21:31:36 +08002653MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002654static int ssl_tls13_session_save(const mbedtls_ssl_session *session,
2655 unsigned char *buf,
2656 size_t buf_len,
2657 size_t *olen)
Jerry Yu251a12e2022-07-13 15:15:48 +08002658{
2659 ((void) session);
2660 ((void) buf);
2661 ((void) buf_len);
Jerry Yue36fdd62022-08-17 21:31:36 +08002662 *olen = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002663 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Jerry Yu251a12e2022-07-13 15:15:48 +08002664}
Jerry Yu438ddd82022-07-07 06:55:50 +00002665
Gilles Peskine449bd832023-01-11 14:50:10 +01002666static int ssl_tls13_session_load(const mbedtls_ssl_session *session,
2667 unsigned char *buf,
2668 size_t buf_len)
Jerry Yu438ddd82022-07-07 06:55:50 +00002669{
2670 ((void) session);
2671 ((void) buf);
2672 ((void) buf_len);
Gilles Peskine449bd832023-01-11 14:50:10 +01002673 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Jerry Yu438ddd82022-07-07 06:55:50 +00002674}
2675#endif /* !MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yu251a12e2022-07-13 15:15:48 +08002676#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
2677
Gilles Peskine449bd832023-01-11 14:50:10 +01002678psa_status_t mbedtls_ssl_cipher_to_psa(mbedtls_cipher_type_t mbedtls_cipher_type,
2679 size_t taglen,
2680 psa_algorithm_t *alg,
2681 psa_key_type_t *key_type,
2682 size_t *key_size)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002683{
Gilles Peskine449bd832023-01-11 14:50:10 +01002684 switch (mbedtls_cipher_type) {
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002685 case MBEDTLS_CIPHER_AES_128_CBC:
2686 *alg = PSA_ALG_CBC_NO_PADDING;
2687 *key_type = PSA_KEY_TYPE_AES;
2688 *key_size = 128;
2689 break;
2690 case MBEDTLS_CIPHER_AES_128_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002691 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002692 *key_type = PSA_KEY_TYPE_AES;
2693 *key_size = 128;
2694 break;
2695 case MBEDTLS_CIPHER_AES_128_GCM:
2696 *alg = PSA_ALG_GCM;
2697 *key_type = PSA_KEY_TYPE_AES;
2698 *key_size = 128;
2699 break;
2700 case MBEDTLS_CIPHER_AES_192_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002701 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002702 *key_type = PSA_KEY_TYPE_AES;
2703 *key_size = 192;
2704 break;
2705 case MBEDTLS_CIPHER_AES_192_GCM:
2706 *alg = PSA_ALG_GCM;
2707 *key_type = PSA_KEY_TYPE_AES;
2708 *key_size = 192;
2709 break;
2710 case MBEDTLS_CIPHER_AES_256_CBC:
2711 *alg = PSA_ALG_CBC_NO_PADDING;
2712 *key_type = PSA_KEY_TYPE_AES;
2713 *key_size = 256;
2714 break;
2715 case MBEDTLS_CIPHER_AES_256_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002716 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002717 *key_type = PSA_KEY_TYPE_AES;
2718 *key_size = 256;
2719 break;
2720 case MBEDTLS_CIPHER_AES_256_GCM:
2721 *alg = PSA_ALG_GCM;
2722 *key_type = PSA_KEY_TYPE_AES;
2723 *key_size = 256;
2724 break;
2725 case MBEDTLS_CIPHER_ARIA_128_CBC:
2726 *alg = PSA_ALG_CBC_NO_PADDING;
2727 *key_type = PSA_KEY_TYPE_ARIA;
2728 *key_size = 128;
2729 break;
2730 case MBEDTLS_CIPHER_ARIA_128_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002731 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002732 *key_type = PSA_KEY_TYPE_ARIA;
2733 *key_size = 128;
2734 break;
2735 case MBEDTLS_CIPHER_ARIA_128_GCM:
2736 *alg = PSA_ALG_GCM;
2737 *key_type = PSA_KEY_TYPE_ARIA;
2738 *key_size = 128;
2739 break;
2740 case MBEDTLS_CIPHER_ARIA_192_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002741 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002742 *key_type = PSA_KEY_TYPE_ARIA;
2743 *key_size = 192;
2744 break;
2745 case MBEDTLS_CIPHER_ARIA_192_GCM:
2746 *alg = PSA_ALG_GCM;
2747 *key_type = PSA_KEY_TYPE_ARIA;
2748 *key_size = 192;
2749 break;
2750 case MBEDTLS_CIPHER_ARIA_256_CBC:
2751 *alg = PSA_ALG_CBC_NO_PADDING;
2752 *key_type = PSA_KEY_TYPE_ARIA;
2753 *key_size = 256;
2754 break;
2755 case MBEDTLS_CIPHER_ARIA_256_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002756 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002757 *key_type = PSA_KEY_TYPE_ARIA;
2758 *key_size = 256;
2759 break;
2760 case MBEDTLS_CIPHER_ARIA_256_GCM:
2761 *alg = PSA_ALG_GCM;
2762 *key_type = PSA_KEY_TYPE_ARIA;
2763 *key_size = 256;
2764 break;
2765 case MBEDTLS_CIPHER_CAMELLIA_128_CBC:
2766 *alg = PSA_ALG_CBC_NO_PADDING;
2767 *key_type = PSA_KEY_TYPE_CAMELLIA;
2768 *key_size = 128;
2769 break;
2770 case MBEDTLS_CIPHER_CAMELLIA_128_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002771 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002772 *key_type = PSA_KEY_TYPE_CAMELLIA;
2773 *key_size = 128;
2774 break;
2775 case MBEDTLS_CIPHER_CAMELLIA_128_GCM:
2776 *alg = PSA_ALG_GCM;
2777 *key_type = PSA_KEY_TYPE_CAMELLIA;
2778 *key_size = 128;
2779 break;
2780 case MBEDTLS_CIPHER_CAMELLIA_192_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002781 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002782 *key_type = PSA_KEY_TYPE_CAMELLIA;
2783 *key_size = 192;
2784 break;
2785 case MBEDTLS_CIPHER_CAMELLIA_192_GCM:
2786 *alg = PSA_ALG_GCM;
2787 *key_type = PSA_KEY_TYPE_CAMELLIA;
2788 *key_size = 192;
2789 break;
2790 case MBEDTLS_CIPHER_CAMELLIA_256_CBC:
2791 *alg = PSA_ALG_CBC_NO_PADDING;
2792 *key_type = PSA_KEY_TYPE_CAMELLIA;
2793 *key_size = 256;
2794 break;
2795 case MBEDTLS_CIPHER_CAMELLIA_256_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002796 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002797 *key_type = PSA_KEY_TYPE_CAMELLIA;
2798 *key_size = 256;
2799 break;
2800 case MBEDTLS_CIPHER_CAMELLIA_256_GCM:
2801 *alg = PSA_ALG_GCM;
2802 *key_type = PSA_KEY_TYPE_CAMELLIA;
2803 *key_size = 256;
2804 break;
2805 case MBEDTLS_CIPHER_CHACHA20_POLY1305:
2806 *alg = PSA_ALG_CHACHA20_POLY1305;
2807 *key_type = PSA_KEY_TYPE_CHACHA20;
2808 *key_size = 256;
2809 break;
2810 case MBEDTLS_CIPHER_NULL:
2811 *alg = MBEDTLS_SSL_NULL_CIPHER;
2812 *key_type = 0;
2813 *key_size = 0;
2814 break;
2815 default:
2816 return PSA_ERROR_NOT_SUPPORTED;
2817 }
2818
2819 return PSA_SUCCESS;
2820}
Neil Armstrong8395d7a2022-05-18 11:44:56 +02002821#endif /* MBEDTLS_USE_PSA_CRYPTO || MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002822
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002823#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002824int mbedtls_ssl_conf_dh_param_bin(mbedtls_ssl_config *conf,
2825 const unsigned char *dhm_P, size_t P_len,
2826 const unsigned char *dhm_G, size_t G_len)
Hanno Beckera90658f2017-10-04 15:29:08 +01002827{
Janos Follath865b3eb2019-12-16 11:46:15 +00002828 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckera90658f2017-10-04 15:29:08 +01002829
Gilles Peskine449bd832023-01-11 14:50:10 +01002830 mbedtls_mpi_free(&conf->dhm_P);
2831 mbedtls_mpi_free(&conf->dhm_G);
Glenn Strausscee11292021-12-20 01:43:17 -05002832
Gilles Peskine449bd832023-01-11 14:50:10 +01002833 if ((ret = mbedtls_mpi_read_binary(&conf->dhm_P, dhm_P, P_len)) != 0 ||
2834 (ret = mbedtls_mpi_read_binary(&conf->dhm_G, dhm_G, G_len)) != 0) {
2835 mbedtls_mpi_free(&conf->dhm_P);
2836 mbedtls_mpi_free(&conf->dhm_G);
2837 return ret;
Hanno Beckera90658f2017-10-04 15:29:08 +01002838 }
2839
Gilles Peskine449bd832023-01-11 14:50:10 +01002840 return 0;
Hanno Beckera90658f2017-10-04 15:29:08 +01002841}
Paul Bakker5121ce52009-01-03 21:22:43 +00002842
Gilles Peskine449bd832023-01-11 14:50:10 +01002843int mbedtls_ssl_conf_dh_param_ctx(mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx)
Paul Bakker1b57b062011-01-06 15:48:19 +00002844{
Janos Follath865b3eb2019-12-16 11:46:15 +00002845 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1b57b062011-01-06 15:48:19 +00002846
Gilles Peskine449bd832023-01-11 14:50:10 +01002847 mbedtls_mpi_free(&conf->dhm_P);
2848 mbedtls_mpi_free(&conf->dhm_G);
Glenn Strausscee11292021-12-20 01:43:17 -05002849
Gilles Peskine449bd832023-01-11 14:50:10 +01002850 if ((ret = mbedtls_dhm_get_value(dhm_ctx, MBEDTLS_DHM_PARAM_P,
2851 &conf->dhm_P)) != 0 ||
2852 (ret = mbedtls_dhm_get_value(dhm_ctx, MBEDTLS_DHM_PARAM_G,
2853 &conf->dhm_G)) != 0) {
2854 mbedtls_mpi_free(&conf->dhm_P);
2855 mbedtls_mpi_free(&conf->dhm_G);
2856 return ret;
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01002857 }
Paul Bakker1b57b062011-01-06 15:48:19 +00002858
Gilles Peskine449bd832023-01-11 14:50:10 +01002859 return 0;
Paul Bakker1b57b062011-01-06 15:48:19 +00002860}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002861#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00002862
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02002863#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
2864/*
2865 * Set the minimum length for Diffie-Hellman parameters
2866 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002867void mbedtls_ssl_conf_dhm_min_bitlen(mbedtls_ssl_config *conf,
2868 unsigned int bitlen)
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02002869{
2870 conf->dhm_min_bitlen = bitlen;
2871}
2872#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
2873
Ronald Crone68ab4f2022-10-05 12:46:29 +02002874#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002875#if !defined(MBEDTLS_DEPRECATED_REMOVED) && defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002876/*
2877 * Set allowed/preferred hashes for handshake signatures
2878 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002879void mbedtls_ssl_conf_sig_hashes(mbedtls_ssl_config *conf,
2880 const int *hashes)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002881{
2882 conf->sig_hashes = hashes;
2883}
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002884#endif /* !MBEDTLS_DEPRECATED_REMOVED && MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker1cd6e002021-08-10 13:27:10 +01002885
Jerry Yuf017ee42022-01-12 15:49:48 +08002886/* Configure allowed signature algorithms for handshake */
Gilles Peskine449bd832023-01-11 14:50:10 +01002887void mbedtls_ssl_conf_sig_algs(mbedtls_ssl_config *conf,
2888 const uint16_t *sig_algs)
Hanno Becker1cd6e002021-08-10 13:27:10 +01002889{
Jerry Yuf017ee42022-01-12 15:49:48 +08002890#if !defined(MBEDTLS_DEPRECATED_REMOVED)
2891 conf->sig_hashes = NULL;
2892#endif /* !MBEDTLS_DEPRECATED_REMOVED */
2893 conf->sig_algs = sig_algs;
Hanno Becker1cd6e002021-08-10 13:27:10 +01002894}
Ronald Crone68ab4f2022-10-05 12:46:29 +02002895#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002896
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02002897#if defined(MBEDTLS_ECP_C)
Brett Warrene0edc842021-08-17 09:53:13 +01002898#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002899/*
2900 * Set the allowed elliptic curves
Brett Warrene0edc842021-08-17 09:53:13 +01002901 *
2902 * mbedtls_ssl_setup() takes the provided list
2903 * and translates it to a list of IANA TLS group identifiers,
2904 * stored in ssl->handshake->group_list.
2905 *
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002906 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002907void mbedtls_ssl_conf_curves(mbedtls_ssl_config *conf,
2908 const mbedtls_ecp_group_id *curve_list)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002909{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002910 conf->curve_list = curve_list;
Brett Warrene0edc842021-08-17 09:53:13 +01002911 conf->group_list = NULL;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002912}
Brett Warrene0edc842021-08-17 09:53:13 +01002913#endif /* MBEDTLS_DEPRECATED_REMOVED */
Hanno Becker947194e2017-04-07 13:25:49 +01002914#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002915
Brett Warrene0edc842021-08-17 09:53:13 +01002916/*
2917 * Set the allowed groups
2918 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002919void mbedtls_ssl_conf_groups(mbedtls_ssl_config *conf,
2920 const uint16_t *group_list)
Brett Warrene0edc842021-08-17 09:53:13 +01002921{
2922#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
2923 conf->curve_list = NULL;
2924#endif
2925 conf->group_list = group_list;
2926}
2927
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002928#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002929int mbedtls_ssl_set_hostname(mbedtls_ssl_context *ssl, const char *hostname)
Paul Bakker5121ce52009-01-03 21:22:43 +00002930{
Hanno Becker947194e2017-04-07 13:25:49 +01002931 /* Initialize to suppress unnecessary compiler warning */
2932 size_t hostname_len = 0;
2933
2934 /* Check if new hostname is valid before
2935 * making any change to current one */
Gilles Peskine449bd832023-01-11 14:50:10 +01002936 if (hostname != NULL) {
2937 hostname_len = strlen(hostname);
Hanno Becker947194e2017-04-07 13:25:49 +01002938
Gilles Peskine449bd832023-01-11 14:50:10 +01002939 if (hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN) {
2940 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2941 }
Hanno Becker947194e2017-04-07 13:25:49 +01002942 }
2943
2944 /* Now it's clear that we will overwrite the old hostname,
2945 * so we can free it safely */
2946
Gilles Peskine449bd832023-01-11 14:50:10 +01002947 if (ssl->hostname != NULL) {
2948 mbedtls_platform_zeroize(ssl->hostname, strlen(ssl->hostname));
2949 mbedtls_free(ssl->hostname);
Hanno Becker947194e2017-04-07 13:25:49 +01002950 }
2951
2952 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01002953
Gilles Peskine449bd832023-01-11 14:50:10 +01002954 if (hostname == NULL) {
Hanno Becker947194e2017-04-07 13:25:49 +01002955 ssl->hostname = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01002956 } else {
2957 ssl->hostname = mbedtls_calloc(1, hostname_len + 1);
2958 if (ssl->hostname == NULL) {
2959 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2960 }
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002961
Gilles Peskine449bd832023-01-11 14:50:10 +01002962 memcpy(ssl->hostname, hostname, hostname_len);
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002963
Hanno Becker947194e2017-04-07 13:25:49 +01002964 ssl->hostname[hostname_len] = '\0';
2965 }
Paul Bakker5121ce52009-01-03 21:22:43 +00002966
Gilles Peskine449bd832023-01-11 14:50:10 +01002967 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00002968}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01002969#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00002970
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002971#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01002972void mbedtls_ssl_conf_sni(mbedtls_ssl_config *conf,
2973 int (*f_sni)(void *, mbedtls_ssl_context *,
2974 const unsigned char *, size_t),
2975 void *p_sni)
Paul Bakker5701cdc2012-09-27 21:49:42 +00002976{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002977 conf->f_sni = f_sni;
2978 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00002979}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002980#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00002981
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02002982#if defined(MBEDTLS_SSL_ALPN)
Gilles Peskine449bd832023-01-11 14:50:10 +01002983int mbedtls_ssl_conf_alpn_protocols(mbedtls_ssl_config *conf, const char **protos)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02002984{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002985 size_t cur_len, tot_len;
2986 const char **p;
2987
2988 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08002989 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
2990 * MUST NOT be truncated."
2991 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002992 */
2993 tot_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002994 for (p = protos; *p != NULL; p++) {
2995 cur_len = strlen(*p);
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02002996 tot_len += cur_len;
2997
Gilles Peskine449bd832023-01-11 14:50:10 +01002998 if ((cur_len == 0) ||
2999 (cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN) ||
3000 (tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN)) {
3001 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3002 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02003003 }
3004
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003005 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02003006
Gilles Peskine449bd832023-01-11 14:50:10 +01003007 return 0;
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003008}
3009
Gilles Peskine449bd832023-01-11 14:50:10 +01003010const char *mbedtls_ssl_get_alpn_protocol(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003011{
Gilles Peskine449bd832023-01-11 14:50:10 +01003012 return ssl->alpn_chosen;
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003013}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003014#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003015
Johan Pascalb62bb512015-12-03 21:56:45 +01003016#if defined(MBEDTLS_SSL_DTLS_SRTP)
Gilles Peskine449bd832023-01-11 14:50:10 +01003017void mbedtls_ssl_conf_srtp_mki_value_supported(mbedtls_ssl_config *conf,
3018 int support_mki_value)
Ron Eldor591f1622018-01-22 12:30:04 +02003019{
3020 conf->dtls_srtp_mki_support = support_mki_value;
3021}
3022
Gilles Peskine449bd832023-01-11 14:50:10 +01003023int mbedtls_ssl_dtls_srtp_set_mki_value(mbedtls_ssl_context *ssl,
3024 unsigned char *mki_value,
3025 uint16_t mki_len)
Ron Eldor591f1622018-01-22 12:30:04 +02003026{
Gilles Peskine449bd832023-01-11 14:50:10 +01003027 if (mki_len > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH) {
3028 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Ron Eldora9788042018-12-05 11:04:31 +02003029 }
Ron Eldor591f1622018-01-22 12:30:04 +02003030
Gilles Peskine449bd832023-01-11 14:50:10 +01003031 if (ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_UNSUPPORTED) {
3032 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Ron Eldora9788042018-12-05 11:04:31 +02003033 }
Ron Eldor591f1622018-01-22 12:30:04 +02003034
Gilles Peskine449bd832023-01-11 14:50:10 +01003035 memcpy(ssl->dtls_srtp_info.mki_value, mki_value, mki_len);
Ron Eldor591f1622018-01-22 12:30:04 +02003036 ssl->dtls_srtp_info.mki_len = mki_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01003037 return 0;
Ron Eldor591f1622018-01-22 12:30:04 +02003038}
3039
Gilles Peskine449bd832023-01-11 14:50:10 +01003040int mbedtls_ssl_conf_dtls_srtp_protection_profiles(mbedtls_ssl_config *conf,
3041 const mbedtls_ssl_srtp_profile *profiles)
Johan Pascalb62bb512015-12-03 21:56:45 +01003042{
Johan Pascal253d0262020-09-22 13:04:45 +02003043 const mbedtls_ssl_srtp_profile *p;
3044 size_t list_size = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01003045
Johan Pascal253d0262020-09-22 13:04:45 +02003046 /* check the profiles list: all entry must be valid,
3047 * its size cannot be more than the total number of supported profiles, currently 4 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003048 for (p = profiles; *p != MBEDTLS_TLS_SRTP_UNSET &&
3049 list_size <= MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH;
3050 p++) {
3051 if (mbedtls_ssl_check_srtp_profile_value(*p) != MBEDTLS_TLS_SRTP_UNSET) {
Johan Pascal76fdf1d2020-10-22 23:31:00 +02003052 list_size++;
Gilles Peskine449bd832023-01-11 14:50:10 +01003053 } else {
Johan Pascal76fdf1d2020-10-22 23:31:00 +02003054 /* unsupported value, stop parsing and set the size to an error value */
3055 list_size = MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH + 1;
Johan Pascalb62bb512015-12-03 21:56:45 +01003056 }
3057 }
3058
Gilles Peskine449bd832023-01-11 14:50:10 +01003059 if (list_size > MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH) {
3060 conf->dtls_srtp_profile_list = NULL;
3061 conf->dtls_srtp_profile_list_len = 0;
3062 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Johan Pascal253d0262020-09-22 13:04:45 +02003063 }
3064
Johan Pascal9bc97ca2020-09-21 23:44:45 +02003065 conf->dtls_srtp_profile_list = profiles;
Johan Pascal253d0262020-09-22 13:04:45 +02003066 conf->dtls_srtp_profile_list_len = list_size;
Johan Pascalb62bb512015-12-03 21:56:45 +01003067
Gilles Peskine449bd832023-01-11 14:50:10 +01003068 return 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01003069}
3070
Gilles Peskine449bd832023-01-11 14:50:10 +01003071void mbedtls_ssl_get_dtls_srtp_negotiation_result(const mbedtls_ssl_context *ssl,
3072 mbedtls_dtls_srtp_info *dtls_srtp_info)
Johan Pascalb62bb512015-12-03 21:56:45 +01003073{
Johan Pascal2258a4f2020-10-28 13:53:09 +01003074 dtls_srtp_info->chosen_dtls_srtp_profile = ssl->dtls_srtp_info.chosen_dtls_srtp_profile;
3075 /* do not copy the mki value if there is no chosen profile */
Gilles Peskine449bd832023-01-11 14:50:10 +01003076 if (dtls_srtp_info->chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET) {
Johan Pascal2258a4f2020-10-28 13:53:09 +01003077 dtls_srtp_info->mki_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003078 } else {
Johan Pascal2258a4f2020-10-28 13:53:09 +01003079 dtls_srtp_info->mki_len = ssl->dtls_srtp_info.mki_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01003080 memcpy(dtls_srtp_info->mki_value, ssl->dtls_srtp_info.mki_value,
3081 ssl->dtls_srtp_info.mki_len);
Johan Pascal2258a4f2020-10-28 13:53:09 +01003082 }
Johan Pascalb62bb512015-12-03 21:56:45 +01003083}
Johan Pascalb62bb512015-12-03 21:56:45 +01003084#endif /* MBEDTLS_SSL_DTLS_SRTP */
3085
Aditya Patwardhan3096f332022-07-26 14:31:46 +05303086#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003087void mbedtls_ssl_conf_max_version(mbedtls_ssl_config *conf, int major, int minor)
Paul Bakker490ecc82011-10-06 13:04:09 +00003088{
Glenn Strauss2dfcea22022-03-14 17:26:42 -04003089 conf->max_tls_version = (major << 8) | minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00003090}
3091
Gilles Peskine449bd832023-01-11 14:50:10 +01003092void mbedtls_ssl_conf_min_version(mbedtls_ssl_config *conf, int major, int minor)
Paul Bakker1d29fb52012-09-28 13:28:45 +00003093{
Glenn Strauss2dfcea22022-03-14 17:26:42 -04003094 conf->min_tls_version = (major << 8) | minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00003095}
Aditya Patwardhan3096f332022-07-26 14:31:46 +05303096#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker1d29fb52012-09-28 13:28:45 +00003097
Janos Follath088ce432017-04-10 12:42:31 +01003098#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003099void mbedtls_ssl_conf_cert_req_ca_list(mbedtls_ssl_config *conf,
3100 char cert_req_ca_list)
Janos Follath088ce432017-04-10 12:42:31 +01003101{
3102 conf->cert_req_ca_list = cert_req_ca_list;
3103}
3104#endif
3105
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003106#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01003107void mbedtls_ssl_conf_encrypt_then_mac(mbedtls_ssl_config *conf, char etm)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01003108{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003109 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01003110}
3111#endif
3112
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003113#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Gilles Peskine449bd832023-01-11 14:50:10 +01003114void mbedtls_ssl_conf_extended_master_secret(mbedtls_ssl_config *conf, char ems)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02003115{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003116 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02003117}
3118#endif
3119
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003120#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01003121int mbedtls_ssl_conf_max_frag_len(mbedtls_ssl_config *conf, unsigned char mfl_code)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003122{
Gilles Peskine449bd832023-01-11 14:50:10 +01003123 if (mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
3124 ssl_mfl_code_to_length(mfl_code) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN) {
3125 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003126 }
3127
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01003128 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003129
Gilles Peskine449bd832023-01-11 14:50:10 +01003130 return 0;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003131}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003132#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003133
Gilles Peskine449bd832023-01-11 14:50:10 +01003134void mbedtls_ssl_conf_legacy_renegotiation(mbedtls_ssl_config *conf, int allow_legacy)
Paul Bakker48916f92012-09-16 19:57:18 +00003135{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003136 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00003137}
3138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003139#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01003140void mbedtls_ssl_conf_renegotiation(mbedtls_ssl_config *conf, int renegotiation)
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003141{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003142 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003143}
3144
Gilles Peskine449bd832023-01-11 14:50:10 +01003145void mbedtls_ssl_conf_renegotiation_enforced(mbedtls_ssl_config *conf, int max_records)
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003146{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003147 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003148}
3149
Gilles Peskine449bd832023-01-11 14:50:10 +01003150void mbedtls_ssl_conf_renegotiation_period(mbedtls_ssl_config *conf,
3151 const unsigned char period[8])
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01003152{
Gilles Peskine449bd832023-01-11 14:50:10 +01003153 memcpy(conf->renego_period, period, 8);
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01003154}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003155#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00003156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003157#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003158#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003159void mbedtls_ssl_conf_session_tickets(mbedtls_ssl_config *conf, int use_tickets)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003160{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01003161 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003162}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003163#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02003164
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003165#if defined(MBEDTLS_SSL_SRV_C)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08003166
Jerry Yud0766ec2022-09-22 10:46:57 +08003167#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003168void mbedtls_ssl_conf_new_session_tickets(mbedtls_ssl_config *conf,
3169 uint16_t num_tickets)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08003170{
Jerry Yud0766ec2022-09-22 10:46:57 +08003171 conf->new_session_tickets_count = num_tickets;
Jerry Yu1ad7ace2022-08-09 13:28:39 +08003172}
3173#endif
3174
Gilles Peskine449bd832023-01-11 14:50:10 +01003175void mbedtls_ssl_conf_session_tickets_cb(mbedtls_ssl_config *conf,
3176 mbedtls_ssl_ticket_write_t *f_ticket_write,
3177 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
3178 void *p_ticket)
Paul Bakker606b4ba2013-08-14 16:52:14 +02003179{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02003180 conf->f_ticket_write = f_ticket_write;
3181 conf->f_ticket_parse = f_ticket_parse;
3182 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02003183}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003184#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003185#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003186
Gilles Peskine449bd832023-01-11 14:50:10 +01003187void mbedtls_ssl_set_export_keys_cb(mbedtls_ssl_context *ssl,
3188 mbedtls_ssl_export_keys_t *f_export_keys,
3189 void *p_export_keys)
Robert Cragie4feb7ae2015-10-02 13:33:37 +01003190{
Hanno Becker7e6c1782021-06-08 09:24:55 +01003191 ssl->f_export_keys = f_export_keys;
3192 ssl->p_export_keys = p_export_keys;
Ron Eldorf5cc10d2019-05-07 18:33:40 +03003193}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01003194
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003195#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003196void mbedtls_ssl_conf_async_private_cb(
3197 mbedtls_ssl_config *conf,
3198 mbedtls_ssl_async_sign_t *f_async_sign,
3199 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
3200 mbedtls_ssl_async_resume_t *f_async_resume,
3201 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskine449bd832023-01-11 14:50:10 +01003202 void *async_config_data)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003203{
3204 conf->f_async_sign_start = f_async_sign;
3205 conf->f_async_decrypt_start = f_async_decrypt;
3206 conf->f_async_resume = f_async_resume;
3207 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003208 conf->p_async_config_data = async_config_data;
3209}
3210
Gilles Peskine449bd832023-01-11 14:50:10 +01003211void *mbedtls_ssl_conf_get_async_config_data(const mbedtls_ssl_config *conf)
Gilles Peskine8f97af72018-04-26 11:46:10 +02003212{
Gilles Peskine449bd832023-01-11 14:50:10 +01003213 return conf->p_async_config_data;
Gilles Peskine8f97af72018-04-26 11:46:10 +02003214}
3215
Gilles Peskine449bd832023-01-11 14:50:10 +01003216void *mbedtls_ssl_get_async_operation_data(const mbedtls_ssl_context *ssl)
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003217{
Gilles Peskine449bd832023-01-11 14:50:10 +01003218 if (ssl->handshake == NULL) {
3219 return NULL;
3220 } else {
3221 return ssl->handshake->user_async_ctx;
3222 }
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003223}
3224
Gilles Peskine449bd832023-01-11 14:50:10 +01003225void mbedtls_ssl_set_async_operation_data(mbedtls_ssl_context *ssl,
3226 void *ctx)
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003227{
Gilles Peskine449bd832023-01-11 14:50:10 +01003228 if (ssl->handshake != NULL) {
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003229 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +01003230 }
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003231}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003232#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003233
Paul Bakker5121ce52009-01-03 21:22:43 +00003234/*
3235 * SSL get accessors
3236 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003237uint32_t mbedtls_ssl_get_verify_result(const mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00003238{
Gilles Peskine449bd832023-01-11 14:50:10 +01003239 if (ssl->session != NULL) {
3240 return ssl->session->verify_result;
3241 }
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00003242
Gilles Peskine449bd832023-01-11 14:50:10 +01003243 if (ssl->session_negotiate != NULL) {
3244 return ssl->session_negotiate->verify_result;
3245 }
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00003246
Gilles Peskine449bd832023-01-11 14:50:10 +01003247 return 0xFFFFFFFF;
Paul Bakker5121ce52009-01-03 21:22:43 +00003248}
3249
Gilles Peskine449bd832023-01-11 14:50:10 +01003250int mbedtls_ssl_get_ciphersuite_id_from_ssl(const mbedtls_ssl_context *ssl)
Glenn Strauss8f526902022-01-13 00:04:49 -05003251{
Gilles Peskine449bd832023-01-11 14:50:10 +01003252 if (ssl == NULL || ssl->session == NULL) {
3253 return 0;
3254 }
Glenn Strauss8f526902022-01-13 00:04:49 -05003255
Gilles Peskine449bd832023-01-11 14:50:10 +01003256 return ssl->session->ciphersuite;
Glenn Strauss8f526902022-01-13 00:04:49 -05003257}
3258
Gilles Peskine449bd832023-01-11 14:50:10 +01003259const char *mbedtls_ssl_get_ciphersuite(const mbedtls_ssl_context *ssl)
Paul Bakker72f62662011-01-16 21:27:44 +00003260{
Gilles Peskine449bd832023-01-11 14:50:10 +01003261 if (ssl == NULL || ssl->session == NULL) {
3262 return NULL;
3263 }
Paul Bakker926c8e42013-03-06 10:23:34 +01003264
Gilles Peskine449bd832023-01-11 14:50:10 +01003265 return mbedtls_ssl_get_ciphersuite_name(ssl->session->ciphersuite);
Paul Bakker72f62662011-01-16 21:27:44 +00003266}
3267
Gilles Peskine449bd832023-01-11 14:50:10 +01003268const char *mbedtls_ssl_get_version(const mbedtls_ssl_context *ssl)
Paul Bakker43ca69c2011-01-15 17:35:19 +00003269{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003270#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003271 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
3272 switch (ssl->tls_version) {
Glenn Strauss60bfe602022-03-14 19:04:24 -04003273 case MBEDTLS_SSL_VERSION_TLS1_2:
Gilles Peskine449bd832023-01-11 14:50:10 +01003274 return "DTLSv1.2";
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003275 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01003276 return "unknown (DTLS)";
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003277 }
3278 }
3279#endif
3280
Gilles Peskine449bd832023-01-11 14:50:10 +01003281 switch (ssl->tls_version) {
Glenn Strauss60bfe602022-03-14 19:04:24 -04003282 case MBEDTLS_SSL_VERSION_TLS1_2:
Gilles Peskine449bd832023-01-11 14:50:10 +01003283 return "TLSv1.2";
Glenn Strauss60bfe602022-03-14 19:04:24 -04003284 case MBEDTLS_SSL_VERSION_TLS1_3:
Gilles Peskine449bd832023-01-11 14:50:10 +01003285 return "TLSv1.3";
Paul Bakker43ca69c2011-01-15 17:35:19 +00003286 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01003287 return "unknown";
Paul Bakker43ca69c2011-01-15 17:35:19 +00003288 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00003289}
3290
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003291#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01003292size_t mbedtls_ssl_get_input_max_frag_len(const mbedtls_ssl_context *ssl)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003293{
David Horstmann95d516f2021-05-04 18:36:56 +01003294 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003295 size_t read_mfl;
3296
Jerry Yuddda0502022-12-01 19:43:12 +08003297#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003298 /* Use the configured MFL for the client if we're past SERVER_HELLO_DONE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003299 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
3300 ssl->state >= MBEDTLS_SSL_SERVER_HELLO_DONE) {
3301 return ssl_mfl_code_to_length(ssl->conf->mfl_code);
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003302 }
Jerry Yuddda0502022-12-01 19:43:12 +08003303#endif
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003304
3305 /* Check if a smaller max length was negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01003306 if (ssl->session_out != NULL) {
3307 read_mfl = ssl_mfl_code_to_length(ssl->session_out->mfl_code);
3308 if (read_mfl < max_len) {
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003309 max_len = read_mfl;
3310 }
3311 }
3312
Jerry Yuddda0502022-12-01 19:43:12 +08003313 /* During a handshake, use the value being negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01003314 if (ssl->session_negotiate != NULL) {
3315 read_mfl = ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code);
3316 if (read_mfl < max_len) {
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003317 max_len = read_mfl;
3318 }
3319 }
3320
Gilles Peskine449bd832023-01-11 14:50:10 +01003321 return max_len;
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003322}
3323
Gilles Peskine449bd832023-01-11 14:50:10 +01003324size_t mbedtls_ssl_get_output_max_frag_len(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003325{
3326 size_t max_len;
3327
3328 /*
3329 * Assume mfl_code is correct since it was checked when set
3330 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003331 max_len = ssl_mfl_code_to_length(ssl->conf->mfl_code);
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003332
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02003333 /* Check if a smaller max length was negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01003334 if (ssl->session_out != NULL &&
3335 ssl_mfl_code_to_length(ssl->session_out->mfl_code) < max_len) {
3336 max_len = ssl_mfl_code_to_length(ssl->session_out->mfl_code);
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003337 }
3338
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02003339 /* During a handshake, use the value being negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01003340 if (ssl->session_negotiate != NULL &&
3341 ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code) < max_len) {
3342 max_len = ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code);
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02003343 }
3344
Gilles Peskine449bd832023-01-11 14:50:10 +01003345 return max_len;
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003346}
3347#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
3348
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003349#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003350size_t mbedtls_ssl_get_current_mtu(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003351{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04003352 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003353 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
3354 (ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
3355 ssl->state == MBEDTLS_SSL_SERVER_HELLO)) {
3356 return 0;
3357 }
Andrzej Kurekef43ce62018-10-09 08:24:12 -04003358
Gilles Peskine449bd832023-01-11 14:50:10 +01003359 if (ssl->handshake == NULL || ssl->handshake->mtu == 0) {
3360 return ssl->mtu;
3361 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003362
Gilles Peskine449bd832023-01-11 14:50:10 +01003363 if (ssl->mtu == 0) {
3364 return ssl->handshake->mtu;
3365 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003366
Gilles Peskine449bd832023-01-11 14:50:10 +01003367 return ssl->mtu < ssl->handshake->mtu ?
3368 ssl->mtu : ssl->handshake->mtu;
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003369}
3370#endif /* MBEDTLS_SSL_PROTO_DTLS */
3371
Gilles Peskine449bd832023-01-11 14:50:10 +01003372int mbedtls_ssl_get_max_out_record_payload(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003373{
3374 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
3375
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02003376#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
3377 !defined(MBEDTLS_SSL_PROTO_DTLS)
3378 (void) ssl;
3379#endif
3380
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003381#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01003382 const size_t mfl = mbedtls_ssl_get_output_max_frag_len(ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003383
Gilles Peskine449bd832023-01-11 14:50:10 +01003384 if (max_len > mfl) {
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003385 max_len = mfl;
Gilles Peskine449bd832023-01-11 14:50:10 +01003386 }
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003387#endif
3388
3389#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003390 if (mbedtls_ssl_get_current_mtu(ssl) != 0) {
3391 const size_t mtu = mbedtls_ssl_get_current_mtu(ssl);
3392 const int ret = mbedtls_ssl_get_record_expansion(ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003393 const size_t overhead = (size_t) ret;
3394
Gilles Peskine449bd832023-01-11 14:50:10 +01003395 if (ret < 0) {
3396 return ret;
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003397 }
3398
Gilles Peskine449bd832023-01-11 14:50:10 +01003399 if (mtu <= overhead) {
3400 MBEDTLS_SSL_DEBUG_MSG(1, ("MTU too low for record expansion"));
3401 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
3402 }
3403
3404 if (max_len > mtu - overhead) {
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003405 max_len = mtu - overhead;
Gilles Peskine449bd832023-01-11 14:50:10 +01003406 }
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003407 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003408#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003409
Hanno Becker0defedb2018-08-10 12:35:02 +01003410#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
3411 !defined(MBEDTLS_SSL_PROTO_DTLS)
3412 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003413#endif
3414
Gilles Peskine449bd832023-01-11 14:50:10 +01003415 return (int) max_len;
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003416}
3417
Gilles Peskine449bd832023-01-11 14:50:10 +01003418int mbedtls_ssl_get_max_in_record_payload(const mbedtls_ssl_context *ssl)
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003419{
3420 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
3421
3422#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
3423 (void) ssl;
3424#endif
3425
3426#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01003427 const size_t mfl = mbedtls_ssl_get_input_max_frag_len(ssl);
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003428
Gilles Peskine449bd832023-01-11 14:50:10 +01003429 if (max_len > mfl) {
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003430 max_len = mfl;
Gilles Peskine449bd832023-01-11 14:50:10 +01003431 }
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003432#endif
3433
Gilles Peskine449bd832023-01-11 14:50:10 +01003434 return (int) max_len;
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003435}
3436
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003437#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003438const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert(const mbedtls_ssl_context *ssl)
Paul Bakkerb0550d92012-10-30 07:51:03 +00003439{
Gilles Peskine449bd832023-01-11 14:50:10 +01003440 if (ssl == NULL || ssl->session == NULL) {
3441 return NULL;
3442 }
Paul Bakkerb0550d92012-10-30 07:51:03 +00003443
Hanno Beckere6824572019-02-07 13:18:46 +00003444#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01003445 return ssl->session->peer_cert;
Hanno Beckere6824572019-02-07 13:18:46 +00003446#else
Gilles Peskine449bd832023-01-11 14:50:10 +01003447 return NULL;
Hanno Beckere6824572019-02-07 13:18:46 +00003448#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00003449}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003450#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00003451
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003452#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003453int mbedtls_ssl_get_session(const mbedtls_ssl_context *ssl,
3454 mbedtls_ssl_session *dst)
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003455{
Hanno Beckere810bbc2021-05-14 16:01:05 +01003456 int ret;
3457
Gilles Peskine449bd832023-01-11 14:50:10 +01003458 if (ssl == NULL ||
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003459 dst == NULL ||
3460 ssl->session == NULL ||
Gilles Peskine449bd832023-01-11 14:50:10 +01003461 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT) {
3462 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003463 }
3464
Hanno Beckere810bbc2021-05-14 16:01:05 +01003465 /* Since Mbed TLS 3.0, mbedtls_ssl_get_session() is no longer
3466 * idempotent: Each session can only be exported once.
3467 *
3468 * (This is in preparation for TLS 1.3 support where we will
3469 * need the ability to export multiple sessions (aka tickets),
3470 * which will be achieved by calling mbedtls_ssl_get_session()
3471 * multiple times until it fails.)
3472 *
3473 * Check whether we have already exported the current session,
3474 * and fail if so.
3475 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003476 if (ssl->session->exported == 1) {
3477 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
3478 }
Hanno Beckere810bbc2021-05-14 16:01:05 +01003479
Gilles Peskine449bd832023-01-11 14:50:10 +01003480 ret = mbedtls_ssl_session_copy(dst, ssl->session);
3481 if (ret != 0) {
3482 return ret;
3483 }
Hanno Beckere810bbc2021-05-14 16:01:05 +01003484
3485 /* Remember that we've exported the session. */
3486 ssl->session->exported = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003487 return 0;
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003488}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003489#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003490
Paul Bakker5121ce52009-01-03 21:22:43 +00003491/*
Hanno Beckera835da52019-05-16 12:39:07 +01003492 * Define ticket header determining Mbed TLS version
3493 * and structure of the ticket.
3494 */
3495
Hanno Becker94ef3b32019-05-16 12:50:45 +01003496/*
Hanno Becker50b59662019-05-28 14:30:45 +01003497 * Define bitflag determining compile-time settings influencing
3498 * structure of serialized SSL sessions.
Hanno Becker94ef3b32019-05-16 12:50:45 +01003499 */
3500
Hanno Becker50b59662019-05-28 14:30:45 +01003501#if defined(MBEDTLS_HAVE_TIME)
Hanno Becker3e088662019-05-29 11:10:18 +01003502#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker50b59662019-05-28 14:30:45 +01003503#else
Hanno Becker3e088662019-05-29 11:10:18 +01003504#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003505#endif /* MBEDTLS_HAVE_TIME */
3506
3507#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker3e088662019-05-29 11:10:18 +01003508#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003509#else
Hanno Becker3e088662019-05-29 11:10:18 +01003510#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003511#endif /* MBEDTLS_X509_CRT_PARSE_C */
3512
3513#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Becker3e088662019-05-29 11:10:18 +01003514#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003515#else
Hanno Becker3e088662019-05-29 11:10:18 +01003516#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003517#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
3518
3519#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Becker3e088662019-05-29 11:10:18 +01003520#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003521#else
Hanno Becker3e088662019-05-29 11:10:18 +01003522#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003523#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
3524
Hanno Becker94ef3b32019-05-16 12:50:45 +01003525#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3e088662019-05-29 11:10:18 +01003526#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003527#else
Hanno Becker3e088662019-05-29 11:10:18 +01003528#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003529#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
3530
Hanno Becker94ef3b32019-05-16 12:50:45 +01003531#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3532#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
3533#else
3534#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
3535#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3536
Hanno Becker3e088662019-05-29 11:10:18 +01003537#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
3538#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
3539#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
3540#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
Hanno Becker37bdbe62021-08-01 05:38:58 +01003541#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 4
3542#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 5
Hanno Becker3e088662019-05-29 11:10:18 +01003543
Hanno Becker50b59662019-05-28 14:30:45 +01003544#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Gilles Peskine449bd832023-01-11 14:50:10 +01003545 ((uint16_t) ( \
3546 (SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT) | \
3547 (SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT) | \
3548 (SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << \
3549 SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT) | \
3550 (SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT) | \
3551 (SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT) | \
3552 (SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT)))
Hanno Becker94ef3b32019-05-16 12:50:45 +01003553
Hanno Beckerf8787072019-05-16 12:41:07 +01003554static unsigned char ssl_serialized_session_header[] = {
Hanno Becker94ef3b32019-05-16 12:50:45 +01003555 MBEDTLS_VERSION_MAJOR,
3556 MBEDTLS_VERSION_MINOR,
3557 MBEDTLS_VERSION_PATCH,
Gilles Peskine449bd832023-01-11 14:50:10 +01003558 MBEDTLS_BYTE_1(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
3559 MBEDTLS_BYTE_0(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
Hanno Beckerf8787072019-05-16 12:41:07 +01003560};
Hanno Beckera835da52019-05-16 12:39:07 +01003561
3562/*
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003563 * Serialize a session in the following format:
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02003564 * (in the presentation language of TLS, RFC 8446 section 3)
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003565 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003566 * struct {
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01003567 *
Hanno Beckerdce50972021-08-01 05:39:23 +01003568 * opaque mbedtls_version[3]; // library version: major, minor, patch
3569 * opaque session_format[2]; // library-version specific 16-bit field
3570 * // determining the format of the remaining
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003571 * // serialized data.
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01003572 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003573 * Note: When updating the format, remember to keep
3574 * these version+format bytes.
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003575 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003576 * // In this version, `session_format` determines
3577 * // the setting of those compile-time
3578 * // configuration options which influence
3579 * // the structure of mbedtls_ssl_session.
3580 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003581 * uint8_t minor_ver; // Protocol minor version. Possible values:
Jerry Yu0c2a7382022-12-06 13:27:25 +08003582 * // - TLS 1.2 (0x0303)
3583 * // - TLS 1.3 (0x0304)
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003584 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003585 * select (serialized_session.tls_version) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003586 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003587 * case MBEDTLS_SSL_VERSION_TLS1_2:
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003588 * serialized_session_tls12 data;
Jerry Yu0c2a7382022-12-06 13:27:25 +08003589 * case MBEDTLS_SSL_VERSION_TLS1_3:
Jerry Yuddda0502022-12-01 19:43:12 +08003590 * serialized_session_tls13 data;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003591 *
3592 * };
3593 *
3594 * } serialized_session;
3595 *
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003596 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003597
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003598MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003599static int ssl_session_save(const mbedtls_ssl_session *session,
3600 unsigned char omit_header,
3601 unsigned char *buf,
3602 size_t buf_len,
3603 size_t *olen)
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003604{
3605 unsigned char *p = buf;
3606 size_t used = 0;
Jerry Yu251a12e2022-07-13 15:15:48 +08003607 size_t remaining_len;
Jerry Yue36fdd62022-08-17 21:31:36 +08003608#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3609 size_t out_len;
3610 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3611#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01003612 if (session == NULL) {
3613 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
3614 }
Jerry Yu438ddd82022-07-07 06:55:50 +00003615
Gilles Peskine449bd832023-01-11 14:50:10 +01003616 if (!omit_header) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003617 /*
3618 * Add Mbed TLS version identifier
3619 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003620 used += sizeof(ssl_serialized_session_header);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003621
Gilles Peskine449bd832023-01-11 14:50:10 +01003622 if (used <= buf_len) {
3623 memcpy(p, ssl_serialized_session_header,
3624 sizeof(ssl_serialized_session_header));
3625 p += sizeof(ssl_serialized_session_header);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003626 }
3627 }
3628
3629 /*
3630 * TLS version identifier
3631 */
3632 used += 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003633 if (used <= buf_len) {
3634 *p++ = MBEDTLS_BYTE_0(session->tls_version);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003635 }
3636
3637 /* Forward to version-specific serialization routine. */
Jerry Yufca4d572022-07-21 10:37:48 +08003638 remaining_len = (buf_len >= used) ? buf_len - used : 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003639 switch (session->tls_version) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003640#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01003641 case MBEDTLS_SSL_VERSION_TLS1_2:
3642 used += ssl_tls12_session_save(session, p, remaining_len);
3643 break;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003644#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3645
Jerry Yu251a12e2022-07-13 15:15:48 +08003646#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01003647 case MBEDTLS_SSL_VERSION_TLS1_3:
3648 ret = ssl_tls13_session_save(session, p, remaining_len, &out_len);
3649 if (ret != 0 && ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL) {
3650 return ret;
3651 }
3652 used += out_len;
3653 break;
Jerry Yu251a12e2022-07-13 15:15:48 +08003654#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3655
Gilles Peskine449bd832023-01-11 14:50:10 +01003656 default:
3657 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003658 }
3659
3660 *olen = used;
Gilles Peskine449bd832023-01-11 14:50:10 +01003661 if (used > buf_len) {
3662 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
3663 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003664
Gilles Peskine449bd832023-01-11 14:50:10 +01003665 return 0;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003666}
3667
3668/*
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003669 * Public wrapper for ssl_session_save()
3670 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003671int mbedtls_ssl_session_save(const mbedtls_ssl_session *session,
3672 unsigned char *buf,
3673 size_t buf_len,
3674 size_t *olen)
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003675{
Gilles Peskine449bd832023-01-11 14:50:10 +01003676 return ssl_session_save(session, 0, buf, buf_len, olen);
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003677}
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003678
Jerry Yuf1b23ca2022-02-18 11:48:47 +08003679/*
3680 * Deserialize session, see mbedtls_ssl_session_save() for format.
3681 *
3682 * This internal version is wrapped by a public function that cleans up in
3683 * case of error, and has an extra option omit_header.
3684 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003685MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003686static int ssl_session_load(mbedtls_ssl_session *session,
3687 unsigned char omit_header,
3688 const unsigned char *buf,
3689 size_t len)
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003690{
3691 const unsigned char *p = buf;
3692 const unsigned char * const end = buf + len;
Jerry Yu438ddd82022-07-07 06:55:50 +00003693 size_t remaining_len;
3694
3695
Gilles Peskine449bd832023-01-11 14:50:10 +01003696 if (session == NULL) {
3697 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
3698 }
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003699
Gilles Peskine449bd832023-01-11 14:50:10 +01003700 if (!omit_header) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003701 /*
3702 * Check Mbed TLS version identifier
3703 */
3704
Gilles Peskine449bd832023-01-11 14:50:10 +01003705 if ((size_t) (end - p) < sizeof(ssl_serialized_session_header)) {
3706 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003707 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003708
3709 if (memcmp(p, ssl_serialized_session_header,
3710 sizeof(ssl_serialized_session_header)) != 0) {
3711 return MBEDTLS_ERR_SSL_VERSION_MISMATCH;
3712 }
3713 p += sizeof(ssl_serialized_session_header);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003714 }
3715
3716 /*
3717 * TLS version identifier
3718 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003719 if (1 > (size_t) (end - p)) {
3720 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3721 }
Glenn Straussda7851c2022-03-14 13:29:48 -04003722 session->tls_version = 0x0300 | *p++;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003723
3724 /* Dispatch according to TLS version. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003725 remaining_len = (end - p);
3726 switch (session->tls_version) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003727#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01003728 case MBEDTLS_SSL_VERSION_TLS1_2:
3729 return ssl_tls12_session_load(session, p, remaining_len);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003730#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3731
Jerry Yu438ddd82022-07-07 06:55:50 +00003732#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01003733 case MBEDTLS_SSL_VERSION_TLS1_3:
3734 return ssl_tls13_session_load(session, p, remaining_len);
Jerry Yu438ddd82022-07-07 06:55:50 +00003735#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3736
Gilles Peskine449bd832023-01-11 14:50:10 +01003737 default:
3738 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003739 }
3740}
3741
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003742/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02003743 * Deserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003744 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003745int mbedtls_ssl_session_load(mbedtls_ssl_session *session,
3746 const unsigned char *buf,
3747 size_t len)
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003748{
Gilles Peskine449bd832023-01-11 14:50:10 +01003749 int ret = ssl_session_load(session, 0, buf, len);
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003750
Gilles Peskine449bd832023-01-11 14:50:10 +01003751 if (ret != 0) {
3752 mbedtls_ssl_session_free(session);
3753 }
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003754
Gilles Peskine449bd832023-01-11 14:50:10 +01003755 return ret;
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003756}
3757
3758/*
Paul Bakker1961b702013-01-25 14:49:24 +01003759 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00003760 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003761MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003762static int ssl_prepare_handshake_step(mbedtls_ssl_context *ssl)
Hanno Becker41934dd2021-08-07 19:13:43 +01003763{
3764 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3765
Ronald Cron66dbf912022-02-02 15:33:46 +01003766 /*
3767 * We may have not been able to send to the peer all the handshake data
Ronald Cron3f20b772022-03-08 16:00:02 +01003768 * that were written into the output buffer by the previous handshake step,
3769 * if the write to the network callback returned with the
Ronald Cron66dbf912022-02-02 15:33:46 +01003770 * #MBEDTLS_ERR_SSL_WANT_WRITE error code.
3771 * We proceed to the next handshake step only when all data from the
3772 * previous one have been sent to the peer, thus we make sure that this is
3773 * the case here by calling `mbedtls_ssl_flush_output()`. The function may
3774 * return with the #MBEDTLS_ERR_SSL_WANT_WRITE error code in which case
3775 * we have to wait before to go ahead.
3776 * In the case of TLS 1.3, handshake step handlers do not send data to the
3777 * peer. Data are only sent here and through
3778 * `mbedtls_ssl_handle_pending_alert` in case an error that triggered an
Andrzej Kurek5c65c572022-04-13 14:28:52 -04003779 * alert occurred.
Ronald Cron66dbf912022-02-02 15:33:46 +01003780 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003781 if ((ret = mbedtls_ssl_flush_output(ssl)) != 0) {
3782 return ret;
3783 }
Hanno Becker41934dd2021-08-07 19:13:43 +01003784
3785#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003786 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3787 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING) {
3788 if ((ret = mbedtls_ssl_flight_transmit(ssl)) != 0) {
3789 return ret;
3790 }
Hanno Becker41934dd2021-08-07 19:13:43 +01003791 }
3792#endif /* MBEDTLS_SSL_PROTO_DTLS */
3793
Gilles Peskine449bd832023-01-11 14:50:10 +01003794 return ret;
Hanno Becker41934dd2021-08-07 19:13:43 +01003795}
3796
Gilles Peskine449bd832023-01-11 14:50:10 +01003797int mbedtls_ssl_handshake_step(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00003798{
Hanno Becker41934dd2021-08-07 19:13:43 +01003799 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003800
Gilles Peskine449bd832023-01-11 14:50:10 +01003801 if (ssl == NULL ||
Hanno Becker41934dd2021-08-07 19:13:43 +01003802 ssl->conf == NULL ||
3803 ssl->handshake == NULL ||
Gilles Peskine449bd832023-01-11 14:50:10 +01003804 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER) {
3805 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Becker41934dd2021-08-07 19:13:43 +01003806 }
3807
Gilles Peskine449bd832023-01-11 14:50:10 +01003808 ret = ssl_prepare_handshake_step(ssl);
3809 if (ret != 0) {
3810 return ret;
3811 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003812
Gilles Peskine449bd832023-01-11 14:50:10 +01003813 ret = mbedtls_ssl_handle_pending_alert(ssl);
3814 if (ret != 0) {
Jerry Yue7047812021-09-13 19:26:39 +08003815 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01003816 }
Jerry Yue7047812021-09-13 19:26:39 +08003817
Tom Cosgrove2fdc7b32022-09-21 12:33:17 +01003818 /* If ssl->conf->endpoint is not one of MBEDTLS_SSL_IS_CLIENT or
3819 * MBEDTLS_SSL_IS_SERVER, this is the return code we give */
3820 ret = MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3821
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003822#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003823 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
3824 MBEDTLS_SSL_DEBUG_MSG(2, ("client state: %s",
3825 mbedtls_ssl_states_str(ssl->state)));
Jerry Yub9930e72021-08-06 17:11:51 +08003826
Gilles Peskine449bd832023-01-11 14:50:10 +01003827 switch (ssl->state) {
Ronald Cron9f0fba32022-02-10 16:45:15 +01003828 case MBEDTLS_SSL_HELLO_REQUEST:
3829 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
Tom Cosgrove87d9c6c2022-09-22 09:27:56 +01003830 ret = 0;
Ronald Cron9f0fba32022-02-10 16:45:15 +01003831 break;
Jerry Yub9930e72021-08-06 17:11:51 +08003832
Ronald Cron9f0fba32022-02-10 16:45:15 +01003833 case MBEDTLS_SSL_CLIENT_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01003834 ret = mbedtls_ssl_write_client_hello(ssl);
Ronald Cron9f0fba32022-02-10 16:45:15 +01003835 break;
3836
3837 default:
3838#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01003839 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
3840 ret = mbedtls_ssl_tls13_handshake_client_step(ssl);
3841 } else {
3842 ret = mbedtls_ssl_handshake_client_step(ssl);
3843 }
Ronald Cron9f0fba32022-02-10 16:45:15 +01003844#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01003845 ret = mbedtls_ssl_handshake_client_step(ssl);
Ronald Cron9f0fba32022-02-10 16:45:15 +01003846#else
Gilles Peskine449bd832023-01-11 14:50:10 +01003847 ret = mbedtls_ssl_tls13_handshake_client_step(ssl);
Ronald Cron9f0fba32022-02-10 16:45:15 +01003848#endif
3849 }
Jerry Yub9930e72021-08-06 17:11:51 +08003850 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003851#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003852#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003853 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Ronald Cron6f135e12021-12-08 16:57:54 +01003854#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01003855 if (mbedtls_ssl_conf_is_tls13_only(ssl->conf)) {
3856 ret = mbedtls_ssl_tls13_handshake_server_step(ssl);
3857 }
Ronald Cron6f135e12021-12-08 16:57:54 +01003858#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yub9930e72021-08-06 17:11:51 +08003859
3860#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01003861 if (mbedtls_ssl_conf_is_tls12_only(ssl->conf)) {
3862 ret = mbedtls_ssl_handshake_server_step(ssl);
3863 }
Jerry Yub9930e72021-08-06 17:11:51 +08003864#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3865 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003866#endif
3867
Gilles Peskine449bd832023-01-11 14:50:10 +01003868 if (ret != 0) {
Jerry Yubbd5a3f2021-09-18 20:50:22 +08003869 /* handshake_step return error. And it is same
3870 * with alert_reason.
3871 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003872 if (ssl->send_alert) {
3873 ret = mbedtls_ssl_handle_pending_alert(ssl);
Jerry Yue7047812021-09-13 19:26:39 +08003874 goto cleanup;
3875 }
3876 }
3877
3878cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01003879 return ret;
Paul Bakker1961b702013-01-25 14:49:24 +01003880}
3881
3882/*
3883 * Perform the SSL handshake
3884 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003885int mbedtls_ssl_handshake(mbedtls_ssl_context *ssl)
Paul Bakker1961b702013-01-25 14:49:24 +01003886{
3887 int ret = 0;
3888
Hanno Beckera817ea42020-10-20 15:20:23 +01003889 /* Sanity checks */
3890
Gilles Peskine449bd832023-01-11 14:50:10 +01003891 if (ssl == NULL || ssl->conf == NULL) {
3892 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3893 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003894
Hanno Beckera817ea42020-10-20 15:20:23 +01003895#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003896 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3897 (ssl->f_set_timer == NULL || ssl->f_get_timer == NULL)) {
3898 MBEDTLS_SSL_DEBUG_MSG(1, ("You must use "
3899 "mbedtls_ssl_set_timer_cb() for DTLS"));
3900 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Beckera817ea42020-10-20 15:20:23 +01003901 }
3902#endif /* MBEDTLS_SSL_PROTO_DTLS */
3903
Gilles Peskine449bd832023-01-11 14:50:10 +01003904 MBEDTLS_SSL_DEBUG_MSG(2, ("=> handshake"));
Paul Bakker1961b702013-01-25 14:49:24 +01003905
Hanno Beckera817ea42020-10-20 15:20:23 +01003906 /* Main handshake loop */
Gilles Peskine449bd832023-01-11 14:50:10 +01003907 while (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER) {
3908 ret = mbedtls_ssl_handshake_step(ssl);
Paul Bakker1961b702013-01-25 14:49:24 +01003909
Gilles Peskine449bd832023-01-11 14:50:10 +01003910 if (ret != 0) {
Paul Bakker1961b702013-01-25 14:49:24 +01003911 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01003912 }
Paul Bakker1961b702013-01-25 14:49:24 +01003913 }
3914
Gilles Peskine449bd832023-01-11 14:50:10 +01003915 MBEDTLS_SSL_DEBUG_MSG(2, ("<= handshake"));
Paul Bakker5121ce52009-01-03 21:22:43 +00003916
Gilles Peskine449bd832023-01-11 14:50:10 +01003917 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003918}
3919
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003920#if defined(MBEDTLS_SSL_RENEGOTIATION)
3921#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003922/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003923 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00003924 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003925MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003926static int ssl_write_hello_request(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003927{
Janos Follath865b3eb2019-12-16 11:46:15 +00003928 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003929
Gilles Peskine449bd832023-01-11 14:50:10 +01003930 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write hello request"));
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003931
3932 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003933 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3934 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003935
Gilles Peskine449bd832023-01-11 14:50:10 +01003936 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
3937 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
3938 return ret;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003939 }
3940
Gilles Peskine449bd832023-01-11 14:50:10 +01003941 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write hello request"));
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003942
Gilles Peskine449bd832023-01-11 14:50:10 +01003943 return 0;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003944}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003945#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003946
3947/*
3948 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003949 * - any side: calling mbedtls_ssl_renegotiate(),
3950 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
3951 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02003952 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003953 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003954 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003955 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003956int mbedtls_ssl_start_renegotiation(mbedtls_ssl_context *ssl)
Paul Bakker48916f92012-09-16 19:57:18 +00003957{
Janos Follath865b3eb2019-12-16 11:46:15 +00003958 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker48916f92012-09-16 19:57:18 +00003959
Gilles Peskine449bd832023-01-11 14:50:10 +01003960 MBEDTLS_SSL_DEBUG_MSG(2, ("=> renegotiate"));
Paul Bakker48916f92012-09-16 19:57:18 +00003961
Gilles Peskine449bd832023-01-11 14:50:10 +01003962 if ((ret = ssl_handshake_init(ssl)) != 0) {
3963 return ret;
3964 }
Paul Bakker48916f92012-09-16 19:57:18 +00003965
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003966 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
3967 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003968#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003969 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3970 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING) {
3971 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003972 ssl->handshake->out_msg_seq = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003973 } else {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02003974 ssl->handshake->in_msg_seq = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003975 }
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02003976 }
3977#endif
3978
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003979 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
3980 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00003981
Gilles Peskine449bd832023-01-11 14:50:10 +01003982 if ((ret = mbedtls_ssl_handshake(ssl)) != 0) {
3983 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_handshake", ret);
3984 return ret;
Paul Bakker48916f92012-09-16 19:57:18 +00003985 }
3986
Gilles Peskine449bd832023-01-11 14:50:10 +01003987 MBEDTLS_SSL_DEBUG_MSG(2, ("<= renegotiate"));
Paul Bakker48916f92012-09-16 19:57:18 +00003988
Gilles Peskine449bd832023-01-11 14:50:10 +01003989 return 0;
Paul Bakker48916f92012-09-16 19:57:18 +00003990}
3991
3992/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003993 * Renegotiate current connection on client,
3994 * or request renegotiation on server
3995 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003996int mbedtls_ssl_renegotiate(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003997{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003998 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003999
Gilles Peskine449bd832023-01-11 14:50:10 +01004000 if (ssl == NULL || ssl->conf == NULL) {
4001 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4002 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02004003
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004004#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004005 /* On server, just send the request */
Gilles Peskine449bd832023-01-11 14:50:10 +01004006 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
4007 if (mbedtls_ssl_is_handshake_over(ssl) == 0) {
4008 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4009 }
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004010
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004011 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02004012
4013 /* Did we already try/start sending HelloRequest? */
Gilles Peskine449bd832023-01-11 14:50:10 +01004014 if (ssl->out_left != 0) {
4015 return mbedtls_ssl_flush_output(ssl);
4016 }
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02004017
Gilles Peskine449bd832023-01-11 14:50:10 +01004018 return ssl_write_hello_request(ssl);
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004019 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004020#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004022#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004023 /*
4024 * On client, either start the renegotiation process or,
4025 * if already in progress, continue the handshake
4026 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004027 if (ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
4028 if (mbedtls_ssl_is_handshake_over(ssl) == 0) {
4029 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004030 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004031
4032 if ((ret = mbedtls_ssl_start_renegotiation(ssl)) != 0) {
4033 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_start_renegotiation", ret);
4034 return ret;
4035 }
4036 } else {
4037 if ((ret = mbedtls_ssl_handshake(ssl)) != 0) {
4038 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_handshake", ret);
4039 return ret;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004040 }
4041 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004042#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004043
Gilles Peskine449bd832023-01-11 14:50:10 +01004044 return ret;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004045}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004046#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004047
Gilles Peskine449bd832023-01-11 14:50:10 +01004048void mbedtls_ssl_handshake_free(mbedtls_ssl_context *ssl)
Paul Bakker48916f92012-09-16 19:57:18 +00004049{
Gilles Peskine9b562d52018-04-25 20:32:43 +02004050 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
4051
Gilles Peskine449bd832023-01-11 14:50:10 +01004052 if (handshake == NULL) {
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004053 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01004054 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004055
Brett Warrene0edc842021-08-17 09:53:13 +01004056#if defined(MBEDTLS_ECP_C)
4057#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01004058 if (ssl->handshake->group_list_heap_allocated) {
4059 mbedtls_free((void *) handshake->group_list);
4060 }
Brett Warrene0edc842021-08-17 09:53:13 +01004061 handshake->group_list = NULL;
4062#endif /* MBEDTLS_DEPRECATED_REMOVED */
4063#endif /* MBEDTLS_ECP_C */
4064
Ronald Crone68ab4f2022-10-05 12:46:29 +02004065#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +08004066#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01004067 if (ssl->handshake->sig_algs_heap_allocated) {
4068 mbedtls_free((void *) handshake->sig_algs);
4069 }
Jerry Yuf017ee42022-01-12 15:49:48 +08004070 handshake->sig_algs = NULL;
4071#endif /* MBEDTLS_DEPRECATED_REMOVED */
Xiaofei Baic234ecf2022-02-08 09:59:23 +00004072#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01004073 if (ssl->handshake->certificate_request_context) {
4074 mbedtls_free((void *) handshake->certificate_request_context);
Xiaofei Baic234ecf2022-02-08 09:59:23 +00004075 }
4076#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Ronald Crone68ab4f2022-10-05 12:46:29 +02004077#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yuf017ee42022-01-12 15:49:48 +08004078
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02004079#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01004080 if (ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0) {
4081 ssl->conf->f_async_cancel(ssl);
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02004082 handshake->async_in_progress = 0;
4083 }
4084#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
4085
Andrzej Kurek25f27152022-08-17 16:09:31 -04004086#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -05004087#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01004088 psa_hash_abort(&handshake->fin_sha256_psa);
Andrzej Kurekeb342242019-01-29 09:14:33 -05004089#else
Gilles Peskine449bd832023-01-11 14:50:10 +01004090 mbedtls_sha256_free(&handshake->fin_sha256);
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02004091#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05004092#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04004093#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Andrzej Kurekeb342242019-01-29 09:14:33 -05004094#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01004095 psa_hash_abort(&handshake->fin_sha384_psa);
Andrzej Kurekeb342242019-01-29 09:14:33 -05004096#else
Gilles Peskine449bd832023-01-11 14:50:10 +01004097 mbedtls_sha512_free(&handshake->fin_sha384);
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02004098#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05004099#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02004100
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004101#if defined(MBEDTLS_DHM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004102 mbedtls_dhm_free(&handshake->dhm_ctx);
Paul Bakker48916f92012-09-16 19:57:18 +00004103#endif
Neil Armstrongf3f46412022-04-12 14:43:39 +02004104#if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004105 mbedtls_ecdh_free(&handshake->ecdh_ctx);
Paul Bakker61d113b2013-07-04 11:51:43 +02004106#endif
Valerio Setti02c25b52022-11-15 14:08:42 +01004107
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02004108#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Neil Armstrongca7d5062022-05-31 14:43:23 +02004109#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01004110 psa_pake_abort(&handshake->psa_pake_ctx);
Valerio Settieb3f7882022-12-08 18:42:58 +01004111 /*
4112 * Opaque keys are not stored in the handshake's data and it's the user
4113 * responsibility to destroy them. Clear ones, instead, are created by
4114 * the TLS library and should be destroyed at the same level
4115 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004116 if (!mbedtls_svc_key_id_is_null(handshake->psa_pake_password)) {
4117 psa_destroy_key(handshake->psa_pake_password);
Valerio Settieb3f7882022-12-08 18:42:58 +01004118 }
Neil Armstrongca7d5062022-05-31 14:43:23 +02004119 handshake->psa_pake_password = MBEDTLS_SVC_KEY_ID_INIT;
4120#else
Gilles Peskine449bd832023-01-11 14:50:10 +01004121 mbedtls_ecjpake_free(&handshake->ecjpake_ctx);
Neil Armstrongca7d5062022-05-31 14:43:23 +02004122#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02004123#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004124 mbedtls_free(handshake->ecjpake_cache);
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02004125 handshake->ecjpake_cache = NULL;
4126 handshake->ecjpake_cache_len = 0;
4127#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02004128#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02004129
Janos Follath4ae5c292016-02-10 11:27:43 +00004130#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
4131 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02004132 /* explicit void pointer cast for buggy MS compiler */
Gilles Peskine449bd832023-01-11 14:50:10 +01004133 mbedtls_free((void *) handshake->curves_tls_id);
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004134#endif
4135
Ronald Cron73fe8df2022-10-05 14:31:43 +02004136#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02004137#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01004138 if (!mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque)) {
Neil Armstrong501c9322022-05-03 09:35:09 +02004139 /* The maintenance of the external PSK key slot is the
4140 * user's responsibility. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004141 if (ssl->handshake->psk_opaque_is_internal) {
4142 psa_destroy_key(ssl->handshake->psk_opaque);
Neil Armstrong501c9322022-05-03 09:35:09 +02004143 ssl->handshake->psk_opaque_is_internal = 0;
4144 }
4145 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
4146 }
Neil Armstronge952a302022-05-03 10:22:14 +02004147#else
Gilles Peskine449bd832023-01-11 14:50:10 +01004148 if (handshake->psk != NULL) {
4149 mbedtls_platform_zeroize(handshake->psk, handshake->psk_len);
4150 mbedtls_free(handshake->psk);
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004151 }
Neil Armstronge952a302022-05-03 10:22:14 +02004152#endif /* MBEDTLS_USE_PSA_CRYPTO */
Ronald Cron73fe8df2022-10-05 14:31:43 +02004153#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004154
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004155#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
4156 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02004157 /*
4158 * Free only the linked list wrapper, not the keys themselves
4159 * since the belong to the SNI callback
4160 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004161 ssl_key_cert_free(handshake->sni_key_cert);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004162#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004163
Gilles Peskineeccd8882020-03-10 12:19:08 +01004164#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01004165 mbedtls_x509_crt_restart_free(&handshake->ecrs_ctx);
4166 if (handshake->ecrs_peer_cert != NULL) {
4167 mbedtls_x509_crt_free(handshake->ecrs_peer_cert);
4168 mbedtls_free(handshake->ecrs_peer_cert);
Hanno Becker3dad3112019-02-05 17:19:52 +00004169 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02004170#endif
4171
Hanno Becker75173122019-02-06 16:18:31 +00004172#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
4173 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01004174 mbedtls_pk_free(&handshake->peer_pubkey);
Hanno Becker75173122019-02-06 16:18:31 +00004175#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
4176
XiaokangQian9b93c0d2022-02-09 06:02:25 +00004177#if defined(MBEDTLS_SSL_CLI_C) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01004178 (defined(MBEDTLS_SSL_PROTO_DTLS) || defined(MBEDTLS_SSL_PROTO_TLS1_3))
4179 mbedtls_free(handshake->cookie);
XiaokangQian9b93c0d2022-02-09 06:02:25 +00004180#endif /* MBEDTLS_SSL_CLI_C &&
4181 ( MBEDTLS_SSL_PROTO_DTLS || MBEDTLS_SSL_PROTO_TLS1_3 ) */
XiaokangQian8499b6c2022-01-27 09:00:11 +00004182
4183#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004184 mbedtls_ssl_flight_free(handshake->flight);
4185 mbedtls_ssl_buffering_free(ssl);
XiaokangQian8499b6c2022-01-27 09:00:11 +00004186#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02004187
Ronald Cronf12b81d2022-03-15 10:42:41 +01004188#if defined(MBEDTLS_ECDH_C) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01004189 (defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3))
4190 if (handshake->ecdh_psa_privkey_is_external == 0) {
4191 psa_destroy_key(handshake->ecdh_psa_privkey);
4192 }
Hanno Becker4a63ed42019-01-08 11:39:35 +00004193#endif /* MBEDTLS_ECDH_C && MBEDTLS_USE_PSA_CRYPTO */
4194
Ronald Cron6f135e12021-12-08 16:57:54 +01004195#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01004196 mbedtls_ssl_transform_free(handshake->transform_handshake);
4197 mbedtls_free(handshake->transform_handshake);
Jerry Yu3d9b5902022-11-04 14:07:25 +08004198#if defined(MBEDTLS_SSL_EARLY_DATA)
Gilles Peskine449bd832023-01-11 14:50:10 +01004199 mbedtls_ssl_transform_free(handshake->transform_earlydata);
4200 mbedtls_free(handshake->transform_earlydata);
Jerry Yu3d9b5902022-11-04 14:07:25 +08004201#endif
Ronald Cron6f135e12021-12-08 16:57:54 +01004202#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yuba9c7272021-10-30 11:54:10 +08004203
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004204
4205#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4206 /* If the buffers are too big - reallocate. Because of the way Mbed TLS
4207 * processes datagrams and the fact that a datagram is allowed to have
4208 * several records in it, it is possible that the I/O buffers are not
4209 * empty at this stage */
Gilles Peskine449bd832023-01-11 14:50:10 +01004210 handle_buffer_resizing(ssl, 1, mbedtls_ssl_get_input_buflen(ssl),
4211 mbedtls_ssl_get_output_buflen(ssl));
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004212#endif
Hanno Becker3aa186f2021-08-10 09:24:19 +01004213
Jerry Yuba9c7272021-10-30 11:54:10 +08004214 /* mbedtls_platform_zeroize MUST be last one in this function */
Gilles Peskine449bd832023-01-11 14:50:10 +01004215 mbedtls_platform_zeroize(handshake,
4216 sizeof(mbedtls_ssl_handshake_params));
Paul Bakker48916f92012-09-16 19:57:18 +00004217}
4218
Gilles Peskine449bd832023-01-11 14:50:10 +01004219void mbedtls_ssl_session_free(mbedtls_ssl_session *session)
Paul Bakker48916f92012-09-16 19:57:18 +00004220{
Gilles Peskine449bd832023-01-11 14:50:10 +01004221 if (session == NULL) {
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004222 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01004223 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004224
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004225#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004226 ssl_clear_peer_cert(session);
Paul Bakkered27a042013-04-18 22:46:23 +02004227#endif
Paul Bakker0a597072012-09-25 21:55:46 +00004228
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02004229#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Xiaokang Qianbc663a02022-10-09 11:14:39 +00004230#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
4231 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01004232 mbedtls_free(session->hostname);
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00004233#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01004234 mbedtls_free(session->ticket);
Paul Bakkera503a632013-08-14 13:48:06 +02004235#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004236
Gilles Peskine449bd832023-01-11 14:50:10 +01004237 mbedtls_platform_zeroize(session, sizeof(mbedtls_ssl_session));
Paul Bakker48916f92012-09-16 19:57:18 +00004238}
4239
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02004240#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004241
4242#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4243#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u
4244#else
4245#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u
4246#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4247
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004248#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004249
4250#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4251#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u
4252#else
4253#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u
4254#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4255
4256#if defined(MBEDTLS_SSL_ALPN)
4257#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u
4258#else
4259#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u
4260#endif /* MBEDTLS_SSL_ALPN */
4261
4262#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0
4263#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1
4264#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2
4265#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3
4266
4267#define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \
Gilles Peskine449bd832023-01-11 14:50:10 +01004268 ((uint32_t) ( \
4269 (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << \
4270 SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT) | \
4271 (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << \
4272 SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT) | \
4273 (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << \
4274 SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT) | \
4275 (SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT) | \
4276 0u))
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004277
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004278static unsigned char ssl_serialized_context_header[] = {
4279 MBEDTLS_VERSION_MAJOR,
4280 MBEDTLS_VERSION_MINOR,
4281 MBEDTLS_VERSION_PATCH,
Gilles Peskine449bd832023-01-11 14:50:10 +01004282 MBEDTLS_BYTE_1(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
4283 MBEDTLS_BYTE_0(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
4284 MBEDTLS_BYTE_2(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG),
4285 MBEDTLS_BYTE_1(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG),
4286 MBEDTLS_BYTE_0(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG),
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004287};
4288
Paul Bakker5121ce52009-01-03 21:22:43 +00004289/*
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004290 * Serialize a full SSL context
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02004291 *
4292 * The format of the serialized data is:
4293 * (in the presentation language of TLS, RFC 8446 section 3)
4294 *
4295 * // header
4296 * opaque mbedtls_version[3]; // major, minor, patch
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004297 * opaque context_format[5]; // version-specific field determining
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02004298 * // the format of the remaining
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004299 * // serialized data.
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004300 * Note: When updating the format, remember to keep these
4301 * version+format bytes. (We may make their size part of the API.)
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02004302 *
4303 * // session sub-structure
4304 * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save()
4305 * // transform sub-structure
4306 * uint8 random[64]; // ServerHello.random+ClientHello.random
4307 * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value
4308 * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use
4309 * // fields from ssl_context
4310 * uint32 badmac_seen; // DTLS: number of records with failing MAC
4311 * uint64 in_window_top; // DTLS: last validated record seq_num
4312 * uint64 in_window; // DTLS: bitmask for replay protection
4313 * uint8 disable_datagram_packing; // DTLS: only one record per datagram
4314 * uint64 cur_out_ctr; // Record layer: outgoing sequence number
4315 * uint16 mtu; // DTLS: path mtu (max outgoing fragment size)
4316 * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol
4317 *
4318 * Note that many fields of the ssl_context or sub-structures are not
4319 * serialized, as they fall in one of the following categories:
4320 *
4321 * 1. forced value (eg in_left must be 0)
4322 * 2. pointer to dynamically-allocated memory (eg session, transform)
4323 * 3. value can be re-derived from other data (eg session keys from MS)
4324 * 4. value was temporary (eg content of input buffer)
4325 * 5. value will be provided by the user again (eg I/O callbacks and context)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004326 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004327int mbedtls_ssl_context_save(mbedtls_ssl_context *ssl,
4328 unsigned char *buf,
4329 size_t buf_len,
4330 size_t *olen)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004331{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004332 unsigned char *p = buf;
4333 size_t used = 0;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004334 size_t session_len;
4335 int ret = 0;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004336
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02004337 /*
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004338 * Enforce usage restrictions, see "return BAD_INPUT_DATA" in
4339 * this function's documentation.
4340 *
4341 * These are due to assumptions/limitations in the implementation. Some of
4342 * them are likely to stay (no handshake in progress) some might go away
4343 * (only DTLS) but are currently used to simplify the implementation.
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02004344 */
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004345 /* The initial handshake must be over */
Gilles Peskine449bd832023-01-11 14:50:10 +01004346 if (mbedtls_ssl_is_handshake_over(ssl) == 0) {
4347 MBEDTLS_SSL_DEBUG_MSG(1, ("Initial handshake isn't over"));
4348 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004349 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004350 if (ssl->handshake != NULL) {
4351 MBEDTLS_SSL_DEBUG_MSG(1, ("Handshake isn't completed"));
4352 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004353 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004354 /* Double-check that sub-structures are indeed ready */
Gilles Peskine449bd832023-01-11 14:50:10 +01004355 if (ssl->transform == NULL || ssl->session == NULL) {
4356 MBEDTLS_SSL_DEBUG_MSG(1, ("Serialised structures aren't ready"));
4357 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004358 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004359 /* There must be no pending incoming or outgoing data */
Gilles Peskine449bd832023-01-11 14:50:10 +01004360 if (mbedtls_ssl_check_pending(ssl) != 0) {
4361 MBEDTLS_SSL_DEBUG_MSG(1, ("There is pending incoming data"));
4362 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004363 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004364 if (ssl->out_left != 0) {
4365 MBEDTLS_SSL_DEBUG_MSG(1, ("There is pending outgoing data"));
4366 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004367 }
Dave Rodgman556e8a32022-12-06 16:31:25 +00004368 /* Protocol must be DTLS, not TLS */
Gilles Peskine449bd832023-01-11 14:50:10 +01004369 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
4370 MBEDTLS_SSL_DEBUG_MSG(1, ("Only DTLS is supported"));
4371 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004372 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004373 /* Version must be 1.2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004374 if (ssl->tls_version != MBEDTLS_SSL_VERSION_TLS1_2) {
4375 MBEDTLS_SSL_DEBUG_MSG(1, ("Only version 1.2 supported"));
4376 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004377 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004378 /* We must be using an AEAD ciphersuite */
Gilles Peskine449bd832023-01-11 14:50:10 +01004379 if (mbedtls_ssl_transform_uses_aead(ssl->transform) != 1) {
4380 MBEDTLS_SSL_DEBUG_MSG(1, ("Only AEAD ciphersuites supported"));
4381 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004382 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004383 /* Renegotiation must not be enabled */
4384#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01004385 if (ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED) {
4386 MBEDTLS_SSL_DEBUG_MSG(1, ("Renegotiation must not be enabled"));
4387 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004388 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004389#endif
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004390
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004391 /*
4392 * Version and format identifier
4393 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004394 used += sizeof(ssl_serialized_context_header);
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004395
Gilles Peskine449bd832023-01-11 14:50:10 +01004396 if (used <= buf_len) {
4397 memcpy(p, ssl_serialized_context_header,
4398 sizeof(ssl_serialized_context_header));
4399 p += sizeof(ssl_serialized_context_header);
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004400 }
4401
4402 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004403 * Session (length + data)
4404 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004405 ret = ssl_session_save(ssl->session, 1, NULL, 0, &session_len);
4406 if (ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL) {
4407 return ret;
4408 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004409
4410 used += 4 + session_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004411 if (used <= buf_len) {
4412 MBEDTLS_PUT_UINT32_BE(session_len, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004413 p += 4;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004414
Gilles Peskine449bd832023-01-11 14:50:10 +01004415 ret = ssl_session_save(ssl->session, 1,
4416 p, session_len, &session_len);
4417 if (ret != 0) {
4418 return ret;
4419 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004420
4421 p += session_len;
4422 }
4423
4424 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004425 * Transform
4426 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004427 used += sizeof(ssl->transform->randbytes);
4428 if (used <= buf_len) {
4429 memcpy(p, ssl->transform->randbytes,
4430 sizeof(ssl->transform->randbytes));
4431 p += sizeof(ssl->transform->randbytes);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004432 }
4433
4434#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4435 used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004436 if (used <= buf_len) {
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004437 *p++ = ssl->transform->in_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004438 memcpy(p, ssl->transform->in_cid, ssl->transform->in_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004439 p += ssl->transform->in_cid_len;
4440
4441 *p++ = ssl->transform->out_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004442 memcpy(p, ssl->transform->out_cid, ssl->transform->out_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004443 p += ssl->transform->out_cid_len;
4444 }
4445#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4446
4447 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004448 * Saved fields from top-level ssl_context structure
4449 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004450 used += 4;
Gilles Peskine449bd832023-01-11 14:50:10 +01004451 if (used <= buf_len) {
4452 MBEDTLS_PUT_UINT32_BE(ssl->badmac_seen, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004453 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004454 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004455
4456#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4457 used += 16;
Gilles Peskine449bd832023-01-11 14:50:10 +01004458 if (used <= buf_len) {
4459 MBEDTLS_PUT_UINT64_BE(ssl->in_window_top, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004460 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004461
Gilles Peskine449bd832023-01-11 14:50:10 +01004462 MBEDTLS_PUT_UINT64_BE(ssl->in_window, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004463 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004464 }
4465#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4466
4467#if defined(MBEDTLS_SSL_PROTO_DTLS)
4468 used += 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004469 if (used <= buf_len) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004470 *p++ = ssl->disable_datagram_packing;
4471 }
4472#endif /* MBEDTLS_SSL_PROTO_DTLS */
4473
Jerry Yuae0b2e22021-10-08 15:21:19 +08004474 used += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Gilles Peskine449bd832023-01-11 14:50:10 +01004475 if (used <= buf_len) {
4476 memcpy(p, ssl->cur_out_ctr, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN);
Jerry Yuae0b2e22021-10-08 15:21:19 +08004477 p += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004478 }
4479
4480#if defined(MBEDTLS_SSL_PROTO_DTLS)
4481 used += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01004482 if (used <= buf_len) {
4483 MBEDTLS_PUT_UINT16_BE(ssl->mtu, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004484 p += 2;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004485 }
4486#endif /* MBEDTLS_SSL_PROTO_DTLS */
4487
4488#if defined(MBEDTLS_SSL_ALPN)
4489 {
4490 const uint8_t alpn_len = ssl->alpn_chosen
Gilles Peskine449bd832023-01-11 14:50:10 +01004491 ? (uint8_t) strlen(ssl->alpn_chosen)
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004492 : 0;
4493
4494 used += 1 + alpn_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004495 if (used <= buf_len) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004496 *p++ = alpn_len;
4497
Gilles Peskine449bd832023-01-11 14:50:10 +01004498 if (ssl->alpn_chosen != NULL) {
4499 memcpy(p, ssl->alpn_chosen, alpn_len);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004500 p += alpn_len;
4501 }
4502 }
4503 }
4504#endif /* MBEDTLS_SSL_ALPN */
4505
4506 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004507 * Done
4508 */
4509 *olen = used;
4510
Gilles Peskine449bd832023-01-11 14:50:10 +01004511 if (used > buf_len) {
4512 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
4513 }
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004514
Gilles Peskine449bd832023-01-11 14:50:10 +01004515 MBEDTLS_SSL_DEBUG_BUF(4, "saved context", buf, used);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004516
Gilles Peskine449bd832023-01-11 14:50:10 +01004517 return mbedtls_ssl_session_reset_int(ssl, 0);
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004518}
4519
4520/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02004521 * Deserialize context, see mbedtls_ssl_context_save() for format.
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004522 *
4523 * This internal version is wrapped by a public function that cleans up in
4524 * case of error.
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004525 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004526MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01004527static int ssl_context_load(mbedtls_ssl_context *ssl,
4528 const unsigned char *buf,
4529 size_t len)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004530{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004531 const unsigned char *p = buf;
4532 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004533 size_t session_len;
Janos Follath865b3eb2019-12-16 11:46:15 +00004534 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004535#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek894edde2022-09-29 06:31:14 -04004536 tls_prf_fn prf_func = NULL;
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004537#endif
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004538
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004539 /*
4540 * The context should have been freshly setup or reset.
4541 * Give the user an error in case of obvious misuse.
Manuel Pégourié-Gonnard4ca930f2019-07-26 16:31:53 +02004542 * (Checking session is useful because it won't be NULL if we're
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004543 * renegotiating, or if the user mistakenly loaded a session first.)
4544 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004545 if (ssl->state != MBEDTLS_SSL_HELLO_REQUEST ||
4546 ssl->session != NULL) {
4547 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004548 }
4549
4550 /*
4551 * We can't check that the config matches the initial one, but we can at
4552 * least check it matches the requirements for serializing.
4553 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004554 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
Glenn Strauss2dfcea22022-03-14 17:26:42 -04004555 ssl->conf->max_tls_version < MBEDTLS_SSL_VERSION_TLS1_2 ||
4556 ssl->conf->min_tls_version > MBEDTLS_SSL_VERSION_TLS1_2 ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004557#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02004558 ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004559#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01004560 0) {
4561 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004562 }
4563
Gilles Peskine449bd832023-01-11 14:50:10 +01004564 MBEDTLS_SSL_DEBUG_BUF(4, "context to load", buf, len);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004565
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004566 /*
4567 * Check version identifier
4568 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004569 if ((size_t) (end - p) < sizeof(ssl_serialized_context_header)) {
4570 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004571 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004572
4573 if (memcmp(p, ssl_serialized_context_header,
4574 sizeof(ssl_serialized_context_header)) != 0) {
4575 return MBEDTLS_ERR_SSL_VERSION_MISMATCH;
4576 }
4577 p += sizeof(ssl_serialized_context_header);
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004578
4579 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004580 * Session
4581 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004582 if ((size_t) (end - p) < 4) {
4583 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4584 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004585
Gilles Peskine449bd832023-01-11 14:50:10 +01004586 session_len = ((size_t) p[0] << 24) |
4587 ((size_t) p[1] << 16) |
4588 ((size_t) p[2] << 8) |
4589 ((size_t) p[3]);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004590 p += 4;
4591
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004592 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00004593 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004594 ssl->session = ssl->session_negotiate;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004595 ssl->session_in = ssl->session;
4596 ssl->session_out = ssl->session;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004597 ssl->session_negotiate = NULL;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004598
Gilles Peskine449bd832023-01-11 14:50:10 +01004599 if ((size_t) (end - p) < session_len) {
4600 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4601 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004602
Gilles Peskine449bd832023-01-11 14:50:10 +01004603 ret = ssl_session_load(ssl->session, 1, p, session_len);
4604 if (ret != 0) {
4605 mbedtls_ssl_session_free(ssl->session);
4606 return ret;
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004607 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004608
4609 p += session_len;
4610
4611 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004612 * Transform
4613 */
4614
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004615 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00004616 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Jerry Yu2e199812022-12-01 18:57:19 +08004617#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004618 ssl->transform = ssl->transform_negotiate;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004619 ssl->transform_in = ssl->transform;
4620 ssl->transform_out = ssl->transform;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004621 ssl->transform_negotiate = NULL;
Jerry Yu2e199812022-12-01 18:57:19 +08004622#endif
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004623
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004624#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01004625 prf_func = ssl_tls12prf_from_cs(ssl->session->ciphersuite);
4626 if (prf_func == NULL) {
4627 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4628 }
Andrzej Kurek894edde2022-09-29 06:31:14 -04004629
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004630 /* Read random bytes and populate structure */
Gilles Peskine449bd832023-01-11 14:50:10 +01004631 if ((size_t) (end - p) < sizeof(ssl->transform->randbytes)) {
4632 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4633 }
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004634
Gilles Peskine449bd832023-01-11 14:50:10 +01004635 ret = ssl_tls12_populate_transform(ssl->transform,
4636 ssl->session->ciphersuite,
4637 ssl->session->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02004638#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004639 ssl->session->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02004640#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01004641 prf_func,
4642 p, /* currently pointing to randbytes */
4643 MBEDTLS_SSL_VERSION_TLS1_2, /* (D)TLS 1.2 is forced */
4644 ssl->conf->endpoint,
4645 ssl);
4646 if (ret != 0) {
4647 return ret;
4648 }
Jerry Yu840fbb22022-02-17 14:59:29 +08004649#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004650 p += sizeof(ssl->transform->randbytes);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004651
4652#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4653 /* Read connection IDs and store them */
Gilles Peskine449bd832023-01-11 14:50:10 +01004654 if ((size_t) (end - p) < 1) {
4655 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4656 }
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004657
4658 ssl->transform->in_cid_len = *p++;
4659
Gilles Peskine449bd832023-01-11 14:50:10 +01004660 if ((size_t) (end - p) < ssl->transform->in_cid_len + 1u) {
4661 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4662 }
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004663
Gilles Peskine449bd832023-01-11 14:50:10 +01004664 memcpy(ssl->transform->in_cid, p, ssl->transform->in_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004665 p += ssl->transform->in_cid_len;
4666
4667 ssl->transform->out_cid_len = *p++;
4668
Gilles Peskine449bd832023-01-11 14:50:10 +01004669 if ((size_t) (end - p) < ssl->transform->out_cid_len) {
4670 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4671 }
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004672
Gilles Peskine449bd832023-01-11 14:50:10 +01004673 memcpy(ssl->transform->out_cid, p, ssl->transform->out_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004674 p += ssl->transform->out_cid_len;
4675#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4676
4677 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004678 * Saved fields from top-level ssl_context structure
4679 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004680 if ((size_t) (end - p) < 4) {
4681 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4682 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004683
Gilles Peskine449bd832023-01-11 14:50:10 +01004684 ssl->badmac_seen = ((uint32_t) p[0] << 24) |
4685 ((uint32_t) p[1] << 16) |
4686 ((uint32_t) p[2] << 8) |
4687 ((uint32_t) p[3]);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004688 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004689
4690#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine449bd832023-01-11 14:50:10 +01004691 if ((size_t) (end - p) < 16) {
4692 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4693 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004694
Gilles Peskine449bd832023-01-11 14:50:10 +01004695 ssl->in_window_top = ((uint64_t) p[0] << 56) |
4696 ((uint64_t) p[1] << 48) |
4697 ((uint64_t) p[2] << 40) |
4698 ((uint64_t) p[3] << 32) |
4699 ((uint64_t) p[4] << 24) |
4700 ((uint64_t) p[5] << 16) |
4701 ((uint64_t) p[6] << 8) |
4702 ((uint64_t) p[7]);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004703 p += 8;
4704
Gilles Peskine449bd832023-01-11 14:50:10 +01004705 ssl->in_window = ((uint64_t) p[0] << 56) |
4706 ((uint64_t) p[1] << 48) |
4707 ((uint64_t) p[2] << 40) |
4708 ((uint64_t) p[3] << 32) |
4709 ((uint64_t) p[4] << 24) |
4710 ((uint64_t) p[5] << 16) |
4711 ((uint64_t) p[6] << 8) |
4712 ((uint64_t) p[7]);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004713 p += 8;
4714#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4715
4716#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004717 if ((size_t) (end - p) < 1) {
4718 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4719 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004720
4721 ssl->disable_datagram_packing = *p++;
4722#endif /* MBEDTLS_SSL_PROTO_DTLS */
4723
Gilles Peskine449bd832023-01-11 14:50:10 +01004724 if ((size_t) (end - p) < sizeof(ssl->cur_out_ctr)) {
4725 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4726 }
4727 memcpy(ssl->cur_out_ctr, p, sizeof(ssl->cur_out_ctr));
4728 p += sizeof(ssl->cur_out_ctr);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004729
4730#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004731 if ((size_t) (end - p) < 2) {
4732 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4733 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004734
Gilles Peskine449bd832023-01-11 14:50:10 +01004735 ssl->mtu = (p[0] << 8) | p[1];
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004736 p += 2;
4737#endif /* MBEDTLS_SSL_PROTO_DTLS */
4738
4739#if defined(MBEDTLS_SSL_ALPN)
4740 {
4741 uint8_t alpn_len;
4742 const char **cur;
4743
Gilles Peskine449bd832023-01-11 14:50:10 +01004744 if ((size_t) (end - p) < 1) {
4745 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4746 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004747
4748 alpn_len = *p++;
4749
Gilles Peskine449bd832023-01-11 14:50:10 +01004750 if (alpn_len != 0 && ssl->conf->alpn_list != NULL) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004751 /* alpn_chosen should point to an item in the configured list */
Gilles Peskine449bd832023-01-11 14:50:10 +01004752 for (cur = ssl->conf->alpn_list; *cur != NULL; cur++) {
4753 if (strlen(*cur) == alpn_len &&
4754 memcmp(p, cur, alpn_len) == 0) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004755 ssl->alpn_chosen = *cur;
4756 break;
4757 }
4758 }
4759 }
4760
4761 /* can only happen on conf mismatch */
Gilles Peskine449bd832023-01-11 14:50:10 +01004762 if (alpn_len != 0 && ssl->alpn_chosen == NULL) {
4763 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4764 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004765
4766 p += alpn_len;
4767 }
4768#endif /* MBEDTLS_SSL_ALPN */
4769
4770 /*
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004771 * Forced fields from top-level ssl_context structure
4772 *
4773 * Most of them already set to the correct value by mbedtls_ssl_init() and
4774 * mbedtls_ssl_reset(), so we only need to set the remaining ones.
4775 */
4776 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
Glenn Strauss60bfe602022-03-14 19:04:24 -04004777 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004778
Hanno Becker361b10d2019-08-30 10:42:49 +01004779 /* Adjust pointers for header fields of outgoing records to
4780 * the given transform, accounting for explicit IV and CID. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004781 mbedtls_ssl_update_out_pointers(ssl, ssl->transform);
Hanno Becker361b10d2019-08-30 10:42:49 +01004782
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004783#if defined(MBEDTLS_SSL_PROTO_DTLS)
4784 ssl->in_epoch = 1;
4785#endif
4786
4787 /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated,
4788 * which we don't want - otherwise we'd end up freeing the wrong transform
Hanno Beckerce5f5fd2020-02-05 10:47:44 +00004789 * by calling mbedtls_ssl_handshake_wrapup_free_hs_transform()
4790 * inappropriately. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004791 if (ssl->handshake != NULL) {
4792 mbedtls_ssl_handshake_free(ssl);
4793 mbedtls_free(ssl->handshake);
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004794 ssl->handshake = NULL;
4795 }
4796
4797 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004798 * Done - should have consumed entire buffer
4799 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004800 if (p != end) {
4801 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4802 }
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004803
Gilles Peskine449bd832023-01-11 14:50:10 +01004804 return 0;
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004805}
4806
4807/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02004808 * Deserialize context: public wrapper for error cleaning
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004809 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004810int mbedtls_ssl_context_load(mbedtls_ssl_context *context,
4811 const unsigned char *buf,
4812 size_t len)
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004813{
Gilles Peskine449bd832023-01-11 14:50:10 +01004814 int ret = ssl_context_load(context, buf, len);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004815
Gilles Peskine449bd832023-01-11 14:50:10 +01004816 if (ret != 0) {
4817 mbedtls_ssl_free(context);
4818 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004819
Gilles Peskine449bd832023-01-11 14:50:10 +01004820 return ret;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004821}
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02004822#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004823
4824/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004825 * Free an SSL context
4826 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004827void mbedtls_ssl_free(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00004828{
Gilles Peskine449bd832023-01-11 14:50:10 +01004829 if (ssl == NULL) {
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004830 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01004831 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004832
Gilles Peskine449bd832023-01-11 14:50:10 +01004833 MBEDTLS_SSL_DEBUG_MSG(2, ("=> free"));
Paul Bakker5121ce52009-01-03 21:22:43 +00004834
Gilles Peskine449bd832023-01-11 14:50:10 +01004835 if (ssl->out_buf != NULL) {
sander-visserb8aa2072020-05-06 22:05:13 +02004836#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4837 size_t out_buf_len = ssl->out_buf_len;
4838#else
4839 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
4840#endif
4841
Gilles Peskine449bd832023-01-11 14:50:10 +01004842 mbedtls_platform_zeroize(ssl->out_buf, out_buf_len);
4843 mbedtls_free(ssl->out_buf);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004844 ssl->out_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004845 }
4846
Gilles Peskine449bd832023-01-11 14:50:10 +01004847 if (ssl->in_buf != NULL) {
sander-visserb8aa2072020-05-06 22:05:13 +02004848#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4849 size_t in_buf_len = ssl->in_buf_len;
4850#else
4851 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
4852#endif
4853
Gilles Peskine449bd832023-01-11 14:50:10 +01004854 mbedtls_platform_zeroize(ssl->in_buf, in_buf_len);
4855 mbedtls_free(ssl->in_buf);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004856 ssl->in_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004857 }
4858
Gilles Peskine449bd832023-01-11 14:50:10 +01004859 if (ssl->transform) {
4860 mbedtls_ssl_transform_free(ssl->transform);
4861 mbedtls_free(ssl->transform);
Paul Bakker48916f92012-09-16 19:57:18 +00004862 }
4863
Gilles Peskine449bd832023-01-11 14:50:10 +01004864 if (ssl->handshake) {
4865 mbedtls_ssl_handshake_free(ssl);
4866 mbedtls_free(ssl->handshake);
Jerry Yu2e199812022-12-01 18:57:19 +08004867
4868#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01004869 mbedtls_ssl_transform_free(ssl->transform_negotiate);
4870 mbedtls_free(ssl->transform_negotiate);
Jerry Yu2e199812022-12-01 18:57:19 +08004871#endif
4872
Gilles Peskine449bd832023-01-11 14:50:10 +01004873 mbedtls_ssl_session_free(ssl->session_negotiate);
4874 mbedtls_free(ssl->session_negotiate);
Paul Bakker48916f92012-09-16 19:57:18 +00004875 }
4876
Ronald Cron6f135e12021-12-08 16:57:54 +01004877#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01004878 mbedtls_ssl_transform_free(ssl->transform_application);
4879 mbedtls_free(ssl->transform_application);
Ronald Cron6f135e12021-12-08 16:57:54 +01004880#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker3aa186f2021-08-10 09:24:19 +01004881
Gilles Peskine449bd832023-01-11 14:50:10 +01004882 if (ssl->session) {
4883 mbedtls_ssl_session_free(ssl->session);
4884 mbedtls_free(ssl->session);
Paul Bakkerc0463502013-02-14 11:19:38 +01004885 }
4886
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02004887#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004888 if (ssl->hostname != NULL) {
4889 mbedtls_platform_zeroize(ssl->hostname, strlen(ssl->hostname));
4890 mbedtls_free(ssl->hostname);
Paul Bakker5121ce52009-01-03 21:22:43 +00004891 }
Paul Bakker0be444a2013-08-27 21:55:01 +02004892#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004893
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02004894#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004895 mbedtls_free(ssl->cli_id);
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02004896#endif
4897
Gilles Peskine449bd832023-01-11 14:50:10 +01004898 MBEDTLS_SSL_DEBUG_MSG(2, ("<= free"));
Paul Bakker2da561c2009-02-05 18:00:28 +00004899
Paul Bakker86f04f42013-02-14 11:20:09 +01004900 /* Actually clear after last debug message */
Gilles Peskine449bd832023-01-11 14:50:10 +01004901 mbedtls_platform_zeroize(ssl, sizeof(mbedtls_ssl_context));
Paul Bakker5121ce52009-01-03 21:22:43 +00004902}
4903
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004904/*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08004905 * Initialize mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004906 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004907void mbedtls_ssl_config_init(mbedtls_ssl_config *conf)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004908{
Gilles Peskine449bd832023-01-11 14:50:10 +01004909 memset(conf, 0, sizeof(mbedtls_ssl_config));
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004910}
4911
Gilles Peskineae270bf2021-06-02 00:05:29 +02004912/* The selection should be the same as mbedtls_x509_crt_profile_default in
4913 * x509_crt.c, plus Montgomery curves for ECDHE. Here, the order matters:
Gilles Peskineb1940a72021-06-02 15:18:12 +02004914 * curves with a lower resource usage come first.
Gilles Peskineae270bf2021-06-02 00:05:29 +02004915 * See the documentation of mbedtls_ssl_conf_curves() for what we promise
Gilles Peskineb1940a72021-06-02 15:18:12 +02004916 * about this list.
4917 */
Brett Warrene0edc842021-08-17 09:53:13 +01004918static uint16_t ssl_preset_default_groups[] = {
Gilles Peskineae270bf2021-06-02 00:05:29 +02004919#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004920 MBEDTLS_SSL_IANA_TLS_GROUP_X25519,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004921#endif
4922#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004923 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004924#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02004925#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004926 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004927#endif
4928#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004929 MBEDTLS_SSL_IANA_TLS_GROUP_X448,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004930#endif
4931#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004932 MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004933#endif
Gilles Peskineae270bf2021-06-02 00:05:29 +02004934#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004935 MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004936#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02004937#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004938 MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004939#endif
4940#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004941 MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004942#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004943 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Gilles Peskineae270bf2021-06-02 00:05:29 +02004944};
Gilles Peskineae270bf2021-06-02 00:05:29 +02004945
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004946static int ssl_preset_suiteb_ciphersuites[] = {
4947 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
4948 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
4949 0
4950};
4951
Ronald Crone68ab4f2022-10-05 12:46:29 +02004952#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004953
Jerry Yu909df7b2022-01-22 11:56:27 +08004954/* NOTICE:
Jerry Yu0b994b82022-01-25 17:22:12 +08004955 * For ssl_preset_*_sig_algs and ssl_tls12_preset_*_sig_algs, the following
Jerry Yu370e1462022-01-25 10:36:53 +08004956 * rules SHOULD be upheld.
4957 * - No duplicate entries.
4958 * - But if there is a good reason, do not change the order of the algorithms.
Jerry Yu09a99fc2022-07-28 14:22:17 +08004959 * - ssl_tls12_preset* is for TLS 1.2 use only.
Jerry Yu370e1462022-01-25 10:36:53 +08004960 * - ssl_preset_* is for TLS 1.3 only or hybrid TLS 1.3/1.2 handshakes.
Jerry Yu1a8b4812022-01-20 17:56:50 +08004961 */
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004962static uint16_t ssl_preset_default_sig_algs[] = {
Jerry Yu1a8b4812022-01-20 17:56:50 +08004963
Andrzej Kurek25f27152022-08-17 16:09:31 -04004964#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 +08004965 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
4966 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004967#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA &&
Jerry Yued5e9f42022-01-26 11:21:34 +08004968 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004969
Andrzej Kurek25f27152022-08-17 16:09:31 -04004970#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 +08004971 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
4972 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004973#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu909df7b2022-01-22 11:56:27 +08004974 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
4975
Andrzej Kurek25f27152022-08-17 16:09:31 -04004976#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 +08004977 defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
4978 MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04004979#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yued5e9f42022-01-26 11:21:34 +08004980 MBEDTLS_ECP_DP_SECP521R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08004981
Gilles Peskine449bd832023-01-11 14:50:10 +01004982#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && \
4983 defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu9bb3ee42022-06-23 10:16:33 +08004984 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
Gilles Peskine449bd832023-01-11 14:50:10 +01004985#endif \
4986 /* 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 +08004987
Gilles Peskine449bd832023-01-11 14:50:10 +01004988#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && \
4989 defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu9bb3ee42022-06-23 10:16:33 +08004990 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
Gilles Peskine449bd832023-01-11 14:50:10 +01004991#endif \
4992 /* 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 +08004993
Gilles Peskine449bd832023-01-11 14:50:10 +01004994#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && \
4995 defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu9bb3ee42022-06-23 10:16:33 +08004996 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Gilles Peskine449bd832023-01-11 14:50:10 +01004997#endif \
4998 /* 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 +08004999
Andrzej Kurek25f27152022-08-17 16:09:31 -04005000#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 +08005001 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512,
5002#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA512_C */
5003
Andrzej Kurek25f27152022-08-17 16:09:31 -04005004#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 +08005005 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384,
5006#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA384_C */
5007
Andrzej Kurek25f27152022-08-17 16:09:31 -04005008#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 +08005009 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
5010#endif /* MBEDTLS_RSA_C && MBEDTLS_SHA256_C */
5011
Gabor Mezei15b95a62022-05-09 16:37:58 +02005012 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08005013};
5014
5015/* NOTICE: see above */
5016#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5017static uint16_t ssl_tls12_preset_default_sig_algs[] = {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005018#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02005019#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005020 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA512),
Jerry Yu713013f2022-01-17 18:16:35 +08005021#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08005022#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
5023 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
5024#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02005025#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005026 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA512),
Gabor Mezeic1051b62022-05-10 13:13:58 +02005027#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005028#endif /* MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04005029#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02005030#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005031 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08005032#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08005033#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
5034 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
5035#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02005036#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005037 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384),
Gabor Mezeic1051b62022-05-10 13:13:58 +02005038#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005039#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04005040#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02005041#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005042 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08005043#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08005044#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
5045 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
5046#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02005047#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005048 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256),
Gabor Mezeic1051b62022-05-10 13:13:58 +02005049#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005050#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Gabor Mezei15b95a62022-05-09 16:37:58 +02005051 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08005052};
5053#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5054/* NOTICE: see above */
5055static uint16_t ssl_preset_suiteb_sig_algs[] = {
5056
Andrzej Kurek25f27152022-08-17 16:09:31 -04005057#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 +08005058 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
5059 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005060#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu909df7b2022-01-22 11:56:27 +08005061 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
5062
Andrzej Kurek25f27152022-08-17 16:09:31 -04005063#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 +08005064 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
5065 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005066#endif /* MBEDTLS_ECDSA_C && MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA&&
Jerry Yu53037892022-01-25 11:02:06 +08005067 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08005068
Gilles Peskine449bd832023-01-11 14:50:10 +01005069#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && \
5070 defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yu909df7b2022-01-22 11:56:27 +08005071 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Gilles Peskine449bd832023-01-11 14:50:10 +01005072#endif \
5073 /* 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 +08005074
Andrzej Kurek25f27152022-08-17 16:09:31 -04005075#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 +08005076 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005077#endif /* MBEDTLS_RSA_C && MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yu53037892022-01-25 11:02:06 +08005078
Gabor Mezei15b95a62022-05-09 16:37:58 +02005079 MBEDTLS_TLS_SIG_NONE
Jerry Yu713013f2022-01-17 18:16:35 +08005080};
Jerry Yu6106fdc2022-01-12 16:36:14 +08005081
Jerry Yu909df7b2022-01-22 11:56:27 +08005082/* NOTICE: see above */
5083#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5084static uint16_t ssl_tls12_preset_suiteb_sig_algs[] = {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005085#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02005086#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005087 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256),
Jerry Yu713013f2022-01-17 18:16:35 +08005088#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02005089#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005090 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256),
Gabor Mezeic1051b62022-05-10 13:13:58 +02005091#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005092#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Andrzej Kurek25f27152022-08-17 16:09:31 -04005093#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gabor Mezeic1051b62022-05-10 13:13:58 +02005094#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005095 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384),
Jerry Yu18c833e2022-01-25 10:55:47 +08005096#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02005097#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005098 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384),
Gabor Mezeic1051b62022-05-10 13:13:58 +02005099#endif
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005100#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Gabor Mezei15b95a62022-05-09 16:37:58 +02005101 MBEDTLS_TLS_SIG_NONE
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01005102};
Jerry Yu909df7b2022-01-22 11:56:27 +08005103#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5104
Ronald Crone68ab4f2022-10-05 12:46:29 +02005105#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005106
Brett Warrene0edc842021-08-17 09:53:13 +01005107static uint16_t ssl_preset_suiteb_groups[] = {
Jaeden Amerod4311042019-06-03 08:27:16 +01005108#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01005109 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01005110#endif
5111#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01005112 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01005113#endif
Brett Warrene0edc842021-08-17 09:53:13 +01005114 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005115};
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005116
Ronald Crone68ab4f2022-10-05 12:46:29 +02005117#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08005118/* Function for checking `ssl_preset_*_sig_algs` and `ssl_tls12_preset_*_sig_algs`
Jerry Yu370e1462022-01-25 10:36:53 +08005119 * to make sure there are no duplicated signature algorithm entries. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005120MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01005121static int ssl_check_no_sig_alg_duplication(uint16_t *sig_algs)
Jerry Yu1a8b4812022-01-20 17:56:50 +08005122{
5123 size_t i, j;
5124 int ret = 0;
Jerry Yu909df7b2022-01-22 11:56:27 +08005125
Gilles Peskine449bd832023-01-11 14:50:10 +01005126 for (i = 0; sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++) {
5127 for (j = 0; j < i; j++) {
5128 if (sig_algs[i] != sig_algs[j]) {
Jerry Yuf377d642022-01-25 10:43:59 +08005129 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01005130 }
5131 mbedtls_printf(" entry(%04x,%" MBEDTLS_PRINTF_SIZET
5132 ") is duplicated at %" MBEDTLS_PRINTF_SIZET "\n",
5133 sig_algs[i], j, i);
Jerry Yuf377d642022-01-25 10:43:59 +08005134 ret = -1;
Jerry Yu1a8b4812022-01-20 17:56:50 +08005135 }
5136 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005137 return ret;
Jerry Yu1a8b4812022-01-20 17:56:50 +08005138}
5139
Ronald Crone68ab4f2022-10-05 12:46:29 +02005140#endif /* MBEDTLS_DEBUG_C && MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yu1a8b4812022-01-20 17:56:50 +08005141
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005142/*
Tillmann Karras588ad502015-09-25 04:27:22 +02005143 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005144 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005145int mbedtls_ssl_config_defaults(mbedtls_ssl_config *conf,
5146 int endpoint, int transport, int preset)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005147{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005148#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Janos Follath865b3eb2019-12-16 11:46:15 +00005149 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005150#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005151
Ronald Crone68ab4f2022-10-05 12:46:29 +02005152#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01005153 if (ssl_check_no_sig_alg_duplication(ssl_preset_suiteb_sig_algs)) {
5154 mbedtls_printf("ssl_preset_suiteb_sig_algs has duplicated entries\n");
5155 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu1a8b4812022-01-20 17:56:50 +08005156 }
5157
Gilles Peskine449bd832023-01-11 14:50:10 +01005158 if (ssl_check_no_sig_alg_duplication(ssl_preset_default_sig_algs)) {
5159 mbedtls_printf("ssl_preset_default_sig_algs has duplicated entries\n");
5160 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu1a8b4812022-01-20 17:56:50 +08005161 }
Jerry Yu909df7b2022-01-22 11:56:27 +08005162
5163#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01005164 if (ssl_check_no_sig_alg_duplication(ssl_tls12_preset_suiteb_sig_algs)) {
5165 mbedtls_printf("ssl_tls12_preset_suiteb_sig_algs has duplicated entries\n");
5166 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu909df7b2022-01-22 11:56:27 +08005167 }
5168
Gilles Peskine449bd832023-01-11 14:50:10 +01005169 if (ssl_check_no_sig_alg_duplication(ssl_tls12_preset_default_sig_algs)) {
5170 mbedtls_printf("ssl_tls12_preset_default_sig_algs has duplicated entries\n");
5171 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu909df7b2022-01-22 11:56:27 +08005172 }
5173#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Ronald Crone68ab4f2022-10-05 12:46:29 +02005174#endif /* MBEDTLS_DEBUG_C && MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yu1a8b4812022-01-20 17:56:50 +08005175
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02005176 /* Use the functions here so that they are covered in tests,
5177 * but otherwise access member directly for efficiency */
Gilles Peskine449bd832023-01-11 14:50:10 +01005178 mbedtls_ssl_conf_endpoint(conf, endpoint);
5179 mbedtls_ssl_conf_transport(conf, transport);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005180
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005181 /*
5182 * Things that are common to all presets
5183 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02005184#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005185 if (endpoint == MBEDTLS_SSL_IS_CLIENT) {
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02005186 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
5187#if defined(MBEDTLS_SSL_SESSION_TICKETS)
5188 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
5189#endif
5190 }
5191#endif
5192
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005193#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
5194 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
5195#endif
5196
5197#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
5198 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
5199#endif
5200
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02005201#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005202 conf->f_cookie_write = ssl_cookie_write_dummy;
5203 conf->f_cookie_check = ssl_cookie_check_dummy;
5204#endif
5205
5206#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
5207 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
5208#endif
5209
Janos Follath088ce432017-04-10 12:42:31 +01005210#if defined(MBEDTLS_SSL_SRV_C)
5211 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
TRodziewicz3946f792021-06-14 12:11:18 +02005212 conf->respect_cli_pref = MBEDTLS_SSL_SRV_CIPHERSUITE_ORDER_SERVER;
Janos Follath088ce432017-04-10 12:42:31 +01005213#endif
5214
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005215#if defined(MBEDTLS_SSL_PROTO_DTLS)
5216 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
5217 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
5218#endif
5219
5220#if defined(MBEDTLS_SSL_RENEGOTIATION)
5221 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005222 memset(conf->renego_period, 0x00, 2);
5223 memset(conf->renego_period + 2, 0xFF, 6);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005224#endif
5225
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005226#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005227 if (endpoint == MBEDTLS_SSL_IS_SERVER) {
Hanno Beckere2defad2021-07-24 05:59:17 +01005228 const unsigned char dhm_p[] =
5229 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
5230 const unsigned char dhm_g[] =
5231 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
Hanno Becker00d0a682017-10-04 13:14:29 +01005232
Gilles Peskine449bd832023-01-11 14:50:10 +01005233 if ((ret = mbedtls_ssl_conf_dh_param_bin(conf,
5234 dhm_p, sizeof(dhm_p),
5235 dhm_g, sizeof(dhm_g))) != 0) {
5236 return ret;
Hanno Beckere2defad2021-07-24 05:59:17 +01005237 }
5238 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02005239#endif
5240
Ronald Cron6f135e12021-12-08 16:57:54 +01005241#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu6ee56aa2022-12-06 17:47:22 +08005242
5243#if defined(MBEDTLS_SSL_EARLY_DATA)
Gilles Peskine449bd832023-01-11 14:50:10 +01005244 mbedtls_ssl_tls13_conf_early_data(conf, MBEDTLS_SSL_EARLY_DATA_DISABLED);
Jerry Yu6ee56aa2022-12-06 17:47:22 +08005245#if defined(MBEDTLS_SSL_SRV_C)
5246 mbedtls_ssl_tls13_conf_max_early_data_size(
Gilles Peskine449bd832023-01-11 14:50:10 +01005247 conf, MBEDTLS_SSL_MAX_EARLY_DATA_SIZE);
Jerry Yu6ee56aa2022-12-06 17:47:22 +08005248#endif
5249#endif /* MBEDTLS_SSL_EARLY_DATA */
5250
Jerry Yud0766ec2022-09-22 10:46:57 +08005251#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08005252 mbedtls_ssl_conf_new_session_tickets(
Gilles Peskine449bd832023-01-11 14:50:10 +01005253 conf, MBEDTLS_SSL_TLS1_3_DEFAULT_NEW_SESSION_TICKETS);
Jerry Yu1ad7ace2022-08-09 13:28:39 +08005254#endif
Hanno Becker71f1ed62021-07-24 06:01:47 +01005255 /*
5256 * Allow all TLS 1.3 key exchange modes by default.
5257 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00005258 conf->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Ronald Cron6f135e12021-12-08 16:57:54 +01005259#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01005260
Gilles Peskine449bd832023-01-11 14:50:10 +01005261 if (transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Glenn Strauss2dfcea22022-03-14 17:26:42 -04005262#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
XiaokangQian4d3a6042022-04-21 13:46:17 +00005263 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian060d8672022-04-21 09:24:56 +00005264 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian4d3a6042022-04-21 13:46:17 +00005265#else
Gilles Peskine449bd832023-01-11 14:50:10 +01005266 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQian060d8672022-04-21 09:24:56 +00005267#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005268 } else {
XiaokangQian4d3a6042022-04-21 13:46:17 +00005269#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01005270 if (endpoint == MBEDTLS_SSL_IS_CLIENT) {
XiaokangQian4d3a6042022-04-21 13:46:17 +00005271 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5272 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
Gilles Peskine449bd832023-01-11 14:50:10 +01005273 } else {
5274 /* Hybrid TLS 1.2 / 1.3 is not supported on server side yet */
XiaokangQian4d3a6042022-04-21 13:46:17 +00005275 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5276 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5277 }
5278#elif defined(MBEDTLS_SSL_PROTO_TLS1_3)
5279 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
5280 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
5281#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
5282 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5283 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5284#else
Gilles Peskine449bd832023-01-11 14:50:10 +01005285 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQian4d3a6042022-04-21 13:46:17 +00005286#endif
5287 }
Glenn Strauss2dfcea22022-03-14 17:26:42 -04005288
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005289 /*
5290 * Preset-specific defaults
5291 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005292 switch (preset) {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005293 /*
5294 * NSA Suite B
5295 */
5296 case MBEDTLS_SSL_PRESET_SUITEB:
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005297
Hanno Beckerd60b6c62021-04-29 12:04:11 +01005298 conf->ciphersuite_list = ssl_preset_suiteb_ciphersuites;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005299
5300#if defined(MBEDTLS_X509_CRT_PARSE_C)
5301 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005302#endif
5303
Ronald Crone68ab4f2022-10-05 12:46:29 +02005304#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08005305#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01005306 if (mbedtls_ssl_conf_is_tls12_only(conf)) {
Jerry Yu909df7b2022-01-22 11:56:27 +08005307 conf->sig_algs = ssl_tls12_preset_suiteb_sig_algs;
Gilles Peskine449bd832023-01-11 14:50:10 +01005308 } else
Jerry Yu909df7b2022-01-22 11:56:27 +08005309#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005310 conf->sig_algs = ssl_preset_suiteb_sig_algs;
Ronald Crone68ab4f2022-10-05 12:46:29 +02005311#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005312
Brett Warrene0edc842021-08-17 09:53:13 +01005313#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
5314 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005315#endif
Brett Warrene0edc842021-08-17 09:53:13 +01005316 conf->group_list = ssl_preset_suiteb_groups;
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02005317 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005318
5319 /*
5320 * Default
5321 */
5322 default:
Ronald Cronf6606552022-03-15 11:23:25 +01005323
Hanno Beckerd60b6c62021-04-29 12:04:11 +01005324 conf->ciphersuite_list = mbedtls_ssl_list_ciphersuites();
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005325
5326#if defined(MBEDTLS_X509_CRT_PARSE_C)
5327 conf->cert_profile = &mbedtls_x509_crt_profile_default;
5328#endif
5329
Ronald Crone68ab4f2022-10-05 12:46:29 +02005330#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08005331#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01005332 if (mbedtls_ssl_conf_is_tls12_only(conf)) {
Jerry Yu909df7b2022-01-22 11:56:27 +08005333 conf->sig_algs = ssl_tls12_preset_default_sig_algs;
Gilles Peskine449bd832023-01-11 14:50:10 +01005334 } else
Jerry Yu909df7b2022-01-22 11:56:27 +08005335#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005336 conf->sig_algs = ssl_preset_default_sig_algs;
Ronald Crone68ab4f2022-10-05 12:46:29 +02005337#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005338
Brett Warrene0edc842021-08-17 09:53:13 +01005339#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
5340 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005341#endif
Brett Warrene0edc842021-08-17 09:53:13 +01005342 conf->group_list = ssl_preset_default_groups;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005343
5344#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
5345 conf->dhm_min_bitlen = 1024;
5346#endif
5347 }
5348
Gilles Peskine449bd832023-01-11 14:50:10 +01005349 return 0;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005350}
5351
5352/*
5353 * Free mbedtls_ssl_config
5354 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005355void mbedtls_ssl_config_free(mbedtls_ssl_config *conf)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005356{
5357#if defined(MBEDTLS_DHM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005358 mbedtls_mpi_free(&conf->dhm_P);
5359 mbedtls_mpi_free(&conf->dhm_G);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005360#endif
5361
Ronald Cron73fe8df2022-10-05 14:31:43 +02005362#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02005363#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01005364 if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) {
Neil Armstrong501c9322022-05-03 09:35:09 +02005365 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
5366 }
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005367#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01005368 if (conf->psk != NULL) {
5369 mbedtls_platform_zeroize(conf->psk, conf->psk_len);
5370 mbedtls_free(conf->psk);
Azim Khan27e8a122018-03-21 14:24:11 +00005371 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005372 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09005373 }
5374
Gilles Peskine449bd832023-01-11 14:50:10 +01005375 if (conf->psk_identity != NULL) {
5376 mbedtls_platform_zeroize(conf->psk_identity, conf->psk_identity_len);
5377 mbedtls_free(conf->psk_identity);
Azim Khan27e8a122018-03-21 14:24:11 +00005378 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005379 conf->psk_identity_len = 0;
5380 }
Ronald Cron73fe8df2022-10-05 14:31:43 +02005381#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005382
5383#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005384 ssl_key_cert_free(conf->key_cert);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005385#endif
5386
Gilles Peskine449bd832023-01-11 14:50:10 +01005387 mbedtls_platform_zeroize(conf, sizeof(mbedtls_ssl_config));
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005388}
5389
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02005390#if defined(MBEDTLS_PK_C) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01005391 (defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_C))
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005392/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005393 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005394 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005395unsigned char mbedtls_ssl_sig_from_pk(mbedtls_pk_context *pk)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005396{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005397#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005398 if (mbedtls_pk_can_do(pk, MBEDTLS_PK_RSA)) {
5399 return MBEDTLS_SSL_SIG_RSA;
5400 }
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005401#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005402#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005403 if (mbedtls_pk_can_do(pk, MBEDTLS_PK_ECDSA)) {
5404 return MBEDTLS_SSL_SIG_ECDSA;
5405 }
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005406#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005407 return MBEDTLS_SSL_SIG_ANON;
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005408}
5409
Gilles Peskine449bd832023-01-11 14:50:10 +01005410unsigned char mbedtls_ssl_sig_from_pk_alg(mbedtls_pk_type_t type)
Hanno Becker7e5437a2017-04-28 17:15:26 +01005411{
Gilles Peskine449bd832023-01-11 14:50:10 +01005412 switch (type) {
Hanno Becker7e5437a2017-04-28 17:15:26 +01005413 case MBEDTLS_PK_RSA:
Gilles Peskine449bd832023-01-11 14:50:10 +01005414 return MBEDTLS_SSL_SIG_RSA;
Hanno Becker7e5437a2017-04-28 17:15:26 +01005415 case MBEDTLS_PK_ECDSA:
5416 case MBEDTLS_PK_ECKEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01005417 return MBEDTLS_SSL_SIG_ECDSA;
Hanno Becker7e5437a2017-04-28 17:15:26 +01005418 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005419 return MBEDTLS_SSL_SIG_ANON;
Hanno Becker7e5437a2017-04-28 17:15:26 +01005420 }
5421}
5422
Gilles Peskine449bd832023-01-11 14:50:10 +01005423mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig(unsigned char sig)
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005424{
Gilles Peskine449bd832023-01-11 14:50:10 +01005425 switch (sig) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005426#if defined(MBEDTLS_RSA_C)
5427 case MBEDTLS_SSL_SIG_RSA:
Gilles Peskine449bd832023-01-11 14:50:10 +01005428 return MBEDTLS_PK_RSA;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005429#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005430#if defined(MBEDTLS_ECDSA_C)
5431 case MBEDTLS_SSL_SIG_ECDSA:
Gilles Peskine449bd832023-01-11 14:50:10 +01005432 return MBEDTLS_PK_ECDSA;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005433#endif
5434 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005435 return MBEDTLS_PK_NONE;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005436 }
5437}
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02005438#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_ECDSA_C ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005439
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02005440/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005441 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02005442 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005443mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash(unsigned char hash)
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005444{
Gilles Peskine449bd832023-01-11 14:50:10 +01005445 switch (hash) {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005446#if defined(MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005447 case MBEDTLS_SSL_HASH_MD5:
Gilles Peskine449bd832023-01-11 14:50:10 +01005448 return MBEDTLS_MD_MD5;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005449#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005450#if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005451 case MBEDTLS_SSL_HASH_SHA1:
Gilles Peskine449bd832023-01-11 14:50:10 +01005452 return MBEDTLS_MD_SHA1;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005453#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005454#if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005455 case MBEDTLS_SSL_HASH_SHA224:
Gilles Peskine449bd832023-01-11 14:50:10 +01005456 return MBEDTLS_MD_SHA224;
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02005457#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005458#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005459 case MBEDTLS_SSL_HASH_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01005460 return MBEDTLS_MD_SHA256;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005461#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005462#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005463 case MBEDTLS_SSL_HASH_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01005464 return MBEDTLS_MD_SHA384;
Mateusz Starzyk3352a532021-04-06 14:28:22 +02005465#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005466#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005467 case MBEDTLS_SSL_HASH_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01005468 return MBEDTLS_MD_SHA512;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005469#endif
5470 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005471 return MBEDTLS_MD_NONE;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005472 }
5473}
5474
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005475/*
5476 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
5477 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005478unsigned char mbedtls_ssl_hash_from_md_alg(int md)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005479{
Gilles Peskine449bd832023-01-11 14:50:10 +01005480 switch (md) {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005481#if defined(MBEDTLS_HAS_ALG_MD5_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005482 case MBEDTLS_MD_MD5:
Gilles Peskine449bd832023-01-11 14:50:10 +01005483 return MBEDTLS_SSL_HASH_MD5;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005484#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005485#if defined(MBEDTLS_HAS_ALG_SHA_1_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005486 case MBEDTLS_MD_SHA1:
Gilles Peskine449bd832023-01-11 14:50:10 +01005487 return MBEDTLS_SSL_HASH_SHA1;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005488#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005489#if defined(MBEDTLS_HAS_ALG_SHA_224_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005490 case MBEDTLS_MD_SHA224:
Gilles Peskine449bd832023-01-11 14:50:10 +01005491 return MBEDTLS_SSL_HASH_SHA224;
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02005492#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005493#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005494 case MBEDTLS_MD_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01005495 return MBEDTLS_SSL_HASH_SHA256;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005496#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005497#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005498 case MBEDTLS_MD_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01005499 return MBEDTLS_SSL_HASH_SHA384;
Mateusz Starzyk3352a532021-04-06 14:28:22 +02005500#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04005501#if defined(MBEDTLS_HAS_ALG_SHA_512_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005502 case MBEDTLS_MD_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01005503 return MBEDTLS_SSL_HASH_SHA512;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005504#endif
5505 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005506 return MBEDTLS_SSL_HASH_NONE;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005507 }
5508}
5509
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005510/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005511 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005512 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005513 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005514int mbedtls_ssl_check_curve_tls_id(const mbedtls_ssl_context *ssl, uint16_t tls_id)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005515{
Gilles Peskine449bd832023-01-11 14:50:10 +01005516 const uint16_t *group_list = mbedtls_ssl_get_groups(ssl);
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005517
Gilles Peskine449bd832023-01-11 14:50:10 +01005518 if (group_list == NULL) {
5519 return -1;
Brett Warrene0edc842021-08-17 09:53:13 +01005520 }
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005521
Gilles Peskine449bd832023-01-11 14:50:10 +01005522 for (; *group_list != 0; group_list++) {
5523 if (*group_list == tls_id) {
5524 return 0;
5525 }
5526 }
5527
5528 return -1;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005529}
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005530
5531#if defined(MBEDTLS_ECP_C)
5532/*
5533 * Same as mbedtls_ssl_check_curve_tls_id() but with a mbedtls_ecp_group_id.
5534 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005535int mbedtls_ssl_check_curve(const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id)
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005536{
Gilles Peskine449bd832023-01-11 14:50:10 +01005537 uint16_t tls_id = mbedtls_ssl_get_tls_id_from_ecp_group_id(grp_id);
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005538
Gilles Peskine449bd832023-01-11 14:50:10 +01005539 if (tls_id == 0) {
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005540 return -1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005541 }
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005542
Gilles Peskine449bd832023-01-11 14:50:10 +01005543 return mbedtls_ssl_check_curve_tls_id(ssl, tls_id);
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005544}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02005545#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005546
Gilles Peskine449bd832023-01-11 14:50:10 +01005547#if defined(MBEDTLS_DEBUG_C)
Valerio Setti67419f02023-01-04 16:12:42 +01005548#define EC_NAME(_name_) _name_
5549#else
5550#define EC_NAME(_name_) NULL
5551#endif
5552
Valerio Setti18c9fed2022-12-30 17:44:24 +01005553static const struct {
5554 uint16_t tls_id;
5555 mbedtls_ecp_group_id ecp_group_id;
5556 psa_ecc_family_t psa_family;
5557 uint16_t bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01005558 const char *name;
Valerio Setti18c9fed2022-12-30 17:44:24 +01005559} tls_id_match_table[] =
5560{
5561#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_521)
Gilles Peskine449bd832023-01-11 14:50:10 +01005562 { 25, MBEDTLS_ECP_DP_SECP521R1, PSA_ECC_FAMILY_SECP_R1, 521, EC_NAME("secp521r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005563#endif
5564#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED) || defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512)
Gilles Peskine449bd832023-01-11 14:50:10 +01005565 { 28, MBEDTLS_ECP_DP_BP512R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 512, EC_NAME("brainpoolP512r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005566#endif
Valerio Setti67419f02023-01-04 16:12:42 +01005567#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_384)
Gilles Peskine449bd832023-01-11 14:50:10 +01005568 { 24, MBEDTLS_ECP_DP_SECP384R1, PSA_ECC_FAMILY_SECP_R1, 384, EC_NAME("secp384r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005569#endif
Valerio Setti67419f02023-01-04 16:12:42 +01005570#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED) || defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384)
Gilles Peskine449bd832023-01-11 14:50:10 +01005571 { 27, MBEDTLS_ECP_DP_BP384R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 384, EC_NAME("brainpoolP384r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005572#endif
Valerio Setti67419f02023-01-04 16:12:42 +01005573#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_256)
Gilles Peskine449bd832023-01-11 14:50:10 +01005574 { 23, MBEDTLS_ECP_DP_SECP256R1, PSA_ECC_FAMILY_SECP_R1, 256, EC_NAME("secp256r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005575#endif
5576#if defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED) || defined(PSA_WANT_ECC_SECP_K1_256)
Gilles Peskine449bd832023-01-11 14:50:10 +01005577 { 22, MBEDTLS_ECP_DP_SECP256K1, PSA_ECC_FAMILY_SECP_K1, 256, EC_NAME("secp256k1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005578#endif
Valerio Setti67419f02023-01-04 16:12:42 +01005579#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED) || defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256)
Gilles Peskine449bd832023-01-11 14:50:10 +01005580 { 26, MBEDTLS_ECP_DP_BP256R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 256, EC_NAME("brainpoolP256r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005581#endif
5582#if defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_224)
Gilles Peskine449bd832023-01-11 14:50:10 +01005583 { 21, MBEDTLS_ECP_DP_SECP224R1, PSA_ECC_FAMILY_SECP_R1, 224, EC_NAME("secp224r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005584#endif
5585#if defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED) || defined(PSA_WANT_ECC_SECP_K1_224)
Gilles Peskine449bd832023-01-11 14:50:10 +01005586 { 20, MBEDTLS_ECP_DP_SECP224K1, PSA_ECC_FAMILY_SECP_K1, 224, EC_NAME("secp224k1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005587#endif
5588#if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_192)
Gilles Peskine449bd832023-01-11 14:50:10 +01005589 { 19, MBEDTLS_ECP_DP_SECP192R1, PSA_ECC_FAMILY_SECP_R1, 192, EC_NAME("secp192r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005590#endif
5591#if defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED) || defined(PSA_WANT_ECC_SECP_K1_192)
Gilles Peskine449bd832023-01-11 14:50:10 +01005592 { 18, MBEDTLS_ECP_DP_SECP192K1, PSA_ECC_FAMILY_SECP_K1, 192, EC_NAME("secp192k1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005593#endif
5594#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED) || defined(PSA_WANT_ECC_MONTGOMERY_255)
Gilles Peskine449bd832023-01-11 14:50:10 +01005595 { 29, MBEDTLS_ECP_DP_CURVE25519, PSA_ECC_FAMILY_MONTGOMERY, 255, EC_NAME("x25519") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005596#endif
5597#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED) || defined(PSA_WANT_ECC_MONTGOMERY_448)
Gilles Peskine449bd832023-01-11 14:50:10 +01005598 { 30, MBEDTLS_ECP_DP_CURVE448, PSA_ECC_FAMILY_MONTGOMERY, 448, EC_NAME("x448") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005599#endif
5600 { 0, MBEDTLS_ECP_DP_NONE, 0, 0, NULL },
5601};
5602
Gilles Peskine449bd832023-01-11 14:50:10 +01005603int mbedtls_ssl_get_psa_curve_info_from_tls_id(uint16_t tls_id,
5604 psa_ecc_family_t *family,
5605 size_t *bits)
Valerio Setti18c9fed2022-12-30 17:44:24 +01005606{
Gilles Peskine449bd832023-01-11 14:50:10 +01005607 for (int i = 0; tls_id_match_table[i].tls_id != 0; i++) {
5608 if (tls_id_match_table[i].tls_id == tls_id) {
5609 if (family != NULL) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01005610 *family = tls_id_match_table[i].psa_family;
Gilles Peskine449bd832023-01-11 14:50:10 +01005611 }
5612 if (bits != NULL) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01005613 *bits = tls_id_match_table[i].bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01005614 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01005615 return PSA_SUCCESS;
5616 }
5617 }
5618
5619 return PSA_ERROR_NOT_SUPPORTED;
5620}
5621
Gilles Peskine449bd832023-01-11 14:50:10 +01005622mbedtls_ecp_group_id mbedtls_ssl_get_ecp_group_id_from_tls_id(uint16_t tls_id)
Valerio Setti18c9fed2022-12-30 17:44:24 +01005623{
Gilles Peskine449bd832023-01-11 14:50:10 +01005624 for (int i = 0; tls_id_match_table[i].tls_id != 0; i++) {
5625 if (tls_id_match_table[i].tls_id == tls_id) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01005626 return tls_id_match_table[i].ecp_group_id;
Gilles Peskine449bd832023-01-11 14:50:10 +01005627 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01005628 }
5629
5630 return MBEDTLS_ECP_DP_NONE;
5631}
5632
Gilles Peskine449bd832023-01-11 14:50:10 +01005633uint16_t mbedtls_ssl_get_tls_id_from_ecp_group_id(mbedtls_ecp_group_id grp_id)
Valerio Setti18c9fed2022-12-30 17:44:24 +01005634{
Gilles Peskine449bd832023-01-11 14:50:10 +01005635 for (int i = 0; tls_id_match_table[i].ecp_group_id != MBEDTLS_ECP_DP_NONE;
5636 i++) {
5637 if (tls_id_match_table[i].ecp_group_id == grp_id) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01005638 return tls_id_match_table[i].tls_id;
Gilles Peskine449bd832023-01-11 14:50:10 +01005639 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01005640 }
5641
5642 return 0;
5643}
5644
Valerio Setti67419f02023-01-04 16:12:42 +01005645#if defined(MBEDTLS_DEBUG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005646const char *mbedtls_ssl_get_curve_name_from_tls_id(uint16_t tls_id)
Valerio Setti18c9fed2022-12-30 17:44:24 +01005647{
Gilles Peskine449bd832023-01-11 14:50:10 +01005648 for (int i = 0; tls_id_match_table[i].tls_id != 0; i++) {
5649 if (tls_id_match_table[i].tls_id == tls_id) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01005650 return tls_id_match_table[i].name;
Gilles Peskine449bd832023-01-11 14:50:10 +01005651 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01005652 }
5653
5654 return NULL;
5655}
Valerio Setti67419f02023-01-04 16:12:42 +01005656#endif
Valerio Setti18c9fed2022-12-30 17:44:24 +01005657
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005658#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005659int mbedtls_ssl_check_cert_usage(const mbedtls_x509_crt *cert,
5660 const mbedtls_ssl_ciphersuite_t *ciphersuite,
5661 int cert_endpoint,
5662 uint32_t *flags)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005663{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005664 int ret = 0;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005665 int usage = 0;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005666 const char *ext_oid;
5667 size_t ext_len;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005668
Gilles Peskine449bd832023-01-11 14:50:10 +01005669 if (cert_endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005670 /* Server part of the key exchange */
Gilles Peskine449bd832023-01-11 14:50:10 +01005671 switch (ciphersuite->key_exchange) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005672 case MBEDTLS_KEY_EXCHANGE_RSA:
5673 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005674 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005675 break;
5676
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005677 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
5678 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
5679 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
5680 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005681 break;
5682
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005683 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
5684 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005685 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005686 break;
5687
5688 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005689 case MBEDTLS_KEY_EXCHANGE_NONE:
5690 case MBEDTLS_KEY_EXCHANGE_PSK:
5691 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
5692 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02005693 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005694 usage = 0;
5695 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005696 } else {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005697 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
5698 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005699 }
5700
Gilles Peskine449bd832023-01-11 14:50:10 +01005701 if (mbedtls_x509_crt_check_key_usage(cert, usage) != 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005702 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005703 ret = -1;
5704 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005705
Gilles Peskine449bd832023-01-11 14:50:10 +01005706 if (cert_endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005707 ext_oid = MBEDTLS_OID_SERVER_AUTH;
Gilles Peskine449bd832023-01-11 14:50:10 +01005708 ext_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_SERVER_AUTH);
5709 } else {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005710 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
Gilles Peskine449bd832023-01-11 14:50:10 +01005711 ext_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_CLIENT_AUTH);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005712 }
5713
Gilles Peskine449bd832023-01-11 14:50:10 +01005714 if (mbedtls_x509_crt_check_extended_key_usage(cert, ext_oid, ext_len) != 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005715 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005716 ret = -1;
5717 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005718
Gilles Peskine449bd832023-01-11 14:50:10 +01005719 return ret;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005720}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005721#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02005722
Jerry Yu148165c2021-09-24 23:20:59 +08005723#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01005724int mbedtls_ssl_get_handshake_transcript(mbedtls_ssl_context *ssl,
5725 const mbedtls_md_type_t md,
5726 unsigned char *dst,
5727 size_t dst_len,
5728 size_t *olen)
Jerry Yu148165c2021-09-24 23:20:59 +08005729{
Ronald Cronf6893e12022-01-07 22:09:01 +01005730 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5731 psa_hash_operation_t *hash_operation_to_clone;
5732 psa_hash_operation_t hash_operation = psa_hash_operation_init();
5733
Jerry Yu148165c2021-09-24 23:20:59 +08005734 *olen = 0;
Ronald Cronf6893e12022-01-07 22:09:01 +01005735
Gilles Peskine449bd832023-01-11 14:50:10 +01005736 switch (md) {
Andrzej Kurek25f27152022-08-17 16:09:31 -04005737#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01005738 case MBEDTLS_MD_SHA384:
5739 hash_operation_to_clone = &ssl->handshake->fin_sha384_psa;
5740 break;
Ronald Cronf6893e12022-01-07 22:09:01 +01005741#endif
5742
Andrzej Kurek25f27152022-08-17 16:09:31 -04005743#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01005744 case MBEDTLS_MD_SHA256:
5745 hash_operation_to_clone = &ssl->handshake->fin_sha256_psa;
5746 break;
Ronald Cronf6893e12022-01-07 22:09:01 +01005747#endif
5748
Gilles Peskine449bd832023-01-11 14:50:10 +01005749 default:
5750 goto exit;
5751 }
5752
5753 status = psa_hash_clone(hash_operation_to_clone, &hash_operation);
5754 if (status != PSA_SUCCESS) {
Ronald Cronf6893e12022-01-07 22:09:01 +01005755 goto exit;
5756 }
5757
Gilles Peskine449bd832023-01-11 14:50:10 +01005758 status = psa_hash_finish(&hash_operation, dst, dst_len, olen);
5759 if (status != PSA_SUCCESS) {
Ronald Cronf6893e12022-01-07 22:09:01 +01005760 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005761 }
Ronald Cronf6893e12022-01-07 22:09:01 +01005762
5763exit:
Andrzej Kurekeabeb302022-10-17 07:52:51 -04005764#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
5765 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
5766 (void) ssl;
5767#endif
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05005768 return PSA_TO_MBEDTLS_ERR(status);
Jerry Yu148165c2021-09-24 23:20:59 +08005769}
5770#else /* MBEDTLS_USE_PSA_CRYPTO */
5771
Andrzej Kurek25f27152022-08-17 16:09:31 -04005772#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005773MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01005774static int ssl_get_handshake_transcript_sha384(mbedtls_ssl_context *ssl,
5775 unsigned char *dst,
5776 size_t dst_len,
5777 size_t *olen)
Jerry Yu24c0ec32021-09-09 14:21:07 +08005778{
Jerry Yu24c0ec32021-09-09 14:21:07 +08005779 int ret;
5780 mbedtls_sha512_context sha512;
5781
Gilles Peskine449bd832023-01-11 14:50:10 +01005782 if (dst_len < 48) {
5783 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
5784 }
Jerry Yu24c0ec32021-09-09 14:21:07 +08005785
Gilles Peskine449bd832023-01-11 14:50:10 +01005786 mbedtls_sha512_init(&sha512);
5787 mbedtls_sha512_clone(&sha512, &ssl->handshake->fin_sha384);
Jerry Yu24c0ec32021-09-09 14:21:07 +08005788
Gilles Peskine449bd832023-01-11 14:50:10 +01005789 if ((ret = mbedtls_sha512_finish(&sha512, dst)) != 0) {
5790 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_sha512_finish", ret);
Jerry Yu24c0ec32021-09-09 14:21:07 +08005791 goto exit;
5792 }
5793
5794 *olen = 48;
5795
5796exit:
5797
Gilles Peskine449bd832023-01-11 14:50:10 +01005798 mbedtls_sha512_free(&sha512);
5799 return ret;
Jerry Yu24c0ec32021-09-09 14:21:07 +08005800}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005801#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005802
Andrzej Kurek25f27152022-08-17 16:09:31 -04005803#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005804MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01005805static int ssl_get_handshake_transcript_sha256(mbedtls_ssl_context *ssl,
5806 unsigned char *dst,
5807 size_t dst_len,
5808 size_t *olen)
Jerry Yu24c0ec32021-09-09 14:21:07 +08005809{
Jerry Yu24c0ec32021-09-09 14:21:07 +08005810 int ret;
5811 mbedtls_sha256_context sha256;
5812
Gilles Peskine449bd832023-01-11 14:50:10 +01005813 if (dst_len < 32) {
5814 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
5815 }
Jerry Yu24c0ec32021-09-09 14:21:07 +08005816
Gilles Peskine449bd832023-01-11 14:50:10 +01005817 mbedtls_sha256_init(&sha256);
5818 mbedtls_sha256_clone(&sha256, &ssl->handshake->fin_sha256);
Jerry Yuc5aef882021-12-23 20:15:02 +08005819
Gilles Peskine449bd832023-01-11 14:50:10 +01005820 if ((ret = mbedtls_sha256_finish(&sha256, dst)) != 0) {
5821 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_sha256_finish", ret);
Jerry Yu24c0ec32021-09-09 14:21:07 +08005822 goto exit;
5823 }
5824
5825 *olen = 32;
5826
5827exit:
5828
Gilles Peskine449bd832023-01-11 14:50:10 +01005829 mbedtls_sha256_free(&sha256);
5830 return ret;
Jerry Yu24c0ec32021-09-09 14:21:07 +08005831}
Andrzej Kurek25f27152022-08-17 16:09:31 -04005832#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005833
Gilles Peskine449bd832023-01-11 14:50:10 +01005834int mbedtls_ssl_get_handshake_transcript(mbedtls_ssl_context *ssl,
5835 const mbedtls_md_type_t md,
5836 unsigned char *dst,
5837 size_t dst_len,
5838 size_t *olen)
Jerry Yu24c0ec32021-09-09 14:21:07 +08005839{
Gilles Peskine449bd832023-01-11 14:50:10 +01005840 switch (md) {
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005841
Andrzej Kurek25f27152022-08-17 16:09:31 -04005842#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01005843 case MBEDTLS_MD_SHA384:
5844 return ssl_get_handshake_transcript_sha384(ssl, dst, dst_len, olen);
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005845#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005846
Andrzej Kurek25f27152022-08-17 16:09:31 -04005847#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01005848 case MBEDTLS_MD_SHA256:
5849 return ssl_get_handshake_transcript_sha256(ssl, dst, dst_len, olen);
Andrzej Kurekcccb0442022-08-19 03:42:11 -04005850#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005851
Gilles Peskine449bd832023-01-11 14:50:10 +01005852 default:
Andrzej Kurek409248a2022-10-24 10:33:21 -04005853#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01005854 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
5855 (void) ssl;
5856 (void) dst;
5857 (void) dst_len;
5858 (void) olen;
Andrzej Kurek409248a2022-10-24 10:33:21 -04005859#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005860 break;
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005861 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005862 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu24c0ec32021-09-09 14:21:07 +08005863}
XiaokangQian647719a2021-12-07 09:16:29 +00005864
Jerry Yu148165c2021-09-24 23:20:59 +08005865#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005866
Ronald Crone68ab4f2022-10-05 12:46:29 +02005867#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Gabor Mezei078e8032022-04-27 21:17:56 +02005868/* mbedtls_ssl_parse_sig_alg_ext()
5869 *
5870 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
5871 * value (TLS 1.3 RFC8446):
5872 * enum {
5873 * ....
5874 * ecdsa_secp256r1_sha256( 0x0403 ),
5875 * ecdsa_secp384r1_sha384( 0x0503 ),
5876 * ecdsa_secp521r1_sha512( 0x0603 ),
5877 * ....
5878 * } SignatureScheme;
5879 *
5880 * struct {
5881 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
5882 * } SignatureSchemeList;
5883 *
5884 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
5885 * value (TLS 1.2 RFC5246):
5886 * enum {
5887 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
5888 * sha512(6), (255)
5889 * } HashAlgorithm;
5890 *
5891 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
5892 * SignatureAlgorithm;
5893 *
5894 * struct {
5895 * HashAlgorithm hash;
5896 * SignatureAlgorithm signature;
5897 * } SignatureAndHashAlgorithm;
5898 *
5899 * SignatureAndHashAlgorithm
5900 * supported_signature_algorithms<2..2^16-2>;
5901 *
5902 * The TLS 1.3 signature algorithm extension was defined to be a compatible
5903 * generalization of the TLS 1.2 signature algorithm extension.
5904 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
5905 * `SignatureScheme` field of TLS 1.3
5906 *
5907 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005908int mbedtls_ssl_parse_sig_alg_ext(mbedtls_ssl_context *ssl,
5909 const unsigned char *buf,
5910 const unsigned char *end)
Gabor Mezei078e8032022-04-27 21:17:56 +02005911{
5912 const unsigned char *p = buf;
5913 size_t supported_sig_algs_len = 0;
5914 const unsigned char *supported_sig_algs_end;
5915 uint16_t sig_alg;
5916 uint32_t common_idx = 0;
5917
Gilles Peskine449bd832023-01-11 14:50:10 +01005918 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
5919 supported_sig_algs_len = MBEDTLS_GET_UINT16_BE(p, 0);
Gabor Mezei078e8032022-04-27 21:17:56 +02005920 p += 2;
5921
Gilles Peskine449bd832023-01-11 14:50:10 +01005922 memset(ssl->handshake->received_sig_algs, 0,
5923 sizeof(ssl->handshake->received_sig_algs));
Gabor Mezei078e8032022-04-27 21:17:56 +02005924
Gilles Peskine449bd832023-01-11 14:50:10 +01005925 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, supported_sig_algs_len);
Gabor Mezei078e8032022-04-27 21:17:56 +02005926 supported_sig_algs_end = p + supported_sig_algs_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01005927 while (p < supported_sig_algs_end) {
5928 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, supported_sig_algs_end, 2);
5929 sig_alg = MBEDTLS_GET_UINT16_BE(p, 0);
Gabor Mezei078e8032022-04-27 21:17:56 +02005930 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01005931 MBEDTLS_SSL_DEBUG_MSG(4, ("received signature algorithm: 0x%x %s",
5932 sig_alg,
5933 mbedtls_ssl_sig_alg_to_str(sig_alg)));
Jerry Yu2fe6c632022-06-29 10:02:38 +08005934#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01005935 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 &&
5936 (!(mbedtls_ssl_sig_alg_is_supported(ssl, sig_alg) &&
5937 mbedtls_ssl_sig_alg_is_offered(ssl, sig_alg)))) {
Gabor Mezei078e8032022-04-27 21:17:56 +02005938 continue;
Jerry Yu2fe6c632022-06-29 10:02:38 +08005939 }
5940#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gabor Mezei078e8032022-04-27 21:17:56 +02005941
Gilles Peskine449bd832023-01-11 14:50:10 +01005942 MBEDTLS_SSL_DEBUG_MSG(4, ("valid signature algorithm: %s",
5943 mbedtls_ssl_sig_alg_to_str(sig_alg)));
Gabor Mezei078e8032022-04-27 21:17:56 +02005944
Gilles Peskine449bd832023-01-11 14:50:10 +01005945 if (common_idx + 1 < MBEDTLS_RECEIVED_SIG_ALGS_SIZE) {
Gabor Mezei078e8032022-04-27 21:17:56 +02005946 ssl->handshake->received_sig_algs[common_idx] = sig_alg;
5947 common_idx += 1;
5948 }
5949 }
5950 /* Check that we consumed all the message. */
Gilles Peskine449bd832023-01-11 14:50:10 +01005951 if (p != end) {
5952 MBEDTLS_SSL_DEBUG_MSG(1,
5953 ("Signature algorithms extension length misaligned"));
5954 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
5955 MBEDTLS_ERR_SSL_DECODE_ERROR);
5956 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Gabor Mezei078e8032022-04-27 21:17:56 +02005957 }
5958
Gilles Peskine449bd832023-01-11 14:50:10 +01005959 if (common_idx == 0) {
5960 MBEDTLS_SSL_DEBUG_MSG(3, ("no signature algorithm in common"));
5961 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
5962 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
5963 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Gabor Mezei078e8032022-04-27 21:17:56 +02005964 }
5965
Gabor Mezei15b95a62022-05-09 16:37:58 +02005966 ssl->handshake->received_sig_algs[common_idx] = MBEDTLS_TLS_SIG_NONE;
Gilles Peskine449bd832023-01-11 14:50:10 +01005967 return 0;
Gabor Mezei078e8032022-04-27 21:17:56 +02005968}
5969
Ronald Crone68ab4f2022-10-05 12:46:29 +02005970#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Gabor Mezei078e8032022-04-27 21:17:56 +02005971
Jerry Yudc7bd172022-02-17 13:44:15 +08005972#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5973
5974#if defined(MBEDTLS_USE_PSA_CRYPTO)
5975
Gilles Peskine449bd832023-01-11 14:50:10 +01005976static psa_status_t setup_psa_key_derivation(psa_key_derivation_operation_t *derivation,
5977 mbedtls_svc_key_id_t key,
5978 psa_algorithm_t alg,
5979 const unsigned char *raw_psk, size_t raw_psk_length,
5980 const unsigned char *seed, size_t seed_length,
5981 const unsigned char *label, size_t label_length,
5982 const unsigned char *other_secret,
5983 size_t other_secret_length,
5984 size_t capacity)
Jerry Yudc7bd172022-02-17 13:44:15 +08005985{
5986 psa_status_t status;
5987
Gilles Peskine449bd832023-01-11 14:50:10 +01005988 status = psa_key_derivation_setup(derivation, alg);
5989 if (status != PSA_SUCCESS) {
5990 return status;
5991 }
Jerry Yudc7bd172022-02-17 13:44:15 +08005992
Gilles Peskine449bd832023-01-11 14:50:10 +01005993 if (PSA_ALG_IS_TLS12_PRF(alg) || PSA_ALG_IS_TLS12_PSK_TO_MS(alg)) {
5994 status = psa_key_derivation_input_bytes(derivation,
5995 PSA_KEY_DERIVATION_INPUT_SEED,
5996 seed, seed_length);
5997 if (status != PSA_SUCCESS) {
5998 return status;
Przemek Stekiel1f027032022-04-05 17:12:11 +02005999 }
6000
Gilles Peskine449bd832023-01-11 14:50:10 +01006001 if (other_secret != NULL) {
6002 status = psa_key_derivation_input_bytes(derivation,
6003 PSA_KEY_DERIVATION_INPUT_OTHER_SECRET,
6004 other_secret, other_secret_length);
6005 if (status != PSA_SUCCESS) {
6006 return status;
6007 }
6008 }
6009
6010 if (mbedtls_svc_key_id_is_null(key)) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006011 status = psa_key_derivation_input_bytes(
6012 derivation, PSA_KEY_DERIVATION_INPUT_SECRET,
Gilles Peskine449bd832023-01-11 14:50:10 +01006013 raw_psk, raw_psk_length);
6014 } else {
Jerry Yudc7bd172022-02-17 13:44:15 +08006015 status = psa_key_derivation_input_key(
Gilles Peskine449bd832023-01-11 14:50:10 +01006016 derivation, PSA_KEY_DERIVATION_INPUT_SECRET, key);
Jerry Yudc7bd172022-02-17 13:44:15 +08006017 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006018 if (status != PSA_SUCCESS) {
6019 return status;
6020 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006021
Gilles Peskine449bd832023-01-11 14:50:10 +01006022 status = psa_key_derivation_input_bytes(derivation,
6023 PSA_KEY_DERIVATION_INPUT_LABEL,
6024 label, label_length);
6025 if (status != PSA_SUCCESS) {
6026 return status;
6027 }
6028 } else {
6029 return PSA_ERROR_NOT_SUPPORTED;
Jerry Yudc7bd172022-02-17 13:44:15 +08006030 }
6031
Gilles Peskine449bd832023-01-11 14:50:10 +01006032 status = psa_key_derivation_set_capacity(derivation, capacity);
6033 if (status != PSA_SUCCESS) {
6034 return status;
6035 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006036
Gilles Peskine449bd832023-01-11 14:50:10 +01006037 return PSA_SUCCESS;
Jerry Yudc7bd172022-02-17 13:44:15 +08006038}
6039
Andrzej Kurek57d10632022-10-24 10:32:01 -04006040#if defined(PSA_WANT_ALG_SHA_384) || \
6041 defined(PSA_WANT_ALG_SHA_256)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006042MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006043static int tls_prf_generic(mbedtls_md_type_t md_type,
6044 const unsigned char *secret, size_t slen,
6045 const char *label,
6046 const unsigned char *random, size_t rlen,
6047 unsigned char *dstbuf, size_t dlen)
Jerry Yudc7bd172022-02-17 13:44:15 +08006048{
6049 psa_status_t status;
6050 psa_algorithm_t alg;
6051 mbedtls_svc_key_id_t master_key = MBEDTLS_SVC_KEY_ID_INIT;
6052 psa_key_derivation_operation_t derivation =
6053 PSA_KEY_DERIVATION_OPERATION_INIT;
6054
Gilles Peskine449bd832023-01-11 14:50:10 +01006055 if (md_type == MBEDTLS_MD_SHA384) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006056 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
Gilles Peskine449bd832023-01-11 14:50:10 +01006057 } else {
Jerry Yudc7bd172022-02-17 13:44:15 +08006058 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
Gilles Peskine449bd832023-01-11 14:50:10 +01006059 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006060
6061 /* Normally a "secret" should be long enough to be impossible to
6062 * find by brute force, and in particular should not be empty. But
6063 * this PRF is also used to derive an IV, in particular in EAP-TLS,
6064 * and for this use case it makes sense to have a 0-length "secret".
6065 * Since the key API doesn't allow importing a key of length 0,
6066 * keep master_key=0, which setup_psa_key_derivation() understands
6067 * to mean a 0-length "secret" input. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006068 if (slen != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006069 psa_key_attributes_t key_attributes = psa_key_attributes_init();
Gilles Peskine449bd832023-01-11 14:50:10 +01006070 psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE);
6071 psa_set_key_algorithm(&key_attributes, alg);
6072 psa_set_key_type(&key_attributes, PSA_KEY_TYPE_DERIVE);
Jerry Yudc7bd172022-02-17 13:44:15 +08006073
Gilles Peskine449bd832023-01-11 14:50:10 +01006074 status = psa_import_key(&key_attributes, secret, slen, &master_key);
6075 if (status != PSA_SUCCESS) {
6076 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
6077 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006078 }
6079
Gilles Peskine449bd832023-01-11 14:50:10 +01006080 status = setup_psa_key_derivation(&derivation,
6081 master_key, alg,
6082 NULL, 0,
6083 random, rlen,
6084 (unsigned char const *) label,
6085 (size_t) strlen(label),
6086 NULL, 0,
6087 dlen);
6088 if (status != PSA_SUCCESS) {
6089 psa_key_derivation_abort(&derivation);
6090 psa_destroy_key(master_key);
6091 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yudc7bd172022-02-17 13:44:15 +08006092 }
6093
Gilles Peskine449bd832023-01-11 14:50:10 +01006094 status = psa_key_derivation_output_bytes(&derivation, dstbuf, dlen);
6095 if (status != PSA_SUCCESS) {
6096 psa_key_derivation_abort(&derivation);
6097 psa_destroy_key(master_key);
6098 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yudc7bd172022-02-17 13:44:15 +08006099 }
6100
Gilles Peskine449bd832023-01-11 14:50:10 +01006101 status = psa_key_derivation_abort(&derivation);
6102 if (status != PSA_SUCCESS) {
6103 psa_destroy_key(master_key);
6104 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yudc7bd172022-02-17 13:44:15 +08006105 }
6106
Gilles Peskine449bd832023-01-11 14:50:10 +01006107 if (!mbedtls_svc_key_id_is_null(master_key)) {
6108 status = psa_destroy_key(master_key);
6109 }
6110 if (status != PSA_SUCCESS) {
6111 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
6112 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006113
Gilles Peskine449bd832023-01-11 14:50:10 +01006114 return 0;
Jerry Yudc7bd172022-02-17 13:44:15 +08006115}
Andrzej Kurek57d10632022-10-24 10:32:01 -04006116#endif /* PSA_WANT_ALG_SHA_256 || PSA_WANT_ALG_SHA_384 */
Jerry Yudc7bd172022-02-17 13:44:15 +08006117#else /* MBEDTLS_USE_PSA_CRYPTO */
6118
Andrzej Kurek57d10632022-10-24 10:32:01 -04006119#if defined(MBEDTLS_MD_C) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01006120 (defined(MBEDTLS_SHA256_C) || \
6121 defined(MBEDTLS_SHA384_C))
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006122MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006123static int tls_prf_generic(mbedtls_md_type_t md_type,
6124 const unsigned char *secret, size_t slen,
6125 const char *label,
6126 const unsigned char *random, size_t rlen,
6127 unsigned char *dstbuf, size_t dlen)
Jerry Yudc7bd172022-02-17 13:44:15 +08006128{
6129 size_t nb;
6130 size_t i, j, k, md_len;
6131 unsigned char *tmp;
6132 size_t tmp_len = 0;
6133 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
6134 const mbedtls_md_info_t *md_info;
6135 mbedtls_md_context_t md_ctx;
6136 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6137
Gilles Peskine449bd832023-01-11 14:50:10 +01006138 mbedtls_md_init(&md_ctx);
Jerry Yudc7bd172022-02-17 13:44:15 +08006139
Gilles Peskine449bd832023-01-11 14:50:10 +01006140 if ((md_info = mbedtls_md_info_from_type(md_type)) == NULL) {
6141 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
6142 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006143
Gilles Peskine449bd832023-01-11 14:50:10 +01006144 md_len = mbedtls_md_get_size(md_info);
Jerry Yudc7bd172022-02-17 13:44:15 +08006145
Gilles Peskine449bd832023-01-11 14:50:10 +01006146 tmp_len = md_len + strlen(label) + rlen;
6147 tmp = mbedtls_calloc(1, tmp_len);
6148 if (tmp == NULL) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006149 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
6150 goto exit;
6151 }
6152
Gilles Peskine449bd832023-01-11 14:50:10 +01006153 nb = strlen(label);
6154 memcpy(tmp + md_len, label, nb);
6155 memcpy(tmp + md_len + nb, random, rlen);
Jerry Yudc7bd172022-02-17 13:44:15 +08006156 nb += rlen;
6157
6158 /*
6159 * Compute P_<hash>(secret, label + random)[0..dlen]
6160 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006161 if ((ret = mbedtls_md_setup(&md_ctx, md_info, 1)) != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006162 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006163 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006164
Gilles Peskine449bd832023-01-11 14:50:10 +01006165 ret = mbedtls_md_hmac_starts(&md_ctx, secret, slen);
6166 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006167 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006168 }
6169 ret = mbedtls_md_hmac_update(&md_ctx, tmp + md_len, nb);
6170 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006171 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006172 }
6173 ret = mbedtls_md_hmac_finish(&md_ctx, tmp);
6174 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006175 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006176 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006177
Gilles Peskine449bd832023-01-11 14:50:10 +01006178 for (i = 0; i < dlen; i += md_len) {
6179 ret = mbedtls_md_hmac_reset(&md_ctx);
6180 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006181 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006182 }
6183 ret = mbedtls_md_hmac_update(&md_ctx, tmp, md_len + nb);
6184 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006185 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006186 }
6187 ret = mbedtls_md_hmac_finish(&md_ctx, h_i);
6188 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006189 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006190 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006191
Gilles Peskine449bd832023-01-11 14:50:10 +01006192 ret = mbedtls_md_hmac_reset(&md_ctx);
6193 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006194 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006195 }
6196 ret = mbedtls_md_hmac_update(&md_ctx, tmp, md_len);
6197 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006198 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006199 }
6200 ret = mbedtls_md_hmac_finish(&md_ctx, tmp);
6201 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006202 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006203 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006204
Gilles Peskine449bd832023-01-11 14:50:10 +01006205 k = (i + md_len > dlen) ? dlen % md_len : md_len;
Jerry Yudc7bd172022-02-17 13:44:15 +08006206
Gilles Peskine449bd832023-01-11 14:50:10 +01006207 for (j = 0; j < k; j++) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006208 dstbuf[i + j] = h_i[j];
Gilles Peskine449bd832023-01-11 14:50:10 +01006209 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006210 }
6211
6212exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006213 mbedtls_md_free(&md_ctx);
Jerry Yudc7bd172022-02-17 13:44:15 +08006214
Gilles Peskine449bd832023-01-11 14:50:10 +01006215 if (tmp != NULL) {
6216 mbedtls_platform_zeroize(tmp, tmp_len);
6217 }
Dave Rodgman29b9b2b2022-11-01 16:08:14 +00006218
Gilles Peskine449bd832023-01-11 14:50:10 +01006219 mbedtls_platform_zeroize(h_i, sizeof(h_i));
Jerry Yudc7bd172022-02-17 13:44:15 +08006220
Gilles Peskine449bd832023-01-11 14:50:10 +01006221 mbedtls_free(tmp);
Jerry Yudc7bd172022-02-17 13:44:15 +08006222
Gilles Peskine449bd832023-01-11 14:50:10 +01006223 return ret;
Jerry Yudc7bd172022-02-17 13:44:15 +08006224}
Andrzej Kurek57d10632022-10-24 10:32:01 -04006225#endif /* MBEDTLS_MD_C && ( MBEDTLS_SHA256_C || MBEDTLS_SHA384_C ) */
Jerry Yudc7bd172022-02-17 13:44:15 +08006226#endif /* MBEDTLS_USE_PSA_CRYPTO */
6227
Andrzej Kurek25f27152022-08-17 16:09:31 -04006228#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006229MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006230static int tls_prf_sha256(const unsigned char *secret, size_t slen,
6231 const char *label,
6232 const unsigned char *random, size_t rlen,
6233 unsigned char *dstbuf, size_t dlen)
Jerry Yudc7bd172022-02-17 13:44:15 +08006234{
Gilles Peskine449bd832023-01-11 14:50:10 +01006235 return tls_prf_generic(MBEDTLS_MD_SHA256, secret, slen,
6236 label, random, rlen, dstbuf, dlen);
Jerry Yudc7bd172022-02-17 13:44:15 +08006237}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04006238#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudc7bd172022-02-17 13:44:15 +08006239
Andrzej Kurek25f27152022-08-17 16:09:31 -04006240#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006241MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006242static int tls_prf_sha384(const unsigned char *secret, size_t slen,
6243 const char *label,
6244 const unsigned char *random, size_t rlen,
6245 unsigned char *dstbuf, size_t dlen)
Jerry Yudc7bd172022-02-17 13:44:15 +08006246{
Gilles Peskine449bd832023-01-11 14:50:10 +01006247 return tls_prf_generic(MBEDTLS_MD_SHA384, secret, slen,
6248 label, random, rlen, dstbuf, dlen);
Jerry Yudc7bd172022-02-17 13:44:15 +08006249}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04006250#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yudc7bd172022-02-17 13:44:15 +08006251
Jerry Yuf009d862022-02-17 14:01:37 +08006252/*
6253 * Set appropriate PRF function and other SSL / TLS1.2 functions
6254 *
6255 * Inputs:
Jerry Yuf009d862022-02-17 14:01:37 +08006256 * - hash associated with the ciphersuite (only used by TLS 1.2)
6257 *
6258 * Outputs:
6259 * - the tls_prf, calc_verify and calc_finished members of handshake structure
6260 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006261MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006262static int ssl_set_handshake_prfs(mbedtls_ssl_handshake_params *handshake,
6263 mbedtls_md_type_t hash)
Jerry Yuf009d862022-02-17 14:01:37 +08006264{
Andrzej Kurek25f27152022-08-17 16:09:31 -04006265#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01006266 if (hash == MBEDTLS_MD_SHA384) {
Jerry Yuf009d862022-02-17 14:01:37 +08006267 handshake->tls_prf = tls_prf_sha384;
6268 handshake->calc_verify = ssl_calc_verify_tls_sha384;
6269 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Gilles Peskine449bd832023-01-11 14:50:10 +01006270 } else
Jerry Yuf009d862022-02-17 14:01:37 +08006271#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04006272#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Jerry Yuf009d862022-02-17 14:01:37 +08006273 {
Ronald Cron81591aa2022-03-07 09:05:51 +01006274 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08006275 handshake->tls_prf = tls_prf_sha256;
6276 handshake->calc_verify = ssl_calc_verify_tls_sha256;
6277 handshake->calc_finished = ssl_calc_finished_tls_sha256;
6278 }
Ronald Cron81591aa2022-03-07 09:05:51 +01006279#else
Jerry Yuf009d862022-02-17 14:01:37 +08006280 {
Jerry Yuf009d862022-02-17 14:01:37 +08006281 (void) handshake;
Ronald Cron81591aa2022-03-07 09:05:51 +01006282 (void) hash;
Gilles Peskine449bd832023-01-11 14:50:10 +01006283 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yuf009d862022-02-17 14:01:37 +08006284 }
Ronald Cron81591aa2022-03-07 09:05:51 +01006285#endif
Jerry Yuf009d862022-02-17 14:01:37 +08006286
Gilles Peskine449bd832023-01-11 14:50:10 +01006287 return 0;
Jerry Yuf009d862022-02-17 14:01:37 +08006288}
Jerry Yud6ab2352022-02-17 14:03:43 +08006289
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006290/*
6291 * Compute master secret if needed
6292 *
6293 * Parameters:
6294 * [in/out] handshake
6295 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
6296 * (PSA-PSK) ciphersuite_info, psk_opaque
6297 * [out] premaster (cleared)
6298 * [out] master
6299 * [in] ssl: optionally used for debugging, EMS and PSA-PSK
6300 * debug: conf->f_dbg, conf->p_dbg
6301 * EMS: passed to calc_verify (debug + session_negotiate)
Ronald Crona25cf582022-03-07 11:10:36 +01006302 * PSA-PSA: conf
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006303 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006304MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006305static int ssl_compute_master(mbedtls_ssl_handshake_params *handshake,
6306 unsigned char *master,
6307 const mbedtls_ssl_context *ssl)
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006308{
6309 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6310
6311 /* cf. RFC 5246, Section 8.1:
6312 * "The master secret is always exactly 48 bytes in length." */
6313 size_t const master_secret_len = 48;
6314
6315#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
6316 unsigned char session_hash[48];
6317#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
6318
6319 /* The label for the KDF used for key expansion.
6320 * This is either "master secret" or "extended master secret"
6321 * depending on whether the Extended Master Secret extension
6322 * is used. */
6323 char const *lbl = "master secret";
6324
Przemek Stekielae4ed302022-04-05 17:15:55 +02006325 /* The seed for the KDF used for key expansion.
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006326 * - If the Extended Master Secret extension is not used,
6327 * this is ClientHello.Random + ServerHello.Random
6328 * (see Sect. 8.1 in RFC 5246).
6329 * - If the Extended Master Secret extension is used,
6330 * this is the transcript of the handshake so far.
6331 * (see Sect. 4 in RFC 7627). */
Przemek Stekielae4ed302022-04-05 17:15:55 +02006332 unsigned char const *seed = handshake->randbytes;
6333 size_t seed_len = 64;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006334
6335#if !defined(MBEDTLS_DEBUG_C) && \
6336 !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \
6337 !(defined(MBEDTLS_USE_PSA_CRYPTO) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01006338 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED))
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006339 ssl = NULL; /* make sure we don't use it except for those cases */
6340 (void) ssl;
6341#endif
6342
Gilles Peskine449bd832023-01-11 14:50:10 +01006343 if (handshake->resume != 0) {
6344 MBEDTLS_SSL_DEBUG_MSG(3, ("no premaster (session resumed)"));
6345 return 0;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006346 }
6347
6348#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Gilles Peskine449bd832023-01-11 14:50:10 +01006349 if (handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED) {
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006350 lbl = "extended master secret";
Przemek Stekielae4ed302022-04-05 17:15:55 +02006351 seed = session_hash;
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01006352 ret = handshake->calc_verify(ssl, session_hash, &seed_len);
6353 if (ret != 0) {
6354 MBEDTLS_SSL_DEBUG_RET(1, "calc_verify", ret);
6355 }
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006356
Gilles Peskine449bd832023-01-11 14:50:10 +01006357 MBEDTLS_SSL_DEBUG_BUF(3, "session hash for extended master secret",
6358 session_hash, seed_len);
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006359 }
Przemek Stekiel169bf0b2022-04-29 07:53:29 +02006360#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006361
Przemek Stekiel99114f32022-04-22 11:20:09 +02006362#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Przemek Stekiel8a4b7fd2022-04-28 09:22:22 +02006363 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006364 if (mbedtls_ssl_ciphersuite_uses_psk(handshake->ciphersuite_info) == 1) {
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006365 /* Perform PSK-to-MS expansion in a single step. */
6366 psa_status_t status;
6367 psa_algorithm_t alg;
6368 mbedtls_svc_key_id_t psk;
6369 psa_key_derivation_operation_t derivation =
6370 PSA_KEY_DERIVATION_OPERATION_INIT;
6371 mbedtls_md_type_t hash_alg = handshake->ciphersuite_info->mac;
6372
Gilles Peskine449bd832023-01-11 14:50:10 +01006373 MBEDTLS_SSL_DEBUG_MSG(2, ("perform PSA-based PSK-to-MS expansion"));
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006374
Gilles Peskine449bd832023-01-11 14:50:10 +01006375 psk = mbedtls_ssl_get_opaque_psk(ssl);
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006376
Gilles Peskine449bd832023-01-11 14:50:10 +01006377 if (hash_alg == MBEDTLS_MD_SHA384) {
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006378 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
Gilles Peskine449bd832023-01-11 14:50:10 +01006379 } else {
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006380 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
Gilles Peskine449bd832023-01-11 14:50:10 +01006381 }
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006382
Przemek Stekiel51a1f362022-04-13 08:57:06 +02006383 size_t other_secret_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006384 unsigned char *other_secret = NULL;
Przemek Stekielc2033402022-04-05 17:19:41 +02006385
Gilles Peskine449bd832023-01-11 14:50:10 +01006386 switch (handshake->ciphersuite_info->key_exchange) {
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006387 /* Provide other secret.
Przemek Stekiel51a1f362022-04-13 08:57:06 +02006388 * Other secret is stored in premaster, where first 2 bytes hold the
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006389 * length of the other key.
Przemek Stekielc2033402022-04-05 17:19:41 +02006390 */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006391 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Przemek Stekiel8abcee92022-04-28 09:16:28 +02006392 /* For RSA-PSK other key length is always 48 bytes. */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006393 other_secret_len = 48;
6394 other_secret = handshake->premaster + 2;
6395 break;
6396 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006397 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006398 other_secret_len = MBEDTLS_GET_UINT16_BE(handshake->premaster, 0);
6399 other_secret = handshake->premaster + 2;
6400 break;
6401 default:
6402 break;
Przemek Stekielc2033402022-04-05 17:19:41 +02006403 }
6404
Gilles Peskine449bd832023-01-11 14:50:10 +01006405 status = setup_psa_key_derivation(&derivation, psk, alg,
6406 ssl->conf->psk, ssl->conf->psk_len,
6407 seed, seed_len,
6408 (unsigned char const *) lbl,
6409 (size_t) strlen(lbl),
6410 other_secret, other_secret_len,
6411 master_secret_len);
6412 if (status != PSA_SUCCESS) {
6413 psa_key_derivation_abort(&derivation);
6414 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006415 }
6416
Gilles Peskine449bd832023-01-11 14:50:10 +01006417 status = psa_key_derivation_output_bytes(&derivation,
6418 master,
6419 master_secret_len);
6420 if (status != PSA_SUCCESS) {
6421 psa_key_derivation_abort(&derivation);
6422 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006423 }
6424
Gilles Peskine449bd832023-01-11 14:50:10 +01006425 status = psa_key_derivation_abort(&derivation);
6426 if (status != PSA_SUCCESS) {
6427 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
6428 }
6429 } else
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006430#endif
6431 {
Neil Armstrongca7d5062022-05-31 14:43:23 +02006432#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01006433 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
6434 if (handshake->ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE) {
Neil Armstrongca7d5062022-05-31 14:43:23 +02006435 psa_status_t status;
6436 psa_algorithm_t alg = PSA_ALG_TLS12_ECJPAKE_TO_PMS;
6437 psa_key_derivation_operation_t derivation =
6438 PSA_KEY_DERIVATION_OPERATION_INIT;
6439
Gilles Peskine449bd832023-01-11 14:50:10 +01006440 MBEDTLS_SSL_DEBUG_MSG(2, ("perform PSA-based PMS KDF for ECJPAKE"));
Neil Armstrongca7d5062022-05-31 14:43:23 +02006441
6442 handshake->pmslen = PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE;
6443
Gilles Peskine449bd832023-01-11 14:50:10 +01006444 status = psa_key_derivation_setup(&derivation, alg);
6445 if (status != PSA_SUCCESS) {
6446 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Neil Armstrongca7d5062022-05-31 14:43:23 +02006447 }
6448
Gilles Peskine449bd832023-01-11 14:50:10 +01006449 status = psa_key_derivation_set_capacity(&derivation,
6450 PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE);
6451 if (status != PSA_SUCCESS) {
6452 psa_key_derivation_abort(&derivation);
6453 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Neil Armstrongca7d5062022-05-31 14:43:23 +02006454 }
6455
Gilles Peskine449bd832023-01-11 14:50:10 +01006456 status = psa_pake_get_implicit_key(&handshake->psa_pake_ctx,
6457 &derivation);
6458 if (status != PSA_SUCCESS) {
6459 psa_key_derivation_abort(&derivation);
6460 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Neil Armstrongca7d5062022-05-31 14:43:23 +02006461 }
6462
Gilles Peskine449bd832023-01-11 14:50:10 +01006463 status = psa_key_derivation_output_bytes(&derivation,
6464 handshake->premaster,
6465 handshake->pmslen);
6466 if (status != PSA_SUCCESS) {
6467 psa_key_derivation_abort(&derivation);
6468 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
6469 }
6470
6471 status = psa_key_derivation_abort(&derivation);
6472 if (status != PSA_SUCCESS) {
6473 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Neil Armstrongca7d5062022-05-31 14:43:23 +02006474 }
6475 }
6476#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01006477 ret = handshake->tls_prf(handshake->premaster, handshake->pmslen,
6478 lbl, seed, seed_len,
6479 master,
6480 master_secret_len);
6481 if (ret != 0) {
6482 MBEDTLS_SSL_DEBUG_RET(1, "prf", ret);
6483 return ret;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006484 }
6485
Gilles Peskine449bd832023-01-11 14:50:10 +01006486 MBEDTLS_SSL_DEBUG_BUF(3, "premaster secret",
6487 handshake->premaster,
6488 handshake->pmslen);
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006489
Gilles Peskine449bd832023-01-11 14:50:10 +01006490 mbedtls_platform_zeroize(handshake->premaster,
6491 sizeof(handshake->premaster));
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006492 }
6493
Gilles Peskine449bd832023-01-11 14:50:10 +01006494 return 0;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006495}
6496
Gilles Peskine449bd832023-01-11 14:50:10 +01006497int mbedtls_ssl_derive_keys(mbedtls_ssl_context *ssl)
Jerry Yud62f87e2022-02-17 14:09:02 +08006498{
6499 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6500 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
6501 ssl->handshake->ciphersuite_info;
6502
Gilles Peskine449bd832023-01-11 14:50:10 +01006503 MBEDTLS_SSL_DEBUG_MSG(2, ("=> derive keys"));
Jerry Yud62f87e2022-02-17 14:09:02 +08006504
6505 /* Set PRF, calc_verify and calc_finished function pointers */
Gilles Peskine449bd832023-01-11 14:50:10 +01006506 ret = ssl_set_handshake_prfs(ssl->handshake,
6507 ciphersuite_info->mac);
6508 if (ret != 0) {
6509 MBEDTLS_SSL_DEBUG_RET(1, "ssl_set_handshake_prfs", ret);
6510 return ret;
Jerry Yud62f87e2022-02-17 14:09:02 +08006511 }
6512
6513 /* Compute master secret if needed */
Gilles Peskine449bd832023-01-11 14:50:10 +01006514 ret = ssl_compute_master(ssl->handshake,
6515 ssl->session_negotiate->master,
6516 ssl);
6517 if (ret != 0) {
6518 MBEDTLS_SSL_DEBUG_RET(1, "ssl_compute_master", ret);
6519 return ret;
Jerry Yud62f87e2022-02-17 14:09:02 +08006520 }
6521
6522 /* Swap the client and server random values:
6523 * - MS derivation wanted client+server (RFC 5246 8.1)
6524 * - key derivation wants server+client (RFC 5246 6.3) */
6525 {
6526 unsigned char tmp[64];
Gilles Peskine449bd832023-01-11 14:50:10 +01006527 memcpy(tmp, ssl->handshake->randbytes, 64);
6528 memcpy(ssl->handshake->randbytes, tmp + 32, 32);
6529 memcpy(ssl->handshake->randbytes + 32, tmp, 32);
6530 mbedtls_platform_zeroize(tmp, sizeof(tmp));
Jerry Yud62f87e2022-02-17 14:09:02 +08006531 }
6532
6533 /* Populate transform structure */
Gilles Peskine449bd832023-01-11 14:50:10 +01006534 ret = ssl_tls12_populate_transform(ssl->transform_negotiate,
6535 ssl->session_negotiate->ciphersuite,
6536 ssl->session_negotiate->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02006537#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01006538 ssl->session_negotiate->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02006539#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01006540 ssl->handshake->tls_prf,
6541 ssl->handshake->randbytes,
6542 ssl->tls_version,
6543 ssl->conf->endpoint,
6544 ssl);
6545 if (ret != 0) {
6546 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls12_populate_transform", ret);
6547 return ret;
Jerry Yud62f87e2022-02-17 14:09:02 +08006548 }
6549
6550 /* We no longer need Server/ClientHello.random values */
Gilles Peskine449bd832023-01-11 14:50:10 +01006551 mbedtls_platform_zeroize(ssl->handshake->randbytes,
6552 sizeof(ssl->handshake->randbytes));
Jerry Yud62f87e2022-02-17 14:09:02 +08006553
Gilles Peskine449bd832023-01-11 14:50:10 +01006554 MBEDTLS_SSL_DEBUG_MSG(2, ("<= derive keys"));
Jerry Yud62f87e2022-02-17 14:09:02 +08006555
Gilles Peskine449bd832023-01-11 14:50:10 +01006556 return 0;
Jerry Yud62f87e2022-02-17 14:09:02 +08006557}
Jerry Yu8392e0d2022-02-17 14:10:24 +08006558
Gilles Peskine449bd832023-01-11 14:50:10 +01006559int mbedtls_ssl_set_calc_verify_md(mbedtls_ssl_context *ssl, int md)
Ronald Cron4dcbca92022-03-07 10:21:40 +01006560{
Gilles Peskine449bd832023-01-11 14:50:10 +01006561 switch (md) {
Andrzej Kurek25f27152022-08-17 16:09:31 -04006562#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron4dcbca92022-03-07 10:21:40 +01006563 case MBEDTLS_SSL_HASH_SHA384:
6564 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
6565 break;
6566#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04006567#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Ronald Cron4dcbca92022-03-07 10:21:40 +01006568 case MBEDTLS_SSL_HASH_SHA256:
6569 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
6570 break;
6571#endif
6572 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006573 return -1;
Ronald Cron4dcbca92022-03-07 10:21:40 +01006574 }
Andrzej Kurekeabeb302022-10-17 07:52:51 -04006575#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
6576 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
6577 (void) ssl;
6578#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01006579 return 0;
Ronald Cron4dcbca92022-03-07 10:21:40 +01006580}
6581
Andrzej Kurek25f27152022-08-17 16:09:31 -04006582#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01006583int ssl_calc_verify_tls_sha256(const mbedtls_ssl_context *ssl,
6584 unsigned char *hash,
6585 size_t *hlen)
Jerry Yu8392e0d2022-02-17 14:10:24 +08006586{
6587#if defined(MBEDTLS_USE_PSA_CRYPTO)
6588 size_t hash_size;
6589 psa_status_t status;
6590 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
6591
Gilles Peskine449bd832023-01-11 14:50:10 +01006592 MBEDTLS_SSL_DEBUG_MSG(2, ("=> PSA calc verify sha256"));
6593 status = psa_hash_clone(&ssl->handshake->fin_sha256_psa, &sha256_psa);
6594 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006595 goto exit;
Jerry Yu8392e0d2022-02-17 14:10:24 +08006596 }
6597
Gilles Peskine449bd832023-01-11 14:50:10 +01006598 status = psa_hash_finish(&sha256_psa, hash, 32, &hash_size);
6599 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006600 goto exit;
Jerry Yu8392e0d2022-02-17 14:10:24 +08006601 }
6602
6603 *hlen = 32;
Gilles Peskine449bd832023-01-11 14:50:10 +01006604 MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated verify result", hash, *hlen);
6605 MBEDTLS_SSL_DEBUG_MSG(2, ("<= PSA calc verify"));
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006606
6607exit:
6608 psa_hash_abort(&sha256_psa);
6609 return mbedtls_md_error_from_psa(status);
Jerry Yu8392e0d2022-02-17 14:10:24 +08006610#else
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006611 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu8392e0d2022-02-17 14:10:24 +08006612 mbedtls_sha256_context sha256;
6613
Gilles Peskine449bd832023-01-11 14:50:10 +01006614 mbedtls_sha256_init(&sha256);
Jerry Yu8392e0d2022-02-17 14:10:24 +08006615
Gilles Peskine449bd832023-01-11 14:50:10 +01006616 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc verify sha256"));
Jerry Yu8392e0d2022-02-17 14:10:24 +08006617
Gilles Peskine449bd832023-01-11 14:50:10 +01006618 mbedtls_sha256_clone(&sha256, &ssl->handshake->fin_sha256);
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006619
6620 ret = mbedtls_sha256_finish(&sha256, hash);
6621 if (ret != 0) {
6622 goto exit;
6623 }
Jerry Yu8392e0d2022-02-17 14:10:24 +08006624
6625 *hlen = 32;
6626
Gilles Peskine449bd832023-01-11 14:50:10 +01006627 MBEDTLS_SSL_DEBUG_BUF(3, "calculated verify result", hash, *hlen);
6628 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc verify"));
Jerry Yu8392e0d2022-02-17 14:10:24 +08006629
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006630exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006631 mbedtls_sha256_free(&sha256);
Manuel Pégourié-Gonnard8e176f72023-02-09 10:33:54 +01006632 return ret;
Jerry Yu8392e0d2022-02-17 14:10:24 +08006633#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu8392e0d2022-02-17 14:10:24 +08006634}
Andrzej Kurek25f27152022-08-17 16:09:31 -04006635#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yu8392e0d2022-02-17 14:10:24 +08006636
Andrzej Kurek25f27152022-08-17 16:09:31 -04006637#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01006638int ssl_calc_verify_tls_sha384(const mbedtls_ssl_context *ssl,
6639 unsigned char *hash,
6640 size_t *hlen)
Jerry Yuc1cb3842022-02-17 14:13:48 +08006641{
6642#if defined(MBEDTLS_USE_PSA_CRYPTO)
6643 size_t hash_size;
6644 psa_status_t status;
6645 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
6646
Gilles Peskine449bd832023-01-11 14:50:10 +01006647 MBEDTLS_SSL_DEBUG_MSG(2, ("=> PSA calc verify sha384"));
6648 status = psa_hash_clone(&ssl->handshake->fin_sha384_psa, &sha384_psa);
6649 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006650 goto exit;
Jerry Yuc1cb3842022-02-17 14:13:48 +08006651 }
6652
Gilles Peskine449bd832023-01-11 14:50:10 +01006653 status = psa_hash_finish(&sha384_psa, hash, 48, &hash_size);
6654 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006655 goto exit;
Jerry Yuc1cb3842022-02-17 14:13:48 +08006656 }
6657
6658 *hlen = 48;
Gilles Peskine449bd832023-01-11 14:50:10 +01006659 MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated verify result", hash, *hlen);
6660 MBEDTLS_SSL_DEBUG_MSG(2, ("<= PSA calc verify"));
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006661
6662exit:
6663 psa_hash_abort(&sha384_psa);
6664 return mbedtls_md_error_from_psa(status);
Jerry Yuc1cb3842022-02-17 14:13:48 +08006665#else
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006666 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yuc1cb3842022-02-17 14:13:48 +08006667 mbedtls_sha512_context sha512;
6668
Gilles Peskine449bd832023-01-11 14:50:10 +01006669 mbedtls_sha512_init(&sha512);
Jerry Yuc1cb3842022-02-17 14:13:48 +08006670
Gilles Peskine449bd832023-01-11 14:50:10 +01006671 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc verify sha384"));
Jerry Yuc1cb3842022-02-17 14:13:48 +08006672
Gilles Peskine449bd832023-01-11 14:50:10 +01006673 mbedtls_sha512_clone(&sha512, &ssl->handshake->fin_sha384);
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006674
6675 ret = mbedtls_sha512_finish(&sha512, hash);
6676 if (ret != 0) {
6677 goto exit;
6678 }
Jerry Yuc1cb3842022-02-17 14:13:48 +08006679
6680 *hlen = 48;
6681
Gilles Peskine449bd832023-01-11 14:50:10 +01006682 MBEDTLS_SSL_DEBUG_BUF(3, "calculated verify result", hash, *hlen);
6683 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc verify"));
Jerry Yuc1cb3842022-02-17 14:13:48 +08006684
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006685exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006686 mbedtls_sha512_free(&sha512);
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006687 return ret;
Jerry Yuc1cb3842022-02-17 14:13:48 +08006688#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yuc1cb3842022-02-17 14:13:48 +08006689}
Andrzej Kurek25f27152022-08-17 16:09:31 -04006690#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA */
Jerry Yuc1cb3842022-02-17 14:13:48 +08006691
Neil Armstrong80f6f322022-05-03 17:56:38 +02006692#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
6693 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006694int mbedtls_ssl_psk_derive_premaster(mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex)
Jerry Yuce3dca42022-02-17 14:16:37 +08006695{
6696 unsigned char *p = ssl->handshake->premaster;
Gilles Peskine449bd832023-01-11 14:50:10 +01006697 unsigned char *end = p + sizeof(ssl->handshake->premaster);
Jerry Yuce3dca42022-02-17 14:16:37 +08006698 const unsigned char *psk = NULL;
6699 size_t psk_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006700 int psk_ret = mbedtls_ssl_get_psk(ssl, &psk, &psk_len);
Jerry Yuce3dca42022-02-17 14:16:37 +08006701
Gilles Peskine449bd832023-01-11 14:50:10 +01006702 if (psk_ret == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED) {
Jerry Yuce3dca42022-02-17 14:16:37 +08006703 /*
6704 * This should never happen because the existence of a PSK is always
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006705 * checked before calling this function.
6706 *
6707 * The exception is opaque DHE-PSK. For DHE-PSK fill premaster with
Przemek Stekiel8abcee92022-04-28 09:16:28 +02006708 * the shared secret without PSK.
Jerry Yuce3dca42022-02-17 14:16:37 +08006709 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006710 if (key_ex != MBEDTLS_KEY_EXCHANGE_DHE_PSK) {
6711 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
6712 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006713 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006714 }
6715
6716 /*
6717 * PMS = struct {
6718 * opaque other_secret<0..2^16-1>;
6719 * opaque psk<0..2^16-1>;
6720 * };
6721 * with "other_secret" depending on the particular key exchange
6722 */
6723#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006724 if (key_ex == MBEDTLS_KEY_EXCHANGE_PSK) {
6725 if (end - p < 2) {
6726 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6727 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006728
Gilles Peskine449bd832023-01-11 14:50:10 +01006729 MBEDTLS_PUT_UINT16_BE(psk_len, p, 0);
Jerry Yuce3dca42022-02-17 14:16:37 +08006730 p += 2;
6731
Gilles Peskine449bd832023-01-11 14:50:10 +01006732 if (end < p || (size_t) (end - p) < psk_len) {
6733 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6734 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006735
Gilles Peskine449bd832023-01-11 14:50:10 +01006736 memset(p, 0, psk_len);
Jerry Yuce3dca42022-02-17 14:16:37 +08006737 p += psk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01006738 } else
Jerry Yuce3dca42022-02-17 14:16:37 +08006739#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
6740#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006741 if (key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK) {
Jerry Yuce3dca42022-02-17 14:16:37 +08006742 /*
6743 * other_secret already set by the ClientKeyExchange message,
6744 * and is 48 bytes long
6745 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006746 if (end - p < 2) {
6747 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6748 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006749
6750 *p++ = 0;
6751 *p++ = 48;
6752 p += 48;
Gilles Peskine449bd832023-01-11 14:50:10 +01006753 } else
Jerry Yuce3dca42022-02-17 14:16:37 +08006754#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
6755#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006756 if (key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK) {
Jerry Yuce3dca42022-02-17 14:16:37 +08006757 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6758 size_t len;
6759
6760 /* Write length only when we know the actual value */
Gilles Peskine449bd832023-01-11 14:50:10 +01006761 if ((ret = mbedtls_dhm_calc_secret(&ssl->handshake->dhm_ctx,
6762 p + 2, end - (p + 2), &len,
6763 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
6764 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_calc_secret", ret);
6765 return ret;
Jerry Yuce3dca42022-02-17 14:16:37 +08006766 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006767 MBEDTLS_PUT_UINT16_BE(len, p, 0);
Jerry Yuce3dca42022-02-17 14:16:37 +08006768 p += 2 + len;
6769
Gilles Peskine449bd832023-01-11 14:50:10 +01006770 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: K ", &ssl->handshake->dhm_ctx.K);
6771 } else
Jerry Yuce3dca42022-02-17 14:16:37 +08006772#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Neil Armstrong80f6f322022-05-03 17:56:38 +02006773#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006774 if (key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK) {
Jerry Yuce3dca42022-02-17 14:16:37 +08006775 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6776 size_t zlen;
6777
Gilles Peskine449bd832023-01-11 14:50:10 +01006778 if ((ret = mbedtls_ecdh_calc_secret(&ssl->handshake->ecdh_ctx, &zlen,
6779 p + 2, end - (p + 2),
6780 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
6781 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecdh_calc_secret", ret);
6782 return ret;
Jerry Yuce3dca42022-02-17 14:16:37 +08006783 }
6784
Gilles Peskine449bd832023-01-11 14:50:10 +01006785 MBEDTLS_PUT_UINT16_BE(zlen, p, 0);
Jerry Yuce3dca42022-02-17 14:16:37 +08006786 p += 2 + zlen;
6787
Gilles Peskine449bd832023-01-11 14:50:10 +01006788 MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx,
6789 MBEDTLS_DEBUG_ECDH_Z);
6790 } else
Neil Armstrong80f6f322022-05-03 17:56:38 +02006791#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Jerry Yuce3dca42022-02-17 14:16:37 +08006792 {
Gilles Peskine449bd832023-01-11 14:50:10 +01006793 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
6794 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yuce3dca42022-02-17 14:16:37 +08006795 }
6796
6797 /* opaque psk<0..2^16-1>; */
Gilles Peskine449bd832023-01-11 14:50:10 +01006798 if (end - p < 2) {
6799 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6800 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006801
Gilles Peskine449bd832023-01-11 14:50:10 +01006802 MBEDTLS_PUT_UINT16_BE(psk_len, p, 0);
Jerry Yuce3dca42022-02-17 14:16:37 +08006803 p += 2;
6804
Gilles Peskine449bd832023-01-11 14:50:10 +01006805 if (end < p || (size_t) (end - p) < psk_len) {
6806 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6807 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006808
Gilles Peskine449bd832023-01-11 14:50:10 +01006809 memcpy(p, psk, psk_len);
Jerry Yuce3dca42022-02-17 14:16:37 +08006810 p += psk_len;
6811
6812 ssl->handshake->pmslen = p - ssl->handshake->premaster;
6813
Gilles Peskine449bd832023-01-11 14:50:10 +01006814 return 0;
Jerry Yuce3dca42022-02-17 14:16:37 +08006815}
Neil Armstrong80f6f322022-05-03 17:56:38 +02006816#endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Jerry Yuc2c673d2022-02-17 14:20:39 +08006817
6818#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006819MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006820static int ssl_write_hello_request(mbedtls_ssl_context *ssl);
Jerry Yuc2c673d2022-02-17 14:20:39 +08006821
6822#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006823int mbedtls_ssl_resend_hello_request(mbedtls_ssl_context *ssl)
Jerry Yuc2c673d2022-02-17 14:20:39 +08006824{
6825 /* If renegotiation is not enforced, retransmit until we would reach max
6826 * timeout if we were using the usual handshake doubling scheme */
Gilles Peskine449bd832023-01-11 14:50:10 +01006827 if (ssl->conf->renego_max_records < 0) {
Jerry Yuc2c673d2022-02-17 14:20:39 +08006828 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
6829 unsigned char doublings = 1;
6830
Gilles Peskine449bd832023-01-11 14:50:10 +01006831 while (ratio != 0) {
Jerry Yuc2c673d2022-02-17 14:20:39 +08006832 ++doublings;
6833 ratio >>= 1;
6834 }
6835
Gilles Peskine449bd832023-01-11 14:50:10 +01006836 if (++ssl->renego_records_seen > doublings) {
6837 MBEDTLS_SSL_DEBUG_MSG(2, ("no longer retransmitting hello request"));
6838 return 0;
Jerry Yuc2c673d2022-02-17 14:20:39 +08006839 }
6840 }
6841
Gilles Peskine449bd832023-01-11 14:50:10 +01006842 return ssl_write_hello_request(ssl);
Jerry Yuc2c673d2022-02-17 14:20:39 +08006843}
6844#endif
6845#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Jerry Yud9526692022-02-17 14:23:47 +08006846
Jerry Yud9526692022-02-17 14:23:47 +08006847/*
6848 * Handshake functions
6849 */
6850#if !defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
6851/* No certificate support -> dummy functions */
Gilles Peskine449bd832023-01-11 14:50:10 +01006852int mbedtls_ssl_write_certificate(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08006853{
6854 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6855 ssl->handshake->ciphersuite_info;
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 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
6860 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006861 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01006862 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08006863 }
6864
Gilles Peskine449bd832023-01-11 14:50:10 +01006865 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
6866 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08006867}
6868
Gilles Peskine449bd832023-01-11 14:50:10 +01006869int mbedtls_ssl_parse_certificate(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08006870{
6871 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6872 ssl->handshake->ciphersuite_info;
6873
Gilles Peskine449bd832023-01-11 14:50:10 +01006874 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006875
Gilles Peskine449bd832023-01-11 14:50:10 +01006876 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
6877 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006878 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01006879 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08006880 }
6881
Gilles Peskine449bd832023-01-11 14:50:10 +01006882 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
6883 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08006884}
6885
6886#else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
6887/* Some certificate support -> implement write and parse */
6888
Gilles Peskine449bd832023-01-11 14:50:10 +01006889int mbedtls_ssl_write_certificate(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08006890{
6891 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
6892 size_t i, n;
6893 const mbedtls_x509_crt *crt;
6894 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6895 ssl->handshake->ciphersuite_info;
6896
Gilles Peskine449bd832023-01-11 14:50:10 +01006897 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006898
Gilles Peskine449bd832023-01-11 14:50:10 +01006899 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
6900 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006901 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01006902 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08006903 }
6904
6905#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01006906 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
6907 if (ssl->handshake->client_auth == 0) {
6908 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006909 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01006910 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08006911 }
6912 }
6913#endif /* MBEDTLS_SSL_CLI_C */
6914#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01006915 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
6916 if (mbedtls_ssl_own_cert(ssl) == NULL) {
Jerry Yud9526692022-02-17 14:23:47 +08006917 /* Should never happen because we shouldn't have picked the
6918 * ciphersuite if we don't have a certificate. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006919 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08006920 }
6921 }
6922#endif
6923
Gilles Peskine449bd832023-01-11 14:50:10 +01006924 MBEDTLS_SSL_DEBUG_CRT(3, "own certificate", mbedtls_ssl_own_cert(ssl));
Jerry Yud9526692022-02-17 14:23:47 +08006925
6926 /*
6927 * 0 . 0 handshake type
6928 * 1 . 3 handshake length
6929 * 4 . 6 length of all certs
6930 * 7 . 9 length of cert. 1
6931 * 10 . n-1 peer certificate
6932 * n . n+2 length of cert. 2
6933 * n+3 . ... upper level cert, etc.
6934 */
6935 i = 7;
Gilles Peskine449bd832023-01-11 14:50:10 +01006936 crt = mbedtls_ssl_own_cert(ssl);
Jerry Yud9526692022-02-17 14:23:47 +08006937
Gilles Peskine449bd832023-01-11 14:50:10 +01006938 while (crt != NULL) {
Jerry Yud9526692022-02-17 14:23:47 +08006939 n = crt->raw.len;
Gilles Peskine449bd832023-01-11 14:50:10 +01006940 if (n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i) {
6941 MBEDTLS_SSL_DEBUG_MSG(1, ("certificate too large, %" MBEDTLS_PRINTF_SIZET
6942 " > %" MBEDTLS_PRINTF_SIZET,
6943 i + 3 + n, (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN));
6944 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
Jerry Yud9526692022-02-17 14:23:47 +08006945 }
6946
Gilles Peskine449bd832023-01-11 14:50:10 +01006947 ssl->out_msg[i] = MBEDTLS_BYTE_2(n);
6948 ssl->out_msg[i + 1] = MBEDTLS_BYTE_1(n);
6949 ssl->out_msg[i + 2] = MBEDTLS_BYTE_0(n);
Jerry Yud9526692022-02-17 14:23:47 +08006950
Gilles Peskine449bd832023-01-11 14:50:10 +01006951 i += 3; memcpy(ssl->out_msg + i, crt->raw.p, n);
Jerry Yud9526692022-02-17 14:23:47 +08006952 i += n; crt = crt->next;
6953 }
6954
Gilles Peskine449bd832023-01-11 14:50:10 +01006955 ssl->out_msg[4] = MBEDTLS_BYTE_2(i - 7);
6956 ssl->out_msg[5] = MBEDTLS_BYTE_1(i - 7);
6957 ssl->out_msg[6] = MBEDTLS_BYTE_0(i - 7);
Jerry Yud9526692022-02-17 14:23:47 +08006958
6959 ssl->out_msglen = i;
6960 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
6961 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
6962
6963 ssl->state++;
6964
Gilles Peskine449bd832023-01-11 14:50:10 +01006965 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
6966 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
6967 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08006968 }
6969
Gilles Peskine449bd832023-01-11 14:50:10 +01006970 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006971
Gilles Peskine449bd832023-01-11 14:50:10 +01006972 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08006973}
6974
6975#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
6976
6977#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006978MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006979static int ssl_check_peer_crt_unchanged(mbedtls_ssl_context *ssl,
6980 unsigned char *crt_buf,
6981 size_t crt_buf_len)
Jerry Yud9526692022-02-17 14:23:47 +08006982{
6983 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
6984
Gilles Peskine449bd832023-01-11 14:50:10 +01006985 if (peer_crt == NULL) {
6986 return -1;
6987 }
Jerry Yud9526692022-02-17 14:23:47 +08006988
Gilles Peskine449bd832023-01-11 14:50:10 +01006989 if (peer_crt->raw.len != crt_buf_len) {
6990 return -1;
6991 }
Jerry Yud9526692022-02-17 14:23:47 +08006992
Gilles Peskine449bd832023-01-11 14:50:10 +01006993 return memcmp(peer_crt->raw.p, crt_buf, peer_crt->raw.len);
Jerry Yud9526692022-02-17 14:23:47 +08006994}
6995#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006996MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006997static int ssl_check_peer_crt_unchanged(mbedtls_ssl_context *ssl,
6998 unsigned char *crt_buf,
6999 size_t crt_buf_len)
Jerry Yud9526692022-02-17 14:23:47 +08007000{
7001 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7002 unsigned char const * const peer_cert_digest =
7003 ssl->session->peer_cert_digest;
7004 mbedtls_md_type_t const peer_cert_digest_type =
7005 ssl->session->peer_cert_digest_type;
7006 mbedtls_md_info_t const * const digest_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01007007 mbedtls_md_info_from_type(peer_cert_digest_type);
Jerry Yud9526692022-02-17 14:23:47 +08007008 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
7009 size_t digest_len;
7010
Gilles Peskine449bd832023-01-11 14:50:10 +01007011 if (peer_cert_digest == NULL || digest_info == NULL) {
7012 return -1;
7013 }
Jerry Yud9526692022-02-17 14:23:47 +08007014
Gilles Peskine449bd832023-01-11 14:50:10 +01007015 digest_len = mbedtls_md_get_size(digest_info);
7016 if (digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN) {
7017 return -1;
7018 }
Jerry Yud9526692022-02-17 14:23:47 +08007019
Gilles Peskine449bd832023-01-11 14:50:10 +01007020 ret = mbedtls_md(digest_info, crt_buf, crt_buf_len, tmp_digest);
7021 if (ret != 0) {
7022 return -1;
7023 }
Jerry Yud9526692022-02-17 14:23:47 +08007024
Gilles Peskine449bd832023-01-11 14:50:10 +01007025 return memcmp(tmp_digest, peer_cert_digest, digest_len);
Jerry Yud9526692022-02-17 14:23:47 +08007026}
7027#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7028#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
7029
7030/*
7031 * Once the certificate message is read, parse it into a cert chain and
7032 * perform basic checks, but leave actual verification to the caller
7033 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007034MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007035static int ssl_parse_certificate_chain(mbedtls_ssl_context *ssl,
7036 mbedtls_x509_crt *chain)
Jerry Yud9526692022-02-17 14:23:47 +08007037{
7038 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7039#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007040 int crt_cnt = 0;
Jerry Yud9526692022-02-17 14:23:47 +08007041#endif
7042 size_t i, n;
7043 uint8_t alert;
7044
Gilles Peskine449bd832023-01-11 14:50:10 +01007045 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
7046 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7047 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7048 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
7049 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Jerry Yud9526692022-02-17 14:23:47 +08007050 }
7051
Gilles Peskine449bd832023-01-11 14:50:10 +01007052 if (ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE) {
7053 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7054 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
7055 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Jerry Yud9526692022-02-17 14:23:47 +08007056 }
7057
Gilles Peskine449bd832023-01-11 14:50:10 +01007058 if (ssl->in_hslen < mbedtls_ssl_hs_hdr_len(ssl) + 3 + 3) {
7059 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7060 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7061 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
7062 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007063 }
7064
Gilles Peskine449bd832023-01-11 14:50:10 +01007065 i = mbedtls_ssl_hs_hdr_len(ssl);
Jerry Yud9526692022-02-17 14:23:47 +08007066
7067 /*
7068 * Same message structure as in mbedtls_ssl_write_certificate()
7069 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007070 n = (ssl->in_msg[i+1] << 8) | ssl->in_msg[i+2];
Jerry Yud9526692022-02-17 14:23:47 +08007071
Gilles Peskine449bd832023-01-11 14:50:10 +01007072 if (ssl->in_msg[i] != 0 ||
7073 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len(ssl)) {
7074 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7075 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7076 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
7077 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007078 }
7079
7080 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
7081 i += 3;
7082
7083 /* Iterate through and parse the CRTs in the provided chain. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007084 while (i < ssl->in_hslen) {
Jerry Yud9526692022-02-17 14:23:47 +08007085 /* Check that there's room for the next CRT's length fields. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007086 if (i + 3 > ssl->in_hslen) {
7087 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7088 mbedtls_ssl_send_alert_message(ssl,
7089 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7090 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
7091 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007092 }
7093 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
7094 * anything beyond 2**16 ~ 64K. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007095 if (ssl->in_msg[i] != 0) {
7096 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7097 mbedtls_ssl_send_alert_message(ssl,
7098 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7099 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT);
7100 return MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Jerry Yud9526692022-02-17 14:23:47 +08007101 }
7102
7103 /* Read length of the next CRT in the chain. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007104 n = ((unsigned int) ssl->in_msg[i + 1] << 8)
Jerry Yud9526692022-02-17 14:23:47 +08007105 | (unsigned int) ssl->in_msg[i + 2];
7106 i += 3;
7107
Gilles Peskine449bd832023-01-11 14:50:10 +01007108 if (n < 128 || i + n > ssl->in_hslen) {
7109 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7110 mbedtls_ssl_send_alert_message(ssl,
7111 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7112 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
7113 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007114 }
7115
7116 /* Check if we're handling the first CRT in the chain. */
7117#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007118 if (crt_cnt++ == 0 &&
Jerry Yud9526692022-02-17 14:23:47 +08007119 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Gilles Peskine449bd832023-01-11 14:50:10 +01007120 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
Jerry Yud9526692022-02-17 14:23:47 +08007121 /* During client-side renegotiation, check that the server's
7122 * end-CRTs hasn't changed compared to the initial handshake,
7123 * mitigating the triple handshake attack. On success, reuse
7124 * the original end-CRT instead of parsing it again. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007125 MBEDTLS_SSL_DEBUG_MSG(3, ("Check that peer CRT hasn't changed during renegotiation"));
7126 if (ssl_check_peer_crt_unchanged(ssl,
7127 &ssl->in_msg[i],
7128 n) != 0) {
7129 MBEDTLS_SSL_DEBUG_MSG(1, ("new server cert during renegotiation"));
7130 mbedtls_ssl_send_alert_message(ssl,
7131 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7132 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED);
7133 return MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Jerry Yud9526692022-02-17 14:23:47 +08007134 }
7135
7136 /* Now we can safely free the original chain. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007137 ssl_clear_peer_cert(ssl->session);
Jerry Yud9526692022-02-17 14:23:47 +08007138 }
7139#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
7140
7141 /* Parse the next certificate in the chain. */
7142#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007143 ret = mbedtls_x509_crt_parse_der(chain, ssl->in_msg + i, n);
Jerry Yud9526692022-02-17 14:23:47 +08007144#else
7145 /* If we don't need to store the CRT chain permanently, parse
7146 * it in-place from the input buffer instead of making a copy. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007147 ret = mbedtls_x509_crt_parse_der_nocopy(chain, ssl->in_msg + i, n);
Jerry Yud9526692022-02-17 14:23:47 +08007148#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007149 switch (ret) {
Jerry Yud9526692022-02-17 14:23:47 +08007150 case 0: /*ok*/
7151 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
7152 /* Ignore certificate with an unknown algorithm: maybe a
7153 prior certificate was already trusted. */
7154 break;
7155
7156 case MBEDTLS_ERR_X509_ALLOC_FAILED:
7157 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
7158 goto crt_parse_der_failed;
7159
7160 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
7161 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7162 goto crt_parse_der_failed;
7163
7164 default:
7165 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007166crt_parse_der_failed:
7167 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert);
7168 MBEDTLS_SSL_DEBUG_RET(1, " mbedtls_x509_crt_parse_der", ret);
7169 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007170 }
7171
7172 i += n;
7173 }
7174
Gilles Peskine449bd832023-01-11 14:50:10 +01007175 MBEDTLS_SSL_DEBUG_CRT(3, "peer certificate", chain);
7176 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08007177}
7178
7179#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007180MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007181static int ssl_srv_check_client_no_crt_notification(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08007182{
Gilles Peskine449bd832023-01-11 14:50:10 +01007183 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
7184 return -1;
7185 }
Jerry Yud9526692022-02-17 14:23:47 +08007186
Gilles Peskine449bd832023-01-11 14:50:10 +01007187 if (ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len(ssl) &&
Jerry Yud9526692022-02-17 14:23:47 +08007188 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
7189 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
Gilles Peskine449bd832023-01-11 14:50:10 +01007190 memcmp(ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl), "\0\0\0", 3) == 0) {
7191 MBEDTLS_SSL_DEBUG_MSG(1, ("peer has no certificate"));
7192 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08007193 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007194 return -1;
Jerry Yud9526692022-02-17 14:23:47 +08007195}
7196#endif /* MBEDTLS_SSL_SRV_C */
7197
7198/* Check if a certificate message is expected.
7199 * Return either
7200 * - SSL_CERTIFICATE_EXPECTED, or
7201 * - SSL_CERTIFICATE_SKIP
7202 * indicating whether a Certificate message is expected or not.
7203 */
7204#define SSL_CERTIFICATE_EXPECTED 0
7205#define SSL_CERTIFICATE_SKIP 1
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007206MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007207static int ssl_parse_certificate_coordinate(mbedtls_ssl_context *ssl,
7208 int authmode)
Jerry Yud9526692022-02-17 14:23:47 +08007209{
7210 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
7211 ssl->handshake->ciphersuite_info;
7212
Gilles Peskine449bd832023-01-11 14:50:10 +01007213 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
7214 return SSL_CERTIFICATE_SKIP;
7215 }
Jerry Yud9526692022-02-17 14:23:47 +08007216
7217#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007218 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
7219 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK) {
7220 return SSL_CERTIFICATE_SKIP;
7221 }
Jerry Yud9526692022-02-17 14:23:47 +08007222
Gilles Peskine449bd832023-01-11 14:50:10 +01007223 if (authmode == MBEDTLS_SSL_VERIFY_NONE) {
Jerry Yud9526692022-02-17 14:23:47 +08007224 ssl->session_negotiate->verify_result =
7225 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
Gilles Peskine449bd832023-01-11 14:50:10 +01007226 return SSL_CERTIFICATE_SKIP;
Jerry Yud9526692022-02-17 14:23:47 +08007227 }
7228 }
7229#else
7230 ((void) authmode);
7231#endif /* MBEDTLS_SSL_SRV_C */
7232
Gilles Peskine449bd832023-01-11 14:50:10 +01007233 return SSL_CERTIFICATE_EXPECTED;
Jerry Yud9526692022-02-17 14:23:47 +08007234}
7235
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007236MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007237static int ssl_parse_certificate_verify(mbedtls_ssl_context *ssl,
7238 int authmode,
7239 mbedtls_x509_crt *chain,
7240 void *rs_ctx)
Jerry Yud9526692022-02-17 14:23:47 +08007241{
7242 int ret = 0;
7243 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
7244 ssl->handshake->ciphersuite_info;
7245 int have_ca_chain = 0;
7246
7247 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
7248 void *p_vrfy;
7249
Gilles Peskine449bd832023-01-11 14:50:10 +01007250 if (authmode == MBEDTLS_SSL_VERIFY_NONE) {
7251 return 0;
7252 }
Jerry Yud9526692022-02-17 14:23:47 +08007253
Gilles Peskine449bd832023-01-11 14:50:10 +01007254 if (ssl->f_vrfy != NULL) {
7255 MBEDTLS_SSL_DEBUG_MSG(3, ("Use context-specific verification callback"));
Jerry Yud9526692022-02-17 14:23:47 +08007256 f_vrfy = ssl->f_vrfy;
7257 p_vrfy = ssl->p_vrfy;
Gilles Peskine449bd832023-01-11 14:50:10 +01007258 } else {
7259 MBEDTLS_SSL_DEBUG_MSG(3, ("Use configuration-specific verification callback"));
Jerry Yud9526692022-02-17 14:23:47 +08007260 f_vrfy = ssl->conf->f_vrfy;
7261 p_vrfy = ssl->conf->p_vrfy;
7262 }
7263
7264 /*
7265 * Main check: verify certificate
7266 */
7267#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Gilles Peskine449bd832023-01-11 14:50:10 +01007268 if (ssl->conf->f_ca_cb != NULL) {
Jerry Yud9526692022-02-17 14:23:47 +08007269 ((void) rs_ctx);
7270 have_ca_chain = 1;
7271
Gilles Peskine449bd832023-01-11 14:50:10 +01007272 MBEDTLS_SSL_DEBUG_MSG(3, ("use CA callback for X.509 CRT verification"));
Jerry Yud9526692022-02-17 14:23:47 +08007273 ret = mbedtls_x509_crt_verify_with_ca_cb(
7274 chain,
7275 ssl->conf->f_ca_cb,
7276 ssl->conf->p_ca_cb,
7277 ssl->conf->cert_profile,
7278 ssl->hostname,
7279 &ssl->session_negotiate->verify_result,
Gilles Peskine449bd832023-01-11 14:50:10 +01007280 f_vrfy, p_vrfy);
7281 } else
Jerry Yud9526692022-02-17 14:23:47 +08007282#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
7283 {
7284 mbedtls_x509_crt *ca_chain;
7285 mbedtls_x509_crl *ca_crl;
7286
7287#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01007288 if (ssl->handshake->sni_ca_chain != NULL) {
Jerry Yud9526692022-02-17 14:23:47 +08007289 ca_chain = ssl->handshake->sni_ca_chain;
7290 ca_crl = ssl->handshake->sni_ca_crl;
Gilles Peskine449bd832023-01-11 14:50:10 +01007291 } else
Jerry Yud9526692022-02-17 14:23:47 +08007292#endif
7293 {
7294 ca_chain = ssl->conf->ca_chain;
7295 ca_crl = ssl->conf->ca_crl;
7296 }
7297
Gilles Peskine449bd832023-01-11 14:50:10 +01007298 if (ca_chain != NULL) {
Jerry Yud9526692022-02-17 14:23:47 +08007299 have_ca_chain = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01007300 }
Jerry Yud9526692022-02-17 14:23:47 +08007301
7302 ret = mbedtls_x509_crt_verify_restartable(
7303 chain,
7304 ca_chain, ca_crl,
7305 ssl->conf->cert_profile,
7306 ssl->hostname,
7307 &ssl->session_negotiate->verify_result,
Gilles Peskine449bd832023-01-11 14:50:10 +01007308 f_vrfy, p_vrfy, rs_ctx);
Jerry Yud9526692022-02-17 14:23:47 +08007309 }
7310
Gilles Peskine449bd832023-01-11 14:50:10 +01007311 if (ret != 0) {
7312 MBEDTLS_SSL_DEBUG_RET(1, "x509_verify_cert", ret);
Jerry Yud9526692022-02-17 14:23:47 +08007313 }
7314
7315#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007316 if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
7317 return MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
7318 }
Jerry Yud9526692022-02-17 14:23:47 +08007319#endif
7320
7321 /*
7322 * Secondary checks: always done, but change 'ret' only if it was 0
7323 */
7324
7325#if defined(MBEDTLS_ECP_C)
7326 {
7327 const mbedtls_pk_context *pk = &chain->pk;
7328
Manuel Pégourié-Gonnard66b0d612022-06-17 10:49:29 +02007329 /* If certificate uses an EC key, make sure the curve is OK.
7330 * This is a public key, so it can't be opaque, so can_do() is a good
7331 * enough check to ensure pk_ec() is safe to use here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007332 if (mbedtls_pk_can_do(pk, MBEDTLS_PK_ECKEY)) {
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007333 /* and in the unlikely case the above assumption no longer holds
7334 * we are making sure that pk_ec() here does not return a NULL
7335 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007336 const mbedtls_ecp_keypair *ec = mbedtls_pk_ec(*pk);
7337 if (ec == NULL) {
7338 MBEDTLS_SSL_DEBUG_MSG(1, ("mbedtls_pk_ec() returned NULL"));
7339 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007340 }
Jerry Yud9526692022-02-17 14:23:47 +08007341
Gilles Peskine449bd832023-01-11 14:50:10 +01007342 if (mbedtls_ssl_check_curve(ssl, ec->grp.id) != 0) {
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007343 ssl->session_negotiate->verify_result |=
7344 MBEDTLS_X509_BADCERT_BAD_KEY;
7345
Gilles Peskine449bd832023-01-11 14:50:10 +01007346 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate (EC key curve)"));
7347 if (ret == 0) {
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007348 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Gilles Peskine449bd832023-01-11 14:50:10 +01007349 }
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007350 }
Jerry Yud9526692022-02-17 14:23:47 +08007351 }
7352 }
7353#endif /* MBEDTLS_ECP_C */
7354
Gilles Peskine449bd832023-01-11 14:50:10 +01007355 if (mbedtls_ssl_check_cert_usage(chain,
7356 ciphersuite_info,
7357 !ssl->conf->endpoint,
7358 &ssl->session_negotiate->verify_result) != 0) {
7359 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate (usage extensions)"));
7360 if (ret == 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007361 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Gilles Peskine449bd832023-01-11 14:50:10 +01007362 }
Jerry Yud9526692022-02-17 14:23:47 +08007363 }
7364
7365 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
7366 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
7367 * with details encoded in the verification flags. All other kinds
7368 * of error codes, including those from the user provided f_vrfy
7369 * functions, are treated as fatal and lead to a failure of
7370 * ssl_parse_certificate even if verification was optional. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007371 if (authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
7372 (ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
7373 ret == MBEDTLS_ERR_SSL_BAD_CERTIFICATE)) {
Jerry Yud9526692022-02-17 14:23:47 +08007374 ret = 0;
7375 }
7376
Gilles Peskine449bd832023-01-11 14:50:10 +01007377 if (have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED) {
7378 MBEDTLS_SSL_DEBUG_MSG(1, ("got no CA chain"));
Jerry Yud9526692022-02-17 14:23:47 +08007379 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
7380 }
7381
Gilles Peskine449bd832023-01-11 14:50:10 +01007382 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007383 uint8_t alert;
7384
7385 /* The certificate may have been rejected for several reasons.
7386 Pick one and send the corresponding alert. Which alert to send
7387 may be a subject of debate in some cases. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007388 if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER) {
Jerry Yud9526692022-02-17 14:23:47 +08007389 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
Gilles Peskine449bd832023-01-11 14:50:10 +01007390 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH) {
Jerry Yud9526692022-02-17 14:23:47 +08007391 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007392 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE) {
Jerry Yud9526692022-02-17 14:23:47 +08007393 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007394 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE) {
Jerry Yud9526692022-02-17 14:23:47 +08007395 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007396 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE) {
Jerry Yud9526692022-02-17 14:23:47 +08007397 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007398 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK) {
Jerry Yud9526692022-02-17 14:23:47 +08007399 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007400 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY) {
Jerry Yud9526692022-02-17 14:23:47 +08007401 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007402 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED) {
Jerry Yud9526692022-02-17 14:23:47 +08007403 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
Gilles Peskine449bd832023-01-11 14:50:10 +01007404 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED) {
Jerry Yud9526692022-02-17 14:23:47 +08007405 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
Gilles Peskine449bd832023-01-11 14:50:10 +01007406 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED) {
Jerry Yud9526692022-02-17 14:23:47 +08007407 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
Gilles Peskine449bd832023-01-11 14:50:10 +01007408 } else {
Jerry Yud9526692022-02-17 14:23:47 +08007409 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
Gilles Peskine449bd832023-01-11 14:50:10 +01007410 }
7411 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7412 alert);
Jerry Yud9526692022-02-17 14:23:47 +08007413 }
7414
7415#if defined(MBEDTLS_DEBUG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007416 if (ssl->session_negotiate->verify_result != 0) {
7417 MBEDTLS_SSL_DEBUG_MSG(3, ("! Certificate verification flags %08x",
7418 (unsigned int) ssl->session_negotiate->verify_result));
7419 } else {
7420 MBEDTLS_SSL_DEBUG_MSG(3, ("Certificate verification flags clear"));
Jerry Yud9526692022-02-17 14:23:47 +08007421 }
7422#endif /* MBEDTLS_DEBUG_C */
7423
Gilles Peskine449bd832023-01-11 14:50:10 +01007424 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007425}
7426
7427#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007428MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007429static int ssl_remember_peer_crt_digest(mbedtls_ssl_context *ssl,
7430 unsigned char *start, size_t len)
Jerry Yud9526692022-02-17 14:23:47 +08007431{
7432 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7433 /* Remember digest of the peer's end-CRT. */
7434 ssl->session_negotiate->peer_cert_digest =
Gilles Peskine449bd832023-01-11 14:50:10 +01007435 mbedtls_calloc(1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN);
7436 if (ssl->session_negotiate->peer_cert_digest == NULL) {
7437 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%d bytes) failed",
7438 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN));
7439 mbedtls_ssl_send_alert_message(ssl,
7440 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7441 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
Jerry Yud9526692022-02-17 14:23:47 +08007442
Gilles Peskine449bd832023-01-11 14:50:10 +01007443 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Jerry Yud9526692022-02-17 14:23:47 +08007444 }
7445
Gilles Peskine449bd832023-01-11 14:50:10 +01007446 ret = mbedtls_md(mbedtls_md_info_from_type(
7447 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE),
7448 start, len,
7449 ssl->session_negotiate->peer_cert_digest);
Jerry Yud9526692022-02-17 14:23:47 +08007450
7451 ssl->session_negotiate->peer_cert_digest_type =
7452 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
7453 ssl->session_negotiate->peer_cert_digest_len =
7454 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
7455
Gilles Peskine449bd832023-01-11 14:50:10 +01007456 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007457}
7458
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007459MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007460static int ssl_remember_peer_pubkey(mbedtls_ssl_context *ssl,
7461 unsigned char *start, size_t len)
Jerry Yud9526692022-02-17 14:23:47 +08007462{
7463 unsigned char *end = start + len;
7464 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7465
7466 /* Make a copy of the peer's raw public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007467 mbedtls_pk_init(&ssl->handshake->peer_pubkey);
7468 ret = mbedtls_pk_parse_subpubkey(&start, end,
7469 &ssl->handshake->peer_pubkey);
7470 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007471 /* We should have parsed the public key before. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007472 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007473 }
7474
Gilles Peskine449bd832023-01-11 14:50:10 +01007475 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08007476}
7477#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7478
Gilles Peskine449bd832023-01-11 14:50:10 +01007479int mbedtls_ssl_parse_certificate(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08007480{
7481 int ret = 0;
7482 int crt_expected;
7483#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7484 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
7485 ? ssl->handshake->sni_authmode
7486 : ssl->conf->authmode;
7487#else
7488 const int authmode = ssl->conf->authmode;
7489#endif
7490 void *rs_ctx = NULL;
7491 mbedtls_x509_crt *chain = NULL;
7492
Gilles Peskine449bd832023-01-11 14:50:10 +01007493 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007494
Gilles Peskine449bd832023-01-11 14:50:10 +01007495 crt_expected = ssl_parse_certificate_coordinate(ssl, authmode);
7496 if (crt_expected == SSL_CERTIFICATE_SKIP) {
7497 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007498 goto exit;
7499 }
7500
7501#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007502 if (ssl->handshake->ecrs_enabled &&
7503 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify) {
Jerry Yud9526692022-02-17 14:23:47 +08007504 chain = ssl->handshake->ecrs_peer_cert;
7505 ssl->handshake->ecrs_peer_cert = NULL;
7506 goto crt_verify;
7507 }
7508#endif
7509
Gilles Peskine449bd832023-01-11 14:50:10 +01007510 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007511 /* mbedtls_ssl_read_record may have sent an alert already. We
7512 let it decide whether to alert. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007513 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
Jerry Yud9526692022-02-17 14:23:47 +08007514 goto exit;
7515 }
7516
7517#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007518 if (ssl_srv_check_client_no_crt_notification(ssl) == 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007519 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
7520
Gilles Peskine449bd832023-01-11 14:50:10 +01007521 if (authmode != MBEDTLS_SSL_VERIFY_OPTIONAL) {
Jerry Yud9526692022-02-17 14:23:47 +08007522 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Gilles Peskine449bd832023-01-11 14:50:10 +01007523 }
Jerry Yud9526692022-02-17 14:23:47 +08007524
7525 goto exit;
7526 }
7527#endif /* MBEDTLS_SSL_SRV_C */
7528
7529 /* Clear existing peer CRT structure in case we tried to
7530 * reuse a session but it failed, and allocate a new one. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007531 ssl_clear_peer_cert(ssl->session_negotiate);
Jerry Yud9526692022-02-17 14:23:47 +08007532
Gilles Peskine449bd832023-01-11 14:50:10 +01007533 chain = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
7534 if (chain == NULL) {
7535 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed",
7536 sizeof(mbedtls_x509_crt)));
7537 mbedtls_ssl_send_alert_message(ssl,
7538 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7539 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
Jerry Yud9526692022-02-17 14:23:47 +08007540
7541 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
7542 goto exit;
7543 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007544 mbedtls_x509_crt_init(chain);
Jerry Yud9526692022-02-17 14:23:47 +08007545
Gilles Peskine449bd832023-01-11 14:50:10 +01007546 ret = ssl_parse_certificate_chain(ssl, chain);
7547 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007548 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007549 }
Jerry Yud9526692022-02-17 14:23:47 +08007550
7551#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007552 if (ssl->handshake->ecrs_enabled) {
Jerry Yud9526692022-02-17 14:23:47 +08007553 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Gilles Peskine449bd832023-01-11 14:50:10 +01007554 }
Jerry Yud9526692022-02-17 14:23:47 +08007555
7556crt_verify:
Gilles Peskine449bd832023-01-11 14:50:10 +01007557 if (ssl->handshake->ecrs_enabled) {
Jerry Yud9526692022-02-17 14:23:47 +08007558 rs_ctx = &ssl->handshake->ecrs_ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +01007559 }
Jerry Yud9526692022-02-17 14:23:47 +08007560#endif
7561
Gilles Peskine449bd832023-01-11 14:50:10 +01007562 ret = ssl_parse_certificate_verify(ssl, authmode,
7563 chain, rs_ctx);
7564 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007565 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007566 }
Jerry Yud9526692022-02-17 14:23:47 +08007567
7568#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7569 {
7570 unsigned char *crt_start, *pk_start;
7571 size_t crt_len, pk_len;
7572
7573 /* We parse the CRT chain without copying, so
7574 * these pointers point into the input buffer,
7575 * and are hence still valid after freeing the
7576 * CRT chain. */
7577
7578 crt_start = chain->raw.p;
7579 crt_len = chain->raw.len;
7580
7581 pk_start = chain->pk_raw.p;
7582 pk_len = chain->pk_raw.len;
7583
7584 /* Free the CRT structures before computing
7585 * digest and copying the peer's public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007586 mbedtls_x509_crt_free(chain);
7587 mbedtls_free(chain);
Jerry Yud9526692022-02-17 14:23:47 +08007588 chain = NULL;
7589
Gilles Peskine449bd832023-01-11 14:50:10 +01007590 ret = ssl_remember_peer_crt_digest(ssl, crt_start, crt_len);
7591 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007592 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007593 }
Jerry Yud9526692022-02-17 14:23:47 +08007594
Gilles Peskine449bd832023-01-11 14:50:10 +01007595 ret = ssl_remember_peer_pubkey(ssl, pk_start, pk_len);
7596 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007597 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007598 }
Jerry Yud9526692022-02-17 14:23:47 +08007599 }
7600#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7601 /* Pass ownership to session structure. */
7602 ssl->session_negotiate->peer_cert = chain;
7603 chain = NULL;
7604#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7605
Gilles Peskine449bd832023-01-11 14:50:10 +01007606 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007607
7608exit:
7609
Gilles Peskine449bd832023-01-11 14:50:10 +01007610 if (ret == 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007611 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01007612 }
Jerry Yud9526692022-02-17 14:23:47 +08007613
7614#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007615 if (ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS) {
Jerry Yud9526692022-02-17 14:23:47 +08007616 ssl->handshake->ecrs_peer_cert = chain;
7617 chain = NULL;
7618 }
7619#endif
7620
Gilles Peskine449bd832023-01-11 14:50:10 +01007621 if (chain != NULL) {
7622 mbedtls_x509_crt_free(chain);
7623 mbedtls_free(chain);
Jerry Yud9526692022-02-17 14:23:47 +08007624 }
7625
Gilles Peskine449bd832023-01-11 14:50:10 +01007626 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007627}
7628#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
7629
Andrzej Kurek25f27152022-08-17 16:09:31 -04007630#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01007631static int ssl_calc_finished_tls_sha256(
Gilles Peskine449bd832023-01-11 14:50:10 +01007632 mbedtls_ssl_context *ssl, unsigned char *buf, int from)
Jerry Yu615bd6f2022-02-17 14:25:15 +08007633{
7634 int len = 12;
7635 const char *sender;
7636 unsigned char padbuf[32];
7637#if defined(MBEDTLS_USE_PSA_CRYPTO)
7638 size_t hash_size;
7639 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
7640 psa_status_t status;
7641#else
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007642 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu615bd6f2022-02-17 14:25:15 +08007643 mbedtls_sha256_context sha256;
7644#endif
7645
7646 mbedtls_ssl_session *session = ssl->session_negotiate;
Gilles Peskine449bd832023-01-11 14:50:10 +01007647 if (!session) {
Jerry Yu615bd6f2022-02-17 14:25:15 +08007648 session = ssl->session;
Gilles Peskine449bd832023-01-11 14:50:10 +01007649 }
Jerry Yu615bd6f2022-02-17 14:25:15 +08007650
Gilles Peskine449bd832023-01-11 14:50:10 +01007651 sender = (from == MBEDTLS_SSL_IS_CLIENT)
Jerry Yu615bd6f2022-02-17 14:25:15 +08007652 ? "client finished"
7653 : "server finished";
7654
7655#if defined(MBEDTLS_USE_PSA_CRYPTO)
7656 sha256_psa = psa_hash_operation_init();
7657
Gilles Peskine449bd832023-01-11 14:50:10 +01007658 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc PSA finished tls sha256"));
Jerry Yu615bd6f2022-02-17 14:25:15 +08007659
Gilles Peskine449bd832023-01-11 14:50:10 +01007660 status = psa_hash_clone(&ssl->handshake->fin_sha256_psa, &sha256_psa);
7661 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007662 goto exit;
Jerry Yu615bd6f2022-02-17 14:25:15 +08007663 }
7664
Gilles Peskine449bd832023-01-11 14:50:10 +01007665 status = psa_hash_finish(&sha256_psa, padbuf, sizeof(padbuf), &hash_size);
7666 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007667 goto exit;
Jerry Yu615bd6f2022-02-17 14:25:15 +08007668 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007669 MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated padbuf", padbuf, 32);
Jerry Yu615bd6f2022-02-17 14:25:15 +08007670#else
7671
Gilles Peskine449bd832023-01-11 14:50:10 +01007672 mbedtls_sha256_init(&sha256);
Jerry Yu615bd6f2022-02-17 14:25:15 +08007673
Gilles Peskine449bd832023-01-11 14:50:10 +01007674 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc finished tls sha256"));
Jerry Yu615bd6f2022-02-17 14:25:15 +08007675
Gilles Peskine449bd832023-01-11 14:50:10 +01007676 mbedtls_sha256_clone(&sha256, &ssl->handshake->fin_sha256);
Jerry Yu615bd6f2022-02-17 14:25:15 +08007677
7678 /*
7679 * TLSv1.2:
7680 * hash = PRF( master, finished_label,
7681 * Hash( handshake ) )[0.11]
7682 */
7683
7684#if !defined(MBEDTLS_SHA256_ALT)
Gilles Peskine449bd832023-01-11 14:50:10 +01007685 MBEDTLS_SSL_DEBUG_BUF(4, "finished sha2 state", (unsigned char *)
7686 sha256.state, sizeof(sha256.state));
Jerry Yu615bd6f2022-02-17 14:25:15 +08007687#endif
7688
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007689 ret = mbedtls_sha256_finish(&sha256, padbuf);
7690 if (ret != 0) {
7691 goto exit;
7692 }
Jerry Yu615bd6f2022-02-17 14:25:15 +08007693#endif /* MBEDTLS_USE_PSA_CRYPTO */
7694
Gilles Peskine449bd832023-01-11 14:50:10 +01007695 ssl->handshake->tls_prf(session->master, 48, sender,
7696 padbuf, 32, buf, len);
Jerry Yu615bd6f2022-02-17 14:25:15 +08007697
Gilles Peskine449bd832023-01-11 14:50:10 +01007698 MBEDTLS_SSL_DEBUG_BUF(3, "calc finished result", buf, len);
Jerry Yu615bd6f2022-02-17 14:25:15 +08007699
Gilles Peskine449bd832023-01-11 14:50:10 +01007700 mbedtls_platform_zeroize(padbuf, sizeof(padbuf));
Jerry Yu615bd6f2022-02-17 14:25:15 +08007701
Gilles Peskine449bd832023-01-11 14:50:10 +01007702 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc finished"));
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007703
7704exit:
7705#if defined(MBEDTLS_USE_PSA_CRYPTO)
7706 psa_hash_abort(&sha256_psa);
7707 return mbedtls_md_error_from_psa(status);
7708#else
7709 mbedtls_sha256_free(&sha256);
7710 return ret;
7711#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu615bd6f2022-02-17 14:25:15 +08007712}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04007713#endif /* MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yu615bd6f2022-02-17 14:25:15 +08007714
Jerry Yub7ba49e2022-02-17 14:25:53 +08007715
Andrzej Kurek25f27152022-08-17 16:09:31 -04007716#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01007717static int ssl_calc_finished_tls_sha384(
Gilles Peskine449bd832023-01-11 14:50:10 +01007718 mbedtls_ssl_context *ssl, unsigned char *buf, int from)
Jerry Yub7ba49e2022-02-17 14:25:53 +08007719{
7720 int len = 12;
7721 const char *sender;
7722 unsigned char padbuf[48];
7723#if defined(MBEDTLS_USE_PSA_CRYPTO)
7724 size_t hash_size;
7725 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
7726 psa_status_t status;
7727#else
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007728 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yub7ba49e2022-02-17 14:25:53 +08007729 mbedtls_sha512_context sha512;
7730#endif
7731
7732 mbedtls_ssl_session *session = ssl->session_negotiate;
Gilles Peskine449bd832023-01-11 14:50:10 +01007733 if (!session) {
Jerry Yub7ba49e2022-02-17 14:25:53 +08007734 session = ssl->session;
Gilles Peskine449bd832023-01-11 14:50:10 +01007735 }
Jerry Yub7ba49e2022-02-17 14:25:53 +08007736
Gilles Peskine449bd832023-01-11 14:50:10 +01007737 sender = (from == MBEDTLS_SSL_IS_CLIENT)
Jerry Yub7ba49e2022-02-17 14:25:53 +08007738 ? "client finished"
7739 : "server finished";
7740
7741#if defined(MBEDTLS_USE_PSA_CRYPTO)
7742 sha384_psa = psa_hash_operation_init();
7743
Gilles Peskine449bd832023-01-11 14:50:10 +01007744 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc PSA finished tls sha384"));
Jerry Yub7ba49e2022-02-17 14:25:53 +08007745
Gilles Peskine449bd832023-01-11 14:50:10 +01007746 status = psa_hash_clone(&ssl->handshake->fin_sha384_psa, &sha384_psa);
7747 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007748 goto exit;
Jerry Yub7ba49e2022-02-17 14:25:53 +08007749 }
7750
Gilles Peskine449bd832023-01-11 14:50:10 +01007751 status = psa_hash_finish(&sha384_psa, padbuf, sizeof(padbuf), &hash_size);
7752 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007753 goto exit;
Jerry Yub7ba49e2022-02-17 14:25:53 +08007754 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007755 MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated padbuf", padbuf, 48);
Jerry Yub7ba49e2022-02-17 14:25:53 +08007756#else
Gilles Peskine449bd832023-01-11 14:50:10 +01007757 mbedtls_sha512_init(&sha512);
Jerry Yub7ba49e2022-02-17 14:25:53 +08007758
Gilles Peskine449bd832023-01-11 14:50:10 +01007759 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc finished tls sha384"));
Jerry Yub7ba49e2022-02-17 14:25:53 +08007760
Gilles Peskine449bd832023-01-11 14:50:10 +01007761 mbedtls_sha512_clone(&sha512, &ssl->handshake->fin_sha384);
Jerry Yub7ba49e2022-02-17 14:25:53 +08007762
7763 /*
7764 * TLSv1.2:
7765 * hash = PRF( master, finished_label,
7766 * Hash( handshake ) )[0.11]
7767 */
7768
7769#if !defined(MBEDTLS_SHA512_ALT)
Gilles Peskine449bd832023-01-11 14:50:10 +01007770 MBEDTLS_SSL_DEBUG_BUF(4, "finished sha512 state", (unsigned char *)
7771 sha512.state, sizeof(sha512.state));
Jerry Yub7ba49e2022-02-17 14:25:53 +08007772#endif
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007773 ret = mbedtls_sha512_finish(&sha512, padbuf);
7774 if (ret != 0) {
7775 goto exit;
7776 }
Jerry Yub7ba49e2022-02-17 14:25:53 +08007777#endif
7778
Gilles Peskine449bd832023-01-11 14:50:10 +01007779 ssl->handshake->tls_prf(session->master, 48, sender,
7780 padbuf, 48, buf, len);
Jerry Yub7ba49e2022-02-17 14:25:53 +08007781
Gilles Peskine449bd832023-01-11 14:50:10 +01007782 MBEDTLS_SSL_DEBUG_BUF(3, "calc finished result", buf, len);
Jerry Yub7ba49e2022-02-17 14:25:53 +08007783
Gilles Peskine449bd832023-01-11 14:50:10 +01007784 mbedtls_platform_zeroize(padbuf, sizeof(padbuf));
Jerry Yub7ba49e2022-02-17 14:25:53 +08007785
Gilles Peskine449bd832023-01-11 14:50:10 +01007786 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc finished"));
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007787
7788exit:
7789#if defined(MBEDTLS_USE_PSA_CRYPTO)
7790 psa_hash_abort(&sha384_psa);
7791 return mbedtls_md_error_from_psa(status);
7792#else
7793 mbedtls_sha512_free(&sha512);
7794 return ret;
7795#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yub7ba49e2022-02-17 14:25:53 +08007796}
Andrzej Kurekcccb0442022-08-19 03:42:11 -04007797#endif /* MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA*/
Jerry Yub7ba49e2022-02-17 14:25:53 +08007798
Gilles Peskine449bd832023-01-11 14:50:10 +01007799void mbedtls_ssl_handshake_wrapup_free_hs_transform(mbedtls_ssl_context *ssl)
Jerry Yuaef00152022-02-17 14:27:31 +08007800{
Gilles Peskine449bd832023-01-11 14:50:10 +01007801 MBEDTLS_SSL_DEBUG_MSG(3, ("=> handshake wrapup: final free"));
Jerry Yuaef00152022-02-17 14:27:31 +08007802
7803 /*
7804 * Free our handshake params
7805 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007806 mbedtls_ssl_handshake_free(ssl);
7807 mbedtls_free(ssl->handshake);
Jerry Yuaef00152022-02-17 14:27:31 +08007808 ssl->handshake = NULL;
7809
7810 /*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08007811 * Free the previous transform and switch in the current one
Jerry Yuaef00152022-02-17 14:27:31 +08007812 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007813 if (ssl->transform) {
7814 mbedtls_ssl_transform_free(ssl->transform);
7815 mbedtls_free(ssl->transform);
Jerry Yuaef00152022-02-17 14:27:31 +08007816 }
7817 ssl->transform = ssl->transform_negotiate;
7818 ssl->transform_negotiate = NULL;
7819
Gilles Peskine449bd832023-01-11 14:50:10 +01007820 MBEDTLS_SSL_DEBUG_MSG(3, ("<= handshake wrapup: final free"));
Jerry Yuaef00152022-02-17 14:27:31 +08007821}
7822
Gilles Peskine449bd832023-01-11 14:50:10 +01007823void mbedtls_ssl_handshake_wrapup(mbedtls_ssl_context *ssl)
Jerry Yu2a9fff52022-02-17 14:28:51 +08007824{
7825 int resume = ssl->handshake->resume;
7826
Gilles Peskine449bd832023-01-11 14:50:10 +01007827 MBEDTLS_SSL_DEBUG_MSG(3, ("=> handshake wrapup"));
Jerry Yu2a9fff52022-02-17 14:28:51 +08007828
7829#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01007830 if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
Jerry Yu2a9fff52022-02-17 14:28:51 +08007831 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
7832 ssl->renego_records_seen = 0;
7833 }
7834#endif
7835
7836 /*
7837 * Free the previous session and switch in the current one
7838 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007839 if (ssl->session) {
Jerry Yu2a9fff52022-02-17 14:28:51 +08007840#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
7841 /* RFC 7366 3.1: keep the EtM state */
7842 ssl->session_negotiate->encrypt_then_mac =
Gilles Peskine449bd832023-01-11 14:50:10 +01007843 ssl->session->encrypt_then_mac;
Jerry Yu2a9fff52022-02-17 14:28:51 +08007844#endif
7845
Gilles Peskine449bd832023-01-11 14:50:10 +01007846 mbedtls_ssl_session_free(ssl->session);
7847 mbedtls_free(ssl->session);
Jerry Yu2a9fff52022-02-17 14:28:51 +08007848 }
7849 ssl->session = ssl->session_negotiate;
7850 ssl->session_negotiate = NULL;
7851
7852 /*
7853 * Add cache entry
7854 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007855 if (ssl->conf->f_set_cache != NULL &&
Jerry Yu2a9fff52022-02-17 14:28:51 +08007856 ssl->session->id_len != 0 &&
Gilles Peskine449bd832023-01-11 14:50:10 +01007857 resume == 0) {
7858 if (ssl->conf->f_set_cache(ssl->conf->p_cache,
7859 ssl->session->id,
7860 ssl->session->id_len,
7861 ssl->session) != 0) {
7862 MBEDTLS_SSL_DEBUG_MSG(1, ("cache did not store session"));
7863 }
Jerry Yu2a9fff52022-02-17 14:28:51 +08007864 }
7865
7866#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007867 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7868 ssl->handshake->flight != NULL) {
Jerry Yu2a9fff52022-02-17 14:28:51 +08007869 /* Cancel handshake timer */
Gilles Peskine449bd832023-01-11 14:50:10 +01007870 mbedtls_ssl_set_timer(ssl, 0);
Jerry Yu2a9fff52022-02-17 14:28:51 +08007871
7872 /* Keep last flight around in case we need to resend it:
7873 * we need the handshake and transform structures for that */
Gilles Peskine449bd832023-01-11 14:50:10 +01007874 MBEDTLS_SSL_DEBUG_MSG(3, ("skip freeing handshake and transform"));
7875 } else
Jerry Yu2a9fff52022-02-17 14:28:51 +08007876#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01007877 mbedtls_ssl_handshake_wrapup_free_hs_transform(ssl);
Jerry Yu2a9fff52022-02-17 14:28:51 +08007878
Jerry Yu5ed73ff2022-10-27 13:08:42 +08007879 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
Jerry Yu2a9fff52022-02-17 14:28:51 +08007880
Gilles Peskine449bd832023-01-11 14:50:10 +01007881 MBEDTLS_SSL_DEBUG_MSG(3, ("<= handshake wrapup"));
Jerry Yu2a9fff52022-02-17 14:28:51 +08007882}
7883
Gilles Peskine449bd832023-01-11 14:50:10 +01007884int mbedtls_ssl_write_finished(mbedtls_ssl_context *ssl)
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007885{
7886 int ret, hash_len;
7887
Gilles Peskine449bd832023-01-11 14:50:10 +01007888 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write finished"));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007889
Gilles Peskine449bd832023-01-11 14:50:10 +01007890 mbedtls_ssl_update_out_pointers(ssl, ssl->transform_negotiate);
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007891
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01007892 ret = ssl->handshake->calc_finished(ssl, ssl->out_msg + 4, ssl->conf->endpoint);
7893 if (ret != 0) {
7894 MBEDTLS_SSL_DEBUG_RET(1, "calc_finished", ret);
7895 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007896
7897 /*
7898 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7899 * may define some other value. Currently (early 2016), no defined
7900 * ciphersuite does this (and this is unlikely to change as activity has
7901 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7902 */
7903 hash_len = 12;
7904
7905#if defined(MBEDTLS_SSL_RENEGOTIATION)
7906 ssl->verify_data_len = hash_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01007907 memcpy(ssl->own_verify_data, ssl->out_msg + 4, hash_len);
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007908#endif
7909
7910 ssl->out_msglen = 4 + hash_len;
7911 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7912 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
7913
7914 /*
7915 * In case of session resuming, invert the client and server
7916 * ChangeCipherSpec messages order.
7917 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007918 if (ssl->handshake->resume != 0) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007919#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007920 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007921 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Gilles Peskine449bd832023-01-11 14:50:10 +01007922 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007923#endif
7924#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007925 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007926 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Gilles Peskine449bd832023-01-11 14:50:10 +01007927 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007928#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01007929 } else {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007930 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01007931 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007932
7933 /*
7934 * Switch to our negotiated transform and session parameters for outbound
7935 * data.
7936 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007937 MBEDTLS_SSL_DEBUG_MSG(3, ("switching to new transform spec for outbound data"));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007938
7939#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007940 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007941 unsigned char i;
7942
7943 /* Remember current epoch settings for resending */
7944 ssl->handshake->alt_transform_out = ssl->transform_out;
Gilles Peskine449bd832023-01-11 14:50:10 +01007945 memcpy(ssl->handshake->alt_out_ctr, ssl->cur_out_ctr,
7946 sizeof(ssl->handshake->alt_out_ctr));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007947
7948 /* Set sequence_number to zero */
Gilles Peskine449bd832023-01-11 14:50:10 +01007949 memset(&ssl->cur_out_ctr[2], 0, sizeof(ssl->cur_out_ctr) - 2);
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007950
7951
7952 /* Increment epoch */
Gilles Peskine449bd832023-01-11 14:50:10 +01007953 for (i = 2; i > 0; i--) {
7954 if (++ssl->cur_out_ctr[i - 1] != 0) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007955 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01007956 }
7957 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007958
7959 /* The loop goes to its end iff the counter is wrapping */
Gilles Peskine449bd832023-01-11 14:50:10 +01007960 if (i == 0) {
7961 MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS epoch would wrap"));
7962 return MBEDTLS_ERR_SSL_COUNTER_WRAPPING;
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007963 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007964 } else
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007965#endif /* MBEDTLS_SSL_PROTO_DTLS */
Gilles Peskine449bd832023-01-11 14:50:10 +01007966 memset(ssl->cur_out_ctr, 0, sizeof(ssl->cur_out_ctr));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007967
7968 ssl->transform_out = ssl->transform_negotiate;
7969 ssl->session_out = ssl->session_negotiate;
7970
7971#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007972 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
7973 mbedtls_ssl_send_flight_completed(ssl);
7974 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007975#endif
7976
Gilles Peskine449bd832023-01-11 14:50:10 +01007977 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
7978 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
7979 return ret;
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007980 }
7981
7982#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007983 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7984 (ret = mbedtls_ssl_flight_transmit(ssl)) != 0) {
7985 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_flight_transmit", ret);
7986 return ret;
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007987 }
7988#endif
7989
Gilles Peskine449bd832023-01-11 14:50:10 +01007990 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write finished"));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007991
Gilles Peskine449bd832023-01-11 14:50:10 +01007992 return 0;
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007993}
7994
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007995#define SSL_MAX_HASH_LEN 12
7996
Gilles Peskine449bd832023-01-11 14:50:10 +01007997int mbedtls_ssl_parse_finished(mbedtls_ssl_context *ssl)
Jerry Yu0b3d7c12022-02-17 14:30:51 +08007998{
7999 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
8000 unsigned int hash_len = 12;
8001 unsigned char buf[SSL_MAX_HASH_LEN];
8002
Gilles Peskine449bd832023-01-11 14:50:10 +01008003 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse finished"));
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008004
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01008005 ret = ssl->handshake->calc_finished(ssl, buf, ssl->conf->endpoint ^ 1);
8006 if (ret != 0) {
8007 MBEDTLS_SSL_DEBUG_RET(1, "calc_finished", ret);
8008 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008009
Gilles Peskine449bd832023-01-11 14:50:10 +01008010 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
8011 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008012 goto exit;
8013 }
8014
Gilles Peskine449bd832023-01-11 14:50:10 +01008015 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
8016 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
8017 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8018 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008019 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
8020 goto exit;
8021 }
8022
Gilles Peskine449bd832023-01-11 14:50:10 +01008023 if (ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED) {
8024 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8025 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008026 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
8027 goto exit;
8028 }
8029
Gilles Peskine449bd832023-01-11 14:50:10 +01008030 if (ssl->in_hslen != mbedtls_ssl_hs_hdr_len(ssl) + hash_len) {
8031 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
8032 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8033 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008034 ret = MBEDTLS_ERR_SSL_DECODE_ERROR;
8035 goto exit;
8036 }
8037
Gilles Peskine449bd832023-01-11 14:50:10 +01008038 if (mbedtls_ct_memcmp(ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl),
8039 buf, hash_len) != 0) {
8040 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
8041 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8042 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008043 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
8044 goto exit;
8045 }
8046
8047#if defined(MBEDTLS_SSL_RENEGOTIATION)
8048 ssl->verify_data_len = hash_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008049 memcpy(ssl->peer_verify_data, buf, hash_len);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008050#endif
8051
Gilles Peskine449bd832023-01-11 14:50:10 +01008052 if (ssl->handshake->resume != 0) {
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008053#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008054 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008055 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Gilles Peskine449bd832023-01-11 14:50:10 +01008056 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008057#endif
8058#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008059 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008060 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Gilles Peskine449bd832023-01-11 14:50:10 +01008061 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008062#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01008063 } else {
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008064 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01008065 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008066
8067#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01008068 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
8069 mbedtls_ssl_recv_flight_completed(ssl);
8070 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008071#endif
8072
Gilles Peskine449bd832023-01-11 14:50:10 +01008073 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse finished"));
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008074
8075exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008076 mbedtls_platform_zeroize(buf, hash_len);
8077 return ret;
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008078}
8079
Jerry Yu392112c2022-02-17 14:34:10 +08008080#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
8081/*
8082 * Helper to get TLS 1.2 PRF from ciphersuite
8083 * (Duplicates bits of logic from ssl_set_handshake_prfs().)
8084 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008085static tls_prf_fn ssl_tls12prf_from_cs(int ciphersuite_id)
Jerry Yu392112c2022-02-17 14:34:10 +08008086{
Jerry Yu392112c2022-02-17 14:34:10 +08008087 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01008088 mbedtls_ssl_ciphersuite_from_id(ciphersuite_id);
Andrzej Kurek68327742022-10-03 06:18:18 -04008089#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01008090 if (ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA384) {
8091 return tls_prf_sha384;
8092 } else
Jerry Yu392112c2022-02-17 14:34:10 +08008093#endif
Andrzej Kurek894edde2022-09-29 06:31:14 -04008094#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
8095 {
Gilles Peskine449bd832023-01-11 14:50:10 +01008096 if (ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA256) {
8097 return tls_prf_sha256;
8098 }
Andrzej Kurek894edde2022-09-29 06:31:14 -04008099 }
8100#endif
8101#if !defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA) && \
8102 !defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
8103 (void) ciphersuite_info;
8104#endif
Andrzej Kurekeabeb302022-10-17 07:52:51 -04008105
Gilles Peskine449bd832023-01-11 14:50:10 +01008106 return NULL;
Jerry Yu392112c2022-02-17 14:34:10 +08008107}
8108#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Jerry Yue93ffcd2022-02-17 14:37:06 +08008109
Gilles Peskine449bd832023-01-11 14:50:10 +01008110static mbedtls_tls_prf_types tls_prf_get_type(mbedtls_ssl_tls_prf_cb *tls_prf)
Jerry Yue93ffcd2022-02-17 14:37:06 +08008111{
8112 ((void) tls_prf);
Andrzej Kurek25f27152022-08-17 16:09:31 -04008113#if defined(MBEDTLS_HAS_ALG_SHA_384_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01008114 if (tls_prf == tls_prf_sha384) {
8115 return MBEDTLS_SSL_TLS_PRF_SHA384;
8116 } else
Jerry Yue93ffcd2022-02-17 14:37:06 +08008117#endif
Andrzej Kurek25f27152022-08-17 16:09:31 -04008118#if defined(MBEDTLS_HAS_ALG_SHA_256_VIA_MD_OR_PSA_BASED_ON_USE_PSA)
Gilles Peskine449bd832023-01-11 14:50:10 +01008119 if (tls_prf == tls_prf_sha256) {
8120 return MBEDTLS_SSL_TLS_PRF_SHA256;
8121 } else
Jerry Yue93ffcd2022-02-17 14:37:06 +08008122#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01008123 return MBEDTLS_SSL_TLS_PRF_NONE;
Jerry Yue93ffcd2022-02-17 14:37:06 +08008124}
8125
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008126/*
8127 * Populate a transform structure with session keys and all the other
8128 * necessary information.
8129 *
8130 * Parameters:
8131 * - [in/out]: transform: structure to populate
8132 * [in] must be just initialised with mbedtls_ssl_transform_init()
8133 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
8134 * - [in] ciphersuite
8135 * - [in] master
8136 * - [in] encrypt_then_mac
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008137 * - [in] tls_prf: pointer to PRF to use for key derivation
8138 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
Glenn Strauss07c64162022-03-14 12:34:51 -04008139 * - [in] tls_version: TLS version
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008140 * - [in] endpoint: client or server
8141 * - [in] ssl: used for:
8142 * - ssl->conf->{f,p}_export_keys
8143 * [in] optionally used for:
8144 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
8145 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02008146MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01008147static int ssl_tls12_populate_transform(mbedtls_ssl_transform *transform,
8148 int ciphersuite,
8149 const unsigned char master[48],
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008150#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01008151 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008152#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01008153 ssl_tls_prf_t tls_prf,
8154 const unsigned char randbytes[64],
8155 mbedtls_ssl_protocol_version tls_version,
8156 unsigned endpoint,
8157 const mbedtls_ssl_context *ssl)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008158{
8159 int ret = 0;
8160 unsigned char keyblk[256];
8161 unsigned char *key1;
8162 unsigned char *key2;
8163 unsigned char *mac_enc;
8164 unsigned char *mac_dec;
8165 size_t mac_key_len = 0;
8166 size_t iv_copy_len;
8167 size_t keylen;
8168 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008169 mbedtls_ssl_mode_t ssl_mode;
Neil Armstronge4512952022-03-08 09:08:22 +01008170#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008171 const mbedtls_cipher_info_t *cipher_info;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008172 const mbedtls_md_info_t *md_info;
Neil Armstronge4512952022-03-08 09:08:22 +01008173#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008174
8175#if defined(MBEDTLS_USE_PSA_CRYPTO)
8176 psa_key_type_t key_type;
8177 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8178 psa_algorithm_t alg;
Neil Armstronge4512952022-03-08 09:08:22 +01008179 psa_algorithm_t mac_alg = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008180 size_t key_bits;
8181 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
8182#endif
8183
8184#if !defined(MBEDTLS_DEBUG_C) && \
8185 !defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Gilles Peskine449bd832023-01-11 14:50:10 +01008186 if (ssl->f_export_keys == NULL) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008187 ssl = NULL; /* make sure we don't use it except for these cases */
8188 (void) ssl;
8189 }
8190#endif
8191
8192 /*
8193 * Some data just needs copying into the structure
8194 */
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008195#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008196 transform->encrypt_then_mac = encrypt_then_mac;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008197#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Glenn Strauss07c64162022-03-14 12:34:51 -04008198 transform->tls_version = tls_version;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008199
8200#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01008201 memcpy(transform->randbytes, randbytes, sizeof(transform->randbytes));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008202#endif
8203
8204#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01008205 if (tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008206 /* At the moment, we keep TLS <= 1.2 and TLS 1.3 transform
8207 * generation separate. This should never happen. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008208 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008209 }
8210#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
8211
8212 /*
8213 * Get various info structures
8214 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008215 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(ciphersuite);
8216 if (ciphersuite_info == NULL) {
8217 MBEDTLS_SSL_DEBUG_MSG(1, ("ciphersuite info for %d not found",
8218 ciphersuite));
8219 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008220 }
8221
Neil Armstrongab555e02022-04-04 11:07:59 +02008222 ssl_mode = mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008223#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01008224 encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008225#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01008226 ciphersuite_info);
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008227
Gilles Peskine449bd832023-01-11 14:50:10 +01008228 if (ssl_mode == MBEDTLS_SSL_MODE_AEAD) {
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008229 transform->taglen =
8230 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Gilles Peskine449bd832023-01-11 14:50:10 +01008231 }
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008232
8233#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008234 if ((status = mbedtls_ssl_cipher_to_psa(ciphersuite_info->cipher,
8235 transform->taglen,
8236 &alg,
8237 &key_type,
8238 &key_bits)) != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05008239 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01008240 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_cipher_to_psa", ret);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008241 goto end;
8242 }
8243#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008244 cipher_info = mbedtls_cipher_info_from_type(ciphersuite_info->cipher);
8245 if (cipher_info == NULL) {
8246 MBEDTLS_SSL_DEBUG_MSG(1, ("cipher info for %u not found",
8247 ciphersuite_info->cipher));
8248 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008249 }
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008250#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008251
Neil Armstronge4512952022-03-08 09:08:22 +01008252#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008253 mac_alg = mbedtls_hash_info_psa_from_md(ciphersuite_info->mac);
8254 if (mac_alg == 0) {
8255 MBEDTLS_SSL_DEBUG_MSG(1, ("mbedtls_hash_info_psa_from_md for %u not found",
8256 (unsigned) ciphersuite_info->mac));
8257 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Neil Armstronge4512952022-03-08 09:08:22 +01008258 }
8259#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008260 md_info = mbedtls_md_info_from_type(ciphersuite_info->mac);
8261 if (md_info == NULL) {
8262 MBEDTLS_SSL_DEBUG_MSG(1, ("mbedtls_md info for %u not found",
8263 (unsigned) ciphersuite_info->mac));
8264 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008265 }
Neil Armstronge4512952022-03-08 09:08:22 +01008266#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008267
8268#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
8269 /* Copy own and peer's CID if the use of the CID
8270 * extension has been negotiated. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008271 if (ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED) {
8272 MBEDTLS_SSL_DEBUG_MSG(3, ("Copy CIDs into SSL transform"));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008273
8274 transform->in_cid_len = ssl->own_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008275 memcpy(transform->in_cid, ssl->own_cid, ssl->own_cid_len);
8276 MBEDTLS_SSL_DEBUG_BUF(3, "Incoming CID", transform->in_cid,
8277 transform->in_cid_len);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008278
8279 transform->out_cid_len = ssl->handshake->peer_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008280 memcpy(transform->out_cid, ssl->handshake->peer_cid,
8281 ssl->handshake->peer_cid_len);
8282 MBEDTLS_SSL_DEBUG_BUF(3, "Outgoing CID", transform->out_cid,
8283 transform->out_cid_len);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008284 }
8285#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
8286
8287 /*
8288 * Compute key block using the PRF
8289 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008290 ret = tls_prf(master, 48, "key expansion", randbytes, 64, keyblk, 256);
8291 if (ret != 0) {
8292 MBEDTLS_SSL_DEBUG_RET(1, "prf", ret);
8293 return ret;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008294 }
8295
Gilles Peskine449bd832023-01-11 14:50:10 +01008296 MBEDTLS_SSL_DEBUG_MSG(3, ("ciphersuite = %s",
8297 mbedtls_ssl_get_ciphersuite_name(ciphersuite)));
8298 MBEDTLS_SSL_DEBUG_BUF(3, "master secret", master, 48);
8299 MBEDTLS_SSL_DEBUG_BUF(4, "random bytes", randbytes, 64);
8300 MBEDTLS_SSL_DEBUG_BUF(4, "key block", keyblk, 256);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008301
8302 /*
8303 * Determine the appropriate key, IV and MAC length.
8304 */
8305
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008306#if defined(MBEDTLS_USE_PSA_CRYPTO)
8307 keylen = PSA_BITS_TO_BYTES(key_bits);
8308#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008309 keylen = mbedtls_cipher_info_get_key_bitlen(cipher_info) / 8;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008310#endif
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008311
8312#if defined(MBEDTLS_GCM_C) || \
8313 defined(MBEDTLS_CCM_C) || \
8314 defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008315 if (ssl_mode == MBEDTLS_SSL_MODE_AEAD) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008316 size_t explicit_ivlen;
8317
8318 transform->maclen = 0;
8319 mac_key_len = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008320
8321 /* All modes haves 96-bit IVs, but the length of the static parts vary
8322 * with mode and version:
8323 * - For GCM and CCM in TLS 1.2, there's a static IV of 4 Bytes
8324 * (to be concatenated with a dynamically chosen IV of 8 Bytes)
8325 * - For ChaChaPoly in TLS 1.2, and all modes in TLS 1.3, there's
8326 * a static IV of 12 Bytes (to be XOR'ed with the 8 Byte record
8327 * sequence number).
8328 */
8329 transform->ivlen = 12;
David Horstmann3b2276a2022-10-06 14:49:08 +01008330
8331 int is_chachapoly = 0;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008332#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008333 is_chachapoly = (key_type == PSA_KEY_TYPE_CHACHA20);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008334#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008335 is_chachapoly = (mbedtls_cipher_info_get_mode(cipher_info)
8336 == MBEDTLS_MODE_CHACHAPOLY);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008337#endif /* MBEDTLS_USE_PSA_CRYPTO */
David Horstmann3b2276a2022-10-06 14:49:08 +01008338
Gilles Peskine449bd832023-01-11 14:50:10 +01008339 if (is_chachapoly) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008340 transform->fixed_ivlen = 12;
Gilles Peskine449bd832023-01-11 14:50:10 +01008341 } else {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008342 transform->fixed_ivlen = 4;
Gilles Peskine449bd832023-01-11 14:50:10 +01008343 }
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008344
8345 /* Minimum length of encrypted record */
8346 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
8347 transform->minlen = explicit_ivlen + transform->taglen;
Gilles Peskine449bd832023-01-11 14:50:10 +01008348 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008349#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
8350#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01008351 if (ssl_mode == MBEDTLS_SSL_MODE_STREAM ||
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008352 ssl_mode == MBEDTLS_SSL_MODE_CBC ||
Gilles Peskine449bd832023-01-11 14:50:10 +01008353 ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM) {
Neil Armstronge4512952022-03-08 09:08:22 +01008354#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008355 size_t block_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008356#else
8357 size_t block_size = cipher_info->block_size;
8358#endif /* MBEDTLS_USE_PSA_CRYPTO */
8359
8360#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronge4512952022-03-08 09:08:22 +01008361 /* Get MAC length */
8362 mac_key_len = PSA_HASH_LENGTH(mac_alg);
8363#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008364 /* Initialize HMAC contexts */
Gilles Peskine449bd832023-01-11 14:50:10 +01008365 if ((ret = mbedtls_md_setup(&transform->md_ctx_enc, md_info, 1)) != 0 ||
8366 (ret = mbedtls_md_setup(&transform->md_ctx_dec, md_info, 1)) != 0) {
8367 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_setup", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008368 goto end;
8369 }
8370
8371 /* Get MAC length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008372 mac_key_len = mbedtls_md_get_size(md_info);
Neil Armstronge4512952022-03-08 09:08:22 +01008373#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008374 transform->maclen = mac_key_len;
8375
8376 /* IV length */
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008377#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008378 transform->ivlen = PSA_CIPHER_IV_LENGTH(key_type, alg);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008379#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008380 transform->ivlen = cipher_info->iv_size;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008381#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008382
8383 /* Minimum length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008384 if (ssl_mode == MBEDTLS_SSL_MODE_STREAM) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008385 transform->minlen = transform->maclen;
Gilles Peskine449bd832023-01-11 14:50:10 +01008386 } else {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008387 /*
8388 * GenericBlockCipher:
8389 * 1. if EtM is in use: one block plus MAC
8390 * otherwise: * first multiple of blocklen greater than maclen
8391 * 2. IV
8392 */
8393#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01008394 if (ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008395 transform->minlen = transform->maclen
Gilles Peskine449bd832023-01-11 14:50:10 +01008396 + block_size;
8397 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008398#endif
8399 {
8400 transform->minlen = transform->maclen
Gilles Peskine449bd832023-01-11 14:50:10 +01008401 + block_size
8402 - transform->maclen % block_size;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008403 }
8404
Gilles Peskine449bd832023-01-11 14:50:10 +01008405 if (tls_version == MBEDTLS_SSL_VERSION_TLS1_2) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008406 transform->minlen += transform->ivlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01008407 } else {
8408 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008409 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
8410 goto end;
8411 }
8412 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008413 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008414#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
8415 {
Gilles Peskine449bd832023-01-11 14:50:10 +01008416 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
8417 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008418 }
8419
Gilles Peskine449bd832023-01-11 14:50:10 +01008420 MBEDTLS_SSL_DEBUG_MSG(3, ("keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
8421 (unsigned) keylen,
8422 (unsigned) transform->minlen,
8423 (unsigned) transform->ivlen,
8424 (unsigned) transform->maclen));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008425
8426 /*
8427 * Finally setup the cipher contexts, IVs and MAC secrets.
8428 */
8429#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008430 if (endpoint == MBEDTLS_SSL_IS_CLIENT) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008431 key1 = keyblk + mac_key_len * 2;
8432 key2 = keyblk + mac_key_len * 2 + keylen;
8433
8434 mac_enc = keyblk;
8435 mac_dec = keyblk + mac_key_len;
8436
Gilles Peskine449bd832023-01-11 14:50:10 +01008437 iv_copy_len = (transform->fixed_ivlen) ?
8438 transform->fixed_ivlen : transform->ivlen;
8439 memcpy(transform->iv_enc, key2 + keylen, iv_copy_len);
8440 memcpy(transform->iv_dec, key2 + keylen + iv_copy_len,
8441 iv_copy_len);
8442 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008443#endif /* MBEDTLS_SSL_CLI_C */
8444#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008445 if (endpoint == MBEDTLS_SSL_IS_SERVER) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008446 key1 = keyblk + mac_key_len * 2 + keylen;
8447 key2 = keyblk + mac_key_len * 2;
8448
8449 mac_enc = keyblk + mac_key_len;
8450 mac_dec = keyblk;
8451
Gilles Peskine449bd832023-01-11 14:50:10 +01008452 iv_copy_len = (transform->fixed_ivlen) ?
8453 transform->fixed_ivlen : transform->ivlen;
8454 memcpy(transform->iv_dec, key1 + keylen, iv_copy_len);
8455 memcpy(transform->iv_enc, key1 + keylen + iv_copy_len,
8456 iv_copy_len);
8457 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008458#endif /* MBEDTLS_SSL_SRV_C */
8459 {
Gilles Peskine449bd832023-01-11 14:50:10 +01008460 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008461 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
8462 goto end;
8463 }
8464
Gilles Peskine449bd832023-01-11 14:50:10 +01008465 if (ssl != NULL && ssl->f_export_keys != NULL) {
8466 ssl->f_export_keys(ssl->p_export_keys,
8467 MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET,
8468 master, 48,
8469 randbytes + 32,
8470 randbytes,
8471 tls_prf_get_type(tls_prf));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008472 }
8473
8474#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008475 transform->psa_alg = alg;
8476
Gilles Peskine449bd832023-01-11 14:50:10 +01008477 if (alg != MBEDTLS_SSL_NULL_CIPHER) {
8478 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
8479 psa_set_key_algorithm(&attributes, alg);
8480 psa_set_key_type(&attributes, key_type);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008481
Gilles Peskine449bd832023-01-11 14:50:10 +01008482 if ((status = psa_import_key(&attributes,
8483 key1,
8484 PSA_BITS_TO_BYTES(key_bits),
8485 &transform->psa_key_enc)) != PSA_SUCCESS) {
8486 MBEDTLS_SSL_DEBUG_RET(3, "psa_import_key", (int) status);
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05008487 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01008488 MBEDTLS_SSL_DEBUG_RET(1, "psa_import_key", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008489 goto end;
8490 }
8491
Gilles Peskine449bd832023-01-11 14:50:10 +01008492 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008493
Gilles Peskine449bd832023-01-11 14:50:10 +01008494 if ((status = psa_import_key(&attributes,
8495 key2,
8496 PSA_BITS_TO_BYTES(key_bits),
8497 &transform->psa_key_dec)) != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05008498 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01008499 MBEDTLS_SSL_DEBUG_RET(1, "psa_import_key", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008500 goto end;
8501 }
8502 }
8503#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008504 if ((ret = mbedtls_cipher_setup(&transform->cipher_ctx_enc,
8505 cipher_info)) != 0) {
8506 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008507 goto end;
8508 }
8509
Gilles Peskine449bd832023-01-11 14:50:10 +01008510 if ((ret = mbedtls_cipher_setup(&transform->cipher_ctx_dec,
8511 cipher_info)) != 0) {
8512 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008513 goto end;
8514 }
8515
Gilles Peskine449bd832023-01-11 14:50:10 +01008516 if ((ret = mbedtls_cipher_setkey(&transform->cipher_ctx_enc, key1,
8517 (int) mbedtls_cipher_info_get_key_bitlen(cipher_info),
8518 MBEDTLS_ENCRYPT)) != 0) {
8519 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setkey", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008520 goto end;
8521 }
8522
Gilles Peskine449bd832023-01-11 14:50:10 +01008523 if ((ret = mbedtls_cipher_setkey(&transform->cipher_ctx_dec, key2,
8524 (int) mbedtls_cipher_info_get_key_bitlen(cipher_info),
8525 MBEDTLS_DECRYPT)) != 0) {
8526 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setkey", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008527 goto end;
8528 }
8529
8530#if defined(MBEDTLS_CIPHER_MODE_CBC)
Gilles Peskine449bd832023-01-11 14:50:10 +01008531 if (mbedtls_cipher_info_get_mode(cipher_info) == MBEDTLS_MODE_CBC) {
8532 if ((ret = mbedtls_cipher_set_padding_mode(&transform->cipher_ctx_enc,
8533 MBEDTLS_PADDING_NONE)) != 0) {
8534 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_set_padding_mode", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008535 goto end;
8536 }
8537
Gilles Peskine449bd832023-01-11 14:50:10 +01008538 if ((ret = mbedtls_cipher_set_padding_mode(&transform->cipher_ctx_dec,
8539 MBEDTLS_PADDING_NONE)) != 0) {
8540 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_set_padding_mode", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008541 goto end;
8542 }
8543 }
8544#endif /* MBEDTLS_CIPHER_MODE_CBC */
8545#endif /* MBEDTLS_USE_PSA_CRYPTO */
8546
Neil Armstrong29c0c042022-03-17 17:47:28 +01008547#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
8548 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
8549 For AEAD-based ciphersuites, there is nothing to do here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008550 if (mac_key_len != 0) {
Neil Armstrong29c0c042022-03-17 17:47:28 +01008551#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008552 transform->psa_mac_alg = PSA_ALG_HMAC(mac_alg);
Neil Armstrong29c0c042022-03-17 17:47:28 +01008553
Gilles Peskine449bd832023-01-11 14:50:10 +01008554 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
8555 psa_set_key_algorithm(&attributes, PSA_ALG_HMAC(mac_alg));
8556 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
Neil Armstrong29c0c042022-03-17 17:47:28 +01008557
Gilles Peskine449bd832023-01-11 14:50:10 +01008558 if ((status = psa_import_key(&attributes,
8559 mac_enc, mac_key_len,
8560 &transform->psa_mac_enc)) != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05008561 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01008562 MBEDTLS_SSL_DEBUG_RET(1, "psa_import_mac_key", ret);
Neil Armstrong29c0c042022-03-17 17:47:28 +01008563 goto end;
8564 }
8565
Gilles Peskine449bd832023-01-11 14:50:10 +01008566 if ((transform->psa_alg == MBEDTLS_SSL_NULL_CIPHER) ||
8567 ((transform->psa_alg == PSA_ALG_CBC_NO_PADDING)
Andrzej Kurek8c95ac42022-08-17 16:17:00 -04008568#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01008569 && (transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED)
Andrzej Kurek8c95ac42022-08-17 16:17:00 -04008570#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01008571 )) {
Neil Armstrong29c0c042022-03-17 17:47:28 +01008572 /* mbedtls_ct_hmac() requires the key to be exportable */
Gilles Peskine449bd832023-01-11 14:50:10 +01008573 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT |
8574 PSA_KEY_USAGE_VERIFY_HASH);
8575 } else {
8576 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
8577 }
Neil Armstrong29c0c042022-03-17 17:47:28 +01008578
Gilles Peskine449bd832023-01-11 14:50:10 +01008579 if ((status = psa_import_key(&attributes,
8580 mac_dec, mac_key_len,
8581 &transform->psa_mac_dec)) != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05008582 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01008583 MBEDTLS_SSL_DEBUG_RET(1, "psa_import_mac_key", ret);
Neil Armstrong29c0c042022-03-17 17:47:28 +01008584 goto end;
8585 }
8586#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008587 ret = mbedtls_md_hmac_starts(&transform->md_ctx_enc, mac_enc, mac_key_len);
8588 if (ret != 0) {
Neil Armstrong29c0c042022-03-17 17:47:28 +01008589 goto end;
Gilles Peskine449bd832023-01-11 14:50:10 +01008590 }
8591 ret = mbedtls_md_hmac_starts(&transform->md_ctx_dec, mac_dec, mac_key_len);
8592 if (ret != 0) {
Neil Armstrong29c0c042022-03-17 17:47:28 +01008593 goto end;
Gilles Peskine449bd832023-01-11 14:50:10 +01008594 }
Neil Armstrong29c0c042022-03-17 17:47:28 +01008595#endif /* MBEDTLS_USE_PSA_CRYPTO */
8596 }
8597#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
8598
8599 ((void) mac_dec);
8600 ((void) mac_enc);
8601
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008602end:
Gilles Peskine449bd832023-01-11 14:50:10 +01008603 mbedtls_platform_zeroize(keyblk, sizeof(keyblk));
8604 return ret;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008605}
8606
Valerio Settia08b1a42022-11-17 15:10:02 +01008607#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) && \
8608 defined(MBEDTLS_USE_PSA_CRYPTO)
Valerio Setti6b3dab02022-11-17 17:14:54 +01008609int mbedtls_psa_ecjpake_read_round(
Gilles Peskine449bd832023-01-11 14:50:10 +01008610 psa_pake_operation_t *pake_ctx,
8611 const unsigned char *buf,
8612 size_t len, mbedtls_ecjpake_rounds_t round)
Valerio Settia08b1a42022-11-17 15:10:02 +01008613{
8614 psa_status_t status;
8615 size_t input_offset = 0;
Valerio Setti819de862022-11-17 18:05:19 +01008616 /*
Valerio Setti6b3dab02022-11-17 17:14:54 +01008617 * At round one repeat the KEY_SHARE, ZK_PUBLIC & ZF_PROOF twice
8618 * At round two perform a single cycle
8619 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008620 unsigned int remaining_steps = (round == MBEDTLS_ECJPAKE_ROUND_ONE) ? 2 : 1;
Valerio Settia08b1a42022-11-17 15:10:02 +01008621
Gilles Peskine449bd832023-01-11 14:50:10 +01008622 for (; remaining_steps > 0; remaining_steps--) {
8623 for (psa_pake_step_t step = PSA_PAKE_STEP_KEY_SHARE;
Valerio Settia08b1a42022-11-17 15:10:02 +01008624 step <= PSA_PAKE_STEP_ZK_PROOF;
Gilles Peskine449bd832023-01-11 14:50:10 +01008625 ++step) {
Valerio Settia08b1a42022-11-17 15:10:02 +01008626 /* Length is stored at the first byte */
8627 size_t length = buf[input_offset];
8628 input_offset += 1;
8629
Gilles Peskine449bd832023-01-11 14:50:10 +01008630 if (input_offset + length > len) {
Valerio Settia08b1a42022-11-17 15:10:02 +01008631 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
8632 }
8633
Gilles Peskine449bd832023-01-11 14:50:10 +01008634 status = psa_pake_input(pake_ctx, step,
8635 buf + input_offset, length);
8636 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05008637 return PSA_TO_MBEDTLS_ERR(status);
Valerio Settia08b1a42022-11-17 15:10:02 +01008638 }
8639
8640 input_offset += length;
8641 }
8642 }
8643
Gilles Peskine449bd832023-01-11 14:50:10 +01008644 if (input_offset != len) {
Valerio Setti61ea17d2022-11-18 12:11:00 +01008645 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01008646 }
Valerio Setti30ebe112022-11-17 16:23:34 +01008647
Gilles Peskine449bd832023-01-11 14:50:10 +01008648 return 0;
Valerio Settia08b1a42022-11-17 15:10:02 +01008649}
8650
Valerio Setti6b3dab02022-11-17 17:14:54 +01008651int mbedtls_psa_ecjpake_write_round(
Gilles Peskine449bd832023-01-11 14:50:10 +01008652 psa_pake_operation_t *pake_ctx,
8653 unsigned char *buf,
8654 size_t len, size_t *olen,
8655 mbedtls_ecjpake_rounds_t round)
Valerio Settia08b1a42022-11-17 15:10:02 +01008656{
8657 psa_status_t status;
8658 size_t output_offset = 0;
8659 size_t output_len;
Valerio Setti819de862022-11-17 18:05:19 +01008660 /*
Valerio Setti6b3dab02022-11-17 17:14:54 +01008661 * At round one repeat the KEY_SHARE, ZK_PUBLIC & ZF_PROOF twice
8662 * At round two perform a single cycle
8663 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008664 unsigned int remaining_steps = (round == MBEDTLS_ECJPAKE_ROUND_ONE) ? 2 : 1;
Valerio Settia08b1a42022-11-17 15:10:02 +01008665
Gilles Peskine449bd832023-01-11 14:50:10 +01008666 for (; remaining_steps > 0; remaining_steps--) {
8667 for (psa_pake_step_t step = PSA_PAKE_STEP_KEY_SHARE;
8668 step <= PSA_PAKE_STEP_ZK_PROOF;
8669 ++step) {
Valerio Setti79f6b6b2022-11-21 14:17:03 +01008670 /*
Valerio Settid4a9b1a2022-11-22 11:11:10 +01008671 * For each step, prepend 1 byte with the length of the data as
8672 * given by psa_pake_output().
Valerio Setti79f6b6b2022-11-21 14:17:03 +01008673 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008674 status = psa_pake_output(pake_ctx, step,
8675 buf + output_offset + 1,
8676 len - output_offset - 1,
8677 &output_len);
8678 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05008679 return PSA_TO_MBEDTLS_ERR(status);
Valerio Settia08b1a42022-11-17 15:10:02 +01008680 }
8681
Valerio Setti99d88c12022-11-22 16:03:43 +01008682 *(buf + output_offset) = (uint8_t) output_len;
Valerio Setti79f6b6b2022-11-21 14:17:03 +01008683
8684 output_offset += output_len + 1;
Valerio Settia08b1a42022-11-17 15:10:02 +01008685 }
8686 }
8687
8688 *olen = output_offset;
8689
Gilles Peskine449bd832023-01-11 14:50:10 +01008690 return 0;
Valerio Settia08b1a42022-11-17 15:10:02 +01008691}
Valerio Settia08b1a42022-11-17 15:10:02 +01008692#endif //MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED && MBEDTLS_USE_PSA_CRYPTO
8693
Jerry Yuee40f9d2022-02-17 14:55:16 +08008694#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008695int mbedtls_ssl_get_key_exchange_md_tls1_2(mbedtls_ssl_context *ssl,
8696 unsigned char *hash, size_t *hashlen,
8697 unsigned char *data, size_t data_len,
8698 mbedtls_md_type_t md_alg)
Jerry Yuee40f9d2022-02-17 14:55:16 +08008699{
8700 psa_status_t status;
8701 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01008702 psa_algorithm_t hash_alg = mbedtls_hash_info_psa_from_md(md_alg);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008703
Gilles Peskine449bd832023-01-11 14:50:10 +01008704 MBEDTLS_SSL_DEBUG_MSG(3, ("Perform PSA-based computation of digest of ServerKeyExchange"));
Jerry Yuee40f9d2022-02-17 14:55:16 +08008705
Gilles Peskine449bd832023-01-11 14:50:10 +01008706 if ((status = psa_hash_setup(&hash_operation,
8707 hash_alg)) != PSA_SUCCESS) {
8708 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_setup", status);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008709 goto exit;
8710 }
8711
Gilles Peskine449bd832023-01-11 14:50:10 +01008712 if ((status = psa_hash_update(&hash_operation, ssl->handshake->randbytes,
8713 64)) != PSA_SUCCESS) {
8714 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_update", status);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008715 goto exit;
8716 }
8717
Gilles Peskine449bd832023-01-11 14:50:10 +01008718 if ((status = psa_hash_update(&hash_operation,
8719 data, data_len)) != PSA_SUCCESS) {
8720 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_update", status);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008721 goto exit;
8722 }
8723
Gilles Peskine449bd832023-01-11 14:50:10 +01008724 if ((status = psa_hash_finish(&hash_operation, hash, PSA_HASH_MAX_SIZE,
8725 hashlen)) != PSA_SUCCESS) {
8726 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_finish", status);
8727 goto exit;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008728 }
8729
8730exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008731 if (status != PSA_SUCCESS) {
8732 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8733 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
8734 switch (status) {
Jerry Yuee40f9d2022-02-17 14:55:16 +08008735 case PSA_ERROR_NOT_SUPPORTED:
Gilles Peskine449bd832023-01-11 14:50:10 +01008736 return MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008737 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
8738 case PSA_ERROR_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +01008739 return MBEDTLS_ERR_MD_BAD_INPUT_DATA;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008740 case PSA_ERROR_INSUFFICIENT_MEMORY:
Gilles Peskine449bd832023-01-11 14:50:10 +01008741 return MBEDTLS_ERR_MD_ALLOC_FAILED;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008742 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01008743 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008744 }
8745 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008746 return 0;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008747}
8748
8749#else
8750
Gilles Peskine449bd832023-01-11 14:50:10 +01008751int mbedtls_ssl_get_key_exchange_md_tls1_2(mbedtls_ssl_context *ssl,
8752 unsigned char *hash, size_t *hashlen,
8753 unsigned char *data, size_t data_len,
8754 mbedtls_md_type_t md_alg)
Jerry Yuee40f9d2022-02-17 14:55:16 +08008755{
8756 int ret = 0;
8757 mbedtls_md_context_t ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +01008758 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type(md_alg);
8759 *hashlen = mbedtls_md_get_size(md_info);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008760
Gilles Peskine449bd832023-01-11 14:50:10 +01008761 MBEDTLS_SSL_DEBUG_MSG(3, ("Perform mbedtls-based computation of digest of ServerKeyExchange"));
Jerry Yuee40f9d2022-02-17 14:55:16 +08008762
Gilles Peskine449bd832023-01-11 14:50:10 +01008763 mbedtls_md_init(&ctx);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008764
8765 /*
8766 * digitally-signed struct {
8767 * opaque client_random[32];
8768 * opaque server_random[32];
8769 * ServerDHParams params;
8770 * };
8771 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008772 if ((ret = mbedtls_md_setup(&ctx, md_info, 0)) != 0) {
8773 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_setup", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008774 goto exit;
8775 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008776 if ((ret = mbedtls_md_starts(&ctx)) != 0) {
8777 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_starts", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008778 goto exit;
8779 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008780 if ((ret = mbedtls_md_update(&ctx, ssl->handshake->randbytes, 64)) != 0) {
8781 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_update", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008782 goto exit;
8783 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008784 if ((ret = mbedtls_md_update(&ctx, data, data_len)) != 0) {
8785 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_update", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008786 goto exit;
8787 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008788 if ((ret = mbedtls_md_finish(&ctx, hash)) != 0) {
8789 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_finish", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008790 goto exit;
8791 }
8792
8793exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008794 mbedtls_md_free(&ctx);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008795
Gilles Peskine449bd832023-01-11 14:50:10 +01008796 if (ret != 0) {
8797 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8798 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
8799 }
Jerry Yuee40f9d2022-02-17 14:55:16 +08008800
Gilles Peskine449bd832023-01-11 14:50:10 +01008801 return ret;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008802}
8803#endif /* MBEDTLS_USE_PSA_CRYPTO */
8804
Jerry Yud9d91da2022-02-17 14:57:06 +08008805#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
8806
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008807/* Find the preferred hash for a given signature algorithm. */
8808unsigned int mbedtls_ssl_tls12_get_preferred_hash_for_sig_alg(
Gilles Peskine449bd832023-01-11 14:50:10 +01008809 mbedtls_ssl_context *ssl,
8810 unsigned int sig_alg)
Jerry Yud9d91da2022-02-17 14:57:06 +08008811{
Gabor Mezei078e8032022-04-27 21:17:56 +02008812 unsigned int i;
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008813 uint16_t *received_sig_algs = ssl->handshake->received_sig_algs;
Gabor Mezei078e8032022-04-27 21:17:56 +02008814
Gilles Peskine449bd832023-01-11 14:50:10 +01008815 if (sig_alg == MBEDTLS_SSL_SIG_ANON) {
8816 return MBEDTLS_SSL_HASH_NONE;
8817 }
Gabor Mezei078e8032022-04-27 21:17:56 +02008818
Gilles Peskine449bd832023-01-11 14:50:10 +01008819 for (i = 0; received_sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++) {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008820 unsigned int hash_alg_received =
Gilles Peskine449bd832023-01-11 14:50:10 +01008821 MBEDTLS_SSL_TLS12_HASH_ALG_FROM_SIG_AND_HASH_ALG(
8822 received_sig_algs[i]);
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008823 unsigned int sig_alg_received =
Gilles Peskine449bd832023-01-11 14:50:10 +01008824 MBEDTLS_SSL_TLS12_SIG_ALG_FROM_SIG_AND_HASH_ALG(
8825 received_sig_algs[i]);
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008826
Gilles Peskine449bd832023-01-11 14:50:10 +01008827 if (sig_alg == sig_alg_received) {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008828#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008829 if (ssl->handshake->key_cert && ssl->handshake->key_cert->key) {
Neil Armstrong96eceb82022-06-30 18:05:05 +02008830 psa_algorithm_t psa_hash_alg =
Gilles Peskine449bd832023-01-11 14:50:10 +01008831 mbedtls_hash_info_psa_from_md(hash_alg_received);
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008832
Gilles Peskine449bd832023-01-11 14:50:10 +01008833 if (sig_alg_received == MBEDTLS_SSL_SIG_ECDSA &&
8834 !mbedtls_pk_can_do_ext(ssl->handshake->key_cert->key,
8835 PSA_ALG_ECDSA(psa_hash_alg),
8836 PSA_KEY_USAGE_SIGN_HASH)) {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008837 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01008838 }
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008839
Gilles Peskine449bd832023-01-11 14:50:10 +01008840 if (sig_alg_received == MBEDTLS_SSL_SIG_RSA &&
8841 !mbedtls_pk_can_do_ext(ssl->handshake->key_cert->key,
8842 PSA_ALG_RSA_PKCS1V15_SIGN(
8843 psa_hash_alg),
8844 PSA_KEY_USAGE_SIGN_HASH)) {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008845 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01008846 }
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008847 }
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008848#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong96eceb82022-06-30 18:05:05 +02008849
Gilles Peskine449bd832023-01-11 14:50:10 +01008850 return hash_alg_received;
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008851 }
Jerry Yud9d91da2022-02-17 14:57:06 +08008852 }
Jerry Yud9d91da2022-02-17 14:57:06 +08008853
Gilles Peskine449bd832023-01-11 14:50:10 +01008854 return MBEDTLS_SSL_HASH_NONE;
Jerry Yud9d91da2022-02-17 14:57:06 +08008855}
8856
8857#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
8858
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008859/* Serialization of TLS 1.2 sessions:
8860 *
8861 * struct {
8862 * uint64 start_time;
8863 * uint8 ciphersuite[2]; // defined by the standard
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008864 * uint8 session_id_len; // at most 32
8865 * opaque session_id[32];
8866 * opaque master[48]; // fixed length in the standard
8867 * uint32 verify_result;
8868 * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert
8869 * opaque ticket<0..2^24-1>; // length 0 means no ticket
8870 * uint32 ticket_lifetime;
8871 * uint8 mfl_code; // up to 255 according to standard
8872 * uint8 encrypt_then_mac; // 0 or 1
8873 * } serialized_session_tls12;
8874 *
8875 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008876static size_t ssl_tls12_session_save(const mbedtls_ssl_session *session,
8877 unsigned char *buf,
8878 size_t buf_len)
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008879{
8880 unsigned char *p = buf;
8881 size_t used = 0;
8882
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 /*
8893 * Time
8894 */
8895#if defined(MBEDTLS_HAVE_TIME)
8896 used += 8;
8897
Gilles Peskine449bd832023-01-11 14:50:10 +01008898 if (used <= buf_len) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008899 start = (uint64_t) session->start;
8900
Gilles Peskine449bd832023-01-11 14:50:10 +01008901 MBEDTLS_PUT_UINT64_BE(start, p, 0);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008902 p += 8;
8903 }
8904#endif /* MBEDTLS_HAVE_TIME */
8905
8906 /*
8907 * Basic mandatory fields
8908 */
8909 used += 2 /* ciphersuite */
Gilles Peskine449bd832023-01-11 14:50:10 +01008910 + 1 /* id_len */
8911 + sizeof(session->id)
8912 + sizeof(session->master)
8913 + 4; /* verify_result */
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008914
Gilles Peskine449bd832023-01-11 14:50:10 +01008915 if (used <= buf_len) {
8916 MBEDTLS_PUT_UINT16_BE(session->ciphersuite, p, 0);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008917 p += 2;
8918
Gilles Peskine449bd832023-01-11 14:50:10 +01008919 *p++ = MBEDTLS_BYTE_0(session->id_len);
8920 memcpy(p, session->id, 32);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008921 p += 32;
8922
Gilles Peskine449bd832023-01-11 14:50:10 +01008923 memcpy(p, session->master, 48);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008924 p += 48;
8925
Gilles Peskine449bd832023-01-11 14:50:10 +01008926 MBEDTLS_PUT_UINT32_BE(session->verify_result, p, 0);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008927 p += 4;
8928 }
8929
8930 /*
8931 * Peer's end-entity certificate
8932 */
8933#if defined(MBEDTLS_X509_CRT_PARSE_C)
8934#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01008935 if (session->peer_cert == NULL) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008936 cert_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01008937 } else {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008938 cert_len = session->peer_cert->raw.len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008939 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008940
8941 used += 3 + cert_len;
8942
Gilles Peskine449bd832023-01-11 14:50:10 +01008943 if (used <= buf_len) {
8944 *p++ = MBEDTLS_BYTE_2(cert_len);
8945 *p++ = MBEDTLS_BYTE_1(cert_len);
8946 *p++ = MBEDTLS_BYTE_0(cert_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008947
Gilles Peskine449bd832023-01-11 14:50:10 +01008948 if (session->peer_cert != NULL) {
8949 memcpy(p, session->peer_cert->raw.p, cert_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008950 p += cert_len;
8951 }
8952 }
8953#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +01008954 if (session->peer_cert_digest != NULL) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008955 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008956 if (used <= buf_len) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008957 *p++ = (unsigned char) session->peer_cert_digest_type;
8958 *p++ = (unsigned char) session->peer_cert_digest_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008959 memcpy(p, session->peer_cert_digest,
8960 session->peer_cert_digest_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008961 p += session->peer_cert_digest_len;
8962 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008963 } else {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008964 used += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01008965 if (used <= buf_len) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008966 *p++ = (unsigned char) MBEDTLS_MD_NONE;
8967 *p++ = 0;
8968 }
8969 }
8970#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8971#endif /* MBEDTLS_X509_CRT_PARSE_C */
8972
8973 /*
8974 * Session ticket if any, plus associated data
8975 */
8976#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
8977 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
8978
Gilles Peskine449bd832023-01-11 14:50:10 +01008979 if (used <= buf_len) {
8980 *p++ = MBEDTLS_BYTE_2(session->ticket_len);
8981 *p++ = MBEDTLS_BYTE_1(session->ticket_len);
8982 *p++ = MBEDTLS_BYTE_0(session->ticket_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008983
Gilles Peskine449bd832023-01-11 14:50:10 +01008984 if (session->ticket != NULL) {
8985 memcpy(p, session->ticket, session->ticket_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008986 p += session->ticket_len;
8987 }
8988
Gilles Peskine449bd832023-01-11 14:50:10 +01008989 MBEDTLS_PUT_UINT32_BE(session->ticket_lifetime, p, 0);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008990 p += 4;
8991 }
8992#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
8993
8994 /*
8995 * Misc extension-related info
8996 */
8997#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
8998 used += 1;
8999
Gilles Peskine449bd832023-01-11 14:50:10 +01009000 if (used <= buf_len) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009001 *p++ = session->mfl_code;
Gilles Peskine449bd832023-01-11 14:50:10 +01009002 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009003#endif
9004
9005#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
9006 used += 1;
9007
Gilles Peskine449bd832023-01-11 14:50:10 +01009008 if (used <= buf_len) {
9009 *p++ = MBEDTLS_BYTE_0(session->encrypt_then_mac);
9010 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009011#endif
9012
Gilles Peskine449bd832023-01-11 14:50:10 +01009013 return used;
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009014}
9015
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02009016MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01009017static int ssl_tls12_session_load(mbedtls_ssl_session *session,
9018 const unsigned char *buf,
9019 size_t len)
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009020{
9021#if defined(MBEDTLS_HAVE_TIME)
9022 uint64_t start;
9023#endif
9024#if defined(MBEDTLS_X509_CRT_PARSE_C)
9025#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
9026 size_t cert_len;
9027#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9028#endif /* MBEDTLS_X509_CRT_PARSE_C */
9029
9030 const unsigned char *p = buf;
9031 const unsigned char * const end = buf + len;
9032
9033 /*
9034 * Time
9035 */
9036#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01009037 if (8 > (size_t) (end - p)) {
9038 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9039 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009040
Gilles Peskine449bd832023-01-11 14:50:10 +01009041 start = ((uint64_t) p[0] << 56) |
9042 ((uint64_t) p[1] << 48) |
9043 ((uint64_t) p[2] << 40) |
9044 ((uint64_t) p[3] << 32) |
9045 ((uint64_t) p[4] << 24) |
9046 ((uint64_t) p[5] << 16) |
9047 ((uint64_t) p[6] << 8) |
9048 ((uint64_t) p[7]);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009049 p += 8;
9050
9051 session->start = (time_t) start;
9052#endif /* MBEDTLS_HAVE_TIME */
9053
9054 /*
9055 * Basic mandatory fields
9056 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009057 if (2 + 1 + 32 + 48 + 4 > (size_t) (end - p)) {
9058 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9059 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009060
Gilles Peskine449bd832023-01-11 14:50:10 +01009061 session->ciphersuite = (p[0] << 8) | p[1];
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009062 p += 2;
9063
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009064 session->id_len = *p++;
Gilles Peskine449bd832023-01-11 14:50:10 +01009065 memcpy(session->id, p, 32);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009066 p += 32;
9067
Gilles Peskine449bd832023-01-11 14:50:10 +01009068 memcpy(session->master, p, 48);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009069 p += 48;
9070
Gilles Peskine449bd832023-01-11 14:50:10 +01009071 session->verify_result = ((uint32_t) p[0] << 24) |
9072 ((uint32_t) p[1] << 16) |
9073 ((uint32_t) p[2] << 8) |
9074 ((uint32_t) p[3]);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009075 p += 4;
9076
9077 /* Immediately clear invalid pointer values that have been read, in case
9078 * we exit early before we replaced them with valid ones. */
9079#if defined(MBEDTLS_X509_CRT_PARSE_C)
9080#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
9081 session->peer_cert = NULL;
9082#else
9083 session->peer_cert_digest = NULL;
9084#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9085#endif /* MBEDTLS_X509_CRT_PARSE_C */
9086#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
9087 session->ticket = NULL;
9088#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
9089
9090 /*
9091 * Peer certificate
9092 */
9093#if defined(MBEDTLS_X509_CRT_PARSE_C)
9094#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
9095 /* Deserialize CRT from the end of the ticket. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009096 if (3 > (size_t) (end - p)) {
9097 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9098 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009099
Gilles Peskine449bd832023-01-11 14:50:10 +01009100 cert_len = (p[0] << 16) | (p[1] << 8) | p[2];
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009101 p += 3;
9102
Gilles Peskine449bd832023-01-11 14:50:10 +01009103 if (cert_len != 0) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009104 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
9105
Gilles Peskine449bd832023-01-11 14:50:10 +01009106 if (cert_len > (size_t) (end - p)) {
9107 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9108 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009109
Gilles Peskine449bd832023-01-11 14:50:10 +01009110 session->peer_cert = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009111
Gilles Peskine449bd832023-01-11 14:50:10 +01009112 if (session->peer_cert == NULL) {
9113 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
9114 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009115
Gilles Peskine449bd832023-01-11 14:50:10 +01009116 mbedtls_x509_crt_init(session->peer_cert);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009117
Gilles Peskine449bd832023-01-11 14:50:10 +01009118 if ((ret = mbedtls_x509_crt_parse_der(session->peer_cert,
9119 p, cert_len)) != 0) {
9120 mbedtls_x509_crt_free(session->peer_cert);
9121 mbedtls_free(session->peer_cert);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009122 session->peer_cert = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01009123 return ret;
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009124 }
9125
9126 p += cert_len;
9127 }
9128#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9129 /* Deserialize CRT digest from the end of the ticket. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009130 if (2 > (size_t) (end - p)) {
9131 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9132 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009133
9134 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
9135 session->peer_cert_digest_len = (size_t) *p++;
9136
Gilles Peskine449bd832023-01-11 14:50:10 +01009137 if (session->peer_cert_digest_len != 0) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009138 const mbedtls_md_info_t *md_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01009139 mbedtls_md_info_from_type(session->peer_cert_digest_type);
9140 if (md_info == NULL) {
9141 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9142 }
9143 if (session->peer_cert_digest_len != mbedtls_md_get_size(md_info)) {
9144 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9145 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009146
Gilles Peskine449bd832023-01-11 14:50:10 +01009147 if (session->peer_cert_digest_len > (size_t) (end - p)) {
9148 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9149 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009150
9151 session->peer_cert_digest =
Gilles Peskine449bd832023-01-11 14:50:10 +01009152 mbedtls_calloc(1, session->peer_cert_digest_len);
9153 if (session->peer_cert_digest == NULL) {
9154 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
9155 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009156
Gilles Peskine449bd832023-01-11 14:50:10 +01009157 memcpy(session->peer_cert_digest, p,
9158 session->peer_cert_digest_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009159 p += session->peer_cert_digest_len;
9160 }
9161#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9162#endif /* MBEDTLS_X509_CRT_PARSE_C */
9163
9164 /*
9165 * Session ticket and associated data
9166 */
9167#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01009168 if (3 > (size_t) (end - p)) {
9169 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9170 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009171
Gilles Peskine449bd832023-01-11 14:50:10 +01009172 session->ticket_len = (p[0] << 16) | (p[1] << 8) | p[2];
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009173 p += 3;
9174
Gilles Peskine449bd832023-01-11 14:50:10 +01009175 if (session->ticket_len != 0) {
9176 if (session->ticket_len > (size_t) (end - p)) {
9177 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9178 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009179
Gilles Peskine449bd832023-01-11 14:50:10 +01009180 session->ticket = mbedtls_calloc(1, session->ticket_len);
9181 if (session->ticket == NULL) {
9182 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
9183 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009184
Gilles Peskine449bd832023-01-11 14:50:10 +01009185 memcpy(session->ticket, p, session->ticket_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009186 p += session->ticket_len;
9187 }
9188
Gilles Peskine449bd832023-01-11 14:50:10 +01009189 if (4 > (size_t) (end - p)) {
9190 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9191 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009192
Gilles Peskine449bd832023-01-11 14:50:10 +01009193 session->ticket_lifetime = ((uint32_t) p[0] << 24) |
9194 ((uint32_t) p[1] << 16) |
9195 ((uint32_t) p[2] << 8) |
9196 ((uint32_t) p[3]);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009197 p += 4;
9198#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
9199
9200 /*
9201 * Misc extension-related info
9202 */
9203#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01009204 if (1 > (size_t) (end - p)) {
9205 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9206 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009207
9208 session->mfl_code = *p++;
9209#endif
9210
9211#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01009212 if (1 > (size_t) (end - p)) {
9213 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9214 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009215
9216 session->encrypt_then_mac = *p++;
9217#endif
9218
9219 /* Done, should have consumed entire buffer */
Gilles Peskine449bd832023-01-11 14:50:10 +01009220 if (p != end) {
9221 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9222 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009223
Gilles Peskine449bd832023-01-11 14:50:10 +01009224 return 0;
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009225}
Jerry Yudc7bd172022-02-17 13:44:15 +08009226#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9227
XiaokangQian75d40ef2022-04-20 11:05:24 +00009228int mbedtls_ssl_validate_ciphersuite(
9229 const mbedtls_ssl_context *ssl,
9230 const mbedtls_ssl_ciphersuite_t *suite_info,
9231 mbedtls_ssl_protocol_version min_tls_version,
Gilles Peskine449bd832023-01-11 14:50:10 +01009232 mbedtls_ssl_protocol_version max_tls_version)
XiaokangQian75d40ef2022-04-20 11:05:24 +00009233{
9234 (void) ssl;
9235
Gilles Peskine449bd832023-01-11 14:50:10 +01009236 if (suite_info == NULL) {
9237 return -1;
9238 }
XiaokangQian75d40ef2022-04-20 11:05:24 +00009239
Gilles Peskine449bd832023-01-11 14:50:10 +01009240 if ((suite_info->min_tls_version > max_tls_version) ||
9241 (suite_info->max_tls_version < min_tls_version)) {
9242 return -1;
XiaokangQian75d40ef2022-04-20 11:05:24 +00009243 }
9244
XiaokangQian060d8672022-04-21 09:24:56 +00009245#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_CLI_C)
XiaokangQian75d40ef2022-04-20 11:05:24 +00009246#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Neil Armstrongca7d5062022-05-31 14:43:23 +02009247#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01009248 if (suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
9249 ssl->handshake->psa_pake_ctx_is_ok != 1)
Neil Armstrongca7d5062022-05-31 14:43:23 +02009250#else
Gilles Peskine449bd832023-01-11 14:50:10 +01009251 if (suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
9252 mbedtls_ecjpake_check(&ssl->handshake->ecjpake_ctx) != 0)
Neil Armstrongca7d5062022-05-31 14:43:23 +02009253#endif /* MBEDTLS_USE_PSA_CRYPTO */
XiaokangQian75d40ef2022-04-20 11:05:24 +00009254 {
Gilles Peskine449bd832023-01-11 14:50:10 +01009255 return -1;
XiaokangQian75d40ef2022-04-20 11:05:24 +00009256 }
9257#endif
9258
9259 /* Don't suggest PSK-based ciphersuite if no PSK is available. */
9260#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01009261 if (mbedtls_ssl_ciphersuite_uses_psk(suite_info) &&
9262 mbedtls_ssl_conf_has_static_psk(ssl->conf) == 0) {
9263 return -1;
XiaokangQian75d40ef2022-04-20 11:05:24 +00009264 }
9265#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
9266#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9267
Gilles Peskine449bd832023-01-11 14:50:10 +01009268 return 0;
XiaokangQian75d40ef2022-04-20 11:05:24 +00009269}
9270
Ronald Crone68ab4f2022-10-05 12:46:29 +02009271#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
XiaokangQianeaf36512022-04-24 09:07:44 +00009272/*
9273 * Function for writing a signature algorithm extension.
9274 *
9275 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
9276 * value (TLS 1.3 RFC8446):
9277 * enum {
9278 * ....
9279 * ecdsa_secp256r1_sha256( 0x0403 ),
9280 * ecdsa_secp384r1_sha384( 0x0503 ),
9281 * ecdsa_secp521r1_sha512( 0x0603 ),
9282 * ....
9283 * } SignatureScheme;
9284 *
9285 * struct {
9286 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
9287 * } SignatureSchemeList;
9288 *
9289 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
9290 * value (TLS 1.2 RFC5246):
9291 * enum {
9292 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
9293 * sha512(6), (255)
9294 * } HashAlgorithm;
9295 *
9296 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
9297 * SignatureAlgorithm;
9298 *
9299 * struct {
9300 * HashAlgorithm hash;
9301 * SignatureAlgorithm signature;
9302 * } SignatureAndHashAlgorithm;
9303 *
9304 * SignatureAndHashAlgorithm
9305 * supported_signature_algorithms<2..2^16-2>;
9306 *
9307 * The TLS 1.3 signature algorithm extension was defined to be a compatible
9308 * generalization of the TLS 1.2 signature algorithm extension.
9309 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
9310 * `SignatureScheme` field of TLS 1.3
9311 *
9312 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009313int mbedtls_ssl_write_sig_alg_ext(mbedtls_ssl_context *ssl, unsigned char *buf,
9314 const unsigned char *end, size_t *out_len)
XiaokangQianeaf36512022-04-24 09:07:44 +00009315{
9316 unsigned char *p = buf;
9317 unsigned char *supported_sig_alg; /* Start of supported_signature_algorithms */
9318 size_t supported_sig_alg_len = 0; /* Length of supported_signature_algorithms */
9319
9320 *out_len = 0;
9321
Gilles Peskine449bd832023-01-11 14:50:10 +01009322 MBEDTLS_SSL_DEBUG_MSG(3, ("adding signature_algorithms extension"));
XiaokangQianeaf36512022-04-24 09:07:44 +00009323
9324 /* Check if we have space for header and length field:
9325 * - extension_type (2 bytes)
9326 * - extension_data_length (2 bytes)
9327 * - supported_signature_algorithms_length (2 bytes)
9328 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009329 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 6);
XiaokangQianeaf36512022-04-24 09:07:44 +00009330 p += 6;
9331
9332 /*
9333 * Write supported_signature_algorithms
9334 */
9335 supported_sig_alg = p;
Gilles Peskine449bd832023-01-11 14:50:10 +01009336 const uint16_t *sig_alg = mbedtls_ssl_get_sig_algs(ssl);
9337 if (sig_alg == NULL) {
9338 return MBEDTLS_ERR_SSL_BAD_CONFIG;
9339 }
XiaokangQianeaf36512022-04-24 09:07:44 +00009340
Gilles Peskine449bd832023-01-11 14:50:10 +01009341 for (; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++) {
9342 MBEDTLS_SSL_DEBUG_MSG(3, ("got signature scheme [%x] %s",
9343 *sig_alg,
9344 mbedtls_ssl_sig_alg_to_str(*sig_alg)));
9345 if (!mbedtls_ssl_sig_alg_is_supported(ssl, *sig_alg)) {
XiaokangQianeaf36512022-04-24 09:07:44 +00009346 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01009347 }
9348 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
9349 MBEDTLS_PUT_UINT16_BE(*sig_alg, p, 0);
XiaokangQianeaf36512022-04-24 09:07:44 +00009350 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01009351 MBEDTLS_SSL_DEBUG_MSG(3, ("sent signature scheme [%x] %s",
9352 *sig_alg,
9353 mbedtls_ssl_sig_alg_to_str(*sig_alg)));
XiaokangQianeaf36512022-04-24 09:07:44 +00009354 }
9355
9356 /* Length of supported_signature_algorithms */
9357 supported_sig_alg_len = p - supported_sig_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01009358 if (supported_sig_alg_len == 0) {
9359 MBEDTLS_SSL_DEBUG_MSG(1, ("No signature algorithms defined."));
9360 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
XiaokangQianeaf36512022-04-24 09:07:44 +00009361 }
9362
Gilles Peskine449bd832023-01-11 14:50:10 +01009363 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SIG_ALG, buf, 0);
9364 MBEDTLS_PUT_UINT16_BE(supported_sig_alg_len + 2, buf, 2);
9365 MBEDTLS_PUT_UINT16_BE(supported_sig_alg_len, buf, 4);
XiaokangQianeaf36512022-04-24 09:07:44 +00009366
XiaokangQianeaf36512022-04-24 09:07:44 +00009367 *out_len = p - buf;
9368
9369#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01009370 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_SIG_ALG);
XiaokangQianeaf36512022-04-24 09:07:44 +00009371#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu0c354a22022-08-29 15:25:36 +08009372
Gilles Peskine449bd832023-01-11 14:50:10 +01009373 return 0;
XiaokangQianeaf36512022-04-24 09:07:44 +00009374}
Ronald Crone68ab4f2022-10-05 12:46:29 +02009375#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
XiaokangQianeaf36512022-04-24 09:07:44 +00009376
XiaokangQian40a35232022-05-07 09:02:40 +00009377#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
XiaokangQian9b2b7712022-05-17 02:57:00 +00009378/*
9379 * mbedtls_ssl_parse_server_name_ext
9380 *
9381 * Structure of server_name extension:
9382 *
9383 * enum {
9384 * host_name(0), (255)
9385 * } NameType;
9386 * opaque HostName<1..2^16-1>;
9387 *
9388 * struct {
9389 * NameType name_type;
9390 * select (name_type) {
9391 * case host_name: HostName;
9392 * } name;
9393 * } ServerName;
9394 * struct {
9395 * ServerName server_name_list<1..2^16-1>
9396 * } ServerNameList;
9397 */
Ronald Cronce7d76e2022-07-08 18:56:49 +02009398MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01009399int mbedtls_ssl_parse_server_name_ext(mbedtls_ssl_context *ssl,
9400 const unsigned char *buf,
9401 const unsigned char *end)
XiaokangQian40a35232022-05-07 09:02:40 +00009402{
9403 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
9404 const unsigned char *p = buf;
XiaokangQian9b2b7712022-05-17 02:57:00 +00009405 size_t server_name_list_len, hostname_len;
9406 const unsigned char *server_name_list_end;
XiaokangQian40a35232022-05-07 09:02:40 +00009407
Gilles Peskine449bd832023-01-11 14:50:10 +01009408 MBEDTLS_SSL_DEBUG_MSG(3, ("parse ServerName extension"));
XiaokangQian40a35232022-05-07 09:02:40 +00009409
Gilles Peskine449bd832023-01-11 14:50:10 +01009410 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
9411 server_name_list_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian40a35232022-05-07 09:02:40 +00009412 p += 2;
9413
Gilles Peskine449bd832023-01-11 14:50:10 +01009414 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, server_name_list_len);
XiaokangQian9b2b7712022-05-17 02:57:00 +00009415 server_name_list_end = p + server_name_list_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01009416 while (p < server_name_list_end) {
9417 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, server_name_list_end, 3);
9418 hostname_len = MBEDTLS_GET_UINT16_BE(p, 1);
9419 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, server_name_list_end,
9420 hostname_len + 3);
XiaokangQian40a35232022-05-07 09:02:40 +00009421
Gilles Peskine449bd832023-01-11 14:50:10 +01009422 if (p[0] == MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME) {
XiaokangQian75fe8c72022-06-15 09:42:45 +00009423 /* sni_name is intended to be used only during the parsing of the
9424 * ClientHello message (it is reset to NULL before the end of
9425 * the message parsing). Thus it is ok to just point to the
9426 * reception buffer and not make a copy of it.
9427 */
XiaokangQianf2a94202022-05-20 06:44:24 +00009428 ssl->handshake->sni_name = p + 3;
9429 ssl->handshake->sni_name_len = hostname_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01009430 if (ssl->conf->f_sni == NULL) {
9431 return 0;
XiaokangQian40a35232022-05-07 09:02:40 +00009432 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009433 ret = ssl->conf->f_sni(ssl->conf->p_sni,
9434 ssl, p + 3, hostname_len);
9435 if (ret != 0) {
9436 MBEDTLS_SSL_DEBUG_RET(1, "ssl_sni_wrapper", ret);
9437 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_UNRECOGNIZED_NAME,
9438 MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME);
9439 return MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME;
9440 }
9441 return 0;
XiaokangQian40a35232022-05-07 09:02:40 +00009442 }
9443
9444 p += hostname_len + 3;
9445 }
9446
Gilles Peskine449bd832023-01-11 14:50:10 +01009447 return 0;
XiaokangQian40a35232022-05-07 09:02:40 +00009448}
9449#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
9450
XiaokangQianacb39922022-06-17 10:18:48 +00009451#if defined(MBEDTLS_SSL_ALPN)
Ronald Cronce7d76e2022-07-08 18:56:49 +02009452MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01009453int mbedtls_ssl_parse_alpn_ext(mbedtls_ssl_context *ssl,
9454 const unsigned char *buf,
9455 const unsigned char *end)
XiaokangQianacb39922022-06-17 10:18:48 +00009456{
9457 const unsigned char *p = buf;
XiaokangQianc7403452022-06-23 03:24:12 +00009458 size_t protocol_name_list_len;
XiaokangQian95d5f542022-06-24 02:29:26 +00009459 const unsigned char *protocol_name_list;
9460 const unsigned char *protocol_name_list_end;
XiaokangQianc7403452022-06-23 03:24:12 +00009461 size_t protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009462
9463 /* If ALPN not configured, just ignore the extension */
Gilles Peskine449bd832023-01-11 14:50:10 +01009464 if (ssl->conf->alpn_list == NULL) {
9465 return 0;
9466 }
XiaokangQianacb39922022-06-17 10:18:48 +00009467
9468 /*
XiaokangQianc7403452022-06-23 03:24:12 +00009469 * RFC7301, section 3.1
9470 * opaque ProtocolName<1..2^8-1>;
XiaokangQianacb39922022-06-17 10:18:48 +00009471 *
XiaokangQianc7403452022-06-23 03:24:12 +00009472 * struct {
9473 * ProtocolName protocol_name_list<2..2^16-1>
9474 * } ProtocolNameList;
XiaokangQianacb39922022-06-17 10:18:48 +00009475 */
9476
XiaokangQianc7403452022-06-23 03:24:12 +00009477 /*
XiaokangQian0b776e22022-06-24 09:04:59 +00009478 * protocol_name_list_len 2 bytes
9479 * protocol_name_len 1 bytes
9480 * protocol_name >=1 byte
XiaokangQianc7403452022-06-23 03:24:12 +00009481 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009482 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 4);
XiaokangQianacb39922022-06-17 10:18:48 +00009483
Gilles Peskine449bd832023-01-11 14:50:10 +01009484 protocol_name_list_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQianacb39922022-06-17 10:18:48 +00009485 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01009486 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, protocol_name_list_len);
XiaokangQian95d5f542022-06-24 02:29:26 +00009487 protocol_name_list = p;
9488 protocol_name_list_end = p + protocol_name_list_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009489
9490 /* Validate peer's list (lengths) */
Gilles Peskine449bd832023-01-11 14:50:10 +01009491 while (p < protocol_name_list_end) {
XiaokangQian95d5f542022-06-24 02:29:26 +00009492 protocol_name_len = *p++;
Gilles Peskine449bd832023-01-11 14:50:10 +01009493 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, protocol_name_list_end,
9494 protocol_name_len);
9495 if (protocol_name_len == 0) {
XiaokangQian95d5f542022-06-24 02:29:26 +00009496 MBEDTLS_SSL_PEND_FATAL_ALERT(
9497 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
Gilles Peskine449bd832023-01-11 14:50:10 +01009498 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
9499 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
XiaokangQian95d5f542022-06-24 02:29:26 +00009500 }
9501
9502 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009503 }
9504
9505 /* Use our order of preference */
Gilles Peskine449bd832023-01-11 14:50:10 +01009506 for (const char **alpn = ssl->conf->alpn_list; *alpn != NULL; alpn++) {
9507 size_t const alpn_len = strlen(*alpn);
XiaokangQian95d5f542022-06-24 02:29:26 +00009508 p = protocol_name_list;
Gilles Peskine449bd832023-01-11 14:50:10 +01009509 while (p < protocol_name_list_end) {
XiaokangQian95d5f542022-06-24 02:29:26 +00009510 protocol_name_len = *p++;
Gilles Peskine449bd832023-01-11 14:50:10 +01009511 if (protocol_name_len == alpn_len &&
9512 memcmp(p, *alpn, alpn_len) == 0) {
XiaokangQianacb39922022-06-17 10:18:48 +00009513 ssl->alpn_chosen = *alpn;
Gilles Peskine449bd832023-01-11 14:50:10 +01009514 return 0;
XiaokangQianacb39922022-06-17 10:18:48 +00009515 }
XiaokangQian95d5f542022-06-24 02:29:26 +00009516
9517 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009518 }
9519 }
9520
XiaokangQian95d5f542022-06-24 02:29:26 +00009521 /* If we get here, no match was found */
XiaokangQianacb39922022-06-17 10:18:48 +00009522 MBEDTLS_SSL_PEND_FATAL_ALERT(
Gilles Peskine449bd832023-01-11 14:50:10 +01009523 MBEDTLS_SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL,
9524 MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL);
9525 return MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL;
XiaokangQianacb39922022-06-17 10:18:48 +00009526}
9527
Gilles Peskine449bd832023-01-11 14:50:10 +01009528int mbedtls_ssl_write_alpn_ext(mbedtls_ssl_context *ssl,
9529 unsigned char *buf,
9530 unsigned char *end,
9531 size_t *out_len)
XiaokangQianacb39922022-06-17 10:18:48 +00009532{
9533 unsigned char *p = buf;
XiaokangQian95d5f542022-06-24 02:29:26 +00009534 size_t protocol_name_len;
XiaokangQianc7403452022-06-23 03:24:12 +00009535 *out_len = 0;
XiaokangQianacb39922022-06-17 10:18:48 +00009536
Gilles Peskine449bd832023-01-11 14:50:10 +01009537 if (ssl->alpn_chosen == NULL) {
9538 return 0;
XiaokangQianacb39922022-06-17 10:18:48 +00009539 }
9540
Gilles Peskine449bd832023-01-11 14:50:10 +01009541 protocol_name_len = strlen(ssl->alpn_chosen);
9542 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 7 + protocol_name_len);
XiaokangQianacb39922022-06-17 10:18:48 +00009543
Gilles Peskine449bd832023-01-11 14:50:10 +01009544 MBEDTLS_SSL_DEBUG_MSG(3, ("server side, adding alpn extension"));
XiaokangQianacb39922022-06-17 10:18:48 +00009545 /*
9546 * 0 . 1 ext identifier
9547 * 2 . 3 ext length
9548 * 4 . 5 protocol list length
9549 * 6 . 6 protocol name length
9550 * 7 . 7+n protocol name
9551 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009552 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_ALPN, p, 0);
XiaokangQianacb39922022-06-17 10:18:48 +00009553
XiaokangQian95d5f542022-06-24 02:29:26 +00009554 *out_len = 7 + protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009555
Gilles Peskine449bd832023-01-11 14:50:10 +01009556 MBEDTLS_PUT_UINT16_BE(protocol_name_len + 3, p, 2);
9557 MBEDTLS_PUT_UINT16_BE(protocol_name_len + 1, p, 4);
XiaokangQian0b776e22022-06-24 09:04:59 +00009558 /* Note: the length of the chosen protocol has been checked to be less
9559 * than 255 bytes in `mbedtls_ssl_conf_alpn_protocols`.
9560 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009561 p[6] = MBEDTLS_BYTE_0(protocol_name_len);
XiaokangQianacb39922022-06-17 10:18:48 +00009562
Gilles Peskine449bd832023-01-11 14:50:10 +01009563 memcpy(p + 7, ssl->alpn_chosen, protocol_name_len);
Jerry Yub95dd362022-11-08 21:19:34 +08009564
9565#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01009566 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_ALPN);
Jerry Yub95dd362022-11-08 21:19:34 +08009567#endif
9568
Gilles Peskine449bd832023-01-11 14:50:10 +01009569 return 0;
XiaokangQianacb39922022-06-17 10:18:48 +00009570}
9571#endif /* MBEDTLS_SSL_ALPN */
9572
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009573#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
Xiaokang Qian03409292022-10-12 02:49:52 +00009574 defined(MBEDTLS_SSL_SESSION_TICKETS) && \
Xiaokang Qianed0620c2022-10-12 06:58:13 +00009575 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \
Xiaokang Qianed3afcd2022-10-12 08:31:11 +00009576 defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01009577int mbedtls_ssl_session_set_hostname(mbedtls_ssl_session *session,
9578 const char *hostname)
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009579{
9580 /* Initialize to suppress unnecessary compiler warning */
9581 size_t hostname_len = 0;
9582
9583 /* Check if new hostname is valid before
9584 * making any change to current one */
Gilles Peskine449bd832023-01-11 14:50:10 +01009585 if (hostname != NULL) {
9586 hostname_len = strlen(hostname);
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009587
Gilles Peskine449bd832023-01-11 14:50:10 +01009588 if (hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN) {
9589 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9590 }
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009591 }
9592
9593 /* Now it's clear that we will overwrite the old hostname,
9594 * so we can free it safely */
Gilles Peskine449bd832023-01-11 14:50:10 +01009595 if (session->hostname != NULL) {
9596 mbedtls_platform_zeroize(session->hostname,
9597 strlen(session->hostname));
9598 mbedtls_free(session->hostname);
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009599 }
9600
9601 /* Passing NULL as hostname shall clear the old one */
Gilles Peskine449bd832023-01-11 14:50:10 +01009602 if (hostname == NULL) {
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009603 session->hostname = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01009604 } else {
9605 session->hostname = mbedtls_calloc(1, hostname_len + 1);
9606 if (session->hostname == NULL) {
9607 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
9608 }
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009609
Gilles Peskine449bd832023-01-11 14:50:10 +01009610 memcpy(session->hostname, hostname, hostname_len);
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009611 }
9612
Gilles Peskine449bd832023-01-11 14:50:10 +01009613 return 0;
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009614}
Xiaokang Qian03409292022-10-12 02:49:52 +00009615#endif /* MBEDTLS_SSL_PROTO_TLS1_3 &&
9616 MBEDTLS_SSL_SESSION_TICKETS &&
Xiaokang Qianed0620c2022-10-12 06:58:13 +00009617 MBEDTLS_SSL_SERVER_NAME_INDICATION &&
Xiaokang Qianed3afcd2022-10-12 08:31:11 +00009618 MBEDTLS_SSL_CLI_C */
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009619
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009620#endif /* MBEDTLS_SSL_TLS_C */