blob: 811d63d096f9d3676c82e75ac6c5fe3824cca1e0 [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
SimonBd5800b72016-04-26 07:43:27 +010028#include "mbedtls/platform.h"
SimonBd5800b72016-04-26 07:43:27 +010029
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000030#include "mbedtls/ssl.h"
Ronald Cron9f0fba32022-02-10 16:45:15 +010031#include "ssl_client.h"
Ronald Cron27c85e72022-03-08 11:37:55 +010032#include "ssl_debug_helpers.h"
Chris Jones84a773f2021-03-05 18:38:47 +000033#include "ssl_misc.h"
Andrzej Kurek25f27152022-08-17 16:09:31 -040034
Janos Follath73c616b2019-12-18 15:07:04 +000035#include "mbedtls/debug.h"
36#include "mbedtls/error.h"
Andres Amaya Garcia1f6301b2018-04-17 09:51:09 -050037#include "mbedtls/platform_util.h"
Hanno Beckera835da52019-05-16 12:39:07 +010038#include "mbedtls/version.h"
Gabor Mezei765862c2021-10-19 12:22:25 +020039#include "mbedtls/constant_time.h"
Paul Bakker0be444a2013-08-27 21:55:01 +020040
Rich Evans00ab4702015-02-06 13:43:58 +000041#include <string.h>
42
Andrzej Kurekd6db9be2019-01-10 05:27:10 -050043#if defined(MBEDTLS_USE_PSA_CRYPTO)
44#include "mbedtls/psa_util.h"
45#include "psa/crypto.h"
46#endif
47
Janos Follath23bdca02016-10-07 14:47:14 +010048#if defined(MBEDTLS_X509_CRT_PARSE_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000049#include "mbedtls/oid.h"
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +020050#endif
51
Andrzej Kurek8a045ce2022-12-23 11:00:06 -050052#if defined(MBEDTLS_USE_PSA_CRYPTO)
53#define PSA_TO_MBEDTLS_ERR(status) PSA_TO_MBEDTLS_ERR_LIST(status, \
54 psa_to_ssl_errors, \
55 psa_generic_status_to_mbedtls)
Andrzej Kurekdaf5b562023-03-03 05:52:28 -050056#define PSA_TO_MD_ERR(status) PSA_TO_MBEDTLS_ERR_LIST(status, \
57 psa_to_md_errors, \
58 psa_generic_status_to_mbedtls)
Andrzej Kurek8a045ce2022-12-23 11:00:06 -050059#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
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100421#if defined(MBEDTLS_MD_CAN_SHA256)
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
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100430#endif /* MBEDTLS_MD_CAN_SHA256*/
Gilles Peskine449bd832023-01-11 14:50:10 +0100431
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100432#if defined(MBEDTLS_MD_CAN_SHA384)
Gilles Peskine449bd832023-01-11 14:50:10 +0100433MBEDTLS_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);
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100441#endif /* MBEDTLS_MD_CAN_SHA384*/
Gilles Peskine449bd832023-01-11 14:50:10 +0100442
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
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100455#if defined(MBEDTLS_MD_CAN_SHA256)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100456static int ssl_update_checksum_sha256(mbedtls_ssl_context *, const unsigned char *, size_t);
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100457#endif /* MBEDTLS_MD_CAN_SHA256*/
Gilles Peskine449bd832023-01-11 14:50:10 +0100458
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100459#if defined(MBEDTLS_MD_CAN_SHA384)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100460static int ssl_update_checksum_sha384(mbedtls_ssl_context *, const unsigned char *, size_t);
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100461#endif /* MBEDTLS_MD_CAN_SHA384*/
Gilles Peskine449bd832023-01-11 14:50:10 +0100462
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)
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100473#if defined(MBEDTLS_MD_CAN_SHA384)
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;
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100477#endif /* MBEDTLS_MD_CAN_SHA384*/
478#if defined(MBEDTLS_MD_CAN_SHA256)
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;
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100482#endif /* MBEDTLS_MD_CAN_SHA256*/
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
Jan Bruckner151f6422023-02-10 12:45:19 +0100593 case MBEDTLS_TLS_EXT_RECORD_SIZE_LIMIT:
594 return MBEDTLS_SSL_EXT_ID_RECORD_SIZE_LIMIT;
595
Jerry Yu7a485c12022-10-31 13:08:18 +0800596 case MBEDTLS_TLS_EXT_SESSION_TICKET:
Gilles Peskine449bd832023-01-11 14:50:10 +0100597 return MBEDTLS_SSL_EXT_ID_SESSION_TICKET;
Jerry Yu7a485c12022-10-31 13:08:18 +0800598
599 }
600
Gilles Peskine449bd832023-01-11 14:50:10 +0100601 return MBEDTLS_SSL_EXT_ID_UNRECOGNIZED;
Jerry Yu7a485c12022-10-31 13:08:18 +0800602}
603
Gilles Peskine449bd832023-01-11 14:50:10 +0100604uint32_t mbedtls_ssl_get_extension_mask(unsigned int extension_type)
Jerry Yu7a485c12022-10-31 13:08:18 +0800605{
Gilles Peskine449bd832023-01-11 14:50:10 +0100606 return 1 << mbedtls_ssl_get_extension_id(extension_type);
Jerry Yu7a485c12022-10-31 13:08:18 +0800607}
608
Jerry Yud25cab02022-10-31 12:48:30 +0800609#if defined(MBEDTLS_DEBUG_C)
610static const char *extension_name_table[] = {
Jerry Yuea52ed92022-11-08 21:01:17 +0800611 [MBEDTLS_SSL_EXT_ID_UNRECOGNIZED] = "unrecognized",
Jerry Yud25cab02022-10-31 12:48:30 +0800612 [MBEDTLS_SSL_EXT_ID_SERVERNAME] = "server_name",
613 [MBEDTLS_SSL_EXT_ID_MAX_FRAGMENT_LENGTH] = "max_fragment_length",
614 [MBEDTLS_SSL_EXT_ID_STATUS_REQUEST] = "status_request",
615 [MBEDTLS_SSL_EXT_ID_SUPPORTED_GROUPS] = "supported_groups",
616 [MBEDTLS_SSL_EXT_ID_SIG_ALG] = "signature_algorithms",
617 [MBEDTLS_SSL_EXT_ID_USE_SRTP] = "use_srtp",
618 [MBEDTLS_SSL_EXT_ID_HEARTBEAT] = "heartbeat",
619 [MBEDTLS_SSL_EXT_ID_ALPN] = "application_layer_protocol_negotiation",
620 [MBEDTLS_SSL_EXT_ID_SCT] = "signed_certificate_timestamp",
621 [MBEDTLS_SSL_EXT_ID_CLI_CERT_TYPE] = "client_certificate_type",
622 [MBEDTLS_SSL_EXT_ID_SERV_CERT_TYPE] = "server_certificate_type",
623 [MBEDTLS_SSL_EXT_ID_PADDING] = "padding",
624 [MBEDTLS_SSL_EXT_ID_PRE_SHARED_KEY] = "pre_shared_key",
625 [MBEDTLS_SSL_EXT_ID_EARLY_DATA] = "early_data",
626 [MBEDTLS_SSL_EXT_ID_SUPPORTED_VERSIONS] = "supported_versions",
627 [MBEDTLS_SSL_EXT_ID_COOKIE] = "cookie",
628 [MBEDTLS_SSL_EXT_ID_PSK_KEY_EXCHANGE_MODES] = "psk_key_exchange_modes",
629 [MBEDTLS_SSL_EXT_ID_CERT_AUTH] = "certificate_authorities",
630 [MBEDTLS_SSL_EXT_ID_OID_FILTERS] = "oid_filters",
631 [MBEDTLS_SSL_EXT_ID_POST_HANDSHAKE_AUTH] = "post_handshake_auth",
632 [MBEDTLS_SSL_EXT_ID_SIG_ALG_CERT] = "signature_algorithms_cert",
633 [MBEDTLS_SSL_EXT_ID_KEY_SHARE] = "key_share",
634 [MBEDTLS_SSL_EXT_ID_TRUNCATED_HMAC] = "truncated_hmac",
635 [MBEDTLS_SSL_EXT_ID_SUPPORTED_POINT_FORMATS] = "supported_point_formats",
636 [MBEDTLS_SSL_EXT_ID_ENCRYPT_THEN_MAC] = "encrypt_then_mac",
637 [MBEDTLS_SSL_EXT_ID_EXTENDED_MASTER_SECRET] = "extended_master_secret",
Jan Bruckner151f6422023-02-10 12:45:19 +0100638 [MBEDTLS_SSL_EXT_ID_SESSION_TICKET] = "session_ticket",
639 [MBEDTLS_SSL_EXT_ID_RECORD_SIZE_LIMIT] = "record_size_limit"
Jerry Yud25cab02022-10-31 12:48:30 +0800640};
641
Gilles Peskine449bd832023-01-11 14:50:10 +0100642static unsigned int extension_type_table[] = {
Jerry Yud25cab02022-10-31 12:48:30 +0800643 [MBEDTLS_SSL_EXT_ID_UNRECOGNIZED] = 0xff,
644 [MBEDTLS_SSL_EXT_ID_SERVERNAME] = MBEDTLS_TLS_EXT_SERVERNAME,
645 [MBEDTLS_SSL_EXT_ID_MAX_FRAGMENT_LENGTH] = MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH,
646 [MBEDTLS_SSL_EXT_ID_STATUS_REQUEST] = MBEDTLS_TLS_EXT_STATUS_REQUEST,
647 [MBEDTLS_SSL_EXT_ID_SUPPORTED_GROUPS] = MBEDTLS_TLS_EXT_SUPPORTED_GROUPS,
648 [MBEDTLS_SSL_EXT_ID_SIG_ALG] = MBEDTLS_TLS_EXT_SIG_ALG,
649 [MBEDTLS_SSL_EXT_ID_USE_SRTP] = MBEDTLS_TLS_EXT_USE_SRTP,
650 [MBEDTLS_SSL_EXT_ID_HEARTBEAT] = MBEDTLS_TLS_EXT_HEARTBEAT,
651 [MBEDTLS_SSL_EXT_ID_ALPN] = MBEDTLS_TLS_EXT_ALPN,
652 [MBEDTLS_SSL_EXT_ID_SCT] = MBEDTLS_TLS_EXT_SCT,
653 [MBEDTLS_SSL_EXT_ID_CLI_CERT_TYPE] = MBEDTLS_TLS_EXT_CLI_CERT_TYPE,
654 [MBEDTLS_SSL_EXT_ID_SERV_CERT_TYPE] = MBEDTLS_TLS_EXT_SERV_CERT_TYPE,
655 [MBEDTLS_SSL_EXT_ID_PADDING] = MBEDTLS_TLS_EXT_PADDING,
656 [MBEDTLS_SSL_EXT_ID_PRE_SHARED_KEY] = MBEDTLS_TLS_EXT_PRE_SHARED_KEY,
657 [MBEDTLS_SSL_EXT_ID_EARLY_DATA] = MBEDTLS_TLS_EXT_EARLY_DATA,
658 [MBEDTLS_SSL_EXT_ID_SUPPORTED_VERSIONS] = MBEDTLS_TLS_EXT_SUPPORTED_VERSIONS,
659 [MBEDTLS_SSL_EXT_ID_COOKIE] = MBEDTLS_TLS_EXT_COOKIE,
660 [MBEDTLS_SSL_EXT_ID_PSK_KEY_EXCHANGE_MODES] = MBEDTLS_TLS_EXT_PSK_KEY_EXCHANGE_MODES,
661 [MBEDTLS_SSL_EXT_ID_CERT_AUTH] = MBEDTLS_TLS_EXT_CERT_AUTH,
662 [MBEDTLS_SSL_EXT_ID_OID_FILTERS] = MBEDTLS_TLS_EXT_OID_FILTERS,
663 [MBEDTLS_SSL_EXT_ID_POST_HANDSHAKE_AUTH] = MBEDTLS_TLS_EXT_POST_HANDSHAKE_AUTH,
664 [MBEDTLS_SSL_EXT_ID_SIG_ALG_CERT] = MBEDTLS_TLS_EXT_SIG_ALG_CERT,
665 [MBEDTLS_SSL_EXT_ID_KEY_SHARE] = MBEDTLS_TLS_EXT_KEY_SHARE,
666 [MBEDTLS_SSL_EXT_ID_TRUNCATED_HMAC] = MBEDTLS_TLS_EXT_TRUNCATED_HMAC,
667 [MBEDTLS_SSL_EXT_ID_SUPPORTED_POINT_FORMATS] = MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS,
668 [MBEDTLS_SSL_EXT_ID_ENCRYPT_THEN_MAC] = MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC,
669 [MBEDTLS_SSL_EXT_ID_EXTENDED_MASTER_SECRET] = MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET,
Jan Bruckner151f6422023-02-10 12:45:19 +0100670 [MBEDTLS_SSL_EXT_ID_SESSION_TICKET] = MBEDTLS_TLS_EXT_SESSION_TICKET,
671 [MBEDTLS_SSL_EXT_ID_RECORD_SIZE_LIMIT] = MBEDTLS_TLS_EXT_RECORD_SIZE_LIMIT
Jerry Yud25cab02022-10-31 12:48:30 +0800672};
673
Gilles Peskine449bd832023-01-11 14:50:10 +0100674const char *mbedtls_ssl_get_extension_name(unsigned int extension_type)
Jerry Yud25cab02022-10-31 12:48:30 +0800675{
Gilles Peskine449bd832023-01-11 14:50:10 +0100676 return extension_name_table[
677 mbedtls_ssl_get_extension_id(extension_type)];
Jerry Yud25cab02022-10-31 12:48:30 +0800678}
679
Gilles Peskine449bd832023-01-11 14:50:10 +0100680static const char *ssl_tls13_get_hs_msg_name(int hs_msg_type)
Jerry Yud25cab02022-10-31 12:48:30 +0800681{
Gilles Peskine449bd832023-01-11 14:50:10 +0100682 switch (hs_msg_type) {
Jerry Yud25cab02022-10-31 12:48:30 +0800683 case MBEDTLS_SSL_HS_CLIENT_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +0100684 return "ClientHello";
Jerry Yud25cab02022-10-31 12:48:30 +0800685 case MBEDTLS_SSL_HS_SERVER_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +0100686 return "ServerHello";
Jerry Yud25cab02022-10-31 12:48:30 +0800687 case MBEDTLS_SSL_TLS1_3_HS_HELLO_RETRY_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +0100688 return "HelloRetryRequest";
Jerry Yud25cab02022-10-31 12:48:30 +0800689 case MBEDTLS_SSL_HS_NEW_SESSION_TICKET:
Gilles Peskine449bd832023-01-11 14:50:10 +0100690 return "NewSessionTicket";
Jerry Yud25cab02022-10-31 12:48:30 +0800691 case MBEDTLS_SSL_HS_ENCRYPTED_EXTENSIONS:
Gilles Peskine449bd832023-01-11 14:50:10 +0100692 return "EncryptedExtensions";
Jerry Yud25cab02022-10-31 12:48:30 +0800693 case MBEDTLS_SSL_HS_CERTIFICATE:
Gilles Peskine449bd832023-01-11 14:50:10 +0100694 return "Certificate";
Jerry Yud25cab02022-10-31 12:48:30 +0800695 case MBEDTLS_SSL_HS_CERTIFICATE_REQUEST:
Gilles Peskine449bd832023-01-11 14:50:10 +0100696 return "CertificateRequest";
Jerry Yud25cab02022-10-31 12:48:30 +0800697 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100698 return "Unknown";
Jerry Yud25cab02022-10-31 12:48:30 +0800699}
700
Gilles Peskine449bd832023-01-11 14:50:10 +0100701void mbedtls_ssl_print_extension(const mbedtls_ssl_context *ssl,
702 int level, const char *file, int line,
703 int hs_msg_type, unsigned int extension_type,
704 const char *extra_msg0, const char *extra_msg1)
Jerry Yud25cab02022-10-31 12:48:30 +0800705{
706 const char *extra_msg;
Gilles Peskine449bd832023-01-11 14:50:10 +0100707 if (extra_msg0 && extra_msg1) {
Jerry Yud25cab02022-10-31 12:48:30 +0800708 mbedtls_debug_print_msg(
709 ssl, level, file, line,
710 "%s: %s(%u) extension %s %s.",
Gilles Peskine449bd832023-01-11 14:50:10 +0100711 ssl_tls13_get_hs_msg_name(hs_msg_type),
712 mbedtls_ssl_get_extension_name(extension_type),
Jerry Yud25cab02022-10-31 12:48:30 +0800713 extension_type,
Gilles Peskine449bd832023-01-11 14:50:10 +0100714 extra_msg0, extra_msg1);
Jerry Yud25cab02022-10-31 12:48:30 +0800715 return;
716 }
717
718 extra_msg = extra_msg0 ? extra_msg0 : extra_msg1;
Gilles Peskine449bd832023-01-11 14:50:10 +0100719 if (extra_msg) {
Jerry Yud25cab02022-10-31 12:48:30 +0800720 mbedtls_debug_print_msg(
721 ssl, level, file, line,
Gilles Peskine449bd832023-01-11 14:50:10 +0100722 "%s: %s(%u) extension %s.", ssl_tls13_get_hs_msg_name(hs_msg_type),
723 mbedtls_ssl_get_extension_name(extension_type), extension_type,
724 extra_msg);
Jerry Yud25cab02022-10-31 12:48:30 +0800725 return;
726 }
727
728 mbedtls_debug_print_msg(
729 ssl, level, file, line,
Gilles Peskine449bd832023-01-11 14:50:10 +0100730 "%s: %s(%u) extension.", ssl_tls13_get_hs_msg_name(hs_msg_type),
731 mbedtls_ssl_get_extension_name(extension_type), extension_type);
Jerry Yud25cab02022-10-31 12:48:30 +0800732}
733
Gilles Peskine449bd832023-01-11 14:50:10 +0100734void mbedtls_ssl_print_extensions(const mbedtls_ssl_context *ssl,
735 int level, const char *file, int line,
736 int hs_msg_type, uint32_t extensions_mask,
737 const char *extra)
Jerry Yud25cab02022-10-31 12:48:30 +0800738{
739
Gilles Peskine449bd832023-01-11 14:50:10 +0100740 for (unsigned i = 0;
741 i < sizeof(extension_name_table) / sizeof(extension_name_table[0]);
742 i++) {
Jerry Yu79aa7212022-11-08 21:30:21 +0800743 mbedtls_ssl_print_extension(
Jerry Yuea52ed92022-11-08 21:01:17 +0800744 ssl, level, file, line, hs_msg_type, extension_type_table[i],
Gilles Peskine449bd832023-01-11 14:50:10 +0100745 extensions_mask & (1 << i) ? "exists" : "does not exist", extra);
Jerry Yud25cab02022-10-31 12:48:30 +0800746 }
747}
748
Pengyu Lvee455c02023-01-12 14:37:24 +0800749#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS)
750#define ARRAY_LENGTH(a) (sizeof(a) / sizeof(*(a)))
751
752static const char *ticket_flag_name_table[] =
753{
754 [0] = "ALLOW_PSK_RESUMPTION",
755 [2] = "ALLOW_PSK_EPHEMERAL_RESUMPTION",
756 [3] = "ALLOW_EARLY_DATA",
757};
758
Pengyu Lv4938a562023-01-16 11:28:49 +0800759void mbedtls_ssl_print_ticket_flags(const mbedtls_ssl_context *ssl,
760 int level, const char *file, int line,
761 unsigned int flags)
Pengyu Lvee455c02023-01-12 14:37:24 +0800762{
763 size_t i;
764
765 mbedtls_debug_print_msg(ssl, level, file, line,
Pengyu Lv4938a562023-01-16 11:28:49 +0800766 "print ticket_flags (0x%02x)", flags);
767
768 flags = flags & MBEDTLS_SSL_TLS1_3_TICKET_FLAGS_MASK;
Pengyu Lvee455c02023-01-12 14:37:24 +0800769
770 for (i = 0; i < ARRAY_LENGTH(ticket_flag_name_table); i++) {
Pengyu Lv4938a562023-01-16 11:28:49 +0800771 if ((flags & (1 << i))) {
Pengyu Lvee455c02023-01-12 14:37:24 +0800772 mbedtls_debug_print_msg(ssl, level, file, line, "- %s is set.",
773 ticket_flag_name_table[i]);
774 }
775 }
776}
777#endif /* MBEDTLS_SSL_PROTO_TLS1_3 && MBEDTLS_SSL_SESSION_TICKETS */
778
Jerry Yud25cab02022-10-31 12:48:30 +0800779#endif /* MBEDTLS_DEBUG_C */
780
Gilles Peskine449bd832023-01-11 14:50:10 +0100781void mbedtls_ssl_optimize_checksum(mbedtls_ssl_context *ssl,
782 const mbedtls_ssl_ciphersuite_t *ciphersuite_info)
Jerry Yuc73c6182022-02-08 20:29:25 +0800783{
784 ((void) ciphersuite_info);
785
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100786#if defined(MBEDTLS_MD_CAN_SHA384)
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_sha384;
Gilles Peskine449bd832023-01-11 14:50:10 +0100789 } else
Jerry Yuc73c6182022-02-08 20:29:25 +0800790#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100791#if defined(MBEDTLS_MD_CAN_SHA256)
Gilles Peskine449bd832023-01-11 14:50:10 +0100792 if (ciphersuite_info->mac != MBEDTLS_MD_SHA384) {
Jerry Yuc73c6182022-02-08 20:29:25 +0800793 ssl->handshake->update_checksum = ssl_update_checksum_sha256;
Gilles Peskine449bd832023-01-11 14:50:10 +0100794 } else
Jerry Yuc73c6182022-02-08 20:29:25 +0800795#endif
796 {
Gilles Peskine449bd832023-01-11 14:50:10 +0100797 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
Jerry Yuc73c6182022-02-08 20:29:25 +0800798 return;
799 }
800}
801
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100802int mbedtls_ssl_add_hs_hdr_to_checksum(mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100803 unsigned hs_type,
804 size_t total_hs_len)
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100805{
806 unsigned char hs_hdr[4];
807
808 /* Build HS header for checksum update. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100809 hs_hdr[0] = MBEDTLS_BYTE_0(hs_type);
810 hs_hdr[1] = MBEDTLS_BYTE_2(total_hs_len);
811 hs_hdr[2] = MBEDTLS_BYTE_1(total_hs_len);
812 hs_hdr[3] = MBEDTLS_BYTE_0(total_hs_len);
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100813
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100814 return ssl->handshake->update_checksum(ssl, hs_hdr, sizeof(hs_hdr));
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100815}
816
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100817int mbedtls_ssl_add_hs_msg_to_checksum(mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100818 unsigned hs_type,
819 unsigned char const *msg,
820 size_t msg_len)
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100821{
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100822 int ret;
823 ret = mbedtls_ssl_add_hs_hdr_to_checksum(ssl, hs_type, msg_len);
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100824 if (ret != 0) {
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100825 return ret;
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100826 }
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +0100827 return ssl->handshake->update_checksum(ssl, msg, msg_len);
Ronald Cron8f6d39a2022-03-10 18:56:50 +0100828}
829
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100830int mbedtls_ssl_reset_checksum(mbedtls_ssl_context *ssl)
Jerry Yuc73c6182022-02-08 20:29:25 +0800831{
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100832#if defined(MBEDTLS_MD_CAN_SHA256) || \
833 defined(MBEDTLS_MD_CAN_SHA384)
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100834#if defined(MBEDTLS_USE_PSA_CRYPTO)
835 psa_status_t status;
836#else
837 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
838#endif
Manuel Pégourié-Gonnard626aaed2023-02-06 22:03:06 +0100839#else /* SHA-256 or SHA-384 */
Jerry Yuc73c6182022-02-08 20:29:25 +0800840 ((void) ssl);
Manuel Pégourié-Gonnard626aaed2023-02-06 22:03:06 +0100841#endif /* SHA-256 or SHA-384 */
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100842#if defined(MBEDTLS_MD_CAN_SHA256)
Jerry Yuc73c6182022-02-08 20:29:25 +0800843#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100844 status = psa_hash_abort(&ssl->handshake->fin_sha256_psa);
845 if (status != PSA_SUCCESS) {
Andrzej Kurekdaf5b562023-03-03 05:52:28 -0500846 return PSA_TO_MD_ERR(status);
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100847 }
848 status = psa_hash_setup(&ssl->handshake->fin_sha256_psa, PSA_ALG_SHA_256);
849 if (status != PSA_SUCCESS) {
Andrzej Kurekdaf5b562023-03-03 05:52:28 -0500850 return PSA_TO_MD_ERR(status);
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100851 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800852#else
Manuel Pégourié-Gonnard947cee12023-03-06 11:59:59 +0100853 mbedtls_md_free(&ssl->handshake->fin_sha256);
854 mbedtls_md_init(&ssl->handshake->fin_sha256);
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +0100855 ret = mbedtls_md_setup(&ssl->handshake->fin_sha256,
856 mbedtls_md_info_from_type(MBEDTLS_MD_SHA256),
857 0);
858 if (ret != 0) {
859 return ret;
860 }
861 ret = mbedtls_md_starts(&ssl->handshake->fin_sha256);
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100862 if (ret != 0) {
863 return ret;
864 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800865#endif
866#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100867#if defined(MBEDTLS_MD_CAN_SHA384)
Jerry Yuc73c6182022-02-08 20:29:25 +0800868#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100869 status = psa_hash_abort(&ssl->handshake->fin_sha384_psa);
870 if (status != PSA_SUCCESS) {
Andrzej Kurekdaf5b562023-03-03 05:52:28 -0500871 return PSA_TO_MD_ERR(status);
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100872 }
873 status = psa_hash_setup(&ssl->handshake->fin_sha384_psa, PSA_ALG_SHA_384);
874 if (status != PSA_SUCCESS) {
Andrzej Kurekdaf5b562023-03-03 05:52:28 -0500875 return PSA_TO_MD_ERR(status);
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100876 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800877#else
Manuel Pégourié-Gonnard947cee12023-03-06 11:59:59 +0100878 mbedtls_md_free(&ssl->handshake->fin_sha384);
879 mbedtls_md_init(&ssl->handshake->fin_sha384);
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +0100880 ret = mbedtls_md_setup(&ssl->handshake->fin_sha384,
881 mbedtls_md_info_from_type(MBEDTLS_MD_SHA384), 0);
882 if (ret != 0) {
883 return ret;
884 }
885 ret = mbedtls_md_starts(&ssl->handshake->fin_sha384);
Manuel Pégourié-Gonnardb72ff492023-02-06 09:54:49 +0100886 if (ret != 0) {
887 return ret;
888 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800889#endif
890#endif
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100891 return 0;
Jerry Yuc73c6182022-02-08 20:29:25 +0800892}
893
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100894static int ssl_update_checksum_start(mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100895 const unsigned char *buf, size_t len)
Jerry Yuc73c6182022-02-08 20:29:25 +0800896{
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100897#if defined(MBEDTLS_MD_CAN_SHA256) || \
898 defined(MBEDTLS_MD_CAN_SHA384)
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100899#if defined(MBEDTLS_USE_PSA_CRYPTO)
900 psa_status_t status;
901#else
902 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
903#endif
Manuel Pégourié-Gonnard626aaed2023-02-06 22:03:06 +0100904#else /* SHA-256 or SHA-384 */
905 ((void) ssl);
906 (void) buf;
907 (void) len;
908#endif /* SHA-256 or SHA-384 */
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100909#if defined(MBEDTLS_MD_CAN_SHA256)
Jerry Yuc73c6182022-02-08 20:29:25 +0800910#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100911 status = psa_hash_update(&ssl->handshake->fin_sha256_psa, buf, len);
912 if (status != PSA_SUCCESS) {
Andrzej Kurekdaf5b562023-03-03 05:52:28 -0500913 return PSA_TO_MD_ERR(status);
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100914 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800915#else
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +0100916 ret = mbedtls_md_update(&ssl->handshake->fin_sha256, buf, len);
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100917 if (ret != 0) {
918 return ret;
919 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800920#endif
921#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100922#if defined(MBEDTLS_MD_CAN_SHA384)
Jerry Yuc73c6182022-02-08 20:29:25 +0800923#if defined(MBEDTLS_USE_PSA_CRYPTO)
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100924 status = psa_hash_update(&ssl->handshake->fin_sha384_psa, buf, len);
925 if (status != PSA_SUCCESS) {
Andrzej Kurekdaf5b562023-03-03 05:52:28 -0500926 return PSA_TO_MD_ERR(status);
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100927 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800928#else
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +0100929 ret = mbedtls_md_update(&ssl->handshake->fin_sha384, buf, len);
Manuel Pégourié-Gonnarddf949012023-02-06 10:00:52 +0100930 if (ret != 0) {
931 return ret;
932 }
Jerry Yuc73c6182022-02-08 20:29:25 +0800933#endif
934#endif
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100935 return 0;
Jerry Yuc73c6182022-02-08 20:29:25 +0800936}
937
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100938#if defined(MBEDTLS_MD_CAN_SHA256)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100939static int ssl_update_checksum_sha256(mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100940 const unsigned char *buf, size_t len)
Jerry Yuc73c6182022-02-08 20:29:25 +0800941{
942#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurekdaf5b562023-03-03 05:52:28 -0500943 return PSA_TO_MD_ERR(psa_hash_update(
944 &ssl->handshake->fin_sha256_psa, buf, len));
Jerry Yuc73c6182022-02-08 20:29:25 +0800945#else
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +0100946 return mbedtls_md_update(&ssl->handshake->fin_sha256, buf, len);
Jerry Yuc73c6182022-02-08 20:29:25 +0800947#endif
948}
949#endif
950
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100951#if defined(MBEDTLS_MD_CAN_SHA384)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +0100952static int ssl_update_checksum_sha384(mbedtls_ssl_context *ssl,
Manuel Pégourié-Gonnard43cc1272023-02-06 11:48:19 +0100953 const unsigned char *buf, size_t len)
Jerry Yuc73c6182022-02-08 20:29:25 +0800954{
955#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurekdaf5b562023-03-03 05:52:28 -0500956 return PSA_TO_MD_ERR(psa_hash_update(
957 &ssl->handshake->fin_sha384_psa, buf, len));
Jerry Yuc73c6182022-02-08 20:29:25 +0800958#else
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +0100959 return mbedtls_md_update(&ssl->handshake->fin_sha384, buf, len);
Jerry Yuc73c6182022-02-08 20:29:25 +0800960#endif
961}
962#endif
963
Gilles Peskine449bd832023-01-11 14:50:10 +0100964static void ssl_handshake_params_init(mbedtls_ssl_handshake_params *handshake)
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200965{
Gilles Peskine449bd832023-01-11 14:50:10 +0100966 memset(handshake, 0, sizeof(mbedtls_ssl_handshake_params));
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200967
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100968#if defined(MBEDTLS_MD_CAN_SHA256)
Andrzej Kurekeb342242019-01-29 09:14:33 -0500969#if defined(MBEDTLS_USE_PSA_CRYPTO)
970 handshake->fin_sha256_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -0500971#else
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +0100972 mbedtls_md_init(&handshake->fin_sha256);
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200973#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -0500974#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +0100975#if defined(MBEDTLS_MD_CAN_SHA384)
Andrzej Kurekeb342242019-01-29 09:14:33 -0500976#if defined(MBEDTLS_USE_PSA_CRYPTO)
Andrzej Kurek972fba52019-01-30 03:29:12 -0500977 handshake->fin_sha384_psa = psa_hash_operation_init();
Andrzej Kurekeb342242019-01-29 09:14:33 -0500978#else
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +0100979 mbedtls_md_init(&handshake->fin_sha384);
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200980#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -0500981#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200982
983 handshake->update_checksum = ssl_update_checksum_start;
Hanno Becker7e5437a2017-04-28 17:15:26 +0100984
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200985#if defined(MBEDTLS_DHM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100986 mbedtls_dhm_init(&handshake->dhm_ctx);
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200987#endif
Neil Armstrongf3f46412022-04-12 14:43:39 +0200988#if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C)
Gilles Peskine449bd832023-01-11 14:50:10 +0100989 mbedtls_ecdh_init(&handshake->ecdh_ctx);
Paul Bakkeraccaffe2014-06-26 13:37:14 +0200990#endif
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +0200991#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Neil Armstrongca7d5062022-05-31 14:43:23 +0200992#if defined(MBEDTLS_USE_PSA_CRYPTO)
993 handshake->psa_pake_ctx = psa_pake_operation_init();
994 handshake->psa_pake_password = MBEDTLS_SVC_KEY_ID_INIT;
995#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100996 mbedtls_ecjpake_init(&handshake->ecjpake_ctx);
Neil Armstrongca7d5062022-05-31 14:43:23 +0200997#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +0200998#if defined(MBEDTLS_SSL_CLI_C)
999 handshake->ecjpake_cache = NULL;
1000 handshake->ecjpake_cache_len = 0;
1001#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02001002#endif
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001003
Gilles Peskineeccd8882020-03-10 12:19:08 +01001004#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001005 mbedtls_x509_crt_restart_init(&handshake->ecrs_ctx);
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02001006#endif
1007
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001008#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
1009 handshake->sni_authmode = MBEDTLS_SSL_VERIFY_UNSET;
1010#endif
Hanno Becker75173122019-02-06 16:18:31 +00001011
1012#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
1013 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01001014 mbedtls_pk_init(&handshake->peer_pubkey);
Hanno Becker75173122019-02-06 16:18:31 +00001015#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001016}
1017
Gilles Peskine449bd832023-01-11 14:50:10 +01001018void mbedtls_ssl_transform_init(mbedtls_ssl_transform *transform)
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001019{
Gilles Peskine449bd832023-01-11 14:50:10 +01001020 memset(transform, 0, sizeof(mbedtls_ssl_transform));
Paul Bakker84bbeb52014-07-01 14:53:22 +02001021
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +01001022#if defined(MBEDTLS_USE_PSA_CRYPTO)
1023 transform->psa_key_enc = MBEDTLS_SVC_KEY_ID_INIT;
1024 transform->psa_key_dec = MBEDTLS_SVC_KEY_ID_INIT;
Przemyslaw Stekiel6be9cf52022-01-19 16:00:22 +01001025#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001026 mbedtls_cipher_init(&transform->cipher_ctx_enc);
1027 mbedtls_cipher_init(&transform->cipher_ctx_dec);
Przemyslaw Stekiel8f80fb92022-01-11 08:28:13 +01001028#endif
1029
Hanno Beckerfd86ca82020-11-30 08:54:23 +00001030#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Neil Armstrong39b8e7d2022-02-23 09:24:45 +01001031#if defined(MBEDTLS_USE_PSA_CRYPTO)
1032 transform->psa_mac_enc = MBEDTLS_SVC_KEY_ID_INIT;
1033 transform->psa_mac_dec = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrongcf8841a2022-02-24 11:17:45 +01001034#else
Gilles Peskine449bd832023-01-11 14:50:10 +01001035 mbedtls_md_init(&transform->md_ctx_enc);
1036 mbedtls_md_init(&transform->md_ctx_dec);
Hanno Beckerd56ed242018-01-03 15:32:51 +00001037#endif
Neil Armstrongcf8841a2022-02-24 11:17:45 +01001038#endif
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001039}
1040
Gilles Peskine449bd832023-01-11 14:50:10 +01001041void mbedtls_ssl_session_init(mbedtls_ssl_session *session)
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001042{
Gilles Peskine449bd832023-01-11 14:50:10 +01001043 memset(session, 0, sizeof(mbedtls_ssl_session));
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001044}
1045
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001046MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001047static int ssl_handshake_init(mbedtls_ssl_context *ssl)
Paul Bakker48916f92012-09-16 19:57:18 +00001048{
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001049 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1050
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001051 /* Clear old handshake information if present */
Jerry Yu2e199812022-12-01 18:57:19 +08001052#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01001053 if (ssl->transform_negotiate) {
1054 mbedtls_ssl_transform_free(ssl->transform_negotiate);
1055 }
Jerry Yu2e199812022-12-01 18:57:19 +08001056#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001057 if (ssl->session_negotiate) {
1058 mbedtls_ssl_session_free(ssl->session_negotiate);
1059 }
1060 if (ssl->handshake) {
1061 mbedtls_ssl_handshake_free(ssl);
1062 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001063
Jerry Yu2e199812022-12-01 18:57:19 +08001064#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001065 /*
1066 * Either the pointers are now NULL or cleared properly and can be freed.
1067 * Now allocate missing structures.
1068 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001069 if (ssl->transform_negotiate == NULL) {
1070 ssl->transform_negotiate = mbedtls_calloc(1, sizeof(mbedtls_ssl_transform));
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001071 }
Jerry Yu2e199812022-12-01 18:57:19 +08001072#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Paul Bakker48916f92012-09-16 19:57:18 +00001073
Gilles Peskine449bd832023-01-11 14:50:10 +01001074 if (ssl->session_negotiate == NULL) {
1075 ssl->session_negotiate = mbedtls_calloc(1, sizeof(mbedtls_ssl_session));
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001076 }
Paul Bakker48916f92012-09-16 19:57:18 +00001077
Gilles Peskine449bd832023-01-11 14:50:10 +01001078 if (ssl->handshake == NULL) {
1079 ssl->handshake = mbedtls_calloc(1, sizeof(mbedtls_ssl_handshake_params));
Paul Bakkerb9cfaa02013-10-11 18:58:55 +02001080 }
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05001081#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1082 /* If the buffers are too small - reallocate */
Andrzej Kurek8ea68722020-04-03 06:40:47 -04001083
Gilles Peskine449bd832023-01-11 14:50:10 +01001084 handle_buffer_resizing(ssl, 0, MBEDTLS_SSL_IN_BUFFER_LEN,
1085 MBEDTLS_SSL_OUT_BUFFER_LEN);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05001086#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001087
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001088 /* All pointers should exist and can be directly freed without issue */
Gilles Peskine449bd832023-01-11 14:50:10 +01001089 if (ssl->handshake == NULL ||
Jerry Yu2e199812022-12-01 18:57:19 +08001090#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Paul Bakker48916f92012-09-16 19:57:18 +00001091 ssl->transform_negotiate == NULL ||
Jerry Yu2e199812022-12-01 18:57:19 +08001092#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001093 ssl->session_negotiate == NULL) {
1094 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc() of ssl sub-contexts failed"));
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001095
Gilles Peskine449bd832023-01-11 14:50:10 +01001096 mbedtls_free(ssl->handshake);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001097 ssl->handshake = NULL;
Jerry Yu2e199812022-12-01 18:57:19 +08001098
1099#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01001100 mbedtls_free(ssl->transform_negotiate);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001101 ssl->transform_negotiate = NULL;
Jerry Yu2e199812022-12-01 18:57:19 +08001102#endif
1103
Gilles Peskine449bd832023-01-11 14:50:10 +01001104 mbedtls_free(ssl->session_negotiate);
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001105 ssl->session_negotiate = NULL;
1106
Gilles Peskine449bd832023-01-11 14:50:10 +01001107 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Paul Bakker48916f92012-09-16 19:57:18 +00001108 }
1109
Paul Bakkeraccaffe2014-06-26 13:37:14 +02001110 /* Initialize structures */
Gilles Peskine449bd832023-01-11 14:50:10 +01001111 mbedtls_ssl_session_init(ssl->session_negotiate);
1112 ssl_handshake_params_init(ssl->handshake);
Paul Bakker968afaa2014-07-09 11:09:24 +02001113
Jerry Yu2e199812022-12-01 18:57:19 +08001114#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01001115 mbedtls_ssl_transform_init(ssl->transform_negotiate);
Jerry Yu2e199812022-12-01 18:57:19 +08001116#endif
1117
Manuel Pégourié-Gonnard537f2312023-02-05 10:17:45 +01001118 /* Setup handshake checksums */
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01001119 ret = mbedtls_ssl_reset_checksum(ssl);
1120 if (ret != 0) {
1121 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_reset_checksum", ret);
1122 return ret;
1123 }
Manuel Pégourié-Gonnard537f2312023-02-05 10:17:45 +01001124
Jerry Yud0766ec2022-09-22 10:46:57 +08001125#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
1126 defined(MBEDTLS_SSL_SRV_C) && \
1127 defined(MBEDTLS_SSL_SESSION_TICKETS)
1128 ssl->handshake->new_session_tickets_count =
Gilles Peskine449bd832023-01-11 14:50:10 +01001129 ssl->conf->new_session_tickets_count;
Jerry Yud0766ec2022-09-22 10:46:57 +08001130#endif
1131
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001132#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001133 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001134 ssl->handshake->alt_transform_out = ssl->transform_out;
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001135
Gilles Peskine449bd832023-01-11 14:50:10 +01001136 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001137 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_PREPARING;
Gilles Peskine449bd832023-01-11 14:50:10 +01001138 } else {
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001139 ssl->handshake->retransmit_state = MBEDTLS_SSL_RETRANS_WAITING;
Gilles Peskine449bd832023-01-11 14:50:10 +01001140 }
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001141
Gilles Peskine449bd832023-01-11 14:50:10 +01001142 mbedtls_ssl_set_timer(ssl, 0);
Manuel Pégourié-Gonnard06939ce2015-05-11 11:25:46 +02001143 }
Manuel Pégourié-Gonnard5d8ba532014-09-19 15:09:21 +02001144#endif
1145
Brett Warrene0edc842021-08-17 09:53:13 +01001146/*
1147 * curve_list is translated to IANA TLS group identifiers here because
1148 * mbedtls_ssl_conf_curves returns void and so can't return
1149 * any error codes.
1150 */
1151#if defined(MBEDTLS_ECP_C)
1152#if !defined(MBEDTLS_DEPRECATED_REMOVED)
1153 /* Heap allocate and translate curve_list from internal to IANA group ids */
Gilles Peskine449bd832023-01-11 14:50:10 +01001154 if (ssl->conf->curve_list != NULL) {
Brett Warrene0edc842021-08-17 09:53:13 +01001155 size_t length;
1156 const mbedtls_ecp_group_id *curve_list = ssl->conf->curve_list;
1157
Gilles Peskine449bd832023-01-11 14:50:10 +01001158 for (length = 0; (curve_list[length] != MBEDTLS_ECP_DP_NONE) &&
1159 (length < MBEDTLS_ECP_DP_MAX); length++) {
1160 }
Brett Warrene0edc842021-08-17 09:53:13 +01001161
1162 /* Leave room for zero termination */
Gilles Peskine449bd832023-01-11 14:50:10 +01001163 uint16_t *group_list = mbedtls_calloc(length + 1, sizeof(uint16_t));
1164 if (group_list == NULL) {
1165 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
1166 }
Brett Warrene0edc842021-08-17 09:53:13 +01001167
Gilles Peskine449bd832023-01-11 14:50:10 +01001168 for (size_t i = 0; i < length; i++) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01001169 uint16_t tls_id = mbedtls_ssl_get_tls_id_from_ecp_group_id(
Gilles Peskine449bd832023-01-11 14:50:10 +01001170 curve_list[i]);
1171 if (tls_id == 0) {
1172 mbedtls_free(group_list);
1173 return MBEDTLS_ERR_SSL_BAD_CONFIG;
Brett Warrene0edc842021-08-17 09:53:13 +01001174 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01001175 group_list[i] = tls_id;
Brett Warrene0edc842021-08-17 09:53:13 +01001176 }
1177
1178 group_list[length] = 0;
1179
1180 ssl->handshake->group_list = group_list;
1181 ssl->handshake->group_list_heap_allocated = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001182 } else {
Brett Warrene0edc842021-08-17 09:53:13 +01001183 ssl->handshake->group_list = ssl->conf->group_list;
1184 ssl->handshake->group_list_heap_allocated = 0;
1185 }
1186#endif /* MBEDTLS_DEPRECATED_REMOVED */
1187#endif /* MBEDTLS_ECP_C */
1188
Ronald Crone68ab4f2022-10-05 12:46:29 +02001189#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +08001190#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Jerry Yua69269a2022-01-17 21:06:01 +08001191#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Jerry Yu713013f2022-01-17 18:16:35 +08001192 /* Heap allocate and translate sig_hashes from internal hash identifiers to
1193 signature algorithms IANA identifiers. */
Gilles Peskine449bd832023-01-11 14:50:10 +01001194 if (mbedtls_ssl_conf_is_tls12_only(ssl->conf) &&
1195 ssl->conf->sig_hashes != NULL) {
Jerry Yuf017ee42022-01-12 15:49:48 +08001196 const int *md;
1197 const int *sig_hashes = ssl->conf->sig_hashes;
Jerry Yub476a442022-01-21 18:14:45 +08001198 size_t sig_algs_len = 0;
Jerry Yuf017ee42022-01-12 15:49:48 +08001199 uint16_t *p;
1200
Tom Cosgrove6ef9bb32023-03-08 14:19:51 +00001201 MBEDTLS_STATIC_ASSERT(MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN
1202 <= (SIZE_MAX - (2 * sizeof(uint16_t))),
1203 "MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN too big");
Jerry Yua68dca22022-01-20 16:28:27 +08001204
Gilles Peskine449bd832023-01-11 14:50:10 +01001205 for (md = sig_hashes; *md != MBEDTLS_MD_NONE; md++) {
1206 if (mbedtls_ssl_hash_from_md_alg(*md) == MBEDTLS_SSL_HASH_NONE) {
Jerry Yuf017ee42022-01-12 15:49:48 +08001207 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001208 }
Valerio Setti75fba322023-02-23 17:35:09 +01001209#if defined(MBEDTLS_PK_CAN_ECDSA_SOME)
Gilles Peskine449bd832023-01-11 14:50:10 +01001210 sig_algs_len += sizeof(uint16_t);
Jerry Yub476a442022-01-21 18:14:45 +08001211#endif
Jerry Yua68dca22022-01-20 16:28:27 +08001212
Jerry Yub476a442022-01-21 18:14:45 +08001213#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001214 sig_algs_len += sizeof(uint16_t);
Jerry Yub476a442022-01-21 18:14:45 +08001215#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001216 if (sig_algs_len > MBEDTLS_SSL_MAX_SIG_ALG_LIST_LEN) {
1217 return MBEDTLS_ERR_SSL_BAD_CONFIG;
1218 }
Jerry Yuf017ee42022-01-12 15:49:48 +08001219 }
1220
Gilles Peskine449bd832023-01-11 14:50:10 +01001221 if (sig_algs_len < MBEDTLS_SSL_MIN_SIG_ALG_LIST_LEN) {
1222 return MBEDTLS_ERR_SSL_BAD_CONFIG;
1223 }
Jerry Yuf017ee42022-01-12 15:49:48 +08001224
Gilles Peskine449bd832023-01-11 14:50:10 +01001225 ssl->handshake->sig_algs = mbedtls_calloc(1, sig_algs_len +
1226 sizeof(uint16_t));
1227 if (ssl->handshake->sig_algs == NULL) {
1228 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
1229 }
Jerry Yuf017ee42022-01-12 15:49:48 +08001230
Gilles Peskine449bd832023-01-11 14:50:10 +01001231 p = (uint16_t *) ssl->handshake->sig_algs;
1232 for (md = sig_hashes; *md != MBEDTLS_MD_NONE; md++) {
1233 unsigned char hash = mbedtls_ssl_hash_from_md_alg(*md);
1234 if (hash == MBEDTLS_SSL_HASH_NONE) {
Jerry Yuf017ee42022-01-12 15:49:48 +08001235 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01001236 }
Valerio Setti75fba322023-02-23 17:35:09 +01001237#if defined(MBEDTLS_PK_CAN_ECDSA_SOME)
Gilles Peskine449bd832023-01-11 14:50:10 +01001238 *p = ((hash << 8) | MBEDTLS_SSL_SIG_ECDSA);
Jerry Yuf017ee42022-01-12 15:49:48 +08001239 p++;
Jerry Yu6106fdc2022-01-12 16:36:14 +08001240#endif
1241#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001242 *p = ((hash << 8) | MBEDTLS_SSL_SIG_RSA);
Jerry Yuf017ee42022-01-12 15:49:48 +08001243 p++;
Jerry Yu6106fdc2022-01-12 16:36:14 +08001244#endif
Jerry Yuf017ee42022-01-12 15:49:48 +08001245 }
Gabor Mezei15b95a62022-05-09 16:37:58 +02001246 *p = MBEDTLS_TLS_SIG_NONE;
Jerry Yuf017ee42022-01-12 15:49:48 +08001247 ssl->handshake->sig_algs_heap_allocated = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01001248 } else
Jerry Yua69269a2022-01-17 21:06:01 +08001249#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Jerry Yuf017ee42022-01-12 15:49:48 +08001250 {
Jerry Yuf017ee42022-01-12 15:49:48 +08001251 ssl->handshake->sig_algs_heap_allocated = 0;
1252 }
Jerry Yucc539102022-06-27 16:27:35 +08001253#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Ronald Crone68ab4f2022-10-05 12:46:29 +02001254#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Gilles Peskine449bd832023-01-11 14:50:10 +01001255 return 0;
Paul Bakker48916f92012-09-16 19:57:18 +00001256}
1257
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001258#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001259/* Dummy cookie callbacks for defaults */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001260MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001261static int ssl_cookie_write_dummy(void *ctx,
1262 unsigned char **p, unsigned char *end,
1263 const unsigned char *cli_id, size_t cli_id_len)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001264{
1265 ((void) ctx);
1266 ((void) p);
1267 ((void) end);
1268 ((void) cli_id);
1269 ((void) cli_id_len);
1270
Gilles Peskine449bd832023-01-11 14:50:10 +01001271 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001272}
1273
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001274MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001275static int ssl_cookie_check_dummy(void *ctx,
1276 const unsigned char *cookie, size_t cookie_len,
1277 const unsigned char *cli_id, size_t cli_id_len)
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001278{
1279 ((void) ctx);
1280 ((void) cookie);
1281 ((void) cookie_len);
1282 ((void) cli_id);
1283 ((void) cli_id_len);
1284
Gilles Peskine449bd832023-01-11 14:50:10 +01001285 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001286}
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001287#endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard7d38d212014-07-23 17:52:09 +02001288
Paul Bakker5121ce52009-01-03 21:22:43 +00001289/*
1290 * Initialize an SSL context
1291 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001292void mbedtls_ssl_init(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001293{
Gilles Peskine449bd832023-01-11 14:50:10 +01001294 memset(ssl, 0, sizeof(mbedtls_ssl_context));
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001295}
1296
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001297MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001298static int ssl_conf_version_check(const mbedtls_ssl_context *ssl)
Jerry Yu60835a82021-08-04 10:13:52 +08001299{
Ronald Cron086ee0b2022-03-15 15:18:51 +01001300 const mbedtls_ssl_config *conf = ssl->conf;
1301
Ronald Cron6f135e12021-12-08 16:57:54 +01001302#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001303 if (mbedtls_ssl_conf_is_tls13_only(conf)) {
1304 if (conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
1305 MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS 1.3 is not yet supported."));
1306 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQianed582dd2022-04-13 08:21:05 +00001307 }
1308
Gilles Peskine449bd832023-01-11 14:50:10 +01001309 MBEDTLS_SSL_DEBUG_MSG(4, ("The SSL configuration is tls13 only."));
1310 return 0;
Jerry Yu60835a82021-08-04 10:13:52 +08001311 }
1312#endif
1313
1314#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01001315 if (mbedtls_ssl_conf_is_tls12_only(conf)) {
1316 MBEDTLS_SSL_DEBUG_MSG(4, ("The SSL configuration is tls12 only."));
1317 return 0;
Jerry Yu60835a82021-08-04 10:13:52 +08001318 }
1319#endif
1320
Ronald Cron6f135e12021-12-08 16:57:54 +01001321#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001322 if (mbedtls_ssl_conf_is_hybrid_tls12_tls13(conf)) {
1323 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
1324 MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS not yet supported in Hybrid TLS 1.3 + TLS 1.2"));
1325 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQianed582dd2022-04-13 08:21:05 +00001326 }
1327
Gilles Peskine449bd832023-01-11 14:50:10 +01001328 if (conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
1329 MBEDTLS_SSL_DEBUG_MSG(1, ("TLS 1.3 server is not supported yet."));
1330 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQian8f9dfe42022-04-15 02:52:39 +00001331 }
1332
1333
Gilles Peskine449bd832023-01-11 14:50:10 +01001334 MBEDTLS_SSL_DEBUG_MSG(4, ("The SSL configuration is TLS 1.3 or TLS 1.2."));
1335 return 0;
Jerry Yu60835a82021-08-04 10:13:52 +08001336 }
1337#endif
1338
Gilles Peskine449bd832023-01-11 14:50:10 +01001339 MBEDTLS_SSL_DEBUG_MSG(1, ("The SSL configuration is invalid."));
1340 return MBEDTLS_ERR_SSL_BAD_CONFIG;
Jerry Yu60835a82021-08-04 10:13:52 +08001341}
1342
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001343MBEDTLS_CHECK_RETURN_CRITICAL
Jerry Yu60835a82021-08-04 10:13:52 +08001344static int ssl_conf_check(const mbedtls_ssl_context *ssl)
1345{
1346 int ret;
Gilles Peskine449bd832023-01-11 14:50:10 +01001347 ret = ssl_conf_version_check(ssl);
1348 if (ret != 0) {
1349 return ret;
1350 }
Jerry Yu60835a82021-08-04 10:13:52 +08001351
Jerry Yudef7ae42022-10-30 14:13:19 +08001352#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
1353 /* RFC 8446 section 4.4.3
1354 *
1355 * If the verification fails, the receiver MUST terminate the handshake with
1356 * a "decrypt_error" alert.
1357 *
1358 * If the client is configured as TLS 1.3 only with optional verify, return
1359 * bad config.
1360 *
1361 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001362 if (mbedtls_ssl_conf_tls13_ephemeral_enabled(
1363 (mbedtls_ssl_context *) ssl) &&
Jerry Yudef7ae42022-10-30 14:13:19 +08001364 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
1365 ssl->conf->max_tls_version == MBEDTLS_SSL_VERSION_TLS1_3 &&
1366 ssl->conf->min_tls_version == MBEDTLS_SSL_VERSION_TLS1_3 &&
Gilles Peskine449bd832023-01-11 14:50:10 +01001367 ssl->conf->authmode == MBEDTLS_SSL_VERIFY_OPTIONAL) {
Jerry Yudef7ae42022-10-30 14:13:19 +08001368 MBEDTLS_SSL_DEBUG_MSG(
Gilles Peskine449bd832023-01-11 14:50:10 +01001369 1, ("Optional verify auth mode "
1370 "is not available for TLS 1.3 client"));
1371 return MBEDTLS_ERR_SSL_BAD_CONFIG;
Jerry Yudef7ae42022-10-30 14:13:19 +08001372 }
1373#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
1374
Jerry Yu60835a82021-08-04 10:13:52 +08001375 /* Space for further checks */
1376
Gilles Peskine449bd832023-01-11 14:50:10 +01001377 return 0;
Jerry Yu60835a82021-08-04 10:13:52 +08001378}
1379
Manuel Pégourié-Gonnard41d479e2015-04-29 00:48:22 +02001380/*
1381 * Setup an SSL context
1382 */
Hanno Becker2a43f6f2018-08-10 11:12:52 +01001383
Gilles Peskine449bd832023-01-11 14:50:10 +01001384int mbedtls_ssl_setup(mbedtls_ssl_context *ssl,
1385 const mbedtls_ssl_config *conf)
Paul Bakker5121ce52009-01-03 21:22:43 +00001386{
Janos Follath865b3eb2019-12-16 11:46:15 +00001387 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Darryl Greenb33cc762019-11-28 14:29:44 +00001388 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1389 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
Paul Bakker5121ce52009-01-03 21:22:43 +00001390
Manuel Pégourié-Gonnarddef0bbe2015-05-04 14:56:36 +02001391 ssl->conf = conf;
Paul Bakker62f2dee2012-09-28 07:31:51 +00001392
Gilles Peskine449bd832023-01-11 14:50:10 +01001393 if ((ret = ssl_conf_check(ssl)) != 0) {
1394 return ret;
1395 }
Ronald Cron8a12aee2023-03-08 15:30:43 +01001396 ssl->tls_version = ssl->conf->max_tls_version;
Jerry Yu60835a82021-08-04 10:13:52 +08001397
Paul Bakker62f2dee2012-09-28 07:31:51 +00001398 /*
Manuel Pégourié-Gonnard06193482014-02-14 08:39:32 +01001399 * Prepare base structures
Paul Bakker62f2dee2012-09-28 07:31:51 +00001400 */
k-stachowiakc9a5f022018-07-24 13:53:31 +02001401
1402 /* Set to NULL in case of an error condition */
1403 ssl->out_buf = NULL;
k-stachowiaka47911c2018-07-04 17:41:58 +02001404
Darryl Greenb33cc762019-11-28 14:29:44 +00001405#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1406 ssl->in_buf_len = in_buf_len;
1407#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001408 ssl->in_buf = mbedtls_calloc(1, in_buf_len);
1409 if (ssl->in_buf == NULL) {
1410 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", in_buf_len));
k-stachowiak9f7798e2018-07-31 16:52:32 +02001411 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02001412 goto error;
Angus Grattond8213d02016-05-25 20:56:48 +10001413 }
1414
Darryl Greenb33cc762019-11-28 14:29:44 +00001415#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1416 ssl->out_buf_len = out_buf_len;
1417#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001418 ssl->out_buf = mbedtls_calloc(1, out_buf_len);
1419 if (ssl->out_buf == NULL) {
1420 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed", out_buf_len));
k-stachowiak9f7798e2018-07-31 16:52:32 +02001421 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
k-stachowiaka47911c2018-07-04 17:41:58 +02001422 goto error;
Paul Bakker5121ce52009-01-03 21:22:43 +00001423 }
1424
Gilles Peskine449bd832023-01-11 14:50:10 +01001425 mbedtls_ssl_reset_in_out_pointers(ssl);
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02001426
Johan Pascalb62bb512015-12-03 21:56:45 +01001427#if defined(MBEDTLS_SSL_DTLS_SRTP)
Gilles Peskine449bd832023-01-11 14:50:10 +01001428 memset(&ssl->dtls_srtp_info, 0, sizeof(ssl->dtls_srtp_info));
Johan Pascalb62bb512015-12-03 21:56:45 +01001429#endif
1430
Gilles Peskine449bd832023-01-11 14:50:10 +01001431 if ((ret = ssl_handshake_init(ssl)) != 0) {
k-stachowiaka47911c2018-07-04 17:41:58 +02001432 goto error;
Gilles Peskine449bd832023-01-11 14:50:10 +01001433 }
Paul Bakker5121ce52009-01-03 21:22:43 +00001434
Gilles Peskine449bd832023-01-11 14:50:10 +01001435 return 0;
k-stachowiaka47911c2018-07-04 17:41:58 +02001436
1437error:
Gilles Peskine449bd832023-01-11 14:50:10 +01001438 mbedtls_free(ssl->in_buf);
1439 mbedtls_free(ssl->out_buf);
k-stachowiaka47911c2018-07-04 17:41:58 +02001440
1441 ssl->conf = NULL;
1442
Darryl Greenb33cc762019-11-28 14:29:44 +00001443#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1444 ssl->in_buf_len = 0;
1445 ssl->out_buf_len = 0;
1446#endif
k-stachowiaka47911c2018-07-04 17:41:58 +02001447 ssl->in_buf = NULL;
1448 ssl->out_buf = NULL;
1449
1450 ssl->in_hdr = NULL;
1451 ssl->in_ctr = NULL;
1452 ssl->in_len = NULL;
1453 ssl->in_iv = NULL;
1454 ssl->in_msg = NULL;
1455
1456 ssl->out_hdr = NULL;
1457 ssl->out_ctr = NULL;
1458 ssl->out_len = NULL;
1459 ssl->out_iv = NULL;
1460 ssl->out_msg = NULL;
1461
Gilles Peskine449bd832023-01-11 14:50:10 +01001462 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00001463}
1464
1465/*
Paul Bakker7eb013f2011-10-06 12:37:39 +00001466 * Reset an initialized and used SSL context for re-use while retaining
1467 * all application-set variables, function pointers and data.
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001468 *
1469 * If partial is non-zero, keep data in the input buffer and client ID.
1470 * (Use when a DTLS client reconnects from the same port.)
Paul Bakker7eb013f2011-10-06 12:37:39 +00001471 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001472void mbedtls_ssl_session_reset_msg_layer(mbedtls_ssl_context *ssl,
1473 int partial)
Paul Bakker7eb013f2011-10-06 12:37:39 +00001474{
Darryl Greenb33cc762019-11-28 14:29:44 +00001475#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
1476 size_t in_buf_len = ssl->in_buf_len;
1477 size_t out_buf_len = ssl->out_buf_len;
1478#else
1479 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
1480 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
1481#endif
Paul Bakker48916f92012-09-16 19:57:18 +00001482
Hanno Beckerb0302c42021-08-03 09:39:42 +01001483#if !defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE) || !defined(MBEDTLS_SSL_SRV_C)
1484 partial = 0;
Hanno Becker7e772132018-08-10 12:38:21 +01001485#endif
1486
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001487 /* Cancel any possibly running timer */
Gilles Peskine449bd832023-01-11 14:50:10 +01001488 mbedtls_ssl_set_timer(ssl, 0);
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001489
Gilles Peskine449bd832023-01-11 14:50:10 +01001490 mbedtls_ssl_reset_in_out_pointers(ssl);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001491
1492 /* Reset incoming message parsing */
1493 ssl->in_offt = NULL;
1494 ssl->nb_zero = 0;
1495 ssl->in_msgtype = 0;
1496 ssl->in_msglen = 0;
1497 ssl->in_hslen = 0;
1498 ssl->keep_current_message = 0;
1499 ssl->transform_in = NULL;
1500
1501#if defined(MBEDTLS_SSL_PROTO_DTLS)
1502 ssl->next_record_offset = 0;
1503 ssl->in_epoch = 0;
1504#endif
1505
1506 /* Keep current datagram if partial == 1 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001507 if (partial == 0) {
Hanno Beckerb0302c42021-08-03 09:39:42 +01001508 ssl->in_left = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001509 memset(ssl->in_buf, 0, in_buf_len);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001510 }
1511
Ronald Cronad8c17b2022-06-10 17:18:09 +02001512 ssl->send_alert = 0;
1513
Hanno Beckerb0302c42021-08-03 09:39:42 +01001514 /* Reset outgoing message writing */
1515 ssl->out_msgtype = 0;
1516 ssl->out_msglen = 0;
1517 ssl->out_left = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001518 memset(ssl->out_buf, 0, out_buf_len);
1519 memset(ssl->cur_out_ctr, 0, sizeof(ssl->cur_out_ctr));
Hanno Beckerb0302c42021-08-03 09:39:42 +01001520 ssl->transform_out = NULL;
1521
1522#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine449bd832023-01-11 14:50:10 +01001523 mbedtls_ssl_dtls_replay_reset(ssl);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001524#endif
1525
XiaokangQian2b01dc32022-01-21 02:53:13 +00001526#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01001527 if (ssl->transform) {
1528 mbedtls_ssl_transform_free(ssl->transform);
1529 mbedtls_free(ssl->transform);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001530 ssl->transform = NULL;
1531 }
XiaokangQian2b01dc32022-01-21 02:53:13 +00001532#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
1533
XiaokangQian2b01dc32022-01-21 02:53:13 +00001534#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001535 mbedtls_ssl_transform_free(ssl->transform_application);
1536 mbedtls_free(ssl->transform_application);
XiaokangQian2b01dc32022-01-21 02:53:13 +00001537 ssl->transform_application = NULL;
1538
Gilles Peskine449bd832023-01-11 14:50:10 +01001539 if (ssl->handshake != NULL) {
Jerry Yu3d9b5902022-11-04 14:07:25 +08001540#if defined(MBEDTLS_SSL_EARLY_DATA)
Gilles Peskine449bd832023-01-11 14:50:10 +01001541 mbedtls_ssl_transform_free(ssl->handshake->transform_earlydata);
1542 mbedtls_free(ssl->handshake->transform_earlydata);
XiaokangQian2b01dc32022-01-21 02:53:13 +00001543 ssl->handshake->transform_earlydata = NULL;
Jerry Yu3d9b5902022-11-04 14:07:25 +08001544#endif
XiaokangQian2b01dc32022-01-21 02:53:13 +00001545
Gilles Peskine449bd832023-01-11 14:50:10 +01001546 mbedtls_ssl_transform_free(ssl->handshake->transform_handshake);
1547 mbedtls_free(ssl->handshake->transform_handshake);
XiaokangQian2b01dc32022-01-21 02:53:13 +00001548 ssl->handshake->transform_handshake = NULL;
1549 }
1550
XiaokangQian2b01dc32022-01-21 02:53:13 +00001551#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerb0302c42021-08-03 09:39:42 +01001552}
1553
Gilles Peskine449bd832023-01-11 14:50:10 +01001554int mbedtls_ssl_session_reset_int(mbedtls_ssl_context *ssl, int partial)
Hanno Beckerb0302c42021-08-03 09:39:42 +01001555{
1556 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
1557
1558 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
1559
Gilles Peskine449bd832023-01-11 14:50:10 +01001560 mbedtls_ssl_session_reset_msg_layer(ssl, partial);
Hanno Beckerb0302c42021-08-03 09:39:42 +01001561
1562 /* Reset renegotiation state */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001563#if defined(MBEDTLS_SSL_RENEGOTIATION)
1564 ssl->renego_status = MBEDTLS_SSL_INITIAL_HANDSHAKE;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001565 ssl->renego_records_seen = 0;
Paul Bakker48916f92012-09-16 19:57:18 +00001566
1567 ssl->verify_data_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001568 memset(ssl->own_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN);
1569 memset(ssl->peer_verify_data, 0, MBEDTLS_SSL_VERIFY_DATA_MAX_LEN);
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01001570#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001571 ssl->secure_renegotiation = MBEDTLS_SSL_LEGACY_RENEGOTIATION;
Paul Bakker48916f92012-09-16 19:57:18 +00001572
Hanno Beckerb0302c42021-08-03 09:39:42 +01001573 ssl->session_in = NULL;
Hanno Becker78640902018-08-13 16:35:15 +01001574 ssl->session_out = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001575 if (ssl->session) {
1576 mbedtls_ssl_session_free(ssl->session);
1577 mbedtls_free(ssl->session);
Paul Bakkerc0463502013-02-14 11:19:38 +01001578 ssl->session = NULL;
1579 }
1580
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001581#if defined(MBEDTLS_SSL_ALPN)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02001582 ssl->alpn_chosen = NULL;
1583#endif
1584
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02001585#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
David Horstmann3b2276a2022-10-06 14:49:08 +01001586 int free_cli_id = 1;
Hanno Becker4ccbf062018-08-10 11:20:38 +01001587#if defined(MBEDTLS_SSL_DTLS_CLIENT_PORT_REUSE)
Gilles Peskine449bd832023-01-11 14:50:10 +01001588 free_cli_id = (partial == 0);
Hanno Becker4ccbf062018-08-10 11:20:38 +01001589#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01001590 if (free_cli_id) {
1591 mbedtls_free(ssl->cli_id);
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001592 ssl->cli_id = NULL;
1593 ssl->cli_id_len = 0;
1594 }
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02001595#endif
1596
Gilles Peskine449bd832023-01-11 14:50:10 +01001597 if ((ret = ssl_handshake_init(ssl)) != 0) {
1598 return ret;
1599 }
Paul Bakker2770fbd2012-07-03 13:30:23 +00001600
Gilles Peskine449bd832023-01-11 14:50:10 +01001601 return 0;
Paul Bakker7eb013f2011-10-06 12:37:39 +00001602}
1603
Manuel Pégourié-Gonnard779e4292013-08-03 13:50:48 +02001604/*
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001605 * Reset an initialized and used SSL context for re-use while retaining
1606 * all application-set variables, function pointers and data.
1607 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001608int mbedtls_ssl_session_reset(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001609{
Gilles Peskine449bd832023-01-11 14:50:10 +01001610 return mbedtls_ssl_session_reset_int(ssl, 0);
Manuel Pégourié-Gonnard3f09b6d2015-09-08 11:58:14 +02001611}
1612
1613/*
Paul Bakker5121ce52009-01-03 21:22:43 +00001614 * SSL set accessors
1615 */
Gilles Peskine449bd832023-01-11 14:50:10 +01001616void mbedtls_ssl_conf_endpoint(mbedtls_ssl_config *conf, int endpoint)
Paul Bakker5121ce52009-01-03 21:22:43 +00001617{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001618 conf->endpoint = endpoint;
Paul Bakker5121ce52009-01-03 21:22:43 +00001619}
1620
Gilles Peskine449bd832023-01-11 14:50:10 +01001621void mbedtls_ssl_conf_transport(mbedtls_ssl_config *conf, int transport)
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001622{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001623 conf->transport = transport;
Manuel Pégourié-Gonnard0b1ff292014-02-06 13:04:16 +01001624}
1625
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001626#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine449bd832023-01-11 14:50:10 +01001627void mbedtls_ssl_conf_dtls_anti_replay(mbedtls_ssl_config *conf, char mode)
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001628{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001629 conf->anti_replay = mode;
Manuel Pégourié-Gonnard27393132014-09-24 14:41:11 +02001630}
1631#endif
1632
Gilles Peskine449bd832023-01-11 14:50:10 +01001633void mbedtls_ssl_conf_dtls_badmac_limit(mbedtls_ssl_config *conf, unsigned limit)
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001634{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001635 conf->badmac_limit = limit;
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001636}
Manuel Pégourié-Gonnardb0643d12014-10-14 18:30:36 +02001637
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001638#if defined(MBEDTLS_SSL_PROTO_DTLS)
Hanno Becker04da1892018-08-14 13:22:10 +01001639
Gilles Peskine449bd832023-01-11 14:50:10 +01001640void mbedtls_ssl_set_datagram_packing(mbedtls_ssl_context *ssl,
1641 unsigned allow_packing)
Hanno Becker04da1892018-08-14 13:22:10 +01001642{
1643 ssl->disable_datagram_packing = !allow_packing;
1644}
1645
Gilles Peskine449bd832023-01-11 14:50:10 +01001646void mbedtls_ssl_conf_handshake_timeout(mbedtls_ssl_config *conf,
1647 uint32_t min, uint32_t max)
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001648{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001649 conf->hs_timeout_min = min;
1650 conf->hs_timeout_max = max;
Manuel Pégourié-Gonnard905dd242014-10-01 12:03:55 +02001651}
1652#endif
1653
Gilles Peskine449bd832023-01-11 14:50:10 +01001654void mbedtls_ssl_conf_authmode(mbedtls_ssl_config *conf, int authmode)
Paul Bakker5121ce52009-01-03 21:22:43 +00001655{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001656 conf->authmode = authmode;
Paul Bakker5121ce52009-01-03 21:22:43 +00001657}
1658
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001659#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001660void mbedtls_ssl_conf_verify(mbedtls_ssl_config *conf,
1661 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
1662 void *p_vrfy)
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001663{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001664 conf->f_vrfy = f_vrfy;
1665 conf->p_vrfy = p_vrfy;
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001666}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001667#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb63b0af2011-01-13 17:54:59 +00001668
Gilles Peskine449bd832023-01-11 14:50:10 +01001669void mbedtls_ssl_conf_rng(mbedtls_ssl_config *conf,
1670 int (*f_rng)(void *, unsigned char *, size_t),
1671 void *p_rng)
Paul Bakker5121ce52009-01-03 21:22:43 +00001672{
Manuel Pégourié-Gonnard750e4d72015-05-07 12:35:38 +01001673 conf->f_rng = f_rng;
1674 conf->p_rng = p_rng;
Paul Bakker5121ce52009-01-03 21:22:43 +00001675}
1676
Gilles Peskine449bd832023-01-11 14:50:10 +01001677void mbedtls_ssl_conf_dbg(mbedtls_ssl_config *conf,
1678 void (*f_dbg)(void *, int, const char *, int, const char *),
1679 void *p_dbg)
Paul Bakker5121ce52009-01-03 21:22:43 +00001680{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001681 conf->f_dbg = f_dbg;
1682 conf->p_dbg = p_dbg;
Paul Bakker5121ce52009-01-03 21:22:43 +00001683}
1684
Gilles Peskine449bd832023-01-11 14:50:10 +01001685void mbedtls_ssl_set_bio(mbedtls_ssl_context *ssl,
1686 void *p_bio,
1687 mbedtls_ssl_send_t *f_send,
1688 mbedtls_ssl_recv_t *f_recv,
1689 mbedtls_ssl_recv_timeout_t *f_recv_timeout)
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001690{
1691 ssl->p_bio = p_bio;
1692 ssl->f_send = f_send;
1693 ssl->f_recv = f_recv;
1694 ssl->f_recv_timeout = f_recv_timeout;
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001695}
1696
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02001697#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01001698void mbedtls_ssl_set_mtu(mbedtls_ssl_context *ssl, uint16_t mtu)
Manuel Pégourié-Gonnard6e7aaca2018-08-20 10:37:23 +02001699{
1700 ssl->mtu = mtu;
1701}
1702#endif
1703
Gilles Peskine449bd832023-01-11 14:50:10 +01001704void mbedtls_ssl_conf_read_timeout(mbedtls_ssl_config *conf, uint32_t timeout)
Manuel Pégourié-Gonnard97fd52c2015-05-06 15:38:52 +01001705{
1706 conf->read_timeout = timeout;
Manuel Pégourié-Gonnard8fa6dfd2014-09-17 10:47:43 +02001707}
1708
Gilles Peskine449bd832023-01-11 14:50:10 +01001709void mbedtls_ssl_set_timer_cb(mbedtls_ssl_context *ssl,
1710 void *p_timer,
1711 mbedtls_ssl_set_timer_t *f_set_timer,
1712 mbedtls_ssl_get_timer_t *f_get_timer)
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001713{
1714 ssl->p_timer = p_timer;
1715 ssl->f_set_timer = f_set_timer;
1716 ssl->f_get_timer = f_get_timer;
Manuel Pégourié-Gonnard286a1362015-05-13 16:22:05 +02001717
1718 /* Make sure we start with no timer running */
Gilles Peskine449bd832023-01-11 14:50:10 +01001719 mbedtls_ssl_set_timer(ssl, 0);
Manuel Pégourié-Gonnard2e012912015-05-12 20:55:41 +02001720}
1721
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001722#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001723void mbedtls_ssl_conf_session_cache(mbedtls_ssl_config *conf,
1724 void *p_cache,
1725 mbedtls_ssl_cache_get_t *f_get_cache,
1726 mbedtls_ssl_cache_set_t *f_set_cache)
Paul Bakker5121ce52009-01-03 21:22:43 +00001727{
Manuel Pégourié-Gonnard5cb33082015-05-06 18:06:26 +01001728 conf->p_cache = p_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001729 conf->f_get_cache = f_get_cache;
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02001730 conf->f_set_cache = f_set_cache;
Paul Bakker5121ce52009-01-03 21:22:43 +00001731}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001732#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001733
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001734#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001735int mbedtls_ssl_set_session(mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session)
Paul Bakker5121ce52009-01-03 21:22:43 +00001736{
Janos Follath865b3eb2019-12-16 11:46:15 +00001737 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001738
Gilles Peskine449bd832023-01-11 14:50:10 +01001739 if (ssl == NULL ||
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001740 session == NULL ||
1741 ssl->session_negotiate == NULL ||
Gilles Peskine449bd832023-01-11 14:50:10 +01001742 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT) {
1743 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001744 }
1745
Gilles Peskine449bd832023-01-11 14:50:10 +01001746 if (ssl->handshake->resume == 1) {
1747 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
1748 }
Hanno Beckere810bbc2021-05-14 16:01:05 +01001749
Jerry Yu21092062022-10-10 21:21:31 +08001750#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001751 if (session->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
Jerry Yu21092062022-10-10 21:21:31 +08001752 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01001753 mbedtls_ssl_ciphersuite_from_id(session->ciphersuite);
Jerry Yu21092062022-10-10 21:21:31 +08001754
Gilles Peskine449bd832023-01-11 14:50:10 +01001755 if (mbedtls_ssl_validate_ciphersuite(
Jerry Yu21092062022-10-10 21:21:31 +08001756 ssl, ciphersuite_info, MBEDTLS_SSL_VERSION_TLS1_3,
Gilles Peskine449bd832023-01-11 14:50:10 +01001757 MBEDTLS_SSL_VERSION_TLS1_3) != 0) {
1758 MBEDTLS_SSL_DEBUG_MSG(4, ("%d is not a valid TLS 1.3 ciphersuite.",
1759 session->ciphersuite));
1760 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jerry Yu21092062022-10-10 21:21:31 +08001761 }
Jerry Yu40afab62022-10-08 10:42:13 +08001762 }
Jerry Yu21092062022-10-10 21:21:31 +08001763#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu40afab62022-10-08 10:42:13 +08001764
Gilles Peskine449bd832023-01-11 14:50:10 +01001765 if ((ret = mbedtls_ssl_session_copy(ssl->session_negotiate,
1766 session)) != 0) {
1767 return ret;
1768 }
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001769
Paul Bakker0a597072012-09-25 21:55:46 +00001770 ssl->handshake->resume = 1;
Manuel Pégourié-Gonnard06650f62013-08-02 15:34:52 +02001771
Gilles Peskine449bd832023-01-11 14:50:10 +01001772 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00001773}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001774#endif /* MBEDTLS_SSL_CLI_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00001775
Gilles Peskine449bd832023-01-11 14:50:10 +01001776void mbedtls_ssl_conf_ciphersuites(mbedtls_ssl_config *conf,
1777 const int *ciphersuites)
Mateusz Starzyk06b07fb2021-02-18 13:55:21 +01001778{
Hanno Beckerd60b6c62021-04-29 12:04:11 +01001779 conf->ciphersuite_list = ciphersuites;
Paul Bakker5121ce52009-01-03 21:22:43 +00001780}
1781
Ronald Cron6f135e12021-12-08 16:57:54 +01001782#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01001783void mbedtls_ssl_conf_tls13_key_exchange_modes(mbedtls_ssl_config *conf,
1784 const int kex_modes)
Hanno Becker71f1ed62021-07-24 06:01:47 +01001785{
Xiaofei Bai746f9482021-11-12 08:53:56 +00001786 conf->tls13_kex_modes = kex_modes & MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Hanno Becker71f1ed62021-07-24 06:01:47 +01001787}
Xiaokang Qian72de95d2022-10-25 02:54:33 +00001788
1789#if defined(MBEDTLS_SSL_EARLY_DATA)
Gilles Peskine449bd832023-01-11 14:50:10 +01001790void mbedtls_ssl_tls13_conf_early_data(mbedtls_ssl_config *conf,
1791 int early_data_enabled)
Xiaokang Qian72de95d2022-10-25 02:54:33 +00001792{
1793 conf->early_data_enabled = early_data_enabled;
1794}
Jerry Yucc4e0072022-11-22 17:22:22 +08001795
1796#if defined(MBEDTLS_SSL_SRV_C)
1797void mbedtls_ssl_tls13_conf_max_early_data_size(
Gilles Peskine449bd832023-01-11 14:50:10 +01001798 mbedtls_ssl_config *conf, uint32_t max_early_data_size)
Jerry Yucc4e0072022-11-22 17:22:22 +08001799{
Jerry Yu39da9852022-12-06 16:58:36 +08001800 conf->max_early_data_size = max_early_data_size;
Jerry Yucc4e0072022-11-22 17:22:22 +08001801}
1802#endif /* MBEDTLS_SSL_SRV_C */
1803
Xiaokang Qian72de95d2022-10-25 02:54:33 +00001804#endif /* MBEDTLS_SSL_EARLY_DATA */
Ronald Cron6f135e12021-12-08 16:57:54 +01001805#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01001806
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001807#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001808void mbedtls_ssl_conf_cert_profile(mbedtls_ssl_config *conf,
1809 const mbedtls_x509_crt_profile *profile)
Manuel Pégourié-Gonnard6e3ee3a2015-06-17 10:58:20 +02001810{
1811 conf->cert_profile = profile;
1812}
1813
Gilles Peskine449bd832023-01-11 14:50:10 +01001814static void ssl_key_cert_free(mbedtls_ssl_key_cert *key_cert)
Glenn Strauss36872db2022-01-22 05:06:31 -05001815{
1816 mbedtls_ssl_key_cert *cur = key_cert, *next;
1817
Gilles Peskine449bd832023-01-11 14:50:10 +01001818 while (cur != NULL) {
Glenn Strauss36872db2022-01-22 05:06:31 -05001819 next = cur->next;
Gilles Peskine449bd832023-01-11 14:50:10 +01001820 mbedtls_free(cur);
Glenn Strauss36872db2022-01-22 05:06:31 -05001821 cur = next;
1822 }
1823}
1824
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001825/* Append a new keycert entry to a (possibly empty) list */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02001826MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01001827static int ssl_append_key_cert(mbedtls_ssl_key_cert **head,
1828 mbedtls_x509_crt *cert,
1829 mbedtls_pk_context *key)
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001830{
niisato8ee24222018-06-25 19:05:48 +09001831 mbedtls_ssl_key_cert *new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001832
Gilles Peskine449bd832023-01-11 14:50:10 +01001833 if (cert == NULL) {
Glenn Strauss36872db2022-01-22 05:06:31 -05001834 /* Free list if cert is null */
Gilles Peskine449bd832023-01-11 14:50:10 +01001835 ssl_key_cert_free(*head);
Glenn Strauss36872db2022-01-22 05:06:31 -05001836 *head = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01001837 return 0;
Glenn Strauss36872db2022-01-22 05:06:31 -05001838 }
1839
Gilles Peskine449bd832023-01-11 14:50:10 +01001840 new_cert = mbedtls_calloc(1, sizeof(mbedtls_ssl_key_cert));
1841 if (new_cert == NULL) {
1842 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
1843 }
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001844
niisato8ee24222018-06-25 19:05:48 +09001845 new_cert->cert = cert;
1846 new_cert->key = key;
1847 new_cert->next = NULL;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001848
Glenn Strauss36872db2022-01-22 05:06:31 -05001849 /* Update head if the list was null, else add to the end */
Gilles Peskine449bd832023-01-11 14:50:10 +01001850 if (*head == NULL) {
niisato8ee24222018-06-25 19:05:48 +09001851 *head = new_cert;
Gilles Peskine449bd832023-01-11 14:50:10 +01001852 } else {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001853 mbedtls_ssl_key_cert *cur = *head;
Gilles Peskine449bd832023-01-11 14:50:10 +01001854 while (cur->next != NULL) {
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001855 cur = cur->next;
Gilles Peskine449bd832023-01-11 14:50:10 +01001856 }
niisato8ee24222018-06-25 19:05:48 +09001857 cur->next = new_cert;
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001858 }
1859
Gilles Peskine449bd832023-01-11 14:50:10 +01001860 return 0;
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001861}
1862
Gilles Peskine449bd832023-01-11 14:50:10 +01001863int mbedtls_ssl_conf_own_cert(mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001864 mbedtls_x509_crt *own_cert,
Gilles Peskine449bd832023-01-11 14:50:10 +01001865 mbedtls_pk_context *pk_key)
Manuel Pégourié-Gonnard8f618a82015-05-10 21:13:36 +02001866{
Gilles Peskine449bd832023-01-11 14:50:10 +01001867 return ssl_append_key_cert(&conf->key_cert, own_cert, pk_key);
Manuel Pégourié-Gonnard834ea852013-09-23 14:46:13 +02001868}
1869
Gilles Peskine449bd832023-01-11 14:50:10 +01001870void mbedtls_ssl_conf_ca_chain(mbedtls_ssl_config *conf,
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001871 mbedtls_x509_crt *ca_chain,
Gilles Peskine449bd832023-01-11 14:50:10 +01001872 mbedtls_x509_crl *ca_crl)
Paul Bakker5121ce52009-01-03 21:22:43 +00001873{
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01001874 conf->ca_chain = ca_chain;
1875 conf->ca_crl = ca_crl;
Hanno Becker5adaad92019-03-27 16:54:37 +00001876
1877#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
1878 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1879 * cannot be used together. */
1880 conf->f_ca_cb = NULL;
1881 conf->p_ca_cb = NULL;
1882#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Paul Bakker5121ce52009-01-03 21:22:43 +00001883}
Hanno Becker5adaad92019-03-27 16:54:37 +00001884
1885#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Gilles Peskine449bd832023-01-11 14:50:10 +01001886void mbedtls_ssl_conf_ca_cb(mbedtls_ssl_config *conf,
1887 mbedtls_x509_crt_ca_cb_t f_ca_cb,
1888 void *p_ca_cb)
Hanno Becker5adaad92019-03-27 16:54:37 +00001889{
1890 conf->f_ca_cb = f_ca_cb;
1891 conf->p_ca_cb = p_ca_cb;
1892
1893 /* mbedtls_ssl_conf_ca_chain() and mbedtls_ssl_conf_ca_cb()
1894 * cannot be used together. */
1895 conf->ca_chain = NULL;
1896 conf->ca_crl = NULL;
1897}
1898#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02001899#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkereb2c6582012-09-27 19:15:01 +00001900
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001901#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01001902const unsigned char *mbedtls_ssl_get_hs_sni(mbedtls_ssl_context *ssl,
1903 size_t *name_len)
Glenn Strauss69894072022-01-24 12:58:00 -05001904{
1905 *name_len = ssl->handshake->sni_name_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01001906 return ssl->handshake->sni_name;
Glenn Strauss69894072022-01-24 12:58:00 -05001907}
1908
Gilles Peskine449bd832023-01-11 14:50:10 +01001909int mbedtls_ssl_set_hs_own_cert(mbedtls_ssl_context *ssl,
1910 mbedtls_x509_crt *own_cert,
1911 mbedtls_pk_context *pk_key)
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001912{
Gilles Peskine449bd832023-01-11 14:50:10 +01001913 return ssl_append_key_cert(&ssl->handshake->sni_key_cert,
1914 own_cert, pk_key);
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001915}
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02001916
Gilles Peskine449bd832023-01-11 14:50:10 +01001917void mbedtls_ssl_set_hs_ca_chain(mbedtls_ssl_context *ssl,
1918 mbedtls_x509_crt *ca_chain,
1919 mbedtls_x509_crl *ca_crl)
Manuel Pégourié-Gonnard22bfa4b2015-05-11 08:46:37 +02001920{
1921 ssl->handshake->sni_ca_chain = ca_chain;
1922 ssl->handshake->sni_ca_crl = ca_crl;
1923}
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001924
Glenn Strauss999ef702022-03-11 01:37:23 -05001925#if defined(MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01001926void mbedtls_ssl_set_hs_dn_hints(mbedtls_ssl_context *ssl,
1927 const mbedtls_x509_crt *crt)
Glenn Strauss999ef702022-03-11 01:37:23 -05001928{
1929 ssl->handshake->dn_hints = crt;
1930}
1931#endif /* MBEDTLS_KEY_EXCHANGE_CERT_REQ_ALLOWED_ENABLED */
1932
Gilles Peskine449bd832023-01-11 14:50:10 +01001933void mbedtls_ssl_set_hs_authmode(mbedtls_ssl_context *ssl,
1934 int authmode)
Manuel Pégourié-Gonnardcdc26ae2015-06-19 12:16:31 +02001935{
1936 ssl->handshake->sni_authmode = authmode;
1937}
Manuel Pégourié-Gonnard1af6c852015-05-10 23:10:37 +02001938#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
1939
Hanno Becker8927c832019-04-03 12:52:50 +01001940#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01001941void mbedtls_ssl_set_verify(mbedtls_ssl_context *ssl,
1942 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
1943 void *p_vrfy)
Hanno Becker8927c832019-04-03 12:52:50 +01001944{
1945 ssl->f_vrfy = f_vrfy;
1946 ssl->p_vrfy = p_vrfy;
1947}
1948#endif
1949
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02001950#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001951
Valerio Setti016f6822022-12-09 14:17:50 +01001952#if defined(MBEDTLS_USE_PSA_CRYPTO)
Przemek Stekielfde11282023-03-13 16:06:09 +01001953static const uint8_t jpake_server_id[] = { 's', 'e', 'r', 'v', 'e', 'r' };
1954static const uint8_t jpake_client_id[] = { 'c', 'l', 'i', 'e', 'n', 't' };
1955
Valerio Setti016f6822022-12-09 14:17:50 +01001956static psa_status_t mbedtls_ssl_set_hs_ecjpake_password_common(
Gilles Peskine449bd832023-01-11 14:50:10 +01001957 mbedtls_ssl_context *ssl,
1958 mbedtls_svc_key_id_t pwd)
Valerio Setti016f6822022-12-09 14:17:50 +01001959{
1960 psa_status_t status;
Valerio Setti016f6822022-12-09 14:17:50 +01001961 psa_pake_cipher_suite_t cipher_suite = psa_pake_cipher_suite_init();
Przemek Stekielfde11282023-03-13 16:06:09 +01001962 const uint8_t *user = NULL;
Przemek Stekiel4cd20312023-03-01 11:11:28 +01001963 size_t user_len = 0;
Przemek Stekielfde11282023-03-13 16:06:09 +01001964 const uint8_t *peer = NULL;
Przemek Stekiel4cd20312023-03-01 11:11:28 +01001965 size_t peer_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01001966 psa_pake_cs_set_algorithm(&cipher_suite, PSA_ALG_JPAKE);
1967 psa_pake_cs_set_primitive(&cipher_suite,
1968 PSA_PAKE_PRIMITIVE(PSA_PAKE_PRIMITIVE_TYPE_ECC,
1969 PSA_ECC_FAMILY_SECP_R1,
1970 256));
1971 psa_pake_cs_set_hash(&cipher_suite, PSA_ALG_SHA_256);
Valerio Setti016f6822022-12-09 14:17:50 +01001972
Gilles Peskine449bd832023-01-11 14:50:10 +01001973 status = psa_pake_setup(&ssl->handshake->psa_pake_ctx, &cipher_suite);
1974 if (status != PSA_SUCCESS) {
Valerio Setti016f6822022-12-09 14:17:50 +01001975 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001976 }
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02001977
Gilles Peskine449bd832023-01-11 14:50:10 +01001978 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Przemek Stekielfde11282023-03-13 16:06:09 +01001979 user = jpake_server_id;
1980 user_len = sizeof(jpake_server_id);
1981 peer = jpake_client_id;
1982 peer_len = sizeof(jpake_client_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01001983 } else {
Przemek Stekielfde11282023-03-13 16:06:09 +01001984 user = jpake_client_id;
1985 user_len = sizeof(jpake_client_id);
1986 peer = jpake_server_id;
1987 peer_len = sizeof(jpake_server_id);
Gilles Peskine449bd832023-01-11 14:50:10 +01001988 }
Neil Armstrongca7d5062022-05-31 14:43:23 +02001989
Przemek Stekiel4cd20312023-03-01 11:11:28 +01001990 status = psa_pake_set_user(&ssl->handshake->psa_pake_ctx, user, user_len);
1991 if (status != PSA_SUCCESS) {
1992 return status;
1993 }
1994
1995 status = psa_pake_set_peer(&ssl->handshake->psa_pake_ctx, peer, peer_len);
Gilles Peskine449bd832023-01-11 14:50:10 +01001996 if (status != PSA_SUCCESS) {
Valerio Setti016f6822022-12-09 14:17:50 +01001997 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01001998 }
Valerio Setti016f6822022-12-09 14:17:50 +01001999
Gilles Peskine449bd832023-01-11 14:50:10 +01002000 status = psa_pake_set_password_key(&ssl->handshake->psa_pake_ctx, pwd);
2001 if (status != PSA_SUCCESS) {
Valerio Setti016f6822022-12-09 14:17:50 +01002002 return status;
Gilles Peskine449bd832023-01-11 14:50:10 +01002003 }
Valerio Setti016f6822022-12-09 14:17:50 +01002004
2005 ssl->handshake->psa_pake_ctx_is_ok = 1;
2006
Gilles Peskine449bd832023-01-11 14:50:10 +01002007 return PSA_SUCCESS;
Valerio Setti016f6822022-12-09 14:17:50 +01002008}
2009
Gilles Peskine449bd832023-01-11 14:50:10 +01002010int mbedtls_ssl_set_hs_ecjpake_password(mbedtls_ssl_context *ssl,
2011 const unsigned char *pw,
2012 size_t pw_len)
Valerio Setti016f6822022-12-09 14:17:50 +01002013{
2014 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
2015 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 /* Empty password is not valid */
2022 if ((pw == NULL) || (pw_len == 0)) {
2023 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2024 }
2025
2026 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DERIVE);
2027 psa_set_key_algorithm(&attributes, PSA_ALG_JPAKE);
2028 psa_set_key_type(&attributes, PSA_KEY_TYPE_PASSWORD);
2029
2030 status = psa_import_key(&attributes, pw, pw_len,
2031 &ssl->handshake->psa_pake_password);
2032 if (status != PSA_SUCCESS) {
2033 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2034 }
2035
2036 status = mbedtls_ssl_set_hs_ecjpake_password_common(ssl,
2037 ssl->handshake->psa_pake_password);
2038 if (status != PSA_SUCCESS) {
2039 psa_destroy_key(ssl->handshake->psa_pake_password);
2040 psa_pake_abort(&ssl->handshake->psa_pake_ctx);
2041 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2042 }
2043
2044 return 0;
Valerio Setti02c25b52022-11-15 14:08:42 +01002045}
Valerio Settia9a97dc2022-11-28 18:26:16 +01002046
Gilles Peskine449bd832023-01-11 14:50:10 +01002047int mbedtls_ssl_set_hs_ecjpake_password_opaque(mbedtls_ssl_context *ssl,
2048 mbedtls_svc_key_id_t pwd)
Valerio Settia9a97dc2022-11-28 18:26:16 +01002049{
Valerio Settia9a97dc2022-11-28 18:26:16 +01002050 psa_status_t status;
2051
Gilles Peskine449bd832023-01-11 14:50:10 +01002052 if (ssl->handshake == NULL || ssl->conf == NULL) {
2053 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Neil Armstrongca7d5062022-05-31 14:43:23 +02002054 }
2055
Gilles Peskine449bd832023-01-11 14:50:10 +01002056 if (mbedtls_svc_key_id_is_null(pwd)) {
2057 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2058 }
2059
2060 status = mbedtls_ssl_set_hs_ecjpake_password_common(ssl, pwd);
2061 if (status != PSA_SUCCESS) {
2062 psa_pake_abort(&ssl->handshake->psa_pake_ctx);
2063 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2064 }
2065
2066 return 0;
Valerio Setti02c25b52022-11-15 14:08:42 +01002067}
2068#else /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01002069int mbedtls_ssl_set_hs_ecjpake_password(mbedtls_ssl_context *ssl,
2070 const unsigned char *pw,
2071 size_t pw_len)
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002072{
2073 mbedtls_ecjpake_role role;
2074
Gilles Peskine449bd832023-01-11 14:50:10 +01002075 if (ssl->handshake == NULL || ssl->conf == NULL) {
2076 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2077 }
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002078
Valerio Settic689ed82022-12-07 14:40:38 +01002079 /* Empty password is not valid */
Gilles Peskine449bd832023-01-11 14:50:10 +01002080 if ((pw == NULL) || (pw_len == 0)) {
2081 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2082 }
Valerio Settic689ed82022-12-07 14:40:38 +01002083
Gilles Peskine449bd832023-01-11 14:50:10 +01002084 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002085 role = MBEDTLS_ECJPAKE_SERVER;
Gilles Peskine449bd832023-01-11 14:50:10 +01002086 } else {
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002087 role = MBEDTLS_ECJPAKE_CLIENT;
Gilles Peskine449bd832023-01-11 14:50:10 +01002088 }
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002089
Gilles Peskine449bd832023-01-11 14:50:10 +01002090 return mbedtls_ecjpake_setup(&ssl->handshake->ecjpake_ctx,
2091 role,
2092 MBEDTLS_MD_SHA256,
2093 MBEDTLS_ECP_DP_SECP256R1,
2094 pw, pw_len);
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002095}
Valerio Setti02c25b52022-11-15 14:08:42 +01002096#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02002097#endif /* MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
Manuel Pégourié-Gonnard7002f4a2015-09-15 12:43:43 +02002098
Ronald Cron73fe8df2022-10-05 14:31:43 +02002099#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01002100int mbedtls_ssl_conf_has_static_psk(mbedtls_ssl_config const *conf)
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002101{
Gilles Peskine449bd832023-01-11 14:50:10 +01002102 if (conf->psk_identity == NULL ||
2103 conf->psk_identity_len == 0) {
2104 return 0;
Ronald Crond29e13e2022-10-19 10:33:48 +02002105 }
2106
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002107#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002108 if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) {
2109 return 1;
2110 }
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002111#endif /* MBEDTLS_USE_PSA_CRYPTO */
Ronald Crond29e13e2022-10-19 10:33:48 +02002112
Gilles Peskine449bd832023-01-11 14:50:10 +01002113 if (conf->psk != NULL && conf->psk_len != 0) {
2114 return 1;
2115 }
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002116
Gilles Peskine449bd832023-01-11 14:50:10 +01002117 return 0;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002118}
2119
Gilles Peskine449bd832023-01-11 14:50:10 +01002120static void ssl_conf_remove_psk(mbedtls_ssl_config *conf)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002121{
2122 /* Remove reference to existing PSK, if any. */
2123#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002124 if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) {
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002125 /* The maintenance of the PSK key slot is the
2126 * user's responsibility. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002127 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002128 }
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002129#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01002130 if (conf->psk != NULL) {
2131 mbedtls_platform_zeroize(conf->psk, conf->psk_len);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002132
Gilles Peskine449bd832023-01-11 14:50:10 +01002133 mbedtls_free(conf->psk);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002134 conf->psk = NULL;
2135 conf->psk_len = 0;
2136 }
2137
2138 /* Remove reference to PSK identity, if any. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002139 if (conf->psk_identity != NULL) {
2140 mbedtls_free(conf->psk_identity);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002141 conf->psk_identity = NULL;
2142 conf->psk_identity_len = 0;
2143 }
2144}
2145
Hanno Becker7390c712018-11-15 13:33:04 +00002146/* This function assumes that PSK identity in the SSL config is unset.
2147 * It checks that the provided identity is well-formed and attempts
2148 * to make a copy of it in the SSL config.
2149 * On failure, the PSK identity in the config remains unset. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02002150MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002151static int ssl_conf_set_psk_identity(mbedtls_ssl_config *conf,
2152 unsigned char const *psk_identity,
2153 size_t psk_identity_len)
Paul Bakkerd4a56ec2013-04-16 18:05:29 +02002154{
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02002155 /* Identity len will be encoded on two bytes */
Gilles Peskine449bd832023-01-11 14:50:10 +01002156 if (psk_identity == NULL ||
Ronald Cron2a87e9b2022-10-19 10:55:26 +02002157 psk_identity_len == 0 ||
Gilles Peskine449bd832023-01-11 14:50:10 +01002158 (psk_identity_len >> 16) != 0 ||
2159 psk_identity_len > MBEDTLS_SSL_OUT_CONTENT_LEN) {
2160 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnardc6b5d832015-08-27 16:37:35 +02002161 }
2162
Gilles Peskine449bd832023-01-11 14:50:10 +01002163 conf->psk_identity = mbedtls_calloc(1, psk_identity_len);
2164 if (conf->psk_identity == NULL) {
2165 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2166 }
Paul Bakker6db455e2013-09-18 17:29:31 +02002167
Manuel Pégourié-Gonnard120fdbd2015-05-07 17:07:50 +01002168 conf->psk_identity_len = psk_identity_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002169 memcpy(conf->psk_identity, psk_identity, conf->psk_identity_len);
Paul Bakker5ad403f2013-09-18 21:21:30 +02002170
Gilles Peskine449bd832023-01-11 14:50:10 +01002171 return 0;
Paul Bakker6db455e2013-09-18 17:29:31 +02002172}
2173
Gilles Peskine449bd832023-01-11 14:50:10 +01002174int mbedtls_ssl_conf_psk(mbedtls_ssl_config *conf,
2175 const unsigned char *psk, size_t psk_len,
2176 const unsigned char *psk_identity, size_t psk_identity_len)
Hanno Becker7390c712018-11-15 13:33:04 +00002177{
Janos Follath865b3eb2019-12-16 11:46:15 +00002178 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002179
2180 /* We currently only support one PSK, raw or opaque. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002181 if (mbedtls_ssl_conf_has_static_psk(conf)) {
2182 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
2183 }
Hanno Becker7390c712018-11-15 13:33:04 +00002184
2185 /* Check and set raw PSK */
Gilles Peskine449bd832023-01-11 14:50:10 +01002186 if (psk == NULL) {
2187 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2188 }
2189 if (psk_len == 0) {
2190 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2191 }
2192 if (psk_len > MBEDTLS_PSK_MAX_LEN) {
2193 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2194 }
Piotr Nowicki9926eaf2019-11-20 14:54:36 +01002195
Gilles Peskine449bd832023-01-11 14:50:10 +01002196 if ((conf->psk = mbedtls_calloc(1, psk_len)) == NULL) {
2197 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2198 }
Hanno Becker7390c712018-11-15 13:33:04 +00002199 conf->psk_len = psk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002200 memcpy(conf->psk, psk, conf->psk_len);
Hanno Becker7390c712018-11-15 13:33:04 +00002201
2202 /* Check and set PSK Identity */
Gilles Peskine449bd832023-01-11 14:50:10 +01002203 ret = ssl_conf_set_psk_identity(conf, psk_identity, psk_identity_len);
2204 if (ret != 0) {
2205 ssl_conf_remove_psk(conf);
2206 }
Hanno Becker7390c712018-11-15 13:33:04 +00002207
Gilles Peskine449bd832023-01-11 14:50:10 +01002208 return ret;
Hanno Becker7390c712018-11-15 13:33:04 +00002209}
2210
Gilles Peskine449bd832023-01-11 14:50:10 +01002211static void ssl_remove_psk(mbedtls_ssl_context *ssl)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002212{
2213#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002214 if (!mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque)) {
Neil Armstrong501c9322022-05-03 09:35:09 +02002215 /* The maintenance of the external PSK key slot is the
2216 * user's responsibility. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002217 if (ssl->handshake->psk_opaque_is_internal) {
2218 psa_destroy_key(ssl->handshake->psk_opaque);
Neil Armstrong501c9322022-05-03 09:35:09 +02002219 ssl->handshake->psk_opaque_is_internal = 0;
2220 }
Ronald Croncf56a0a2020-08-04 09:51:30 +02002221 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002222 }
Neil Armstronge952a302022-05-03 10:22:14 +02002223#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002224 if (ssl->handshake->psk != NULL) {
2225 mbedtls_platform_zeroize(ssl->handshake->psk,
2226 ssl->handshake->psk_len);
2227 mbedtls_free(ssl->handshake->psk);
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002228 ssl->handshake->psk_len = 0;
2229 }
Neil Armstronge952a302022-05-03 10:22:14 +02002230#endif /* MBEDTLS_USE_PSA_CRYPTO */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002231}
2232
Gilles Peskine449bd832023-01-11 14:50:10 +01002233int mbedtls_ssl_set_hs_psk(mbedtls_ssl_context *ssl,
2234 const unsigned char *psk, size_t psk_len)
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002235{
Neil Armstrong501c9322022-05-03 09:35:09 +02002236#if defined(MBEDTLS_USE_PSA_CRYPTO)
2237 psa_key_attributes_t key_attributes = psa_key_attributes_init();
Jerry Yu5d01c052022-08-17 10:18:10 +08002238 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jerry Yu24b8c812022-08-20 19:06:56 +08002239 psa_algorithm_t alg = PSA_ALG_NONE;
Jerry Yu5d01c052022-08-17 10:18:10 +08002240 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Neil Armstrong501c9322022-05-03 09:35:09 +02002241#endif /* MBEDTLS_USE_PSA_CRYPTO */
2242
Gilles Peskine449bd832023-01-11 14:50:10 +01002243 if (psk == NULL || ssl->handshake == NULL) {
2244 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2245 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002246
Gilles Peskine449bd832023-01-11 14:50:10 +01002247 if (psk_len > MBEDTLS_PSK_MAX_LEN) {
2248 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2249 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002250
Gilles Peskine449bd832023-01-11 14:50:10 +01002251 ssl_remove_psk(ssl);
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002252
Neil Armstrong501c9322022-05-03 09:35:09 +02002253#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yuccc68a42022-07-26 16:39:20 +08002254#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01002255 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2) {
2256 if (ssl->handshake->ciphersuite_info->mac == MBEDTLS_MD_SHA384) {
2257 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
2258 } else {
2259 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
2260 }
2261 psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE);
Jerry Yuccc68a42022-07-26 16:39:20 +08002262 }
2263#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Neil Armstrong501c9322022-05-03 09:35:09 +02002264
Jerry Yu568ec252022-07-22 21:27:34 +08002265#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01002266 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
2267 alg = PSA_ALG_HKDF_EXTRACT(PSA_ALG_ANY_HASH);
2268 psa_set_key_usage_flags(&key_attributes,
2269 PSA_KEY_USAGE_DERIVE | PSA_KEY_USAGE_EXPORT);
Jerry Yuccc68a42022-07-26 16:39:20 +08002270 }
2271#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
2272
Gilles Peskine449bd832023-01-11 14:50:10 +01002273 psa_set_key_algorithm(&key_attributes, alg);
2274 psa_set_key_type(&key_attributes, PSA_KEY_TYPE_DERIVE);
Neil Armstrong501c9322022-05-03 09:35:09 +02002275
Gilles Peskine449bd832023-01-11 14:50:10 +01002276 status = psa_import_key(&key_attributes, psk, psk_len, &key);
2277 if (status != PSA_SUCCESS) {
2278 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
2279 }
Neil Armstrong501c9322022-05-03 09:35:09 +02002280
2281 /* Allow calling psa_destroy_key() on psk remove */
2282 ssl->handshake->psk_opaque_is_internal = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01002283 return mbedtls_ssl_set_hs_psk_opaque(ssl, key);
Neil Armstrong501c9322022-05-03 09:35:09 +02002284#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002285 if ((ssl->handshake->psk = mbedtls_calloc(1, psk_len)) == NULL) {
2286 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2287 }
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002288
2289 ssl->handshake->psk_len = psk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01002290 memcpy(ssl->handshake->psk, psk, ssl->handshake->psk_len);
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002291
Gilles Peskine449bd832023-01-11 14:50:10 +01002292 return 0;
Neil Armstrong501c9322022-05-03 09:35:09 +02002293#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01002294}
2295
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002296#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002297int mbedtls_ssl_conf_psk_opaque(mbedtls_ssl_config *conf,
2298 mbedtls_svc_key_id_t psk,
2299 const unsigned char *psk_identity,
2300 size_t psk_identity_len)
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002301{
Janos Follath865b3eb2019-12-16 11:46:15 +00002302 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Becker2ed3dce2021-04-19 21:59:14 +01002303
2304 /* We currently only support one PSK, raw or opaque. */
Gilles Peskine449bd832023-01-11 14:50:10 +01002305 if (mbedtls_ssl_conf_has_static_psk(conf)) {
2306 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
2307 }
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002308
Hanno Becker7390c712018-11-15 13:33:04 +00002309 /* Check and set opaque PSK */
Gilles Peskine449bd832023-01-11 14:50:10 +01002310 if (mbedtls_svc_key_id_is_null(psk)) {
2311 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2312 }
Ronald Croncf56a0a2020-08-04 09:51:30 +02002313 conf->psk_opaque = psk;
Hanno Becker7390c712018-11-15 13:33:04 +00002314
2315 /* Check and set PSK Identity */
Gilles Peskine449bd832023-01-11 14:50:10 +01002316 ret = ssl_conf_set_psk_identity(conf, psk_identity,
2317 psk_identity_len);
2318 if (ret != 0) {
2319 ssl_conf_remove_psk(conf);
2320 }
Hanno Becker7390c712018-11-15 13:33:04 +00002321
Gilles Peskine449bd832023-01-11 14:50:10 +01002322 return ret;
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002323}
2324
Gilles Peskine449bd832023-01-11 14:50:10 +01002325int mbedtls_ssl_set_hs_psk_opaque(mbedtls_ssl_context *ssl,
2326 mbedtls_svc_key_id_t psk)
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002327{
Gilles Peskine449bd832023-01-11 14:50:10 +01002328 if ((mbedtls_svc_key_id_is_null(psk)) ||
2329 (ssl->handshake == NULL)) {
2330 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2331 }
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002332
Gilles Peskine449bd832023-01-11 14:50:10 +01002333 ssl_remove_psk(ssl);
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002334 ssl->handshake->psk_opaque = psk;
Gilles Peskine449bd832023-01-11 14:50:10 +01002335 return 0;
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002336}
2337#endif /* MBEDTLS_USE_PSA_CRYPTO */
2338
Jerry Yu8897c072022-08-12 13:56:53 +08002339#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002340void mbedtls_ssl_conf_psk_cb(mbedtls_ssl_config *conf,
2341 int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
2342 size_t),
2343 void *p_psk)
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002344{
2345 conf->f_psk = f_psk;
2346 conf->p_psk = p_psk;
2347}
Jerry Yu8897c072022-08-12 13:56:53 +08002348#endif /* MBEDTLS_SSL_SRV_C */
2349
Ronald Cron73fe8df2022-10-05 14:31:43 +02002350#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Przemyslaw Stekiel6928a512022-02-03 13:50:35 +01002351
2352#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine301711e2022-04-26 16:57:05 +02002353static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
Gilles Peskine449bd832023-01-11 14:50:10 +01002354 psa_algorithm_t alg)
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002355{
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002356#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01002357 if (alg == PSA_ALG_CBC_NO_PADDING) {
2358 return MBEDTLS_SSL_MODE_CBC;
2359 }
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002360#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
Gilles Peskine449bd832023-01-11 14:50:10 +01002361 if (PSA_ALG_IS_AEAD(alg)) {
2362 return MBEDTLS_SSL_MODE_AEAD;
2363 }
2364 return MBEDTLS_SSL_MODE_STREAM;
Gilles Peskine301711e2022-04-26 16:57:05 +02002365}
2366
2367#else /* MBEDTLS_USE_PSA_CRYPTO */
2368
2369static mbedtls_ssl_mode_t mbedtls_ssl_get_base_mode(
Gilles Peskine449bd832023-01-11 14:50:10 +01002370 mbedtls_cipher_mode_t mode)
Gilles Peskine301711e2022-04-26 16:57:05 +02002371{
2372#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01002373 if (mode == MBEDTLS_MODE_CBC) {
2374 return MBEDTLS_SSL_MODE_CBC;
2375 }
Gilles Peskine301711e2022-04-26 16:57:05 +02002376#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
2377
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002378#if defined(MBEDTLS_GCM_C) || \
2379 defined(MBEDTLS_CCM_C) || \
2380 defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002381 if (mode == MBEDTLS_MODE_GCM ||
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002382 mode == MBEDTLS_MODE_CCM ||
Gilles Peskine449bd832023-01-11 14:50:10 +01002383 mode == MBEDTLS_MODE_CHACHAPOLY) {
2384 return MBEDTLS_SSL_MODE_AEAD;
2385 }
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002386#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002387
Gilles Peskine449bd832023-01-11 14:50:10 +01002388 return MBEDTLS_SSL_MODE_STREAM;
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002389}
Gilles Peskine301711e2022-04-26 16:57:05 +02002390#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong8a0f3e82022-03-30 10:57:37 +02002391
Gilles Peskinee108d982022-04-26 16:50:40 +02002392static mbedtls_ssl_mode_t mbedtls_ssl_get_actual_mode(
2393 mbedtls_ssl_mode_t base_mode,
Gilles Peskine449bd832023-01-11 14:50:10 +01002394 int encrypt_then_mac)
Gilles Peskinee108d982022-04-26 16:50:40 +02002395{
2396#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01002397 if (encrypt_then_mac == MBEDTLS_SSL_ETM_ENABLED &&
2398 base_mode == MBEDTLS_SSL_MODE_CBC) {
2399 return MBEDTLS_SSL_MODE_CBC_ETM;
Gilles Peskinee108d982022-04-26 16:50:40 +02002400 }
2401#else
2402 (void) encrypt_then_mac;
2403#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002404 return base_mode;
Gilles Peskinee108d982022-04-26 16:50:40 +02002405}
2406
Neil Armstrongab555e02022-04-04 11:07:59 +02002407mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_transform(
Gilles Peskine449bd832023-01-11 14:50:10 +01002408 const mbedtls_ssl_transform *transform)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002409{
Gilles Peskinee108d982022-04-26 16:50:40 +02002410 mbedtls_ssl_mode_t base_mode = mbedtls_ssl_get_base_mode(
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002411#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01002412 transform->psa_alg
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002413#else
Gilles Peskine449bd832023-01-11 14:50:10 +01002414 mbedtls_cipher_get_cipher_mode(&transform->cipher_ctx_enc)
Gilles Peskinee108d982022-04-26 16:50:40 +02002415#endif
2416 );
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002417
Gilles Peskinee108d982022-04-26 16:50:40 +02002418 int encrypt_then_mac = 0;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002419#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskinee108d982022-04-26 16:50:40 +02002420 encrypt_then_mac = transform->encrypt_then_mac;
2421#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002422 return mbedtls_ssl_get_actual_mode(base_mode, encrypt_then_mac);
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002423}
2424
Neil Armstrongab555e02022-04-04 11:07:59 +02002425mbedtls_ssl_mode_t mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002426#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01002427 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02002428#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01002429 const mbedtls_ssl_ciphersuite_t *suite)
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002430{
Gilles Peskinee108d982022-04-26 16:50:40 +02002431 mbedtls_ssl_mode_t base_mode = MBEDTLS_SSL_MODE_STREAM;
2432
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002433#if defined(MBEDTLS_USE_PSA_CRYPTO)
2434 psa_status_t status;
2435 psa_algorithm_t alg;
2436 psa_key_type_t type;
2437 size_t size;
Gilles Peskine449bd832023-01-11 14:50:10 +01002438 status = mbedtls_ssl_cipher_to_psa(suite->cipher, 0, &alg, &type, &size);
2439 if (status == PSA_SUCCESS) {
2440 base_mode = mbedtls_ssl_get_base_mode(alg);
2441 }
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002442#else
2443 const mbedtls_cipher_info_t *cipher =
Gilles Peskine449bd832023-01-11 14:50:10 +01002444 mbedtls_cipher_info_from_type(suite->cipher);
2445 if (cipher != NULL) {
Gilles Peskinee108d982022-04-26 16:50:40 +02002446 base_mode =
2447 mbedtls_ssl_get_base_mode(
Gilles Peskine449bd832023-01-11 14:50:10 +01002448 mbedtls_cipher_info_get_mode(cipher));
Gilles Peskinee108d982022-04-26 16:50:40 +02002449 }
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002450#endif /* MBEDTLS_USE_PSA_CRYPTO */
2451
Gilles Peskinee108d982022-04-26 16:50:40 +02002452#if !defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
2453 int encrypt_then_mac = 0;
2454#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002455 return mbedtls_ssl_get_actual_mode(base_mode, encrypt_then_mac);
Neil Armstrong4bf4c862022-04-01 10:35:48 +02002456}
2457
Neil Armstrong8395d7a2022-05-18 11:44:56 +02002458#if defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu251a12e2022-07-13 15:15:48 +08002459
2460#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu438ddd82022-07-07 06:55:50 +00002461/* Serialization of TLS 1.3 sessions:
2462 *
2463 * struct {
Xiaokang Qian126bf8e2022-10-13 02:22:40 +00002464 * opaque hostname<0..2^16-1>;
Jerry Yu438ddd82022-07-07 06:55:50 +00002465 * uint64 ticket_received;
2466 * uint32 ticket_lifetime;
Jerry Yu34191072022-08-18 10:32:09 +08002467 * opaque ticket<1..2^16-1>;
Jerry Yu438ddd82022-07-07 06:55:50 +00002468 * } ClientOnlyData;
2469 *
2470 * struct {
2471 * uint8 endpoint;
2472 * uint8 ciphersuite[2];
2473 * uint32 ticket_age_add;
2474 * uint8 ticket_flags;
2475 * opaque resumption_key<0..255>;
2476 * select ( endpoint ) {
2477 * case client: ClientOnlyData;
2478 * case server: uint64 start_time;
2479 * };
2480 * } serialized_session_tls13;
2481 *
2482 */
2483#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yue36fdd62022-08-17 21:31:36 +08002484MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002485static int ssl_tls13_session_save(const mbedtls_ssl_session *session,
2486 unsigned char *buf,
2487 size_t buf_len,
2488 size_t *olen)
Jerry Yu438ddd82022-07-07 06:55:50 +00002489{
2490 unsigned char *p = buf;
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00002491#if defined(MBEDTLS_SSL_CLI_C) && \
2492 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01002493 size_t hostname_len = (session->hostname == NULL) ?
2494 0 : strlen(session->hostname) + 1;
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00002495#endif
Jerry Yubc7c1a42022-07-21 22:57:37 +08002496 size_t needed = 1 /* endpoint */
2497 + 2 /* ciphersuite */
2498 + 4 /* ticket_age_add */
Jerry Yu34191072022-08-18 10:32:09 +08002499 + 1 /* ticket_flags */
2500 + 1; /* resumption_key length */
Jerry Yue36fdd62022-08-17 21:31:36 +08002501 *olen = 0;
Jerry Yu34191072022-08-18 10:32:09 +08002502
Gilles Peskine449bd832023-01-11 14:50:10 +01002503 if (session->resumption_key_len > MBEDTLS_SSL_TLS1_3_TICKET_RESUMPTION_KEY_LEN) {
2504 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2505 }
Jerry Yu34191072022-08-18 10:32:09 +08002506 needed += session->resumption_key_len; /* resumption_key */
2507
Jerry Yu438ddd82022-07-07 06:55:50 +00002508#if defined(MBEDTLS_HAVE_TIME)
2509 needed += 8; /* start_time or ticket_received */
2510#endif
2511
2512#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002513 if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Xiaokang Qian126bf8e2022-10-13 02:22:40 +00002514#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Xiaokang Qianbc663a02022-10-09 11:14:39 +00002515 needed += 2 /* hostname_len */
Gilles Peskine449bd832023-01-11 14:50:10 +01002516 + hostname_len; /* hostname */
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00002517#endif
2518
Jerry Yu438ddd82022-07-07 06:55:50 +00002519 needed += 4 /* ticket_lifetime */
Jerry Yue36fdd62022-08-17 21:31:36 +08002520 + 2; /* ticket_len */
Jerry Yu34191072022-08-18 10:32:09 +08002521
2522 /* Check size_t overflow */
Gilles Peskine449bd832023-01-11 14:50:10 +01002523 if (session->ticket_len > SIZE_MAX - needed) {
2524 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2525 }
Jerry Yu34191072022-08-18 10:32:09 +08002526
Jerry Yue28d9742022-08-18 15:44:03 +08002527 needed += session->ticket_len; /* ticket */
Jerry Yu438ddd82022-07-07 06:55:50 +00002528 }
2529#endif /* MBEDTLS_SSL_CLI_C */
2530
Jerry Yue36fdd62022-08-17 21:31:36 +08002531 *olen = needed;
Gilles Peskine449bd832023-01-11 14:50:10 +01002532 if (needed > buf_len) {
2533 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
2534 }
Jerry Yu438ddd82022-07-07 06:55:50 +00002535
2536 p[0] = session->endpoint;
Gilles Peskine449bd832023-01-11 14:50:10 +01002537 MBEDTLS_PUT_UINT16_BE(session->ciphersuite, p, 1);
2538 MBEDTLS_PUT_UINT32_BE(session->ticket_age_add, p, 3);
Jerry Yu438ddd82022-07-07 06:55:50 +00002539 p[7] = session->ticket_flags;
2540
2541 /* save resumption_key */
Jerry Yubc7c1a42022-07-21 22:57:37 +08002542 p[8] = session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002543 p += 9;
Gilles Peskine449bd832023-01-11 14:50:10 +01002544 memcpy(p, session->resumption_key, session->resumption_key_len);
Jerry Yubc7c1a42022-07-21 22:57:37 +08002545 p += session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002546
2547#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002548 if (session->endpoint == MBEDTLS_SSL_IS_SERVER) {
2549 MBEDTLS_PUT_UINT64_BE((uint64_t) session->start, p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002550 p += 8;
2551 }
2552#endif /* MBEDTLS_HAVE_TIME */
2553
2554#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002555 if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002556#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01002557 MBEDTLS_PUT_UINT16_BE(hostname_len, p, 0);
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002558 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01002559 if (hostname_len > 0) {
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002560 /* save host name */
Gilles Peskine449bd832023-01-11 14:50:10 +01002561 memcpy(p, session->hostname, hostname_len);
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002562 p += hostname_len;
2563 }
2564#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
2565
Jerry Yu438ddd82022-07-07 06:55:50 +00002566#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01002567 MBEDTLS_PUT_UINT64_BE((uint64_t) session->ticket_received, p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002568 p += 8;
2569#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002570 MBEDTLS_PUT_UINT32_BE(session->ticket_lifetime, p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002571 p += 4;
2572
Gilles Peskine449bd832023-01-11 14:50:10 +01002573 MBEDTLS_PUT_UINT16_BE(session->ticket_len, p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002574 p += 2;
Jerry Yu34191072022-08-18 10:32:09 +08002575
Gilles Peskine449bd832023-01-11 14:50:10 +01002576 if (session->ticket != NULL && session->ticket_len > 0) {
2577 memcpy(p, session->ticket, session->ticket_len);
Jerry Yu438ddd82022-07-07 06:55:50 +00002578 p += session->ticket_len;
2579 }
2580 }
2581#endif /* MBEDTLS_SSL_CLI_C */
Gilles Peskine449bd832023-01-11 14:50:10 +01002582 return 0;
Jerry Yu438ddd82022-07-07 06:55:50 +00002583}
2584
2585MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002586static int ssl_tls13_session_load(mbedtls_ssl_session *session,
2587 const unsigned char *buf,
2588 size_t len)
Jerry Yu438ddd82022-07-07 06:55:50 +00002589{
2590 const unsigned char *p = buf;
2591 const unsigned char *end = buf + len;
2592
Gilles Peskine449bd832023-01-11 14:50:10 +01002593 if (end - p < 9) {
2594 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2595 }
Jerry Yu438ddd82022-07-07 06:55:50 +00002596 session->endpoint = p[0];
Gilles Peskine449bd832023-01-11 14:50:10 +01002597 session->ciphersuite = MBEDTLS_GET_UINT16_BE(p, 1);
2598 session->ticket_age_add = MBEDTLS_GET_UINT32_BE(p, 3);
Jerry Yu438ddd82022-07-07 06:55:50 +00002599 session->ticket_flags = p[7];
2600
2601 /* load resumption_key */
Jerry Yubc7c1a42022-07-21 22:57:37 +08002602 session->resumption_key_len = p[8];
Jerry Yu438ddd82022-07-07 06:55:50 +00002603 p += 9;
2604
Gilles Peskine449bd832023-01-11 14:50:10 +01002605 if (end - p < session->resumption_key_len) {
2606 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2607 }
Jerry Yu438ddd82022-07-07 06:55:50 +00002608
Gilles Peskine449bd832023-01-11 14:50:10 +01002609 if (sizeof(session->resumption_key) < session->resumption_key_len) {
2610 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2611 }
2612 memcpy(session->resumption_key, p, session->resumption_key_len);
Jerry Yubc7c1a42022-07-21 22:57:37 +08002613 p += session->resumption_key_len;
Jerry Yu438ddd82022-07-07 06:55:50 +00002614
2615#if defined(MBEDTLS_HAVE_TIME) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002616 if (session->endpoint == MBEDTLS_SSL_IS_SERVER) {
2617 if (end - p < 8) {
2618 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2619 }
2620 session->start = MBEDTLS_GET_UINT64_BE(p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002621 p += 8;
2622 }
2623#endif /* MBEDTLS_HAVE_TIME */
2624
2625#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002626 if (session->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002627#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01002628 defined(MBEDTLS_SSL_SESSION_TICKETS)
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002629 size_t hostname_len;
2630 /* load host name */
Gilles Peskine449bd832023-01-11 14:50:10 +01002631 if (end - p < 2) {
2632 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2633 }
2634 hostname_len = MBEDTLS_GET_UINT16_BE(p, 0);
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002635 p += 2;
2636
Gilles Peskine449bd832023-01-11 14:50:10 +01002637 if (end - p < (long int) hostname_len) {
2638 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2639 }
2640 if (hostname_len > 0) {
2641 session->hostname = mbedtls_calloc(1, hostname_len);
2642 if (session->hostname == NULL) {
2643 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2644 }
2645 memcpy(session->hostname, p, hostname_len);
Xiaokang Qianed0620c2022-10-12 06:58:13 +00002646 p += hostname_len;
2647 }
2648#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION &&
2649 MBEDTLS_SSL_SESSION_TICKETS */
2650
Jerry Yu438ddd82022-07-07 06:55:50 +00002651#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01002652 if (end - p < 8) {
2653 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2654 }
2655 session->ticket_received = MBEDTLS_GET_UINT64_BE(p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002656 p += 8;
2657#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01002658 if (end - p < 4) {
2659 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2660 }
2661 session->ticket_lifetime = MBEDTLS_GET_UINT32_BE(p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002662 p += 4;
2663
Gilles Peskine449bd832023-01-11 14:50:10 +01002664 if (end - p < 2) {
2665 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2666 }
2667 session->ticket_len = MBEDTLS_GET_UINT16_BE(p, 0);
Jerry Yu438ddd82022-07-07 06:55:50 +00002668 p += 2;
2669
Gilles Peskine449bd832023-01-11 14:50:10 +01002670 if (end - p < (long int) session->ticket_len) {
2671 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2672 }
2673 if (session->ticket_len > 0) {
2674 session->ticket = mbedtls_calloc(1, session->ticket_len);
2675 if (session->ticket == NULL) {
2676 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2677 }
2678 memcpy(session->ticket, p, session->ticket_len);
Jerry Yu438ddd82022-07-07 06:55:50 +00002679 p += session->ticket_len;
2680 }
2681 }
2682#endif /* MBEDTLS_SSL_CLI_C */
2683
Gilles Peskine449bd832023-01-11 14:50:10 +01002684 return 0;
Jerry Yu438ddd82022-07-07 06:55:50 +00002685
2686}
2687#else /* MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yue36fdd62022-08-17 21:31:36 +08002688MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01002689static int ssl_tls13_session_save(const mbedtls_ssl_session *session,
2690 unsigned char *buf,
2691 size_t buf_len,
2692 size_t *olen)
Jerry Yu251a12e2022-07-13 15:15:48 +08002693{
2694 ((void) session);
2695 ((void) buf);
2696 ((void) buf_len);
Jerry Yue36fdd62022-08-17 21:31:36 +08002697 *olen = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01002698 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Jerry Yu251a12e2022-07-13 15:15:48 +08002699}
Jerry Yu438ddd82022-07-07 06:55:50 +00002700
Gilles Peskine449bd832023-01-11 14:50:10 +01002701static int ssl_tls13_session_load(const mbedtls_ssl_session *session,
2702 unsigned char *buf,
2703 size_t buf_len)
Jerry Yu438ddd82022-07-07 06:55:50 +00002704{
2705 ((void) session);
2706 ((void) buf);
2707 ((void) buf_len);
Gilles Peskine449bd832023-01-11 14:50:10 +01002708 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Jerry Yu438ddd82022-07-07 06:55:50 +00002709}
2710#endif /* !MBEDTLS_SSL_SESSION_TICKETS */
Jerry Yu251a12e2022-07-13 15:15:48 +08002711#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
2712
Gilles Peskine449bd832023-01-11 14:50:10 +01002713psa_status_t mbedtls_ssl_cipher_to_psa(mbedtls_cipher_type_t mbedtls_cipher_type,
2714 size_t taglen,
2715 psa_algorithm_t *alg,
2716 psa_key_type_t *key_type,
2717 size_t *key_size)
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002718{
Gilles Peskine449bd832023-01-11 14:50:10 +01002719 switch (mbedtls_cipher_type) {
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002720 case MBEDTLS_CIPHER_AES_128_CBC:
2721 *alg = PSA_ALG_CBC_NO_PADDING;
2722 *key_type = PSA_KEY_TYPE_AES;
2723 *key_size = 128;
2724 break;
2725 case MBEDTLS_CIPHER_AES_128_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002726 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002727 *key_type = PSA_KEY_TYPE_AES;
2728 *key_size = 128;
2729 break;
2730 case MBEDTLS_CIPHER_AES_128_GCM:
2731 *alg = PSA_ALG_GCM;
2732 *key_type = PSA_KEY_TYPE_AES;
2733 *key_size = 128;
2734 break;
2735 case MBEDTLS_CIPHER_AES_192_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002736 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002737 *key_type = PSA_KEY_TYPE_AES;
2738 *key_size = 192;
2739 break;
2740 case MBEDTLS_CIPHER_AES_192_GCM:
2741 *alg = PSA_ALG_GCM;
2742 *key_type = PSA_KEY_TYPE_AES;
2743 *key_size = 192;
2744 break;
2745 case MBEDTLS_CIPHER_AES_256_CBC:
2746 *alg = PSA_ALG_CBC_NO_PADDING;
2747 *key_type = PSA_KEY_TYPE_AES;
2748 *key_size = 256;
2749 break;
2750 case MBEDTLS_CIPHER_AES_256_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002751 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002752 *key_type = PSA_KEY_TYPE_AES;
2753 *key_size = 256;
2754 break;
2755 case MBEDTLS_CIPHER_AES_256_GCM:
2756 *alg = PSA_ALG_GCM;
2757 *key_type = PSA_KEY_TYPE_AES;
2758 *key_size = 256;
2759 break;
2760 case MBEDTLS_CIPHER_ARIA_128_CBC:
2761 *alg = PSA_ALG_CBC_NO_PADDING;
2762 *key_type = PSA_KEY_TYPE_ARIA;
2763 *key_size = 128;
2764 break;
2765 case MBEDTLS_CIPHER_ARIA_128_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002766 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002767 *key_type = PSA_KEY_TYPE_ARIA;
2768 *key_size = 128;
2769 break;
2770 case MBEDTLS_CIPHER_ARIA_128_GCM:
2771 *alg = PSA_ALG_GCM;
2772 *key_type = PSA_KEY_TYPE_ARIA;
2773 *key_size = 128;
2774 break;
2775 case MBEDTLS_CIPHER_ARIA_192_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002776 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002777 *key_type = PSA_KEY_TYPE_ARIA;
2778 *key_size = 192;
2779 break;
2780 case MBEDTLS_CIPHER_ARIA_192_GCM:
2781 *alg = PSA_ALG_GCM;
2782 *key_type = PSA_KEY_TYPE_ARIA;
2783 *key_size = 192;
2784 break;
2785 case MBEDTLS_CIPHER_ARIA_256_CBC:
2786 *alg = PSA_ALG_CBC_NO_PADDING;
2787 *key_type = PSA_KEY_TYPE_ARIA;
2788 *key_size = 256;
2789 break;
2790 case MBEDTLS_CIPHER_ARIA_256_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002791 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002792 *key_type = PSA_KEY_TYPE_ARIA;
2793 *key_size = 256;
2794 break;
2795 case MBEDTLS_CIPHER_ARIA_256_GCM:
2796 *alg = PSA_ALG_GCM;
2797 *key_type = PSA_KEY_TYPE_ARIA;
2798 *key_size = 256;
2799 break;
2800 case MBEDTLS_CIPHER_CAMELLIA_128_CBC:
2801 *alg = PSA_ALG_CBC_NO_PADDING;
2802 *key_type = PSA_KEY_TYPE_CAMELLIA;
2803 *key_size = 128;
2804 break;
2805 case MBEDTLS_CIPHER_CAMELLIA_128_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002806 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002807 *key_type = PSA_KEY_TYPE_CAMELLIA;
2808 *key_size = 128;
2809 break;
2810 case MBEDTLS_CIPHER_CAMELLIA_128_GCM:
2811 *alg = PSA_ALG_GCM;
2812 *key_type = PSA_KEY_TYPE_CAMELLIA;
2813 *key_size = 128;
2814 break;
2815 case MBEDTLS_CIPHER_CAMELLIA_192_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002816 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002817 *key_type = PSA_KEY_TYPE_CAMELLIA;
2818 *key_size = 192;
2819 break;
2820 case MBEDTLS_CIPHER_CAMELLIA_192_GCM:
2821 *alg = PSA_ALG_GCM;
2822 *key_type = PSA_KEY_TYPE_CAMELLIA;
2823 *key_size = 192;
2824 break;
2825 case MBEDTLS_CIPHER_CAMELLIA_256_CBC:
2826 *alg = PSA_ALG_CBC_NO_PADDING;
2827 *key_type = PSA_KEY_TYPE_CAMELLIA;
2828 *key_size = 256;
2829 break;
2830 case MBEDTLS_CIPHER_CAMELLIA_256_CCM:
Gilles Peskine449bd832023-01-11 14:50:10 +01002831 *alg = taglen ? PSA_ALG_AEAD_WITH_SHORTENED_TAG(PSA_ALG_CCM, taglen) : PSA_ALG_CCM;
Przemyslaw Stekiel430f3372022-01-10 11:55:46 +01002832 *key_type = PSA_KEY_TYPE_CAMELLIA;
2833 *key_size = 256;
2834 break;
2835 case MBEDTLS_CIPHER_CAMELLIA_256_GCM:
2836 *alg = PSA_ALG_GCM;
2837 *key_type = PSA_KEY_TYPE_CAMELLIA;
2838 *key_size = 256;
2839 break;
2840 case MBEDTLS_CIPHER_CHACHA20_POLY1305:
2841 *alg = PSA_ALG_CHACHA20_POLY1305;
2842 *key_type = PSA_KEY_TYPE_CHACHA20;
2843 *key_size = 256;
2844 break;
2845 case MBEDTLS_CIPHER_NULL:
2846 *alg = MBEDTLS_SSL_NULL_CIPHER;
2847 *key_type = 0;
2848 *key_size = 0;
2849 break;
2850 default:
2851 return PSA_ERROR_NOT_SUPPORTED;
2852 }
2853
2854 return PSA_SUCCESS;
2855}
Neil Armstrong8395d7a2022-05-18 11:44:56 +02002856#endif /* MBEDTLS_USE_PSA_CRYPTO || MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Beckerd20a8ca2018-10-22 15:31:26 +01002857
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002858#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002859int mbedtls_ssl_conf_dh_param_bin(mbedtls_ssl_config *conf,
2860 const unsigned char *dhm_P, size_t P_len,
2861 const unsigned char *dhm_G, size_t G_len)
Hanno Beckera90658f2017-10-04 15:29:08 +01002862{
Janos Follath865b3eb2019-12-16 11:46:15 +00002863 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Hanno Beckera90658f2017-10-04 15:29:08 +01002864
Gilles Peskine449bd832023-01-11 14:50:10 +01002865 mbedtls_mpi_free(&conf->dhm_P);
2866 mbedtls_mpi_free(&conf->dhm_G);
Glenn Strausscee11292021-12-20 01:43:17 -05002867
Gilles Peskine449bd832023-01-11 14:50:10 +01002868 if ((ret = mbedtls_mpi_read_binary(&conf->dhm_P, dhm_P, P_len)) != 0 ||
2869 (ret = mbedtls_mpi_read_binary(&conf->dhm_G, dhm_G, G_len)) != 0) {
2870 mbedtls_mpi_free(&conf->dhm_P);
2871 mbedtls_mpi_free(&conf->dhm_G);
2872 return ret;
Hanno Beckera90658f2017-10-04 15:29:08 +01002873 }
2874
Gilles Peskine449bd832023-01-11 14:50:10 +01002875 return 0;
Hanno Beckera90658f2017-10-04 15:29:08 +01002876}
Paul Bakker5121ce52009-01-03 21:22:43 +00002877
Gilles Peskine449bd832023-01-11 14:50:10 +01002878int mbedtls_ssl_conf_dh_param_ctx(mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx)
Paul Bakker1b57b062011-01-06 15:48:19 +00002879{
Janos Follath865b3eb2019-12-16 11:46:15 +00002880 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker1b57b062011-01-06 15:48:19 +00002881
Gilles Peskine449bd832023-01-11 14:50:10 +01002882 mbedtls_mpi_free(&conf->dhm_P);
2883 mbedtls_mpi_free(&conf->dhm_G);
Glenn Strausscee11292021-12-20 01:43:17 -05002884
Gilles Peskine449bd832023-01-11 14:50:10 +01002885 if ((ret = mbedtls_dhm_get_value(dhm_ctx, MBEDTLS_DHM_PARAM_P,
2886 &conf->dhm_P)) != 0 ||
2887 (ret = mbedtls_dhm_get_value(dhm_ctx, MBEDTLS_DHM_PARAM_G,
2888 &conf->dhm_G)) != 0) {
2889 mbedtls_mpi_free(&conf->dhm_P);
2890 mbedtls_mpi_free(&conf->dhm_G);
2891 return ret;
Manuel Pégourié-Gonnard1028b742015-05-06 17:33:07 +01002892 }
Paul Bakker1b57b062011-01-06 15:48:19 +00002893
Gilles Peskine449bd832023-01-11 14:50:10 +01002894 return 0;
Paul Bakker1b57b062011-01-06 15:48:19 +00002895}
Manuel Pégourié-Gonnardcf141ca2015-05-20 10:35:51 +02002896#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_SRV_C */
Paul Bakker1b57b062011-01-06 15:48:19 +00002897
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02002898#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
2899/*
2900 * Set the minimum length for Diffie-Hellman parameters
2901 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002902void mbedtls_ssl_conf_dhm_min_bitlen(mbedtls_ssl_config *conf,
2903 unsigned int bitlen)
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02002904{
2905 conf->dhm_min_bitlen = bitlen;
2906}
2907#endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
2908
Ronald Crone68ab4f2022-10-05 12:46:29 +02002909#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002910#if !defined(MBEDTLS_DEPRECATED_REMOVED) && defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002911/*
2912 * Set allowed/preferred hashes for handshake signatures
2913 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002914void mbedtls_ssl_conf_sig_hashes(mbedtls_ssl_config *conf,
2915 const int *hashes)
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002916{
2917 conf->sig_hashes = hashes;
2918}
Jerry Yu7ddc38c2022-01-19 11:08:05 +08002919#endif /* !MBEDTLS_DEPRECATED_REMOVED && MBEDTLS_SSL_PROTO_TLS1_2 */
Hanno Becker1cd6e002021-08-10 13:27:10 +01002920
Jerry Yuf017ee42022-01-12 15:49:48 +08002921/* Configure allowed signature algorithms for handshake */
Gilles Peskine449bd832023-01-11 14:50:10 +01002922void mbedtls_ssl_conf_sig_algs(mbedtls_ssl_config *conf,
2923 const uint16_t *sig_algs)
Hanno Becker1cd6e002021-08-10 13:27:10 +01002924{
Jerry Yuf017ee42022-01-12 15:49:48 +08002925#if !defined(MBEDTLS_DEPRECATED_REMOVED)
2926 conf->sig_hashes = NULL;
2927#endif /* !MBEDTLS_DEPRECATED_REMOVED */
2928 conf->sig_algs = sig_algs;
Hanno Becker1cd6e002021-08-10 13:27:10 +01002929}
Ronald Crone68ab4f2022-10-05 12:46:29 +02002930#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnard36a8b572015-06-17 12:43:26 +02002931
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02002932#if defined(MBEDTLS_ECP_C)
Brett Warrene0edc842021-08-17 09:53:13 +01002933#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002934/*
2935 * Set the allowed elliptic curves
Brett Warrene0edc842021-08-17 09:53:13 +01002936 *
2937 * mbedtls_ssl_setup() takes the provided list
2938 * and translates it to a list of IANA TLS group identifiers,
2939 * stored in ssl->handshake->group_list.
2940 *
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002941 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002942void mbedtls_ssl_conf_curves(mbedtls_ssl_config *conf,
2943 const mbedtls_ecp_group_id *curve_list)
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002944{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02002945 conf->curve_list = curve_list;
Brett Warrene0edc842021-08-17 09:53:13 +01002946 conf->group_list = NULL;
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002947}
Brett Warrene0edc842021-08-17 09:53:13 +01002948#endif /* MBEDTLS_DEPRECATED_REMOVED */
Hanno Becker947194e2017-04-07 13:25:49 +01002949#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f38ed02014-02-04 15:52:33 +01002950
Brett Warrene0edc842021-08-17 09:53:13 +01002951/*
2952 * Set the allowed groups
2953 */
Gilles Peskine449bd832023-01-11 14:50:10 +01002954void mbedtls_ssl_conf_groups(mbedtls_ssl_config *conf,
2955 const uint16_t *group_list)
Brett Warrene0edc842021-08-17 09:53:13 +01002956{
2957#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
2958 conf->curve_list = NULL;
2959#endif
2960 conf->group_list = group_list;
2961}
2962
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01002963#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01002964int mbedtls_ssl_set_hostname(mbedtls_ssl_context *ssl, const char *hostname)
Paul Bakker5121ce52009-01-03 21:22:43 +00002965{
Hanno Becker947194e2017-04-07 13:25:49 +01002966 /* Initialize to suppress unnecessary compiler warning */
2967 size_t hostname_len = 0;
2968
2969 /* Check if new hostname is valid before
2970 * making any change to current one */
Gilles Peskine449bd832023-01-11 14:50:10 +01002971 if (hostname != NULL) {
2972 hostname_len = strlen(hostname);
Hanno Becker947194e2017-04-07 13:25:49 +01002973
Gilles Peskine449bd832023-01-11 14:50:10 +01002974 if (hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN) {
2975 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
2976 }
Hanno Becker947194e2017-04-07 13:25:49 +01002977 }
2978
2979 /* Now it's clear that we will overwrite the old hostname,
2980 * so we can free it safely */
2981
Gilles Peskine449bd832023-01-11 14:50:10 +01002982 if (ssl->hostname != NULL) {
2983 mbedtls_platform_zeroize(ssl->hostname, strlen(ssl->hostname));
2984 mbedtls_free(ssl->hostname);
Hanno Becker947194e2017-04-07 13:25:49 +01002985 }
2986
2987 /* Passing NULL as hostname shall clear the old one */
Manuel Pégourié-Gonnardba26c242015-05-06 10:47:06 +01002988
Gilles Peskine449bd832023-01-11 14:50:10 +01002989 if (hostname == NULL) {
Hanno Becker947194e2017-04-07 13:25:49 +01002990 ssl->hostname = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01002991 } else {
2992 ssl->hostname = mbedtls_calloc(1, hostname_len + 1);
2993 if (ssl->hostname == NULL) {
2994 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
2995 }
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002996
Gilles Peskine449bd832023-01-11 14:50:10 +01002997 memcpy(ssl->hostname, hostname, hostname_len);
Paul Bakker75c1a6f2013-08-19 14:25:29 +02002998
Hanno Becker947194e2017-04-07 13:25:49 +01002999 ssl->hostname[hostname_len] = '\0';
3000 }
Paul Bakker5121ce52009-01-03 21:22:43 +00003001
Gilles Peskine449bd832023-01-11 14:50:10 +01003002 return 0;
Paul Bakker5121ce52009-01-03 21:22:43 +00003003}
Hanno Becker1a9a51c2017-04-07 13:02:16 +01003004#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00003005
Manuel Pégourié-Gonnardbc2b7712015-05-06 11:14:19 +01003006#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01003007void mbedtls_ssl_conf_sni(mbedtls_ssl_config *conf,
3008 int (*f_sni)(void *, mbedtls_ssl_context *,
3009 const unsigned char *, size_t),
3010 void *p_sni)
Paul Bakker5701cdc2012-09-27 21:49:42 +00003011{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003012 conf->f_sni = f_sni;
3013 conf->p_sni = p_sni;
Paul Bakker5701cdc2012-09-27 21:49:42 +00003014}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003015#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
Paul Bakker5701cdc2012-09-27 21:49:42 +00003016
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003017#if defined(MBEDTLS_SSL_ALPN)
Gilles Peskine449bd832023-01-11 14:50:10 +01003018int mbedtls_ssl_conf_alpn_protocols(mbedtls_ssl_config *conf, const char **protos)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003019{
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02003020 size_t cur_len, tot_len;
3021 const char **p;
3022
3023 /*
Brian J Murray1903fb32016-11-06 04:45:15 -08003024 * RFC 7301 3.1: "Empty strings MUST NOT be included and byte strings
3025 * MUST NOT be truncated."
3026 * We check lengths now rather than later.
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02003027 */
3028 tot_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003029 for (p = protos; *p != NULL; p++) {
3030 cur_len = strlen(*p);
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02003031 tot_len += cur_len;
3032
Gilles Peskine449bd832023-01-11 14:50:10 +01003033 if ((cur_len == 0) ||
3034 (cur_len > MBEDTLS_SSL_MAX_ALPN_NAME_LEN) ||
3035 (tot_len > MBEDTLS_SSL_MAX_ALPN_LIST_LEN)) {
3036 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3037 }
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02003038 }
3039
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003040 conf->alpn_list = protos;
Manuel Pégourié-Gonnard0b874dc2014-04-07 10:57:45 +02003041
Gilles Peskine449bd832023-01-11 14:50:10 +01003042 return 0;
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003043}
3044
Gilles Peskine449bd832023-01-11 14:50:10 +01003045const char *mbedtls_ssl_get_alpn_protocol(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003046{
Gilles Peskine449bd832023-01-11 14:50:10 +01003047 return ssl->alpn_chosen;
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003048}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003049#endif /* MBEDTLS_SSL_ALPN */
Manuel Pégourié-Gonnard7e250d42014-04-04 16:08:41 +02003050
Johan Pascalb62bb512015-12-03 21:56:45 +01003051#if defined(MBEDTLS_SSL_DTLS_SRTP)
Gilles Peskine449bd832023-01-11 14:50:10 +01003052void mbedtls_ssl_conf_srtp_mki_value_supported(mbedtls_ssl_config *conf,
3053 int support_mki_value)
Ron Eldor591f1622018-01-22 12:30:04 +02003054{
3055 conf->dtls_srtp_mki_support = support_mki_value;
3056}
3057
Gilles Peskine449bd832023-01-11 14:50:10 +01003058int mbedtls_ssl_dtls_srtp_set_mki_value(mbedtls_ssl_context *ssl,
3059 unsigned char *mki_value,
3060 uint16_t mki_len)
Ron Eldor591f1622018-01-22 12:30:04 +02003061{
Gilles Peskine449bd832023-01-11 14:50:10 +01003062 if (mki_len > MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH) {
3063 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Ron Eldora9788042018-12-05 11:04:31 +02003064 }
Ron Eldor591f1622018-01-22 12:30:04 +02003065
Gilles Peskine449bd832023-01-11 14:50:10 +01003066 if (ssl->conf->dtls_srtp_mki_support == MBEDTLS_SSL_DTLS_SRTP_MKI_UNSUPPORTED) {
3067 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Ron Eldora9788042018-12-05 11:04:31 +02003068 }
Ron Eldor591f1622018-01-22 12:30:04 +02003069
Gilles Peskine449bd832023-01-11 14:50:10 +01003070 memcpy(ssl->dtls_srtp_info.mki_value, mki_value, mki_len);
Ron Eldor591f1622018-01-22 12:30:04 +02003071 ssl->dtls_srtp_info.mki_len = mki_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01003072 return 0;
Ron Eldor591f1622018-01-22 12:30:04 +02003073}
3074
Gilles Peskine449bd832023-01-11 14:50:10 +01003075int mbedtls_ssl_conf_dtls_srtp_protection_profiles(mbedtls_ssl_config *conf,
3076 const mbedtls_ssl_srtp_profile *profiles)
Johan Pascalb62bb512015-12-03 21:56:45 +01003077{
Johan Pascal253d0262020-09-22 13:04:45 +02003078 const mbedtls_ssl_srtp_profile *p;
3079 size_t list_size = 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01003080
Johan Pascal253d0262020-09-22 13:04:45 +02003081 /* check the profiles list: all entry must be valid,
3082 * its size cannot be more than the total number of supported profiles, currently 4 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003083 for (p = profiles; *p != MBEDTLS_TLS_SRTP_UNSET &&
3084 list_size <= MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH;
3085 p++) {
3086 if (mbedtls_ssl_check_srtp_profile_value(*p) != MBEDTLS_TLS_SRTP_UNSET) {
Johan Pascal76fdf1d2020-10-22 23:31:00 +02003087 list_size++;
Gilles Peskine449bd832023-01-11 14:50:10 +01003088 } else {
Johan Pascal76fdf1d2020-10-22 23:31:00 +02003089 /* unsupported value, stop parsing and set the size to an error value */
3090 list_size = MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH + 1;
Johan Pascalb62bb512015-12-03 21:56:45 +01003091 }
3092 }
3093
Gilles Peskine449bd832023-01-11 14:50:10 +01003094 if (list_size > MBEDTLS_TLS_SRTP_MAX_PROFILE_LIST_LENGTH) {
3095 conf->dtls_srtp_profile_list = NULL;
3096 conf->dtls_srtp_profile_list_len = 0;
3097 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Johan Pascal253d0262020-09-22 13:04:45 +02003098 }
3099
Johan Pascal9bc97ca2020-09-21 23:44:45 +02003100 conf->dtls_srtp_profile_list = profiles;
Johan Pascal253d0262020-09-22 13:04:45 +02003101 conf->dtls_srtp_profile_list_len = list_size;
Johan Pascalb62bb512015-12-03 21:56:45 +01003102
Gilles Peskine449bd832023-01-11 14:50:10 +01003103 return 0;
Johan Pascalb62bb512015-12-03 21:56:45 +01003104}
3105
Gilles Peskine449bd832023-01-11 14:50:10 +01003106void mbedtls_ssl_get_dtls_srtp_negotiation_result(const mbedtls_ssl_context *ssl,
3107 mbedtls_dtls_srtp_info *dtls_srtp_info)
Johan Pascalb62bb512015-12-03 21:56:45 +01003108{
Johan Pascal2258a4f2020-10-28 13:53:09 +01003109 dtls_srtp_info->chosen_dtls_srtp_profile = ssl->dtls_srtp_info.chosen_dtls_srtp_profile;
3110 /* do not copy the mki value if there is no chosen profile */
Gilles Peskine449bd832023-01-11 14:50:10 +01003111 if (dtls_srtp_info->chosen_dtls_srtp_profile == MBEDTLS_TLS_SRTP_UNSET) {
Johan Pascal2258a4f2020-10-28 13:53:09 +01003112 dtls_srtp_info->mki_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003113 } else {
Johan Pascal2258a4f2020-10-28 13:53:09 +01003114 dtls_srtp_info->mki_len = ssl->dtls_srtp_info.mki_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01003115 memcpy(dtls_srtp_info->mki_value, ssl->dtls_srtp_info.mki_value,
3116 ssl->dtls_srtp_info.mki_len);
Johan Pascal2258a4f2020-10-28 13:53:09 +01003117 }
Johan Pascalb62bb512015-12-03 21:56:45 +01003118}
Johan Pascalb62bb512015-12-03 21:56:45 +01003119#endif /* MBEDTLS_SSL_DTLS_SRTP */
3120
Aditya Patwardhan3096f332022-07-26 14:31:46 +05303121#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01003122void mbedtls_ssl_conf_max_version(mbedtls_ssl_config *conf, int major, int minor)
Paul Bakker490ecc82011-10-06 13:04:09 +00003123{
Glenn Strauss2dfcea22022-03-14 17:26:42 -04003124 conf->max_tls_version = (major << 8) | minor;
Paul Bakker490ecc82011-10-06 13:04:09 +00003125}
3126
Gilles Peskine449bd832023-01-11 14:50:10 +01003127void mbedtls_ssl_conf_min_version(mbedtls_ssl_config *conf, int major, int minor)
Paul Bakker1d29fb52012-09-28 13:28:45 +00003128{
Glenn Strauss2dfcea22022-03-14 17:26:42 -04003129 conf->min_tls_version = (major << 8) | minor;
Paul Bakker1d29fb52012-09-28 13:28:45 +00003130}
Aditya Patwardhan3096f332022-07-26 14:31:46 +05303131#endif /* MBEDTLS_DEPRECATED_REMOVED */
Paul Bakker1d29fb52012-09-28 13:28:45 +00003132
Janos Follath088ce432017-04-10 12:42:31 +01003133#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003134void mbedtls_ssl_conf_cert_req_ca_list(mbedtls_ssl_config *conf,
3135 char cert_req_ca_list)
Janos Follath088ce432017-04-10 12:42:31 +01003136{
3137 conf->cert_req_ca_list = cert_req_ca_list;
3138}
3139#endif
3140
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003141#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01003142void mbedtls_ssl_conf_encrypt_then_mac(mbedtls_ssl_config *conf, char etm)
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01003143{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003144 conf->encrypt_then_mac = etm;
Manuel Pégourié-Gonnard699cafa2014-10-27 13:57:03 +01003145}
3146#endif
3147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003148#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Gilles Peskine449bd832023-01-11 14:50:10 +01003149void mbedtls_ssl_conf_extended_master_secret(mbedtls_ssl_config *conf, char ems)
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02003150{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003151 conf->extended_ms = ems;
Manuel Pégourié-Gonnard367381f2014-10-20 18:40:56 +02003152}
3153#endif
3154
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003155#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01003156int mbedtls_ssl_conf_max_frag_len(mbedtls_ssl_config *conf, unsigned char mfl_code)
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003157{
Gilles Peskine449bd832023-01-11 14:50:10 +01003158 if (mfl_code >= MBEDTLS_SSL_MAX_FRAG_LEN_INVALID ||
3159 ssl_mfl_code_to_length(mfl_code) > MBEDTLS_TLS_EXT_ADV_CONTENT_LEN) {
3160 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003161 }
3162
Manuel Pégourié-Gonnard6bf89d62015-05-05 17:01:57 +01003163 conf->mfl_code = mfl_code;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003164
Gilles Peskine449bd832023-01-11 14:50:10 +01003165 return 0;
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003166}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003167#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
Manuel Pégourié-Gonnard8b464592013-07-16 12:45:26 +02003168
Gilles Peskine449bd832023-01-11 14:50:10 +01003169void mbedtls_ssl_conf_legacy_renegotiation(mbedtls_ssl_config *conf, int allow_legacy)
Paul Bakker48916f92012-09-16 19:57:18 +00003170{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003171 conf->allow_legacy_renegotiation = allow_legacy;
Paul Bakker48916f92012-09-16 19:57:18 +00003172}
3173
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003174#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01003175void mbedtls_ssl_conf_renegotiation(mbedtls_ssl_config *conf, int renegotiation)
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003176{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003177 conf->disable_renegotiation = renegotiation;
Manuel Pégourié-Gonnard615e6772014-11-03 08:23:14 +01003178}
3179
Gilles Peskine449bd832023-01-11 14:50:10 +01003180void mbedtls_ssl_conf_renegotiation_enforced(mbedtls_ssl_config *conf, int max_records)
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003181{
Manuel Pégourié-Gonnardd36e33f2015-05-05 10:45:39 +02003182 conf->renego_max_records = max_records;
Manuel Pégourié-Gonnarda9964db2014-07-03 19:29:16 +02003183}
3184
Gilles Peskine449bd832023-01-11 14:50:10 +01003185void mbedtls_ssl_conf_renegotiation_period(mbedtls_ssl_config *conf,
3186 const unsigned char period[8])
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01003187{
Gilles Peskine449bd832023-01-11 14:50:10 +01003188 memcpy(conf->renego_period, period, 8);
Manuel Pégourié-Gonnard837f0fe2014-11-05 13:58:53 +01003189}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003190#endif /* MBEDTLS_SSL_RENEGOTIATION */
Paul Bakker5121ce52009-01-03 21:22:43 +00003191
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003192#if defined(MBEDTLS_SSL_SESSION_TICKETS)
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003193#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003194void mbedtls_ssl_conf_session_tickets(mbedtls_ssl_config *conf, int use_tickets)
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003195{
Manuel Pégourié-Gonnard2b494452015-05-06 10:05:11 +01003196 conf->session_tickets = use_tickets;
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003197}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003198#endif
Paul Bakker606b4ba2013-08-14 16:52:14 +02003199
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003200#if defined(MBEDTLS_SSL_SRV_C)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08003201
Jerry Yud0766ec2022-09-22 10:46:57 +08003202#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003203void mbedtls_ssl_conf_new_session_tickets(mbedtls_ssl_config *conf,
3204 uint16_t num_tickets)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08003205{
Jerry Yud0766ec2022-09-22 10:46:57 +08003206 conf->new_session_tickets_count = num_tickets;
Jerry Yu1ad7ace2022-08-09 13:28:39 +08003207}
3208#endif
3209
Gilles Peskine449bd832023-01-11 14:50:10 +01003210void mbedtls_ssl_conf_session_tickets_cb(mbedtls_ssl_config *conf,
3211 mbedtls_ssl_ticket_write_t *f_ticket_write,
3212 mbedtls_ssl_ticket_parse_t *f_ticket_parse,
3213 void *p_ticket)
Paul Bakker606b4ba2013-08-14 16:52:14 +02003214{
Manuel Pégourié-Gonnardd59675d2015-05-19 15:28:00 +02003215 conf->f_ticket_write = f_ticket_write;
3216 conf->f_ticket_parse = f_ticket_parse;
3217 conf->p_ticket = p_ticket;
Paul Bakker606b4ba2013-08-14 16:52:14 +02003218}
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02003219#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003220#endif /* MBEDTLS_SSL_SESSION_TICKETS */
Manuel Pégourié-Gonnardaa0d4d12013-08-03 13:02:31 +02003221
Gilles Peskine449bd832023-01-11 14:50:10 +01003222void mbedtls_ssl_set_export_keys_cb(mbedtls_ssl_context *ssl,
3223 mbedtls_ssl_export_keys_t *f_export_keys,
3224 void *p_export_keys)
Robert Cragie4feb7ae2015-10-02 13:33:37 +01003225{
Hanno Becker7e6c1782021-06-08 09:24:55 +01003226 ssl->f_export_keys = f_export_keys;
3227 ssl->p_export_keys = p_export_keys;
Ron Eldorf5cc10d2019-05-07 18:33:40 +03003228}
Robert Cragie4feb7ae2015-10-02 13:33:37 +01003229
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003230#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003231void mbedtls_ssl_conf_async_private_cb(
3232 mbedtls_ssl_config *conf,
3233 mbedtls_ssl_async_sign_t *f_async_sign,
3234 mbedtls_ssl_async_decrypt_t *f_async_decrypt,
3235 mbedtls_ssl_async_resume_t *f_async_resume,
3236 mbedtls_ssl_async_cancel_t *f_async_cancel,
Gilles Peskine449bd832023-01-11 14:50:10 +01003237 void *async_config_data)
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003238{
3239 conf->f_async_sign_start = f_async_sign;
3240 conf->f_async_decrypt_start = f_async_decrypt;
3241 conf->f_async_resume = f_async_resume;
3242 conf->f_async_cancel = f_async_cancel;
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003243 conf->p_async_config_data = async_config_data;
3244}
3245
Gilles Peskine449bd832023-01-11 14:50:10 +01003246void *mbedtls_ssl_conf_get_async_config_data(const mbedtls_ssl_config *conf)
Gilles Peskine8f97af72018-04-26 11:46:10 +02003247{
Gilles Peskine449bd832023-01-11 14:50:10 +01003248 return conf->p_async_config_data;
Gilles Peskine8f97af72018-04-26 11:46:10 +02003249}
3250
Gilles Peskine449bd832023-01-11 14:50:10 +01003251void *mbedtls_ssl_get_async_operation_data(const mbedtls_ssl_context *ssl)
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003252{
Gilles Peskine449bd832023-01-11 14:50:10 +01003253 if (ssl->handshake == NULL) {
3254 return NULL;
3255 } else {
3256 return ssl->handshake->user_async_ctx;
3257 }
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003258}
3259
Gilles Peskine449bd832023-01-11 14:50:10 +01003260void mbedtls_ssl_set_async_operation_data(mbedtls_ssl_context *ssl,
3261 void *ctx)
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003262{
Gilles Peskine449bd832023-01-11 14:50:10 +01003263 if (ssl->handshake != NULL) {
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02003264 ssl->handshake->user_async_ctx = ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +01003265 }
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003266}
Gilles Peskineb74a1c72018-04-24 13:09:22 +02003267#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
Gilles Peskine8bf79f62018-01-05 21:11:53 +01003268
Paul Bakker5121ce52009-01-03 21:22:43 +00003269/*
3270 * SSL get accessors
3271 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003272uint32_t mbedtls_ssl_get_verify_result(const mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00003273{
Gilles Peskine449bd832023-01-11 14:50:10 +01003274 if (ssl->session != NULL) {
3275 return ssl->session->verify_result;
3276 }
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00003277
Gilles Peskine449bd832023-01-11 14:50:10 +01003278 if (ssl->session_negotiate != NULL) {
3279 return ssl->session_negotiate->verify_result;
3280 }
Manuel Pégourié-Gonnarde89163c2015-01-23 14:30:57 +00003281
Gilles Peskine449bd832023-01-11 14:50:10 +01003282 return 0xFFFFFFFF;
Paul Bakker5121ce52009-01-03 21:22:43 +00003283}
3284
Gilles Peskine449bd832023-01-11 14:50:10 +01003285int mbedtls_ssl_get_ciphersuite_id_from_ssl(const mbedtls_ssl_context *ssl)
Glenn Strauss8f526902022-01-13 00:04:49 -05003286{
Gilles Peskine449bd832023-01-11 14:50:10 +01003287 if (ssl == NULL || ssl->session == NULL) {
3288 return 0;
3289 }
Glenn Strauss8f526902022-01-13 00:04:49 -05003290
Gilles Peskine449bd832023-01-11 14:50:10 +01003291 return ssl->session->ciphersuite;
Glenn Strauss8f526902022-01-13 00:04:49 -05003292}
3293
Gilles Peskine449bd832023-01-11 14:50:10 +01003294const char *mbedtls_ssl_get_ciphersuite(const mbedtls_ssl_context *ssl)
Paul Bakker72f62662011-01-16 21:27:44 +00003295{
Gilles Peskine449bd832023-01-11 14:50:10 +01003296 if (ssl == NULL || ssl->session == NULL) {
3297 return NULL;
3298 }
Paul Bakker926c8e42013-03-06 10:23:34 +01003299
Gilles Peskine449bd832023-01-11 14:50:10 +01003300 return mbedtls_ssl_get_ciphersuite_name(ssl->session->ciphersuite);
Paul Bakker72f62662011-01-16 21:27:44 +00003301}
3302
Gilles Peskine449bd832023-01-11 14:50:10 +01003303const char *mbedtls_ssl_get_version(const mbedtls_ssl_context *ssl)
Paul Bakker43ca69c2011-01-15 17:35:19 +00003304{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003305#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003306 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
3307 switch (ssl->tls_version) {
Glenn Strauss60bfe602022-03-14 19:04:24 -04003308 case MBEDTLS_SSL_VERSION_TLS1_2:
Gilles Peskine449bd832023-01-11 14:50:10 +01003309 return "DTLSv1.2";
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003310 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01003311 return "unknown (DTLS)";
Manuel Pégourié-Gonnardb21ca2a2014-02-10 13:43:33 +01003312 }
3313 }
3314#endif
3315
Gilles Peskine449bd832023-01-11 14:50:10 +01003316 switch (ssl->tls_version) {
Glenn Strauss60bfe602022-03-14 19:04:24 -04003317 case MBEDTLS_SSL_VERSION_TLS1_2:
Gilles Peskine449bd832023-01-11 14:50:10 +01003318 return "TLSv1.2";
Glenn Strauss60bfe602022-03-14 19:04:24 -04003319 case MBEDTLS_SSL_VERSION_TLS1_3:
Gilles Peskine449bd832023-01-11 14:50:10 +01003320 return "TLSv1.3";
Paul Bakker43ca69c2011-01-15 17:35:19 +00003321 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01003322 return "unknown";
Paul Bakker43ca69c2011-01-15 17:35:19 +00003323 }
Paul Bakker43ca69c2011-01-15 17:35:19 +00003324}
3325
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003326#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01003327size_t mbedtls_ssl_get_input_max_frag_len(const mbedtls_ssl_context *ssl)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003328{
David Horstmann95d516f2021-05-04 18:36:56 +01003329 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003330 size_t read_mfl;
3331
Jerry Yuddda0502022-12-01 19:43:12 +08003332#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003333 /* Use the configured MFL for the client if we're past SERVER_HELLO_DONE */
Gilles Peskine449bd832023-01-11 14:50:10 +01003334 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
3335 ssl->state >= MBEDTLS_SSL_SERVER_HELLO_DONE) {
3336 return ssl_mfl_code_to_length(ssl->conf->mfl_code);
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003337 }
Jerry Yuddda0502022-12-01 19:43:12 +08003338#endif
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003339
3340 /* Check if a smaller max length was negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01003341 if (ssl->session_out != NULL) {
3342 read_mfl = ssl_mfl_code_to_length(ssl->session_out->mfl_code);
3343 if (read_mfl < max_len) {
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003344 max_len = read_mfl;
3345 }
3346 }
3347
Jerry Yuddda0502022-12-01 19:43:12 +08003348 /* During a handshake, use the value being negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01003349 if (ssl->session_negotiate != NULL) {
3350 read_mfl = ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code);
3351 if (read_mfl < max_len) {
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003352 max_len = read_mfl;
3353 }
3354 }
3355
Gilles Peskine449bd832023-01-11 14:50:10 +01003356 return max_len;
Andrzej Kurek90c6e842020-04-03 05:25:29 -04003357}
3358
Gilles Peskine449bd832023-01-11 14:50:10 +01003359size_t mbedtls_ssl_get_output_max_frag_len(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003360{
3361 size_t max_len;
3362
3363 /*
3364 * Assume mfl_code is correct since it was checked when set
3365 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003366 max_len = ssl_mfl_code_to_length(ssl->conf->mfl_code);
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003367
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02003368 /* Check if a smaller max length was negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01003369 if (ssl->session_out != NULL &&
3370 ssl_mfl_code_to_length(ssl->session_out->mfl_code) < max_len) {
3371 max_len = ssl_mfl_code_to_length(ssl->session_out->mfl_code);
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003372 }
3373
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02003374 /* During a handshake, use the value being negotiated */
Gilles Peskine449bd832023-01-11 14:50:10 +01003375 if (ssl->session_negotiate != NULL &&
3376 ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code) < max_len) {
3377 max_len = ssl_mfl_code_to_length(ssl->session_negotiate->mfl_code);
Manuel Pégourié-Gonnard2cb17e22017-09-19 13:00:47 +02003378 }
3379
Gilles Peskine449bd832023-01-11 14:50:10 +01003380 return max_len;
Manuel Pégourié-Gonnarda2cda6b2015-08-31 18:30:52 +02003381}
3382#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
3383
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003384#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003385size_t mbedtls_ssl_get_current_mtu(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003386{
Andrzej Kurekef43ce62018-10-09 08:24:12 -04003387 /* Return unlimited mtu for client hello messages to avoid fragmentation. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003388 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
3389 (ssl->state == MBEDTLS_SSL_CLIENT_HELLO ||
3390 ssl->state == MBEDTLS_SSL_SERVER_HELLO)) {
3391 return 0;
3392 }
Andrzej Kurekef43ce62018-10-09 08:24:12 -04003393
Gilles Peskine449bd832023-01-11 14:50:10 +01003394 if (ssl->handshake == NULL || ssl->handshake->mtu == 0) {
3395 return ssl->mtu;
3396 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003397
Gilles Peskine449bd832023-01-11 14:50:10 +01003398 if (ssl->mtu == 0) {
3399 return ssl->handshake->mtu;
3400 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003401
Gilles Peskine449bd832023-01-11 14:50:10 +01003402 return ssl->mtu < ssl->handshake->mtu ?
3403 ssl->mtu : ssl->handshake->mtu;
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003404}
3405#endif /* MBEDTLS_SSL_PROTO_DTLS */
3406
Gilles Peskine449bd832023-01-11 14:50:10 +01003407int mbedtls_ssl_get_max_out_record_payload(const mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003408{
3409 size_t max_len = MBEDTLS_SSL_OUT_CONTENT_LEN;
3410
Manuel Pégourié-Gonnard000281e2018-08-21 11:20:58 +02003411#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
3412 !defined(MBEDTLS_SSL_PROTO_DTLS)
3413 (void) ssl;
3414#endif
3415
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003416#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01003417 const size_t mfl = mbedtls_ssl_get_output_max_frag_len(ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003418
Gilles Peskine449bd832023-01-11 14:50:10 +01003419 if (max_len > mfl) {
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003420 max_len = mfl;
Gilles Peskine449bd832023-01-11 14:50:10 +01003421 }
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003422#endif
3423
3424#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003425 if (mbedtls_ssl_get_current_mtu(ssl) != 0) {
3426 const size_t mtu = mbedtls_ssl_get_current_mtu(ssl);
3427 const int ret = mbedtls_ssl_get_record_expansion(ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003428 const size_t overhead = (size_t) ret;
3429
Gilles Peskine449bd832023-01-11 14:50:10 +01003430 if (ret < 0) {
3431 return ret;
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003432 }
3433
Gilles Peskine449bd832023-01-11 14:50:10 +01003434 if (mtu <= overhead) {
3435 MBEDTLS_SSL_DEBUG_MSG(1, ("MTU too low for record expansion"));
3436 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
3437 }
3438
3439 if (max_len > mtu - overhead) {
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003440 max_len = mtu - overhead;
Gilles Peskine449bd832023-01-11 14:50:10 +01003441 }
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003442 }
Manuel Pégourié-Gonnardb8eec192018-08-20 09:34:02 +02003443#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003444
Hanno Becker0defedb2018-08-10 12:35:02 +01003445#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH) && \
3446 !defined(MBEDTLS_SSL_PROTO_DTLS)
3447 ((void) ssl);
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003448#endif
3449
Gilles Peskine449bd832023-01-11 14:50:10 +01003450 return (int) max_len;
Manuel Pégourié-Gonnard9468ff12017-09-21 13:49:50 +02003451}
3452
Gilles Peskine449bd832023-01-11 14:50:10 +01003453int mbedtls_ssl_get_max_in_record_payload(const mbedtls_ssl_context *ssl)
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003454{
3455 size_t max_len = MBEDTLS_SSL_IN_CONTENT_LEN;
3456
3457#if !defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
3458 (void) ssl;
3459#endif
3460
3461#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01003462 const size_t mfl = mbedtls_ssl_get_input_max_frag_len(ssl);
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003463
Gilles Peskine449bd832023-01-11 14:50:10 +01003464 if (max_len > mfl) {
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003465 max_len = mfl;
Gilles Peskine449bd832023-01-11 14:50:10 +01003466 }
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003467#endif
3468
Gilles Peskine449bd832023-01-11 14:50:10 +01003469 return (int) max_len;
Hanno Becker2d8e99b2021-04-21 06:19:50 +01003470}
3471
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003472#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003473const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert(const mbedtls_ssl_context *ssl)
Paul Bakkerb0550d92012-10-30 07:51:03 +00003474{
Gilles Peskine449bd832023-01-11 14:50:10 +01003475 if (ssl == NULL || ssl->session == NULL) {
3476 return NULL;
3477 }
Paul Bakkerb0550d92012-10-30 07:51:03 +00003478
Hanno Beckere6824572019-02-07 13:18:46 +00003479#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01003480 return ssl->session->peer_cert;
Hanno Beckere6824572019-02-07 13:18:46 +00003481#else
Gilles Peskine449bd832023-01-11 14:50:10 +01003482 return NULL;
Hanno Beckere6824572019-02-07 13:18:46 +00003483#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Paul Bakkerb0550d92012-10-30 07:51:03 +00003484}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003485#endif /* MBEDTLS_X509_CRT_PARSE_C */
Paul Bakkerb0550d92012-10-30 07:51:03 +00003486
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003487#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003488int mbedtls_ssl_get_session(const mbedtls_ssl_context *ssl,
3489 mbedtls_ssl_session *dst)
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003490{
Hanno Beckere810bbc2021-05-14 16:01:05 +01003491 int ret;
3492
Gilles Peskine449bd832023-01-11 14:50:10 +01003493 if (ssl == NULL ||
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003494 dst == NULL ||
3495 ssl->session == NULL ||
Gilles Peskine449bd832023-01-11 14:50:10 +01003496 ssl->conf->endpoint != MBEDTLS_SSL_IS_CLIENT) {
3497 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003498 }
3499
Hanno Beckere810bbc2021-05-14 16:01:05 +01003500 /* Since Mbed TLS 3.0, mbedtls_ssl_get_session() is no longer
3501 * idempotent: Each session can only be exported once.
3502 *
3503 * (This is in preparation for TLS 1.3 support where we will
3504 * need the ability to export multiple sessions (aka tickets),
3505 * which will be achieved by calling mbedtls_ssl_get_session()
3506 * multiple times until it fails.)
3507 *
3508 * Check whether we have already exported the current session,
3509 * and fail if so.
3510 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003511 if (ssl->session->exported == 1) {
3512 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
3513 }
Hanno Beckere810bbc2021-05-14 16:01:05 +01003514
Gilles Peskine449bd832023-01-11 14:50:10 +01003515 ret = mbedtls_ssl_session_copy(dst, ssl->session);
3516 if (ret != 0) {
3517 return ret;
3518 }
Hanno Beckere810bbc2021-05-14 16:01:05 +01003519
3520 /* Remember that we've exported the session. */
3521 ssl->session->exported = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003522 return 0;
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003523}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003524#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard74718032013-07-30 12:41:56 +02003525
Paul Bakker5121ce52009-01-03 21:22:43 +00003526/*
Hanno Beckera835da52019-05-16 12:39:07 +01003527 * Define ticket header determining Mbed TLS version
3528 * and structure of the ticket.
3529 */
3530
Hanno Becker94ef3b32019-05-16 12:50:45 +01003531/*
Hanno Becker50b59662019-05-28 14:30:45 +01003532 * Define bitflag determining compile-time settings influencing
3533 * structure of serialized SSL sessions.
Hanno Becker94ef3b32019-05-16 12:50:45 +01003534 */
3535
Hanno Becker50b59662019-05-28 14:30:45 +01003536#if defined(MBEDTLS_HAVE_TIME)
Hanno Becker3e088662019-05-29 11:10:18 +01003537#define SSL_SERIALIZED_SESSION_CONFIG_TIME 1
Hanno Becker50b59662019-05-28 14:30:45 +01003538#else
Hanno Becker3e088662019-05-29 11:10:18 +01003539#define SSL_SERIALIZED_SESSION_CONFIG_TIME 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003540#endif /* MBEDTLS_HAVE_TIME */
3541
3542#if defined(MBEDTLS_X509_CRT_PARSE_C)
Hanno Becker3e088662019-05-29 11:10:18 +01003543#define SSL_SERIALIZED_SESSION_CONFIG_CRT 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003544#else
Hanno Becker3e088662019-05-29 11:10:18 +01003545#define SSL_SERIALIZED_SESSION_CONFIG_CRT 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003546#endif /* MBEDTLS_X509_CRT_PARSE_C */
3547
3548#if defined(MBEDTLS_SSL_CLI_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Hanno Becker3e088662019-05-29 11:10:18 +01003549#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003550#else
Hanno Becker3e088662019-05-29 11:10:18 +01003551#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003552#endif /* MBEDTLS_SSL_CLI_C && MBEDTLS_SSL_SESSION_TICKETS */
3553
3554#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Hanno Becker3e088662019-05-29 11:10:18 +01003555#define SSL_SERIALIZED_SESSION_CONFIG_MFL 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003556#else
Hanno Becker3e088662019-05-29 11:10:18 +01003557#define SSL_SERIALIZED_SESSION_CONFIG_MFL 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003558#endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
3559
Hanno Becker94ef3b32019-05-16 12:50:45 +01003560#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Hanno Becker3e088662019-05-29 11:10:18 +01003561#define SSL_SERIALIZED_SESSION_CONFIG_ETM 1
Hanno Becker94ef3b32019-05-16 12:50:45 +01003562#else
Hanno Becker3e088662019-05-29 11:10:18 +01003563#define SSL_SERIALIZED_SESSION_CONFIG_ETM 0
Hanno Becker94ef3b32019-05-16 12:50:45 +01003564#endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
3565
Hanno Becker94ef3b32019-05-16 12:50:45 +01003566#if defined(MBEDTLS_SSL_SESSION_TICKETS)
3567#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 1
3568#else
3569#define SSL_SERIALIZED_SESSION_CONFIG_TICKET 0
3570#endif /* MBEDTLS_SSL_SESSION_TICKETS */
3571
Hanno Becker3e088662019-05-29 11:10:18 +01003572#define SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT 0
3573#define SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT 1
3574#define SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT 2
3575#define SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT 3
Hanno Becker37bdbe62021-08-01 05:38:58 +01003576#define SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT 4
3577#define SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT 5
Hanno Becker3e088662019-05-29 11:10:18 +01003578
Hanno Becker50b59662019-05-28 14:30:45 +01003579#define SSL_SERIALIZED_SESSION_CONFIG_BITFLAG \
Gilles Peskine449bd832023-01-11 14:50:10 +01003580 ((uint16_t) ( \
3581 (SSL_SERIALIZED_SESSION_CONFIG_TIME << SSL_SERIALIZED_SESSION_CONFIG_TIME_BIT) | \
3582 (SSL_SERIALIZED_SESSION_CONFIG_CRT << SSL_SERIALIZED_SESSION_CONFIG_CRT_BIT) | \
3583 (SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET << \
3584 SSL_SERIALIZED_SESSION_CONFIG_CLIENT_TICKET_BIT) | \
3585 (SSL_SERIALIZED_SESSION_CONFIG_MFL << SSL_SERIALIZED_SESSION_CONFIG_MFL_BIT) | \
3586 (SSL_SERIALIZED_SESSION_CONFIG_ETM << SSL_SERIALIZED_SESSION_CONFIG_ETM_BIT) | \
3587 (SSL_SERIALIZED_SESSION_CONFIG_TICKET << SSL_SERIALIZED_SESSION_CONFIG_TICKET_BIT)))
Hanno Becker94ef3b32019-05-16 12:50:45 +01003588
Hanno Beckerf8787072019-05-16 12:41:07 +01003589static unsigned char ssl_serialized_session_header[] = {
Hanno Becker94ef3b32019-05-16 12:50:45 +01003590 MBEDTLS_VERSION_MAJOR,
3591 MBEDTLS_VERSION_MINOR,
3592 MBEDTLS_VERSION_PATCH,
Gilles Peskine449bd832023-01-11 14:50:10 +01003593 MBEDTLS_BYTE_1(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
3594 MBEDTLS_BYTE_0(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
Hanno Beckerf8787072019-05-16 12:41:07 +01003595};
Hanno Beckera835da52019-05-16 12:39:07 +01003596
3597/*
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003598 * Serialize a session in the following format:
Manuel Pégourié-Gonnard35eb8022019-05-16 11:11:08 +02003599 * (in the presentation language of TLS, RFC 8446 section 3)
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003600 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003601 * struct {
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01003602 *
Hanno Beckerdce50972021-08-01 05:39:23 +01003603 * opaque mbedtls_version[3]; // library version: major, minor, patch
3604 * opaque session_format[2]; // library-version specific 16-bit field
3605 * // determining the format of the remaining
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003606 * // serialized data.
Hanno Beckerdc28b6c2019-05-29 11:08:00 +01003607 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003608 * Note: When updating the format, remember to keep
3609 * these version+format bytes.
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003610 *
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003611 * // In this version, `session_format` determines
3612 * // the setting of those compile-time
3613 * // configuration options which influence
3614 * // the structure of mbedtls_ssl_session.
3615 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003616 * uint8_t minor_ver; // Protocol minor version. Possible values:
Jerry Yu0c2a7382022-12-06 13:27:25 +08003617 * // - TLS 1.2 (0x0303)
3618 * // - TLS 1.3 (0x0304)
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003619 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003620 * select (serialized_session.tls_version) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003621 *
Glenn Straussda7851c2022-03-14 13:29:48 -04003622 * case MBEDTLS_SSL_VERSION_TLS1_2:
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003623 * serialized_session_tls12 data;
Jerry Yu0c2a7382022-12-06 13:27:25 +08003624 * case MBEDTLS_SSL_VERSION_TLS1_3:
Jerry Yuddda0502022-12-01 19:43:12 +08003625 * serialized_session_tls13 data;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003626 *
3627 * };
3628 *
3629 * } serialized_session;
3630 *
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003631 */
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003632
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003633MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003634static int ssl_session_save(const mbedtls_ssl_session *session,
3635 unsigned char omit_header,
3636 unsigned char *buf,
3637 size_t buf_len,
3638 size_t *olen)
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003639{
3640 unsigned char *p = buf;
3641 size_t used = 0;
Jerry Yu251a12e2022-07-13 15:15:48 +08003642 size_t remaining_len;
Jerry Yue36fdd62022-08-17 21:31:36 +08003643#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
3644 size_t out_len;
3645 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3646#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01003647 if (session == NULL) {
3648 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
3649 }
Jerry Yu438ddd82022-07-07 06:55:50 +00003650
Gilles Peskine449bd832023-01-11 14:50:10 +01003651 if (!omit_header) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003652 /*
3653 * Add Mbed TLS version identifier
3654 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003655 used += sizeof(ssl_serialized_session_header);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003656
Gilles Peskine449bd832023-01-11 14:50:10 +01003657 if (used <= buf_len) {
3658 memcpy(p, ssl_serialized_session_header,
3659 sizeof(ssl_serialized_session_header));
3660 p += sizeof(ssl_serialized_session_header);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003661 }
3662 }
3663
3664 /*
3665 * TLS version identifier
3666 */
3667 used += 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01003668 if (used <= buf_len) {
3669 *p++ = MBEDTLS_BYTE_0(session->tls_version);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003670 }
3671
3672 /* Forward to version-specific serialization routine. */
Jerry Yufca4d572022-07-21 10:37:48 +08003673 remaining_len = (buf_len >= used) ? buf_len - used : 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01003674 switch (session->tls_version) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003675#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01003676 case MBEDTLS_SSL_VERSION_TLS1_2:
3677 used += ssl_tls12_session_save(session, p, remaining_len);
3678 break;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003679#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3680
Jerry Yu251a12e2022-07-13 15:15:48 +08003681#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01003682 case MBEDTLS_SSL_VERSION_TLS1_3:
3683 ret = ssl_tls13_session_save(session, p, remaining_len, &out_len);
3684 if (ret != 0 && ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL) {
3685 return ret;
3686 }
3687 used += out_len;
3688 break;
Jerry Yu251a12e2022-07-13 15:15:48 +08003689#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3690
Gilles Peskine449bd832023-01-11 14:50:10 +01003691 default:
3692 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003693 }
3694
3695 *olen = used;
Gilles Peskine449bd832023-01-11 14:50:10 +01003696 if (used > buf_len) {
3697 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
3698 }
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003699
Gilles Peskine449bd832023-01-11 14:50:10 +01003700 return 0;
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003701}
3702
3703/*
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003704 * Public wrapper for ssl_session_save()
3705 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003706int mbedtls_ssl_session_save(const mbedtls_ssl_session *session,
3707 unsigned char *buf,
3708 size_t buf_len,
3709 size_t *olen)
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003710{
Gilles Peskine449bd832023-01-11 14:50:10 +01003711 return ssl_session_save(session, 0, buf, buf_len, olen);
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02003712}
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003713
Jerry Yuf1b23ca2022-02-18 11:48:47 +08003714/*
3715 * Deserialize session, see mbedtls_ssl_session_save() for format.
3716 *
3717 * This internal version is wrapped by a public function that cleans up in
3718 * case of error, and has an extra option omit_header.
3719 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003720MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003721static int ssl_session_load(mbedtls_ssl_session *session,
3722 unsigned char omit_header,
3723 const unsigned char *buf,
3724 size_t len)
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003725{
3726 const unsigned char *p = buf;
3727 const unsigned char * const end = buf + len;
Jerry Yu438ddd82022-07-07 06:55:50 +00003728 size_t remaining_len;
3729
3730
Gilles Peskine449bd832023-01-11 14:50:10 +01003731 if (session == NULL) {
3732 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
3733 }
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003734
Gilles Peskine449bd832023-01-11 14:50:10 +01003735 if (!omit_header) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003736 /*
3737 * Check Mbed TLS version identifier
3738 */
3739
Gilles Peskine449bd832023-01-11 14:50:10 +01003740 if ((size_t) (end - p) < sizeof(ssl_serialized_session_header)) {
3741 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003742 }
Gilles Peskine449bd832023-01-11 14:50:10 +01003743
3744 if (memcmp(p, ssl_serialized_session_header,
3745 sizeof(ssl_serialized_session_header)) != 0) {
3746 return MBEDTLS_ERR_SSL_VERSION_MISMATCH;
3747 }
3748 p += sizeof(ssl_serialized_session_header);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003749 }
3750
3751 /*
3752 * TLS version identifier
3753 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003754 if (1 > (size_t) (end - p)) {
3755 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3756 }
Glenn Straussda7851c2022-03-14 13:29:48 -04003757 session->tls_version = 0x0300 | *p++;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003758
3759 /* Dispatch according to TLS version. */
Gilles Peskine449bd832023-01-11 14:50:10 +01003760 remaining_len = (end - p);
3761 switch (session->tls_version) {
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003762#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01003763 case MBEDTLS_SSL_VERSION_TLS1_2:
3764 return ssl_tls12_session_load(session, p, remaining_len);
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003765#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
3766
Jerry Yu438ddd82022-07-07 06:55:50 +00003767#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01003768 case MBEDTLS_SSL_VERSION_TLS1_3:
3769 return ssl_tls13_session_load(session, p, remaining_len);
Jerry Yu438ddd82022-07-07 06:55:50 +00003770#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
3771
Gilles Peskine449bd832023-01-11 14:50:10 +01003772 default:
3773 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Beckerfadbdbb2021-07-23 06:25:48 +01003774 }
3775}
3776
Manuel Pégourié-Gonnarda3e7c652019-05-16 10:08:35 +02003777/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02003778 * Deserialize session: public wrapper for error cleaning
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003779 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003780int mbedtls_ssl_session_load(mbedtls_ssl_session *session,
3781 const unsigned char *buf,
3782 size_t len)
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003783{
Gilles Peskine449bd832023-01-11 14:50:10 +01003784 int ret = ssl_session_load(session, 0, buf, len);
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003785
Gilles Peskine449bd832023-01-11 14:50:10 +01003786 if (ret != 0) {
3787 mbedtls_ssl_session_free(session);
3788 }
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003789
Gilles Peskine449bd832023-01-11 14:50:10 +01003790 return ret;
Manuel Pégourié-Gonnarda3d831b2019-05-23 12:28:45 +02003791}
3792
3793/*
Paul Bakker1961b702013-01-25 14:49:24 +01003794 * Perform a single step of the SSL handshake
Paul Bakker5121ce52009-01-03 21:22:43 +00003795 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003796MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003797static int ssl_prepare_handshake_step(mbedtls_ssl_context *ssl)
Hanno Becker41934dd2021-08-07 19:13:43 +01003798{
3799 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
3800
Ronald Cron66dbf912022-02-02 15:33:46 +01003801 /*
3802 * We may have not been able to send to the peer all the handshake data
Ronald Cron3f20b772022-03-08 16:00:02 +01003803 * that were written into the output buffer by the previous handshake step,
3804 * if the write to the network callback returned with the
Ronald Cron66dbf912022-02-02 15:33:46 +01003805 * #MBEDTLS_ERR_SSL_WANT_WRITE error code.
3806 * We proceed to the next handshake step only when all data from the
3807 * previous one have been sent to the peer, thus we make sure that this is
3808 * the case here by calling `mbedtls_ssl_flush_output()`. The function may
3809 * return with the #MBEDTLS_ERR_SSL_WANT_WRITE error code in which case
3810 * we have to wait before to go ahead.
3811 * In the case of TLS 1.3, handshake step handlers do not send data to the
3812 * peer. Data are only sent here and through
3813 * `mbedtls_ssl_handle_pending_alert` in case an error that triggered an
Andrzej Kurek5c65c572022-04-13 14:28:52 -04003814 * alert occurred.
Ronald Cron66dbf912022-02-02 15:33:46 +01003815 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003816 if ((ret = mbedtls_ssl_flush_output(ssl)) != 0) {
3817 return ret;
3818 }
Hanno Becker41934dd2021-08-07 19:13:43 +01003819
3820#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003821 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3822 ssl->handshake->retransmit_state == MBEDTLS_SSL_RETRANS_SENDING) {
3823 if ((ret = mbedtls_ssl_flight_transmit(ssl)) != 0) {
3824 return ret;
3825 }
Hanno Becker41934dd2021-08-07 19:13:43 +01003826 }
3827#endif /* MBEDTLS_SSL_PROTO_DTLS */
3828
Gilles Peskine449bd832023-01-11 14:50:10 +01003829 return ret;
Hanno Becker41934dd2021-08-07 19:13:43 +01003830}
3831
Gilles Peskine449bd832023-01-11 14:50:10 +01003832int mbedtls_ssl_handshake_step(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00003833{
Hanno Becker41934dd2021-08-07 19:13:43 +01003834 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker5121ce52009-01-03 21:22:43 +00003835
Gilles Peskine449bd832023-01-11 14:50:10 +01003836 if (ssl == NULL ||
Hanno Becker41934dd2021-08-07 19:13:43 +01003837 ssl->conf == NULL ||
3838 ssl->handshake == NULL ||
Gilles Peskine449bd832023-01-11 14:50:10 +01003839 ssl->state == MBEDTLS_SSL_HANDSHAKE_OVER) {
3840 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Becker41934dd2021-08-07 19:13:43 +01003841 }
3842
Gilles Peskine449bd832023-01-11 14:50:10 +01003843 ret = ssl_prepare_handshake_step(ssl);
3844 if (ret != 0) {
3845 return ret;
3846 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003847
Gilles Peskine449bd832023-01-11 14:50:10 +01003848 ret = mbedtls_ssl_handle_pending_alert(ssl);
3849 if (ret != 0) {
Jerry Yue7047812021-09-13 19:26:39 +08003850 goto cleanup;
Gilles Peskine449bd832023-01-11 14:50:10 +01003851 }
Jerry Yue7047812021-09-13 19:26:39 +08003852
Tom Cosgrove2fdc7b32022-09-21 12:33:17 +01003853 /* If ssl->conf->endpoint is not one of MBEDTLS_SSL_IS_CLIENT or
3854 * MBEDTLS_SSL_IS_SERVER, this is the return code we give */
3855 ret = MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3856
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003857#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003858 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
3859 MBEDTLS_SSL_DEBUG_MSG(2, ("client state: %s",
3860 mbedtls_ssl_states_str(ssl->state)));
Jerry Yub9930e72021-08-06 17:11:51 +08003861
Gilles Peskine449bd832023-01-11 14:50:10 +01003862 switch (ssl->state) {
Ronald Cron9f0fba32022-02-10 16:45:15 +01003863 case MBEDTLS_SSL_HELLO_REQUEST:
3864 ssl->state = MBEDTLS_SSL_CLIENT_HELLO;
Tom Cosgrove87d9c6c2022-09-22 09:27:56 +01003865 ret = 0;
Ronald Cron9f0fba32022-02-10 16:45:15 +01003866 break;
Jerry Yub9930e72021-08-06 17:11:51 +08003867
Ronald Cron9f0fba32022-02-10 16:45:15 +01003868 case MBEDTLS_SSL_CLIENT_HELLO:
Gilles Peskine449bd832023-01-11 14:50:10 +01003869 ret = mbedtls_ssl_write_client_hello(ssl);
Ronald Cron9f0fba32022-02-10 16:45:15 +01003870 break;
3871
3872 default:
3873#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01003874 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
3875 ret = mbedtls_ssl_tls13_handshake_client_step(ssl);
3876 } else {
3877 ret = mbedtls_ssl_handshake_client_step(ssl);
3878 }
Ronald Cron9f0fba32022-02-10 16:45:15 +01003879#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01003880 ret = mbedtls_ssl_handshake_client_step(ssl);
Ronald Cron9f0fba32022-02-10 16:45:15 +01003881#else
Gilles Peskine449bd832023-01-11 14:50:10 +01003882 ret = mbedtls_ssl_tls13_handshake_client_step(ssl);
Ronald Cron9f0fba32022-02-10 16:45:15 +01003883#endif
3884 }
Jerry Yub9930e72021-08-06 17:11:51 +08003885 }
Ronald Cron6291b232023-03-08 15:51:25 +01003886#endif /* MBEDTLS_SSL_CLI_C */
3887
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003888#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01003889 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Ronald Cron6291b232023-03-08 15:51:25 +01003890#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
3891 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
Gilles Peskine449bd832023-01-11 14:50:10 +01003892 ret = mbedtls_ssl_tls13_handshake_server_step(ssl);
Ronald Cron6291b232023-03-08 15:51:25 +01003893 } else {
Gilles Peskine449bd832023-01-11 14:50:10 +01003894 ret = mbedtls_ssl_handshake_server_step(ssl);
3895 }
Ronald Cron6291b232023-03-08 15:51:25 +01003896#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
3897 ret = mbedtls_ssl_handshake_server_step(ssl);
3898#else
3899 ret = mbedtls_ssl_tls13_handshake_server_step(ssl);
Paul Bakker5121ce52009-01-03 21:22:43 +00003900#endif
Ronald Cron6291b232023-03-08 15:51:25 +01003901 }
3902#endif /* MBEDTLS_SSL_SRV_C */
Paul Bakker5121ce52009-01-03 21:22:43 +00003903
Gilles Peskine449bd832023-01-11 14:50:10 +01003904 if (ret != 0) {
Jerry Yubbd5a3f2021-09-18 20:50:22 +08003905 /* handshake_step return error. And it is same
3906 * with alert_reason.
3907 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003908 if (ssl->send_alert) {
3909 ret = mbedtls_ssl_handle_pending_alert(ssl);
Jerry Yue7047812021-09-13 19:26:39 +08003910 goto cleanup;
3911 }
3912 }
3913
3914cleanup:
Gilles Peskine449bd832023-01-11 14:50:10 +01003915 return ret;
Paul Bakker1961b702013-01-25 14:49:24 +01003916}
3917
3918/*
3919 * Perform the SSL handshake
3920 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003921int mbedtls_ssl_handshake(mbedtls_ssl_context *ssl)
Paul Bakker1961b702013-01-25 14:49:24 +01003922{
3923 int ret = 0;
3924
Hanno Beckera817ea42020-10-20 15:20:23 +01003925 /* Sanity checks */
3926
Gilles Peskine449bd832023-01-11 14:50:10 +01003927 if (ssl == NULL || ssl->conf == NULL) {
3928 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
3929 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02003930
Hanno Beckera817ea42020-10-20 15:20:23 +01003931#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01003932 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
3933 (ssl->f_set_timer == NULL || ssl->f_get_timer == NULL)) {
3934 MBEDTLS_SSL_DEBUG_MSG(1, ("You must use "
3935 "mbedtls_ssl_set_timer_cb() for DTLS"));
3936 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Hanno Beckera817ea42020-10-20 15:20:23 +01003937 }
3938#endif /* MBEDTLS_SSL_PROTO_DTLS */
3939
Gilles Peskine449bd832023-01-11 14:50:10 +01003940 MBEDTLS_SSL_DEBUG_MSG(2, ("=> handshake"));
Paul Bakker1961b702013-01-25 14:49:24 +01003941
Hanno Beckera817ea42020-10-20 15:20:23 +01003942 /* Main handshake loop */
Gilles Peskine449bd832023-01-11 14:50:10 +01003943 while (ssl->state != MBEDTLS_SSL_HANDSHAKE_OVER) {
3944 ret = mbedtls_ssl_handshake_step(ssl);
Paul Bakker1961b702013-01-25 14:49:24 +01003945
Gilles Peskine449bd832023-01-11 14:50:10 +01003946 if (ret != 0) {
Paul Bakker1961b702013-01-25 14:49:24 +01003947 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01003948 }
Paul Bakker1961b702013-01-25 14:49:24 +01003949 }
3950
Gilles Peskine449bd832023-01-11 14:50:10 +01003951 MBEDTLS_SSL_DEBUG_MSG(2, ("<= handshake"));
Paul Bakker5121ce52009-01-03 21:22:43 +00003952
Gilles Peskine449bd832023-01-11 14:50:10 +01003953 return ret;
Paul Bakker5121ce52009-01-03 21:22:43 +00003954}
3955
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003956#if defined(MBEDTLS_SSL_RENEGOTIATION)
3957#if defined(MBEDTLS_SSL_SRV_C)
Paul Bakker5121ce52009-01-03 21:22:43 +00003958/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003959 * Write HelloRequest to request renegotiation on server
Paul Bakker48916f92012-09-16 19:57:18 +00003960 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02003961MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01003962static int ssl_write_hello_request(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003963{
Janos Follath865b3eb2019-12-16 11:46:15 +00003964 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003965
Gilles Peskine449bd832023-01-11 14:50:10 +01003966 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write hello request"));
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003967
3968 ssl->out_msglen = 4;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003969 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
3970 ssl->out_msg[0] = MBEDTLS_SSL_HS_HELLO_REQUEST;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003971
Gilles Peskine449bd832023-01-11 14:50:10 +01003972 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
3973 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
3974 return ret;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003975 }
3976
Gilles Peskine449bd832023-01-11 14:50:10 +01003977 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write hello request"));
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003978
Gilles Peskine449bd832023-01-11 14:50:10 +01003979 return 0;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003980}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003981#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003982
3983/*
3984 * Actually renegotiate current connection, triggered by either:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003985 * - any side: calling mbedtls_ssl_renegotiate(),
3986 * - client: receiving a HelloRequest during mbedtls_ssl_read(),
3987 * - server: receiving any handshake message on server during mbedtls_ssl_read() after
Manuel Pégourié-Gonnard55e4ff22014-08-19 11:16:35 +02003988 * the initial handshake is completed.
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01003989 * If the handshake doesn't complete due to waiting for I/O, it will continue
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02003990 * during the next calls to mbedtls_ssl_renegotiate() or mbedtls_ssl_read() respectively.
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01003991 */
Gilles Peskine449bd832023-01-11 14:50:10 +01003992int mbedtls_ssl_start_renegotiation(mbedtls_ssl_context *ssl)
Paul Bakker48916f92012-09-16 19:57:18 +00003993{
Janos Follath865b3eb2019-12-16 11:46:15 +00003994 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Paul Bakker48916f92012-09-16 19:57:18 +00003995
Gilles Peskine449bd832023-01-11 14:50:10 +01003996 MBEDTLS_SSL_DEBUG_MSG(2, ("=> renegotiate"));
Paul Bakker48916f92012-09-16 19:57:18 +00003997
Gilles Peskine449bd832023-01-11 14:50:10 +01003998 if ((ret = ssl_handshake_init(ssl)) != 0) {
3999 return ret;
4000 }
Paul Bakker48916f92012-09-16 19:57:18 +00004001
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02004002 /* RFC 6347 4.2.2: "[...] the HelloRequest will have message_seq = 0 and
4003 * the ServerHello will have message_seq = 1" */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004004#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004005 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
4006 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_PENDING) {
4007 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004008 ssl->handshake->out_msg_seq = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004009 } else {
Manuel Pégourié-Gonnard1aa586e2014-09-03 12:54:04 +02004010 ssl->handshake->in_msg_seq = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004011 }
Manuel Pégourié-Gonnard0557bd52014-08-19 19:18:39 +02004012 }
4013#endif
4014
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004015 ssl->state = MBEDTLS_SSL_HELLO_REQUEST;
4016 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS;
Paul Bakker48916f92012-09-16 19:57:18 +00004017
Gilles Peskine449bd832023-01-11 14:50:10 +01004018 if ((ret = mbedtls_ssl_handshake(ssl)) != 0) {
4019 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_handshake", ret);
4020 return ret;
Paul Bakker48916f92012-09-16 19:57:18 +00004021 }
4022
Gilles Peskine449bd832023-01-11 14:50:10 +01004023 MBEDTLS_SSL_DEBUG_MSG(2, ("<= renegotiate"));
Paul Bakker48916f92012-09-16 19:57:18 +00004024
Gilles Peskine449bd832023-01-11 14:50:10 +01004025 return 0;
Paul Bakker48916f92012-09-16 19:57:18 +00004026}
4027
4028/*
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004029 * Renegotiate current connection on client,
4030 * or request renegotiation on server
4031 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004032int mbedtls_ssl_renegotiate(mbedtls_ssl_context *ssl)
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004033{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004034 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004035
Gilles Peskine449bd832023-01-11 14:50:10 +01004036 if (ssl == NULL || ssl->conf == NULL) {
4037 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4038 }
Manuel Pégourié-Gonnardf81ee2e2015-09-01 17:43:40 +02004039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004040#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004041 /* On server, just send the request */
Gilles Peskine449bd832023-01-11 14:50:10 +01004042 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
4043 if (mbedtls_ssl_is_handshake_over(ssl) == 0) {
4044 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4045 }
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004046
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004047 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_PENDING;
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02004048
4049 /* Did we already try/start sending HelloRequest? */
Gilles Peskine449bd832023-01-11 14:50:10 +01004050 if (ssl->out_left != 0) {
4051 return mbedtls_ssl_flush_output(ssl);
4052 }
Manuel Pégourié-Gonnardf07f4212014-08-15 19:04:47 +02004053
Gilles Peskine449bd832023-01-11 14:50:10 +01004054 return ssl_write_hello_request(ssl);
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004055 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004056#endif /* MBEDTLS_SSL_SRV_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004057
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004058#if defined(MBEDTLS_SSL_CLI_C)
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004059 /*
4060 * On client, either start the renegotiation process or,
4061 * if already in progress, continue the handshake
4062 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004063 if (ssl->renego_status != MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
4064 if (mbedtls_ssl_is_handshake_over(ssl) == 0) {
4065 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004066 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004067
4068 if ((ret = mbedtls_ssl_start_renegotiation(ssl)) != 0) {
4069 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_start_renegotiation", ret);
4070 return ret;
4071 }
4072 } else {
4073 if ((ret = mbedtls_ssl_handshake(ssl)) != 0) {
4074 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_handshake", ret);
4075 return ret;
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004076 }
4077 }
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004078#endif /* MBEDTLS_SSL_CLI_C */
Manuel Pégourié-Gonnard9c1e1892013-10-30 16:41:21 +01004079
Gilles Peskine449bd832023-01-11 14:50:10 +01004080 return ret;
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004081}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004082#endif /* MBEDTLS_SSL_RENEGOTIATION */
Manuel Pégourié-Gonnard214eed32013-10-30 13:06:54 +01004083
Gilles Peskine449bd832023-01-11 14:50:10 +01004084void mbedtls_ssl_handshake_free(mbedtls_ssl_context *ssl)
Paul Bakker48916f92012-09-16 19:57:18 +00004085{
Gilles Peskine9b562d52018-04-25 20:32:43 +02004086 mbedtls_ssl_handshake_params *handshake = ssl->handshake;
4087
Gilles Peskine449bd832023-01-11 14:50:10 +01004088 if (handshake == NULL) {
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004089 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01004090 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004091
Brett Warrene0edc842021-08-17 09:53:13 +01004092#if defined(MBEDTLS_ECP_C)
4093#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01004094 if (ssl->handshake->group_list_heap_allocated) {
4095 mbedtls_free((void *) handshake->group_list);
4096 }
Brett Warrene0edc842021-08-17 09:53:13 +01004097 handshake->group_list = NULL;
4098#endif /* MBEDTLS_DEPRECATED_REMOVED */
4099#endif /* MBEDTLS_ECP_C */
4100
Ronald Crone68ab4f2022-10-05 12:46:29 +02004101#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yuf017ee42022-01-12 15:49:48 +08004102#if !defined(MBEDTLS_DEPRECATED_REMOVED)
Gilles Peskine449bd832023-01-11 14:50:10 +01004103 if (ssl->handshake->sig_algs_heap_allocated) {
4104 mbedtls_free((void *) handshake->sig_algs);
4105 }
Jerry Yuf017ee42022-01-12 15:49:48 +08004106 handshake->sig_algs = NULL;
4107#endif /* MBEDTLS_DEPRECATED_REMOVED */
Xiaofei Baic234ecf2022-02-08 09:59:23 +00004108#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01004109 if (ssl->handshake->certificate_request_context) {
4110 mbedtls_free((void *) handshake->certificate_request_context);
Xiaofei Baic234ecf2022-02-08 09:59:23 +00004111 }
4112#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Ronald Crone68ab4f2022-10-05 12:46:29 +02004113#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yuf017ee42022-01-12 15:49:48 +08004114
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02004115#if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01004116 if (ssl->conf->f_async_cancel != NULL && handshake->async_in_progress != 0) {
4117 ssl->conf->f_async_cancel(ssl);
Gilles Peskinedf13d5c2018-04-25 20:39:48 +02004118 handshake->async_in_progress = 0;
4119 }
4120#endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
4121
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01004122#if defined(MBEDTLS_MD_CAN_SHA256)
Andrzej Kurekeb342242019-01-29 09:14:33 -05004123#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01004124 psa_hash_abort(&handshake->fin_sha256_psa);
Andrzej Kurekeb342242019-01-29 09:14:33 -05004125#else
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01004126 mbedtls_md_free(&handshake->fin_sha256);
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02004127#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05004128#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01004129#if defined(MBEDTLS_MD_CAN_SHA384)
Andrzej Kurekeb342242019-01-29 09:14:33 -05004130#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01004131 psa_hash_abort(&handshake->fin_sha384_psa);
Andrzej Kurekeb342242019-01-29 09:14:33 -05004132#else
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01004133 mbedtls_md_free(&handshake->fin_sha384);
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02004134#endif
Andrzej Kurekeb342242019-01-29 09:14:33 -05004135#endif
Manuel Pégourié-Gonnardb9d64e52015-07-06 14:18:56 +02004136
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004137#if defined(MBEDTLS_DHM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004138 mbedtls_dhm_free(&handshake->dhm_ctx);
Paul Bakker48916f92012-09-16 19:57:18 +00004139#endif
Neil Armstrongf3f46412022-04-12 14:43:39 +02004140#if !defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_ECDH_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004141 mbedtls_ecdh_free(&handshake->ecdh_ctx);
Paul Bakker61d113b2013-07-04 11:51:43 +02004142#endif
Valerio Setti02c25b52022-11-15 14:08:42 +01004143
Manuel Pégourié-Gonnardeef142d2015-09-16 10:05:04 +02004144#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Neil Armstrongca7d5062022-05-31 14:43:23 +02004145#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01004146 psa_pake_abort(&handshake->psa_pake_ctx);
Valerio Settieb3f7882022-12-08 18:42:58 +01004147 /*
4148 * Opaque keys are not stored in the handshake's data and it's the user
4149 * responsibility to destroy them. Clear ones, instead, are created by
4150 * the TLS library and should be destroyed at the same level
4151 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004152 if (!mbedtls_svc_key_id_is_null(handshake->psa_pake_password)) {
4153 psa_destroy_key(handshake->psa_pake_password);
Valerio Settieb3f7882022-12-08 18:42:58 +01004154 }
Neil Armstrongca7d5062022-05-31 14:43:23 +02004155 handshake->psa_pake_password = MBEDTLS_SVC_KEY_ID_INIT;
4156#else
Gilles Peskine449bd832023-01-11 14:50:10 +01004157 mbedtls_ecjpake_free(&handshake->ecjpake_ctx);
Neil Armstrongca7d5062022-05-31 14:43:23 +02004158#endif /* MBEDTLS_USE_PSA_CRYPTO */
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02004159#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004160 mbedtls_free(handshake->ecjpake_cache);
Manuel Pégourié-Gonnard77c06462015-09-17 13:59:49 +02004161 handshake->ecjpake_cache = NULL;
4162 handshake->ecjpake_cache_len = 0;
4163#endif
Manuel Pégourié-Gonnard76cfd3f2015-09-15 12:10:54 +02004164#endif
Paul Bakker61d113b2013-07-04 11:51:43 +02004165
Janos Follath4ae5c292016-02-10 11:27:43 +00004166#if defined(MBEDTLS_ECDH_C) || defined(MBEDTLS_ECDSA_C) || \
4167 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Paul Bakker9af723c2014-05-01 13:03:14 +02004168 /* explicit void pointer cast for buggy MS compiler */
Gilles Peskine449bd832023-01-11 14:50:10 +01004169 mbedtls_free((void *) handshake->curves_tls_id);
Manuel Pégourié-Gonnardd09453c2013-09-23 19:11:32 +02004170#endif
4171
Ronald Cron73fe8df2022-10-05 14:31:43 +02004172#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02004173#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01004174 if (!mbedtls_svc_key_id_is_null(ssl->handshake->psk_opaque)) {
Neil Armstrong501c9322022-05-03 09:35:09 +02004175 /* The maintenance of the external PSK key slot is the
4176 * user's responsibility. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004177 if (ssl->handshake->psk_opaque_is_internal) {
4178 psa_destroy_key(ssl->handshake->psk_opaque);
Neil Armstrong501c9322022-05-03 09:35:09 +02004179 ssl->handshake->psk_opaque_is_internal = 0;
4180 }
4181 ssl->handshake->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
4182 }
Neil Armstronge952a302022-05-03 10:22:14 +02004183#else
Gilles Peskine449bd832023-01-11 14:50:10 +01004184 if (handshake->psk != NULL) {
4185 mbedtls_platform_zeroize(handshake->psk, handshake->psk_len);
4186 mbedtls_free(handshake->psk);
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004187 }
Neil Armstronge952a302022-05-03 10:22:14 +02004188#endif /* MBEDTLS_USE_PSA_CRYPTO */
Ronald Cron73fe8df2022-10-05 14:31:43 +02004189#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Manuel Pégourié-Gonnard4b682962015-05-07 15:59:54 +01004190
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004191#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
4192 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Manuel Pégourié-Gonnard83724542013-09-24 22:30:56 +02004193 /*
4194 * Free only the linked list wrapper, not the keys themselves
4195 * since the belong to the SNI callback
4196 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004197 ssl_key_cert_free(handshake->sni_key_cert);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004198#endif /* MBEDTLS_X509_CRT_PARSE_C && MBEDTLS_SSL_SERVER_NAME_INDICATION */
Manuel Pégourié-Gonnard705fcca2013-09-23 20:04:20 +02004199
Gilles Peskineeccd8882020-03-10 12:19:08 +01004200#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01004201 mbedtls_x509_crt_restart_free(&handshake->ecrs_ctx);
4202 if (handshake->ecrs_peer_cert != NULL) {
4203 mbedtls_x509_crt_free(handshake->ecrs_peer_cert);
4204 mbedtls_free(handshake->ecrs_peer_cert);
Hanno Becker3dad3112019-02-05 17:19:52 +00004205 }
Manuel Pégourié-Gonnard862cde52017-05-17 11:56:15 +02004206#endif
4207
Hanno Becker75173122019-02-06 16:18:31 +00004208#if defined(MBEDTLS_X509_CRT_PARSE_C) && \
4209 !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01004210 mbedtls_pk_free(&handshake->peer_pubkey);
Hanno Becker75173122019-02-06 16:18:31 +00004211#endif /* MBEDTLS_X509_CRT_PARSE_C && !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
4212
XiaokangQian9b93c0d2022-02-09 06:02:25 +00004213#if defined(MBEDTLS_SSL_CLI_C) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01004214 (defined(MBEDTLS_SSL_PROTO_DTLS) || defined(MBEDTLS_SSL_PROTO_TLS1_3))
4215 mbedtls_free(handshake->cookie);
XiaokangQian9b93c0d2022-02-09 06:02:25 +00004216#endif /* MBEDTLS_SSL_CLI_C &&
4217 ( MBEDTLS_SSL_PROTO_DTLS || MBEDTLS_SSL_PROTO_TLS1_3 ) */
XiaokangQian8499b6c2022-01-27 09:00:11 +00004218
4219#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004220 mbedtls_ssl_flight_free(handshake->flight);
4221 mbedtls_ssl_buffering_free(ssl);
XiaokangQian8499b6c2022-01-27 09:00:11 +00004222#endif /* MBEDTLS_SSL_PROTO_DTLS */
Manuel Pégourié-Gonnard74848812014-07-11 02:43:49 +02004223
Valerio Setti080a22b2023-03-20 15:22:47 +01004224#if defined(PSA_WANT_ALG_ECDH) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01004225 (defined(MBEDTLS_USE_PSA_CRYPTO) || defined(MBEDTLS_SSL_PROTO_TLS1_3))
4226 if (handshake->ecdh_psa_privkey_is_external == 0) {
4227 psa_destroy_key(handshake->ecdh_psa_privkey);
4228 }
Valerio Setti080a22b2023-03-20 15:22:47 +01004229#endif /* PSA_WANT_ALG_ECDH && (MBEDTLS_USE_PSA_CRYPTO || MBEDTLS_SSL_PROTO_TLS1_3) */
Hanno Becker4a63ed42019-01-08 11:39:35 +00004230
Ronald Cron6f135e12021-12-08 16:57:54 +01004231#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01004232 mbedtls_ssl_transform_free(handshake->transform_handshake);
4233 mbedtls_free(handshake->transform_handshake);
Jerry Yu3d9b5902022-11-04 14:07:25 +08004234#if defined(MBEDTLS_SSL_EARLY_DATA)
Gilles Peskine449bd832023-01-11 14:50:10 +01004235 mbedtls_ssl_transform_free(handshake->transform_earlydata);
4236 mbedtls_free(handshake->transform_earlydata);
Jerry Yu3d9b5902022-11-04 14:07:25 +08004237#endif
Ronald Cron6f135e12021-12-08 16:57:54 +01004238#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yuba9c7272021-10-30 11:54:10 +08004239
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004240
4241#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4242 /* If the buffers are too big - reallocate. Because of the way Mbed TLS
4243 * processes datagrams and the fact that a datagram is allowed to have
4244 * several records in it, it is possible that the I/O buffers are not
4245 * empty at this stage */
Gilles Peskine449bd832023-01-11 14:50:10 +01004246 handle_buffer_resizing(ssl, 1, mbedtls_ssl_get_input_buflen(ssl),
4247 mbedtls_ssl_get_output_buflen(ssl));
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004248#endif
Hanno Becker3aa186f2021-08-10 09:24:19 +01004249
Jerry Yuba9c7272021-10-30 11:54:10 +08004250 /* mbedtls_platform_zeroize MUST be last one in this function */
Gilles Peskine449bd832023-01-11 14:50:10 +01004251 mbedtls_platform_zeroize(handshake,
4252 sizeof(mbedtls_ssl_handshake_params));
Paul Bakker48916f92012-09-16 19:57:18 +00004253}
4254
Gilles Peskine449bd832023-01-11 14:50:10 +01004255void mbedtls_ssl_session_free(mbedtls_ssl_session *session)
Paul Bakker48916f92012-09-16 19:57:18 +00004256{
Gilles Peskine449bd832023-01-11 14:50:10 +01004257 if (session == NULL) {
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004258 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01004259 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004260
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004261#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004262 ssl_clear_peer_cert(session);
Paul Bakkered27a042013-04-18 22:46:23 +02004263#endif
Paul Bakker0a597072012-09-25 21:55:46 +00004264
Manuel Pégourié-Gonnardb596abf2015-05-20 10:45:29 +02004265#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Xiaokang Qianbc663a02022-10-09 11:14:39 +00004266#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
4267 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01004268 mbedtls_free(session->hostname);
Xiaokang Qian281fd1b2022-09-20 11:35:41 +00004269#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01004270 mbedtls_free(session->ticket);
Paul Bakkera503a632013-08-14 13:48:06 +02004271#endif
Manuel Pégourié-Gonnard75d44012013-08-02 14:44:04 +02004272
Gilles Peskine449bd832023-01-11 14:50:10 +01004273 mbedtls_platform_zeroize(session, sizeof(mbedtls_ssl_session));
Paul Bakker48916f92012-09-16 19:57:18 +00004274}
4275
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02004276#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004277
4278#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4279#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 1u
4280#else
4281#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID 0u
4282#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4283
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004284#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT 1u
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004285
4286#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4287#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 1u
4288#else
4289#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY 0u
4290#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4291
4292#if defined(MBEDTLS_SSL_ALPN)
4293#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 1u
4294#else
4295#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN 0u
4296#endif /* MBEDTLS_SSL_ALPN */
4297
4298#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT 0
4299#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT 1
4300#define SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT 2
4301#define SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT 3
4302
4303#define SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG \
Gilles Peskine449bd832023-01-11 14:50:10 +01004304 ((uint32_t) ( \
4305 (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID << \
4306 SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_CONNECTION_ID_BIT) | \
4307 (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT << \
4308 SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_BADMAC_LIMIT_BIT) | \
4309 (SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY << \
4310 SSL_SERIALIZED_CONTEXT_CONFIG_DTLS_ANTI_REPLAY_BIT) | \
4311 (SSL_SERIALIZED_CONTEXT_CONFIG_ALPN << SSL_SERIALIZED_CONTEXT_CONFIG_ALPN_BIT) | \
4312 0u))
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004313
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004314static unsigned char ssl_serialized_context_header[] = {
4315 MBEDTLS_VERSION_MAJOR,
4316 MBEDTLS_VERSION_MINOR,
4317 MBEDTLS_VERSION_PATCH,
Gilles Peskine449bd832023-01-11 14:50:10 +01004318 MBEDTLS_BYTE_1(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
4319 MBEDTLS_BYTE_0(SSL_SERIALIZED_SESSION_CONFIG_BITFLAG),
4320 MBEDTLS_BYTE_2(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG),
4321 MBEDTLS_BYTE_1(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG),
4322 MBEDTLS_BYTE_0(SSL_SERIALIZED_CONTEXT_CONFIG_BITFLAG),
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004323};
4324
Paul Bakker5121ce52009-01-03 21:22:43 +00004325/*
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004326 * Serialize a full SSL context
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02004327 *
4328 * The format of the serialized data is:
4329 * (in the presentation language of TLS, RFC 8446 section 3)
4330 *
4331 * // header
4332 * opaque mbedtls_version[3]; // major, minor, patch
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004333 * opaque context_format[5]; // version-specific field determining
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02004334 * // the format of the remaining
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004335 * // serialized data.
Manuel Pégourié-Gonnard4e9370b2019-07-23 16:31:16 +02004336 * Note: When updating the format, remember to keep these
4337 * version+format bytes. (We may make their size part of the API.)
Manuel Pégourié-Gonnard00400c22019-07-10 14:58:45 +02004338 *
4339 * // session sub-structure
4340 * opaque session<1..2^32-1>; // see mbedtls_ssl_session_save()
4341 * // transform sub-structure
4342 * uint8 random[64]; // ServerHello.random+ClientHello.random
4343 * uint8 in_cid<0..2^8-1> // Connection ID: expected incoming value
4344 * uint8 out_cid<0..2^8-1> // Connection ID: outgoing value to use
4345 * // fields from ssl_context
4346 * uint32 badmac_seen; // DTLS: number of records with failing MAC
4347 * uint64 in_window_top; // DTLS: last validated record seq_num
4348 * uint64 in_window; // DTLS: bitmask for replay protection
4349 * uint8 disable_datagram_packing; // DTLS: only one record per datagram
4350 * uint64 cur_out_ctr; // Record layer: outgoing sequence number
4351 * uint16 mtu; // DTLS: path mtu (max outgoing fragment size)
4352 * uint8 alpn_chosen<0..2^8-1> // ALPN: negotiated application protocol
4353 *
4354 * Note that many fields of the ssl_context or sub-structures are not
4355 * serialized, as they fall in one of the following categories:
4356 *
4357 * 1. forced value (eg in_left must be 0)
4358 * 2. pointer to dynamically-allocated memory (eg session, transform)
4359 * 3. value can be re-derived from other data (eg session keys from MS)
4360 * 4. value was temporary (eg content of input buffer)
4361 * 5. value will be provided by the user again (eg I/O callbacks and context)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004362 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004363int mbedtls_ssl_context_save(mbedtls_ssl_context *ssl,
4364 unsigned char *buf,
4365 size_t buf_len,
4366 size_t *olen)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004367{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004368 unsigned char *p = buf;
4369 size_t used = 0;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004370 size_t session_len;
4371 int ret = 0;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004372
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02004373 /*
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004374 * Enforce usage restrictions, see "return BAD_INPUT_DATA" in
4375 * this function's documentation.
4376 *
4377 * These are due to assumptions/limitations in the implementation. Some of
4378 * them are likely to stay (no handshake in progress) some might go away
4379 * (only DTLS) but are currently used to simplify the implementation.
Manuel Pégourié-Gonnard1aaf6692019-07-10 14:14:05 +02004380 */
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004381 /* The initial handshake must be over */
Gilles Peskine449bd832023-01-11 14:50:10 +01004382 if (mbedtls_ssl_is_handshake_over(ssl) == 0) {
4383 MBEDTLS_SSL_DEBUG_MSG(1, ("Initial handshake isn't over"));
4384 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004385 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004386 if (ssl->handshake != NULL) {
4387 MBEDTLS_SSL_DEBUG_MSG(1, ("Handshake isn't completed"));
4388 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004389 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004390 /* Double-check that sub-structures are indeed ready */
Gilles Peskine449bd832023-01-11 14:50:10 +01004391 if (ssl->transform == NULL || ssl->session == NULL) {
4392 MBEDTLS_SSL_DEBUG_MSG(1, ("Serialised structures aren't ready"));
4393 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004394 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004395 /* There must be no pending incoming or outgoing data */
Gilles Peskine449bd832023-01-11 14:50:10 +01004396 if (mbedtls_ssl_check_pending(ssl) != 0) {
4397 MBEDTLS_SSL_DEBUG_MSG(1, ("There is pending incoming data"));
4398 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004399 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004400 if (ssl->out_left != 0) {
4401 MBEDTLS_SSL_DEBUG_MSG(1, ("There is pending outgoing data"));
4402 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004403 }
Dave Rodgman556e8a32022-12-06 16:31:25 +00004404 /* Protocol must be DTLS, not TLS */
Gilles Peskine449bd832023-01-11 14:50:10 +01004405 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
4406 MBEDTLS_SSL_DEBUG_MSG(1, ("Only DTLS is supported"));
4407 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004408 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004409 /* Version must be 1.2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004410 if (ssl->tls_version != MBEDTLS_SSL_VERSION_TLS1_2) {
4411 MBEDTLS_SSL_DEBUG_MSG(1, ("Only version 1.2 supported"));
4412 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004413 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004414 /* We must be using an AEAD ciphersuite */
Gilles Peskine449bd832023-01-11 14:50:10 +01004415 if (mbedtls_ssl_transform_uses_aead(ssl->transform) != 1) {
4416 MBEDTLS_SSL_DEBUG_MSG(1, ("Only AEAD ciphersuites supported"));
4417 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004418 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004419 /* Renegotiation must not be enabled */
4420#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01004421 if (ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED) {
4422 MBEDTLS_SSL_DEBUG_MSG(1, ("Renegotiation must not be enabled"));
4423 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jarno Lamsa8c51b7c2019-08-21 13:45:05 +03004424 }
Manuel Pégourié-Gonnarde4588692019-07-29 12:28:52 +02004425#endif
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004426
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004427 /*
4428 * Version and format identifier
4429 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004430 used += sizeof(ssl_serialized_context_header);
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004431
Gilles Peskine449bd832023-01-11 14:50:10 +01004432 if (used <= buf_len) {
4433 memcpy(p, ssl_serialized_context_header,
4434 sizeof(ssl_serialized_context_header));
4435 p += sizeof(ssl_serialized_context_header);
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004436 }
4437
4438 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004439 * Session (length + data)
4440 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004441 ret = ssl_session_save(ssl->session, 1, NULL, 0, &session_len);
4442 if (ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL) {
4443 return ret;
4444 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004445
4446 used += 4 + session_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004447 if (used <= buf_len) {
4448 MBEDTLS_PUT_UINT32_BE(session_len, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004449 p += 4;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004450
Gilles Peskine449bd832023-01-11 14:50:10 +01004451 ret = ssl_session_save(ssl->session, 1,
4452 p, session_len, &session_len);
4453 if (ret != 0) {
4454 return ret;
4455 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004456
4457 p += session_len;
4458 }
4459
4460 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004461 * Transform
4462 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004463 used += sizeof(ssl->transform->randbytes);
4464 if (used <= buf_len) {
4465 memcpy(p, ssl->transform->randbytes,
4466 sizeof(ssl->transform->randbytes));
4467 p += sizeof(ssl->transform->randbytes);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004468 }
4469
4470#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4471 used += 2 + ssl->transform->in_cid_len + ssl->transform->out_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004472 if (used <= buf_len) {
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004473 *p++ = ssl->transform->in_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004474 memcpy(p, ssl->transform->in_cid, ssl->transform->in_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004475 p += ssl->transform->in_cid_len;
4476
4477 *p++ = ssl->transform->out_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004478 memcpy(p, ssl->transform->out_cid, ssl->transform->out_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004479 p += ssl->transform->out_cid_len;
4480 }
4481#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4482
4483 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004484 * Saved fields from top-level ssl_context structure
4485 */
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004486 used += 4;
Gilles Peskine449bd832023-01-11 14:50:10 +01004487 if (used <= buf_len) {
4488 MBEDTLS_PUT_UINT32_BE(ssl->badmac_seen, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004489 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004490 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004491
4492#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
4493 used += 16;
Gilles Peskine449bd832023-01-11 14:50:10 +01004494 if (used <= buf_len) {
4495 MBEDTLS_PUT_UINT64_BE(ssl->in_window_top, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004496 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004497
Gilles Peskine449bd832023-01-11 14:50:10 +01004498 MBEDTLS_PUT_UINT64_BE(ssl->in_window, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004499 p += 8;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004500 }
4501#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4502
4503#if defined(MBEDTLS_SSL_PROTO_DTLS)
4504 used += 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01004505 if (used <= buf_len) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004506 *p++ = ssl->disable_datagram_packing;
4507 }
4508#endif /* MBEDTLS_SSL_PROTO_DTLS */
4509
Jerry Yuae0b2e22021-10-08 15:21:19 +08004510 used += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Gilles Peskine449bd832023-01-11 14:50:10 +01004511 if (used <= buf_len) {
4512 memcpy(p, ssl->cur_out_ctr, MBEDTLS_SSL_SEQUENCE_NUMBER_LEN);
Jerry Yuae0b2e22021-10-08 15:21:19 +08004513 p += MBEDTLS_SSL_SEQUENCE_NUMBER_LEN;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004514 }
4515
4516#if defined(MBEDTLS_SSL_PROTO_DTLS)
4517 used += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01004518 if (used <= buf_len) {
4519 MBEDTLS_PUT_UINT16_BE(ssl->mtu, p, 0);
Joe Subbiani1f6c3ae2021-08-20 11:44:44 +01004520 p += 2;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004521 }
4522#endif /* MBEDTLS_SSL_PROTO_DTLS */
4523
4524#if defined(MBEDTLS_SSL_ALPN)
4525 {
4526 const uint8_t alpn_len = ssl->alpn_chosen
Gilles Peskine449bd832023-01-11 14:50:10 +01004527 ? (uint8_t) strlen(ssl->alpn_chosen)
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004528 : 0;
4529
4530 used += 1 + alpn_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01004531 if (used <= buf_len) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004532 *p++ = alpn_len;
4533
Gilles Peskine449bd832023-01-11 14:50:10 +01004534 if (ssl->alpn_chosen != NULL) {
4535 memcpy(p, ssl->alpn_chosen, alpn_len);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004536 p += alpn_len;
4537 }
4538 }
4539 }
4540#endif /* MBEDTLS_SSL_ALPN */
4541
4542 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004543 * Done
4544 */
4545 *olen = used;
4546
Gilles Peskine449bd832023-01-11 14:50:10 +01004547 if (used > buf_len) {
4548 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
4549 }
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004550
Gilles Peskine449bd832023-01-11 14:50:10 +01004551 MBEDTLS_SSL_DEBUG_BUF(4, "saved context", buf, used);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004552
Gilles Peskine449bd832023-01-11 14:50:10 +01004553 return mbedtls_ssl_session_reset_int(ssl, 0);
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004554}
4555
4556/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02004557 * Deserialize context, see mbedtls_ssl_context_save() for format.
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004558 *
4559 * This internal version is wrapped by a public function that cleans up in
4560 * case of error.
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004561 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02004562MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01004563static int ssl_context_load(mbedtls_ssl_context *ssl,
4564 const unsigned char *buf,
4565 size_t len)
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004566{
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004567 const unsigned char *p = buf;
4568 const unsigned char * const end = buf + len;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004569 size_t session_len;
Janos Follath865b3eb2019-12-16 11:46:15 +00004570 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004571#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Andrzej Kurek894edde2022-09-29 06:31:14 -04004572 tls_prf_fn prf_func = NULL;
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004573#endif
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004574
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004575 /*
4576 * The context should have been freshly setup or reset.
4577 * Give the user an error in case of obvious misuse.
Manuel Pégourié-Gonnard4ca930f2019-07-26 16:31:53 +02004578 * (Checking session is useful because it won't be NULL if we're
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004579 * renegotiating, or if the user mistakenly loaded a session first.)
4580 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004581 if (ssl->state != MBEDTLS_SSL_HELLO_REQUEST ||
4582 ssl->session != NULL) {
4583 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004584 }
4585
4586 /*
4587 * We can't check that the config matches the initial one, but we can at
4588 * least check it matches the requirements for serializing.
4589 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004590 if (ssl->conf->transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM ||
Glenn Strauss2dfcea22022-03-14 17:26:42 -04004591 ssl->conf->max_tls_version < MBEDTLS_SSL_VERSION_TLS1_2 ||
4592 ssl->conf->min_tls_version > MBEDTLS_SSL_VERSION_TLS1_2 ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004593#if defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnard9a96fd72019-07-23 17:11:24 +02004594 ssl->conf->disable_renegotiation != MBEDTLS_SSL_RENEGOTIATION_DISABLED ||
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004595#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01004596 0) {
4597 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard0ff76402019-07-11 09:56:30 +02004598 }
4599
Gilles Peskine449bd832023-01-11 14:50:10 +01004600 MBEDTLS_SSL_DEBUG_BUF(4, "context to load", buf, len);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004601
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004602 /*
4603 * Check version identifier
4604 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004605 if ((size_t) (end - p) < sizeof(ssl_serialized_context_header)) {
4606 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004607 }
Gilles Peskine449bd832023-01-11 14:50:10 +01004608
4609 if (memcmp(p, ssl_serialized_context_header,
4610 sizeof(ssl_serialized_context_header)) != 0) {
4611 return MBEDTLS_ERR_SSL_VERSION_MISMATCH;
4612 }
4613 p += sizeof(ssl_serialized_context_header);
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004614
4615 /*
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004616 * Session
4617 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004618 if ((size_t) (end - p) < 4) {
4619 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4620 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004621
Gilles Peskine449bd832023-01-11 14:50:10 +01004622 session_len = ((size_t) p[0] << 24) |
4623 ((size_t) p[1] << 16) |
4624 ((size_t) p[2] << 8) |
4625 ((size_t) p[3]);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004626 p += 4;
4627
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004628 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00004629 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004630 ssl->session = ssl->session_negotiate;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004631 ssl->session_in = ssl->session;
4632 ssl->session_out = ssl->session;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004633 ssl->session_negotiate = NULL;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004634
Gilles Peskine449bd832023-01-11 14:50:10 +01004635 if ((size_t) (end - p) < session_len) {
4636 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4637 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004638
Gilles Peskine449bd832023-01-11 14:50:10 +01004639 ret = ssl_session_load(ssl->session, 1, p, session_len);
4640 if (ret != 0) {
4641 mbedtls_ssl_session_free(ssl->session);
4642 return ret;
Manuel Pégourié-Gonnard45ac1f02019-07-23 16:52:45 +02004643 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004644
4645 p += session_len;
4646
4647 /*
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004648 * Transform
4649 */
4650
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004651 /* This has been allocated by ssl_handshake_init(), called by
Hanno Becker43aefe22020-02-05 10:44:56 +00004652 * by either mbedtls_ssl_session_reset_int() or mbedtls_ssl_setup(). */
Jerry Yu2e199812022-12-01 18:57:19 +08004653#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004654 ssl->transform = ssl->transform_negotiate;
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004655 ssl->transform_in = ssl->transform;
4656 ssl->transform_out = ssl->transform;
Manuel Pégourié-Gonnard142ba732019-07-23 14:43:30 +02004657 ssl->transform_negotiate = NULL;
Jerry Yu2e199812022-12-01 18:57:19 +08004658#endif
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004659
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004660#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01004661 prf_func = ssl_tls12prf_from_cs(ssl->session->ciphersuite);
4662 if (prf_func == NULL) {
4663 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4664 }
Andrzej Kurek894edde2022-09-29 06:31:14 -04004665
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004666 /* Read random bytes and populate structure */
Gilles Peskine449bd832023-01-11 14:50:10 +01004667 if ((size_t) (end - p) < sizeof(ssl->transform->randbytes)) {
4668 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4669 }
Andrzej Kurek2d59dbc2022-10-13 08:34:38 -04004670
Gilles Peskine449bd832023-01-11 14:50:10 +01004671 ret = ssl_tls12_populate_transform(ssl->transform,
4672 ssl->session->ciphersuite,
4673 ssl->session->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02004674#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01004675 ssl->session->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02004676#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01004677 prf_func,
4678 p, /* currently pointing to randbytes */
4679 MBEDTLS_SSL_VERSION_TLS1_2, /* (D)TLS 1.2 is forced */
4680 ssl->conf->endpoint,
4681 ssl);
4682 if (ret != 0) {
4683 return ret;
4684 }
Jerry Yu840fbb22022-02-17 14:59:29 +08004685#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004686 p += sizeof(ssl->transform->randbytes);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004687
4688#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
4689 /* Read connection IDs and store them */
Gilles Peskine449bd832023-01-11 14:50:10 +01004690 if ((size_t) (end - p) < 1) {
4691 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4692 }
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004693
4694 ssl->transform->in_cid_len = *p++;
4695
Gilles Peskine449bd832023-01-11 14:50:10 +01004696 if ((size_t) (end - p) < ssl->transform->in_cid_len + 1u) {
4697 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4698 }
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004699
Gilles Peskine449bd832023-01-11 14:50:10 +01004700 memcpy(ssl->transform->in_cid, p, ssl->transform->in_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004701 p += ssl->transform->in_cid_len;
4702
4703 ssl->transform->out_cid_len = *p++;
4704
Gilles Peskine449bd832023-01-11 14:50:10 +01004705 if ((size_t) (end - p) < ssl->transform->out_cid_len) {
4706 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4707 }
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004708
Gilles Peskine449bd832023-01-11 14:50:10 +01004709 memcpy(ssl->transform->out_cid, p, ssl->transform->out_cid_len);
Manuel Pégourié-Gonnardc2a7b892019-07-15 09:04:11 +02004710 p += ssl->transform->out_cid_len;
4711#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
4712
4713 /*
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004714 * Saved fields from top-level ssl_context structure
4715 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004716 if ((size_t) (end - p) < 4) {
4717 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4718 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004719
Gilles Peskine449bd832023-01-11 14:50:10 +01004720 ssl->badmac_seen = ((uint32_t) p[0] << 24) |
4721 ((uint32_t) p[1] << 16) |
4722 ((uint32_t) p[2] << 8) |
4723 ((uint32_t) p[3]);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004724 p += 4;
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004725
4726#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
Gilles Peskine449bd832023-01-11 14:50:10 +01004727 if ((size_t) (end - p) < 16) {
4728 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4729 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004730
Gilles Peskine449bd832023-01-11 14:50:10 +01004731 ssl->in_window_top = ((uint64_t) p[0] << 56) |
4732 ((uint64_t) p[1] << 48) |
4733 ((uint64_t) p[2] << 40) |
4734 ((uint64_t) p[3] << 32) |
4735 ((uint64_t) p[4] << 24) |
4736 ((uint64_t) p[5] << 16) |
4737 ((uint64_t) p[6] << 8) |
4738 ((uint64_t) p[7]);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004739 p += 8;
4740
Gilles Peskine449bd832023-01-11 14:50:10 +01004741 ssl->in_window = ((uint64_t) p[0] << 56) |
4742 ((uint64_t) p[1] << 48) |
4743 ((uint64_t) p[2] << 40) |
4744 ((uint64_t) p[3] << 32) |
4745 ((uint64_t) p[4] << 24) |
4746 ((uint64_t) p[5] << 16) |
4747 ((uint64_t) p[6] << 8) |
4748 ((uint64_t) p[7]);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004749 p += 8;
4750#endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
4751
4752#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004753 if ((size_t) (end - p) < 1) {
4754 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4755 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004756
4757 ssl->disable_datagram_packing = *p++;
4758#endif /* MBEDTLS_SSL_PROTO_DTLS */
4759
Gilles Peskine449bd832023-01-11 14:50:10 +01004760 if ((size_t) (end - p) < sizeof(ssl->cur_out_ctr)) {
4761 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4762 }
4763 memcpy(ssl->cur_out_ctr, p, sizeof(ssl->cur_out_ctr));
4764 p += sizeof(ssl->cur_out_ctr);
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004765
4766#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01004767 if ((size_t) (end - p) < 2) {
4768 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4769 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004770
Gilles Peskine449bd832023-01-11 14:50:10 +01004771 ssl->mtu = (p[0] << 8) | p[1];
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004772 p += 2;
4773#endif /* MBEDTLS_SSL_PROTO_DTLS */
4774
4775#if defined(MBEDTLS_SSL_ALPN)
4776 {
4777 uint8_t alpn_len;
4778 const char **cur;
4779
Gilles Peskine449bd832023-01-11 14:50:10 +01004780 if ((size_t) (end - p) < 1) {
4781 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4782 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004783
4784 alpn_len = *p++;
4785
Gilles Peskine449bd832023-01-11 14:50:10 +01004786 if (alpn_len != 0 && ssl->conf->alpn_list != NULL) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004787 /* alpn_chosen should point to an item in the configured list */
Gilles Peskine449bd832023-01-11 14:50:10 +01004788 for (cur = ssl->conf->alpn_list; *cur != NULL; cur++) {
4789 if (strlen(*cur) == alpn_len &&
4790 memcmp(p, cur, alpn_len) == 0) {
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004791 ssl->alpn_chosen = *cur;
4792 break;
4793 }
4794 }
4795 }
4796
4797 /* can only happen on conf mismatch */
Gilles Peskine449bd832023-01-11 14:50:10 +01004798 if (alpn_len != 0 && ssl->alpn_chosen == NULL) {
4799 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4800 }
Manuel Pégourié-Gonnardc86c5df2019-07-15 11:23:03 +02004801
4802 p += alpn_len;
4803 }
4804#endif /* MBEDTLS_SSL_ALPN */
4805
4806 /*
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004807 * Forced fields from top-level ssl_context structure
4808 *
4809 * Most of them already set to the correct value by mbedtls_ssl_init() and
4810 * mbedtls_ssl_reset(), so we only need to set the remaining ones.
4811 */
4812 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
Glenn Strauss60bfe602022-03-14 19:04:24 -04004813 ssl->tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004814
Hanno Becker361b10d2019-08-30 10:42:49 +01004815 /* Adjust pointers for header fields of outgoing records to
4816 * the given transform, accounting for explicit IV and CID. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004817 mbedtls_ssl_update_out_pointers(ssl, ssl->transform);
Hanno Becker361b10d2019-08-30 10:42:49 +01004818
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004819#if defined(MBEDTLS_SSL_PROTO_DTLS)
4820 ssl->in_epoch = 1;
4821#endif
4822
4823 /* mbedtls_ssl_reset() leaves the handshake sub-structure allocated,
4824 * which we don't want - otherwise we'd end up freeing the wrong transform
Hanno Beckerce5f5fd2020-02-05 10:47:44 +00004825 * by calling mbedtls_ssl_handshake_wrapup_free_hs_transform()
4826 * inappropriately. */
Gilles Peskine449bd832023-01-11 14:50:10 +01004827 if (ssl->handshake != NULL) {
4828 mbedtls_ssl_handshake_free(ssl);
4829 mbedtls_free(ssl->handshake);
Manuel Pégourié-Gonnard0eb3eac2019-07-15 11:53:51 +02004830 ssl->handshake = NULL;
4831 }
4832
4833 /*
Manuel Pégourié-Gonnard4c90e852019-07-11 10:58:10 +02004834 * Done - should have consumed entire buffer
4835 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004836 if (p != end) {
4837 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
4838 }
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004839
Gilles Peskine449bd832023-01-11 14:50:10 +01004840 return 0;
Manuel Pégourié-Gonnardac87e282019-05-28 13:02:16 +02004841}
4842
4843/*
Manuel Pégourié-Gonnardb9dfc9f2019-07-12 10:50:19 +02004844 * Deserialize context: public wrapper for error cleaning
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004845 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004846int mbedtls_ssl_context_load(mbedtls_ssl_context *context,
4847 const unsigned char *buf,
4848 size_t len)
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004849{
Gilles Peskine449bd832023-01-11 14:50:10 +01004850 int ret = ssl_context_load(context, buf, len);
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004851
Gilles Peskine449bd832023-01-11 14:50:10 +01004852 if (ret != 0) {
4853 mbedtls_ssl_free(context);
4854 }
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004855
Gilles Peskine449bd832023-01-11 14:50:10 +01004856 return ret;
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004857}
Manuel Pégourié-Gonnard5c0e3772019-07-23 16:13:17 +02004858#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Manuel Pégourié-Gonnard4b7e6b92019-07-11 12:50:53 +02004859
4860/*
Paul Bakker5121ce52009-01-03 21:22:43 +00004861 * Free an SSL context
4862 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004863void mbedtls_ssl_free(mbedtls_ssl_context *ssl)
Paul Bakker5121ce52009-01-03 21:22:43 +00004864{
Gilles Peskine449bd832023-01-11 14:50:10 +01004865 if (ssl == NULL) {
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004866 return;
Gilles Peskine449bd832023-01-11 14:50:10 +01004867 }
Paul Bakkeraccaffe2014-06-26 13:37:14 +02004868
Gilles Peskine449bd832023-01-11 14:50:10 +01004869 MBEDTLS_SSL_DEBUG_MSG(2, ("=> free"));
Paul Bakker5121ce52009-01-03 21:22:43 +00004870
Gilles Peskine449bd832023-01-11 14:50:10 +01004871 if (ssl->out_buf != NULL) {
sander-visserb8aa2072020-05-06 22:05:13 +02004872#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4873 size_t out_buf_len = ssl->out_buf_len;
4874#else
4875 size_t out_buf_len = MBEDTLS_SSL_OUT_BUFFER_LEN;
4876#endif
4877
Gilles Peskine449bd832023-01-11 14:50:10 +01004878 mbedtls_platform_zeroize(ssl->out_buf, out_buf_len);
4879 mbedtls_free(ssl->out_buf);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004880 ssl->out_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004881 }
4882
Gilles Peskine449bd832023-01-11 14:50:10 +01004883 if (ssl->in_buf != NULL) {
sander-visserb8aa2072020-05-06 22:05:13 +02004884#if defined(MBEDTLS_SSL_VARIABLE_BUFFER_LENGTH)
4885 size_t in_buf_len = ssl->in_buf_len;
4886#else
4887 size_t in_buf_len = MBEDTLS_SSL_IN_BUFFER_LEN;
4888#endif
4889
Gilles Peskine449bd832023-01-11 14:50:10 +01004890 mbedtls_platform_zeroize(ssl->in_buf, in_buf_len);
4891 mbedtls_free(ssl->in_buf);
Andrzej Kurek0afa2a12020-03-03 10:39:58 -05004892 ssl->in_buf = NULL;
Paul Bakker5121ce52009-01-03 21:22:43 +00004893 }
4894
Gilles Peskine449bd832023-01-11 14:50:10 +01004895 if (ssl->transform) {
4896 mbedtls_ssl_transform_free(ssl->transform);
4897 mbedtls_free(ssl->transform);
Paul Bakker48916f92012-09-16 19:57:18 +00004898 }
4899
Gilles Peskine449bd832023-01-11 14:50:10 +01004900 if (ssl->handshake) {
4901 mbedtls_ssl_handshake_free(ssl);
4902 mbedtls_free(ssl->handshake);
Jerry Yu2e199812022-12-01 18:57:19 +08004903
4904#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01004905 mbedtls_ssl_transform_free(ssl->transform_negotiate);
4906 mbedtls_free(ssl->transform_negotiate);
Jerry Yu2e199812022-12-01 18:57:19 +08004907#endif
4908
Gilles Peskine449bd832023-01-11 14:50:10 +01004909 mbedtls_ssl_session_free(ssl->session_negotiate);
4910 mbedtls_free(ssl->session_negotiate);
Paul Bakker48916f92012-09-16 19:57:18 +00004911 }
4912
Ronald Cron6f135e12021-12-08 16:57:54 +01004913#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01004914 mbedtls_ssl_transform_free(ssl->transform_application);
4915 mbedtls_free(ssl->transform_application);
Ronald Cron6f135e12021-12-08 16:57:54 +01004916#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker3aa186f2021-08-10 09:24:19 +01004917
Gilles Peskine449bd832023-01-11 14:50:10 +01004918 if (ssl->session) {
4919 mbedtls_ssl_session_free(ssl->session);
4920 mbedtls_free(ssl->session);
Paul Bakkerc0463502013-02-14 11:19:38 +01004921 }
4922
Manuel Pégourié-Gonnard55fab2d2015-05-11 16:15:19 +02004923#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004924 if (ssl->hostname != NULL) {
4925 mbedtls_platform_zeroize(ssl->hostname, strlen(ssl->hostname));
4926 mbedtls_free(ssl->hostname);
Paul Bakker5121ce52009-01-03 21:22:43 +00004927 }
Paul Bakker0be444a2013-08-27 21:55:01 +02004928#endif
Paul Bakker5121ce52009-01-03 21:22:43 +00004929
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02004930#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01004931 mbedtls_free(ssl->cli_id);
Manuel Pégourié-Gonnard43c02182014-07-22 17:32:01 +02004932#endif
4933
Gilles Peskine449bd832023-01-11 14:50:10 +01004934 MBEDTLS_SSL_DEBUG_MSG(2, ("<= free"));
Paul Bakker2da561c2009-02-05 18:00:28 +00004935
Paul Bakker86f04f42013-02-14 11:20:09 +01004936 /* Actually clear after last debug message */
Gilles Peskine449bd832023-01-11 14:50:10 +01004937 mbedtls_platform_zeroize(ssl, sizeof(mbedtls_ssl_context));
Paul Bakker5121ce52009-01-03 21:22:43 +00004938}
4939
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004940/*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08004941 * Initialize mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004942 */
Gilles Peskine449bd832023-01-11 14:50:10 +01004943void mbedtls_ssl_config_init(mbedtls_ssl_config *conf)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004944{
Gilles Peskine449bd832023-01-11 14:50:10 +01004945 memset(conf, 0, sizeof(mbedtls_ssl_config));
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02004946}
4947
Gilles Peskineae270bf2021-06-02 00:05:29 +02004948/* The selection should be the same as mbedtls_x509_crt_profile_default in
4949 * x509_crt.c, plus Montgomery curves for ECDHE. Here, the order matters:
Gilles Peskineb1940a72021-06-02 15:18:12 +02004950 * curves with a lower resource usage come first.
Gilles Peskineae270bf2021-06-02 00:05:29 +02004951 * See the documentation of mbedtls_ssl_conf_curves() for what we promise
Gilles Peskineb1940a72021-06-02 15:18:12 +02004952 * about this list.
4953 */
Brett Warrene0edc842021-08-17 09:53:13 +01004954static uint16_t ssl_preset_default_groups[] = {
Gilles Peskineae270bf2021-06-02 00:05:29 +02004955#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004956 MBEDTLS_SSL_IANA_TLS_GROUP_X25519,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004957#endif
4958#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004959 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004960#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02004961#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004962 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004963#endif
4964#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004965 MBEDTLS_SSL_IANA_TLS_GROUP_X448,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004966#endif
4967#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004968 MBEDTLS_SSL_IANA_TLS_GROUP_SECP521R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004969#endif
Gilles Peskineae270bf2021-06-02 00:05:29 +02004970#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004971 MBEDTLS_SSL_IANA_TLS_GROUP_BP256R1,
Gilles Peskineae270bf2021-06-02 00:05:29 +02004972#endif
Gilles Peskineb1940a72021-06-02 15:18:12 +02004973#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004974 MBEDTLS_SSL_IANA_TLS_GROUP_BP384R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004975#endif
4976#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01004977 MBEDTLS_SSL_IANA_TLS_GROUP_BP512R1,
Gilles Peskineb1940a72021-06-02 15:18:12 +02004978#endif
Brett Warrene0edc842021-08-17 09:53:13 +01004979 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Gilles Peskineae270bf2021-06-02 00:05:29 +02004980};
Gilles Peskineae270bf2021-06-02 00:05:29 +02004981
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02004982static int ssl_preset_suiteb_ciphersuites[] = {
4983 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
4984 MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
4985 0
4986};
4987
Ronald Crone68ab4f2022-10-05 12:46:29 +02004988#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004989
Jerry Yu909df7b2022-01-22 11:56:27 +08004990/* NOTICE:
Jerry Yu0b994b82022-01-25 17:22:12 +08004991 * For ssl_preset_*_sig_algs and ssl_tls12_preset_*_sig_algs, the following
Jerry Yu370e1462022-01-25 10:36:53 +08004992 * rules SHOULD be upheld.
4993 * - No duplicate entries.
4994 * - But if there is a good reason, do not change the order of the algorithms.
Jerry Yu09a99fc2022-07-28 14:22:17 +08004995 * - ssl_tls12_preset* is for TLS 1.2 use only.
Jerry Yu370e1462022-01-25 10:36:53 +08004996 * - ssl_preset_* is for TLS 1.3 only or hybrid TLS 1.3/1.2 handshakes.
Jerry Yu1a8b4812022-01-20 17:56:50 +08004997 */
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01004998static uint16_t ssl_preset_default_sig_algs[] = {
Jerry Yu1a8b4812022-01-20 17:56:50 +08004999
Valerio Setti75fba322023-02-23 17:35:09 +01005000#if defined(MBEDTLS_PK_CAN_ECDSA_SOME) && \
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005001 defined(MBEDTLS_MD_CAN_SHA256) && \
Jerry Yued5e9f42022-01-26 11:21:34 +08005002 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
5003 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005004#endif /* MBEDTLS_PK_CAN_ECDSA_SOME && MBEDTLS_MD_CAN_SHA256 &&
Jerry Yued5e9f42022-01-26 11:21:34 +08005005 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08005006
Valerio Setti75fba322023-02-23 17:35:09 +01005007#if defined(MBEDTLS_PK_CAN_ECDSA_SOME) && \
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005008 defined(MBEDTLS_MD_CAN_SHA384) && \
Jerry Yu909df7b2022-01-22 11:56:27 +08005009 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
5010 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005011#endif /* MBEDTLS_PK_CAN_ECDSA_SOME && MBEDTLS_MD_CAN_SHA384&&
Jerry Yu909df7b2022-01-22 11:56:27 +08005012 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
5013
Valerio Setti75fba322023-02-23 17:35:09 +01005014#if defined(MBEDTLS_PK_CAN_ECDSA_SOME) && \
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005015 defined(MBEDTLS_MD_CAN_SHA512) && \
Jerry Yued5e9f42022-01-26 11:21:34 +08005016 defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED)
5017 MBEDTLS_TLS1_3_SIG_ECDSA_SECP521R1_SHA512,
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005018#endif /* MBEDTLS_PK_CAN_ECDSA_SOME && MBEDTLS_MD_CAN_SHA384&&
Jerry Yued5e9f42022-01-26 11:21:34 +08005019 MBEDTLS_ECP_DP_SECP521R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08005020
Gilles Peskine449bd832023-01-11 14:50:10 +01005021#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && \
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005022 defined(MBEDTLS_MD_CAN_SHA512)
Jerry Yu9bb3ee42022-06-23 10:16:33 +08005023 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
Gilles Peskine449bd832023-01-11 14:50:10 +01005024#endif \
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005025 /* MBEDTLS_X509_RSASSA_PSS_SUPPORT && MBEDTLS_MD_CAN_SHA512 */
Jerry Yu9bb3ee42022-06-23 10:16:33 +08005026
Gilles Peskine449bd832023-01-11 14:50:10 +01005027#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && \
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005028 defined(MBEDTLS_MD_CAN_SHA384)
Jerry Yu9bb3ee42022-06-23 10:16:33 +08005029 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
Gilles Peskine449bd832023-01-11 14:50:10 +01005030#endif \
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005031 /* MBEDTLS_X509_RSASSA_PSS_SUPPORT && MBEDTLS_MD_CAN_SHA384 */
Jerry Yu9bb3ee42022-06-23 10:16:33 +08005032
Gilles Peskine449bd832023-01-11 14:50:10 +01005033#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && \
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005034 defined(MBEDTLS_MD_CAN_SHA256)
Jerry Yu9bb3ee42022-06-23 10:16:33 +08005035 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Gilles Peskine449bd832023-01-11 14:50:10 +01005036#endif \
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005037 /* MBEDTLS_X509_RSASSA_PSS_SUPPORT && MBEDTLS_MD_CAN_SHA256 */
Jerry Yu9bb3ee42022-06-23 10:16:33 +08005038
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005039#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_MD_CAN_SHA512)
Jerry Yu693a47a2022-06-23 14:02:28 +08005040 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA512,
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005041#endif /* MBEDTLS_RSA_C && MBEDTLS_MD_CAN_SHA512 */
Jerry Yu693a47a2022-06-23 14:02:28 +08005042
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005043#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_MD_CAN_SHA384)
Jerry Yu693a47a2022-06-23 14:02:28 +08005044 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA384,
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005045#endif /* MBEDTLS_RSA_C && MBEDTLS_MD_CAN_SHA384 */
Jerry Yu693a47a2022-06-23 14:02:28 +08005046
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005047#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_MD_CAN_SHA256)
Jerry Yu693a47a2022-06-23 14:02:28 +08005048 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005049#endif /* MBEDTLS_RSA_C && MBEDTLS_MD_CAN_SHA256 */
Jerry Yu693a47a2022-06-23 14:02:28 +08005050
Gabor Mezei15b95a62022-05-09 16:37:58 +02005051 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08005052};
5053
5054/* NOTICE: see above */
5055#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5056static uint16_t ssl_tls12_preset_default_sig_algs[] = {
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005057#if defined(MBEDTLS_MD_CAN_SHA512)
Valerio Setti75fba322023-02-23 17:35:09 +01005058#if defined(MBEDTLS_PK_CAN_ECDSA_SOME)
Gilles Peskine449bd832023-01-11 14:50:10 +01005059 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA512),
Jerry Yu713013f2022-01-17 18:16:35 +08005060#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08005061#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
5062 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA512,
5063#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02005064#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005065 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA512),
Gabor Mezeic1051b62022-05-10 13:13:58 +02005066#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005067#endif /* MBEDTLS_MD_CAN_SHA512*/
5068#if defined(MBEDTLS_MD_CAN_SHA384)
Valerio Setti75fba322023-02-23 17:35:09 +01005069#if defined(MBEDTLS_PK_CAN_ECDSA_SOME)
Gilles Peskine449bd832023-01-11 14:50:10 +01005070 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08005071#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08005072#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
5073 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA384,
5074#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02005075#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005076 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384),
Gabor Mezeic1051b62022-05-10 13:13:58 +02005077#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005078#endif /* MBEDTLS_MD_CAN_SHA384*/
5079#if defined(MBEDTLS_MD_CAN_SHA256)
Valerio Setti75fba322023-02-23 17:35:09 +01005080#if defined(MBEDTLS_PK_CAN_ECDSA_SOME)
Gilles Peskine449bd832023-01-11 14:50:10 +01005081 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256),
Jerry Yu11f0a9c2022-01-12 18:43:08 +08005082#endif
Jerry Yu09a99fc2022-07-28 14:22:17 +08005083#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT)
5084 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
5085#endif /* MBEDTLS_X509_RSASSA_PSS_SUPPORT */
Gabor Mezeic1051b62022-05-10 13:13:58 +02005086#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005087 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256),
Gabor Mezeic1051b62022-05-10 13:13:58 +02005088#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005089#endif /* MBEDTLS_MD_CAN_SHA256*/
Gabor Mezei15b95a62022-05-09 16:37:58 +02005090 MBEDTLS_TLS_SIG_NONE
Jerry Yu909df7b2022-01-22 11:56:27 +08005091};
5092#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5093/* NOTICE: see above */
5094static uint16_t ssl_preset_suiteb_sig_algs[] = {
5095
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005096#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_MD_CAN_SHA256) && \
Jerry Yu909df7b2022-01-22 11:56:27 +08005097 defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
5098 MBEDTLS_TLS1_3_SIG_ECDSA_SECP256R1_SHA256,
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005099#endif /* MBEDTLS_ECDSA_C && MBEDTLS_MD_CAN_SHA256&&
Jerry Yu909df7b2022-01-22 11:56:27 +08005100 MBEDTLS_ECP_DP_SECP256R1_ENABLED */
5101
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005102#if defined(MBEDTLS_ECDSA_C) && defined(MBEDTLS_MD_CAN_SHA384) && \
Jerry Yu53037892022-01-25 11:02:06 +08005103 defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
5104 MBEDTLS_TLS1_3_SIG_ECDSA_SECP384R1_SHA384,
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005105#endif /* MBEDTLS_ECDSA_C && MBEDTLS_MD_CAN_SHA384&&
Jerry Yu53037892022-01-25 11:02:06 +08005106 MBEDTLS_ECP_DP_SECP384R1_ENABLED */
Jerry Yu909df7b2022-01-22 11:56:27 +08005107
Gilles Peskine449bd832023-01-11 14:50:10 +01005108#if defined(MBEDTLS_X509_RSASSA_PSS_SUPPORT) && \
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005109 defined(MBEDTLS_MD_CAN_SHA256)
Jerry Yu909df7b2022-01-22 11:56:27 +08005110 MBEDTLS_TLS1_3_SIG_RSA_PSS_RSAE_SHA256,
Gilles Peskine449bd832023-01-11 14:50:10 +01005111#endif \
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005112 /* MBEDTLS_X509_RSASSA_PSS_SUPPORT && MBEDTLS_MD_CAN_SHA256*/
Jerry Yu1a8b4812022-01-20 17:56:50 +08005113
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005114#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_MD_CAN_SHA256)
Jerry Yu53037892022-01-25 11:02:06 +08005115 MBEDTLS_TLS1_3_SIG_RSA_PKCS1_SHA256,
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005116#endif /* MBEDTLS_RSA_C && MBEDTLS_MD_CAN_SHA256*/
Jerry Yu53037892022-01-25 11:02:06 +08005117
Gabor Mezei15b95a62022-05-09 16:37:58 +02005118 MBEDTLS_TLS_SIG_NONE
Jerry Yu713013f2022-01-17 18:16:35 +08005119};
Jerry Yu6106fdc2022-01-12 16:36:14 +08005120
Jerry Yu909df7b2022-01-22 11:56:27 +08005121/* NOTICE: see above */
5122#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
5123static uint16_t ssl_tls12_preset_suiteb_sig_algs[] = {
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005124#if defined(MBEDTLS_MD_CAN_SHA256)
Gabor Mezeic1051b62022-05-10 13:13:58 +02005125#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005126 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA256),
Jerry Yu713013f2022-01-17 18:16:35 +08005127#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02005128#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005129 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA256),
Gabor Mezeic1051b62022-05-10 13:13:58 +02005130#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005131#endif /* MBEDTLS_MD_CAN_SHA256*/
5132#if defined(MBEDTLS_MD_CAN_SHA384)
Gabor Mezeic1051b62022-05-10 13:13:58 +02005133#if defined(MBEDTLS_ECDSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005134 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_ECDSA, MBEDTLS_SSL_HASH_SHA384),
Jerry Yu18c833e2022-01-25 10:55:47 +08005135#endif
Gabor Mezeic1051b62022-05-10 13:13:58 +02005136#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005137 MBEDTLS_SSL_TLS12_SIG_AND_HASH_ALG(MBEDTLS_SSL_SIG_RSA, MBEDTLS_SSL_HASH_SHA384),
Gabor Mezeic1051b62022-05-10 13:13:58 +02005138#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005139#endif /* MBEDTLS_MD_CAN_SHA256*/
Gabor Mezei15b95a62022-05-09 16:37:58 +02005140 MBEDTLS_TLS_SIG_NONE
Hanno Becker9c6aa7b2021-08-10 13:50:43 +01005141};
Jerry Yu909df7b2022-01-22 11:56:27 +08005142#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
5143
Ronald Crone68ab4f2022-10-05 12:46:29 +02005144#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005145
Brett Warrene0edc842021-08-17 09:53:13 +01005146static uint16_t ssl_preset_suiteb_groups[] = {
Jaeden Amerod4311042019-06-03 08:27:16 +01005147#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01005148 MBEDTLS_SSL_IANA_TLS_GROUP_SECP256R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01005149#endif
5150#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED)
Brett Warrene0edc842021-08-17 09:53:13 +01005151 MBEDTLS_SSL_IANA_TLS_GROUP_SECP384R1,
Jaeden Amerod4311042019-06-03 08:27:16 +01005152#endif
Brett Warrene0edc842021-08-17 09:53:13 +01005153 MBEDTLS_SSL_IANA_TLS_GROUP_NONE
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005154};
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005155
Ronald Crone68ab4f2022-10-05 12:46:29 +02005156#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08005157/* Function for checking `ssl_preset_*_sig_algs` and `ssl_tls12_preset_*_sig_algs`
Jerry Yu370e1462022-01-25 10:36:53 +08005158 * to make sure there are no duplicated signature algorithm entries. */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005159MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01005160static int ssl_check_no_sig_alg_duplication(uint16_t *sig_algs)
Jerry Yu1a8b4812022-01-20 17:56:50 +08005161{
5162 size_t i, j;
5163 int ret = 0;
Jerry Yu909df7b2022-01-22 11:56:27 +08005164
Gilles Peskine449bd832023-01-11 14:50:10 +01005165 for (i = 0; sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++) {
5166 for (j = 0; j < i; j++) {
5167 if (sig_algs[i] != sig_algs[j]) {
Jerry Yuf377d642022-01-25 10:43:59 +08005168 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01005169 }
5170 mbedtls_printf(" entry(%04x,%" MBEDTLS_PRINTF_SIZET
5171 ") is duplicated at %" MBEDTLS_PRINTF_SIZET "\n",
5172 sig_algs[i], j, i);
Jerry Yuf377d642022-01-25 10:43:59 +08005173 ret = -1;
Jerry Yu1a8b4812022-01-20 17:56:50 +08005174 }
5175 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005176 return ret;
Jerry Yu1a8b4812022-01-20 17:56:50 +08005177}
5178
Ronald Crone68ab4f2022-10-05 12:46:29 +02005179#endif /* MBEDTLS_DEBUG_C && MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yu1a8b4812022-01-20 17:56:50 +08005180
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005181/*
Tillmann Karras588ad502015-09-25 04:27:22 +02005182 * Load default in mbedtls_ssl_config
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005183 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005184int mbedtls_ssl_config_defaults(mbedtls_ssl_config *conf,
5185 int endpoint, int transport, int preset)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005186{
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005187#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Janos Follath865b3eb2019-12-16 11:46:15 +00005188 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard8b431fb2015-05-11 12:54:52 +02005189#endif
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005190
Ronald Crone68ab4f2022-10-05 12:46:29 +02005191#if defined(MBEDTLS_DEBUG_C) && defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01005192 if (ssl_check_no_sig_alg_duplication(ssl_preset_suiteb_sig_algs)) {
5193 mbedtls_printf("ssl_preset_suiteb_sig_algs has duplicated entries\n");
5194 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu1a8b4812022-01-20 17:56:50 +08005195 }
5196
Gilles Peskine449bd832023-01-11 14:50:10 +01005197 if (ssl_check_no_sig_alg_duplication(ssl_preset_default_sig_algs)) {
5198 mbedtls_printf("ssl_preset_default_sig_algs has duplicated entries\n");
5199 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu1a8b4812022-01-20 17:56:50 +08005200 }
Jerry Yu909df7b2022-01-22 11:56:27 +08005201
5202#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01005203 if (ssl_check_no_sig_alg_duplication(ssl_tls12_preset_suiteb_sig_algs)) {
5204 mbedtls_printf("ssl_tls12_preset_suiteb_sig_algs has duplicated entries\n");
5205 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu909df7b2022-01-22 11:56:27 +08005206 }
5207
Gilles Peskine449bd832023-01-11 14:50:10 +01005208 if (ssl_check_no_sig_alg_duplication(ssl_tls12_preset_default_sig_algs)) {
5209 mbedtls_printf("ssl_tls12_preset_default_sig_algs has duplicated entries\n");
5210 return MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jerry Yu909df7b2022-01-22 11:56:27 +08005211 }
5212#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Ronald Crone68ab4f2022-10-05 12:46:29 +02005213#endif /* MBEDTLS_DEBUG_C && MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Jerry Yu1a8b4812022-01-20 17:56:50 +08005214
Manuel Pégourié-Gonnard0de074f2015-05-14 12:58:01 +02005215 /* Use the functions here so that they are covered in tests,
5216 * but otherwise access member directly for efficiency */
Gilles Peskine449bd832023-01-11 14:50:10 +01005217 mbedtls_ssl_conf_endpoint(conf, endpoint);
5218 mbedtls_ssl_conf_transport(conf, transport);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005219
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005220 /*
5221 * Things that are common to all presets
5222 */
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02005223#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005224 if (endpoint == MBEDTLS_SSL_IS_CLIENT) {
Manuel Pégourié-Gonnard419d5ae2015-05-04 19:32:36 +02005225 conf->authmode = MBEDTLS_SSL_VERIFY_REQUIRED;
5226#if defined(MBEDTLS_SSL_SESSION_TICKETS)
5227 conf->session_tickets = MBEDTLS_SSL_SESSION_TICKETS_ENABLED;
5228#endif
5229 }
5230#endif
5231
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005232#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
5233 conf->encrypt_then_mac = MBEDTLS_SSL_ETM_ENABLED;
5234#endif
5235
5236#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
5237 conf->extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
5238#endif
5239
Manuel Pégourié-Gonnarde057d3b2015-05-20 10:59:43 +02005240#if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005241 conf->f_cookie_write = ssl_cookie_write_dummy;
5242 conf->f_cookie_check = ssl_cookie_check_dummy;
5243#endif
5244
5245#if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
5246 conf->anti_replay = MBEDTLS_SSL_ANTI_REPLAY_ENABLED;
5247#endif
5248
Janos Follath088ce432017-04-10 12:42:31 +01005249#if defined(MBEDTLS_SSL_SRV_C)
5250 conf->cert_req_ca_list = MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED;
TRodziewicz3946f792021-06-14 12:11:18 +02005251 conf->respect_cli_pref = MBEDTLS_SSL_SRV_CIPHERSUITE_ORDER_SERVER;
Janos Follath088ce432017-04-10 12:42:31 +01005252#endif
5253
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005254#if defined(MBEDTLS_SSL_PROTO_DTLS)
5255 conf->hs_timeout_min = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN;
5256 conf->hs_timeout_max = MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX;
5257#endif
5258
5259#if defined(MBEDTLS_SSL_RENEGOTIATION)
5260 conf->renego_max_records = MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT;
Gilles Peskine449bd832023-01-11 14:50:10 +01005261 memset(conf->renego_period, 0x00, 2);
5262 memset(conf->renego_period + 2, 0xFF, 6);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005263#endif
5264
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005265#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005266 if (endpoint == MBEDTLS_SSL_IS_SERVER) {
Hanno Beckere2defad2021-07-24 05:59:17 +01005267 const unsigned char dhm_p[] =
5268 MBEDTLS_DHM_RFC3526_MODP_2048_P_BIN;
5269 const unsigned char dhm_g[] =
5270 MBEDTLS_DHM_RFC3526_MODP_2048_G_BIN;
Hanno Becker00d0a682017-10-04 13:14:29 +01005271
Gilles Peskine449bd832023-01-11 14:50:10 +01005272 if ((ret = mbedtls_ssl_conf_dh_param_bin(conf,
5273 dhm_p, sizeof(dhm_p),
5274 dhm_g, sizeof(dhm_g))) != 0) {
5275 return ret;
Hanno Beckere2defad2021-07-24 05:59:17 +01005276 }
5277 }
Manuel Pégourié-Gonnardbd990d62015-06-11 14:49:42 +02005278#endif
5279
Ronald Cron6f135e12021-12-08 16:57:54 +01005280#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Jerry Yu6ee56aa2022-12-06 17:47:22 +08005281
5282#if defined(MBEDTLS_SSL_EARLY_DATA)
Gilles Peskine449bd832023-01-11 14:50:10 +01005283 mbedtls_ssl_tls13_conf_early_data(conf, MBEDTLS_SSL_EARLY_DATA_DISABLED);
Jerry Yu6ee56aa2022-12-06 17:47:22 +08005284#if defined(MBEDTLS_SSL_SRV_C)
5285 mbedtls_ssl_tls13_conf_max_early_data_size(
Gilles Peskine449bd832023-01-11 14:50:10 +01005286 conf, MBEDTLS_SSL_MAX_EARLY_DATA_SIZE);
Jerry Yu6ee56aa2022-12-06 17:47:22 +08005287#endif
5288#endif /* MBEDTLS_SSL_EARLY_DATA */
5289
Jerry Yud0766ec2022-09-22 10:46:57 +08005290#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SESSION_TICKETS)
Jerry Yu1ad7ace2022-08-09 13:28:39 +08005291 mbedtls_ssl_conf_new_session_tickets(
Gilles Peskine449bd832023-01-11 14:50:10 +01005292 conf, MBEDTLS_SSL_TLS1_3_DEFAULT_NEW_SESSION_TICKETS);
Jerry Yu1ad7ace2022-08-09 13:28:39 +08005293#endif
Hanno Becker71f1ed62021-07-24 06:01:47 +01005294 /*
5295 * Allow all TLS 1.3 key exchange modes by default.
5296 */
Xiaofei Bai746f9482021-11-12 08:53:56 +00005297 conf->tls13_kex_modes = MBEDTLS_SSL_TLS1_3_KEY_EXCHANGE_MODE_ALL;
Ronald Cron6f135e12021-12-08 16:57:54 +01005298#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Hanno Becker71f1ed62021-07-24 06:01:47 +01005299
Gilles Peskine449bd832023-01-11 14:50:10 +01005300 if (transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Glenn Strauss2dfcea22022-03-14 17:26:42 -04005301#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
XiaokangQian4d3a6042022-04-21 13:46:17 +00005302 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian060d8672022-04-21 09:24:56 +00005303 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
XiaokangQian4d3a6042022-04-21 13:46:17 +00005304#else
Gilles Peskine449bd832023-01-11 14:50:10 +01005305 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQian060d8672022-04-21 09:24:56 +00005306#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005307 } else {
XiaokangQian4d3a6042022-04-21 13:46:17 +00005308#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01005309 if (endpoint == MBEDTLS_SSL_IS_CLIENT) {
XiaokangQian4d3a6042022-04-21 13:46:17 +00005310 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5311 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
Gilles Peskine449bd832023-01-11 14:50:10 +01005312 } else {
5313 /* Hybrid TLS 1.2 / 1.3 is not supported on server side yet */
XiaokangQian4d3a6042022-04-21 13:46:17 +00005314 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5315 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5316 }
5317#elif defined(MBEDTLS_SSL_PROTO_TLS1_3)
5318 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
5319 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_3;
5320#elif defined(MBEDTLS_SSL_PROTO_TLS1_2)
5321 conf->min_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5322 conf->max_tls_version = MBEDTLS_SSL_VERSION_TLS1_2;
5323#else
Gilles Peskine449bd832023-01-11 14:50:10 +01005324 return MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
XiaokangQian4d3a6042022-04-21 13:46:17 +00005325#endif
5326 }
Glenn Strauss2dfcea22022-03-14 17:26:42 -04005327
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005328 /*
5329 * Preset-specific defaults
5330 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005331 switch (preset) {
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005332 /*
5333 * NSA Suite B
5334 */
5335 case MBEDTLS_SSL_PRESET_SUITEB:
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005336
Hanno Beckerd60b6c62021-04-29 12:04:11 +01005337 conf->ciphersuite_list = ssl_preset_suiteb_ciphersuites;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005338
5339#if defined(MBEDTLS_X509_CRT_PARSE_C)
5340 conf->cert_profile = &mbedtls_x509_crt_profile_suiteb;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005341#endif
5342
Ronald Crone68ab4f2022-10-05 12:46:29 +02005343#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08005344#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01005345 if (mbedtls_ssl_conf_is_tls12_only(conf)) {
Jerry Yu909df7b2022-01-22 11:56:27 +08005346 conf->sig_algs = ssl_tls12_preset_suiteb_sig_algs;
Gilles Peskine449bd832023-01-11 14:50:10 +01005347 } else
Jerry Yu909df7b2022-01-22 11:56:27 +08005348#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005349 conf->sig_algs = ssl_preset_suiteb_sig_algs;
Ronald Crone68ab4f2022-10-05 12:46:29 +02005350#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005351
Brett Warrene0edc842021-08-17 09:53:13 +01005352#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
5353 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005354#endif
Brett Warrene0edc842021-08-17 09:53:13 +01005355 conf->group_list = ssl_preset_suiteb_groups;
Manuel Pégourié-Gonnardc98204e2015-08-11 04:21:01 +02005356 break;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005357
5358 /*
5359 * Default
5360 */
5361 default:
Ronald Cronf6606552022-03-15 11:23:25 +01005362
Hanno Beckerd60b6c62021-04-29 12:04:11 +01005363 conf->ciphersuite_list = mbedtls_ssl_list_ciphersuites();
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005364
5365#if defined(MBEDTLS_X509_CRT_PARSE_C)
5366 conf->cert_profile = &mbedtls_x509_crt_profile_default;
5367#endif
5368
Ronald Crone68ab4f2022-10-05 12:46:29 +02005369#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Jerry Yu909df7b2022-01-22 11:56:27 +08005370#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01005371 if (mbedtls_ssl_conf_is_tls12_only(conf)) {
Jerry Yu909df7b2022-01-22 11:56:27 +08005372 conf->sig_algs = ssl_tls12_preset_default_sig_algs;
Gilles Peskine449bd832023-01-11 14:50:10 +01005373 } else
Jerry Yu909df7b2022-01-22 11:56:27 +08005374#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005375 conf->sig_algs = ssl_preset_default_sig_algs;
Ronald Crone68ab4f2022-10-05 12:46:29 +02005376#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005377
Brett Warrene0edc842021-08-17 09:53:13 +01005378#if defined(MBEDTLS_ECP_C) && !defined(MBEDTLS_DEPRECATED_REMOVED)
5379 conf->curve_list = NULL;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005380#endif
Brett Warrene0edc842021-08-17 09:53:13 +01005381 conf->group_list = ssl_preset_default_groups;
Manuel Pégourié-Gonnardb31c5f62015-06-17 13:53:47 +02005382
5383#if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
5384 conf->dhm_min_bitlen = 1024;
5385#endif
5386 }
5387
Gilles Peskine449bd832023-01-11 14:50:10 +01005388 return 0;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005389}
5390
5391/*
5392 * Free mbedtls_ssl_config
5393 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005394void mbedtls_ssl_config_free(mbedtls_ssl_config *conf)
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005395{
5396#if defined(MBEDTLS_DHM_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005397 mbedtls_mpi_free(&conf->dhm_P);
5398 mbedtls_mpi_free(&conf->dhm_G);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005399#endif
5400
Ronald Cron73fe8df2022-10-05 14:31:43 +02005401#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED)
Neil Armstrong501c9322022-05-03 09:35:09 +02005402#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01005403 if (!mbedtls_svc_key_id_is_null(conf->psk_opaque)) {
Neil Armstrong501c9322022-05-03 09:35:09 +02005404 conf->psk_opaque = MBEDTLS_SVC_KEY_ID_INIT;
5405 }
Neil Armstrong8ecd6682022-05-05 11:40:35 +02005406#endif /* MBEDTLS_USE_PSA_CRYPTO */
Gilles Peskine449bd832023-01-11 14:50:10 +01005407 if (conf->psk != NULL) {
5408 mbedtls_platform_zeroize(conf->psk, conf->psk_len);
5409 mbedtls_free(conf->psk);
Azim Khan27e8a122018-03-21 14:24:11 +00005410 conf->psk = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005411 conf->psk_len = 0;
junyeonLEE316b1622017-12-20 16:29:30 +09005412 }
5413
Gilles Peskine449bd832023-01-11 14:50:10 +01005414 if (conf->psk_identity != NULL) {
5415 mbedtls_platform_zeroize(conf->psk_identity, conf->psk_identity_len);
5416 mbedtls_free(conf->psk_identity);
Azim Khan27e8a122018-03-21 14:24:11 +00005417 conf->psk_identity = NULL;
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005418 conf->psk_identity_len = 0;
5419 }
Ronald Cron73fe8df2022-10-05 14:31:43 +02005420#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_PSK_ENABLED */
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005421
5422#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005423 ssl_key_cert_free(conf->key_cert);
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005424#endif
5425
Gilles Peskine449bd832023-01-11 14:50:10 +01005426 mbedtls_platform_zeroize(conf, sizeof(mbedtls_ssl_config));
Manuel Pégourié-Gonnardcd523e22015-05-04 13:35:39 +02005427}
5428
Manuel Pégourié-Gonnard5674a972015-10-19 15:14:03 +02005429#if defined(MBEDTLS_PK_C) && \
Valerio Setti1ad9ef22023-02-22 12:38:07 +01005430 (defined(MBEDTLS_RSA_C) || defined(MBEDTLS_PK_CAN_ECDSA_SOME))
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005431/*
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005432 * Convert between MBEDTLS_PK_XXX and SSL_SIG_XXX
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005433 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005434unsigned char mbedtls_ssl_sig_from_pk(mbedtls_pk_context *pk)
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005435{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005436#if defined(MBEDTLS_RSA_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005437 if (mbedtls_pk_can_do(pk, MBEDTLS_PK_RSA)) {
5438 return MBEDTLS_SSL_SIG_RSA;
5439 }
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005440#endif
Valerio Setti1ad9ef22023-02-22 12:38:07 +01005441#if defined(MBEDTLS_PK_CAN_ECDSA_SOME)
Gilles Peskine449bd832023-01-11 14:50:10 +01005442 if (mbedtls_pk_can_do(pk, MBEDTLS_PK_ECDSA)) {
5443 return MBEDTLS_SSL_SIG_ECDSA;
5444 }
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005445#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005446 return MBEDTLS_SSL_SIG_ANON;
Manuel Pégourié-Gonnard0d420492013-08-21 16:14:26 +02005447}
5448
Gilles Peskine449bd832023-01-11 14:50:10 +01005449unsigned char mbedtls_ssl_sig_from_pk_alg(mbedtls_pk_type_t type)
Hanno Becker7e5437a2017-04-28 17:15:26 +01005450{
Gilles Peskine449bd832023-01-11 14:50:10 +01005451 switch (type) {
Hanno Becker7e5437a2017-04-28 17:15:26 +01005452 case MBEDTLS_PK_RSA:
Gilles Peskine449bd832023-01-11 14:50:10 +01005453 return MBEDTLS_SSL_SIG_RSA;
Hanno Becker7e5437a2017-04-28 17:15:26 +01005454 case MBEDTLS_PK_ECDSA:
5455 case MBEDTLS_PK_ECKEY:
Gilles Peskine449bd832023-01-11 14:50:10 +01005456 return MBEDTLS_SSL_SIG_ECDSA;
Hanno Becker7e5437a2017-04-28 17:15:26 +01005457 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005458 return MBEDTLS_SSL_SIG_ANON;
Hanno Becker7e5437a2017-04-28 17:15:26 +01005459 }
5460}
5461
Gilles Peskine449bd832023-01-11 14:50:10 +01005462mbedtls_pk_type_t mbedtls_ssl_pk_alg_from_sig(unsigned char sig)
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005463{
Gilles Peskine449bd832023-01-11 14:50:10 +01005464 switch (sig) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005465#if defined(MBEDTLS_RSA_C)
5466 case MBEDTLS_SSL_SIG_RSA:
Gilles Peskine449bd832023-01-11 14:50:10 +01005467 return MBEDTLS_PK_RSA;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005468#endif
Valerio Setti1ad9ef22023-02-22 12:38:07 +01005469#if defined(MBEDTLS_PK_CAN_ECDSA_SOME)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005470 case MBEDTLS_SSL_SIG_ECDSA:
Gilles Peskine449bd832023-01-11 14:50:10 +01005471 return MBEDTLS_PK_ECDSA;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005472#endif
5473 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005474 return MBEDTLS_PK_NONE;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005475 }
5476}
Valerio Setti1ad9ef22023-02-22 12:38:07 +01005477#endif /* MBEDTLS_PK_C && ( MBEDTLS_RSA_C || MBEDTLS_PK_CAN_ECDSA_SOME ) */
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005478
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02005479/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005480 * Convert from MBEDTLS_SSL_HASH_XXX to MBEDTLS_MD_XXX
Manuel Pégourié-Gonnard1a483832013-09-20 12:29:15 +02005481 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005482mbedtls_md_type_t mbedtls_ssl_md_alg_from_hash(unsigned char hash)
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005483{
Gilles Peskine449bd832023-01-11 14:50:10 +01005484 switch (hash) {
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005485#if defined(MBEDTLS_MD_CAN_MD5)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005486 case MBEDTLS_SSL_HASH_MD5:
Gilles Peskine449bd832023-01-11 14:50:10 +01005487 return MBEDTLS_MD_MD5;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005488#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005489#if defined(MBEDTLS_MD_CAN_SHA1)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005490 case MBEDTLS_SSL_HASH_SHA1:
Gilles Peskine449bd832023-01-11 14:50:10 +01005491 return MBEDTLS_MD_SHA1;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005492#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005493#if defined(MBEDTLS_MD_CAN_SHA224)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005494 case MBEDTLS_SSL_HASH_SHA224:
Gilles Peskine449bd832023-01-11 14:50:10 +01005495 return MBEDTLS_MD_SHA224;
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02005496#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005497#if defined(MBEDTLS_MD_CAN_SHA256)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005498 case MBEDTLS_SSL_HASH_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01005499 return MBEDTLS_MD_SHA256;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005500#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005501#if defined(MBEDTLS_MD_CAN_SHA384)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005502 case MBEDTLS_SSL_HASH_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01005503 return MBEDTLS_MD_SHA384;
Mateusz Starzyk3352a532021-04-06 14:28:22 +02005504#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005505#if defined(MBEDTLS_MD_CAN_SHA512)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005506 case MBEDTLS_SSL_HASH_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01005507 return MBEDTLS_MD_SHA512;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005508#endif
5509 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005510 return MBEDTLS_MD_NONE;
Manuel Pégourié-Gonnarda20c58c2013-08-22 13:52:48 +02005511 }
5512}
5513
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005514/*
5515 * Convert from MBEDTLS_MD_XXX to MBEDTLS_SSL_HASH_XXX
5516 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005517unsigned char mbedtls_ssl_hash_from_md_alg(int md)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005518{
Gilles Peskine449bd832023-01-11 14:50:10 +01005519 switch (md) {
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005520#if defined(MBEDTLS_MD_CAN_MD5)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005521 case MBEDTLS_MD_MD5:
Gilles Peskine449bd832023-01-11 14:50:10 +01005522 return MBEDTLS_SSL_HASH_MD5;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005523#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005524#if defined(MBEDTLS_MD_CAN_SHA1)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005525 case MBEDTLS_MD_SHA1:
Gilles Peskine449bd832023-01-11 14:50:10 +01005526 return MBEDTLS_SSL_HASH_SHA1;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005527#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005528#if defined(MBEDTLS_MD_CAN_SHA224)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005529 case MBEDTLS_MD_SHA224:
Gilles Peskine449bd832023-01-11 14:50:10 +01005530 return MBEDTLS_SSL_HASH_SHA224;
Mateusz Starzyke3c48b42021-04-19 16:46:28 +02005531#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005532#if defined(MBEDTLS_MD_CAN_SHA256)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005533 case MBEDTLS_MD_SHA256:
Gilles Peskine449bd832023-01-11 14:50:10 +01005534 return MBEDTLS_SSL_HASH_SHA256;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005535#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005536#if defined(MBEDTLS_MD_CAN_SHA384)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005537 case MBEDTLS_MD_SHA384:
Gilles Peskine449bd832023-01-11 14:50:10 +01005538 return MBEDTLS_SSL_HASH_SHA384;
Mateusz Starzyk3352a532021-04-06 14:28:22 +02005539#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005540#if defined(MBEDTLS_MD_CAN_SHA512)
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005541 case MBEDTLS_MD_SHA512:
Gilles Peskine449bd832023-01-11 14:50:10 +01005542 return MBEDTLS_SSL_HASH_SHA512;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005543#endif
5544 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01005545 return MBEDTLS_SSL_HASH_NONE;
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005546 }
5547}
5548
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005549/*
Manuel Pégourié-Gonnard7bfc1222015-06-17 14:34:48 +02005550 * Check if a curve proposed by the peer is in our list.
Manuel Pégourié-Gonnard9d412d82015-06-17 12:10:46 +02005551 * Return 0 if we're willing to use it, -1 otherwise.
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005552 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005553int mbedtls_ssl_check_curve_tls_id(const mbedtls_ssl_context *ssl, uint16_t tls_id)
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005554{
Gilles Peskine449bd832023-01-11 14:50:10 +01005555 const uint16_t *group_list = mbedtls_ssl_get_groups(ssl);
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005556
Gilles Peskine449bd832023-01-11 14:50:10 +01005557 if (group_list == NULL) {
5558 return -1;
Brett Warrene0edc842021-08-17 09:53:13 +01005559 }
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005560
Gilles Peskine449bd832023-01-11 14:50:10 +01005561 for (; *group_list != 0; group_list++) {
5562 if (*group_list == tls_id) {
5563 return 0;
5564 }
5565 }
5566
5567 return -1;
Manuel Pégourié-Gonnardab240102014-02-04 16:18:07 +01005568}
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005569
5570#if defined(MBEDTLS_ECP_C)
5571/*
5572 * Same as mbedtls_ssl_check_curve_tls_id() but with a mbedtls_ecp_group_id.
5573 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005574int mbedtls_ssl_check_curve(const mbedtls_ssl_context *ssl, mbedtls_ecp_group_id grp_id)
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005575{
Gilles Peskine449bd832023-01-11 14:50:10 +01005576 uint16_t tls_id = mbedtls_ssl_get_tls_id_from_ecp_group_id(grp_id);
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005577
Gilles Peskine449bd832023-01-11 14:50:10 +01005578 if (tls_id == 0) {
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005579 return -1;
Gilles Peskine449bd832023-01-11 14:50:10 +01005580 }
Leonid Rozenboim19e59732022-08-08 16:52:38 -07005581
Gilles Peskine449bd832023-01-11 14:50:10 +01005582 return mbedtls_ssl_check_curve_tls_id(ssl, tls_id);
Manuel Pégourié-Gonnard0d63b842022-01-18 13:10:56 +01005583}
Manuel Pégourié-Gonnardb541da62015-06-17 11:43:30 +02005584#endif /* MBEDTLS_ECP_C */
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005585
Gilles Peskine449bd832023-01-11 14:50:10 +01005586#if defined(MBEDTLS_DEBUG_C)
Valerio Setti67419f02023-01-04 16:12:42 +01005587#define EC_NAME(_name_) _name_
5588#else
5589#define EC_NAME(_name_) NULL
5590#endif
5591
Valerio Setti18c9fed2022-12-30 17:44:24 +01005592static const struct {
5593 uint16_t tls_id;
5594 mbedtls_ecp_group_id ecp_group_id;
5595 psa_ecc_family_t psa_family;
5596 uint16_t bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01005597 const char *name;
Valerio Setti18c9fed2022-12-30 17:44:24 +01005598} tls_id_match_table[] =
5599{
5600#if defined(MBEDTLS_ECP_DP_SECP521R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_521)
Gilles Peskine449bd832023-01-11 14:50:10 +01005601 { 25, MBEDTLS_ECP_DP_SECP521R1, PSA_ECC_FAMILY_SECP_R1, 521, EC_NAME("secp521r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005602#endif
5603#if defined(MBEDTLS_ECP_DP_BP512R1_ENABLED) || defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512)
Gilles Peskine449bd832023-01-11 14:50:10 +01005604 { 28, MBEDTLS_ECP_DP_BP512R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 512, EC_NAME("brainpoolP512r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005605#endif
Valerio Setti67419f02023-01-04 16:12:42 +01005606#if defined(MBEDTLS_ECP_DP_SECP384R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_384)
Gilles Peskine449bd832023-01-11 14:50:10 +01005607 { 24, MBEDTLS_ECP_DP_SECP384R1, PSA_ECC_FAMILY_SECP_R1, 384, EC_NAME("secp384r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005608#endif
Valerio Setti67419f02023-01-04 16:12:42 +01005609#if defined(MBEDTLS_ECP_DP_BP384R1_ENABLED) || defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384)
Gilles Peskine449bd832023-01-11 14:50:10 +01005610 { 27, MBEDTLS_ECP_DP_BP384R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 384, EC_NAME("brainpoolP384r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005611#endif
Valerio Setti67419f02023-01-04 16:12:42 +01005612#if defined(MBEDTLS_ECP_DP_SECP256R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_256)
Gilles Peskine449bd832023-01-11 14:50:10 +01005613 { 23, MBEDTLS_ECP_DP_SECP256R1, PSA_ECC_FAMILY_SECP_R1, 256, EC_NAME("secp256r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005614#endif
5615#if defined(MBEDTLS_ECP_DP_SECP256K1_ENABLED) || defined(PSA_WANT_ECC_SECP_K1_256)
Gilles Peskine449bd832023-01-11 14:50:10 +01005616 { 22, MBEDTLS_ECP_DP_SECP256K1, PSA_ECC_FAMILY_SECP_K1, 256, EC_NAME("secp256k1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005617#endif
Valerio Setti67419f02023-01-04 16:12:42 +01005618#if defined(MBEDTLS_ECP_DP_BP256R1_ENABLED) || defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256)
Gilles Peskine449bd832023-01-11 14:50:10 +01005619 { 26, MBEDTLS_ECP_DP_BP256R1, PSA_ECC_FAMILY_BRAINPOOL_P_R1, 256, EC_NAME("brainpoolP256r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005620#endif
5621#if defined(MBEDTLS_ECP_DP_SECP224R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_224)
Gilles Peskine449bd832023-01-11 14:50:10 +01005622 { 21, MBEDTLS_ECP_DP_SECP224R1, PSA_ECC_FAMILY_SECP_R1, 224, EC_NAME("secp224r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005623#endif
5624#if defined(MBEDTLS_ECP_DP_SECP224K1_ENABLED) || defined(PSA_WANT_ECC_SECP_K1_224)
Gilles Peskine449bd832023-01-11 14:50:10 +01005625 { 20, MBEDTLS_ECP_DP_SECP224K1, PSA_ECC_FAMILY_SECP_K1, 224, EC_NAME("secp224k1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005626#endif
5627#if defined(MBEDTLS_ECP_DP_SECP192R1_ENABLED) || defined(PSA_WANT_ECC_SECP_R1_192)
Gilles Peskine449bd832023-01-11 14:50:10 +01005628 { 19, MBEDTLS_ECP_DP_SECP192R1, PSA_ECC_FAMILY_SECP_R1, 192, EC_NAME("secp192r1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005629#endif
5630#if defined(MBEDTLS_ECP_DP_SECP192K1_ENABLED) || defined(PSA_WANT_ECC_SECP_K1_192)
Gilles Peskine449bd832023-01-11 14:50:10 +01005631 { 18, MBEDTLS_ECP_DP_SECP192K1, PSA_ECC_FAMILY_SECP_K1, 192, EC_NAME("secp192k1") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005632#endif
5633#if defined(MBEDTLS_ECP_DP_CURVE25519_ENABLED) || defined(PSA_WANT_ECC_MONTGOMERY_255)
Gilles Peskine449bd832023-01-11 14:50:10 +01005634 { 29, MBEDTLS_ECP_DP_CURVE25519, PSA_ECC_FAMILY_MONTGOMERY, 255, EC_NAME("x25519") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005635#endif
5636#if defined(MBEDTLS_ECP_DP_CURVE448_ENABLED) || defined(PSA_WANT_ECC_MONTGOMERY_448)
Gilles Peskine449bd832023-01-11 14:50:10 +01005637 { 30, MBEDTLS_ECP_DP_CURVE448, PSA_ECC_FAMILY_MONTGOMERY, 448, EC_NAME("x448") },
Valerio Setti18c9fed2022-12-30 17:44:24 +01005638#endif
5639 { 0, MBEDTLS_ECP_DP_NONE, 0, 0, NULL },
5640};
5641
Gilles Peskine449bd832023-01-11 14:50:10 +01005642int mbedtls_ssl_get_psa_curve_info_from_tls_id(uint16_t tls_id,
5643 psa_ecc_family_t *family,
5644 size_t *bits)
Valerio Setti18c9fed2022-12-30 17:44:24 +01005645{
Gilles Peskine449bd832023-01-11 14:50:10 +01005646 for (int i = 0; tls_id_match_table[i].tls_id != 0; i++) {
5647 if (tls_id_match_table[i].tls_id == tls_id) {
5648 if (family != NULL) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01005649 *family = tls_id_match_table[i].psa_family;
Gilles Peskine449bd832023-01-11 14:50:10 +01005650 }
5651 if (bits != NULL) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01005652 *bits = tls_id_match_table[i].bits;
Gilles Peskine449bd832023-01-11 14:50:10 +01005653 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01005654 return PSA_SUCCESS;
5655 }
5656 }
5657
5658 return PSA_ERROR_NOT_SUPPORTED;
5659}
5660
Gilles Peskine449bd832023-01-11 14:50:10 +01005661mbedtls_ecp_group_id mbedtls_ssl_get_ecp_group_id_from_tls_id(uint16_t tls_id)
Valerio Setti18c9fed2022-12-30 17:44:24 +01005662{
Gilles Peskine449bd832023-01-11 14:50:10 +01005663 for (int i = 0; tls_id_match_table[i].tls_id != 0; i++) {
5664 if (tls_id_match_table[i].tls_id == tls_id) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01005665 return tls_id_match_table[i].ecp_group_id;
Gilles Peskine449bd832023-01-11 14:50:10 +01005666 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01005667 }
5668
5669 return MBEDTLS_ECP_DP_NONE;
5670}
5671
Gilles Peskine449bd832023-01-11 14:50:10 +01005672uint16_t mbedtls_ssl_get_tls_id_from_ecp_group_id(mbedtls_ecp_group_id grp_id)
Valerio Setti18c9fed2022-12-30 17:44:24 +01005673{
Gilles Peskine449bd832023-01-11 14:50:10 +01005674 for (int i = 0; tls_id_match_table[i].ecp_group_id != MBEDTLS_ECP_DP_NONE;
5675 i++) {
5676 if (tls_id_match_table[i].ecp_group_id == grp_id) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01005677 return tls_id_match_table[i].tls_id;
Gilles Peskine449bd832023-01-11 14:50:10 +01005678 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01005679 }
5680
5681 return 0;
5682}
5683
Valerio Setti67419f02023-01-04 16:12:42 +01005684#if defined(MBEDTLS_DEBUG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005685const char *mbedtls_ssl_get_curve_name_from_tls_id(uint16_t tls_id)
Valerio Setti18c9fed2022-12-30 17:44:24 +01005686{
Gilles Peskine449bd832023-01-11 14:50:10 +01005687 for (int i = 0; tls_id_match_table[i].tls_id != 0; i++) {
5688 if (tls_id_match_table[i].tls_id == tls_id) {
Valerio Setti18c9fed2022-12-30 17:44:24 +01005689 return tls_id_match_table[i].name;
Gilles Peskine449bd832023-01-11 14:50:10 +01005690 }
Valerio Setti18c9fed2022-12-30 17:44:24 +01005691 }
5692
5693 return NULL;
5694}
Valerio Setti67419f02023-01-04 16:12:42 +01005695#endif
Valerio Setti18c9fed2022-12-30 17:44:24 +01005696
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005697#if defined(MBEDTLS_X509_CRT_PARSE_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01005698int mbedtls_ssl_check_cert_usage(const mbedtls_x509_crt *cert,
5699 const mbedtls_ssl_ciphersuite_t *ciphersuite,
5700 int cert_endpoint,
5701 uint32_t *flags)
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005702{
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005703 int ret = 0;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005704 int usage = 0;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005705 const char *ext_oid;
5706 size_t ext_len;
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005707
Gilles Peskine449bd832023-01-11 14:50:10 +01005708 if (cert_endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005709 /* Server part of the key exchange */
Gilles Peskine449bd832023-01-11 14:50:10 +01005710 switch (ciphersuite->key_exchange) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005711 case MBEDTLS_KEY_EXCHANGE_RSA:
5712 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005713 usage = MBEDTLS_X509_KU_KEY_ENCIPHERMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005714 break;
5715
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005716 case MBEDTLS_KEY_EXCHANGE_DHE_RSA:
5717 case MBEDTLS_KEY_EXCHANGE_ECDHE_RSA:
5718 case MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA:
5719 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005720 break;
5721
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005722 case MBEDTLS_KEY_EXCHANGE_ECDH_RSA:
5723 case MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA:
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005724 usage = MBEDTLS_X509_KU_KEY_AGREEMENT;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005725 break;
5726
5727 /* Don't use default: we want warnings when adding new values */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005728 case MBEDTLS_KEY_EXCHANGE_NONE:
5729 case MBEDTLS_KEY_EXCHANGE_PSK:
5730 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
5731 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Manuel Pégourié-Gonnard557535d2015-09-15 17:53:32 +02005732 case MBEDTLS_KEY_EXCHANGE_ECJPAKE:
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005733 usage = 0;
5734 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005735 } else {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005736 /* Client auth: we only implement rsa_sign and mbedtls_ecdsa_sign for now */
5737 usage = MBEDTLS_X509_KU_DIGITAL_SIGNATURE;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005738 }
5739
Gilles Peskine449bd832023-01-11 14:50:10 +01005740 if (mbedtls_x509_crt_check_key_usage(cert, usage) != 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005741 *flags |= MBEDTLS_X509_BADCERT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005742 ret = -1;
5743 }
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005744
Gilles Peskine449bd832023-01-11 14:50:10 +01005745 if (cert_endpoint == MBEDTLS_SSL_IS_SERVER) {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005746 ext_oid = MBEDTLS_OID_SERVER_AUTH;
Gilles Peskine449bd832023-01-11 14:50:10 +01005747 ext_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_SERVER_AUTH);
5748 } else {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005749 ext_oid = MBEDTLS_OID_CLIENT_AUTH;
Gilles Peskine449bd832023-01-11 14:50:10 +01005750 ext_len = MBEDTLS_OID_SIZE(MBEDTLS_OID_CLIENT_AUTH);
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005751 }
5752
Gilles Peskine449bd832023-01-11 14:50:10 +01005753 if (mbedtls_x509_crt_check_extended_key_usage(cert, ext_oid, ext_len) != 0) {
Manuel Pégourié-Gonnarde6028c92015-04-20 12:19:02 +01005754 *flags |= MBEDTLS_X509_BADCERT_EXT_KEY_USAGE;
Manuel Pégourié-Gonnarde6efa6f2015-04-20 11:01:48 +01005755 ret = -1;
5756 }
Manuel Pégourié-Gonnard0408fd12014-04-11 11:06:22 +02005757
Gilles Peskine449bd832023-01-11 14:50:10 +01005758 return ret;
Manuel Pégourié-Gonnard7f2a07d2014-04-09 09:50:57 +02005759}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02005760#endif /* MBEDTLS_X509_CRT_PARSE_C */
Manuel Pégourié-Gonnard3a306b92014-04-29 15:11:17 +02005761
Jerry Yu148165c2021-09-24 23:20:59 +08005762#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01005763int mbedtls_ssl_get_handshake_transcript(mbedtls_ssl_context *ssl,
5764 const mbedtls_md_type_t md,
5765 unsigned char *dst,
5766 size_t dst_len,
5767 size_t *olen)
Jerry Yu148165c2021-09-24 23:20:59 +08005768{
Ronald Cronf6893e12022-01-07 22:09:01 +01005769 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
5770 psa_hash_operation_t *hash_operation_to_clone;
5771 psa_hash_operation_t hash_operation = psa_hash_operation_init();
5772
Jerry Yu148165c2021-09-24 23:20:59 +08005773 *olen = 0;
Ronald Cronf6893e12022-01-07 22:09:01 +01005774
Gilles Peskine449bd832023-01-11 14:50:10 +01005775 switch (md) {
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005776#if defined(MBEDTLS_MD_CAN_SHA384)
Gilles Peskine449bd832023-01-11 14:50:10 +01005777 case MBEDTLS_MD_SHA384:
5778 hash_operation_to_clone = &ssl->handshake->fin_sha384_psa;
5779 break;
Ronald Cronf6893e12022-01-07 22:09:01 +01005780#endif
5781
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005782#if defined(MBEDTLS_MD_CAN_SHA256)
Gilles Peskine449bd832023-01-11 14:50:10 +01005783 case MBEDTLS_MD_SHA256:
5784 hash_operation_to_clone = &ssl->handshake->fin_sha256_psa;
5785 break;
Ronald Cronf6893e12022-01-07 22:09:01 +01005786#endif
5787
Gilles Peskine449bd832023-01-11 14:50:10 +01005788 default:
5789 goto exit;
5790 }
5791
5792 status = psa_hash_clone(hash_operation_to_clone, &hash_operation);
5793 if (status != PSA_SUCCESS) {
Ronald Cronf6893e12022-01-07 22:09:01 +01005794 goto exit;
5795 }
5796
Gilles Peskine449bd832023-01-11 14:50:10 +01005797 status = psa_hash_finish(&hash_operation, dst, dst_len, olen);
5798 if (status != PSA_SUCCESS) {
Ronald Cronf6893e12022-01-07 22:09:01 +01005799 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01005800 }
Ronald Cronf6893e12022-01-07 22:09:01 +01005801
5802exit:
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005803#if !defined(MBEDTLS_MD_CAN_SHA384) && \
5804 !defined(MBEDTLS_MD_CAN_SHA256)
Andrzej Kurekeabeb302022-10-17 07:52:51 -04005805 (void) ssl;
5806#endif
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05005807 return PSA_TO_MBEDTLS_ERR(status);
Jerry Yu148165c2021-09-24 23:20:59 +08005808}
5809#else /* MBEDTLS_USE_PSA_CRYPTO */
5810
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005811#if defined(MBEDTLS_MD_CAN_SHA384)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005812MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01005813static int ssl_get_handshake_transcript_sha384(mbedtls_ssl_context *ssl,
5814 unsigned char *dst,
5815 size_t dst_len,
5816 size_t *olen)
Jerry Yu24c0ec32021-09-09 14:21:07 +08005817{
Jerry Yu24c0ec32021-09-09 14:21:07 +08005818 int ret;
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01005819 mbedtls_md_context_t sha384;
Jerry Yu24c0ec32021-09-09 14:21:07 +08005820
Gilles Peskine449bd832023-01-11 14:50:10 +01005821 if (dst_len < 48) {
5822 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
5823 }
Jerry Yu24c0ec32021-09-09 14:21:07 +08005824
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01005825 mbedtls_md_init(&sha384);
5826 ret = mbedtls_md_setup(&sha384, mbedtls_md_info_from_type(MBEDTLS_MD_SHA384), 0);
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01005827 if (ret != 0) {
5828 goto exit;
5829 }
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01005830 ret = mbedtls_md_clone(&sha384, &ssl->handshake->fin_sha384);
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01005831 if (ret != 0) {
5832 goto exit;
5833 }
Jerry Yu24c0ec32021-09-09 14:21:07 +08005834
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01005835 if ((ret = mbedtls_md_finish(&sha384, dst)) != 0) {
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01005836 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_finish", ret);
Jerry Yu24c0ec32021-09-09 14:21:07 +08005837 goto exit;
5838 }
5839
5840 *olen = 48;
5841
5842exit:
5843
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01005844 mbedtls_md_free(&sha384);
Gilles Peskine449bd832023-01-11 14:50:10 +01005845 return ret;
Jerry Yu24c0ec32021-09-09 14:21:07 +08005846}
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005847#endif /* MBEDTLS_MD_CAN_SHA384 */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005848
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005849#if defined(MBEDTLS_MD_CAN_SHA256)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02005850MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01005851static int ssl_get_handshake_transcript_sha256(mbedtls_ssl_context *ssl,
5852 unsigned char *dst,
5853 size_t dst_len,
5854 size_t *olen)
Jerry Yu24c0ec32021-09-09 14:21:07 +08005855{
Jerry Yu24c0ec32021-09-09 14:21:07 +08005856 int ret;
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01005857 mbedtls_md_context_t sha256;
Jerry Yu24c0ec32021-09-09 14:21:07 +08005858
Gilles Peskine449bd832023-01-11 14:50:10 +01005859 if (dst_len < 32) {
5860 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
5861 }
Jerry Yu24c0ec32021-09-09 14:21:07 +08005862
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01005863 mbedtls_md_init(&sha256);
5864 ret = mbedtls_md_setup(&sha256, mbedtls_md_info_from_type(MBEDTLS_MD_SHA256), 0);
5865 if (ret != 0) {
5866 goto exit;
5867 }
5868 ret = mbedtls_md_clone(&sha256, &ssl->handshake->fin_sha256);
5869 if (ret != 0) {
5870 goto exit;
5871 }
Jerry Yuc5aef882021-12-23 20:15:02 +08005872
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01005873 if ((ret = mbedtls_md_finish(&sha256, dst)) != 0) {
5874 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_finish", ret);
Jerry Yu24c0ec32021-09-09 14:21:07 +08005875 goto exit;
5876 }
5877
5878 *olen = 32;
5879
5880exit:
5881
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01005882 mbedtls_md_free(&sha256);
Gilles Peskine449bd832023-01-11 14:50:10 +01005883 return ret;
Jerry Yu24c0ec32021-09-09 14:21:07 +08005884}
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005885#endif /* MBEDTLS_MD_CAN_SHA256 */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005886
Gilles Peskine449bd832023-01-11 14:50:10 +01005887int mbedtls_ssl_get_handshake_transcript(mbedtls_ssl_context *ssl,
5888 const mbedtls_md_type_t md,
5889 unsigned char *dst,
5890 size_t dst_len,
5891 size_t *olen)
Jerry Yu24c0ec32021-09-09 14:21:07 +08005892{
Gilles Peskine449bd832023-01-11 14:50:10 +01005893 switch (md) {
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005894
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005895#if defined(MBEDTLS_MD_CAN_SHA384)
Gilles Peskine449bd832023-01-11 14:50:10 +01005896 case MBEDTLS_MD_SHA384:
5897 return ssl_get_handshake_transcript_sha384(ssl, dst, dst_len, olen);
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005898#endif /* MBEDTLS_MD_CAN_SHA384*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005899
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005900#if defined(MBEDTLS_MD_CAN_SHA256)
Gilles Peskine449bd832023-01-11 14:50:10 +01005901 case MBEDTLS_MD_SHA256:
5902 return ssl_get_handshake_transcript_sha256(ssl, dst, dst_len, olen);
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005903#endif /* MBEDTLS_MD_CAN_SHA256*/
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005904
Gilles Peskine449bd832023-01-11 14:50:10 +01005905 default:
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01005906#if !defined(MBEDTLS_MD_CAN_SHA384) && \
5907 !defined(MBEDTLS_MD_CAN_SHA256)
Gilles Peskine449bd832023-01-11 14:50:10 +01005908 (void) ssl;
5909 (void) dst;
5910 (void) dst_len;
5911 (void) olen;
Andrzej Kurek409248a2022-10-24 10:33:21 -04005912#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01005913 break;
Jerry Yuc1ddeef2021-10-08 15:14:45 +08005914 }
Gilles Peskine449bd832023-01-11 14:50:10 +01005915 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu24c0ec32021-09-09 14:21:07 +08005916}
XiaokangQian647719a2021-12-07 09:16:29 +00005917
Jerry Yu148165c2021-09-24 23:20:59 +08005918#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu24c0ec32021-09-09 14:21:07 +08005919
Ronald Crone68ab4f2022-10-05 12:46:29 +02005920#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
Gabor Mezei078e8032022-04-27 21:17:56 +02005921/* mbedtls_ssl_parse_sig_alg_ext()
5922 *
5923 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
5924 * value (TLS 1.3 RFC8446):
5925 * enum {
5926 * ....
5927 * ecdsa_secp256r1_sha256( 0x0403 ),
5928 * ecdsa_secp384r1_sha384( 0x0503 ),
5929 * ecdsa_secp521r1_sha512( 0x0603 ),
5930 * ....
5931 * } SignatureScheme;
5932 *
5933 * struct {
5934 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
5935 * } SignatureSchemeList;
5936 *
5937 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
5938 * value (TLS 1.2 RFC5246):
5939 * enum {
5940 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
5941 * sha512(6), (255)
5942 * } HashAlgorithm;
5943 *
5944 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
5945 * SignatureAlgorithm;
5946 *
5947 * struct {
5948 * HashAlgorithm hash;
5949 * SignatureAlgorithm signature;
5950 * } SignatureAndHashAlgorithm;
5951 *
5952 * SignatureAndHashAlgorithm
5953 * supported_signature_algorithms<2..2^16-2>;
5954 *
5955 * The TLS 1.3 signature algorithm extension was defined to be a compatible
5956 * generalization of the TLS 1.2 signature algorithm extension.
5957 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
5958 * `SignatureScheme` field of TLS 1.3
5959 *
5960 */
Gilles Peskine449bd832023-01-11 14:50:10 +01005961int mbedtls_ssl_parse_sig_alg_ext(mbedtls_ssl_context *ssl,
5962 const unsigned char *buf,
5963 const unsigned char *end)
Gabor Mezei078e8032022-04-27 21:17:56 +02005964{
5965 const unsigned char *p = buf;
5966 size_t supported_sig_algs_len = 0;
5967 const unsigned char *supported_sig_algs_end;
5968 uint16_t sig_alg;
5969 uint32_t common_idx = 0;
5970
Gilles Peskine449bd832023-01-11 14:50:10 +01005971 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
5972 supported_sig_algs_len = MBEDTLS_GET_UINT16_BE(p, 0);
Gabor Mezei078e8032022-04-27 21:17:56 +02005973 p += 2;
5974
Gilles Peskine449bd832023-01-11 14:50:10 +01005975 memset(ssl->handshake->received_sig_algs, 0,
5976 sizeof(ssl->handshake->received_sig_algs));
Gabor Mezei078e8032022-04-27 21:17:56 +02005977
Gilles Peskine449bd832023-01-11 14:50:10 +01005978 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, supported_sig_algs_len);
Gabor Mezei078e8032022-04-27 21:17:56 +02005979 supported_sig_algs_end = p + supported_sig_algs_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01005980 while (p < supported_sig_algs_end) {
5981 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, supported_sig_algs_end, 2);
5982 sig_alg = MBEDTLS_GET_UINT16_BE(p, 0);
Gabor Mezei078e8032022-04-27 21:17:56 +02005983 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01005984 MBEDTLS_SSL_DEBUG_MSG(4, ("received signature algorithm: 0x%x %s",
5985 sig_alg,
5986 mbedtls_ssl_sig_alg_to_str(sig_alg)));
Jerry Yu2fe6c632022-06-29 10:02:38 +08005987#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
Gilles Peskine449bd832023-01-11 14:50:10 +01005988 if (ssl->tls_version == MBEDTLS_SSL_VERSION_TLS1_2 &&
5989 (!(mbedtls_ssl_sig_alg_is_supported(ssl, sig_alg) &&
5990 mbedtls_ssl_sig_alg_is_offered(ssl, sig_alg)))) {
Gabor Mezei078e8032022-04-27 21:17:56 +02005991 continue;
Jerry Yu2fe6c632022-06-29 10:02:38 +08005992 }
5993#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
Gabor Mezei078e8032022-04-27 21:17:56 +02005994
Gilles Peskine449bd832023-01-11 14:50:10 +01005995 MBEDTLS_SSL_DEBUG_MSG(4, ("valid signature algorithm: %s",
5996 mbedtls_ssl_sig_alg_to_str(sig_alg)));
Gabor Mezei078e8032022-04-27 21:17:56 +02005997
Gilles Peskine449bd832023-01-11 14:50:10 +01005998 if (common_idx + 1 < MBEDTLS_RECEIVED_SIG_ALGS_SIZE) {
Gabor Mezei078e8032022-04-27 21:17:56 +02005999 ssl->handshake->received_sig_algs[common_idx] = sig_alg;
6000 common_idx += 1;
6001 }
6002 }
6003 /* Check that we consumed all the message. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006004 if (p != end) {
6005 MBEDTLS_SSL_DEBUG_MSG(1,
6006 ("Signature algorithms extension length misaligned"));
6007 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR,
6008 MBEDTLS_ERR_SSL_DECODE_ERROR);
6009 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Gabor Mezei078e8032022-04-27 21:17:56 +02006010 }
6011
Gilles Peskine449bd832023-01-11 14:50:10 +01006012 if (common_idx == 0) {
6013 MBEDTLS_SSL_DEBUG_MSG(3, ("no signature algorithm in common"));
6014 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE,
6015 MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE);
6016 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Gabor Mezei078e8032022-04-27 21:17:56 +02006017 }
6018
Gabor Mezei15b95a62022-05-09 16:37:58 +02006019 ssl->handshake->received_sig_algs[common_idx] = MBEDTLS_TLS_SIG_NONE;
Gilles Peskine449bd832023-01-11 14:50:10 +01006020 return 0;
Gabor Mezei078e8032022-04-27 21:17:56 +02006021}
6022
Ronald Crone68ab4f2022-10-05 12:46:29 +02006023#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
Gabor Mezei078e8032022-04-27 21:17:56 +02006024
Jerry Yudc7bd172022-02-17 13:44:15 +08006025#if defined(MBEDTLS_SSL_PROTO_TLS1_2)
6026
6027#if defined(MBEDTLS_USE_PSA_CRYPTO)
6028
Gilles Peskine449bd832023-01-11 14:50:10 +01006029static psa_status_t setup_psa_key_derivation(psa_key_derivation_operation_t *derivation,
6030 mbedtls_svc_key_id_t key,
6031 psa_algorithm_t alg,
6032 const unsigned char *raw_psk, size_t raw_psk_length,
6033 const unsigned char *seed, size_t seed_length,
6034 const unsigned char *label, size_t label_length,
6035 const unsigned char *other_secret,
6036 size_t other_secret_length,
6037 size_t capacity)
Jerry Yudc7bd172022-02-17 13:44:15 +08006038{
6039 psa_status_t status;
6040
Gilles Peskine449bd832023-01-11 14:50:10 +01006041 status = psa_key_derivation_setup(derivation, alg);
6042 if (status != PSA_SUCCESS) {
6043 return status;
6044 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006045
Gilles Peskine449bd832023-01-11 14:50:10 +01006046 if (PSA_ALG_IS_TLS12_PRF(alg) || PSA_ALG_IS_TLS12_PSK_TO_MS(alg)) {
6047 status = psa_key_derivation_input_bytes(derivation,
6048 PSA_KEY_DERIVATION_INPUT_SEED,
6049 seed, seed_length);
6050 if (status != PSA_SUCCESS) {
6051 return status;
Przemek Stekiel1f027032022-04-05 17:12:11 +02006052 }
6053
Gilles Peskine449bd832023-01-11 14:50:10 +01006054 if (other_secret != NULL) {
6055 status = psa_key_derivation_input_bytes(derivation,
6056 PSA_KEY_DERIVATION_INPUT_OTHER_SECRET,
6057 other_secret, other_secret_length);
6058 if (status != PSA_SUCCESS) {
6059 return status;
6060 }
6061 }
6062
6063 if (mbedtls_svc_key_id_is_null(key)) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006064 status = psa_key_derivation_input_bytes(
6065 derivation, PSA_KEY_DERIVATION_INPUT_SECRET,
Gilles Peskine449bd832023-01-11 14:50:10 +01006066 raw_psk, raw_psk_length);
6067 } else {
Jerry Yudc7bd172022-02-17 13:44:15 +08006068 status = psa_key_derivation_input_key(
Gilles Peskine449bd832023-01-11 14:50:10 +01006069 derivation, PSA_KEY_DERIVATION_INPUT_SECRET, key);
Jerry Yudc7bd172022-02-17 13:44:15 +08006070 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006071 if (status != PSA_SUCCESS) {
6072 return status;
6073 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006074
Gilles Peskine449bd832023-01-11 14:50:10 +01006075 status = psa_key_derivation_input_bytes(derivation,
6076 PSA_KEY_DERIVATION_INPUT_LABEL,
6077 label, label_length);
6078 if (status != PSA_SUCCESS) {
6079 return status;
6080 }
6081 } else {
6082 return PSA_ERROR_NOT_SUPPORTED;
Jerry Yudc7bd172022-02-17 13:44:15 +08006083 }
6084
Gilles Peskine449bd832023-01-11 14:50:10 +01006085 status = psa_key_derivation_set_capacity(derivation, capacity);
6086 if (status != PSA_SUCCESS) {
6087 return status;
6088 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006089
Gilles Peskine449bd832023-01-11 14:50:10 +01006090 return PSA_SUCCESS;
Jerry Yudc7bd172022-02-17 13:44:15 +08006091}
6092
Andrzej Kurek57d10632022-10-24 10:32:01 -04006093#if defined(PSA_WANT_ALG_SHA_384) || \
6094 defined(PSA_WANT_ALG_SHA_256)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006095MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006096static int tls_prf_generic(mbedtls_md_type_t md_type,
6097 const unsigned char *secret, size_t slen,
6098 const char *label,
6099 const unsigned char *random, size_t rlen,
6100 unsigned char *dstbuf, size_t dlen)
Jerry Yudc7bd172022-02-17 13:44:15 +08006101{
6102 psa_status_t status;
6103 psa_algorithm_t alg;
6104 mbedtls_svc_key_id_t master_key = MBEDTLS_SVC_KEY_ID_INIT;
6105 psa_key_derivation_operation_t derivation =
6106 PSA_KEY_DERIVATION_OPERATION_INIT;
6107
Gilles Peskine449bd832023-01-11 14:50:10 +01006108 if (md_type == MBEDTLS_MD_SHA384) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006109 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_384);
Gilles Peskine449bd832023-01-11 14:50:10 +01006110 } else {
Jerry Yudc7bd172022-02-17 13:44:15 +08006111 alg = PSA_ALG_TLS12_PRF(PSA_ALG_SHA_256);
Gilles Peskine449bd832023-01-11 14:50:10 +01006112 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006113
6114 /* Normally a "secret" should be long enough to be impossible to
6115 * find by brute force, and in particular should not be empty. But
6116 * this PRF is also used to derive an IV, in particular in EAP-TLS,
6117 * and for this use case it makes sense to have a 0-length "secret".
6118 * Since the key API doesn't allow importing a key of length 0,
6119 * keep master_key=0, which setup_psa_key_derivation() understands
6120 * to mean a 0-length "secret" input. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006121 if (slen != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006122 psa_key_attributes_t key_attributes = psa_key_attributes_init();
Gilles Peskine449bd832023-01-11 14:50:10 +01006123 psa_set_key_usage_flags(&key_attributes, PSA_KEY_USAGE_DERIVE);
6124 psa_set_key_algorithm(&key_attributes, alg);
6125 psa_set_key_type(&key_attributes, PSA_KEY_TYPE_DERIVE);
Jerry Yudc7bd172022-02-17 13:44:15 +08006126
Gilles Peskine449bd832023-01-11 14:50:10 +01006127 status = psa_import_key(&key_attributes, secret, slen, &master_key);
6128 if (status != PSA_SUCCESS) {
6129 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
6130 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006131 }
6132
Gilles Peskine449bd832023-01-11 14:50:10 +01006133 status = setup_psa_key_derivation(&derivation,
6134 master_key, alg,
6135 NULL, 0,
6136 random, rlen,
6137 (unsigned char const *) label,
6138 (size_t) strlen(label),
6139 NULL, 0,
6140 dlen);
6141 if (status != PSA_SUCCESS) {
6142 psa_key_derivation_abort(&derivation);
6143 psa_destroy_key(master_key);
6144 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yudc7bd172022-02-17 13:44:15 +08006145 }
6146
Gilles Peskine449bd832023-01-11 14:50:10 +01006147 status = psa_key_derivation_output_bytes(&derivation, dstbuf, dlen);
6148 if (status != PSA_SUCCESS) {
6149 psa_key_derivation_abort(&derivation);
6150 psa_destroy_key(master_key);
6151 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yudc7bd172022-02-17 13:44:15 +08006152 }
6153
Gilles Peskine449bd832023-01-11 14:50:10 +01006154 status = psa_key_derivation_abort(&derivation);
6155 if (status != PSA_SUCCESS) {
6156 psa_destroy_key(master_key);
6157 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yudc7bd172022-02-17 13:44:15 +08006158 }
6159
Gilles Peskine449bd832023-01-11 14:50:10 +01006160 if (!mbedtls_svc_key_id_is_null(master_key)) {
6161 status = psa_destroy_key(master_key);
6162 }
6163 if (status != PSA_SUCCESS) {
6164 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
6165 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006166
Gilles Peskine449bd832023-01-11 14:50:10 +01006167 return 0;
Jerry Yudc7bd172022-02-17 13:44:15 +08006168}
Andrzej Kurek57d10632022-10-24 10:32:01 -04006169#endif /* PSA_WANT_ALG_SHA_256 || PSA_WANT_ALG_SHA_384 */
Jerry Yudc7bd172022-02-17 13:44:15 +08006170#else /* MBEDTLS_USE_PSA_CRYPTO */
6171
Andrzej Kurek57d10632022-10-24 10:32:01 -04006172#if defined(MBEDTLS_MD_C) && \
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006173 (defined(MBEDTLS_MD_CAN_SHA256) || \
6174 defined(MBEDTLS_MD_CAN_SHA384))
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006175MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006176static int tls_prf_generic(mbedtls_md_type_t md_type,
6177 const unsigned char *secret, size_t slen,
6178 const char *label,
6179 const unsigned char *random, size_t rlen,
6180 unsigned char *dstbuf, size_t dlen)
Jerry Yudc7bd172022-02-17 13:44:15 +08006181{
6182 size_t nb;
6183 size_t i, j, k, md_len;
6184 unsigned char *tmp;
6185 size_t tmp_len = 0;
6186 unsigned char h_i[MBEDTLS_MD_MAX_SIZE];
6187 const mbedtls_md_info_t *md_info;
6188 mbedtls_md_context_t md_ctx;
6189 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6190
Gilles Peskine449bd832023-01-11 14:50:10 +01006191 mbedtls_md_init(&md_ctx);
Jerry Yudc7bd172022-02-17 13:44:15 +08006192
Gilles Peskine449bd832023-01-11 14:50:10 +01006193 if ((md_info = mbedtls_md_info_from_type(md_type)) == NULL) {
6194 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
6195 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006196
Gilles Peskine449bd832023-01-11 14:50:10 +01006197 md_len = mbedtls_md_get_size(md_info);
Jerry Yudc7bd172022-02-17 13:44:15 +08006198
Gilles Peskine449bd832023-01-11 14:50:10 +01006199 tmp_len = md_len + strlen(label) + rlen;
6200 tmp = mbedtls_calloc(1, tmp_len);
6201 if (tmp == NULL) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006202 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
6203 goto exit;
6204 }
6205
Gilles Peskine449bd832023-01-11 14:50:10 +01006206 nb = strlen(label);
6207 memcpy(tmp + md_len, label, nb);
6208 memcpy(tmp + md_len + nb, random, rlen);
Jerry Yudc7bd172022-02-17 13:44:15 +08006209 nb += rlen;
6210
6211 /*
6212 * Compute P_<hash>(secret, label + random)[0..dlen]
6213 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006214 if ((ret = mbedtls_md_setup(&md_ctx, md_info, 1)) != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006215 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006216 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006217
Gilles Peskine449bd832023-01-11 14:50:10 +01006218 ret = mbedtls_md_hmac_starts(&md_ctx, secret, slen);
6219 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006220 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006221 }
6222 ret = mbedtls_md_hmac_update(&md_ctx, tmp + md_len, nb);
6223 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006224 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006225 }
6226 ret = mbedtls_md_hmac_finish(&md_ctx, tmp);
6227 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006228 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006229 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006230
Gilles Peskine449bd832023-01-11 14:50:10 +01006231 for (i = 0; i < dlen; i += md_len) {
6232 ret = mbedtls_md_hmac_reset(&md_ctx);
6233 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006234 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006235 }
6236 ret = mbedtls_md_hmac_update(&md_ctx, tmp, md_len + nb);
6237 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006238 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006239 }
6240 ret = mbedtls_md_hmac_finish(&md_ctx, h_i);
6241 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006242 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006243 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006244
Gilles Peskine449bd832023-01-11 14:50:10 +01006245 ret = mbedtls_md_hmac_reset(&md_ctx);
6246 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006247 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006248 }
6249 ret = mbedtls_md_hmac_update(&md_ctx, tmp, md_len);
6250 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006251 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006252 }
6253 ret = mbedtls_md_hmac_finish(&md_ctx, tmp);
6254 if (ret != 0) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006255 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01006256 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006257
Gilles Peskine449bd832023-01-11 14:50:10 +01006258 k = (i + md_len > dlen) ? dlen % md_len : md_len;
Jerry Yudc7bd172022-02-17 13:44:15 +08006259
Gilles Peskine449bd832023-01-11 14:50:10 +01006260 for (j = 0; j < k; j++) {
Jerry Yudc7bd172022-02-17 13:44:15 +08006261 dstbuf[i + j] = h_i[j];
Gilles Peskine449bd832023-01-11 14:50:10 +01006262 }
Jerry Yudc7bd172022-02-17 13:44:15 +08006263 }
6264
6265exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01006266 mbedtls_md_free(&md_ctx);
Jerry Yudc7bd172022-02-17 13:44:15 +08006267
Gilles Peskine449bd832023-01-11 14:50:10 +01006268 if (tmp != NULL) {
6269 mbedtls_platform_zeroize(tmp, tmp_len);
6270 }
Dave Rodgman29b9b2b2022-11-01 16:08:14 +00006271
Gilles Peskine449bd832023-01-11 14:50:10 +01006272 mbedtls_platform_zeroize(h_i, sizeof(h_i));
Jerry Yudc7bd172022-02-17 13:44:15 +08006273
Gilles Peskine449bd832023-01-11 14:50:10 +01006274 mbedtls_free(tmp);
Jerry Yudc7bd172022-02-17 13:44:15 +08006275
Gilles Peskine449bd832023-01-11 14:50:10 +01006276 return ret;
Jerry Yudc7bd172022-02-17 13:44:15 +08006277}
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006278#endif /* MBEDTLS_MD_C && ( MBEDTLS_MD_CAN_SHA256 || MBEDTLS_MD_CAN_SHA384 ) */
Jerry Yudc7bd172022-02-17 13:44:15 +08006279#endif /* MBEDTLS_USE_PSA_CRYPTO */
6280
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006281#if defined(MBEDTLS_MD_CAN_SHA256)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006282MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006283static int tls_prf_sha256(const unsigned char *secret, size_t slen,
6284 const char *label,
6285 const unsigned char *random, size_t rlen,
6286 unsigned char *dstbuf, size_t dlen)
Jerry Yudc7bd172022-02-17 13:44:15 +08006287{
Gilles Peskine449bd832023-01-11 14:50:10 +01006288 return tls_prf_generic(MBEDTLS_MD_SHA256, secret, slen,
6289 label, random, rlen, dstbuf, dlen);
Jerry Yudc7bd172022-02-17 13:44:15 +08006290}
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006291#endif /* MBEDTLS_MD_CAN_SHA256*/
Jerry Yudc7bd172022-02-17 13:44:15 +08006292
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006293#if defined(MBEDTLS_MD_CAN_SHA384)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006294MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006295static int tls_prf_sha384(const unsigned char *secret, size_t slen,
6296 const char *label,
6297 const unsigned char *random, size_t rlen,
6298 unsigned char *dstbuf, size_t dlen)
Jerry Yudc7bd172022-02-17 13:44:15 +08006299{
Gilles Peskine449bd832023-01-11 14:50:10 +01006300 return tls_prf_generic(MBEDTLS_MD_SHA384, secret, slen,
6301 label, random, rlen, dstbuf, dlen);
Jerry Yudc7bd172022-02-17 13:44:15 +08006302}
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006303#endif /* MBEDTLS_MD_CAN_SHA384*/
Jerry Yudc7bd172022-02-17 13:44:15 +08006304
Jerry Yuf009d862022-02-17 14:01:37 +08006305/*
6306 * Set appropriate PRF function and other SSL / TLS1.2 functions
6307 *
6308 * Inputs:
Jerry Yuf009d862022-02-17 14:01:37 +08006309 * - hash associated with the ciphersuite (only used by TLS 1.2)
6310 *
6311 * Outputs:
6312 * - the tls_prf, calc_verify and calc_finished members of handshake structure
6313 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006314MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006315static int ssl_set_handshake_prfs(mbedtls_ssl_handshake_params *handshake,
6316 mbedtls_md_type_t hash)
Jerry Yuf009d862022-02-17 14:01:37 +08006317{
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006318#if defined(MBEDTLS_MD_CAN_SHA384)
Gilles Peskine449bd832023-01-11 14:50:10 +01006319 if (hash == MBEDTLS_MD_SHA384) {
Jerry Yuf009d862022-02-17 14:01:37 +08006320 handshake->tls_prf = tls_prf_sha384;
6321 handshake->calc_verify = ssl_calc_verify_tls_sha384;
6322 handshake->calc_finished = ssl_calc_finished_tls_sha384;
Gilles Peskine449bd832023-01-11 14:50:10 +01006323 } else
Jerry Yuf009d862022-02-17 14:01:37 +08006324#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006325#if defined(MBEDTLS_MD_CAN_SHA256)
Jerry Yuf009d862022-02-17 14:01:37 +08006326 {
Ronald Cron81591aa2022-03-07 09:05:51 +01006327 (void) hash;
Jerry Yuf009d862022-02-17 14:01:37 +08006328 handshake->tls_prf = tls_prf_sha256;
6329 handshake->calc_verify = ssl_calc_verify_tls_sha256;
6330 handshake->calc_finished = ssl_calc_finished_tls_sha256;
6331 }
Ronald Cron81591aa2022-03-07 09:05:51 +01006332#else
Jerry Yuf009d862022-02-17 14:01:37 +08006333 {
Jerry Yuf009d862022-02-17 14:01:37 +08006334 (void) handshake;
Ronald Cron81591aa2022-03-07 09:05:51 +01006335 (void) hash;
Gilles Peskine449bd832023-01-11 14:50:10 +01006336 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yuf009d862022-02-17 14:01:37 +08006337 }
Ronald Cron81591aa2022-03-07 09:05:51 +01006338#endif
Jerry Yuf009d862022-02-17 14:01:37 +08006339
Gilles Peskine449bd832023-01-11 14:50:10 +01006340 return 0;
Jerry Yuf009d862022-02-17 14:01:37 +08006341}
Jerry Yud6ab2352022-02-17 14:03:43 +08006342
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006343/*
6344 * Compute master secret if needed
6345 *
6346 * Parameters:
6347 * [in/out] handshake
6348 * [in] resume, premaster, extended_ms, calc_verify, tls_prf
6349 * (PSA-PSK) ciphersuite_info, psk_opaque
6350 * [out] premaster (cleared)
6351 * [out] master
6352 * [in] ssl: optionally used for debugging, EMS and PSA-PSK
6353 * debug: conf->f_dbg, conf->p_dbg
6354 * EMS: passed to calc_verify (debug + session_negotiate)
Ronald Crona25cf582022-03-07 11:10:36 +01006355 * PSA-PSA: conf
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006356 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006357MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006358static int ssl_compute_master(mbedtls_ssl_handshake_params *handshake,
6359 unsigned char *master,
6360 const mbedtls_ssl_context *ssl)
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006361{
6362 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6363
6364 /* cf. RFC 5246, Section 8.1:
6365 * "The master secret is always exactly 48 bytes in length." */
6366 size_t const master_secret_len = 48;
6367
6368#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
6369 unsigned char session_hash[48];
6370#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
6371
6372 /* The label for the KDF used for key expansion.
6373 * This is either "master secret" or "extended master secret"
6374 * depending on whether the Extended Master Secret extension
6375 * is used. */
6376 char const *lbl = "master secret";
6377
Przemek Stekielae4ed302022-04-05 17:15:55 +02006378 /* The seed for the KDF used for key expansion.
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006379 * - If the Extended Master Secret extension is not used,
6380 * this is ClientHello.Random + ServerHello.Random
6381 * (see Sect. 8.1 in RFC 5246).
6382 * - If the Extended Master Secret extension is used,
6383 * this is the transcript of the handshake so far.
6384 * (see Sect. 4 in RFC 7627). */
Przemek Stekielae4ed302022-04-05 17:15:55 +02006385 unsigned char const *seed = handshake->randbytes;
6386 size_t seed_len = 64;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006387
6388#if !defined(MBEDTLS_DEBUG_C) && \
6389 !defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET) && \
6390 !(defined(MBEDTLS_USE_PSA_CRYPTO) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01006391 defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED))
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006392 ssl = NULL; /* make sure we don't use it except for those cases */
6393 (void) ssl;
6394#endif
6395
Gilles Peskine449bd832023-01-11 14:50:10 +01006396 if (handshake->resume != 0) {
6397 MBEDTLS_SSL_DEBUG_MSG(3, ("no premaster (session resumed)"));
6398 return 0;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006399 }
6400
6401#if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
Gilles Peskine449bd832023-01-11 14:50:10 +01006402 if (handshake->extended_ms == MBEDTLS_SSL_EXTENDED_MS_ENABLED) {
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006403 lbl = "extended master secret";
Przemek Stekielae4ed302022-04-05 17:15:55 +02006404 seed = session_hash;
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01006405 ret = handshake->calc_verify(ssl, session_hash, &seed_len);
6406 if (ret != 0) {
6407 MBEDTLS_SSL_DEBUG_RET(1, "calc_verify", ret);
6408 }
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006409
Gilles Peskine449bd832023-01-11 14:50:10 +01006410 MBEDTLS_SSL_DEBUG_BUF(3, "session hash for extended master secret",
6411 session_hash, seed_len);
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006412 }
Przemek Stekiel169bf0b2022-04-29 07:53:29 +02006413#endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006414
Przemek Stekiel99114f32022-04-22 11:20:09 +02006415#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Przemek Stekiel8a4b7fd2022-04-28 09:22:22 +02006416 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006417 if (mbedtls_ssl_ciphersuite_uses_psk(handshake->ciphersuite_info) == 1) {
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006418 /* Perform PSK-to-MS expansion in a single step. */
6419 psa_status_t status;
6420 psa_algorithm_t alg;
6421 mbedtls_svc_key_id_t psk;
6422 psa_key_derivation_operation_t derivation =
6423 PSA_KEY_DERIVATION_OPERATION_INIT;
6424 mbedtls_md_type_t hash_alg = handshake->ciphersuite_info->mac;
6425
Gilles Peskine449bd832023-01-11 14:50:10 +01006426 MBEDTLS_SSL_DEBUG_MSG(2, ("perform PSA-based PSK-to-MS expansion"));
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006427
Gilles Peskine449bd832023-01-11 14:50:10 +01006428 psk = mbedtls_ssl_get_opaque_psk(ssl);
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006429
Gilles Peskine449bd832023-01-11 14:50:10 +01006430 if (hash_alg == MBEDTLS_MD_SHA384) {
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006431 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
Gilles Peskine449bd832023-01-11 14:50:10 +01006432 } else {
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006433 alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
Gilles Peskine449bd832023-01-11 14:50:10 +01006434 }
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006435
Przemek Stekiel51a1f362022-04-13 08:57:06 +02006436 size_t other_secret_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006437 unsigned char *other_secret = NULL;
Przemek Stekielc2033402022-04-05 17:19:41 +02006438
Gilles Peskine449bd832023-01-11 14:50:10 +01006439 switch (handshake->ciphersuite_info->key_exchange) {
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006440 /* Provide other secret.
Przemek Stekiel51a1f362022-04-13 08:57:06 +02006441 * Other secret is stored in premaster, where first 2 bytes hold the
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006442 * length of the other key.
Przemek Stekielc2033402022-04-05 17:19:41 +02006443 */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006444 case MBEDTLS_KEY_EXCHANGE_RSA_PSK:
Przemek Stekiel8abcee92022-04-28 09:16:28 +02006445 /* For RSA-PSK other key length is always 48 bytes. */
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006446 other_secret_len = 48;
6447 other_secret = handshake->premaster + 2;
6448 break;
6449 case MBEDTLS_KEY_EXCHANGE_ECDHE_PSK:
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006450 case MBEDTLS_KEY_EXCHANGE_DHE_PSK:
Przemek Stekiel19b80f82022-04-14 08:29:31 +02006451 other_secret_len = MBEDTLS_GET_UINT16_BE(handshake->premaster, 0);
6452 other_secret = handshake->premaster + 2;
6453 break;
6454 default:
6455 break;
Przemek Stekielc2033402022-04-05 17:19:41 +02006456 }
6457
Gilles Peskine449bd832023-01-11 14:50:10 +01006458 status = setup_psa_key_derivation(&derivation, psk, alg,
6459 ssl->conf->psk, ssl->conf->psk_len,
6460 seed, seed_len,
6461 (unsigned char const *) lbl,
6462 (size_t) strlen(lbl),
6463 other_secret, other_secret_len,
6464 master_secret_len);
6465 if (status != PSA_SUCCESS) {
6466 psa_key_derivation_abort(&derivation);
6467 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006468 }
6469
Gilles Peskine449bd832023-01-11 14:50:10 +01006470 status = psa_key_derivation_output_bytes(&derivation,
6471 master,
6472 master_secret_len);
6473 if (status != PSA_SUCCESS) {
6474 psa_key_derivation_abort(&derivation);
6475 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006476 }
6477
Gilles Peskine449bd832023-01-11 14:50:10 +01006478 status = psa_key_derivation_abort(&derivation);
6479 if (status != PSA_SUCCESS) {
6480 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
6481 }
6482 } else
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006483#endif
6484 {
Neil Armstrongca7d5062022-05-31 14:43:23 +02006485#if defined(MBEDTLS_USE_PSA_CRYPTO) && \
Gilles Peskine449bd832023-01-11 14:50:10 +01006486 defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
6487 if (handshake->ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE) {
Neil Armstrongca7d5062022-05-31 14:43:23 +02006488 psa_status_t status;
6489 psa_algorithm_t alg = PSA_ALG_TLS12_ECJPAKE_TO_PMS;
6490 psa_key_derivation_operation_t derivation =
6491 PSA_KEY_DERIVATION_OPERATION_INIT;
6492
Gilles Peskine449bd832023-01-11 14:50:10 +01006493 MBEDTLS_SSL_DEBUG_MSG(2, ("perform PSA-based PMS KDF for ECJPAKE"));
Neil Armstrongca7d5062022-05-31 14:43:23 +02006494
6495 handshake->pmslen = PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE;
6496
Gilles Peskine449bd832023-01-11 14:50:10 +01006497 status = psa_key_derivation_setup(&derivation, alg);
6498 if (status != PSA_SUCCESS) {
6499 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Neil Armstrongca7d5062022-05-31 14:43:23 +02006500 }
6501
Gilles Peskine449bd832023-01-11 14:50:10 +01006502 status = psa_key_derivation_set_capacity(&derivation,
6503 PSA_TLS12_ECJPAKE_TO_PMS_DATA_SIZE);
6504 if (status != PSA_SUCCESS) {
6505 psa_key_derivation_abort(&derivation);
6506 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Neil Armstrongca7d5062022-05-31 14:43:23 +02006507 }
6508
Gilles Peskine449bd832023-01-11 14:50:10 +01006509 status = psa_pake_get_implicit_key(&handshake->psa_pake_ctx,
6510 &derivation);
6511 if (status != PSA_SUCCESS) {
6512 psa_key_derivation_abort(&derivation);
6513 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Neil Armstrongca7d5062022-05-31 14:43:23 +02006514 }
6515
Gilles Peskine449bd832023-01-11 14:50:10 +01006516 status = psa_key_derivation_output_bytes(&derivation,
6517 handshake->premaster,
6518 handshake->pmslen);
6519 if (status != PSA_SUCCESS) {
6520 psa_key_derivation_abort(&derivation);
6521 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
6522 }
6523
6524 status = psa_key_derivation_abort(&derivation);
6525 if (status != PSA_SUCCESS) {
6526 return MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
Neil Armstrongca7d5062022-05-31 14:43:23 +02006527 }
6528 }
6529#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01006530 ret = handshake->tls_prf(handshake->premaster, handshake->pmslen,
6531 lbl, seed, seed_len,
6532 master,
6533 master_secret_len);
6534 if (ret != 0) {
6535 MBEDTLS_SSL_DEBUG_RET(1, "prf", ret);
6536 return ret;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006537 }
6538
Gilles Peskine449bd832023-01-11 14:50:10 +01006539 MBEDTLS_SSL_DEBUG_BUF(3, "premaster secret",
6540 handshake->premaster,
6541 handshake->pmslen);
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006542
Gilles Peskine449bd832023-01-11 14:50:10 +01006543 mbedtls_platform_zeroize(handshake->premaster,
6544 sizeof(handshake->premaster));
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006545 }
6546
Gilles Peskine449bd832023-01-11 14:50:10 +01006547 return 0;
Jerry Yu2a7b5ac2022-02-17 14:07:00 +08006548}
6549
Gilles Peskine449bd832023-01-11 14:50:10 +01006550int mbedtls_ssl_derive_keys(mbedtls_ssl_context *ssl)
Jerry Yud62f87e2022-02-17 14:09:02 +08006551{
6552 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6553 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
6554 ssl->handshake->ciphersuite_info;
6555
Gilles Peskine449bd832023-01-11 14:50:10 +01006556 MBEDTLS_SSL_DEBUG_MSG(2, ("=> derive keys"));
Jerry Yud62f87e2022-02-17 14:09:02 +08006557
6558 /* Set PRF, calc_verify and calc_finished function pointers */
Gilles Peskine449bd832023-01-11 14:50:10 +01006559 ret = ssl_set_handshake_prfs(ssl->handshake,
6560 ciphersuite_info->mac);
6561 if (ret != 0) {
6562 MBEDTLS_SSL_DEBUG_RET(1, "ssl_set_handshake_prfs", ret);
6563 return ret;
Jerry Yud62f87e2022-02-17 14:09:02 +08006564 }
6565
6566 /* Compute master secret if needed */
Gilles Peskine449bd832023-01-11 14:50:10 +01006567 ret = ssl_compute_master(ssl->handshake,
6568 ssl->session_negotiate->master,
6569 ssl);
6570 if (ret != 0) {
6571 MBEDTLS_SSL_DEBUG_RET(1, "ssl_compute_master", ret);
6572 return ret;
Jerry Yud62f87e2022-02-17 14:09:02 +08006573 }
6574
6575 /* Swap the client and server random values:
6576 * - MS derivation wanted client+server (RFC 5246 8.1)
6577 * - key derivation wants server+client (RFC 5246 6.3) */
6578 {
6579 unsigned char tmp[64];
Gilles Peskine449bd832023-01-11 14:50:10 +01006580 memcpy(tmp, ssl->handshake->randbytes, 64);
6581 memcpy(ssl->handshake->randbytes, tmp + 32, 32);
6582 memcpy(ssl->handshake->randbytes + 32, tmp, 32);
6583 mbedtls_platform_zeroize(tmp, sizeof(tmp));
Jerry Yud62f87e2022-02-17 14:09:02 +08006584 }
6585
6586 /* Populate transform structure */
Gilles Peskine449bd832023-01-11 14:50:10 +01006587 ret = ssl_tls12_populate_transform(ssl->transform_negotiate,
6588 ssl->session_negotiate->ciphersuite,
6589 ssl->session_negotiate->master,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02006590#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01006591 ssl->session_negotiate->encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02006592#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01006593 ssl->handshake->tls_prf,
6594 ssl->handshake->randbytes,
6595 ssl->tls_version,
6596 ssl->conf->endpoint,
6597 ssl);
6598 if (ret != 0) {
6599 MBEDTLS_SSL_DEBUG_RET(1, "ssl_tls12_populate_transform", ret);
6600 return ret;
Jerry Yud62f87e2022-02-17 14:09:02 +08006601 }
6602
6603 /* We no longer need Server/ClientHello.random values */
Gilles Peskine449bd832023-01-11 14:50:10 +01006604 mbedtls_platform_zeroize(ssl->handshake->randbytes,
6605 sizeof(ssl->handshake->randbytes));
Jerry Yud62f87e2022-02-17 14:09:02 +08006606
Gilles Peskine449bd832023-01-11 14:50:10 +01006607 MBEDTLS_SSL_DEBUG_MSG(2, ("<= derive keys"));
Jerry Yud62f87e2022-02-17 14:09:02 +08006608
Gilles Peskine449bd832023-01-11 14:50:10 +01006609 return 0;
Jerry Yud62f87e2022-02-17 14:09:02 +08006610}
Jerry Yu8392e0d2022-02-17 14:10:24 +08006611
Gilles Peskine449bd832023-01-11 14:50:10 +01006612int mbedtls_ssl_set_calc_verify_md(mbedtls_ssl_context *ssl, int md)
Ronald Cron4dcbca92022-03-07 10:21:40 +01006613{
Gilles Peskine449bd832023-01-11 14:50:10 +01006614 switch (md) {
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006615#if defined(MBEDTLS_MD_CAN_SHA384)
Ronald Cron4dcbca92022-03-07 10:21:40 +01006616 case MBEDTLS_SSL_HASH_SHA384:
6617 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha384;
6618 break;
6619#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006620#if defined(MBEDTLS_MD_CAN_SHA256)
Ronald Cron4dcbca92022-03-07 10:21:40 +01006621 case MBEDTLS_SSL_HASH_SHA256:
6622 ssl->handshake->calc_verify = ssl_calc_verify_tls_sha256;
6623 break;
6624#endif
6625 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01006626 return -1;
Ronald Cron4dcbca92022-03-07 10:21:40 +01006627 }
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006628#if !defined(MBEDTLS_MD_CAN_SHA384) && \
6629 !defined(MBEDTLS_MD_CAN_SHA256)
Andrzej Kurekeabeb302022-10-17 07:52:51 -04006630 (void) ssl;
6631#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01006632 return 0;
Ronald Cron4dcbca92022-03-07 10:21:40 +01006633}
6634
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006635#if defined(MBEDTLS_MD_CAN_SHA256)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01006636int ssl_calc_verify_tls_sha256(const mbedtls_ssl_context *ssl,
6637 unsigned char *hash,
6638 size_t *hlen)
Jerry Yu8392e0d2022-02-17 14:10:24 +08006639{
6640#if defined(MBEDTLS_USE_PSA_CRYPTO)
6641 size_t hash_size;
6642 psa_status_t status;
6643 psa_hash_operation_t sha256_psa = psa_hash_operation_init();
6644
Gilles Peskine449bd832023-01-11 14:50:10 +01006645 MBEDTLS_SSL_DEBUG_MSG(2, ("=> PSA calc verify sha256"));
6646 status = psa_hash_clone(&ssl->handshake->fin_sha256_psa, &sha256_psa);
6647 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006648 goto exit;
Jerry Yu8392e0d2022-02-17 14:10:24 +08006649 }
6650
Gilles Peskine449bd832023-01-11 14:50:10 +01006651 status = psa_hash_finish(&sha256_psa, hash, 32, &hash_size);
6652 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006653 goto exit;
Jerry Yu8392e0d2022-02-17 14:10:24 +08006654 }
6655
6656 *hlen = 32;
Gilles Peskine449bd832023-01-11 14:50:10 +01006657 MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated verify result", hash, *hlen);
6658 MBEDTLS_SSL_DEBUG_MSG(2, ("<= PSA calc verify"));
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006659
6660exit:
6661 psa_hash_abort(&sha256_psa);
Andrzej Kurekdaf5b562023-03-03 05:52:28 -05006662 return PSA_TO_MD_ERR(status);
Jerry Yu8392e0d2022-02-17 14:10:24 +08006663#else
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006664 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01006665 mbedtls_md_context_t sha256;
Jerry Yu8392e0d2022-02-17 14:10:24 +08006666
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01006667 mbedtls_md_init(&sha256);
Jerry Yu8392e0d2022-02-17 14:10:24 +08006668
Gilles Peskine449bd832023-01-11 14:50:10 +01006669 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc verify sha256"));
Jerry Yu8392e0d2022-02-17 14:10:24 +08006670
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01006671 ret = mbedtls_md_setup(&sha256, mbedtls_md_info_from_type(MBEDTLS_MD_SHA256), 0);
6672 if (ret != 0) {
6673 goto exit;
6674 }
6675 ret = mbedtls_md_clone(&sha256, &ssl->handshake->fin_sha256);
6676 if (ret != 0) {
6677 goto exit;
6678 }
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006679
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01006680 ret = mbedtls_md_finish(&sha256, hash);
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006681 if (ret != 0) {
6682 goto exit;
6683 }
Jerry Yu8392e0d2022-02-17 14:10:24 +08006684
6685 *hlen = 32;
6686
Gilles Peskine449bd832023-01-11 14:50:10 +01006687 MBEDTLS_SSL_DEBUG_BUF(3, "calculated verify result", hash, *hlen);
6688 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc verify"));
Jerry Yu8392e0d2022-02-17 14:10:24 +08006689
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006690exit:
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01006691 mbedtls_md_free(&sha256);
Manuel Pégourié-Gonnard8e176f72023-02-09 10:33:54 +01006692 return ret;
Jerry Yu8392e0d2022-02-17 14:10:24 +08006693#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu8392e0d2022-02-17 14:10:24 +08006694}
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006695#endif /* MBEDTLS_MD_CAN_SHA256 */
Jerry Yu8392e0d2022-02-17 14:10:24 +08006696
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006697#if defined(MBEDTLS_MD_CAN_SHA384)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01006698int ssl_calc_verify_tls_sha384(const mbedtls_ssl_context *ssl,
6699 unsigned char *hash,
6700 size_t *hlen)
Jerry Yuc1cb3842022-02-17 14:13:48 +08006701{
6702#if defined(MBEDTLS_USE_PSA_CRYPTO)
6703 size_t hash_size;
6704 psa_status_t status;
6705 psa_hash_operation_t sha384_psa = psa_hash_operation_init();
6706
Gilles Peskine449bd832023-01-11 14:50:10 +01006707 MBEDTLS_SSL_DEBUG_MSG(2, ("=> PSA calc verify sha384"));
6708 status = psa_hash_clone(&ssl->handshake->fin_sha384_psa, &sha384_psa);
6709 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006710 goto exit;
Jerry Yuc1cb3842022-02-17 14:13:48 +08006711 }
6712
Gilles Peskine449bd832023-01-11 14:50:10 +01006713 status = psa_hash_finish(&sha384_psa, hash, 48, &hash_size);
6714 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006715 goto exit;
Jerry Yuc1cb3842022-02-17 14:13:48 +08006716 }
6717
6718 *hlen = 48;
Gilles Peskine449bd832023-01-11 14:50:10 +01006719 MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated verify result", hash, *hlen);
6720 MBEDTLS_SSL_DEBUG_MSG(2, ("<= PSA calc verify"));
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006721
6722exit:
6723 psa_hash_abort(&sha384_psa);
Andrzej Kurekdaf5b562023-03-03 05:52:28 -05006724 return PSA_TO_MD_ERR(status);
Jerry Yuc1cb3842022-02-17 14:13:48 +08006725#else
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006726 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01006727 mbedtls_md_context_t sha384;
Jerry Yuc1cb3842022-02-17 14:13:48 +08006728
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01006729 mbedtls_md_init(&sha384);
Jerry Yuc1cb3842022-02-17 14:13:48 +08006730
Gilles Peskine449bd832023-01-11 14:50:10 +01006731 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc verify sha384"));
Jerry Yuc1cb3842022-02-17 14:13:48 +08006732
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01006733 ret = mbedtls_md_setup(&sha384, mbedtls_md_info_from_type(MBEDTLS_MD_SHA384), 0);
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01006734 if (ret != 0) {
6735 goto exit;
6736 }
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01006737 ret = mbedtls_md_clone(&sha384, &ssl->handshake->fin_sha384);
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01006738 if (ret != 0) {
6739 goto exit;
6740 }
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006741
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01006742 ret = mbedtls_md_finish(&sha384, hash);
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006743 if (ret != 0) {
6744 goto exit;
6745 }
Jerry Yuc1cb3842022-02-17 14:13:48 +08006746
6747 *hlen = 48;
6748
Gilles Peskine449bd832023-01-11 14:50:10 +01006749 MBEDTLS_SSL_DEBUG_BUF(3, "calculated verify result", hash, *hlen);
6750 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc verify"));
Jerry Yuc1cb3842022-02-17 14:13:48 +08006751
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006752exit:
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01006753 mbedtls_md_free(&sha384);
Manuel Pégourié-Gonnardb9b564e2023-02-06 10:06:04 +01006754 return ret;
Jerry Yuc1cb3842022-02-17 14:13:48 +08006755#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yuc1cb3842022-02-17 14:13:48 +08006756}
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01006757#endif /* MBEDTLS_MD_CAN_SHA384 */
Jerry Yuc1cb3842022-02-17 14:13:48 +08006758
Neil Armstrong80f6f322022-05-03 17:56:38 +02006759#if !defined(MBEDTLS_USE_PSA_CRYPTO) && \
6760 defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006761int mbedtls_ssl_psk_derive_premaster(mbedtls_ssl_context *ssl, mbedtls_key_exchange_type_t key_ex)
Jerry Yuce3dca42022-02-17 14:16:37 +08006762{
6763 unsigned char *p = ssl->handshake->premaster;
Gilles Peskine449bd832023-01-11 14:50:10 +01006764 unsigned char *end = p + sizeof(ssl->handshake->premaster);
Jerry Yuce3dca42022-02-17 14:16:37 +08006765 const unsigned char *psk = NULL;
6766 size_t psk_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01006767 int psk_ret = mbedtls_ssl_get_psk(ssl, &psk, &psk_len);
Jerry Yuce3dca42022-02-17 14:16:37 +08006768
Gilles Peskine449bd832023-01-11 14:50:10 +01006769 if (psk_ret == MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED) {
Jerry Yuce3dca42022-02-17 14:16:37 +08006770 /*
6771 * This should never happen because the existence of a PSK is always
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006772 * checked before calling this function.
6773 *
6774 * The exception is opaque DHE-PSK. For DHE-PSK fill premaster with
Przemek Stekiel8abcee92022-04-28 09:16:28 +02006775 * the shared secret without PSK.
Jerry Yuce3dca42022-02-17 14:16:37 +08006776 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006777 if (key_ex != MBEDTLS_KEY_EXCHANGE_DHE_PSK) {
6778 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
6779 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Przemek Stekielb293aaa2022-04-19 12:22:38 +02006780 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006781 }
6782
6783 /*
6784 * PMS = struct {
6785 * opaque other_secret<0..2^16-1>;
6786 * opaque psk<0..2^16-1>;
6787 * };
6788 * with "other_secret" depending on the particular key exchange
6789 */
6790#if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006791 if (key_ex == MBEDTLS_KEY_EXCHANGE_PSK) {
6792 if (end - p < 2) {
6793 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6794 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006795
Gilles Peskine449bd832023-01-11 14:50:10 +01006796 MBEDTLS_PUT_UINT16_BE(psk_len, p, 0);
Jerry Yuce3dca42022-02-17 14:16:37 +08006797 p += 2;
6798
Gilles Peskine449bd832023-01-11 14:50:10 +01006799 if (end < p || (size_t) (end - p) < psk_len) {
6800 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6801 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006802
Gilles Peskine449bd832023-01-11 14:50:10 +01006803 memset(p, 0, psk_len);
Jerry Yuce3dca42022-02-17 14:16:37 +08006804 p += psk_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01006805 } else
Jerry Yuce3dca42022-02-17 14:16:37 +08006806#endif /* MBEDTLS_KEY_EXCHANGE_PSK_ENABLED */
6807#if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006808 if (key_ex == MBEDTLS_KEY_EXCHANGE_RSA_PSK) {
Jerry Yuce3dca42022-02-17 14:16:37 +08006809 /*
6810 * other_secret already set by the ClientKeyExchange message,
6811 * and is 48 bytes long
6812 */
Gilles Peskine449bd832023-01-11 14:50:10 +01006813 if (end - p < 2) {
6814 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6815 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006816
6817 *p++ = 0;
6818 *p++ = 48;
6819 p += 48;
Gilles Peskine449bd832023-01-11 14:50:10 +01006820 } else
Jerry Yuce3dca42022-02-17 14:16:37 +08006821#endif /* MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED */
6822#if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006823 if (key_ex == MBEDTLS_KEY_EXCHANGE_DHE_PSK) {
Jerry Yuce3dca42022-02-17 14:16:37 +08006824 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6825 size_t len;
6826
6827 /* Write length only when we know the actual value */
Gilles Peskine449bd832023-01-11 14:50:10 +01006828 if ((ret = mbedtls_dhm_calc_secret(&ssl->handshake->dhm_ctx,
6829 p + 2, end - (p + 2), &len,
6830 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
6831 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_dhm_calc_secret", ret);
6832 return ret;
Jerry Yuce3dca42022-02-17 14:16:37 +08006833 }
Gilles Peskine449bd832023-01-11 14:50:10 +01006834 MBEDTLS_PUT_UINT16_BE(len, p, 0);
Jerry Yuce3dca42022-02-17 14:16:37 +08006835 p += 2 + len;
6836
Gilles Peskine449bd832023-01-11 14:50:10 +01006837 MBEDTLS_SSL_DEBUG_MPI(3, "DHM: K ", &ssl->handshake->dhm_ctx.K);
6838 } else
Jerry Yuce3dca42022-02-17 14:16:37 +08006839#endif /* MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED */
Neil Armstrong80f6f322022-05-03 17:56:38 +02006840#if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01006841 if (key_ex == MBEDTLS_KEY_EXCHANGE_ECDHE_PSK) {
Jerry Yuce3dca42022-02-17 14:16:37 +08006842 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6843 size_t zlen;
6844
Gilles Peskine449bd832023-01-11 14:50:10 +01006845 if ((ret = mbedtls_ecdh_calc_secret(&ssl->handshake->ecdh_ctx, &zlen,
6846 p + 2, end - (p + 2),
6847 ssl->conf->f_rng, ssl->conf->p_rng)) != 0) {
6848 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ecdh_calc_secret", ret);
6849 return ret;
Jerry Yuce3dca42022-02-17 14:16:37 +08006850 }
6851
Gilles Peskine449bd832023-01-11 14:50:10 +01006852 MBEDTLS_PUT_UINT16_BE(zlen, p, 0);
Jerry Yuce3dca42022-02-17 14:16:37 +08006853 p += 2 + zlen;
6854
Gilles Peskine449bd832023-01-11 14:50:10 +01006855 MBEDTLS_SSL_DEBUG_ECDH(3, &ssl->handshake->ecdh_ctx,
6856 MBEDTLS_DEBUG_ECDH_Z);
6857 } else
Neil Armstrong80f6f322022-05-03 17:56:38 +02006858#endif /* MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED */
Jerry Yuce3dca42022-02-17 14:16:37 +08006859 {
Gilles Peskine449bd832023-01-11 14:50:10 +01006860 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
6861 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yuce3dca42022-02-17 14:16:37 +08006862 }
6863
6864 /* opaque psk<0..2^16-1>; */
Gilles Peskine449bd832023-01-11 14:50:10 +01006865 if (end - p < 2) {
6866 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6867 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006868
Gilles Peskine449bd832023-01-11 14:50:10 +01006869 MBEDTLS_PUT_UINT16_BE(psk_len, p, 0);
Jerry Yuce3dca42022-02-17 14:16:37 +08006870 p += 2;
6871
Gilles Peskine449bd832023-01-11 14:50:10 +01006872 if (end < p || (size_t) (end - p) < psk_len) {
6873 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
6874 }
Jerry Yuce3dca42022-02-17 14:16:37 +08006875
Gilles Peskine449bd832023-01-11 14:50:10 +01006876 memcpy(p, psk, psk_len);
Jerry Yuce3dca42022-02-17 14:16:37 +08006877 p += psk_len;
6878
6879 ssl->handshake->pmslen = p - ssl->handshake->premaster;
6880
Gilles Peskine449bd832023-01-11 14:50:10 +01006881 return 0;
Jerry Yuce3dca42022-02-17 14:16:37 +08006882}
Neil Armstrong80f6f322022-05-03 17:56:38 +02006883#endif /* !MBEDTLS_USE_PSA_CRYPTO && MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
Jerry Yuc2c673d2022-02-17 14:20:39 +08006884
6885#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_RENEGOTIATION)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02006886MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01006887static int ssl_write_hello_request(mbedtls_ssl_context *ssl);
Jerry Yuc2c673d2022-02-17 14:20:39 +08006888
6889#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01006890int mbedtls_ssl_resend_hello_request(mbedtls_ssl_context *ssl)
Jerry Yuc2c673d2022-02-17 14:20:39 +08006891{
6892 /* If renegotiation is not enforced, retransmit until we would reach max
6893 * timeout if we were using the usual handshake doubling scheme */
Gilles Peskine449bd832023-01-11 14:50:10 +01006894 if (ssl->conf->renego_max_records < 0) {
Jerry Yuc2c673d2022-02-17 14:20:39 +08006895 uint32_t ratio = ssl->conf->hs_timeout_max / ssl->conf->hs_timeout_min + 1;
6896 unsigned char doublings = 1;
6897
Gilles Peskine449bd832023-01-11 14:50:10 +01006898 while (ratio != 0) {
Jerry Yuc2c673d2022-02-17 14:20:39 +08006899 ++doublings;
6900 ratio >>= 1;
6901 }
6902
Gilles Peskine449bd832023-01-11 14:50:10 +01006903 if (++ssl->renego_records_seen > doublings) {
6904 MBEDTLS_SSL_DEBUG_MSG(2, ("no longer retransmitting hello request"));
6905 return 0;
Jerry Yuc2c673d2022-02-17 14:20:39 +08006906 }
6907 }
6908
Gilles Peskine449bd832023-01-11 14:50:10 +01006909 return ssl_write_hello_request(ssl);
Jerry Yuc2c673d2022-02-17 14:20:39 +08006910}
6911#endif
6912#endif /* MBEDTLS_SSL_SRV_C && MBEDTLS_SSL_RENEGOTIATION */
Jerry Yud9526692022-02-17 14:23:47 +08006913
Jerry Yud9526692022-02-17 14:23:47 +08006914/*
6915 * Handshake functions
6916 */
6917#if !defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
6918/* No certificate support -> dummy functions */
Gilles Peskine449bd832023-01-11 14:50:10 +01006919int mbedtls_ssl_write_certificate(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08006920{
6921 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6922 ssl->handshake->ciphersuite_info;
6923
Gilles Peskine449bd832023-01-11 14:50:10 +01006924 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006925
Gilles Peskine449bd832023-01-11 14:50:10 +01006926 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
6927 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006928 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01006929 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08006930 }
6931
Gilles Peskine449bd832023-01-11 14:50:10 +01006932 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
6933 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08006934}
6935
Gilles Peskine449bd832023-01-11 14:50:10 +01006936int mbedtls_ssl_parse_certificate(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08006937{
6938 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6939 ssl->handshake->ciphersuite_info;
6940
Gilles Peskine449bd832023-01-11 14:50:10 +01006941 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006942
Gilles Peskine449bd832023-01-11 14:50:10 +01006943 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
6944 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006945 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01006946 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08006947 }
6948
Gilles Peskine449bd832023-01-11 14:50:10 +01006949 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
6950 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08006951}
6952
6953#else /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
6954/* Some certificate support -> implement write and parse */
6955
Gilles Peskine449bd832023-01-11 14:50:10 +01006956int mbedtls_ssl_write_certificate(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08006957{
6958 int ret = MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE;
6959 size_t i, n;
6960 const mbedtls_x509_crt *crt;
6961 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
6962 ssl->handshake->ciphersuite_info;
6963
Gilles Peskine449bd832023-01-11 14:50:10 +01006964 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006965
Gilles Peskine449bd832023-01-11 14:50:10 +01006966 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
6967 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006968 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01006969 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08006970 }
6971
6972#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01006973 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
6974 if (ssl->handshake->client_auth == 0) {
6975 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08006976 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01006977 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08006978 }
6979 }
6980#endif /* MBEDTLS_SSL_CLI_C */
6981#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01006982 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
6983 if (mbedtls_ssl_own_cert(ssl) == NULL) {
Jerry Yud9526692022-02-17 14:23:47 +08006984 /* Should never happen because we shouldn't have picked the
6985 * ciphersuite if we don't have a certificate. */
Gilles Peskine449bd832023-01-11 14:50:10 +01006986 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08006987 }
6988 }
6989#endif
6990
Gilles Peskine449bd832023-01-11 14:50:10 +01006991 MBEDTLS_SSL_DEBUG_CRT(3, "own certificate", mbedtls_ssl_own_cert(ssl));
Jerry Yud9526692022-02-17 14:23:47 +08006992
6993 /*
6994 * 0 . 0 handshake type
6995 * 1 . 3 handshake length
6996 * 4 . 6 length of all certs
6997 * 7 . 9 length of cert. 1
6998 * 10 . n-1 peer certificate
6999 * n . n+2 length of cert. 2
7000 * n+3 . ... upper level cert, etc.
7001 */
7002 i = 7;
Gilles Peskine449bd832023-01-11 14:50:10 +01007003 crt = mbedtls_ssl_own_cert(ssl);
Jerry Yud9526692022-02-17 14:23:47 +08007004
Gilles Peskine449bd832023-01-11 14:50:10 +01007005 while (crt != NULL) {
Jerry Yud9526692022-02-17 14:23:47 +08007006 n = crt->raw.len;
Gilles Peskine449bd832023-01-11 14:50:10 +01007007 if (n > MBEDTLS_SSL_OUT_CONTENT_LEN - 3 - i) {
7008 MBEDTLS_SSL_DEBUG_MSG(1, ("certificate too large, %" MBEDTLS_PRINTF_SIZET
7009 " > %" MBEDTLS_PRINTF_SIZET,
7010 i + 3 + n, (size_t) MBEDTLS_SSL_OUT_CONTENT_LEN));
7011 return MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL;
Jerry Yud9526692022-02-17 14:23:47 +08007012 }
7013
Gilles Peskine449bd832023-01-11 14:50:10 +01007014 ssl->out_msg[i] = MBEDTLS_BYTE_2(n);
7015 ssl->out_msg[i + 1] = MBEDTLS_BYTE_1(n);
7016 ssl->out_msg[i + 2] = MBEDTLS_BYTE_0(n);
Jerry Yud9526692022-02-17 14:23:47 +08007017
Gilles Peskine449bd832023-01-11 14:50:10 +01007018 i += 3; memcpy(ssl->out_msg + i, crt->raw.p, n);
Jerry Yud9526692022-02-17 14:23:47 +08007019 i += n; crt = crt->next;
7020 }
7021
Gilles Peskine449bd832023-01-11 14:50:10 +01007022 ssl->out_msg[4] = MBEDTLS_BYTE_2(i - 7);
7023 ssl->out_msg[5] = MBEDTLS_BYTE_1(i - 7);
7024 ssl->out_msg[6] = MBEDTLS_BYTE_0(i - 7);
Jerry Yud9526692022-02-17 14:23:47 +08007025
7026 ssl->out_msglen = i;
7027 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7028 ssl->out_msg[0] = MBEDTLS_SSL_HS_CERTIFICATE;
7029
7030 ssl->state++;
7031
Gilles Peskine449bd832023-01-11 14:50:10 +01007032 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
7033 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
7034 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007035 }
7036
Gilles Peskine449bd832023-01-11 14:50:10 +01007037 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007038
Gilles Peskine449bd832023-01-11 14:50:10 +01007039 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007040}
7041
7042#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
7043
7044#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007045MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007046static int ssl_check_peer_crt_unchanged(mbedtls_ssl_context *ssl,
7047 unsigned char *crt_buf,
7048 size_t crt_buf_len)
Jerry Yud9526692022-02-17 14:23:47 +08007049{
7050 mbedtls_x509_crt const * const peer_crt = ssl->session->peer_cert;
7051
Gilles Peskine449bd832023-01-11 14:50:10 +01007052 if (peer_crt == NULL) {
7053 return -1;
7054 }
Jerry Yud9526692022-02-17 14:23:47 +08007055
Gilles Peskine449bd832023-01-11 14:50:10 +01007056 if (peer_crt->raw.len != crt_buf_len) {
7057 return -1;
7058 }
Jerry Yud9526692022-02-17 14:23:47 +08007059
Gilles Peskine449bd832023-01-11 14:50:10 +01007060 return memcmp(peer_crt->raw.p, crt_buf, peer_crt->raw.len);
Jerry Yud9526692022-02-17 14:23:47 +08007061}
7062#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007063MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007064static int ssl_check_peer_crt_unchanged(mbedtls_ssl_context *ssl,
7065 unsigned char *crt_buf,
7066 size_t crt_buf_len)
Jerry Yud9526692022-02-17 14:23:47 +08007067{
7068 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7069 unsigned char const * const peer_cert_digest =
7070 ssl->session->peer_cert_digest;
7071 mbedtls_md_type_t const peer_cert_digest_type =
7072 ssl->session->peer_cert_digest_type;
7073 mbedtls_md_info_t const * const digest_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01007074 mbedtls_md_info_from_type(peer_cert_digest_type);
Jerry Yud9526692022-02-17 14:23:47 +08007075 unsigned char tmp_digest[MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN];
7076 size_t digest_len;
7077
Gilles Peskine449bd832023-01-11 14:50:10 +01007078 if (peer_cert_digest == NULL || digest_info == NULL) {
7079 return -1;
7080 }
Jerry Yud9526692022-02-17 14:23:47 +08007081
Gilles Peskine449bd832023-01-11 14:50:10 +01007082 digest_len = mbedtls_md_get_size(digest_info);
7083 if (digest_len > MBEDTLS_SSL_PEER_CERT_DIGEST_MAX_LEN) {
7084 return -1;
7085 }
Jerry Yud9526692022-02-17 14:23:47 +08007086
Gilles Peskine449bd832023-01-11 14:50:10 +01007087 ret = mbedtls_md(digest_info, crt_buf, crt_buf_len, tmp_digest);
7088 if (ret != 0) {
7089 return -1;
7090 }
Jerry Yud9526692022-02-17 14:23:47 +08007091
Gilles Peskine449bd832023-01-11 14:50:10 +01007092 return memcmp(tmp_digest, peer_cert_digest, digest_len);
Jerry Yud9526692022-02-17 14:23:47 +08007093}
7094#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7095#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
7096
7097/*
7098 * Once the certificate message is read, parse it into a cert chain and
7099 * perform basic checks, but leave actual verification to the caller
7100 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007101MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007102static int ssl_parse_certificate_chain(mbedtls_ssl_context *ssl,
7103 mbedtls_x509_crt *chain)
Jerry Yud9526692022-02-17 14:23:47 +08007104{
7105 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7106#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007107 int crt_cnt = 0;
Jerry Yud9526692022-02-17 14:23:47 +08007108#endif
7109 size_t i, n;
7110 uint8_t alert;
7111
Gilles Peskine449bd832023-01-11 14:50:10 +01007112 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
7113 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7114 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7115 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
7116 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Jerry Yud9526692022-02-17 14:23:47 +08007117 }
7118
Gilles Peskine449bd832023-01-11 14:50:10 +01007119 if (ssl->in_msg[0] != MBEDTLS_SSL_HS_CERTIFICATE) {
7120 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7121 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
7122 return MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
Jerry Yud9526692022-02-17 14:23:47 +08007123 }
7124
Gilles Peskine449bd832023-01-11 14:50:10 +01007125 if (ssl->in_hslen < mbedtls_ssl_hs_hdr_len(ssl) + 3 + 3) {
7126 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7127 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7128 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
7129 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007130 }
7131
Gilles Peskine449bd832023-01-11 14:50:10 +01007132 i = mbedtls_ssl_hs_hdr_len(ssl);
Jerry Yud9526692022-02-17 14:23:47 +08007133
7134 /*
7135 * Same message structure as in mbedtls_ssl_write_certificate()
7136 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007137 n = (ssl->in_msg[i+1] << 8) | ssl->in_msg[i+2];
Jerry Yud9526692022-02-17 14:23:47 +08007138
Gilles Peskine449bd832023-01-11 14:50:10 +01007139 if (ssl->in_msg[i] != 0 ||
7140 ssl->in_hslen != n + 3 + mbedtls_ssl_hs_hdr_len(ssl)) {
7141 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7142 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7143 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
7144 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007145 }
7146
7147 /* Make &ssl->in_msg[i] point to the beginning of the CRT chain. */
7148 i += 3;
7149
7150 /* Iterate through and parse the CRTs in the provided chain. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007151 while (i < ssl->in_hslen) {
Jerry Yud9526692022-02-17 14:23:47 +08007152 /* Check that there's room for the next CRT's length fields. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007153 if (i + 3 > ssl->in_hslen) {
7154 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7155 mbedtls_ssl_send_alert_message(ssl,
7156 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7157 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
7158 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007159 }
7160 /* In theory, the CRT can be up to 2**24 Bytes, but we don't support
7161 * anything beyond 2**16 ~ 64K. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007162 if (ssl->in_msg[i] != 0) {
7163 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7164 mbedtls_ssl_send_alert_message(ssl,
7165 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7166 MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT);
7167 return MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Jerry Yud9526692022-02-17 14:23:47 +08007168 }
7169
7170 /* Read length of the next CRT in the chain. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007171 n = ((unsigned int) ssl->in_msg[i + 1] << 8)
Jerry Yud9526692022-02-17 14:23:47 +08007172 | (unsigned int) ssl->in_msg[i + 2];
7173 i += 3;
7174
Gilles Peskine449bd832023-01-11 14:50:10 +01007175 if (n < 128 || i + n > ssl->in_hslen) {
7176 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate message"));
7177 mbedtls_ssl_send_alert_message(ssl,
7178 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7179 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
7180 return MBEDTLS_ERR_SSL_DECODE_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007181 }
7182
7183 /* Check if we're handling the first CRT in the chain. */
7184#if defined(MBEDTLS_SSL_RENEGOTIATION) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007185 if (crt_cnt++ == 0 &&
Jerry Yud9526692022-02-17 14:23:47 +08007186 ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT &&
Gilles Peskine449bd832023-01-11 14:50:10 +01007187 ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
Jerry Yud9526692022-02-17 14:23:47 +08007188 /* During client-side renegotiation, check that the server's
7189 * end-CRTs hasn't changed compared to the initial handshake,
7190 * mitigating the triple handshake attack. On success, reuse
7191 * the original end-CRT instead of parsing it again. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007192 MBEDTLS_SSL_DEBUG_MSG(3, ("Check that peer CRT hasn't changed during renegotiation"));
7193 if (ssl_check_peer_crt_unchanged(ssl,
7194 &ssl->in_msg[i],
7195 n) != 0) {
7196 MBEDTLS_SSL_DEBUG_MSG(1, ("new server cert during renegotiation"));
7197 mbedtls_ssl_send_alert_message(ssl,
7198 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7199 MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED);
7200 return MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Jerry Yud9526692022-02-17 14:23:47 +08007201 }
7202
7203 /* Now we can safely free the original chain. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007204 ssl_clear_peer_cert(ssl->session);
Jerry Yud9526692022-02-17 14:23:47 +08007205 }
7206#endif /* MBEDTLS_SSL_RENEGOTIATION && MBEDTLS_SSL_CLI_C */
7207
7208 /* Parse the next certificate in the chain. */
7209#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01007210 ret = mbedtls_x509_crt_parse_der(chain, ssl->in_msg + i, n);
Jerry Yud9526692022-02-17 14:23:47 +08007211#else
7212 /* If we don't need to store the CRT chain permanently, parse
7213 * it in-place from the input buffer instead of making a copy. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007214 ret = mbedtls_x509_crt_parse_der_nocopy(chain, ssl->in_msg + i, n);
Jerry Yud9526692022-02-17 14:23:47 +08007215#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +01007216 switch (ret) {
Jerry Yud9526692022-02-17 14:23:47 +08007217 case 0: /*ok*/
7218 case MBEDTLS_ERR_X509_UNKNOWN_SIG_ALG + MBEDTLS_ERR_OID_NOT_FOUND:
7219 /* Ignore certificate with an unknown algorithm: maybe a
7220 prior certificate was already trusted. */
7221 break;
7222
7223 case MBEDTLS_ERR_X509_ALLOC_FAILED:
7224 alert = MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR;
7225 goto crt_parse_der_failed;
7226
7227 case MBEDTLS_ERR_X509_UNKNOWN_VERSION:
7228 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
7229 goto crt_parse_der_failed;
7230
7231 default:
7232 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007233crt_parse_der_failed:
7234 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL, alert);
7235 MBEDTLS_SSL_DEBUG_RET(1, " mbedtls_x509_crt_parse_der", ret);
7236 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007237 }
7238
7239 i += n;
7240 }
7241
Gilles Peskine449bd832023-01-11 14:50:10 +01007242 MBEDTLS_SSL_DEBUG_CRT(3, "peer certificate", chain);
7243 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08007244}
7245
7246#if defined(MBEDTLS_SSL_SRV_C)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007247MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007248static int ssl_srv_check_client_no_crt_notification(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08007249{
Gilles Peskine449bd832023-01-11 14:50:10 +01007250 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
7251 return -1;
7252 }
Jerry Yud9526692022-02-17 14:23:47 +08007253
Gilles Peskine449bd832023-01-11 14:50:10 +01007254 if (ssl->in_hslen == 3 + mbedtls_ssl_hs_hdr_len(ssl) &&
Jerry Yud9526692022-02-17 14:23:47 +08007255 ssl->in_msgtype == MBEDTLS_SSL_MSG_HANDSHAKE &&
7256 ssl->in_msg[0] == MBEDTLS_SSL_HS_CERTIFICATE &&
Gilles Peskine449bd832023-01-11 14:50:10 +01007257 memcmp(ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl), "\0\0\0", 3) == 0) {
7258 MBEDTLS_SSL_DEBUG_MSG(1, ("peer has no certificate"));
7259 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08007260 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007261 return -1;
Jerry Yud9526692022-02-17 14:23:47 +08007262}
7263#endif /* MBEDTLS_SSL_SRV_C */
7264
7265/* Check if a certificate message is expected.
7266 * Return either
7267 * - SSL_CERTIFICATE_EXPECTED, or
7268 * - SSL_CERTIFICATE_SKIP
7269 * indicating whether a Certificate message is expected or not.
7270 */
7271#define SSL_CERTIFICATE_EXPECTED 0
7272#define SSL_CERTIFICATE_SKIP 1
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007273MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007274static int ssl_parse_certificate_coordinate(mbedtls_ssl_context *ssl,
7275 int authmode)
Jerry Yud9526692022-02-17 14:23:47 +08007276{
7277 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
7278 ssl->handshake->ciphersuite_info;
7279
Gilles Peskine449bd832023-01-11 14:50:10 +01007280 if (!mbedtls_ssl_ciphersuite_uses_srv_cert(ciphersuite_info)) {
7281 return SSL_CERTIFICATE_SKIP;
7282 }
Jerry Yud9526692022-02-17 14:23:47 +08007283
7284#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007285 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
7286 if (ciphersuite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_RSA_PSK) {
7287 return SSL_CERTIFICATE_SKIP;
7288 }
Jerry Yud9526692022-02-17 14:23:47 +08007289
Gilles Peskine449bd832023-01-11 14:50:10 +01007290 if (authmode == MBEDTLS_SSL_VERIFY_NONE) {
Jerry Yud9526692022-02-17 14:23:47 +08007291 ssl->session_negotiate->verify_result =
7292 MBEDTLS_X509_BADCERT_SKIP_VERIFY;
Gilles Peskine449bd832023-01-11 14:50:10 +01007293 return SSL_CERTIFICATE_SKIP;
Jerry Yud9526692022-02-17 14:23:47 +08007294 }
7295 }
7296#else
7297 ((void) authmode);
7298#endif /* MBEDTLS_SSL_SRV_C */
7299
Gilles Peskine449bd832023-01-11 14:50:10 +01007300 return SSL_CERTIFICATE_EXPECTED;
Jerry Yud9526692022-02-17 14:23:47 +08007301}
7302
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007303MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007304static int ssl_parse_certificate_verify(mbedtls_ssl_context *ssl,
7305 int authmode,
7306 mbedtls_x509_crt *chain,
7307 void *rs_ctx)
Jerry Yud9526692022-02-17 14:23:47 +08007308{
7309 int ret = 0;
7310 const mbedtls_ssl_ciphersuite_t *ciphersuite_info =
7311 ssl->handshake->ciphersuite_info;
7312 int have_ca_chain = 0;
7313
7314 int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
7315 void *p_vrfy;
7316
Gilles Peskine449bd832023-01-11 14:50:10 +01007317 if (authmode == MBEDTLS_SSL_VERIFY_NONE) {
7318 return 0;
7319 }
Jerry Yud9526692022-02-17 14:23:47 +08007320
Gilles Peskine449bd832023-01-11 14:50:10 +01007321 if (ssl->f_vrfy != NULL) {
7322 MBEDTLS_SSL_DEBUG_MSG(3, ("Use context-specific verification callback"));
Jerry Yud9526692022-02-17 14:23:47 +08007323 f_vrfy = ssl->f_vrfy;
7324 p_vrfy = ssl->p_vrfy;
Gilles Peskine449bd832023-01-11 14:50:10 +01007325 } else {
7326 MBEDTLS_SSL_DEBUG_MSG(3, ("Use configuration-specific verification callback"));
Jerry Yud9526692022-02-17 14:23:47 +08007327 f_vrfy = ssl->conf->f_vrfy;
7328 p_vrfy = ssl->conf->p_vrfy;
7329 }
7330
7331 /*
7332 * Main check: verify certificate
7333 */
7334#if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
Gilles Peskine449bd832023-01-11 14:50:10 +01007335 if (ssl->conf->f_ca_cb != NULL) {
Jerry Yud9526692022-02-17 14:23:47 +08007336 ((void) rs_ctx);
7337 have_ca_chain = 1;
7338
Gilles Peskine449bd832023-01-11 14:50:10 +01007339 MBEDTLS_SSL_DEBUG_MSG(3, ("use CA callback for X.509 CRT verification"));
Jerry Yud9526692022-02-17 14:23:47 +08007340 ret = mbedtls_x509_crt_verify_with_ca_cb(
7341 chain,
7342 ssl->conf->f_ca_cb,
7343 ssl->conf->p_ca_cb,
7344 ssl->conf->cert_profile,
7345 ssl->hostname,
7346 &ssl->session_negotiate->verify_result,
Gilles Peskine449bd832023-01-11 14:50:10 +01007347 f_vrfy, p_vrfy);
7348 } else
Jerry Yud9526692022-02-17 14:23:47 +08007349#endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
7350 {
7351 mbedtls_x509_crt *ca_chain;
7352 mbedtls_x509_crl *ca_crl;
7353
7354#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01007355 if (ssl->handshake->sni_ca_chain != NULL) {
Jerry Yud9526692022-02-17 14:23:47 +08007356 ca_chain = ssl->handshake->sni_ca_chain;
7357 ca_crl = ssl->handshake->sni_ca_crl;
Gilles Peskine449bd832023-01-11 14:50:10 +01007358 } else
Jerry Yud9526692022-02-17 14:23:47 +08007359#endif
7360 {
7361 ca_chain = ssl->conf->ca_chain;
7362 ca_crl = ssl->conf->ca_crl;
7363 }
7364
Gilles Peskine449bd832023-01-11 14:50:10 +01007365 if (ca_chain != NULL) {
Jerry Yud9526692022-02-17 14:23:47 +08007366 have_ca_chain = 1;
Gilles Peskine449bd832023-01-11 14:50:10 +01007367 }
Jerry Yud9526692022-02-17 14:23:47 +08007368
7369 ret = mbedtls_x509_crt_verify_restartable(
7370 chain,
7371 ca_chain, ca_crl,
7372 ssl->conf->cert_profile,
7373 ssl->hostname,
7374 &ssl->session_negotiate->verify_result,
Gilles Peskine449bd832023-01-11 14:50:10 +01007375 f_vrfy, p_vrfy, rs_ctx);
Jerry Yud9526692022-02-17 14:23:47 +08007376 }
7377
Gilles Peskine449bd832023-01-11 14:50:10 +01007378 if (ret != 0) {
7379 MBEDTLS_SSL_DEBUG_RET(1, "x509_verify_cert", ret);
Jerry Yud9526692022-02-17 14:23:47 +08007380 }
7381
7382#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007383 if (ret == MBEDTLS_ERR_ECP_IN_PROGRESS) {
7384 return MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS;
7385 }
Jerry Yud9526692022-02-17 14:23:47 +08007386#endif
7387
7388 /*
7389 * Secondary checks: always done, but change 'ret' only if it was 0
7390 */
7391
7392#if defined(MBEDTLS_ECP_C)
7393 {
7394 const mbedtls_pk_context *pk = &chain->pk;
7395
Manuel Pégourié-Gonnard66b0d612022-06-17 10:49:29 +02007396 /* If certificate uses an EC key, make sure the curve is OK.
7397 * This is a public key, so it can't be opaque, so can_do() is a good
7398 * enough check to ensure pk_ec() is safe to use here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007399 if (mbedtls_pk_can_do(pk, MBEDTLS_PK_ECKEY)) {
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007400 /* and in the unlikely case the above assumption no longer holds
7401 * we are making sure that pk_ec() here does not return a NULL
7402 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007403 const mbedtls_ecp_keypair *ec = mbedtls_pk_ec(*pk);
7404 if (ec == NULL) {
7405 MBEDTLS_SSL_DEBUG_MSG(1, ("mbedtls_pk_ec() returned NULL"));
7406 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007407 }
Jerry Yud9526692022-02-17 14:23:47 +08007408
Gilles Peskine449bd832023-01-11 14:50:10 +01007409 if (mbedtls_ssl_check_curve(ssl, ec->grp.id) != 0) {
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007410 ssl->session_negotiate->verify_result |=
7411 MBEDTLS_X509_BADCERT_BAD_KEY;
7412
Gilles Peskine449bd832023-01-11 14:50:10 +01007413 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate (EC key curve)"));
7414 if (ret == 0) {
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007415 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Gilles Peskine449bd832023-01-11 14:50:10 +01007416 }
Leonid Rozenboim19e59732022-08-08 16:52:38 -07007417 }
Jerry Yud9526692022-02-17 14:23:47 +08007418 }
7419 }
7420#endif /* MBEDTLS_ECP_C */
7421
Gilles Peskine449bd832023-01-11 14:50:10 +01007422 if (mbedtls_ssl_check_cert_usage(chain,
7423 ciphersuite_info,
7424 !ssl->conf->endpoint,
7425 &ssl->session_negotiate->verify_result) != 0) {
7426 MBEDTLS_SSL_DEBUG_MSG(1, ("bad certificate (usage extensions)"));
7427 if (ret == 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007428 ret = MBEDTLS_ERR_SSL_BAD_CERTIFICATE;
Gilles Peskine449bd832023-01-11 14:50:10 +01007429 }
Jerry Yud9526692022-02-17 14:23:47 +08007430 }
7431
7432 /* mbedtls_x509_crt_verify_with_profile is supposed to report a
7433 * verification failure through MBEDTLS_ERR_X509_CERT_VERIFY_FAILED,
7434 * with details encoded in the verification flags. All other kinds
7435 * of error codes, including those from the user provided f_vrfy
7436 * functions, are treated as fatal and lead to a failure of
7437 * ssl_parse_certificate even if verification was optional. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007438 if (authmode == MBEDTLS_SSL_VERIFY_OPTIONAL &&
7439 (ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED ||
7440 ret == MBEDTLS_ERR_SSL_BAD_CERTIFICATE)) {
Jerry Yud9526692022-02-17 14:23:47 +08007441 ret = 0;
7442 }
7443
Gilles Peskine449bd832023-01-11 14:50:10 +01007444 if (have_ca_chain == 0 && authmode == MBEDTLS_SSL_VERIFY_REQUIRED) {
7445 MBEDTLS_SSL_DEBUG_MSG(1, ("got no CA chain"));
Jerry Yud9526692022-02-17 14:23:47 +08007446 ret = MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED;
7447 }
7448
Gilles Peskine449bd832023-01-11 14:50:10 +01007449 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007450 uint8_t alert;
7451
7452 /* The certificate may have been rejected for several reasons.
7453 Pick one and send the corresponding alert. Which alert to send
7454 may be a subject of debate in some cases. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007455 if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_OTHER) {
Jerry Yud9526692022-02-17 14:23:47 +08007456 alert = MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED;
Gilles Peskine449bd832023-01-11 14:50:10 +01007457 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_CN_MISMATCH) {
Jerry Yud9526692022-02-17 14:23:47 +08007458 alert = MBEDTLS_SSL_ALERT_MSG_BAD_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007459 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_KEY_USAGE) {
Jerry Yud9526692022-02-17 14:23:47 +08007460 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007461 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXT_KEY_USAGE) {
Jerry Yud9526692022-02-17 14:23:47 +08007462 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007463 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NS_CERT_TYPE) {
Jerry Yud9526692022-02-17 14:23:47 +08007464 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007465 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_PK) {
Jerry Yud9526692022-02-17 14:23:47 +08007466 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007467 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_BAD_KEY) {
Jerry Yud9526692022-02-17 14:23:47 +08007468 alert = MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT;
Gilles Peskine449bd832023-01-11 14:50:10 +01007469 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_EXPIRED) {
Jerry Yud9526692022-02-17 14:23:47 +08007470 alert = MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED;
Gilles Peskine449bd832023-01-11 14:50:10 +01007471 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_REVOKED) {
Jerry Yud9526692022-02-17 14:23:47 +08007472 alert = MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED;
Gilles Peskine449bd832023-01-11 14:50:10 +01007473 } else if (ssl->session_negotiate->verify_result & MBEDTLS_X509_BADCERT_NOT_TRUSTED) {
Jerry Yud9526692022-02-17 14:23:47 +08007474 alert = MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA;
Gilles Peskine449bd832023-01-11 14:50:10 +01007475 } else {
Jerry Yud9526692022-02-17 14:23:47 +08007476 alert = MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN;
Gilles Peskine449bd832023-01-11 14:50:10 +01007477 }
7478 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7479 alert);
Jerry Yud9526692022-02-17 14:23:47 +08007480 }
7481
7482#if defined(MBEDTLS_DEBUG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007483 if (ssl->session_negotiate->verify_result != 0) {
7484 MBEDTLS_SSL_DEBUG_MSG(3, ("! Certificate verification flags %08x",
7485 (unsigned int) ssl->session_negotiate->verify_result));
7486 } else {
7487 MBEDTLS_SSL_DEBUG_MSG(3, ("Certificate verification flags clear"));
Jerry Yud9526692022-02-17 14:23:47 +08007488 }
7489#endif /* MBEDTLS_DEBUG_C */
7490
Gilles Peskine449bd832023-01-11 14:50:10 +01007491 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007492}
7493
7494#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007495MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007496static int ssl_remember_peer_crt_digest(mbedtls_ssl_context *ssl,
7497 unsigned char *start, size_t len)
Jerry Yud9526692022-02-17 14:23:47 +08007498{
7499 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7500 /* Remember digest of the peer's end-CRT. */
7501 ssl->session_negotiate->peer_cert_digest =
Gilles Peskine449bd832023-01-11 14:50:10 +01007502 mbedtls_calloc(1, MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN);
7503 if (ssl->session_negotiate->peer_cert_digest == NULL) {
7504 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%d bytes) failed",
7505 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN));
7506 mbedtls_ssl_send_alert_message(ssl,
7507 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7508 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
Jerry Yud9526692022-02-17 14:23:47 +08007509
Gilles Peskine449bd832023-01-11 14:50:10 +01007510 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
Jerry Yud9526692022-02-17 14:23:47 +08007511 }
7512
Gilles Peskine449bd832023-01-11 14:50:10 +01007513 ret = mbedtls_md(mbedtls_md_info_from_type(
7514 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE),
7515 start, len,
7516 ssl->session_negotiate->peer_cert_digest);
Jerry Yud9526692022-02-17 14:23:47 +08007517
7518 ssl->session_negotiate->peer_cert_digest_type =
7519 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_TYPE;
7520 ssl->session_negotiate->peer_cert_digest_len =
7521 MBEDTLS_SSL_PEER_CERT_DIGEST_DFL_LEN;
7522
Gilles Peskine449bd832023-01-11 14:50:10 +01007523 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007524}
7525
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02007526MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01007527static int ssl_remember_peer_pubkey(mbedtls_ssl_context *ssl,
7528 unsigned char *start, size_t len)
Jerry Yud9526692022-02-17 14:23:47 +08007529{
7530 unsigned char *end = start + len;
7531 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
7532
7533 /* Make a copy of the peer's raw public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007534 mbedtls_pk_init(&ssl->handshake->peer_pubkey);
7535 ret = mbedtls_pk_parse_subpubkey(&start, end,
7536 &ssl->handshake->peer_pubkey);
7537 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007538 /* We should have parsed the public key before. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007539 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yud9526692022-02-17 14:23:47 +08007540 }
7541
Gilles Peskine449bd832023-01-11 14:50:10 +01007542 return 0;
Jerry Yud9526692022-02-17 14:23:47 +08007543}
7544#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7545
Gilles Peskine449bd832023-01-11 14:50:10 +01007546int mbedtls_ssl_parse_certificate(mbedtls_ssl_context *ssl)
Jerry Yud9526692022-02-17 14:23:47 +08007547{
7548 int ret = 0;
7549 int crt_expected;
7550#if defined(MBEDTLS_SSL_SRV_C) && defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
7551 const int authmode = ssl->handshake->sni_authmode != MBEDTLS_SSL_VERIFY_UNSET
7552 ? ssl->handshake->sni_authmode
7553 : ssl->conf->authmode;
7554#else
7555 const int authmode = ssl->conf->authmode;
7556#endif
7557 void *rs_ctx = NULL;
7558 mbedtls_x509_crt *chain = NULL;
7559
Gilles Peskine449bd832023-01-11 14:50:10 +01007560 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007561
Gilles Peskine449bd832023-01-11 14:50:10 +01007562 crt_expected = ssl_parse_certificate_coordinate(ssl, authmode);
7563 if (crt_expected == SSL_CERTIFICATE_SKIP) {
7564 MBEDTLS_SSL_DEBUG_MSG(2, ("<= skip parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007565 goto exit;
7566 }
7567
7568#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007569 if (ssl->handshake->ecrs_enabled &&
7570 ssl->handshake->ecrs_state == ssl_ecrs_crt_verify) {
Jerry Yud9526692022-02-17 14:23:47 +08007571 chain = ssl->handshake->ecrs_peer_cert;
7572 ssl->handshake->ecrs_peer_cert = NULL;
7573 goto crt_verify;
7574 }
7575#endif
7576
Gilles Peskine449bd832023-01-11 14:50:10 +01007577 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007578 /* mbedtls_ssl_read_record may have sent an alert already. We
7579 let it decide whether to alert. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007580 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
Jerry Yud9526692022-02-17 14:23:47 +08007581 goto exit;
7582 }
7583
7584#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007585 if (ssl_srv_check_client_no_crt_notification(ssl) == 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007586 ssl->session_negotiate->verify_result = MBEDTLS_X509_BADCERT_MISSING;
7587
Gilles Peskine449bd832023-01-11 14:50:10 +01007588 if (authmode != MBEDTLS_SSL_VERIFY_OPTIONAL) {
Jerry Yud9526692022-02-17 14:23:47 +08007589 ret = MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE;
Gilles Peskine449bd832023-01-11 14:50:10 +01007590 }
Jerry Yud9526692022-02-17 14:23:47 +08007591
7592 goto exit;
7593 }
7594#endif /* MBEDTLS_SSL_SRV_C */
7595
7596 /* Clear existing peer CRT structure in case we tried to
7597 * reuse a session but it failed, and allocate a new one. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007598 ssl_clear_peer_cert(ssl->session_negotiate);
Jerry Yud9526692022-02-17 14:23:47 +08007599
Gilles Peskine449bd832023-01-11 14:50:10 +01007600 chain = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
7601 if (chain == NULL) {
7602 MBEDTLS_SSL_DEBUG_MSG(1, ("alloc(%" MBEDTLS_PRINTF_SIZET " bytes) failed",
7603 sizeof(mbedtls_x509_crt)));
7604 mbedtls_ssl_send_alert_message(ssl,
7605 MBEDTLS_SSL_ALERT_LEVEL_FATAL,
7606 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
Jerry Yud9526692022-02-17 14:23:47 +08007607
7608 ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
7609 goto exit;
7610 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007611 mbedtls_x509_crt_init(chain);
Jerry Yud9526692022-02-17 14:23:47 +08007612
Gilles Peskine449bd832023-01-11 14:50:10 +01007613 ret = ssl_parse_certificate_chain(ssl, chain);
7614 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007615 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007616 }
Jerry Yud9526692022-02-17 14:23:47 +08007617
7618#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007619 if (ssl->handshake->ecrs_enabled) {
Jerry Yud9526692022-02-17 14:23:47 +08007620 ssl->handshake->ecrs_state = ssl_ecrs_crt_verify;
Gilles Peskine449bd832023-01-11 14:50:10 +01007621 }
Jerry Yud9526692022-02-17 14:23:47 +08007622
7623crt_verify:
Gilles Peskine449bd832023-01-11 14:50:10 +01007624 if (ssl->handshake->ecrs_enabled) {
Jerry Yud9526692022-02-17 14:23:47 +08007625 rs_ctx = &ssl->handshake->ecrs_ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +01007626 }
Jerry Yud9526692022-02-17 14:23:47 +08007627#endif
7628
Gilles Peskine449bd832023-01-11 14:50:10 +01007629 ret = ssl_parse_certificate_verify(ssl, authmode,
7630 chain, rs_ctx);
7631 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007632 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007633 }
Jerry Yud9526692022-02-17 14:23:47 +08007634
7635#if !defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
7636 {
7637 unsigned char *crt_start, *pk_start;
7638 size_t crt_len, pk_len;
7639
7640 /* We parse the CRT chain without copying, so
7641 * these pointers point into the input buffer,
7642 * and are hence still valid after freeing the
7643 * CRT chain. */
7644
7645 crt_start = chain->raw.p;
7646 crt_len = chain->raw.len;
7647
7648 pk_start = chain->pk_raw.p;
7649 pk_len = chain->pk_raw.len;
7650
7651 /* Free the CRT structures before computing
7652 * digest and copying the peer's public key. */
Gilles Peskine449bd832023-01-11 14:50:10 +01007653 mbedtls_x509_crt_free(chain);
7654 mbedtls_free(chain);
Jerry Yud9526692022-02-17 14:23:47 +08007655 chain = NULL;
7656
Gilles Peskine449bd832023-01-11 14:50:10 +01007657 ret = ssl_remember_peer_crt_digest(ssl, crt_start, crt_len);
7658 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007659 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007660 }
Jerry Yud9526692022-02-17 14:23:47 +08007661
Gilles Peskine449bd832023-01-11 14:50:10 +01007662 ret = ssl_remember_peer_pubkey(ssl, pk_start, pk_len);
7663 if (ret != 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007664 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +01007665 }
Jerry Yud9526692022-02-17 14:23:47 +08007666 }
7667#else /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7668 /* Pass ownership to session structure. */
7669 ssl->session_negotiate->peer_cert = chain;
7670 chain = NULL;
7671#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
7672
Gilles Peskine449bd832023-01-11 14:50:10 +01007673 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse certificate"));
Jerry Yud9526692022-02-17 14:23:47 +08007674
7675exit:
7676
Gilles Peskine449bd832023-01-11 14:50:10 +01007677 if (ret == 0) {
Jerry Yud9526692022-02-17 14:23:47 +08007678 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01007679 }
Jerry Yud9526692022-02-17 14:23:47 +08007680
7681#if defined(MBEDTLS_SSL_ECP_RESTARTABLE_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01007682 if (ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS) {
Jerry Yud9526692022-02-17 14:23:47 +08007683 ssl->handshake->ecrs_peer_cert = chain;
7684 chain = NULL;
7685 }
7686#endif
7687
Gilles Peskine449bd832023-01-11 14:50:10 +01007688 if (chain != NULL) {
7689 mbedtls_x509_crt_free(chain);
7690 mbedtls_free(chain);
Jerry Yud9526692022-02-17 14:23:47 +08007691 }
7692
Gilles Peskine449bd832023-01-11 14:50:10 +01007693 return ret;
Jerry Yud9526692022-02-17 14:23:47 +08007694}
7695#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
7696
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01007697#if defined(MBEDTLS_MD_CAN_SHA256)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01007698static int ssl_calc_finished_tls_sha256(
Gilles Peskine449bd832023-01-11 14:50:10 +01007699 mbedtls_ssl_context *ssl, unsigned char *buf, int from)
Jerry Yu615bd6f2022-02-17 14:25:15 +08007700{
7701 int len = 12;
7702 const char *sender;
7703 unsigned char padbuf[32];
7704#if defined(MBEDTLS_USE_PSA_CRYPTO)
7705 size_t hash_size;
7706 psa_hash_operation_t sha256_psa = PSA_HASH_OPERATION_INIT;
7707 psa_status_t status;
7708#else
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007709 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01007710 mbedtls_md_context_t sha256;
Jerry Yu615bd6f2022-02-17 14:25:15 +08007711#endif
7712
7713 mbedtls_ssl_session *session = ssl->session_negotiate;
Gilles Peskine449bd832023-01-11 14:50:10 +01007714 if (!session) {
Jerry Yu615bd6f2022-02-17 14:25:15 +08007715 session = ssl->session;
Gilles Peskine449bd832023-01-11 14:50:10 +01007716 }
Jerry Yu615bd6f2022-02-17 14:25:15 +08007717
Gilles Peskine449bd832023-01-11 14:50:10 +01007718 sender = (from == MBEDTLS_SSL_IS_CLIENT)
Jerry Yu615bd6f2022-02-17 14:25:15 +08007719 ? "client finished"
7720 : "server finished";
7721
7722#if defined(MBEDTLS_USE_PSA_CRYPTO)
7723 sha256_psa = psa_hash_operation_init();
7724
Gilles Peskine449bd832023-01-11 14:50:10 +01007725 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc PSA finished tls sha256"));
Jerry Yu615bd6f2022-02-17 14:25:15 +08007726
Gilles Peskine449bd832023-01-11 14:50:10 +01007727 status = psa_hash_clone(&ssl->handshake->fin_sha256_psa, &sha256_psa);
7728 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007729 goto exit;
Jerry Yu615bd6f2022-02-17 14:25:15 +08007730 }
7731
Gilles Peskine449bd832023-01-11 14:50:10 +01007732 status = psa_hash_finish(&sha256_psa, padbuf, sizeof(padbuf), &hash_size);
7733 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007734 goto exit;
Jerry Yu615bd6f2022-02-17 14:25:15 +08007735 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007736 MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated padbuf", padbuf, 32);
Jerry Yu615bd6f2022-02-17 14:25:15 +08007737#else
7738
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01007739 mbedtls_md_init(&sha256);
Jerry Yu615bd6f2022-02-17 14:25:15 +08007740
Gilles Peskine449bd832023-01-11 14:50:10 +01007741 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc finished tls sha256"));
Jerry Yu615bd6f2022-02-17 14:25:15 +08007742
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01007743 ret = mbedtls_md_setup(&sha256, mbedtls_md_info_from_type(MBEDTLS_MD_SHA256), 0);
7744 if (ret != 0) {
7745 goto exit;
7746 }
7747 ret = mbedtls_md_clone(&sha256, &ssl->handshake->fin_sha256);
7748 if (ret != 0) {
7749 goto exit;
7750 }
Jerry Yu615bd6f2022-02-17 14:25:15 +08007751
7752 /*
7753 * TLSv1.2:
7754 * hash = PRF( master, finished_label,
7755 * Hash( handshake ) )[0.11]
7756 */
7757
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01007758 ret = mbedtls_md_finish(&sha256, padbuf);
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007759 if (ret != 0) {
7760 goto exit;
7761 }
Jerry Yu615bd6f2022-02-17 14:25:15 +08007762#endif /* MBEDTLS_USE_PSA_CRYPTO */
7763
Manuel Pégourié-Gonnard0ac71c02023-02-24 12:13:55 +01007764 MBEDTLS_SSL_DEBUG_BUF(4, "finished sha256 output", padbuf, 32);
7765
Gilles Peskine449bd832023-01-11 14:50:10 +01007766 ssl->handshake->tls_prf(session->master, 48, sender,
7767 padbuf, 32, buf, len);
Jerry Yu615bd6f2022-02-17 14:25:15 +08007768
Gilles Peskine449bd832023-01-11 14:50:10 +01007769 MBEDTLS_SSL_DEBUG_BUF(3, "calc finished result", buf, len);
Jerry Yu615bd6f2022-02-17 14:25:15 +08007770
Gilles Peskine449bd832023-01-11 14:50:10 +01007771 mbedtls_platform_zeroize(padbuf, sizeof(padbuf));
Jerry Yu615bd6f2022-02-17 14:25:15 +08007772
Gilles Peskine449bd832023-01-11 14:50:10 +01007773 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc finished"));
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007774
7775exit:
7776#if defined(MBEDTLS_USE_PSA_CRYPTO)
7777 psa_hash_abort(&sha256_psa);
Andrzej Kurekdaf5b562023-03-03 05:52:28 -05007778 return PSA_TO_MD_ERR(status);
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007779#else
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01007780 mbedtls_md_free(&sha256);
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007781 return ret;
7782#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu615bd6f2022-02-17 14:25:15 +08007783}
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01007784#endif /* MBEDTLS_MD_CAN_SHA256*/
Jerry Yu615bd6f2022-02-17 14:25:15 +08007785
Jerry Yub7ba49e2022-02-17 14:25:53 +08007786
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01007787#if defined(MBEDTLS_MD_CAN_SHA384)
Manuel Pégourié-Gonnard226aa152023-02-05 09:46:59 +01007788static int ssl_calc_finished_tls_sha384(
Gilles Peskine449bd832023-01-11 14:50:10 +01007789 mbedtls_ssl_context *ssl, unsigned char *buf, int from)
Jerry Yub7ba49e2022-02-17 14:25:53 +08007790{
7791 int len = 12;
7792 const char *sender;
7793 unsigned char padbuf[48];
7794#if defined(MBEDTLS_USE_PSA_CRYPTO)
7795 size_t hash_size;
7796 psa_hash_operation_t sha384_psa = PSA_HASH_OPERATION_INIT;
7797 psa_status_t status;
7798#else
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007799 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01007800 mbedtls_md_context_t sha384;
Jerry Yub7ba49e2022-02-17 14:25:53 +08007801#endif
7802
7803 mbedtls_ssl_session *session = ssl->session_negotiate;
Gilles Peskine449bd832023-01-11 14:50:10 +01007804 if (!session) {
Jerry Yub7ba49e2022-02-17 14:25:53 +08007805 session = ssl->session;
Gilles Peskine449bd832023-01-11 14:50:10 +01007806 }
Jerry Yub7ba49e2022-02-17 14:25:53 +08007807
Gilles Peskine449bd832023-01-11 14:50:10 +01007808 sender = (from == MBEDTLS_SSL_IS_CLIENT)
Jerry Yub7ba49e2022-02-17 14:25:53 +08007809 ? "client finished"
7810 : "server finished";
7811
7812#if defined(MBEDTLS_USE_PSA_CRYPTO)
7813 sha384_psa = psa_hash_operation_init();
7814
Gilles Peskine449bd832023-01-11 14:50:10 +01007815 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc PSA finished tls sha384"));
Jerry Yub7ba49e2022-02-17 14:25:53 +08007816
Gilles Peskine449bd832023-01-11 14:50:10 +01007817 status = psa_hash_clone(&ssl->handshake->fin_sha384_psa, &sha384_psa);
7818 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007819 goto exit;
Jerry Yub7ba49e2022-02-17 14:25:53 +08007820 }
7821
Gilles Peskine449bd832023-01-11 14:50:10 +01007822 status = psa_hash_finish(&sha384_psa, padbuf, sizeof(padbuf), &hash_size);
7823 if (status != PSA_SUCCESS) {
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007824 goto exit;
Jerry Yub7ba49e2022-02-17 14:25:53 +08007825 }
Gilles Peskine449bd832023-01-11 14:50:10 +01007826 MBEDTLS_SSL_DEBUG_BUF(3, "PSA calculated padbuf", padbuf, 48);
Jerry Yub7ba49e2022-02-17 14:25:53 +08007827#else
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01007828 mbedtls_md_init(&sha384);
Jerry Yub7ba49e2022-02-17 14:25:53 +08007829
Gilles Peskine449bd832023-01-11 14:50:10 +01007830 MBEDTLS_SSL_DEBUG_MSG(2, ("=> calc finished tls sha384"));
Jerry Yub7ba49e2022-02-17 14:25:53 +08007831
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01007832 ret = mbedtls_md_setup(&sha384, mbedtls_md_info_from_type(MBEDTLS_MD_SHA384), 0);
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01007833 if (ret != 0) {
7834 goto exit;
7835 }
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01007836 ret = mbedtls_md_clone(&sha384, &ssl->handshake->fin_sha384);
Manuel Pégourié-Gonnardf057ecf2023-02-24 13:19:17 +01007837 if (ret != 0) {
7838 goto exit;
7839 }
Jerry Yub7ba49e2022-02-17 14:25:53 +08007840
7841 /*
7842 * TLSv1.2:
7843 * hash = PRF( master, finished_label,
7844 * Hash( handshake ) )[0.11]
7845 */
7846
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01007847 ret = mbedtls_md_finish(&sha384, padbuf);
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007848 if (ret != 0) {
7849 goto exit;
7850 }
Jerry Yub7ba49e2022-02-17 14:25:53 +08007851#endif
7852
Manuel Pégourié-Gonnard0ac71c02023-02-24 12:13:55 +01007853 MBEDTLS_SSL_DEBUG_BUF(4, "finished sha384 output", padbuf, 48);
7854
Gilles Peskine449bd832023-01-11 14:50:10 +01007855 ssl->handshake->tls_prf(session->master, 48, sender,
7856 padbuf, 48, buf, len);
Jerry Yub7ba49e2022-02-17 14:25:53 +08007857
Gilles Peskine449bd832023-01-11 14:50:10 +01007858 MBEDTLS_SSL_DEBUG_BUF(3, "calc finished result", buf, len);
Jerry Yub7ba49e2022-02-17 14:25:53 +08007859
Gilles Peskine449bd832023-01-11 14:50:10 +01007860 mbedtls_platform_zeroize(padbuf, sizeof(padbuf));
Jerry Yub7ba49e2022-02-17 14:25:53 +08007861
Gilles Peskine449bd832023-01-11 14:50:10 +01007862 MBEDTLS_SSL_DEBUG_MSG(2, ("<= calc finished"));
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007863
7864exit:
7865#if defined(MBEDTLS_USE_PSA_CRYPTO)
7866 psa_hash_abort(&sha384_psa);
Andrzej Kurekdaf5b562023-03-03 05:52:28 -05007867 return PSA_TO_MD_ERR(status);
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007868#else
Manuel Pégourié-Gonnard02d55d52023-02-24 13:21:16 +01007869 mbedtls_md_free(&sha384);
Manuel Pégourié-Gonnarde1a4caa2023-02-06 10:14:25 +01007870 return ret;
7871#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yub7ba49e2022-02-17 14:25:53 +08007872}
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01007873#endif /* MBEDTLS_MD_CAN_SHA384*/
Jerry Yub7ba49e2022-02-17 14:25:53 +08007874
Gilles Peskine449bd832023-01-11 14:50:10 +01007875void mbedtls_ssl_handshake_wrapup_free_hs_transform(mbedtls_ssl_context *ssl)
Jerry Yuaef00152022-02-17 14:27:31 +08007876{
Gilles Peskine449bd832023-01-11 14:50:10 +01007877 MBEDTLS_SSL_DEBUG_MSG(3, ("=> handshake wrapup: final free"));
Jerry Yuaef00152022-02-17 14:27:31 +08007878
7879 /*
7880 * Free our handshake params
7881 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007882 mbedtls_ssl_handshake_free(ssl);
7883 mbedtls_free(ssl->handshake);
Jerry Yuaef00152022-02-17 14:27:31 +08007884 ssl->handshake = NULL;
7885
7886 /*
Shaun Case8b0ecbc2021-12-20 21:14:10 -08007887 * Free the previous transform and switch in the current one
Jerry Yuaef00152022-02-17 14:27:31 +08007888 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007889 if (ssl->transform) {
7890 mbedtls_ssl_transform_free(ssl->transform);
7891 mbedtls_free(ssl->transform);
Jerry Yuaef00152022-02-17 14:27:31 +08007892 }
7893 ssl->transform = ssl->transform_negotiate;
7894 ssl->transform_negotiate = NULL;
7895
Gilles Peskine449bd832023-01-11 14:50:10 +01007896 MBEDTLS_SSL_DEBUG_MSG(3, ("<= handshake wrapup: final free"));
Jerry Yuaef00152022-02-17 14:27:31 +08007897}
7898
Gilles Peskine449bd832023-01-11 14:50:10 +01007899void mbedtls_ssl_handshake_wrapup(mbedtls_ssl_context *ssl)
Jerry Yu2a9fff52022-02-17 14:28:51 +08007900{
7901 int resume = ssl->handshake->resume;
7902
Gilles Peskine449bd832023-01-11 14:50:10 +01007903 MBEDTLS_SSL_DEBUG_MSG(3, ("=> handshake wrapup"));
Jerry Yu2a9fff52022-02-17 14:28:51 +08007904
7905#if defined(MBEDTLS_SSL_RENEGOTIATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01007906 if (ssl->renego_status == MBEDTLS_SSL_RENEGOTIATION_IN_PROGRESS) {
Jerry Yu2a9fff52022-02-17 14:28:51 +08007907 ssl->renego_status = MBEDTLS_SSL_RENEGOTIATION_DONE;
7908 ssl->renego_records_seen = 0;
7909 }
7910#endif
7911
7912 /*
7913 * Free the previous session and switch in the current one
7914 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007915 if (ssl->session) {
Jerry Yu2a9fff52022-02-17 14:28:51 +08007916#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
7917 /* RFC 7366 3.1: keep the EtM state */
7918 ssl->session_negotiate->encrypt_then_mac =
Gilles Peskine449bd832023-01-11 14:50:10 +01007919 ssl->session->encrypt_then_mac;
Jerry Yu2a9fff52022-02-17 14:28:51 +08007920#endif
7921
Gilles Peskine449bd832023-01-11 14:50:10 +01007922 mbedtls_ssl_session_free(ssl->session);
7923 mbedtls_free(ssl->session);
Jerry Yu2a9fff52022-02-17 14:28:51 +08007924 }
7925 ssl->session = ssl->session_negotiate;
7926 ssl->session_negotiate = NULL;
7927
7928 /*
7929 * Add cache entry
7930 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007931 if (ssl->conf->f_set_cache != NULL &&
Jerry Yu2a9fff52022-02-17 14:28:51 +08007932 ssl->session->id_len != 0 &&
Gilles Peskine449bd832023-01-11 14:50:10 +01007933 resume == 0) {
7934 if (ssl->conf->f_set_cache(ssl->conf->p_cache,
7935 ssl->session->id,
7936 ssl->session->id_len,
7937 ssl->session) != 0) {
7938 MBEDTLS_SSL_DEBUG_MSG(1, ("cache did not store session"));
7939 }
Jerry Yu2a9fff52022-02-17 14:28:51 +08007940 }
7941
7942#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01007943 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
7944 ssl->handshake->flight != NULL) {
Jerry Yu2a9fff52022-02-17 14:28:51 +08007945 /* Cancel handshake timer */
Gilles Peskine449bd832023-01-11 14:50:10 +01007946 mbedtls_ssl_set_timer(ssl, 0);
Jerry Yu2a9fff52022-02-17 14:28:51 +08007947
7948 /* Keep last flight around in case we need to resend it:
7949 * we need the handshake and transform structures for that */
Gilles Peskine449bd832023-01-11 14:50:10 +01007950 MBEDTLS_SSL_DEBUG_MSG(3, ("skip freeing handshake and transform"));
7951 } else
Jerry Yu2a9fff52022-02-17 14:28:51 +08007952#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01007953 mbedtls_ssl_handshake_wrapup_free_hs_transform(ssl);
Jerry Yu2a9fff52022-02-17 14:28:51 +08007954
Jerry Yu5ed73ff2022-10-27 13:08:42 +08007955 ssl->state = MBEDTLS_SSL_HANDSHAKE_OVER;
Jerry Yu2a9fff52022-02-17 14:28:51 +08007956
Gilles Peskine449bd832023-01-11 14:50:10 +01007957 MBEDTLS_SSL_DEBUG_MSG(3, ("<= handshake wrapup"));
Jerry Yu2a9fff52022-02-17 14:28:51 +08007958}
7959
Gilles Peskine449bd832023-01-11 14:50:10 +01007960int mbedtls_ssl_write_finished(mbedtls_ssl_context *ssl)
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007961{
7962 int ret, hash_len;
7963
Gilles Peskine449bd832023-01-11 14:50:10 +01007964 MBEDTLS_SSL_DEBUG_MSG(2, ("=> write finished"));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007965
Gilles Peskine449bd832023-01-11 14:50:10 +01007966 mbedtls_ssl_update_out_pointers(ssl, ssl->transform_negotiate);
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007967
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01007968 ret = ssl->handshake->calc_finished(ssl, ssl->out_msg + 4, ssl->conf->endpoint);
7969 if (ret != 0) {
7970 MBEDTLS_SSL_DEBUG_RET(1, "calc_finished", ret);
7971 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007972
7973 /*
7974 * RFC 5246 7.4.9 (Page 63) says 12 is the default length and ciphersuites
7975 * may define some other value. Currently (early 2016), no defined
7976 * ciphersuite does this (and this is unlikely to change as activity has
7977 * moved to TLS 1.3 now) so we can keep the hardcoded 12 here.
7978 */
7979 hash_len = 12;
7980
7981#if defined(MBEDTLS_SSL_RENEGOTIATION)
7982 ssl->verify_data_len = hash_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01007983 memcpy(ssl->own_verify_data, ssl->out_msg + 4, hash_len);
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007984#endif
7985
7986 ssl->out_msglen = 4 + hash_len;
7987 ssl->out_msgtype = MBEDTLS_SSL_MSG_HANDSHAKE;
7988 ssl->out_msg[0] = MBEDTLS_SSL_HS_FINISHED;
7989
7990 /*
7991 * In case of session resuming, invert the client and server
7992 * ChangeCipherSpec messages order.
7993 */
Gilles Peskine449bd832023-01-11 14:50:10 +01007994 if (ssl->handshake->resume != 0) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007995#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01007996 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007997 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Gilles Peskine449bd832023-01-11 14:50:10 +01007998 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08007999#endif
8000#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008001 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008002 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Gilles Peskine449bd832023-01-11 14:50:10 +01008003 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008004#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01008005 } else {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008006 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01008007 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008008
8009 /*
8010 * Switch to our negotiated transform and session parameters for outbound
8011 * data.
8012 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008013 MBEDTLS_SSL_DEBUG_MSG(3, ("switching to new transform spec for outbound data"));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008014
8015#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01008016 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008017 unsigned char i;
8018
8019 /* Remember current epoch settings for resending */
8020 ssl->handshake->alt_transform_out = ssl->transform_out;
Gilles Peskine449bd832023-01-11 14:50:10 +01008021 memcpy(ssl->handshake->alt_out_ctr, ssl->cur_out_ctr,
8022 sizeof(ssl->handshake->alt_out_ctr));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008023
8024 /* Set sequence_number to zero */
Gilles Peskine449bd832023-01-11 14:50:10 +01008025 memset(&ssl->cur_out_ctr[2], 0, sizeof(ssl->cur_out_ctr) - 2);
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008026
8027
8028 /* Increment epoch */
Gilles Peskine449bd832023-01-11 14:50:10 +01008029 for (i = 2; i > 0; i--) {
8030 if (++ssl->cur_out_ctr[i - 1] != 0) {
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008031 break;
Gilles Peskine449bd832023-01-11 14:50:10 +01008032 }
8033 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008034
8035 /* The loop goes to its end iff the counter is wrapping */
Gilles Peskine449bd832023-01-11 14:50:10 +01008036 if (i == 0) {
8037 MBEDTLS_SSL_DEBUG_MSG(1, ("DTLS epoch would wrap"));
8038 return MBEDTLS_ERR_SSL_COUNTER_WRAPPING;
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008039 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008040 } else
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008041#endif /* MBEDTLS_SSL_PROTO_DTLS */
Gilles Peskine449bd832023-01-11 14:50:10 +01008042 memset(ssl->cur_out_ctr, 0, sizeof(ssl->cur_out_ctr));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008043
8044 ssl->transform_out = ssl->transform_negotiate;
8045 ssl->session_out = ssl->session_negotiate;
8046
8047#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01008048 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
8049 mbedtls_ssl_send_flight_completed(ssl);
8050 }
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008051#endif
8052
Gilles Peskine449bd832023-01-11 14:50:10 +01008053 if ((ret = mbedtls_ssl_write_handshake_msg(ssl)) != 0) {
8054 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_write_handshake_msg", ret);
8055 return ret;
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008056 }
8057
8058#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01008059 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
8060 (ret = mbedtls_ssl_flight_transmit(ssl)) != 0) {
8061 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_flight_transmit", ret);
8062 return ret;
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008063 }
8064#endif
8065
Gilles Peskine449bd832023-01-11 14:50:10 +01008066 MBEDTLS_SSL_DEBUG_MSG(2, ("<= write finished"));
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008067
Gilles Peskine449bd832023-01-11 14:50:10 +01008068 return 0;
Jerry Yu3c8e47b2022-02-17 14:30:01 +08008069}
8070
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008071#define SSL_MAX_HASH_LEN 12
8072
Gilles Peskine449bd832023-01-11 14:50:10 +01008073int mbedtls_ssl_parse_finished(mbedtls_ssl_context *ssl)
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008074{
8075 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
8076 unsigned int hash_len = 12;
8077 unsigned char buf[SSL_MAX_HASH_LEN];
8078
Gilles Peskine449bd832023-01-11 14:50:10 +01008079 MBEDTLS_SSL_DEBUG_MSG(2, ("=> parse finished"));
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008080
Manuel Pégourié-Gonnardb8b07aa2023-02-06 00:34:21 +01008081 ret = ssl->handshake->calc_finished(ssl, buf, ssl->conf->endpoint ^ 1);
8082 if (ret != 0) {
8083 MBEDTLS_SSL_DEBUG_RET(1, "calc_finished", ret);
8084 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008085
Gilles Peskine449bd832023-01-11 14:50:10 +01008086 if ((ret = mbedtls_ssl_read_record(ssl, 1)) != 0) {
8087 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_read_record", ret);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008088 goto exit;
8089 }
8090
Gilles Peskine449bd832023-01-11 14:50:10 +01008091 if (ssl->in_msgtype != MBEDTLS_SSL_MSG_HANDSHAKE) {
8092 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
8093 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8094 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008095 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
8096 goto exit;
8097 }
8098
Gilles Peskine449bd832023-01-11 14:50:10 +01008099 if (ssl->in_msg[0] != MBEDTLS_SSL_HS_FINISHED) {
8100 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8101 MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008102 ret = MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE;
8103 goto exit;
8104 }
8105
Gilles Peskine449bd832023-01-11 14:50:10 +01008106 if (ssl->in_hslen != mbedtls_ssl_hs_hdr_len(ssl) + hash_len) {
8107 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
8108 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8109 MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008110 ret = MBEDTLS_ERR_SSL_DECODE_ERROR;
8111 goto exit;
8112 }
8113
Gilles Peskine449bd832023-01-11 14:50:10 +01008114 if (mbedtls_ct_memcmp(ssl->in_msg + mbedtls_ssl_hs_hdr_len(ssl),
8115 buf, hash_len) != 0) {
8116 MBEDTLS_SSL_DEBUG_MSG(1, ("bad finished message"));
8117 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8118 MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008119 ret = MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
8120 goto exit;
8121 }
8122
8123#if defined(MBEDTLS_SSL_RENEGOTIATION)
8124 ssl->verify_data_len = hash_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008125 memcpy(ssl->peer_verify_data, buf, hash_len);
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008126#endif
8127
Gilles Peskine449bd832023-01-11 14:50:10 +01008128 if (ssl->handshake->resume != 0) {
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008129#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008130 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_CLIENT) {
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008131 ssl->state = MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC;
Gilles Peskine449bd832023-01-11 14:50:10 +01008132 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008133#endif
8134#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008135 if (ssl->conf->endpoint == MBEDTLS_SSL_IS_SERVER) {
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008136 ssl->state = MBEDTLS_SSL_HANDSHAKE_WRAPUP;
Gilles Peskine449bd832023-01-11 14:50:10 +01008137 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008138#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01008139 } else {
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008140 ssl->state++;
Gilles Peskine449bd832023-01-11 14:50:10 +01008141 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008142
8143#if defined(MBEDTLS_SSL_PROTO_DTLS)
Gilles Peskine449bd832023-01-11 14:50:10 +01008144 if (ssl->conf->transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM) {
8145 mbedtls_ssl_recv_flight_completed(ssl);
8146 }
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008147#endif
8148
Gilles Peskine449bd832023-01-11 14:50:10 +01008149 MBEDTLS_SSL_DEBUG_MSG(2, ("<= parse finished"));
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008150
8151exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008152 mbedtls_platform_zeroize(buf, hash_len);
8153 return ret;
Jerry Yu0b3d7c12022-02-17 14:30:51 +08008154}
8155
Jerry Yu392112c2022-02-17 14:34:10 +08008156#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
8157/*
8158 * Helper to get TLS 1.2 PRF from ciphersuite
8159 * (Duplicates bits of logic from ssl_set_handshake_prfs().)
8160 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008161static tls_prf_fn ssl_tls12prf_from_cs(int ciphersuite_id)
Jerry Yu392112c2022-02-17 14:34:10 +08008162{
Jerry Yu392112c2022-02-17 14:34:10 +08008163 const mbedtls_ssl_ciphersuite_t * const ciphersuite_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01008164 mbedtls_ssl_ciphersuite_from_id(ciphersuite_id);
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01008165#if defined(MBEDTLS_MD_CAN_SHA384)
Gilles Peskine449bd832023-01-11 14:50:10 +01008166 if (ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA384) {
8167 return tls_prf_sha384;
8168 } else
Jerry Yu392112c2022-02-17 14:34:10 +08008169#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01008170#if defined(MBEDTLS_MD_CAN_SHA256)
Andrzej Kurek894edde2022-09-29 06:31:14 -04008171 {
Gilles Peskine449bd832023-01-11 14:50:10 +01008172 if (ciphersuite_info != NULL && ciphersuite_info->mac == MBEDTLS_MD_SHA256) {
8173 return tls_prf_sha256;
8174 }
Andrzej Kurek894edde2022-09-29 06:31:14 -04008175 }
8176#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01008177#if !defined(MBEDTLS_MD_CAN_SHA384) && \
8178 !defined(MBEDTLS_MD_CAN_SHA256)
Andrzej Kurek894edde2022-09-29 06:31:14 -04008179 (void) ciphersuite_info;
8180#endif
Andrzej Kurekeabeb302022-10-17 07:52:51 -04008181
Gilles Peskine449bd832023-01-11 14:50:10 +01008182 return NULL;
Jerry Yu392112c2022-02-17 14:34:10 +08008183}
8184#endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
Jerry Yue93ffcd2022-02-17 14:37:06 +08008185
Gilles Peskine449bd832023-01-11 14:50:10 +01008186static mbedtls_tls_prf_types tls_prf_get_type(mbedtls_ssl_tls_prf_cb *tls_prf)
Jerry Yue93ffcd2022-02-17 14:37:06 +08008187{
8188 ((void) tls_prf);
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01008189#if defined(MBEDTLS_MD_CAN_SHA384)
Gilles Peskine449bd832023-01-11 14:50:10 +01008190 if (tls_prf == tls_prf_sha384) {
8191 return MBEDTLS_SSL_TLS_PRF_SHA384;
8192 } else
Jerry Yue93ffcd2022-02-17 14:37:06 +08008193#endif
Manuel Pégourié-Gonnardbef824d2023-03-17 12:50:01 +01008194#if defined(MBEDTLS_MD_CAN_SHA256)
Gilles Peskine449bd832023-01-11 14:50:10 +01008195 if (tls_prf == tls_prf_sha256) {
8196 return MBEDTLS_SSL_TLS_PRF_SHA256;
8197 } else
Jerry Yue93ffcd2022-02-17 14:37:06 +08008198#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01008199 return MBEDTLS_SSL_TLS_PRF_NONE;
Jerry Yue93ffcd2022-02-17 14:37:06 +08008200}
8201
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008202/*
8203 * Populate a transform structure with session keys and all the other
8204 * necessary information.
8205 *
8206 * Parameters:
8207 * - [in/out]: transform: structure to populate
8208 * [in] must be just initialised with mbedtls_ssl_transform_init()
8209 * [out] fully populated, ready for use by mbedtls_ssl_{en,de}crypt_buf()
8210 * - [in] ciphersuite
8211 * - [in] master
8212 * - [in] encrypt_then_mac
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008213 * - [in] tls_prf: pointer to PRF to use for key derivation
8214 * - [in] randbytes: buffer holding ServerHello.random + ClientHello.random
Glenn Strauss07c64162022-03-14 12:34:51 -04008215 * - [in] tls_version: TLS version
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008216 * - [in] endpoint: client or server
8217 * - [in] ssl: used for:
8218 * - ssl->conf->{f,p}_export_keys
8219 * [in] optionally used for:
8220 * - MBEDTLS_DEBUG_C: ssl->conf->{f,p}_dbg
8221 */
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02008222MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01008223static int ssl_tls12_populate_transform(mbedtls_ssl_transform *transform,
8224 int ciphersuite,
8225 const unsigned char master[48],
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008226#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01008227 int encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008228#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01008229 ssl_tls_prf_t tls_prf,
8230 const unsigned char randbytes[64],
8231 mbedtls_ssl_protocol_version tls_version,
8232 unsigned endpoint,
8233 const mbedtls_ssl_context *ssl)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008234{
8235 int ret = 0;
8236 unsigned char keyblk[256];
8237 unsigned char *key1;
8238 unsigned char *key2;
8239 unsigned char *mac_enc;
8240 unsigned char *mac_dec;
8241 size_t mac_key_len = 0;
8242 size_t iv_copy_len;
8243 size_t keylen;
8244 const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008245 mbedtls_ssl_mode_t ssl_mode;
Neil Armstronge4512952022-03-08 09:08:22 +01008246#if !defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008247 const mbedtls_cipher_info_t *cipher_info;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008248 const mbedtls_md_info_t *md_info;
Neil Armstronge4512952022-03-08 09:08:22 +01008249#endif /* !MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008250
8251#if defined(MBEDTLS_USE_PSA_CRYPTO)
8252 psa_key_type_t key_type;
8253 psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
8254 psa_algorithm_t alg;
Neil Armstronge4512952022-03-08 09:08:22 +01008255 psa_algorithm_t mac_alg = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008256 size_t key_bits;
8257 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
8258#endif
8259
8260#if !defined(MBEDTLS_DEBUG_C) && \
8261 !defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
Gilles Peskine449bd832023-01-11 14:50:10 +01008262 if (ssl->f_export_keys == NULL) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008263 ssl = NULL; /* make sure we don't use it except for these cases */
8264 (void) ssl;
8265 }
8266#endif
8267
8268 /*
8269 * Some data just needs copying into the structure
8270 */
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008271#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008272 transform->encrypt_then_mac = encrypt_then_mac;
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008273#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Glenn Strauss07c64162022-03-14 12:34:51 -04008274 transform->tls_version = tls_version;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008275
8276#if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
Gilles Peskine449bd832023-01-11 14:50:10 +01008277 memcpy(transform->randbytes, randbytes, sizeof(transform->randbytes));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008278#endif
8279
8280#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01008281 if (tls_version == MBEDTLS_SSL_VERSION_TLS1_3) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008282 /* At the moment, we keep TLS <= 1.2 and TLS 1.3 transform
8283 * generation separate. This should never happen. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008284 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008285 }
8286#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
8287
8288 /*
8289 * Get various info structures
8290 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008291 ciphersuite_info = mbedtls_ssl_ciphersuite_from_id(ciphersuite);
8292 if (ciphersuite_info == NULL) {
8293 MBEDTLS_SSL_DEBUG_MSG(1, ("ciphersuite info for %d not found",
8294 ciphersuite));
8295 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008296 }
8297
Neil Armstrongab555e02022-04-04 11:07:59 +02008298 ssl_mode = mbedtls_ssl_get_mode_from_ciphersuite(
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008299#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01008300 encrypt_then_mac,
Neil Armstrongf2c82f02022-04-05 11:16:53 +02008301#endif /* MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM */
Gilles Peskine449bd832023-01-11 14:50:10 +01008302 ciphersuite_info);
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008303
Gilles Peskine449bd832023-01-11 14:50:10 +01008304 if (ssl_mode == MBEDTLS_SSL_MODE_AEAD) {
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008305 transform->taglen =
8306 ciphersuite_info->flags & MBEDTLS_CIPHERSUITE_SHORT_TAG ? 8 : 16;
Gilles Peskine449bd832023-01-11 14:50:10 +01008307 }
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008308
8309#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008310 if ((status = mbedtls_ssl_cipher_to_psa(ciphersuite_info->cipher,
8311 transform->taglen,
8312 &alg,
8313 &key_type,
8314 &key_bits)) != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05008315 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01008316 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_ssl_cipher_to_psa", ret);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008317 goto end;
8318 }
8319#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008320 cipher_info = mbedtls_cipher_info_from_type(ciphersuite_info->cipher);
8321 if (cipher_info == NULL) {
8322 MBEDTLS_SSL_DEBUG_MSG(1, ("cipher info for %u not found",
8323 ciphersuite_info->cipher));
8324 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008325 }
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008326#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008327
Neil Armstronge4512952022-03-08 09:08:22 +01008328#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008329 mac_alg = mbedtls_hash_info_psa_from_md(ciphersuite_info->mac);
8330 if (mac_alg == 0) {
8331 MBEDTLS_SSL_DEBUG_MSG(1, ("mbedtls_hash_info_psa_from_md for %u not found",
8332 (unsigned) ciphersuite_info->mac));
8333 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Neil Armstronge4512952022-03-08 09:08:22 +01008334 }
8335#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008336 md_info = mbedtls_md_info_from_type(ciphersuite_info->mac);
8337 if (md_info == NULL) {
8338 MBEDTLS_SSL_DEBUG_MSG(1, ("mbedtls_md info for %u not found",
8339 (unsigned) ciphersuite_info->mac));
8340 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008341 }
Neil Armstronge4512952022-03-08 09:08:22 +01008342#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008343
8344#if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
8345 /* Copy own and peer's CID if the use of the CID
8346 * extension has been negotiated. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008347 if (ssl->handshake->cid_in_use == MBEDTLS_SSL_CID_ENABLED) {
8348 MBEDTLS_SSL_DEBUG_MSG(3, ("Copy CIDs into SSL transform"));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008349
8350 transform->in_cid_len = ssl->own_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008351 memcpy(transform->in_cid, ssl->own_cid, ssl->own_cid_len);
8352 MBEDTLS_SSL_DEBUG_BUF(3, "Incoming CID", transform->in_cid,
8353 transform->in_cid_len);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008354
8355 transform->out_cid_len = ssl->handshake->peer_cid_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01008356 memcpy(transform->out_cid, ssl->handshake->peer_cid,
8357 ssl->handshake->peer_cid_len);
8358 MBEDTLS_SSL_DEBUG_BUF(3, "Outgoing CID", transform->out_cid,
8359 transform->out_cid_len);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008360 }
8361#endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
8362
8363 /*
8364 * Compute key block using the PRF
8365 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008366 ret = tls_prf(master, 48, "key expansion", randbytes, 64, keyblk, 256);
8367 if (ret != 0) {
8368 MBEDTLS_SSL_DEBUG_RET(1, "prf", ret);
8369 return ret;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008370 }
8371
Gilles Peskine449bd832023-01-11 14:50:10 +01008372 MBEDTLS_SSL_DEBUG_MSG(3, ("ciphersuite = %s",
8373 mbedtls_ssl_get_ciphersuite_name(ciphersuite)));
8374 MBEDTLS_SSL_DEBUG_BUF(3, "master secret", master, 48);
8375 MBEDTLS_SSL_DEBUG_BUF(4, "random bytes", randbytes, 64);
8376 MBEDTLS_SSL_DEBUG_BUF(4, "key block", keyblk, 256);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008377
8378 /*
8379 * Determine the appropriate key, IV and MAC length.
8380 */
8381
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008382#if defined(MBEDTLS_USE_PSA_CRYPTO)
8383 keylen = PSA_BITS_TO_BYTES(key_bits);
8384#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008385 keylen = mbedtls_cipher_info_get_key_bitlen(cipher_info) / 8;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008386#endif
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008387
8388#if defined(MBEDTLS_GCM_C) || \
8389 defined(MBEDTLS_CCM_C) || \
8390 defined(MBEDTLS_CHACHAPOLY_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008391 if (ssl_mode == MBEDTLS_SSL_MODE_AEAD) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008392 size_t explicit_ivlen;
8393
8394 transform->maclen = 0;
8395 mac_key_len = 0;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008396
8397 /* All modes haves 96-bit IVs, but the length of the static parts vary
8398 * with mode and version:
8399 * - For GCM and CCM in TLS 1.2, there's a static IV of 4 Bytes
8400 * (to be concatenated with a dynamically chosen IV of 8 Bytes)
8401 * - For ChaChaPoly in TLS 1.2, and all modes in TLS 1.3, there's
8402 * a static IV of 12 Bytes (to be XOR'ed with the 8 Byte record
8403 * sequence number).
8404 */
8405 transform->ivlen = 12;
David Horstmann3b2276a2022-10-06 14:49:08 +01008406
8407 int is_chachapoly = 0;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008408#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008409 is_chachapoly = (key_type == PSA_KEY_TYPE_CHACHA20);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008410#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008411 is_chachapoly = (mbedtls_cipher_info_get_mode(cipher_info)
8412 == MBEDTLS_MODE_CHACHAPOLY);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008413#endif /* MBEDTLS_USE_PSA_CRYPTO */
David Horstmann3b2276a2022-10-06 14:49:08 +01008414
Gilles Peskine449bd832023-01-11 14:50:10 +01008415 if (is_chachapoly) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008416 transform->fixed_ivlen = 12;
Gilles Peskine449bd832023-01-11 14:50:10 +01008417 } else {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008418 transform->fixed_ivlen = 4;
Gilles Peskine449bd832023-01-11 14:50:10 +01008419 }
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008420
8421 /* Minimum length of encrypted record */
8422 explicit_ivlen = transform->ivlen - transform->fixed_ivlen;
8423 transform->minlen = explicit_ivlen + transform->taglen;
Gilles Peskine449bd832023-01-11 14:50:10 +01008424 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008425#endif /* MBEDTLS_GCM_C || MBEDTLS_CCM_C || MBEDTLS_CHACHAPOLY_C */
8426#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01008427 if (ssl_mode == MBEDTLS_SSL_MODE_STREAM ||
Neil Armstrong7fea33e2022-04-01 15:40:25 +02008428 ssl_mode == MBEDTLS_SSL_MODE_CBC ||
Gilles Peskine449bd832023-01-11 14:50:10 +01008429 ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM) {
Neil Armstronge4512952022-03-08 09:08:22 +01008430#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008431 size_t block_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH(key_type);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008432#else
8433 size_t block_size = cipher_info->block_size;
8434#endif /* MBEDTLS_USE_PSA_CRYPTO */
8435
8436#if defined(MBEDTLS_USE_PSA_CRYPTO)
Neil Armstronge4512952022-03-08 09:08:22 +01008437 /* Get MAC length */
8438 mac_key_len = PSA_HASH_LENGTH(mac_alg);
8439#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008440 /* Initialize HMAC contexts */
Gilles Peskine449bd832023-01-11 14:50:10 +01008441 if ((ret = mbedtls_md_setup(&transform->md_ctx_enc, md_info, 1)) != 0 ||
8442 (ret = mbedtls_md_setup(&transform->md_ctx_dec, md_info, 1)) != 0) {
8443 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_setup", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008444 goto end;
8445 }
8446
8447 /* Get MAC length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008448 mac_key_len = mbedtls_md_get_size(md_info);
Neil Armstronge4512952022-03-08 09:08:22 +01008449#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008450 transform->maclen = mac_key_len;
8451
8452 /* IV length */
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008453#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008454 transform->ivlen = PSA_CIPHER_IV_LENGTH(key_type, alg);
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008455#else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008456 transform->ivlen = cipher_info->iv_size;
Neil Armstronga0eeb7f2022-04-01 17:36:10 +02008457#endif /* MBEDTLS_USE_PSA_CRYPTO */
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008458
8459 /* Minimum length */
Gilles Peskine449bd832023-01-11 14:50:10 +01008460 if (ssl_mode == MBEDTLS_SSL_MODE_STREAM) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008461 transform->minlen = transform->maclen;
Gilles Peskine449bd832023-01-11 14:50:10 +01008462 } else {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008463 /*
8464 * GenericBlockCipher:
8465 * 1. if EtM is in use: one block plus MAC
8466 * otherwise: * first multiple of blocklen greater than maclen
8467 * 2. IV
8468 */
8469#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01008470 if (ssl_mode == MBEDTLS_SSL_MODE_CBC_ETM) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008471 transform->minlen = transform->maclen
Gilles Peskine449bd832023-01-11 14:50:10 +01008472 + block_size;
8473 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008474#endif
8475 {
8476 transform->minlen = transform->maclen
Gilles Peskine449bd832023-01-11 14:50:10 +01008477 + block_size
8478 - transform->maclen % block_size;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008479 }
8480
Gilles Peskine449bd832023-01-11 14:50:10 +01008481 if (tls_version == MBEDTLS_SSL_VERSION_TLS1_2) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008482 transform->minlen += transform->ivlen;
Gilles Peskine449bd832023-01-11 14:50:10 +01008483 } else {
8484 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008485 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
8486 goto end;
8487 }
8488 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008489 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008490#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
8491 {
Gilles Peskine449bd832023-01-11 14:50:10 +01008492 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
8493 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008494 }
8495
Gilles Peskine449bd832023-01-11 14:50:10 +01008496 MBEDTLS_SSL_DEBUG_MSG(3, ("keylen: %u, minlen: %u, ivlen: %u, maclen: %u",
8497 (unsigned) keylen,
8498 (unsigned) transform->minlen,
8499 (unsigned) transform->ivlen,
8500 (unsigned) transform->maclen));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008501
8502 /*
8503 * Finally setup the cipher contexts, IVs and MAC secrets.
8504 */
8505#if defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008506 if (endpoint == MBEDTLS_SSL_IS_CLIENT) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008507 key1 = keyblk + mac_key_len * 2;
8508 key2 = keyblk + mac_key_len * 2 + keylen;
8509
8510 mac_enc = keyblk;
8511 mac_dec = keyblk + mac_key_len;
8512
Gilles Peskine449bd832023-01-11 14:50:10 +01008513 iv_copy_len = (transform->fixed_ivlen) ?
8514 transform->fixed_ivlen : transform->ivlen;
8515 memcpy(transform->iv_enc, key2 + keylen, iv_copy_len);
8516 memcpy(transform->iv_dec, key2 + keylen + iv_copy_len,
8517 iv_copy_len);
8518 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008519#endif /* MBEDTLS_SSL_CLI_C */
8520#if defined(MBEDTLS_SSL_SRV_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01008521 if (endpoint == MBEDTLS_SSL_IS_SERVER) {
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008522 key1 = keyblk + mac_key_len * 2 + keylen;
8523 key2 = keyblk + mac_key_len * 2;
8524
8525 mac_enc = keyblk + mac_key_len;
8526 mac_dec = keyblk;
8527
Gilles Peskine449bd832023-01-11 14:50:10 +01008528 iv_copy_len = (transform->fixed_ivlen) ?
8529 transform->fixed_ivlen : transform->ivlen;
8530 memcpy(transform->iv_dec, key1 + keylen, iv_copy_len);
8531 memcpy(transform->iv_enc, key1 + keylen + iv_copy_len,
8532 iv_copy_len);
8533 } else
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008534#endif /* MBEDTLS_SSL_SRV_C */
8535 {
Gilles Peskine449bd832023-01-11 14:50:10 +01008536 MBEDTLS_SSL_DEBUG_MSG(1, ("should never happen"));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008537 ret = MBEDTLS_ERR_SSL_INTERNAL_ERROR;
8538 goto end;
8539 }
8540
Gilles Peskine449bd832023-01-11 14:50:10 +01008541 if (ssl != NULL && ssl->f_export_keys != NULL) {
8542 ssl->f_export_keys(ssl->p_export_keys,
8543 MBEDTLS_SSL_KEY_EXPORT_TLS12_MASTER_SECRET,
8544 master, 48,
8545 randbytes + 32,
8546 randbytes,
8547 tls_prf_get_type(tls_prf));
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008548 }
8549
8550#if defined(MBEDTLS_USE_PSA_CRYPTO)
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008551 transform->psa_alg = alg;
8552
Gilles Peskine449bd832023-01-11 14:50:10 +01008553 if (alg != MBEDTLS_SSL_NULL_CIPHER) {
8554 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_ENCRYPT);
8555 psa_set_key_algorithm(&attributes, alg);
8556 psa_set_key_type(&attributes, key_type);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008557
Gilles Peskine449bd832023-01-11 14:50:10 +01008558 if ((status = psa_import_key(&attributes,
8559 key1,
8560 PSA_BITS_TO_BYTES(key_bits),
8561 &transform->psa_key_enc)) != PSA_SUCCESS) {
8562 MBEDTLS_SSL_DEBUG_RET(3, "psa_import_key", (int) status);
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05008563 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01008564 MBEDTLS_SSL_DEBUG_RET(1, "psa_import_key", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008565 goto end;
8566 }
8567
Gilles Peskine449bd832023-01-11 14:50:10 +01008568 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_DECRYPT);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008569
Gilles Peskine449bd832023-01-11 14:50:10 +01008570 if ((status = psa_import_key(&attributes,
8571 key2,
8572 PSA_BITS_TO_BYTES(key_bits),
8573 &transform->psa_key_dec)) != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05008574 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01008575 MBEDTLS_SSL_DEBUG_RET(1, "psa_import_key", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008576 goto end;
8577 }
8578 }
8579#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008580 if ((ret = mbedtls_cipher_setup(&transform->cipher_ctx_enc,
8581 cipher_info)) != 0) {
8582 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008583 goto end;
8584 }
8585
Gilles Peskine449bd832023-01-11 14:50:10 +01008586 if ((ret = mbedtls_cipher_setup(&transform->cipher_ctx_dec,
8587 cipher_info)) != 0) {
8588 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setup", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008589 goto end;
8590 }
8591
Gilles Peskine449bd832023-01-11 14:50:10 +01008592 if ((ret = mbedtls_cipher_setkey(&transform->cipher_ctx_enc, key1,
8593 (int) mbedtls_cipher_info_get_key_bitlen(cipher_info),
8594 MBEDTLS_ENCRYPT)) != 0) {
8595 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setkey", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008596 goto end;
8597 }
8598
Gilles Peskine449bd832023-01-11 14:50:10 +01008599 if ((ret = mbedtls_cipher_setkey(&transform->cipher_ctx_dec, key2,
8600 (int) mbedtls_cipher_info_get_key_bitlen(cipher_info),
8601 MBEDTLS_DECRYPT)) != 0) {
8602 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_setkey", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008603 goto end;
8604 }
8605
8606#if defined(MBEDTLS_CIPHER_MODE_CBC)
Gilles Peskine449bd832023-01-11 14:50:10 +01008607 if (mbedtls_cipher_info_get_mode(cipher_info) == MBEDTLS_MODE_CBC) {
8608 if ((ret = mbedtls_cipher_set_padding_mode(&transform->cipher_ctx_enc,
8609 MBEDTLS_PADDING_NONE)) != 0) {
8610 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_set_padding_mode", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008611 goto end;
8612 }
8613
Gilles Peskine449bd832023-01-11 14:50:10 +01008614 if ((ret = mbedtls_cipher_set_padding_mode(&transform->cipher_ctx_dec,
8615 MBEDTLS_PADDING_NONE)) != 0) {
8616 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_cipher_set_padding_mode", ret);
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008617 goto end;
8618 }
8619 }
8620#endif /* MBEDTLS_CIPHER_MODE_CBC */
8621#endif /* MBEDTLS_USE_PSA_CRYPTO */
8622
Neil Armstrong29c0c042022-03-17 17:47:28 +01008623#if defined(MBEDTLS_SSL_SOME_SUITES_USE_MAC)
8624 /* For HMAC-based ciphersuites, initialize the HMAC transforms.
8625 For AEAD-based ciphersuites, there is nothing to do here. */
Gilles Peskine449bd832023-01-11 14:50:10 +01008626 if (mac_key_len != 0) {
Neil Armstrong29c0c042022-03-17 17:47:28 +01008627#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008628 transform->psa_mac_alg = PSA_ALG_HMAC(mac_alg);
Neil Armstrong29c0c042022-03-17 17:47:28 +01008629
Gilles Peskine449bd832023-01-11 14:50:10 +01008630 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_SIGN_MESSAGE);
8631 psa_set_key_algorithm(&attributes, PSA_ALG_HMAC(mac_alg));
8632 psa_set_key_type(&attributes, PSA_KEY_TYPE_HMAC);
Neil Armstrong29c0c042022-03-17 17:47:28 +01008633
Gilles Peskine449bd832023-01-11 14:50:10 +01008634 if ((status = psa_import_key(&attributes,
8635 mac_enc, mac_key_len,
8636 &transform->psa_mac_enc)) != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05008637 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01008638 MBEDTLS_SSL_DEBUG_RET(1, "psa_import_mac_key", ret);
Neil Armstrong29c0c042022-03-17 17:47:28 +01008639 goto end;
8640 }
8641
Gilles Peskine449bd832023-01-11 14:50:10 +01008642 if ((transform->psa_alg == MBEDTLS_SSL_NULL_CIPHER) ||
8643 ((transform->psa_alg == PSA_ALG_CBC_NO_PADDING)
Andrzej Kurek8c95ac42022-08-17 16:17:00 -04008644#if defined(MBEDTLS_SSL_SOME_SUITES_USE_CBC_ETM)
Gilles Peskine449bd832023-01-11 14:50:10 +01008645 && (transform->encrypt_then_mac == MBEDTLS_SSL_ETM_DISABLED)
Andrzej Kurek8c95ac42022-08-17 16:17:00 -04008646#endif
Gilles Peskine449bd832023-01-11 14:50:10 +01008647 )) {
Neil Armstrong29c0c042022-03-17 17:47:28 +01008648 /* mbedtls_ct_hmac() requires the key to be exportable */
Gilles Peskine449bd832023-01-11 14:50:10 +01008649 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_EXPORT |
8650 PSA_KEY_USAGE_VERIFY_HASH);
8651 } else {
8652 psa_set_key_usage_flags(&attributes, PSA_KEY_USAGE_VERIFY_HASH);
8653 }
Neil Armstrong29c0c042022-03-17 17:47:28 +01008654
Gilles Peskine449bd832023-01-11 14:50:10 +01008655 if ((status = psa_import_key(&attributes,
8656 mac_dec, mac_key_len,
8657 &transform->psa_mac_dec)) != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05008658 ret = PSA_TO_MBEDTLS_ERR(status);
Gilles Peskine449bd832023-01-11 14:50:10 +01008659 MBEDTLS_SSL_DEBUG_RET(1, "psa_import_mac_key", ret);
Neil Armstrong29c0c042022-03-17 17:47:28 +01008660 goto end;
8661 }
8662#else
Gilles Peskine449bd832023-01-11 14:50:10 +01008663 ret = mbedtls_md_hmac_starts(&transform->md_ctx_enc, mac_enc, mac_key_len);
8664 if (ret != 0) {
Neil Armstrong29c0c042022-03-17 17:47:28 +01008665 goto end;
Gilles Peskine449bd832023-01-11 14:50:10 +01008666 }
8667 ret = mbedtls_md_hmac_starts(&transform->md_ctx_dec, mac_dec, mac_key_len);
8668 if (ret != 0) {
Neil Armstrong29c0c042022-03-17 17:47:28 +01008669 goto end;
Gilles Peskine449bd832023-01-11 14:50:10 +01008670 }
Neil Armstrong29c0c042022-03-17 17:47:28 +01008671#endif /* MBEDTLS_USE_PSA_CRYPTO */
8672 }
8673#endif /* MBEDTLS_SSL_SOME_SUITES_USE_MAC */
8674
8675 ((void) mac_dec);
8676 ((void) mac_enc);
8677
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008678end:
Gilles Peskine449bd832023-01-11 14:50:10 +01008679 mbedtls_platform_zeroize(keyblk, sizeof(keyblk));
8680 return ret;
Jerry Yu9bccc4c2022-02-17 14:38:28 +08008681}
8682
Valerio Settia08b1a42022-11-17 15:10:02 +01008683#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED) && \
8684 defined(MBEDTLS_USE_PSA_CRYPTO)
Valerio Setti6b3dab02022-11-17 17:14:54 +01008685int mbedtls_psa_ecjpake_read_round(
Gilles Peskine449bd832023-01-11 14:50:10 +01008686 psa_pake_operation_t *pake_ctx,
8687 const unsigned char *buf,
8688 size_t len, mbedtls_ecjpake_rounds_t round)
Valerio Settia08b1a42022-11-17 15:10:02 +01008689{
8690 psa_status_t status;
8691 size_t input_offset = 0;
Valerio Setti819de862022-11-17 18:05:19 +01008692 /*
Valerio Setti6b3dab02022-11-17 17:14:54 +01008693 * At round one repeat the KEY_SHARE, ZK_PUBLIC & ZF_PROOF twice
8694 * At round two perform a single cycle
8695 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008696 unsigned int remaining_steps = (round == MBEDTLS_ECJPAKE_ROUND_ONE) ? 2 : 1;
Valerio Settia08b1a42022-11-17 15:10:02 +01008697
Gilles Peskine449bd832023-01-11 14:50:10 +01008698 for (; remaining_steps > 0; remaining_steps--) {
8699 for (psa_pake_step_t step = PSA_PAKE_STEP_KEY_SHARE;
Valerio Settia08b1a42022-11-17 15:10:02 +01008700 step <= PSA_PAKE_STEP_ZK_PROOF;
Gilles Peskine449bd832023-01-11 14:50:10 +01008701 ++step) {
Valerio Settia08b1a42022-11-17 15:10:02 +01008702 /* Length is stored at the first byte */
8703 size_t length = buf[input_offset];
8704 input_offset += 1;
8705
Gilles Peskine449bd832023-01-11 14:50:10 +01008706 if (input_offset + length > len) {
Valerio Settia08b1a42022-11-17 15:10:02 +01008707 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
8708 }
8709
Gilles Peskine449bd832023-01-11 14:50:10 +01008710 status = psa_pake_input(pake_ctx, step,
8711 buf + input_offset, length);
8712 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05008713 return PSA_TO_MBEDTLS_ERR(status);
Valerio Settia08b1a42022-11-17 15:10:02 +01008714 }
8715
8716 input_offset += length;
8717 }
8718 }
8719
Gilles Peskine449bd832023-01-11 14:50:10 +01008720 if (input_offset != len) {
Valerio Setti61ea17d2022-11-18 12:11:00 +01008721 return MBEDTLS_ERR_SSL_HANDSHAKE_FAILURE;
Gilles Peskine449bd832023-01-11 14:50:10 +01008722 }
Valerio Setti30ebe112022-11-17 16:23:34 +01008723
Gilles Peskine449bd832023-01-11 14:50:10 +01008724 return 0;
Valerio Settia08b1a42022-11-17 15:10:02 +01008725}
8726
Valerio Setti6b3dab02022-11-17 17:14:54 +01008727int mbedtls_psa_ecjpake_write_round(
Gilles Peskine449bd832023-01-11 14:50:10 +01008728 psa_pake_operation_t *pake_ctx,
8729 unsigned char *buf,
8730 size_t len, size_t *olen,
8731 mbedtls_ecjpake_rounds_t round)
Valerio Settia08b1a42022-11-17 15:10:02 +01008732{
8733 psa_status_t status;
8734 size_t output_offset = 0;
8735 size_t output_len;
Valerio Setti819de862022-11-17 18:05:19 +01008736 /*
Valerio Setti6b3dab02022-11-17 17:14:54 +01008737 * At round one repeat the KEY_SHARE, ZK_PUBLIC & ZF_PROOF twice
8738 * At round two perform a single cycle
8739 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008740 unsigned int remaining_steps = (round == MBEDTLS_ECJPAKE_ROUND_ONE) ? 2 : 1;
Valerio Settia08b1a42022-11-17 15:10:02 +01008741
Gilles Peskine449bd832023-01-11 14:50:10 +01008742 for (; remaining_steps > 0; remaining_steps--) {
8743 for (psa_pake_step_t step = PSA_PAKE_STEP_KEY_SHARE;
8744 step <= PSA_PAKE_STEP_ZK_PROOF;
8745 ++step) {
Valerio Setti79f6b6b2022-11-21 14:17:03 +01008746 /*
Valerio Settid4a9b1a2022-11-22 11:11:10 +01008747 * For each step, prepend 1 byte with the length of the data as
8748 * given by psa_pake_output().
Valerio Setti79f6b6b2022-11-21 14:17:03 +01008749 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008750 status = psa_pake_output(pake_ctx, step,
8751 buf + output_offset + 1,
8752 len - output_offset - 1,
8753 &output_len);
8754 if (status != PSA_SUCCESS) {
Andrzej Kurek8a045ce2022-12-23 11:00:06 -05008755 return PSA_TO_MBEDTLS_ERR(status);
Valerio Settia08b1a42022-11-17 15:10:02 +01008756 }
8757
Valerio Setti99d88c12022-11-22 16:03:43 +01008758 *(buf + output_offset) = (uint8_t) output_len;
Valerio Setti79f6b6b2022-11-21 14:17:03 +01008759
8760 output_offset += output_len + 1;
Valerio Settia08b1a42022-11-17 15:10:02 +01008761 }
8762 }
8763
8764 *olen = output_offset;
8765
Gilles Peskine449bd832023-01-11 14:50:10 +01008766 return 0;
Valerio Settia08b1a42022-11-17 15:10:02 +01008767}
Valerio Settia08b1a42022-11-17 15:10:02 +01008768#endif //MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED && MBEDTLS_USE_PSA_CRYPTO
8769
Jerry Yuee40f9d2022-02-17 14:55:16 +08008770#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008771int mbedtls_ssl_get_key_exchange_md_tls1_2(mbedtls_ssl_context *ssl,
8772 unsigned char *hash, size_t *hashlen,
8773 unsigned char *data, size_t data_len,
8774 mbedtls_md_type_t md_alg)
Jerry Yuee40f9d2022-02-17 14:55:16 +08008775{
8776 psa_status_t status;
8777 psa_hash_operation_t hash_operation = PSA_HASH_OPERATION_INIT;
Gilles Peskine449bd832023-01-11 14:50:10 +01008778 psa_algorithm_t hash_alg = mbedtls_hash_info_psa_from_md(md_alg);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008779
Gilles Peskine449bd832023-01-11 14:50:10 +01008780 MBEDTLS_SSL_DEBUG_MSG(3, ("Perform PSA-based computation of digest of ServerKeyExchange"));
Jerry Yuee40f9d2022-02-17 14:55:16 +08008781
Gilles Peskine449bd832023-01-11 14:50:10 +01008782 if ((status = psa_hash_setup(&hash_operation,
8783 hash_alg)) != PSA_SUCCESS) {
8784 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_setup", status);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008785 goto exit;
8786 }
8787
Gilles Peskine449bd832023-01-11 14:50:10 +01008788 if ((status = psa_hash_update(&hash_operation, ssl->handshake->randbytes,
8789 64)) != PSA_SUCCESS) {
8790 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_update", status);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008791 goto exit;
8792 }
8793
Gilles Peskine449bd832023-01-11 14:50:10 +01008794 if ((status = psa_hash_update(&hash_operation,
8795 data, data_len)) != PSA_SUCCESS) {
8796 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_update", status);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008797 goto exit;
8798 }
8799
Gilles Peskine449bd832023-01-11 14:50:10 +01008800 if ((status = psa_hash_finish(&hash_operation, hash, PSA_HASH_MAX_SIZE,
8801 hashlen)) != PSA_SUCCESS) {
8802 MBEDTLS_SSL_DEBUG_RET(1, "psa_hash_finish", status);
8803 goto exit;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008804 }
8805
8806exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008807 if (status != PSA_SUCCESS) {
8808 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8809 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
8810 switch (status) {
Jerry Yuee40f9d2022-02-17 14:55:16 +08008811 case PSA_ERROR_NOT_SUPPORTED:
Gilles Peskine449bd832023-01-11 14:50:10 +01008812 return MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008813 case PSA_ERROR_BAD_STATE: /* Intentional fallthrough */
8814 case PSA_ERROR_BUFFER_TOO_SMALL:
Gilles Peskine449bd832023-01-11 14:50:10 +01008815 return MBEDTLS_ERR_MD_BAD_INPUT_DATA;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008816 case PSA_ERROR_INSUFFICIENT_MEMORY:
Gilles Peskine449bd832023-01-11 14:50:10 +01008817 return MBEDTLS_ERR_MD_ALLOC_FAILED;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008818 default:
Gilles Peskine449bd832023-01-11 14:50:10 +01008819 return MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008820 }
8821 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008822 return 0;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008823}
8824
8825#else
8826
Gilles Peskine449bd832023-01-11 14:50:10 +01008827int mbedtls_ssl_get_key_exchange_md_tls1_2(mbedtls_ssl_context *ssl,
8828 unsigned char *hash, size_t *hashlen,
8829 unsigned char *data, size_t data_len,
8830 mbedtls_md_type_t md_alg)
Jerry Yuee40f9d2022-02-17 14:55:16 +08008831{
8832 int ret = 0;
8833 mbedtls_md_context_t ctx;
Gilles Peskine449bd832023-01-11 14:50:10 +01008834 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_type(md_alg);
8835 *hashlen = mbedtls_md_get_size(md_info);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008836
Gilles Peskine449bd832023-01-11 14:50:10 +01008837 MBEDTLS_SSL_DEBUG_MSG(3, ("Perform mbedtls-based computation of digest of ServerKeyExchange"));
Jerry Yuee40f9d2022-02-17 14:55:16 +08008838
Gilles Peskine449bd832023-01-11 14:50:10 +01008839 mbedtls_md_init(&ctx);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008840
8841 /*
8842 * digitally-signed struct {
8843 * opaque client_random[32];
8844 * opaque server_random[32];
8845 * ServerDHParams params;
8846 * };
8847 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008848 if ((ret = mbedtls_md_setup(&ctx, md_info, 0)) != 0) {
8849 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_setup", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008850 goto exit;
8851 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008852 if ((ret = mbedtls_md_starts(&ctx)) != 0) {
8853 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_starts", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008854 goto exit;
8855 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008856 if ((ret = mbedtls_md_update(&ctx, ssl->handshake->randbytes, 64)) != 0) {
8857 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_update", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008858 goto exit;
8859 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008860 if ((ret = mbedtls_md_update(&ctx, data, data_len)) != 0) {
8861 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_update", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008862 goto exit;
8863 }
Gilles Peskine449bd832023-01-11 14:50:10 +01008864 if ((ret = mbedtls_md_finish(&ctx, hash)) != 0) {
8865 MBEDTLS_SSL_DEBUG_RET(1, "mbedtls_md_finish", ret);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008866 goto exit;
8867 }
8868
8869exit:
Gilles Peskine449bd832023-01-11 14:50:10 +01008870 mbedtls_md_free(&ctx);
Jerry Yuee40f9d2022-02-17 14:55:16 +08008871
Gilles Peskine449bd832023-01-11 14:50:10 +01008872 if (ret != 0) {
8873 mbedtls_ssl_send_alert_message(ssl, MBEDTLS_SSL_ALERT_LEVEL_FATAL,
8874 MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR);
8875 }
Jerry Yuee40f9d2022-02-17 14:55:16 +08008876
Gilles Peskine449bd832023-01-11 14:50:10 +01008877 return ret;
Jerry Yuee40f9d2022-02-17 14:55:16 +08008878}
8879#endif /* MBEDTLS_USE_PSA_CRYPTO */
8880
Jerry Yud9d91da2022-02-17 14:57:06 +08008881#if defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
8882
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008883/* Find the preferred hash for a given signature algorithm. */
8884unsigned int mbedtls_ssl_tls12_get_preferred_hash_for_sig_alg(
Gilles Peskine449bd832023-01-11 14:50:10 +01008885 mbedtls_ssl_context *ssl,
8886 unsigned int sig_alg)
Jerry Yud9d91da2022-02-17 14:57:06 +08008887{
Gabor Mezei078e8032022-04-27 21:17:56 +02008888 unsigned int i;
Gabor Mezeia3d016c2022-05-10 12:44:09 +02008889 uint16_t *received_sig_algs = ssl->handshake->received_sig_algs;
Gabor Mezei078e8032022-04-27 21:17:56 +02008890
Gilles Peskine449bd832023-01-11 14:50:10 +01008891 if (sig_alg == MBEDTLS_SSL_SIG_ANON) {
8892 return MBEDTLS_SSL_HASH_NONE;
8893 }
Gabor Mezei078e8032022-04-27 21:17:56 +02008894
Gilles Peskine449bd832023-01-11 14:50:10 +01008895 for (i = 0; received_sig_algs[i] != MBEDTLS_TLS_SIG_NONE; i++) {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008896 unsigned int hash_alg_received =
Gilles Peskine449bd832023-01-11 14:50:10 +01008897 MBEDTLS_SSL_TLS12_HASH_ALG_FROM_SIG_AND_HASH_ALG(
8898 received_sig_algs[i]);
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008899 unsigned int sig_alg_received =
Gilles Peskine449bd832023-01-11 14:50:10 +01008900 MBEDTLS_SSL_TLS12_SIG_ALG_FROM_SIG_AND_HASH_ALG(
8901 received_sig_algs[i]);
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008902
Gilles Peskine449bd832023-01-11 14:50:10 +01008903 if (sig_alg == sig_alg_received) {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008904#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01008905 if (ssl->handshake->key_cert && ssl->handshake->key_cert->key) {
Neil Armstrong96eceb82022-06-30 18:05:05 +02008906 psa_algorithm_t psa_hash_alg =
Gilles Peskine449bd832023-01-11 14:50:10 +01008907 mbedtls_hash_info_psa_from_md(hash_alg_received);
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008908
Gilles Peskine449bd832023-01-11 14:50:10 +01008909 if (sig_alg_received == MBEDTLS_SSL_SIG_ECDSA &&
8910 !mbedtls_pk_can_do_ext(ssl->handshake->key_cert->key,
8911 PSA_ALG_ECDSA(psa_hash_alg),
8912 PSA_KEY_USAGE_SIGN_HASH)) {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008913 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01008914 }
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008915
Gilles Peskine449bd832023-01-11 14:50:10 +01008916 if (sig_alg_received == MBEDTLS_SSL_SIG_RSA &&
8917 !mbedtls_pk_can_do_ext(ssl->handshake->key_cert->key,
8918 PSA_ALG_RSA_PKCS1V15_SIGN(
8919 psa_hash_alg),
8920 PSA_KEY_USAGE_SIGN_HASH)) {
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008921 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01008922 }
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008923 }
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008924#endif /* MBEDTLS_USE_PSA_CRYPTO */
Neil Armstrong96eceb82022-06-30 18:05:05 +02008925
Gilles Peskine449bd832023-01-11 14:50:10 +01008926 return hash_alg_received;
Neil Armstrong9f1176a2022-06-24 18:19:19 +02008927 }
Jerry Yud9d91da2022-02-17 14:57:06 +08008928 }
Jerry Yud9d91da2022-02-17 14:57:06 +08008929
Gilles Peskine449bd832023-01-11 14:50:10 +01008930 return MBEDTLS_SSL_HASH_NONE;
Jerry Yud9d91da2022-02-17 14:57:06 +08008931}
8932
8933#endif /* MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
8934
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008935/* Serialization of TLS 1.2 sessions:
8936 *
8937 * struct {
8938 * uint64 start_time;
8939 * uint8 ciphersuite[2]; // defined by the standard
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008940 * uint8 session_id_len; // at most 32
8941 * opaque session_id[32];
8942 * opaque master[48]; // fixed length in the standard
8943 * uint32 verify_result;
8944 * opaque peer_cert<0..2^24-1>; // length 0 means no peer cert
8945 * opaque ticket<0..2^24-1>; // length 0 means no ticket
8946 * uint32 ticket_lifetime;
8947 * uint8 mfl_code; // up to 255 according to standard
8948 * uint8 encrypt_then_mac; // 0 or 1
8949 * } serialized_session_tls12;
8950 *
8951 */
Gilles Peskine449bd832023-01-11 14:50:10 +01008952static size_t ssl_tls12_session_save(const mbedtls_ssl_session *session,
8953 unsigned char *buf,
8954 size_t buf_len)
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008955{
8956 unsigned char *p = buf;
8957 size_t used = 0;
8958
8959#if defined(MBEDTLS_HAVE_TIME)
8960 uint64_t start;
8961#endif
8962#if defined(MBEDTLS_X509_CRT_PARSE_C)
8963#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
8964 size_t cert_len;
8965#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
8966#endif /* MBEDTLS_X509_CRT_PARSE_C */
8967
8968 /*
8969 * Time
8970 */
8971#if defined(MBEDTLS_HAVE_TIME)
8972 used += 8;
8973
Gilles Peskine449bd832023-01-11 14:50:10 +01008974 if (used <= buf_len) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008975 start = (uint64_t) session->start;
8976
Gilles Peskine449bd832023-01-11 14:50:10 +01008977 MBEDTLS_PUT_UINT64_BE(start, p, 0);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008978 p += 8;
8979 }
8980#endif /* MBEDTLS_HAVE_TIME */
8981
8982 /*
8983 * Basic mandatory fields
8984 */
8985 used += 2 /* ciphersuite */
Gilles Peskine449bd832023-01-11 14:50:10 +01008986 + 1 /* id_len */
8987 + sizeof(session->id)
8988 + sizeof(session->master)
8989 + 4; /* verify_result */
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008990
Gilles Peskine449bd832023-01-11 14:50:10 +01008991 if (used <= buf_len) {
8992 MBEDTLS_PUT_UINT16_BE(session->ciphersuite, p, 0);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008993 p += 2;
8994
Gilles Peskine449bd832023-01-11 14:50:10 +01008995 *p++ = MBEDTLS_BYTE_0(session->id_len);
8996 memcpy(p, session->id, 32);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08008997 p += 32;
8998
Gilles Peskine449bd832023-01-11 14:50:10 +01008999 memcpy(p, session->master, 48);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009000 p += 48;
9001
Gilles Peskine449bd832023-01-11 14:50:10 +01009002 MBEDTLS_PUT_UINT32_BE(session->verify_result, p, 0);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009003 p += 4;
9004 }
9005
9006 /*
9007 * Peer's end-entity certificate
9008 */
9009#if defined(MBEDTLS_X509_CRT_PARSE_C)
9010#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
Gilles Peskine449bd832023-01-11 14:50:10 +01009011 if (session->peer_cert == NULL) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009012 cert_len = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +01009013 } else {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009014 cert_len = session->peer_cert->raw.len;
Gilles Peskine449bd832023-01-11 14:50:10 +01009015 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009016
9017 used += 3 + cert_len;
9018
Gilles Peskine449bd832023-01-11 14:50:10 +01009019 if (used <= buf_len) {
9020 *p++ = MBEDTLS_BYTE_2(cert_len);
9021 *p++ = MBEDTLS_BYTE_1(cert_len);
9022 *p++ = MBEDTLS_BYTE_0(cert_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009023
Gilles Peskine449bd832023-01-11 14:50:10 +01009024 if (session->peer_cert != NULL) {
9025 memcpy(p, session->peer_cert->raw.p, cert_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009026 p += cert_len;
9027 }
9028 }
9029#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
Gilles Peskine449bd832023-01-11 14:50:10 +01009030 if (session->peer_cert_digest != NULL) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009031 used += 1 /* type */ + 1 /* length */ + session->peer_cert_digest_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01009032 if (used <= buf_len) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009033 *p++ = (unsigned char) session->peer_cert_digest_type;
9034 *p++ = (unsigned char) session->peer_cert_digest_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01009035 memcpy(p, session->peer_cert_digest,
9036 session->peer_cert_digest_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009037 p += session->peer_cert_digest_len;
9038 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009039 } else {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009040 used += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01009041 if (used <= buf_len) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009042 *p++ = (unsigned char) MBEDTLS_MD_NONE;
9043 *p++ = 0;
9044 }
9045 }
9046#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9047#endif /* MBEDTLS_X509_CRT_PARSE_C */
9048
9049 /*
9050 * Session ticket if any, plus associated data
9051 */
9052#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
9053 used += 3 + session->ticket_len + 4; /* len + ticket + lifetime */
9054
Gilles Peskine449bd832023-01-11 14:50:10 +01009055 if (used <= buf_len) {
9056 *p++ = MBEDTLS_BYTE_2(session->ticket_len);
9057 *p++ = MBEDTLS_BYTE_1(session->ticket_len);
9058 *p++ = MBEDTLS_BYTE_0(session->ticket_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009059
Gilles Peskine449bd832023-01-11 14:50:10 +01009060 if (session->ticket != NULL) {
9061 memcpy(p, session->ticket, session->ticket_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009062 p += session->ticket_len;
9063 }
9064
Gilles Peskine449bd832023-01-11 14:50:10 +01009065 MBEDTLS_PUT_UINT32_BE(session->ticket_lifetime, p, 0);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009066 p += 4;
9067 }
9068#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
9069
9070 /*
9071 * Misc extension-related info
9072 */
9073#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
9074 used += 1;
9075
Gilles Peskine449bd832023-01-11 14:50:10 +01009076 if (used <= buf_len) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009077 *p++ = session->mfl_code;
Gilles Peskine449bd832023-01-11 14:50:10 +01009078 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009079#endif
9080
9081#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
9082 used += 1;
9083
Gilles Peskine449bd832023-01-11 14:50:10 +01009084 if (used <= buf_len) {
9085 *p++ = MBEDTLS_BYTE_0(session->encrypt_then_mac);
9086 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009087#endif
9088
Gilles Peskine449bd832023-01-11 14:50:10 +01009089 return used;
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009090}
9091
Manuel Pégourié-Gonnarda3115dc2022-06-17 10:52:54 +02009092MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01009093static int ssl_tls12_session_load(mbedtls_ssl_session *session,
9094 const unsigned char *buf,
9095 size_t len)
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009096{
9097#if defined(MBEDTLS_HAVE_TIME)
9098 uint64_t start;
9099#endif
9100#if defined(MBEDTLS_X509_CRT_PARSE_C)
9101#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
9102 size_t cert_len;
9103#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9104#endif /* MBEDTLS_X509_CRT_PARSE_C */
9105
9106 const unsigned char *p = buf;
9107 const unsigned char * const end = buf + len;
9108
9109 /*
9110 * Time
9111 */
9112#if defined(MBEDTLS_HAVE_TIME)
Gilles Peskine449bd832023-01-11 14:50:10 +01009113 if (8 > (size_t) (end - p)) {
9114 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9115 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009116
Gilles Peskine449bd832023-01-11 14:50:10 +01009117 start = ((uint64_t) p[0] << 56) |
9118 ((uint64_t) p[1] << 48) |
9119 ((uint64_t) p[2] << 40) |
9120 ((uint64_t) p[3] << 32) |
9121 ((uint64_t) p[4] << 24) |
9122 ((uint64_t) p[5] << 16) |
9123 ((uint64_t) p[6] << 8) |
9124 ((uint64_t) p[7]);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009125 p += 8;
9126
9127 session->start = (time_t) start;
9128#endif /* MBEDTLS_HAVE_TIME */
9129
9130 /*
9131 * Basic mandatory fields
9132 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009133 if (2 + 1 + 32 + 48 + 4 > (size_t) (end - p)) {
9134 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9135 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009136
Gilles Peskine449bd832023-01-11 14:50:10 +01009137 session->ciphersuite = (p[0] << 8) | p[1];
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009138 p += 2;
9139
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009140 session->id_len = *p++;
Gilles Peskine449bd832023-01-11 14:50:10 +01009141 memcpy(session->id, p, 32);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009142 p += 32;
9143
Gilles Peskine449bd832023-01-11 14:50:10 +01009144 memcpy(session->master, p, 48);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009145 p += 48;
9146
Gilles Peskine449bd832023-01-11 14:50:10 +01009147 session->verify_result = ((uint32_t) p[0] << 24) |
9148 ((uint32_t) p[1] << 16) |
9149 ((uint32_t) p[2] << 8) |
9150 ((uint32_t) p[3]);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009151 p += 4;
9152
9153 /* Immediately clear invalid pointer values that have been read, in case
9154 * we exit early before we replaced them with valid ones. */
9155#if defined(MBEDTLS_X509_CRT_PARSE_C)
9156#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
9157 session->peer_cert = NULL;
9158#else
9159 session->peer_cert_digest = NULL;
9160#endif /* !MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9161#endif /* MBEDTLS_X509_CRT_PARSE_C */
9162#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
9163 session->ticket = NULL;
9164#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
9165
9166 /*
9167 * Peer certificate
9168 */
9169#if defined(MBEDTLS_X509_CRT_PARSE_C)
9170#if defined(MBEDTLS_SSL_KEEP_PEER_CERTIFICATE)
9171 /* Deserialize CRT from the end of the ticket. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009172 if (3 > (size_t) (end - p)) {
9173 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9174 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009175
Gilles Peskine449bd832023-01-11 14:50:10 +01009176 cert_len = (p[0] << 16) | (p[1] << 8) | p[2];
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009177 p += 3;
9178
Gilles Peskine449bd832023-01-11 14:50:10 +01009179 if (cert_len != 0) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009180 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
9181
Gilles Peskine449bd832023-01-11 14:50:10 +01009182 if (cert_len > (size_t) (end - p)) {
9183 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9184 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009185
Gilles Peskine449bd832023-01-11 14:50:10 +01009186 session->peer_cert = mbedtls_calloc(1, sizeof(mbedtls_x509_crt));
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009187
Gilles Peskine449bd832023-01-11 14:50:10 +01009188 if (session->peer_cert == NULL) {
9189 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
9190 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009191
Gilles Peskine449bd832023-01-11 14:50:10 +01009192 mbedtls_x509_crt_init(session->peer_cert);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009193
Gilles Peskine449bd832023-01-11 14:50:10 +01009194 if ((ret = mbedtls_x509_crt_parse_der(session->peer_cert,
9195 p, cert_len)) != 0) {
9196 mbedtls_x509_crt_free(session->peer_cert);
9197 mbedtls_free(session->peer_cert);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009198 session->peer_cert = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01009199 return ret;
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009200 }
9201
9202 p += cert_len;
9203 }
9204#else /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9205 /* Deserialize CRT digest from the end of the ticket. */
Gilles Peskine449bd832023-01-11 14:50:10 +01009206 if (2 > (size_t) (end - p)) {
9207 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9208 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009209
9210 session->peer_cert_digest_type = (mbedtls_md_type_t) *p++;
9211 session->peer_cert_digest_len = (size_t) *p++;
9212
Gilles Peskine449bd832023-01-11 14:50:10 +01009213 if (session->peer_cert_digest_len != 0) {
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009214 const mbedtls_md_info_t *md_info =
Gilles Peskine449bd832023-01-11 14:50:10 +01009215 mbedtls_md_info_from_type(session->peer_cert_digest_type);
9216 if (md_info == NULL) {
9217 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9218 }
9219 if (session->peer_cert_digest_len != mbedtls_md_get_size(md_info)) {
9220 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9221 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009222
Gilles Peskine449bd832023-01-11 14:50:10 +01009223 if (session->peer_cert_digest_len > (size_t) (end - p)) {
9224 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9225 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009226
9227 session->peer_cert_digest =
Gilles Peskine449bd832023-01-11 14:50:10 +01009228 mbedtls_calloc(1, session->peer_cert_digest_len);
9229 if (session->peer_cert_digest == NULL) {
9230 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
9231 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009232
Gilles Peskine449bd832023-01-11 14:50:10 +01009233 memcpy(session->peer_cert_digest, p,
9234 session->peer_cert_digest_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009235 p += session->peer_cert_digest_len;
9236 }
9237#endif /* MBEDTLS_SSL_KEEP_PEER_CERTIFICATE */
9238#endif /* MBEDTLS_X509_CRT_PARSE_C */
9239
9240 /*
9241 * Session ticket and associated data
9242 */
9243#if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01009244 if (3 > (size_t) (end - p)) {
9245 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9246 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009247
Gilles Peskine449bd832023-01-11 14:50:10 +01009248 session->ticket_len = (p[0] << 16) | (p[1] << 8) | p[2];
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009249 p += 3;
9250
Gilles Peskine449bd832023-01-11 14:50:10 +01009251 if (session->ticket_len != 0) {
9252 if (session->ticket_len > (size_t) (end - p)) {
9253 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9254 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009255
Gilles Peskine449bd832023-01-11 14:50:10 +01009256 session->ticket = mbedtls_calloc(1, session->ticket_len);
9257 if (session->ticket == NULL) {
9258 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
9259 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009260
Gilles Peskine449bd832023-01-11 14:50:10 +01009261 memcpy(session->ticket, p, session->ticket_len);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009262 p += session->ticket_len;
9263 }
9264
Gilles Peskine449bd832023-01-11 14:50:10 +01009265 if (4 > (size_t) (end - p)) {
9266 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9267 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009268
Gilles Peskine449bd832023-01-11 14:50:10 +01009269 session->ticket_lifetime = ((uint32_t) p[0] << 24) |
9270 ((uint32_t) p[1] << 16) |
9271 ((uint32_t) p[2] << 8) |
9272 ((uint32_t) p[3]);
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009273 p += 4;
9274#endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
9275
9276 /*
9277 * Misc extension-related info
9278 */
9279#if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
Gilles Peskine449bd832023-01-11 14:50:10 +01009280 if (1 > (size_t) (end - p)) {
9281 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9282 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009283
9284 session->mfl_code = *p++;
9285#endif
9286
9287#if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
Gilles Peskine449bd832023-01-11 14:50:10 +01009288 if (1 > (size_t) (end - p)) {
9289 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9290 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009291
9292 session->encrypt_then_mac = *p++;
9293#endif
9294
9295 /* Done, should have consumed entire buffer */
Gilles Peskine449bd832023-01-11 14:50:10 +01009296 if (p != end) {
9297 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9298 }
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009299
Gilles Peskine449bd832023-01-11 14:50:10 +01009300 return 0;
Jerry Yu4f9e3ef2022-02-17 14:58:27 +08009301}
Jerry Yudc7bd172022-02-17 13:44:15 +08009302#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9303
XiaokangQian75d40ef2022-04-20 11:05:24 +00009304int mbedtls_ssl_validate_ciphersuite(
9305 const mbedtls_ssl_context *ssl,
9306 const mbedtls_ssl_ciphersuite_t *suite_info,
9307 mbedtls_ssl_protocol_version min_tls_version,
Gilles Peskine449bd832023-01-11 14:50:10 +01009308 mbedtls_ssl_protocol_version max_tls_version)
XiaokangQian75d40ef2022-04-20 11:05:24 +00009309{
9310 (void) ssl;
9311
Gilles Peskine449bd832023-01-11 14:50:10 +01009312 if (suite_info == NULL) {
9313 return -1;
9314 }
XiaokangQian75d40ef2022-04-20 11:05:24 +00009315
Gilles Peskine449bd832023-01-11 14:50:10 +01009316 if ((suite_info->min_tls_version > max_tls_version) ||
9317 (suite_info->max_tls_version < min_tls_version)) {
9318 return -1;
XiaokangQian75d40ef2022-04-20 11:05:24 +00009319 }
9320
XiaokangQian060d8672022-04-21 09:24:56 +00009321#if defined(MBEDTLS_SSL_PROTO_TLS1_2) && defined(MBEDTLS_SSL_CLI_C)
XiaokangQian75d40ef2022-04-20 11:05:24 +00009322#if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
Neil Armstrongca7d5062022-05-31 14:43:23 +02009323#if defined(MBEDTLS_USE_PSA_CRYPTO)
Gilles Peskine449bd832023-01-11 14:50:10 +01009324 if (suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
9325 ssl->handshake->psa_pake_ctx_is_ok != 1)
Neil Armstrongca7d5062022-05-31 14:43:23 +02009326#else
Gilles Peskine449bd832023-01-11 14:50:10 +01009327 if (suite_info->key_exchange == MBEDTLS_KEY_EXCHANGE_ECJPAKE &&
9328 mbedtls_ecjpake_check(&ssl->handshake->ecjpake_ctx) != 0)
Neil Armstrongca7d5062022-05-31 14:43:23 +02009329#endif /* MBEDTLS_USE_PSA_CRYPTO */
XiaokangQian75d40ef2022-04-20 11:05:24 +00009330 {
Gilles Peskine449bd832023-01-11 14:50:10 +01009331 return -1;
XiaokangQian75d40ef2022-04-20 11:05:24 +00009332 }
9333#endif
9334
9335 /* Don't suggest PSK-based ciphersuite if no PSK is available. */
9336#if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
Gilles Peskine449bd832023-01-11 14:50:10 +01009337 if (mbedtls_ssl_ciphersuite_uses_psk(suite_info) &&
9338 mbedtls_ssl_conf_has_static_psk(ssl->conf) == 0) {
9339 return -1;
XiaokangQian75d40ef2022-04-20 11:05:24 +00009340 }
9341#endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
9342#endif /* MBEDTLS_SSL_PROTO_TLS1_2 */
9343
Gilles Peskine449bd832023-01-11 14:50:10 +01009344 return 0;
XiaokangQian75d40ef2022-04-20 11:05:24 +00009345}
9346
Ronald Crone68ab4f2022-10-05 12:46:29 +02009347#if defined(MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED)
XiaokangQianeaf36512022-04-24 09:07:44 +00009348/*
9349 * Function for writing a signature algorithm extension.
9350 *
9351 * The `extension_data` field of signature algorithm contains a `SignatureSchemeList`
9352 * value (TLS 1.3 RFC8446):
9353 * enum {
9354 * ....
9355 * ecdsa_secp256r1_sha256( 0x0403 ),
9356 * ecdsa_secp384r1_sha384( 0x0503 ),
9357 * ecdsa_secp521r1_sha512( 0x0603 ),
9358 * ....
9359 * } SignatureScheme;
9360 *
9361 * struct {
9362 * SignatureScheme supported_signature_algorithms<2..2^16-2>;
9363 * } SignatureSchemeList;
9364 *
9365 * The `extension_data` field of signature algorithm contains a `SignatureAndHashAlgorithm`
9366 * value (TLS 1.2 RFC5246):
9367 * enum {
9368 * none(0), md5(1), sha1(2), sha224(3), sha256(4), sha384(5),
9369 * sha512(6), (255)
9370 * } HashAlgorithm;
9371 *
9372 * enum { anonymous(0), rsa(1), dsa(2), ecdsa(3), (255) }
9373 * SignatureAlgorithm;
9374 *
9375 * struct {
9376 * HashAlgorithm hash;
9377 * SignatureAlgorithm signature;
9378 * } SignatureAndHashAlgorithm;
9379 *
9380 * SignatureAndHashAlgorithm
9381 * supported_signature_algorithms<2..2^16-2>;
9382 *
9383 * The TLS 1.3 signature algorithm extension was defined to be a compatible
9384 * generalization of the TLS 1.2 signature algorithm extension.
9385 * `SignatureAndHashAlgorithm` field of TLS 1.2 can be represented by
9386 * `SignatureScheme` field of TLS 1.3
9387 *
9388 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009389int mbedtls_ssl_write_sig_alg_ext(mbedtls_ssl_context *ssl, unsigned char *buf,
9390 const unsigned char *end, size_t *out_len)
XiaokangQianeaf36512022-04-24 09:07:44 +00009391{
9392 unsigned char *p = buf;
9393 unsigned char *supported_sig_alg; /* Start of supported_signature_algorithms */
9394 size_t supported_sig_alg_len = 0; /* Length of supported_signature_algorithms */
9395
9396 *out_len = 0;
9397
Gilles Peskine449bd832023-01-11 14:50:10 +01009398 MBEDTLS_SSL_DEBUG_MSG(3, ("adding signature_algorithms extension"));
XiaokangQianeaf36512022-04-24 09:07:44 +00009399
9400 /* Check if we have space for header and length field:
9401 * - extension_type (2 bytes)
9402 * - extension_data_length (2 bytes)
9403 * - supported_signature_algorithms_length (2 bytes)
9404 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009405 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 6);
XiaokangQianeaf36512022-04-24 09:07:44 +00009406 p += 6;
9407
9408 /*
9409 * Write supported_signature_algorithms
9410 */
9411 supported_sig_alg = p;
Gilles Peskine449bd832023-01-11 14:50:10 +01009412 const uint16_t *sig_alg = mbedtls_ssl_get_sig_algs(ssl);
9413 if (sig_alg == NULL) {
9414 return MBEDTLS_ERR_SSL_BAD_CONFIG;
9415 }
XiaokangQianeaf36512022-04-24 09:07:44 +00009416
Gilles Peskine449bd832023-01-11 14:50:10 +01009417 for (; *sig_alg != MBEDTLS_TLS1_3_SIG_NONE; sig_alg++) {
9418 MBEDTLS_SSL_DEBUG_MSG(3, ("got signature scheme [%x] %s",
9419 *sig_alg,
9420 mbedtls_ssl_sig_alg_to_str(*sig_alg)));
9421 if (!mbedtls_ssl_sig_alg_is_supported(ssl, *sig_alg)) {
XiaokangQianeaf36512022-04-24 09:07:44 +00009422 continue;
Gilles Peskine449bd832023-01-11 14:50:10 +01009423 }
9424 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 2);
9425 MBEDTLS_PUT_UINT16_BE(*sig_alg, p, 0);
XiaokangQianeaf36512022-04-24 09:07:44 +00009426 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01009427 MBEDTLS_SSL_DEBUG_MSG(3, ("sent signature scheme [%x] %s",
9428 *sig_alg,
9429 mbedtls_ssl_sig_alg_to_str(*sig_alg)));
XiaokangQianeaf36512022-04-24 09:07:44 +00009430 }
9431
9432 /* Length of supported_signature_algorithms */
9433 supported_sig_alg_len = p - supported_sig_alg;
Gilles Peskine449bd832023-01-11 14:50:10 +01009434 if (supported_sig_alg_len == 0) {
9435 MBEDTLS_SSL_DEBUG_MSG(1, ("No signature algorithms defined."));
9436 return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
XiaokangQianeaf36512022-04-24 09:07:44 +00009437 }
9438
Gilles Peskine449bd832023-01-11 14:50:10 +01009439 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_SIG_ALG, buf, 0);
9440 MBEDTLS_PUT_UINT16_BE(supported_sig_alg_len + 2, buf, 2);
9441 MBEDTLS_PUT_UINT16_BE(supported_sig_alg_len, buf, 4);
XiaokangQianeaf36512022-04-24 09:07:44 +00009442
XiaokangQianeaf36512022-04-24 09:07:44 +00009443 *out_len = p - buf;
9444
9445#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01009446 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_SIG_ALG);
XiaokangQianeaf36512022-04-24 09:07:44 +00009447#endif /* MBEDTLS_SSL_PROTO_TLS1_3 */
Jerry Yu0c354a22022-08-29 15:25:36 +08009448
Gilles Peskine449bd832023-01-11 14:50:10 +01009449 return 0;
XiaokangQianeaf36512022-04-24 09:07:44 +00009450}
Ronald Crone68ab4f2022-10-05 12:46:29 +02009451#endif /* MBEDTLS_SSL_HANDSHAKE_WITH_CERT_ENABLED */
XiaokangQianeaf36512022-04-24 09:07:44 +00009452
XiaokangQian40a35232022-05-07 09:02:40 +00009453#if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
XiaokangQian9b2b7712022-05-17 02:57:00 +00009454/*
9455 * mbedtls_ssl_parse_server_name_ext
9456 *
9457 * Structure of server_name extension:
9458 *
9459 * enum {
9460 * host_name(0), (255)
9461 * } NameType;
9462 * opaque HostName<1..2^16-1>;
9463 *
9464 * struct {
9465 * NameType name_type;
9466 * select (name_type) {
9467 * case host_name: HostName;
9468 * } name;
9469 * } ServerName;
9470 * struct {
9471 * ServerName server_name_list<1..2^16-1>
9472 * } ServerNameList;
9473 */
Ronald Cronce7d76e2022-07-08 18:56:49 +02009474MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01009475int mbedtls_ssl_parse_server_name_ext(mbedtls_ssl_context *ssl,
9476 const unsigned char *buf,
9477 const unsigned char *end)
XiaokangQian40a35232022-05-07 09:02:40 +00009478{
9479 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
9480 const unsigned char *p = buf;
XiaokangQian9b2b7712022-05-17 02:57:00 +00009481 size_t server_name_list_len, hostname_len;
9482 const unsigned char *server_name_list_end;
XiaokangQian40a35232022-05-07 09:02:40 +00009483
Gilles Peskine449bd832023-01-11 14:50:10 +01009484 MBEDTLS_SSL_DEBUG_MSG(3, ("parse ServerName extension"));
XiaokangQian40a35232022-05-07 09:02:40 +00009485
Gilles Peskine449bd832023-01-11 14:50:10 +01009486 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 2);
9487 server_name_list_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQian40a35232022-05-07 09:02:40 +00009488 p += 2;
9489
Gilles Peskine449bd832023-01-11 14:50:10 +01009490 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, server_name_list_len);
XiaokangQian9b2b7712022-05-17 02:57:00 +00009491 server_name_list_end = p + server_name_list_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01009492 while (p < server_name_list_end) {
9493 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, server_name_list_end, 3);
9494 hostname_len = MBEDTLS_GET_UINT16_BE(p, 1);
9495 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, server_name_list_end,
9496 hostname_len + 3);
XiaokangQian40a35232022-05-07 09:02:40 +00009497
Gilles Peskine449bd832023-01-11 14:50:10 +01009498 if (p[0] == MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME) {
XiaokangQian75fe8c72022-06-15 09:42:45 +00009499 /* sni_name is intended to be used only during the parsing of the
9500 * ClientHello message (it is reset to NULL before the end of
9501 * the message parsing). Thus it is ok to just point to the
9502 * reception buffer and not make a copy of it.
9503 */
XiaokangQianf2a94202022-05-20 06:44:24 +00009504 ssl->handshake->sni_name = p + 3;
9505 ssl->handshake->sni_name_len = hostname_len;
Gilles Peskine449bd832023-01-11 14:50:10 +01009506 if (ssl->conf->f_sni == NULL) {
9507 return 0;
XiaokangQian40a35232022-05-07 09:02:40 +00009508 }
Gilles Peskine449bd832023-01-11 14:50:10 +01009509 ret = ssl->conf->f_sni(ssl->conf->p_sni,
9510 ssl, p + 3, hostname_len);
9511 if (ret != 0) {
9512 MBEDTLS_SSL_DEBUG_RET(1, "ssl_sni_wrapper", ret);
9513 MBEDTLS_SSL_PEND_FATAL_ALERT(MBEDTLS_SSL_ALERT_MSG_UNRECOGNIZED_NAME,
9514 MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME);
9515 return MBEDTLS_ERR_SSL_UNRECOGNIZED_NAME;
9516 }
9517 return 0;
XiaokangQian40a35232022-05-07 09:02:40 +00009518 }
9519
9520 p += hostname_len + 3;
9521 }
9522
Gilles Peskine449bd832023-01-11 14:50:10 +01009523 return 0;
XiaokangQian40a35232022-05-07 09:02:40 +00009524}
9525#endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
9526
XiaokangQianacb39922022-06-17 10:18:48 +00009527#if defined(MBEDTLS_SSL_ALPN)
Ronald Cronce7d76e2022-07-08 18:56:49 +02009528MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +01009529int mbedtls_ssl_parse_alpn_ext(mbedtls_ssl_context *ssl,
9530 const unsigned char *buf,
9531 const unsigned char *end)
XiaokangQianacb39922022-06-17 10:18:48 +00009532{
9533 const unsigned char *p = buf;
XiaokangQianc7403452022-06-23 03:24:12 +00009534 size_t protocol_name_list_len;
XiaokangQian95d5f542022-06-24 02:29:26 +00009535 const unsigned char *protocol_name_list;
9536 const unsigned char *protocol_name_list_end;
XiaokangQianc7403452022-06-23 03:24:12 +00009537 size_t protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009538
9539 /* If ALPN not configured, just ignore the extension */
Gilles Peskine449bd832023-01-11 14:50:10 +01009540 if (ssl->conf->alpn_list == NULL) {
9541 return 0;
9542 }
XiaokangQianacb39922022-06-17 10:18:48 +00009543
9544 /*
XiaokangQianc7403452022-06-23 03:24:12 +00009545 * RFC7301, section 3.1
9546 * opaque ProtocolName<1..2^8-1>;
XiaokangQianacb39922022-06-17 10:18:48 +00009547 *
XiaokangQianc7403452022-06-23 03:24:12 +00009548 * struct {
9549 * ProtocolName protocol_name_list<2..2^16-1>
9550 * } ProtocolNameList;
XiaokangQianacb39922022-06-17 10:18:48 +00009551 */
9552
XiaokangQianc7403452022-06-23 03:24:12 +00009553 /*
XiaokangQian0b776e22022-06-24 09:04:59 +00009554 * protocol_name_list_len 2 bytes
9555 * protocol_name_len 1 bytes
9556 * protocol_name >=1 byte
XiaokangQianc7403452022-06-23 03:24:12 +00009557 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009558 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, 4);
XiaokangQianacb39922022-06-17 10:18:48 +00009559
Gilles Peskine449bd832023-01-11 14:50:10 +01009560 protocol_name_list_len = MBEDTLS_GET_UINT16_BE(p, 0);
XiaokangQianacb39922022-06-17 10:18:48 +00009561 p += 2;
Gilles Peskine449bd832023-01-11 14:50:10 +01009562 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, end, protocol_name_list_len);
XiaokangQian95d5f542022-06-24 02:29:26 +00009563 protocol_name_list = p;
9564 protocol_name_list_end = p + protocol_name_list_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009565
9566 /* Validate peer's list (lengths) */
Gilles Peskine449bd832023-01-11 14:50:10 +01009567 while (p < protocol_name_list_end) {
XiaokangQian95d5f542022-06-24 02:29:26 +00009568 protocol_name_len = *p++;
Gilles Peskine449bd832023-01-11 14:50:10 +01009569 MBEDTLS_SSL_CHK_BUF_READ_PTR(p, protocol_name_list_end,
9570 protocol_name_len);
9571 if (protocol_name_len == 0) {
XiaokangQian95d5f542022-06-24 02:29:26 +00009572 MBEDTLS_SSL_PEND_FATAL_ALERT(
9573 MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER,
Gilles Peskine449bd832023-01-11 14:50:10 +01009574 MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER);
9575 return MBEDTLS_ERR_SSL_ILLEGAL_PARAMETER;
XiaokangQian95d5f542022-06-24 02:29:26 +00009576 }
9577
9578 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009579 }
9580
9581 /* Use our order of preference */
Gilles Peskine449bd832023-01-11 14:50:10 +01009582 for (const char **alpn = ssl->conf->alpn_list; *alpn != NULL; alpn++) {
9583 size_t const alpn_len = strlen(*alpn);
XiaokangQian95d5f542022-06-24 02:29:26 +00009584 p = protocol_name_list;
Gilles Peskine449bd832023-01-11 14:50:10 +01009585 while (p < protocol_name_list_end) {
XiaokangQian95d5f542022-06-24 02:29:26 +00009586 protocol_name_len = *p++;
Gilles Peskine449bd832023-01-11 14:50:10 +01009587 if (protocol_name_len == alpn_len &&
9588 memcmp(p, *alpn, alpn_len) == 0) {
XiaokangQianacb39922022-06-17 10:18:48 +00009589 ssl->alpn_chosen = *alpn;
Gilles Peskine449bd832023-01-11 14:50:10 +01009590 return 0;
XiaokangQianacb39922022-06-17 10:18:48 +00009591 }
XiaokangQian95d5f542022-06-24 02:29:26 +00009592
9593 p += protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009594 }
9595 }
9596
XiaokangQian95d5f542022-06-24 02:29:26 +00009597 /* If we get here, no match was found */
XiaokangQianacb39922022-06-17 10:18:48 +00009598 MBEDTLS_SSL_PEND_FATAL_ALERT(
Gilles Peskine449bd832023-01-11 14:50:10 +01009599 MBEDTLS_SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL,
9600 MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL);
9601 return MBEDTLS_ERR_SSL_NO_APPLICATION_PROTOCOL;
XiaokangQianacb39922022-06-17 10:18:48 +00009602}
9603
Gilles Peskine449bd832023-01-11 14:50:10 +01009604int mbedtls_ssl_write_alpn_ext(mbedtls_ssl_context *ssl,
9605 unsigned char *buf,
9606 unsigned char *end,
9607 size_t *out_len)
XiaokangQianacb39922022-06-17 10:18:48 +00009608{
9609 unsigned char *p = buf;
XiaokangQian95d5f542022-06-24 02:29:26 +00009610 size_t protocol_name_len;
XiaokangQianc7403452022-06-23 03:24:12 +00009611 *out_len = 0;
XiaokangQianacb39922022-06-17 10:18:48 +00009612
Gilles Peskine449bd832023-01-11 14:50:10 +01009613 if (ssl->alpn_chosen == NULL) {
9614 return 0;
XiaokangQianacb39922022-06-17 10:18:48 +00009615 }
9616
Gilles Peskine449bd832023-01-11 14:50:10 +01009617 protocol_name_len = strlen(ssl->alpn_chosen);
9618 MBEDTLS_SSL_CHK_BUF_PTR(p, end, 7 + protocol_name_len);
XiaokangQianacb39922022-06-17 10:18:48 +00009619
Gilles Peskine449bd832023-01-11 14:50:10 +01009620 MBEDTLS_SSL_DEBUG_MSG(3, ("server side, adding alpn extension"));
XiaokangQianacb39922022-06-17 10:18:48 +00009621 /*
9622 * 0 . 1 ext identifier
9623 * 2 . 3 ext length
9624 * 4 . 5 protocol list length
9625 * 6 . 6 protocol name length
9626 * 7 . 7+n protocol name
9627 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009628 MBEDTLS_PUT_UINT16_BE(MBEDTLS_TLS_EXT_ALPN, p, 0);
XiaokangQianacb39922022-06-17 10:18:48 +00009629
XiaokangQian95d5f542022-06-24 02:29:26 +00009630 *out_len = 7 + protocol_name_len;
XiaokangQianacb39922022-06-17 10:18:48 +00009631
Gilles Peskine449bd832023-01-11 14:50:10 +01009632 MBEDTLS_PUT_UINT16_BE(protocol_name_len + 3, p, 2);
9633 MBEDTLS_PUT_UINT16_BE(protocol_name_len + 1, p, 4);
XiaokangQian0b776e22022-06-24 09:04:59 +00009634 /* Note: the length of the chosen protocol has been checked to be less
9635 * than 255 bytes in `mbedtls_ssl_conf_alpn_protocols`.
9636 */
Gilles Peskine449bd832023-01-11 14:50:10 +01009637 p[6] = MBEDTLS_BYTE_0(protocol_name_len);
XiaokangQianacb39922022-06-17 10:18:48 +00009638
Gilles Peskine449bd832023-01-11 14:50:10 +01009639 memcpy(p + 7, ssl->alpn_chosen, protocol_name_len);
Jerry Yub95dd362022-11-08 21:19:34 +08009640
9641#if defined(MBEDTLS_SSL_PROTO_TLS1_3)
Gilles Peskine449bd832023-01-11 14:50:10 +01009642 mbedtls_ssl_tls13_set_hs_sent_ext_mask(ssl, MBEDTLS_TLS_EXT_ALPN);
Jerry Yub95dd362022-11-08 21:19:34 +08009643#endif
9644
Gilles Peskine449bd832023-01-11 14:50:10 +01009645 return 0;
XiaokangQianacb39922022-06-17 10:18:48 +00009646}
9647#endif /* MBEDTLS_SSL_ALPN */
9648
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009649#if defined(MBEDTLS_SSL_PROTO_TLS1_3) && \
Xiaokang Qian03409292022-10-12 02:49:52 +00009650 defined(MBEDTLS_SSL_SESSION_TICKETS) && \
Xiaokang Qianed0620c2022-10-12 06:58:13 +00009651 defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && \
Xiaokang Qianed3afcd2022-10-12 08:31:11 +00009652 defined(MBEDTLS_SSL_CLI_C)
Gilles Peskine449bd832023-01-11 14:50:10 +01009653int mbedtls_ssl_session_set_hostname(mbedtls_ssl_session *session,
9654 const char *hostname)
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009655{
9656 /* Initialize to suppress unnecessary compiler warning */
9657 size_t hostname_len = 0;
9658
9659 /* Check if new hostname is valid before
9660 * making any change to current one */
Gilles Peskine449bd832023-01-11 14:50:10 +01009661 if (hostname != NULL) {
9662 hostname_len = strlen(hostname);
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009663
Gilles Peskine449bd832023-01-11 14:50:10 +01009664 if (hostname_len > MBEDTLS_SSL_MAX_HOST_NAME_LEN) {
9665 return MBEDTLS_ERR_SSL_BAD_INPUT_DATA;
9666 }
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009667 }
9668
9669 /* Now it's clear that we will overwrite the old hostname,
9670 * so we can free it safely */
Gilles Peskine449bd832023-01-11 14:50:10 +01009671 if (session->hostname != NULL) {
9672 mbedtls_platform_zeroize(session->hostname,
9673 strlen(session->hostname));
9674 mbedtls_free(session->hostname);
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009675 }
9676
9677 /* Passing NULL as hostname shall clear the old one */
Gilles Peskine449bd832023-01-11 14:50:10 +01009678 if (hostname == NULL) {
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009679 session->hostname = NULL;
Gilles Peskine449bd832023-01-11 14:50:10 +01009680 } else {
9681 session->hostname = mbedtls_calloc(1, hostname_len + 1);
9682 if (session->hostname == NULL) {
9683 return MBEDTLS_ERR_SSL_ALLOC_FAILED;
9684 }
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009685
Gilles Peskine449bd832023-01-11 14:50:10 +01009686 memcpy(session->hostname, hostname, hostname_len);
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009687 }
9688
Gilles Peskine449bd832023-01-11 14:50:10 +01009689 return 0;
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009690}
Xiaokang Qian03409292022-10-12 02:49:52 +00009691#endif /* MBEDTLS_SSL_PROTO_TLS1_3 &&
9692 MBEDTLS_SSL_SESSION_TICKETS &&
Xiaokang Qianed0620c2022-10-12 06:58:13 +00009693 MBEDTLS_SSL_SERVER_NAME_INDICATION &&
Xiaokang Qianed3afcd2022-10-12 08:31:11 +00009694 MBEDTLS_SSL_CLI_C */
Xiaokang Qiana3b451f2022-10-11 06:20:56 +00009695
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02009696#endif /* MBEDTLS_SSL_TLS_C */